Search in sources :

Example 6 with TopologyView

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

the class AbstractTopologyEventTest method testGetDuringDelay.

@Test
public void testGetDuringDelay() throws Throwable {
    instance1 = newBuilder().setDebugName("firstInstanceA").newRepository("/var/discovery/impl/", true).setConnectorPingTimeout(20).setMinEventDelay(6).build();
    AssertingTopologyEventListener l1 = new AssertingTopologyEventListener("instance1.l1");
    l1.addExpected(TopologyEvent.Type.TOPOLOGY_INIT);
    instance1.bindTopologyEventListener(l1);
    TopologyView earlyTopo = instance1.getDiscoveryService().getTopology();
    assertNotNull(earlyTopo);
    assertFalse(earlyTopo.isCurrent());
    assertEquals(1, earlyTopo.getInstances().size());
    for (int i = 0; i < 4; i++) {
        instance1.heartbeatsAndCheckView();
        Thread.sleep(125);
    }
    TopologyView secondTopo = instance1.getDiscoveryService().getTopology();
    assertEquals(1, secondTopo.getInstances().size());
    assertEquals(instance1.getSlingId(), secondTopo.getInstances().iterator().next().getSlingId());
    assertTrue(secondTopo.isCurrent());
    instance1.dumpRepo();
    assertEarlyAndFirstClusterViewIdMatches(earlyTopo, secondTopo);
    Thread.sleep(500);
    // should have gotten the INIT, hence 0 remaining expected events
    assertEquals(0, l1.getRemainingExpectedCount());
    assertEquals(0, l1.getUnexpectedCount());
    l1.addExpected(TopologyEvent.Type.TOPOLOGY_CHANGING);
    instance2 = newBuilder().setDebugName("secondInstanceB").useRepositoryOf(instance1).setConnectorPingTimeout(20).setMinEventDelay(1).build();
    AssertingTopologyEventListener l2 = new AssertingTopologyEventListener("instance2.l1");
    l2.addExpected(TopologyEvent.Type.TOPOLOGY_INIT);
    instance2.bindTopologyEventListener(l2);
    for (int i = 0; i < 4; i++) {
        instance2.heartbeatsAndCheckView();
        instance1.heartbeatsAndCheckView();
        Thread.sleep(750);
    }
    assertEquals(0, l1.getUnexpectedCount());
    TopologyView topo2 = instance2.getDiscoveryService().getTopology();
    assertTrue(topo2.isCurrent());
    assertEquals(2, topo2.getInstances().size());
    TopologyView topo1 = instance1.getDiscoveryService().getTopology();
    assertTrue(topo1.isCurrent());
    assertEquals(2, topo1.getInstances().size());
    l1.addExpected(TopologyEvent.Type.TOPOLOGY_CHANGED);
    Thread.sleep(5000);
    assertEquals(0, l1.getRemainingExpectedCount());
    assertEquals(0, l1.getUnexpectedCount());
    assertEquals(0, l2.getRemainingExpectedCount());
    assertEquals(0, l2.getUnexpectedCount());
    assertTrue(instance2.getDiscoveryService().getTopology().isCurrent());
    assertEquals(2, instance2.getDiscoveryService().getTopology().getInstances().size());
    assertTrue(instance1.getDiscoveryService().getTopology().isCurrent());
    assertEquals(2, instance1.getDiscoveryService().getTopology().getInstances().size());
}
Also used : AssertingTopologyEventListener(org.apache.sling.discovery.base.its.setup.mock.AssertingTopologyEventListener) TopologyView(org.apache.sling.discovery.TopologyView) Test(org.junit.Test)

Example 7 with TopologyView

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

Example 8 with TopologyView

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

the class RepositoryDelaysTest method testOldView.

/**
     * Tests whether the not-current view returned by getTopology()
     * matches what listeners get in TOPOLOGY_CHANGING - it should
     * basically be the same.
     */
