Search in sources :

Example 6 with InstanceDescription

use of org.apache.sling.discovery.InstanceDescription in project sling by apache.

the class Announcement method asJSON.

/** convert a clusterview into json **/
private static JsonObject asJSON(final ClusterView clusterView) {
    JsonObjectBuilder obj = Json.createObjectBuilder();
    obj.add("id", clusterView.getId());
    JsonArrayBuilder instancesObj = Json.createArrayBuilder();
    List<InstanceDescription> instances = clusterView.getInstances();
    for (Iterator<InstanceDescription> it = instances.iterator(); it.hasNext(); ) {
        InstanceDescription instanceDescription = it.next();
        instancesObj.add(asJSON(instanceDescription));
    }
    obj.add("instances", instancesObj);
    return obj.build();
}
Also used : DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription) NonLocalInstanceDescription(org.apache.sling.discovery.commons.providers.NonLocalInstanceDescription) InstanceDescription(org.apache.sling.discovery.InstanceDescription) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Example 7 with InstanceDescription

use of org.apache.sling.discovery.InstanceDescription in project sling by apache.

the class InstancesDiff method partitionRetained.

@Nonnull
private Map<String, InstanceDescription> partitionRetained(boolean retainFromNewCollection, boolean propertyChanged) {
    Map<String, InstanceDescription> partition = new HashMap<String, InstanceDescription>();
    for (Map.Entry<String, InstanceDescription> oldEntry : oldInstances.entrySet()) {
        String slingId = oldEntry.getKey();
        InstanceDescription newDescription = newInstances.get(slingId);
        if (newDescription != null) {
            InstanceDescription oldDescription = oldEntry.getValue();
            boolean propertiesSame = newDescription.getProperties().equals(oldDescription.getProperties());
            if ((propertiesSame && !propertyChanged) || (!propertiesSame && propertyChanged)) {
                partition.put(slingId, retainFromNewCollection ? newDescription : oldDescription);
            }
        }
    }
    return partition;
}
Also used : HashMap(java.util.HashMap) InstanceDescription(org.apache.sling.discovery.InstanceDescription) HashMap(java.util.HashMap) Map(java.util.Map) Nonnull(javax.annotation.Nonnull)

Example 8 with InstanceDescription

use of org.apache.sling.discovery.InstanceDescription 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)

Example 9 with InstanceDescription

use of org.apache.sling.discovery.InstanceDescription in project sling by apache.

the class AbstractClusterTest method testConnectorSwitching4139.

/*    ok, this test should do the following:
         * cluster A with instance 1 and instance 2
         * cluster B with instance 3 and instance 4
         * cluster C with instance 5

         * initially, instance3 is pinging instance1, and instance 5 is pinging instance1 as well (MAC hub)
          * that should result in instance3 and 5 to inherit the rest from instance1
         * then simulate load balancer switching from instance1 to instance2 - hence pings go to instance2
         *
         */
