1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
|
package org.jgroups.protocols;
import org.jgroups.*;
import org.jgroups.conf.ProtocolStackConfigurator;
import org.jgroups.conf.ClassConfigurator;
import org.jgroups.protocols.pbcast.GMS;
import org.jgroups.protocols.pbcast.NAKACK;
import org.jgroups.protocols.pbcast.STABLE;
import org.jgroups.stack.ProtocolStack;
import org.jgroups.tests.ChannelTestBase;
import org.jgroups.util.Digest;
import org.jgroups.util.MergeId;
import org.jgroups.util.Util;
import org.testng.annotations.Test;
import org.w3c.dom.Element;
import java.io.File;
import java.net.URL;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Tests the GMS protocol for merging functionality
* @author Bela Ban
*/
@Test(groups={Global.STACK_INDEPENDENT}, sequential=true)
public class GMS_MergeTest extends ChannelTestBase {
static final String simple_props="SHARED_LOOPBACK:PING(timeout=1000):" +
"pbcast.NAKACK(use_mcast_xmit=false;gc_lag=0;log_discard_msgs=false;log_not_found_msgs=false)" +
":UNICAST:pbcast.STABLE(stability_delay=200):pbcast.GMS:FC:FRAG2";
static final String flush_props=simple_props + ":pbcast.FLUSH";
static final short GMS_ID=ClassConfigurator.getProtocolId(GMS.class);
public static void testMergeRequestTimeout() throws Exception {
_testMergeRequestTimeout(simple_props, "testMergeRequestTimeout");
}
public static void testMergeRequestTimeoutWithFlush() throws Exception {
_testMergeRequestTimeout(flush_props, "testMergeRequestTimeoutWithFlush");
}
public static void testSimpleMerge() throws Exception {
_testSimpleMerge(simple_props, "testSimpleMerge");
}
public static void testSimpleMergeWithFlush() throws Exception {
_testSimpleMerge(flush_props, "testSimpleMergeWithFlush");
}
public static void testConcurrentMergeTwoPartitions() throws Exception {
_testConcurrentMergeTwoPartitions(simple_props, "testConcurrentMergeTwoPartitions");
}
public static void testConcurrentMergeTwoPartitionsWithFlush() throws Exception {
_testConcurrentMergeTwoPartitions(flush_props, "testConcurrentMergeTwoPartitionsWithFlush");
}
public static void testConcurrentMergeMultiplePartitions() throws Exception {
_testConcurrentMergeMultiplePartitions(simple_props, "testConcurrentMergeMultiplePartitions");
}
public static void testConcurrentMergeMultiplePartitionsWithFlush() throws Exception {
_testConcurrentMergeMultiplePartitions(flush_props, "testConcurrentMergeMultiplePartitionsWithFlush");
}
public static void testMergeAsymmetricPartitions() throws Exception {
_testMergeAsymmetricPartitions(simple_props, "testMergeAsymmetricPartitions");
}
public static void testMergeAsymmetricPartitionsWithFlush() throws Exception {
_testMergeAsymmetricPartitions(flush_props, "testMergeAsymmetricPartitionsWithFlush");
}
public static void testMergeAsymmetricPartitions2() throws Exception {
_testMergeAsymmetricPartitions2(simple_props, "testMergeAsymmetricPartitions2");
}
public static void testMergeAsymmetricPartitionsWithFlush2() throws Exception {
_testMergeAsymmetricPartitions2(flush_props, "testMergeAsymmetricPartitionsWithFlush2");
}
/**
* Simulates the death of a merge leader after having sent a MERG_REQ. Because there is no MergeView or CANCEL_MERGE
* message, the MergeCanceller has to null merge_id after a timeout
*/
static void _testMergeRequestTimeout(String props, String cluster_name) throws Exception {
JChannel c1=new JChannel(props);
try {
c1.connect(cluster_name);
Message merge_request=new Message();
GMS.GmsHeader hdr=new GMS.GmsHeader(GMS.GmsHeader.MERGE_REQ);
MergeId new_merge_id=MergeId.create(c1.getAddress());
hdr.setMergeId(new_merge_id);
merge_request.putHeader(GMS_ID, hdr);
GMS gms=(GMS)c1.getProtocolStack().findProtocol(GMS.class);
gms.setMergeTimeout(2000);
MergeId merge_id=gms.getMergeId();
assert merge_id == null;
System.out.println("starting merge");
gms.up(new Event(Event.MSG, merge_request));
merge_id=gms.getMergeId();
System.out.println("merge_id = " + merge_id);
assert new_merge_id.equals(merge_id);
long timeout=gms.getMergeTimeout() * 2;
System.out.println("sleeping for " + timeout + " ms, then fetching merge_id: should be null (cancelled by the MergeCanceller)");
long target_time=System.currentTimeMillis() + timeout;
while(System.currentTimeMillis() < target_time) {
merge_id=gms.getMergeId();
if(merge_id == null)
break;
Util.sleep(500);
}
merge_id=gms.getMergeId();
System.out.println("merge_id = " + merge_id);
assert merge_id == null : "MergeCanceller didn't kick in";
}
finally {
Util.close(c1);
}
}
static void _testSimpleMerge(String props, String cluster_name) throws Exception {
JChannel[] channels=null;
try {
channels=create(props, true, cluster_name, "A", "B", "C", "D");
print(channels);
View view=channels[channels.length -1].getView();
assert view.size() == channels.length : "view is " + view;
System.out.println("\ncreating partitions: ");
String[][] partitions=generate(new String[]{"A", "B"}, new String[]{"C", "D"});
createPartitions(channels, partitions);
print(channels);
checkViews(channels, "A", "A", "B");
checkViews(channels, "B", "A", "B");
checkViews(channels, "C", "C", "D");
checkViews(channels, "C", "C", "D");
System.out.println("\ndigests:");
printDigests(channels);
Address leader=determineLeader(channels, "A", "C");
long end_time=System.currentTimeMillis() + 30000;
do {
System.out.println("\n==== injecting merge events into " + leader + " ====");
injectMergeEvent(channels, leader, "A", "C");
Util.sleep(1000);
if(allChannelsHaveViewOf(channels, channels.length))
break;
}
while(end_time > System.currentTimeMillis());
System.out.println("\n");
print(channels);
assertAllChannelsHaveViewOf(channels, channels.length);
System.out.println("\ndigests:");
printDigests(channels);
}
finally {
System.out.println("closing channels");
close(channels);
System.out.println("done");
}
}
static void _testConcurrentMergeTwoPartitions(String props, String cluster_name) throws Exception {
JChannel[] channels=null;
try {
channels=create(props, true, cluster_name, "A", "B", "C", "D");
print(channels);
View view=channels[channels.length -1].getView();
assert view.size() == channels.length : "view is " + view;
System.out.println("\ncreating partitions: ");
String[][] partitions=generate(new String[]{"A", "B"}, new String[]{"C", "D"});
createPartitions(channels, partitions);
print(channels);
checkViews(channels, "A", "A", "B");
checkViews(channels, "B", "A", "B");
checkViews(channels, "C", "C", "D");
checkViews(channels, "D", "C", "D");
long end_time=System.currentTimeMillis() + 30000;
do {
System.out.println("\n==== injecting merge events into A and C concurrently ====");
injectMergeEvent(channels, "C", "A", "C");
injectMergeEvent(channels, "A", "A", "C");
Util.sleep(1000);
if(allChannelsHaveViewOf(channels, 4))
break;
}
while(end_time > System.currentTimeMillis());
System.out.println("\n");
print(channels);
assertAllChannelsHaveViewOf(channels, 4);
}
finally {
close(channels);
}
}
static void _testConcurrentMergeMultiplePartitions(String props, String cluster_name) throws Exception {
JChannel[] channels=null;
try {
channels=create(props, true, cluster_name, "A", "B", "C", "D", "E", "F", "G", "H");
print(channels);
View view=channels[channels.length -1].getView();
assert view.size() == channels.length : "view is " + view;
assertAllChannelsHaveViewOf(channels, 8);
System.out.println("\ncreating partitions: ");
String[][] partitions=generate(new String[]{"A", "B"},
new String[]{"C", "D"},
new String[]{"E", "F"},
new String[]{"G", "H"});
createPartitions(channels, partitions);
print(channels);
checkViews(channels, "A", "A", "B");
checkViews(channels, "B", "A", "B");
checkViews(channels, "C", "C", "D");
checkViews(channels, "D", "C", "D");
checkViews(channels, "E", "E", "F");
checkViews(channels, "F", "E", "F");
checkViews(channels, "G", "G", "H");
checkViews(channels, "H", "G", "H");
long end_time=System.currentTimeMillis() + 30000;
do {
System.out.println("\n==== injecting merge event into A, C, E and G concurrently ====");
injectMergeEvent(channels, "G", "A", "C", "E", "G");
injectMergeEvent(channels, "E", "A", "C", "E", "G");
injectMergeEvent(channels, "A", "A", "C", "E", "G");
injectMergeEvent(channels, "C", "A", "C", "E", "G");
Util.sleep(1000);
if(allChannelsHaveViewOf(channels, 8))
break;
}
while(end_time > System.currentTimeMillis());
print(channels);
assertAllChannelsHaveViewOf(channels, 8);
}
finally {
close(channels);
}
}
/**
* Tests the merge of the following partitions:
* <ul>
* <li>A: {B, A, C}
* <li>B: {B, C}
* <li>C: {B, C}
* </ol>
* JIRA: https://jira.jboss.org/jira/browse/JGRP-1031
* @throws Exception
*/
static void _testMergeAsymmetricPartitions(String props, String cluster_name) throws Exception {
JChannel[] channels=null;
MyReceiver[] receivers;
final int NUM=10;
try {
// use simple IDs for UUIDs, so sorting on merge will NOT change the view order
channels=create(props, true, cluster_name, "B", "A", "C");
receivers=new MyReceiver[channels.length];
for(int i=0; i < channels.length; i++) {
receivers[i]=new MyReceiver(channels[i].getName());
channels[i].setReceiver(receivers[i]);
}
JChannel a=findChannel("A", channels), b=findChannel("B", channels), c=findChannel("C", channels);
print(channels);
View view=channels[channels.length -1].getView();
assert view.size() == channels.length : "view is " + view;
System.out.println("sending " + NUM + " msgs:");
for(int i=0; i < NUM; i++)
for(JChannel ch: channels)
ch.send(null, null, "Number #" + i + " from " + ch.getAddress());
waitForNumMessages(NUM * channels.length, 10000, 1000, receivers);
checkMessages(NUM * channels.length, receivers);
System.out.println("\ncreating partitions: ");
applyView(channels, "A", "B", "A", "C");
applyView(channels, "B", "B", "C");
applyView(channels, "C", "B", "C");
print(channels);
checkViews(channels, "A", "B", "A", "C");
checkViews(channels, "B", "B", "C");
checkViews(channels, "C", "B", "C");
for(MyReceiver receiver: receivers)
receiver.clear();
DISCARD discard=new DISCARD();
discard.addIgnoreMember(b.getAddress());
discard.addIgnoreMember(c.getAddress());
// A should drop all traffic from B or C
a.getProtocolStack().insertProtocol(discard, ProtocolStack.ABOVE, SHARED_LOOPBACK.class);
System.out.println("B and C exchange " + NUM + " messages, A discards them");
for(int i=0; i < NUM; i++)
b.send(null, null, "message #" + i +" from B");
for(int i=0; i < NUM; i++)
c.send(null, null, "message #" + i +" from C");
waitForNumMessages(NUM * 2, 10000, 500, receivers[0], receivers[2]); // A *does* receiver B's and C's messages !
checkMessages(NUM * 2, receivers[0], receivers[2]);
checkMessages(0, receivers[1]);
Digest da=((NAKACK)a.getProtocolStack().findProtocol(NAKACK.class)).getDigest(),
db=((NAKACK)b.getProtocolStack().findProtocol(NAKACK.class)).getDigest(),
dc=((NAKACK)c.getProtocolStack().findProtocol(NAKACK.class)).getDigest();
System.out.println("Digest A: " + da + "\nDigest B: " + db + "\nDigest C: " + dc);
System.out.println("Running stability protocol on B and C now");
// a.getProtocolStack().findProtocol(STABLE.class).setLevel("trace");
// b.getProtocolStack().findProtocol(STABLE.class).setLevel("trace");
// c.getProtocolStack().findProtocol(STABLE.class).setLevel("trace");
for(int i=0; i < 3; i++) {
((STABLE)b.getProtocolStack().findProtocol(STABLE.class)).runMessageGarbageCollection();
((STABLE)c.getProtocolStack().findProtocol(STABLE.class)).runMessageGarbageCollection();
Util.sleep(300);
}
db=((NAKACK)a.getProtocolStack().findProtocol(NAKACK.class)).getDigest();
db=((NAKACK)b.getProtocolStack().findProtocol(NAKACK.class)).getDigest();
dc=((NAKACK)c.getProtocolStack().findProtocol(NAKACK.class)).getDigest();
System.out.println("(after purging)\nDigest A: " + da + "\nDigest B: " + db + "\nDigest C: " + dc);
// now enable traffic reception of B and C on A:
discard.removeIgnoredMember(b.getAddress());
discard.removeIgnoredMember(c.getAddress());
Address leader=b.getAddress();
long end_time=System.currentTimeMillis() + 12000;
do {
System.out.println("\n==== injecting merge event into " + leader + " ====");
injectMergeEvent(channels, leader, "B", "A", "C");
Util.sleep(3000);
if(allChannelsHaveView(channels, b.getView()))
break;
}
while(end_time > System.currentTimeMillis());
System.out.println("\n");
print(channels);
assertAllChannelsHaveView(channels, b.getView());
}
finally {
close(channels);
}
}
/**
* Tests the merge of the following partitions:
* <ul>
* <li>A: {A,B}
* <li>B: {B}
* </ol>
* JIRA: https://jira.jboss.org/jira/browse/JGRP-1031
* @throws Exception
*/
static void _testMergeAsymmetricPartitions2(String props, String cluster_name) throws Exception {
JChannel[] channels=null;
MyReceiver[] receivers;
final int NUM=10;
try {
// use simple IDs for UUIDs, so sorting on merge will NOT change the view order
channels=create(props, true, cluster_name, "A", "B");
receivers=new MyReceiver[channels.length];
for(int i=0; i < channels.length; i++) {
receivers[i]=new MyReceiver(channels[i].getName());
channels[i].setReceiver(receivers[i]);
}
JChannel a=findChannel("A", channels), b=findChannel("B", channels);
print(channels);
View view=channels[channels.length -1].getView();
assert view.size() == channels.length : "view is " + view;
System.out.println("sending " + NUM + " msgs:");
for(int i=0; i < NUM; i++)
for(JChannel ch: channels)
ch.send(null, null, "Number #" + i + " from " + ch.getAddress());
waitForNumMessages(NUM * channels.length, 10000, 1000, receivers);
checkMessages(NUM * channels.length, receivers);
System.out.println("\ncreating partitions: ");
applyView(channels, "B", "B"); // B has view {B}
print(channels);
checkViews(channels, "A", "A", "B"); // A: {A,B}
checkViews(channels, "B", "B"); // B: {B}
for(MyReceiver receiver: receivers)
receiver.clear();
Digest da=((NAKACK)a.getProtocolStack().findProtocol(NAKACK.class)).getDigest();
Digest db=((NAKACK)b.getProtocolStack().findProtocol(NAKACK.class)).getDigest();
System.out.println("(after purging)\nDigest A: " + da + "\nDigest B: " + db);
long end_time=System.currentTimeMillis() + 12000;
do {
System.out.println("\n==== injecting merge event ====");
injectMergeEvent(channels, a.getAddress(), "A", "B");
injectMergeEvent(channels, b.getAddress(), "A", "B");
Util.sleep(3000);
if(allChannelsHaveView(channels, a.getView()))
break;
}
while(end_time > System.currentTimeMillis());
/* long end_time=System.currentTimeMillis() + 12000;
do {
if(allChannelsHaveView(channels, a.getView()))
break;
Util.sleep(3000);
}
while(end_time > System.currentTimeMillis());*/
System.out.println("\n");
print(channels);
assertAllChannelsHaveView(channels, a.getView());
}
finally {
close(channels);
}
}
/**
* First name is the channel name, the rest is the view to be applied
* @param members
*/
private static void applyView(JChannel[] channels, String member, String ... members) throws Exception {
JChannel ch=findChannel(member, channels);
View view=createView(members, channels);
GMS gms=(GMS)ch.getProtocolStack().findProtocol(GMS.class);
gms.installView(view);
}
private static boolean allChannelsHaveViewOf(JChannel[] channels, int count) {
for(JChannel ch: channels) {
if(ch.getView().size() != count)
return false;
}
return true;
}
private static boolean allChannelsHaveView(JChannel[] channels, View view) {
for(JChannel ch: channels) {
if(!ch.getView().equals(view))
return false;
}
return true;
}
private static void assertAllChannelsHaveView(JChannel[] channels, View view) {
for(JChannel ch: channels) {
View v=ch.getView();
assert v.equals(view) : "expected view " + view + " but got " + v + " for channel " + ch.getName();
}
}
private static void assertAllChannelsHaveViewOf(JChannel[] channels, int count) {
for(JChannel ch: channels)
assert ch.getView().size() == count : ch.getName() + " has view " + ch.getView() + " (should have " + count + " mbrs)";
}
private static void close(JChannel[] channels) {
if(channels == null) return;
for(int i=channels.length -1; i >= 0; i--) {
JChannel ch=channels[i];
Util.close(ch);
}
}
/*private static JChannel[] create(String cluster_name, String ... names) throws Exception {
return create(false, cluster_name, names);
}*/
private static JChannel[] create(String props, boolean simple_ids, String cluster_name, String ... names) throws Exception {
JChannel[] retval=new JChannel[names.length];
for(int i=0; i < retval.length; i++) {
JChannel ch;
if(simple_ids) {
ch=new MyChannel(props);
((MyChannel)ch).setId(i+1);
}
else
ch=new JChannel(props);
ch.setName(names[i]);
retval[i]=ch;
ch.connect(cluster_name);
if(i == 0)
Util.sleep(3000);
}
return retval;
}
private static void createPartitions(JChannel[] channels, String[]... partitions) throws Exception {
checkUniqueness(partitions);
List<View> views=new ArrayList<View>(partitions.length);
for(String[] partition: partitions) {
View view=createView(partition, channels);
views.add(view);
}
applyViews(views, channels);
}
private static void injectMergeEvent(JChannel[] channels, String leader, String ... coordinators) {
Address leader_addr=leader != null? findAddress(leader, channels) : determineLeader(channels);
injectMergeEvent(channels, leader_addr, coordinators);
}
private static void injectMergeEvent(JChannel[] channels, Address leader_addr, String ... coordinators) {
Map<Address,View> views=new HashMap<Address,View>();
for(String tmp: coordinators) {
Address coord=findAddress(tmp, channels);
views.put(coord, findView(tmp, channels));
}
JChannel coord=findChannel(leader_addr, channels);
GMS gms=(GMS)coord.getProtocolStack().findProtocol(GMS.class);
gms.setLevel("trace");
gms.up(new Event(Event.MERGE, views));
}
private static Address determineLeader(JChannel[] channels, String ... coords) {
Membership membership=new Membership();
for(String coord: coords)
membership.add(findAddress(coord, channels));
membership.sort();
return membership.elementAt(0);
}
private static String[][] generate(String[] ... partitions) {
String[][] retval=new String[partitions.length][];
System.arraycopy(partitions, 0, retval, 0, partitions.length);
return retval;
}
private static void checkUniqueness(String[] ... partitions) throws Exception {
Set<String> set=new HashSet<String>();
for(String[] partition: partitions) {
for(String tmp: partition) {
if(!set.add(tmp))
throw new Exception("partitions are overlapping: element " + tmp + " is in multiple partitions");
}
}
}
private static View createView(String[] partition, JChannel[] channels) throws Exception {
Vector<Address> members=new Vector<Address>(partition.length);
for(String tmp: partition) {
Address addr=findAddress(tmp, channels);
if(addr == null)
throw new Exception(tmp + " not associated with a channel");
members.add(addr);
}
return new View(members.firstElement(), 10, members);
}
private static void checkViews(JChannel[] channels, String channel_name, String ... members) {
JChannel ch=findChannel(channel_name, channels);
View view=ch.getView();
assert view.size() == members.length : "view is " + view + ", members: " + Arrays.toString(members);
for(String member: members) {
Address addr=findAddress(member, channels);
assert view.getMembers().contains(addr) : "view " + view + " does not contain " + addr;
}
}
private static JChannel findChannel(String tmp, JChannel[] channels) {
for(JChannel ch: channels) {
if(ch.getName().equals(tmp))
return ch;
}
return null;
}
private static JChannel findChannel(Address addr, JChannel[] channels) {
for(JChannel ch: channels) {
if(ch.getAddress().equals(addr))
return ch;
}
return null;
}
private static Address findAddress(String tmp, JChannel[] channels) {
for(JChannel ch: channels) {
if(ch.getName().equals(tmp))
return ch.getAddress();
}
return null;
}
private static View findView(String tmp, JChannel[] channels) {
for(JChannel ch: channels) {
if(ch.getName().equals(tmp))
return ch.getView();
}
return null;
}
private static void applyViews(List<View> views, JChannel[] channels) {
for(View view: views) {
Collection<Address> members=view.getMembers();
for(JChannel ch: channels) {
Address addr=ch.getAddress();
if(members.contains(addr)) {
GMS gms=(GMS)ch.getProtocolStack().findProtocol(GMS.class);
gms.installView(view);
}
}
}
}
private static void print(JChannel[] channels) {
for(JChannel ch: channels) {
System.out.println(ch.getName() + ": " + ch.getView());
}
}
private static void printDigests(JChannel[] channels) {
for(JChannel ch: channels) {
NAKACK nak=(NAKACK)ch.getProtocolStack().findProtocol(NAKACK.class);
Digest digest=nak.getDigest();
System.out.println(ch.getName() + ": " + digest.toStringSorted());
}
}
static void waitForNumMessages(int num_msgs, long timeout, long interval, MyReceiver ... receivers) {
long target_time=System.currentTimeMillis() + timeout;
while(System.currentTimeMillis() < target_time) {
boolean all_received=true;
for(MyReceiver receiver: receivers) {
if(receiver.getNumMsgs() < num_msgs) {
all_received=false;
break;
}
}
if(all_received)
break;
Util.sleep(interval);
}
}
static void checkMessages(int expected, MyReceiver ... receivers) {
for(MyReceiver receiver: receivers)
System.out.println(receiver.name + ": " + receiver.getNumMsgs());
for(MyReceiver receiver: receivers)
assert receiver.getNumMsgs() == expected : "[" + receiver.name + "] expected " + expected +
" msgs, but received " + receiver.getNumMsgs();
}
private static class MyChannel extends JChannel {
protected int id=0;
public MyChannel() throws ChannelException {
super();
}
public MyChannel(File properties) throws ChannelException {
super(properties);
}
public MyChannel(Element properties) throws ChannelException {
super(properties);
}
public MyChannel(URL properties) throws ChannelException {
super(properties);
}
public MyChannel(String properties) throws ChannelException {
super(properties);
}
public MyChannel(ProtocolStackConfigurator configurator) throws ChannelException {
super(configurator);
}
public MyChannel(JChannel ch) throws ChannelException {
super(ch);
}
public void setId(int id) {
this.id=id;
}
protected void setAddress() {
Address old_addr=local_addr;
local_addr=new org.jgroups.util.UUID(id, id);
if(old_addr != null)
down(new Event(Event.REMOVE_ADDRESS, old_addr));
if(name == null || name.length() == 0) // generate a logical name if not set
name=Util.generateLocalName();
if(name != null && name.length() > 0)
org.jgroups.util.UUID.add(local_addr, name);
Event evt=new Event(Event.SET_LOCAL_ADDRESS, local_addr);
down(evt);
if(up_handler != null)
up_handler.up(evt);
}
}
private static class MyReceiver extends ReceiverAdapter {
private final String name;
private AtomicInteger num_msgs=new AtomicInteger(0);
public MyReceiver(String name) {
this.name=name;
}
public int getNumMsgs() {return num_msgs.get();}
public void clear() {num_msgs.set(0);}
public void receive(Message msg) {
num_msgs.incrementAndGet();
}
public void viewAccepted(View new_view) {
System.out.println("[" + name + "] view=" + new_view);
}
}
}
|