@Test
public void testOldView() throws Throwable {
    final org.apache.log4j.Logger discoveryLogger = RootLogger.getLogger("org.apache.sling.discovery");
    // info should do
    discoveryLogger.setLevel(Level.INFO);
    FullJR2VirtualInstanceBuilder builder = newBuilder();
    builder.setDebugName("firstInstanceA").newRepository("/var/discovery/impl/", true).setConnectorPingTimeout(3).setMinEventDelay(3);
    instance1 = builder.fullBuild();
    instance1.stopVoting();
    TopologyView t1 = instance1.getDiscoveryService().getTopology();
    // current it should not be
    assertFalse(t1.isCurrent());
    // but it can as well contain the local instance
    assertEquals(1, t1.getInstances().size());
    AssertingTopologyEventListener l1 = new AssertingTopologyEventListener("instance1.l1");
    l1.addExpected(Type.TOPOLOGY_INIT);
    instance1.bindTopologyEventListener(l1);
    logger.info("testOldView: instance1 created, no events expected yet. slingId=" + instance1.slingId);
    instance1.heartbeatsAndCheckView();
    Thread.sleep(200);
    instance1.heartbeatsAndCheckView();
    Thread.sleep(200);
    instance1.heartbeatsAndCheckView();
    Thread.sleep(200);
    logger.info("testOldView: 2nd/3rd heartbeat sent - now expecting a TOPOLOGY_INIT");
    instance1.dumpRepo();
    // one event
    assertEquals(1, l1.getEvents().size());
    // the expected one
    assertEquals(0, l1.getRemainingExpectedCount());
    assertEquals(0, l1.getUnexpectedCount());
    t1 = instance1.getDiscoveryService().getTopology();
    // current it should now be
    assertTrue(t1.isCurrent());
    // and it must contain the local instance
    assertEquals(1, t1.getInstances().size());
    logger.info("testOldView: creating instance2");
    l1.addExpected(Type.TOPOLOGY_CHANGING);
    FullJR2VirtualInstanceBuilder builder2 = newBuilder();
    builder2.setDebugName("secondInstanceB").useRepositoryOf(instance1).setConnectorPingTimeout(3).setMinEventDelay(3);
    instance2 = builder2.fullBuild();
    instance2.stopVoting();
    logger.info("testOldView: instance2 created, now issuing one heartbeat with instance2 first, so that instance1 can take note of it");
    instance2.heartbeatsAndCheckView();
    logger.info("testOldView: now instance1 is also doing a heartbeat and should see that a new instance is there");
    instance1.heartbeatsAndCheckView();
    logger.info("testOldView: 500ms sleep...");
    // allow some time for CHANGING to be sent
    Thread.sleep(500);
    logger.info("testOldView: 500ms sleep done.");
    // INIT and CHANGING
    assertEquals(2, l1.getEvents().size());
    // no remaining expected
    assertEquals(0, l1.getRemainingExpectedCount());
    // and no unexpected
    assertEquals(0, l1.getUnexpectedCount());
    t1 = instance1.getDiscoveryService().getTopology();
    // current it should not be
    assertFalse(t1.isCurrent());
    // but it should still contain the local instance from before
    assertEquals(1, t1.getInstances().size());
    l1.addExpected(Type.TOPOLOGY_CHANGED);
    logger.info("testOldView: now issuing 3 rounds of heartbeats/checks and expecting a TOPOLOGY_CHANGED then");
    instance2.heartbeatsAndCheckView();
    //        instance2.analyzeVotings();
    instance1.heartbeatsAndCheckView();
    //        instance1.analyzeVotings();
    Thread.sleep(1200);
    instance2.heartbeatsAndCheckView();
    instance1.heartbeatsAndCheckView();
    Thread.sleep(1200);
    instance2.heartbeatsAndCheckView();
    instance1.heartbeatsAndCheckView();
    Thread.sleep(1200);
    // INIT, CHANGING and CHANGED
    assertEquals(3, l1.getEvents().size());
    // no remaining expected
    assertEquals(0, l1.getRemainingExpectedCount());
    // and no unexpected
    assertEquals(0, l1.getUnexpectedCount());
    t1 = instance1.getDiscoveryService().getTopology();
    // and we should be current again
    assertTrue(t1.isCurrent());
    // and contain both instances now
    assertEquals(2, t1.getInstances().size());
    // timeout is set to 3sec, so we now do heartbeats for 4sec with only instance1
    // to let instance2 crash
    // force instance1 to no longer analyze the votings
    // since stopVoting() only deactivates the listener, we also
    // have to set votingHandler of heartbeatHandler to null
    PrivateAccessor.setField(instance1.getHeartbeatHandler(), "votingHandler", null);
    l1.addExpected(Type.TOPOLOGY_CHANGING);
    for (int i = 0; i < 8; i++) {
        instance1.getHeartbeatHandler().heartbeatAndCheckView();
        Thread.sleep(500);
    }
    // INIT, CHANGING, CHANGED and CHANGED
    assertEquals(4, l1.getEvents().size());
    // no remaining expected
    assertEquals(0, l1.getRemainingExpectedCount());
    // and no unexpected
    assertEquals(0, l1.getUnexpectedCount());
    t1 = instance1.getDiscoveryService().getTopology();
    // we should still be !current
    assertFalse(t1.isCurrent());
    // and contain both instances
    assertEquals(2, t1.getInstances().size());
}
Also used : FullJR2VirtualInstanceBuilder(org.apache.sling.discovery.impl.setup.FullJR2VirtualInstanceBuilder) AssertingTopologyEventListener(org.apache.sling.discovery.base.its.setup.mock.AssertingTopologyEventListener) TopologyView(org.apache.sling.discovery.TopologyView) Test(org.junit.Test)

