-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Fixed sitesUp()/sitesDown() notifications (https://issues.redhat.com/browse/JGRP-2712)
- Loading branch information
Showing
8 changed files
with
190 additions
and
336 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.jgroups.protocols.relay; | ||
|
||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* Maintains the status of sites (up, down, undefined) | ||
* @author Bela Ban | ||
* @since 5.2.17 | ||
*/ | ||
public class SiteStatus { | ||
public enum Status {up,down}; | ||
protected final Map<String,Status> sites=new HashMap<>(); | ||
|
||
/** | ||
* Adds a set of sites to the cache. Returns a set of sites for which notifications should be emitted. For each | ||
* site S, the following happens: | ||
* <pre> | ||
* - S is not present: add a new entry with the given status for S and add S to the return value | ||
* - S is present: if S != status: change the status and add S to the return value, else no-op | ||
* </pre> | ||
* @param sites | ||
* @param status | ||
* @return | ||
*/ | ||
public synchronized Set<String> add(Set<String> sites, Status status) { | ||
Set<String> retval=new HashSet<>(); | ||
for(String site: sites) { | ||
Status s=this.sites.get(site); | ||
if(s == null) { | ||
this.sites.put(site, status); | ||
retval.add(site); | ||
} | ||
else { | ||
if(s != status) { | ||
this.sites.put(site, status); | ||
retval.add(site); | ||
} | ||
} | ||
} | ||
return retval; | ||
} | ||
|
||
public synchronized Status get(String site) { | ||
return this.sites.get(site); | ||
} | ||
|
||
public synchronized SiteStatus clear() { | ||
sites.clear(); | ||
return this; | ||
} | ||
|
||
public String toString() { | ||
return sites.entrySet().stream() | ||
.map(e -> String.format("%s: %s", e.getKey(), e.getValue())).collect(Collectors.joining("\n")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
tests/junit-functional/org/jgroups/tests/SiteStatusTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.jgroups.tests; | ||
|
||
import org.jgroups.Global; | ||
import org.jgroups.protocols.relay.SiteStatus; | ||
import org.jgroups.protocols.relay.SiteStatus.Status; | ||
import org.testng.annotations.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.Set; | ||
|
||
/** | ||
* Tests {@link org.jgroups.protocols.relay.SiteStatus} | ||
* @author Bela Ban | ||
* @since 5.2.17 | ||
*/ | ||
@Test(groups= Global.FUNCTIONAL) | ||
public class SiteStatusTest { | ||
public void testSiteStatus() { | ||
SiteStatus s=new SiteStatus(); | ||
Set<String> res=s.add(Set.of("net1"), Status.up); | ||
assert res.size() == 1 && res.contains("net1"); | ||
Status status=s.get("net1"); | ||
assert status == Status.up; | ||
res=s.add(Set.of("net1"), Status.up); | ||
assert res.isEmpty(); | ||
res=s.add(Set.of("net1"), Status.down); | ||
assert res.size() == 1 && res.contains("net1"); | ||
status=s.get("net1"); | ||
assert status == Status.down; | ||
|
||
status=s.get("hf"); | ||
assert status == null; | ||
} | ||
|
||
public void testSiteStatus2() { | ||
SiteStatus s=new SiteStatus(); | ||
Set<String> retval=s.add(Set.of("hf","net1","net2"),Status.down); | ||
assert retval.size() == 3; | ||
for(String st: Arrays.asList("hf", "net1", "net2")) | ||
assert s.get(st) == Status.down; | ||
retval=s.add(Set.of("net1"), Status.up); | ||
assert retval.size() == 1; | ||
retval=s.add(Set.of("net1", "net2"), Status.up); | ||
assert retval.size() == 1 && retval.contains("net2"); | ||
} | ||
} |