Skip to content

Commit

Permalink
Fixed INJECT_VIEWTest by picking unique member names
Browse files Browse the repository at this point in the history
  • Loading branch information
belaban committed Dec 18, 2024
1 parent 0857708 commit f8e291e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 43 deletions.
7 changes: 4 additions & 3 deletions src/org/jgroups/protocols/INJECT_VIEW.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public synchronized void injectView(String newView) {
String[] perNode = newView.split(NODE_VIEWS_SEPARATOR);
String thisNodeAddress = getProtocolStack().getChannel().getAddressAsString();

for( String nodeView : perNode ){
for(String nodeView: perNode) {
if( nodeView.startsWith(thisNodeAddress) ) {
log.info("[channel: %s] Injecting a new view: %s", thisNodeAddress, nodeView);

Expand All @@ -48,7 +48,7 @@ public synchronized void injectView(String newView) {
for( String nodeName : nodeView.split(VIEW_SEPARATOR)[1].split(NAMES_SEPARATOR) ){
for( Map.Entry<Address, String> entry : NameCache.getContents().entrySet() ) {
if( nodeName.equals(entry.getValue()) ){
log.debug("[channel: %s] Found name: <%s> for address: <%s>", entry.getValue(), entry.getKey().toString());
log.debug("[channel: %s] Found name: <%s> for address: <%s>", nodeName, entry.getValue(), entry.getKey().toString());
nodes.add( entry.getKey() );
break;
}
Expand All @@ -58,8 +58,9 @@ public synchronized void injectView(String newView) {
View view = new View( nodes.get(0), viewId, nodes);

GMS gms = getProtocolStack().findProtocol(GMS.class);
log.info("[channel: %s] Injecting view %s", gms.addr(), view);
gms.installView(view);
log.info("[channel: %s] Injection finished of view: %s", thisNodeAddress, nodeView);
log.info("[channel: %s] Injection finished of view: %s", thisNodeAddress, view);
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/org/jgroups/stack/ProtocolStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,6 @@ public void setup(List<ProtocolConfiguration> configs, ProtocolHook afterCreatio

/**
* Adds a protocol at the tail of the protocol list
* @param prot
* @return
* @since 2.11
*/
public ProtocolStack addProtocol(Protocol prot) {
Expand Down
59 changes: 21 additions & 38 deletions tests/junit-functional/org/jgroups/protocols/INJECT_VIEWTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import org.jgroups.util.Util;
import org.testng.annotations.Test;

import java.util.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

/**
Expand Down Expand Up @@ -41,18 +43,17 @@ protected static Protocol[] modify(Protocol[] retval) {
public void testInjectView() throws Exception {
JChannel[] channels=null;
try {
channels=create( "testInjectView", "A", "B", "C");
// the names needs to be unique: INJECT_VIEW uses lookup by string, and this might return an address
// associated with a different test running concurrently in the test suite. See the javadoc of this class
channels=create( "testInjectView", "AX", "BX", "CX");
print(channels);
View view=channels[channels.length -1].getView();
assert view.size() == channels.length : "view is " + view;

String injectionViewString = String.format("%s=%s/%s;%s=%s/%s;%s=%s",
"A","A","B",
"B","B","C",
"C","C");
String injectionViewString = "AX=AX/BX;BX=BX/CX;CX=CX"; // AX: {AX,BX} BX: {BX,CX} CX: {CX}
System.out.println("\ninjecting views: "+injectionViewString);
for(JChannel channel: channels)
channel.getProtocolStack().addProtocol( new INJECT_VIEW());
channel.getProtocolStack().addProtocol( new INJECT_VIEW().level("trace"));
for (JChannel channel: channels) {
INJECT_VIEW iv = channel.getProtocolStack().findProtocol(INJECT_VIEW.class);
iv.injectView(injectionViewString);
Expand All @@ -61,22 +62,21 @@ public void testInjectView() throws Exception {
System.out.println("\nInjected views: "+injectionViewString);
print(channels);
System.out.println("\nchecking views: ");
checkViews(channels, "A", "A", "B");
System.out.println("\nA is OK");
checkViews(channels, "B", "B", "C");
System.out.println("\nB is OK");
checkViews(channels, "C", "C");
System.out.println("\nC is OK");
checkViews(channels, "AX", "AX", "BX");
System.out.println("\nAX is OK");
checkViews(channels, "BX", "BX", "CX");
System.out.println("\nBX is OK");
checkViews(channels, "CX", "CX");
System.out.println("\nCX is OK");

System.out.println("\ndigests:");
printDigests(channels);

Address leader=determineLeader(channels, "A", "B", "C");
Address leader=determineLeader(channels, "AX", "BX", "CX");
long end_time=System.currentTimeMillis() + 30000;
do {
System.out.println("\n==== injecting merge events into " + leader + " ====");
injectMergeEvent(channels, leader, "A", "B", "C");
Util.sleep(1000);
injectMergeEvent(channels, leader, "AX", "BX", "CX");
if(allChannelsHaveViewOf(channels, channels.length))
break;
}
Expand Down Expand Up @@ -127,17 +127,14 @@ protected static void disableTracing(JChannel ... channels) {

private static JChannel[] create(String cluster_name, String ... names) throws Exception {
JChannel[] retval=new JChannel[names.length];

for(int i=0; i < retval.length; i++) {
JChannel ch;
Protocol[] props=getProps();
ch=new JChannel(props);
// ((MyChannel)ch).setId(i+1);
ch.setName(names[i]);
ch=new JChannel(props).name(names[i]);
retval[i]=ch;
ch.connect(cluster_name);
if(i == 0)
Util.sleep(3000);
Util.sleep(1000);
}
return retval;
}
Expand Down Expand Up @@ -167,7 +164,7 @@ private static void checkViews(JChannel[] channels, String channel_name, String
JChannel ch=findChannel(channel_name, channels);
View view=ch.getView();
Util.waitUntil(4000, 200, () -> view.size() == members.length,
() -> "view is " + view + ", members: " + Arrays.toString(members));
() -> "view is " + view + ", expected: " + Arrays.toString(members));
for(String member: members) {
Address addr=findAddress(member, channels);
assert view.getMembers().contains(addr) : "view " + view + " does not contain " + addr;
Expand Down Expand Up @@ -206,23 +203,9 @@ private static View findView(String tmp, JChannel[] channels) {
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=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());
}
for(JChannel ch: channels)
System.out.printf("%s: %s\n", ch.name(), ch.view());
}

private static void printDigests(JChannel[] channels) {
Expand Down

0 comments on commit f8e291e

Please sign in to comment.