use of org.apache.sling.discovery.ClusterView in project sling by apache.
the class AnnouncementRegistryImplTest method testExpiry.
@Test
public void testExpiry() throws InterruptedException, NoSuchFieldException {
ClusterView cluster1 = createCluster(4);
ClusterView cluster2 = createCluster(3);
ClusterView cluster3 = createCluster(5);
ClusterView myCluster = createCluster(slingId);
Announcement ann1 = createAnnouncement(cluster1, 0, true);
Announcement ann2 = createAnnouncement(cluster2, 1, true);
Announcement ann3 = createAnnouncement(cluster3, 1, false);
assertTrue(registry.registerAnnouncement(ann1) != -1);
assertTrue(registry.registerAnnouncement(ann2) != -1);
assertTrue(registry.registerAnnouncement(ann3) != -1);
assertTrue(registry.hasActiveAnnouncement(cluster1.getInstances().get(0).getSlingId()));
assertTrue(registry.hasActiveAnnouncement(cluster2.getInstances().get(1).getSlingId()));
assertTrue(registry.hasActiveAnnouncement(cluster3.getInstances().get(1).getSlingId()));
assertEquals(3, registry.listAnnouncementsInSameCluster(myCluster).size());
assertEquals(3, registry.listLocalAnnouncements().size());
assertEquals(1, registry.listLocalIncomingAnnouncements().size());
{
Announcement testAnn = createAnnouncement(myCluster, 0, false);
assertEquals(1, testAnn.listInstances().size());
registry.addAllExcept(testAnn, myCluster, null);
assertEquals(13, testAnn.listInstances().size());
}
Thread.sleep(10500);
{
Announcement testAnn = createAnnouncement(myCluster, 0, false);
assertEquals(1, testAnn.listInstances().size());
registry.addAllExcept(testAnn, myCluster, null);
assertEquals(13, testAnn.listInstances().size());
}
assertTrue(registry.registerAnnouncement(ann3) != -1);
{
Announcement testAnn = createAnnouncement(myCluster, 0, false);
assertEquals(1, testAnn.listInstances().size());
registry.addAllExcept(testAnn, myCluster, null);
assertEquals(13, testAnn.listInstances().size());
}
registry.checkExpiredAnnouncements();
assertEquals(1, registry.listAnnouncementsInSameCluster(myCluster).size());
assertEquals(1, registry.listLocalAnnouncements().size());
assertEquals(1, registry.listLocalIncomingAnnouncements().size());
assertFalse(registry.hasActiveAnnouncement(cluster1.getInstances().get(0).getSlingId()));
assertFalse(registry.hasActiveAnnouncement(cluster2.getInstances().get(1).getSlingId()));
assertTrue(registry.hasActiveAnnouncement(cluster3.getInstances().get(1).getSlingId()));
{
Announcement testAnn = createAnnouncement(myCluster, 0, false);
assertEquals(1, testAnn.listInstances().size());
registry.addAllExcept(testAnn, myCluster, null);
assertEquals(6, testAnn.listInstances().size());
}
}
use of org.apache.sling.discovery.ClusterView in project sling by apache.
the class DefaultTopologyView method getClusterViews.
/**
* @see org.apache.sling.discovery.TopologyView#getClusterViews()
*/
public Set<ClusterView> getClusterViews() {
Set<ClusterView> result = new HashSet<ClusterView>();
for (Iterator<InstanceDescription> it = instances.iterator(); it.hasNext(); ) {
InstanceDescription instance = it.next();
ClusterView cluster = instance.getClusterView();
if (cluster != null) {
result.add(cluster);
}
}
return new HashSet<ClusterView>(result);
}
use of org.apache.sling.discovery.ClusterView in project sling by apache.
the class Announcement method fromJSON.
/** Create an announcement form json **/
public static Announcement fromJSON(final String topologyAnnouncementJSON) {
JsonObject announcement = jsonReaderFactory.createReader(new StringReader(topologyAnnouncementJSON)).readObject();
final String ownerId = announcement.getString("ownerId");
final int protocolVersion;
if (!announcement.containsKey("protocolVersion")) {
protocolVersion = -1;
} else {
protocolVersion = announcement.getInt("protocolVersion");
}
final Announcement result = new Announcement(ownerId, protocolVersion);
if (announcement.containsKey("created")) {
result.originallyCreatedAt = announcement.getJsonNumber("created").longValue();
}
if (announcement.containsKey("backoffInterval")) {
long backoffInterval = announcement.getJsonNumber("backoffInterval").longValue();
result.backoffInterval = backoffInterval;
}
if (announcement.containsKey("resetBackoff")) {
boolean resetBackoff = announcement.getBoolean("resetBackoff");
result.resetBackoff = resetBackoff;
}
if (announcement.containsKey("loop") && announcement.getBoolean("loop")) {
result.setLoop(true);
return result;
}
if (announcement.containsKey("localClusterView")) {
final String localClusterViewJSON = asJSON(announcement.getJsonObject("localClusterView"));
final ClusterView localClusterView = asClusterView(localClusterViewJSON);
result.setLocalCluster(localClusterView);
}
if (announcement.containsKey("inherited")) {
final Boolean inherited = announcement.getBoolean("inherited");
result.inherited = inherited;
}
if (announcement.containsKey("serverInfo")) {
String serverInfo = announcement.getString("serverInfo");
result.serverInfo = serverInfo;
}
final JsonArray subAnnouncements = announcement.getJsonArray("topologyAnnouncements");
for (int i = 0; i < subAnnouncements.size(); i++) {
String subAnnouncementJSON = subAnnouncements.getString(i);
result.addIncomingTopologyAnnouncement(fromJSON(subAnnouncementJSON));
}
return result;
}
use of org.apache.sling.discovery.ClusterView in project sling by apache.
the class AbstractClusterTest method assertTopology.
private void assertTopology(VirtualInstance instance, SimpleClusterView... assertedClusterViews) {
final TopologyView topology = instance.getDiscoveryService().getTopology();
logger.info("assertTopology: instance " + instance.slingId + " sees topology: " + topology + ", expected: " + assertedClusterViews);
assertNotNull(topology);
if (assertedClusterViews.length != topology.getClusterViews().size()) {
dumpFailureDetails(topology, assertedClusterViews);
fail("instance " + instance.slingId + " expected " + assertedClusterViews.length + ", got: " + topology.getClusterViews().size());
}
final Set<ClusterView> actualClusters = new HashSet<ClusterView>(topology.getClusterViews());
for (int i = 0; i < assertedClusterViews.length; i++) {
final SimpleClusterView assertedClusterView = assertedClusterViews[i];
boolean foundMatch = false;
for (Iterator<ClusterView> it = actualClusters.iterator(); it.hasNext(); ) {
final ClusterView actualClusterView = it.next();
if (matches(assertedClusterView, actualClusterView)) {
it.remove();
foundMatch = true;
break;
}
}
if (!foundMatch) {
dumpFailureDetails(topology, assertedClusterViews);
fail("instance " + instance.slingId + " could not find a match in the topology with instance=" + instance.slingId + " and clusterViews=" + assertedClusterViews.length);
}
}
assertEquals("not all asserted clusterviews are in the actual view with instance=" + instance + " and clusterViews=" + assertedClusterViews, actualClusters.size(), 0);
}
use of org.apache.sling.discovery.ClusterView in project sling by apache.
the class AbstractClusterTest method dumpFailureDetails.
private void dumpFailureDetails(TopologyView topology, SimpleClusterView... assertedClusterViews) {
logger.error("assertTopology: expected: " + assertedClusterViews.length);
for (int j = 0; j < assertedClusterViews.length; j++) {
logger.error("assertTopology: [" + j + "]: " + assertedClusterViews[j].toString());
}
final Set<ClusterView> clusterViews = topology.getClusterViews();
final Set<InstanceDescription> instances = topology.getInstances();
logger.error("assertTopology: actual: " + clusterViews.size() + " clusters with a total of " + instances.size() + " instances");
for (Iterator<ClusterView> it = clusterViews.iterator(); it.hasNext(); ) {
final ClusterView aCluster = it.next();
logger.error("assertTopology: a cluster: " + aCluster.getId());
for (Iterator<InstanceDescription> it2 = aCluster.getInstances().iterator(); it2.hasNext(); ) {
final InstanceDescription id = it2.next();
logger.error("assertTopology: - an instance " + id.getSlingId());
}
}
logger.error("assertTopology: list of all instances: " + instances.size());
for (Iterator<InstanceDescription> it = instances.iterator(); it.hasNext(); ) {
final InstanceDescription id = it.next();
logger.error("assertTopology: - an instance: " + id.getSlingId());
}
}
Aggregations