Search in sources :

Example 6 with ClusterView

use of org.apache.sling.discovery.ClusterView 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 7 with ClusterView

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

the class AbstractClusterTest method ping.

private Announcement ping(VirtualInstance to, final Announcement incomingTopologyAnnouncement) throws UndefinedClusterViewException {
    final String slingId = to.slingId;
    final ClusterViewService clusterViewService = to.getClusterViewService();
    final AnnouncementRegistry announcementRegistry = to.getAnnouncementRegistry();
    incomingTopologyAnnouncement.removeInherited(slingId);
    final Announcement replyAnnouncement = new Announcement(slingId);
    long backoffInterval = -1;
    final ClusterView clusterView = clusterViewService.getLocalClusterView();
    if (!incomingTopologyAnnouncement.isCorrectVersion()) {
        fail("incorrect version");
        // never reached
        return null;
    } else if (ClusterViewHelper.contains(clusterView, incomingTopologyAnnouncement.getOwnerId())) {
        fail("loop=true");
        // never reached
        return null;
    } else if (ClusterViewHelper.containsAny(clusterView, incomingTopologyAnnouncement.listInstances())) {
        fail("incoming announcement contains instances that are part of my cluster");
        // never reached
        return null;
    } else {
        backoffInterval = announcementRegistry.registerAnnouncement(incomingTopologyAnnouncement);
        if (backoffInterval == -1) {
            fail("rejecting an announcement from an instance that I already see in my topology: ");
            // never reached
            return null;
        } else {
            // normal, successful case: replying with the part of the topology which this instance sees
            replyAnnouncement.setLocalCluster(clusterView);
            announcementRegistry.addAllExcept(replyAnnouncement, clusterView, new AnnouncementFilter() {

                @Override
                public boolean accept(final String receivingSlingId, Announcement announcement) {
                    if (announcement.getPrimaryKey().equals(incomingTopologyAnnouncement.getPrimaryKey())) {
                        return false;
                    }
                    return true;
                }
            });
            return replyAnnouncement;
        }
    }
}
Also used : ClusterView(org.apache.sling.discovery.ClusterView) Announcement(org.apache.sling.discovery.base.connectors.announcement.Announcement) AnnouncementRegistry(org.apache.sling.discovery.base.connectors.announcement.AnnouncementRegistry) AnnouncementFilter(org.apache.sling.discovery.base.connectors.announcement.AnnouncementFilter) ClusterViewService(org.apache.sling.discovery.base.commons.ClusterViewService)

Example 8 with ClusterView

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

the class TopologyHelper method cloneTopologyView.

public static DefaultTopologyView cloneTopologyView(DefaultTopologyView original) {
    DefaultTopologyView t = new DefaultTopologyView();
    Iterator<ClusterView> it = original.getClusterViews().iterator();
    while (it.hasNext()) {
        DefaultClusterView c = (DefaultClusterView) it.next();
        t.addInstances(clone(c).getInstances());
    }
    return t;
}
Also used : ClusterView(org.apache.sling.discovery.ClusterView) DefaultClusterView(org.apache.sling.discovery.commons.providers.DefaultClusterView) DefaultTopologyView(org.apache.sling.discovery.base.commons.DefaultTopologyView) DefaultClusterView(org.apache.sling.discovery.commons.providers.DefaultClusterView)

Example 9 with ClusterView

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

the class InstancesDiffTest method testIsNotInClusterView.

@Test
public void testIsNotInClusterView() throws Exception {
    ClusterView clusterView = clusterView("yet-another-view");
    Collection<InstanceDescription> instances = new InstancesDiff(Arrays.asList(new Instance("one", true, false, Collections.<String, String>emptyMap(), "otherView"), new Instance("two", false, true, Collections.<String, String>emptyMap(), "viewId")), empty()).all(true).isInClusterView(clusterView).get();
    TestCase.assertEquals(0, instances.size());
}
Also used : ClusterView(org.apache.sling.discovery.ClusterView) InstanceDescription(org.apache.sling.discovery.InstanceDescription) Test(org.junit.Test)

Example 10 with ClusterView

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

the class TestInitDelayingTopologyEventListener method createView.

private TopologyView createView(boolean current) {
    final TopologyView view = Mockito.mock(TopologyView.class);
    Mockito.when(view.isCurrent()).thenReturn(current);
    final InstanceDescription local = Mockito.mock(InstanceDescription.class);
    Mockito.when(local.isLeader()).thenReturn(true);
    Mockito.when(local.isLocal()).thenReturn(true);
    Mockito.when(local.getSlingId()).thenReturn("id");
    Mockito.when(view.getLocalInstance()).thenReturn(local);
    final ClusterView localView = Mockito.mock(ClusterView.class);
    Mockito.when(localView.getId()).thenReturn("1");
    Mockito.when(localView.getInstances()).thenReturn(Collections.singletonList(local));
    Mockito.when(view.getClusterViews()).thenReturn(Collections.singleton(localView));
    Mockito.when(local.getClusterView()).thenReturn(localView);
    return view;
}
Also used : ClusterView(org.apache.sling.discovery.ClusterView) InstanceDescription(org.apache.sling.discovery.InstanceDescription) TopologyView(org.apache.sling.discovery.TopologyView)

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