//TODO: this takes env 45sec
@Category(Slow.class)
@Test
public void testConnectorSwitching4139() throws Throwable {
    final int MIN_EVENT_DELAY = 1;
    // reset any setup that was done - we start with a different setup than the default one
    tearDown();
    final org.apache.log4j.Logger discoveryLogger = RootLogger.getLogger("org.apache.sling.discovery");
    logLevel = discoveryLogger.getLevel();
    discoveryLogger.setLevel(Level.DEBUG);
    instance1 = newBuilder().setDebugName("instance1").newRepository("/var/discovery/clusterA/", true).setConnectorPingTimeout(10).setConnectorPingInterval(999).setMinEventDelay(MIN_EVENT_DELAY).build();
    instance2 = newBuilder().setDebugName("instance2").useRepositoryOf(instance1).setConnectorPingTimeout(10).setConnectorPingInterval(999).setMinEventDelay(MIN_EVENT_DELAY).build();
    // now launch the remote instance
    instance3 = newBuilder().setDebugName("instance3").newRepository("/var/discovery/clusterB/", false).setConnectorPingTimeout(10).setConnectorPingInterval(999).setMinEventDelay(MIN_EVENT_DELAY).build();
    instance4 = newBuilder().setDebugName("instance4").useRepositoryOf(instance3).setConnectorPingTimeout(10).setConnectorPingInterval(999).setMinEventDelay(MIN_EVENT_DELAY).build();
    instance5 = newBuilder().setDebugName("instance5").newRepository("/var/discovery/clusterC/", false).setConnectorPingTimeout(10).setConnectorPingInterval(999).setMinEventDelay(MIN_EVENT_DELAY).build();
    // join the instances to form a cluster by sending out heartbeats
    runHeartbeatOnceWith(instance1, instance2, instance3, instance4, instance5);
    Thread.sleep(500);
    runHeartbeatOnceWith(instance1, instance2, instance3, instance4, instance5);
    Thread.sleep(500);
    runHeartbeatOnceWith(instance1, instance2, instance3, instance4, instance5);
    Thread.sleep(500);
    assertSameTopology(new SimpleClusterView(instance1, instance2));
    assertSameTopology(new SimpleClusterView(instance3, instance4));
    assertSameTopology(new SimpleClusterView(instance5));
    // create a topology connector from instance3 to instance1
    // -> corresponds to starting to ping
    runHeartbeatOnceWith(instance1, instance2, instance3, instance4, instance5);
    pingConnector(instance3, instance1);
    pingConnector(instance5, instance1);
    Thread.sleep(500);
    runHeartbeatOnceWith(instance1, instance2, instance3, instance4, instance5);
    pingConnector(instance3, instance1);
    pingConnector(instance5, instance1);
    Thread.sleep(500);
    // make asserts on the topology
    logger.info("testConnectorSwitching4139: instance1.slingId=" + instance1.slingId);
    logger.info("testConnectorSwitching4139: instance2.slingId=" + instance2.slingId);
    logger.info("testConnectorSwitching4139: instance3.slingId=" + instance3.slingId);
    logger.info("testConnectorSwitching4139: instance4.slingId=" + instance4.slingId);
    logger.info("testConnectorSwitching4139: instance5.slingId=" + instance5.slingId);
    instance1.dumpRepo();
    assertSameTopology(new SimpleClusterView(instance1, instance2), new SimpleClusterView(instance3, instance4), new SimpleClusterView(instance5));
    // simulate a crash of instance1, resulting in load-balancer to switch the pings
    boolean success = false;
    for (int i = 0; i < 25; i++) {
        // loop for max 25 times, min 20 times
        runHeartbeatOnceWith(instance2, instance3, instance4, instance5);
        final boolean ping1 = pingConnector(instance3, instance2);
        final boolean ping2 = pingConnector(instance5, instance2);
        if (ping1 && ping2) {
            // both pings were fine - hence break
            success = true;
            logger.info("testConnectorSwitching4139: successfully switched all pings to instance2 after " + i + " rounds.");
            if (i < 20) {
                logger.info("testConnectorSwitching4139: min loop cnt not yet reached: i=" + i);
                // 20x1000ms = 20sec max - (vs 10sec timeout) - should be enough for timing out
                Thread.sleep(1000);
                continue;
            }
            break;
        }
        logger.info("testConnectorSwitching4139: looping cos ping1=" + ping1 + ", ping2=" + ping2);
        // 25x1000ms = 25sec max - (vs 10sec timeout)
        Thread.sleep(1000);
    }
    assertTrue(success);
    // one final heartbeat
    runHeartbeatOnceWith(instance2, instance3, instance4, instance5);
    assertTrue(pingConnector(instance3, instance2));
    assertTrue(pingConnector(instance5, instance2));
    instance2.dumpRepo();
    assertSameTopology(new SimpleClusterView(instance2), new SimpleClusterView(instance3, instance4), new SimpleClusterView(instance5));
    // restart instance1, crash instance4
    instance4.stopViewChecker();
    instance1Restarted = newBuilder().setDebugName("instance1").useRepositoryOf(instance2).setConnectorPingTimeout(Integer.MAX_VALUE).setMinEventDelay(1).setSlingId(instance1.getSlingId()).build();
    runHeartbeatOnceWith(instance1Restarted, instance2, instance3, instance5);
    // give these heartbeats/votes some time .. so sleep 2sec (timeout is 10sec, so should be safe)
    Thread.sleep(2000);
    assertTrue(pingConnector(instance3, instance2));
    assertTrue(pingConnector(instance5, instance2));
    success = false;
    for (int i = 0; i < 40; i++) {
        runHeartbeatOnceWith(instance1Restarted, instance2, instance3, instance5);
        instance1.getViewChecker().checkView();
        // since instance3 *can* have an undefined cluster view..
        try {
            pingConnector(instance3, instance2);
        } catch (UndefinedClusterViewException ucve) {
        // ignore
        }
        pingConnector(instance5, instance2);
        final TopologyView topology = instance3.getDiscoveryService().getTopology();
        InstanceDescription i3 = null;
        for (Iterator<InstanceDescription> it = topology.getInstances().iterator(); it.hasNext(); ) {
            final InstanceDescription id = it.next();
            if (id.getSlingId().equals(instance3.slingId)) {
                i3 = id;
                break;
            }
        }
        assertNotNull(i3);
        assertEquals(instance3.slingId, i3.getSlingId());
        final ClusterView i3Cluster = i3.getClusterView();
        final int i3ClusterSize = i3Cluster.getInstances().size();
        if (i3ClusterSize == 1) {
            if (i < 30) {
                logger.info("testConnectorSwitching4139: [2] min loop cnt not yet reached: i=" + i);
                // 30x500ms = 15sec max - (vs 10sec-2sec[sleep] timeout) - should be enough for timing out
                Thread.sleep(500);
                continue;
            }
            success = true;
            logger.info("testConnectorSwitching4139: i3ClusterSize: " + i3ClusterSize + ", i=" + i + " (success)");
            break;
        }
        logger.info("testConnectorSwitching4139: i3ClusterSize: " + i3ClusterSize + ", i=" + i);
        Thread.sleep(500);
    }
    logger.info("testConnectorSwitching4139: instance1Restarted.slingId=" + instance1Restarted.slingId);
    logger.info("testConnectorSwitching4139: instance2.slingId=" + instance2.slingId);
    logger.info("testConnectorSwitching4139: instance3.slingId=" + instance3.slingId);
    logger.info("testConnectorSwitching4139: instance4.slingId=" + instance4.slingId);
    logger.info("testConnectorSwitching4139: instance5.slingId=" + instance5.slingId);
    instance1Restarted.dumpRepo();
    assertTrue(success);
    assertSameTopology(new SimpleClusterView(instance1Restarted, instance2), new SimpleClusterView(instance3), new SimpleClusterView(instance5));
    instance1Restarted.stop();
}
Also used : ClusterView(org.apache.sling.discovery.ClusterView) UndefinedClusterViewException(org.apache.sling.discovery.base.commons.UndefinedClusterViewException) InstanceDescription(org.apache.sling.discovery.InstanceDescription) TopologyView(org.apache.sling.discovery.TopologyView) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 10 with InstanceDescription