Example 9 with TopologyView

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

the class TestViewStateManager method testChangedPropertiesChanged.

@Test
public void testChangedPropertiesChanged() throws Exception {
    final DummyListener listener = new DummyListener();
    mgr.installMinEventDelayHandler(new DiscoveryService() {

        @Override
        public TopologyView getTopology() {
            throw new IllegalStateException("not yet impl");
        }
    }, new DummyScheduler(), 1);
    mgr.handleActivated();
    TestHelper.assertNoEvents(listener);
    mgr.bind(listener);
    TestHelper.assertNoEvents(listener);
    mgr.handleChanging();
    TestHelper.assertNoEvents(listener);
    final BaseTopologyView view1 = new DummyTopologyView().addInstance();
    InstanceDescription instance1 = view1.getInstances().iterator().next();
    ClusterView cluster1 = instance1.getClusterView();
    mgr.handleNewView(view1);
    assertEvents(listener, EventHelper.newInitEvent(view1));
    DefaultClusterView cluster2 = new DefaultClusterView(new String(cluster1.getId()));
    final BaseTopologyView view2 = new DummyTopologyView(view1.getLocalClusterSyncTokenId()).addInstance(instance1.getSlingId(), cluster2, instance1.isLeader(), instance1.isLocal());
    DefaultInstanceDescription instance2 = (DefaultInstanceDescription) view2.getLocalInstance();
    instance2.setProperty("foo", "bar");
    mgr.handleNewView(view2);
    assertEvents(listener, EventHelper.newPropertiesChangedEvent(view1, view2));
}
Also used : ClusterView(org.apache.sling.discovery.ClusterView) DefaultClusterView(org.apache.sling.discovery.commons.providers.DefaultClusterView) DummyTopologyView(org.apache.sling.discovery.commons.providers.DummyTopologyView) DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription) DefaultClusterView(org.apache.sling.discovery.commons.providers.DefaultClusterView) DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription) InstanceDescription(org.apache.sling.discovery.InstanceDescription) DiscoveryService(org.apache.sling.discovery.DiscoveryService) BaseTopologyView(org.apache.sling.discovery.commons.providers.BaseTopologyView) TopologyView(org.apache.sling.discovery.TopologyView) DummyTopologyView(org.apache.sling.discovery.commons.providers.DummyTopologyView) BaseTopologyView(org.apache.sling.discovery.commons.providers.BaseTopologyView) Test(org.junit.Test)

