Search in sources :

Example 1 with ClusterView

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());
    }
}
Also used : ClusterView(org.apache.sling.discovery.ClusterView) DefaultClusterView(org.apache.sling.discovery.commons.providers.DefaultClusterView) Test(org.junit.Test)

Example 2 with ClusterView

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);
}
Also used : LocalClusterView(org.apache.sling.discovery.commons.providers.spi.LocalClusterView) ClusterView(org.apache.sling.discovery.ClusterView) InstanceDescription(org.apache.sling.discovery.InstanceDescription) HashSet(java.util.HashSet)

Example 3 with ClusterView

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;
}
Also used : ClusterView(org.apache.sling.discovery.ClusterView) DefaultClusterView(org.apache.sling.discovery.commons.providers.DefaultClusterView) JsonArray(javax.json.JsonArray) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject)

Example 4 with ClusterView

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);
}
Also used : ClusterView(org.apache.sling.discovery.ClusterView) TopologyView(org.apache.sling.discovery.TopologyView) HashSet(java.util.HashSet)

Example 5 with ClusterView

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());
    }
}
Also used : ClusterView(org.apache.sling.discovery.ClusterView) InstanceDescription(org.apache.sling.discovery.InstanceDescription)

Aggregations

ClusterView (org.apache.sling.discovery.ClusterView)30 InstanceDescription (org.apache.sling.discovery.InstanceDescription)16 TopologyView (org.apache.sling.discovery.TopologyView)8 Announcement (org.apache.sling.discovery.base.connectors.announcement.Announcement)6 DefaultClusterView (org.apache.sling.discovery.commons.providers.DefaultClusterView)6 Test (org.junit.Test)6 Iterator (java.util.Iterator)4 UndefinedClusterViewException (org.apache.sling.discovery.base.commons.UndefinedClusterViewException)4 AnnouncementFilter (org.apache.sling.discovery.base.connectors.announcement.AnnouncementFilter)4 HashSet (java.util.HashSet)3 GZIPOutputStream (java.util.zip.GZIPOutputStream)2 JsonException (javax.json.JsonException)2 CachedAnnouncement (org.apache.sling.discovery.base.connectors.announcement.CachedAnnouncement)2 TopologyConnectorClientInformation (org.apache.sling.discovery.base.connectors.ping.TopologyConnectorClientInformation)2 LocalClusterView (org.apache.sling.discovery.commons.providers.spi.LocalClusterView)2 ClusterSyncHistory (org.apache.sling.discovery.commons.providers.spi.base.ClusterSyncHistory)2 Before (org.junit.Before)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1