use of org.apache.sling.discovery.InstanceDescription in project sling by apache.

the class AbstractDiscoveryServiceTest method isStableTopology.

private boolean isStableTopology(Tester... instances) {
    for (Tester tester : instances) {
        TopologyEvent lastEvent = tester.previousEvent;
        if (lastEvent == null) {
            return false;
        }
        Type type = lastEvent.getType();
        if (type == Type.TOPOLOGY_CHANGED || type == Type.TOPOLOGY_INIT) {
        // fine
        } else {
            return false;
        }
        TopologyView newView = lastEvent.getNewView();
        if (newView == null) {
            return false;
        }
        if (instances.length != newView.getInstances().size()) {
            return false;
        }
        TopologyView t = tester.instance.getDiscoveryService().getTopology();
        if (!t.isCurrent()) {
            return false;
        }
        if (instances.length != t.getInstances().size()) {
            return false;
        }
        for (Tester t2 : instances) {
            boolean foundMatch = false;
            for (InstanceDescription id : t.getInstances()) {
                if (t2.instance.getSlingId().equals(id.getSlingId())) {
                    foundMatch = true;
                    break;
                }
            }
            if (!foundMatch) {
                return false;
            }
        }
    }
    return true;
}
Also used : Type(org.apache.sling.discovery.TopologyEvent.Type) TopologyEvent(org.apache.sling.discovery.TopologyEvent) InstanceDescription(org.apache.sling.discovery.InstanceDescription) TopologyView(org.apache.sling.discovery.TopologyView)

Aggregations

InstanceDescription (org.apache.sling.discovery.InstanceDescription)59 ClusterView (org.apache.sling.discovery.ClusterView)16 DefaultInstanceDescription (org.apache.sling.discovery.commons.providers.DefaultInstanceDescription)11 Map (java.util.Map)10 PersistenceException (org.apache.sling.api.resource.PersistenceException)10 TopologyView (org.apache.sling.discovery.TopologyView)9 HashMap (java.util.HashMap)8 LoginException (org.apache.sling.api.resource.LoginException)8 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)8 Test (org.junit.Test)8 Announcement (org.apache.sling.discovery.base.connectors.announcement.Announcement)7 UndefinedClusterViewException (org.apache.sling.discovery.base.commons.UndefinedClusterViewException)6 DefaultClusterView (org.apache.sling.discovery.commons.providers.DefaultClusterView)6 HashSet (java.util.HashSet)5 Resource (org.apache.sling.api.resource.Resource)5 ValueMap (org.apache.sling.api.resource.ValueMap)5 LocalClusterView (org.apache.sling.discovery.commons.providers.spi.LocalClusterView)5 ArrayList (java.util.ArrayList)4 Iterator (java.util.Iterator)4 LinkedList (java.util.LinkedList)4