Example 10 with TopologyView

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

the class TopologyWebConsolePlugin method renderProperties.

/**
     * Render the properties page of a particular instance
     */
private void renderProperties(final PrintWriter pw, final String contextPath, final String nodeId) {
    if (logger.isDebugEnabled()) {
        logger.debug("renderProperties: nodeId=" + nodeId);
    }
    final TopologyView tv = this.currentView;
    @SuppressWarnings("unchecked") Set<InstanceDescription> instances = (tv == null ? (Set<InstanceDescription>) Collections.EMPTY_SET : tv.findInstances(new InstanceFilter() {

        @Override
        public boolean accept(InstanceDescription instance) {
            String slingId = instance.getSlingId();
            if (logger.isDebugEnabled()) {
                logger.debug("renderProperties/picks: slingId={}", slingId);
            }
            return (slingId.equals(nodeId));
        }
    }));
    if (instances != null && instances.size() == 1) {
        InstanceDescription instance = instances.iterator().next();
        pw.println("Properties of " + instance.getSlingId() + ":<br/>");
        pw.println("<table class=\"adapters nicetable ui-widget tablesorter\">");
        pw.println("<thead>");
        pw.println("<tr>");
        pw.println("<th class=\"header ui-widget-header\">Key</th>");
        pw.println("<th class=\"header ui-widget-header\">Value</th>");
        pw.println("</tr>");
        pw.println("</thead>");
        pw.println("<tbody>");
        boolean odd = true;
        for (Iterator<Entry<String, String>> it = instance.getProperties().entrySet().iterator(); it.hasNext(); ) {
            Entry<String, String> entry = it.next();
            String oddEven = odd ? "odd" : "even";
            odd = !odd;
            pw.println("<tr class=\"" + oddEven + " ui-state-default\">");
            pw.println("<td>" + entry.getKey() + "</td>");
            pw.println("<td>" + entry.getValue() + "</td>");
            pw.println("</tr>");
        }
        pw.println("</tbody>");
        pw.println("</table>");
    }
}
Also used : InstanceFilter(org.apache.sling.discovery.InstanceFilter) Entry(java.util.Map.Entry) HashSet(java.util.HashSet) Set(java.util.Set) InstanceDescription(org.apache.sling.discovery.InstanceDescription) TopologyView(org.apache.sling.discovery.TopologyView)

Aggregations

TopologyView (org.apache.sling.discovery.TopologyView)25 InstanceDescription (org.apache.sling.discovery.InstanceDescription)9 Test (org.junit.Test)9 ClusterView (org.apache.sling.discovery.ClusterView)8 TopologyEvent (org.apache.sling.discovery.TopologyEvent)7 FullJR2VirtualInstanceBuilder (org.apache.sling.discovery.impl.setup.FullJR2VirtualInstanceBuilder)4 HashSet (java.util.HashSet)3 TopologyEventListener (org.apache.sling.discovery.TopologyEventListener)3 AssertingTopologyEventListener (org.apache.sling.discovery.base.its.setup.mock.AssertingTopologyEventListener)3 Entry (java.util.Map.Entry)2 Set (java.util.Set)2 InstanceFilter (org.apache.sling.discovery.InstanceFilter)2 Type (org.apache.sling.discovery.TopologyEvent.Type)2 Announcement (org.apache.sling.discovery.base.connectors.announcement.Announcement)2 CachedAnnouncement (org.apache.sling.discovery.base.connectors.announcement.CachedAnnouncement)2 TopologyConnectorClientInformation (org.apache.sling.discovery.base.connectors.ping.TopologyConnectorClientInformation)2 VirtualInstanceBuilder (org.apache.sling.discovery.base.its.setup.VirtualInstanceBuilder)2 FullJR2VirtualInstance (org.apache.sling.discovery.impl.setup.FullJR2VirtualInstance)2 Category (org.junit.experimental.categories.Category)2 ArrayList (java.util.ArrayList)1