Search in sources :

Example 1 with OakVirtualInstanceBuilder

use of org.apache.sling.discovery.oak.its.setup.OakVirtualInstanceBuilder in project sling by apache.

the class OakDiscoveryServiceTest method testBindBeforeActivate.

@Test
public void testBindBeforeActivate() throws Exception {
    OakVirtualInstanceBuilder builder = (OakVirtualInstanceBuilder) new OakVirtualInstanceBuilder().setDebugName("test").newRepository("/foo/bar", true);
    String slingId = UUID.randomUUID().toString();
    ;
    DiscoveryLiteDescriptorBuilder discoBuilder = new DiscoveryLiteDescriptorBuilder();
    discoBuilder.id("id").me(1).activeIds(1);
    // make sure the discovery-lite descriptor is marked as not final
    // such that the view is not already set before we want it to be
    discoBuilder.setFinal(false);
    DescriptorHelper.setDiscoveryLiteDescriptor(builder.getResourceResolverFactory(), discoBuilder);
    IdMapService idMapService = IdMapService.testConstructor(new SimpleCommonsConfig(1000, -1), new DummySlingSettingsService(slingId), builder.getResourceResolverFactory());
    assertTrue(idMapService.waitForInit(2000));
    OakDiscoveryService discoveryService = (OakDiscoveryService) builder.getDiscoverService();
    assertNotNull(discoveryService);
    DummyListener listener = new DummyListener();
    for (int i = 0; i < 100; i++) {
        discoveryService.bindTopologyEventListener(listener);
        discoveryService.unbindTopologyEventListener(listener);
    }
    discoveryService.bindTopologyEventListener(listener);
    assertEquals(0, listener.countEvents());
    discoveryService.activate(null);
    assertEquals(0, listener.countEvents());
    // some more confusion...
    discoveryService.unbindTopologyEventListener(listener);
    discoveryService.bindTopologyEventListener(listener);
    // only set the final flag now - this makes sure that handlePotentialTopologyChange
    // will actually detect a valid new, different view and send out an event -
    // exactly as we want to
    discoBuilder.setFinal(true);
    DescriptorHelper.setDiscoveryLiteDescriptor(builder.getResourceResolverFactory(), discoBuilder);
    discoveryService.checkForTopologyChange();
    assertEquals(0, discoveryService.getViewStateManager().waitForAsyncEvents(2000));
    assertEquals(1, listener.countEvents());
    discoveryService.unbindTopologyEventListener(listener);
    assertEquals(1, listener.countEvents());
    discoveryService.bindTopologyEventListener(listener);
    assertEquals(0, discoveryService.getViewStateManager().waitForAsyncEvents(2000));
    // should now have gotten an INIT too
    assertEquals(2, listener.countEvents());
}
Also used : OakVirtualInstanceBuilder(org.apache.sling.discovery.oak.its.setup.OakVirtualInstanceBuilder) IdMapService(org.apache.sling.discovery.commons.providers.spi.base.IdMapService) DummySlingSettingsService(org.apache.sling.discovery.commons.providers.spi.base.DummySlingSettingsService) DiscoveryLiteDescriptorBuilder(org.apache.sling.discovery.commons.providers.spi.base.DiscoveryLiteDescriptorBuilder) DummyListener(org.apache.sling.discovery.commons.providers.base.DummyListener) Test(org.junit.Test)

Example 2 with OakVirtualInstanceBuilder

use of org.apache.sling.discovery.oak.its.setup.OakVirtualInstanceBuilder in project sling by apache.

the class OakDiscoveryServiceTest method testDescriptorSeqNumChange.

@Test
public void testDescriptorSeqNumChange() throws Exception {
    logger.info("testDescriptorSeqNumChange: start");
    OakVirtualInstanceBuilder builder1 = (OakVirtualInstanceBuilder) new OakVirtualInstanceBuilder().setDebugName("instance1").newRepository("/foo/barry/foo/", true).setConnectorPingInterval(999).setConnectorPingTimeout(999);
    VirtualInstance instance1 = builder1.build();
    OakVirtualInstanceBuilder builder2 = (OakVirtualInstanceBuilder) new OakVirtualInstanceBuilder().setDebugName("instance2").useRepositoryOf(instance1).setConnectorPingInterval(999).setConnectorPingTimeout(999);
    VirtualInstance instance2 = builder2.build();
    logger.info("testDescriptorSeqNumChange: created both instances, binding listener...");
    DummyListener listener = new DummyListener();
    OakDiscoveryService discoveryService = (OakDiscoveryService) instance1.getDiscoveryService();
    discoveryService.bindTopologyEventListener(listener);
    logger.info("testDescriptorSeqNumChange: waiting 2sec, listener should not get anything yet");
    assertEquals(0, discoveryService.getViewStateManager().waitForAsyncEvents(2000));
    assertEquals(0, listener.countEvents());
    logger.info("testDescriptorSeqNumChange: issuing 2 heartbeats with each instance should let the topology get established");
    instance1.heartbeatsAndCheckView();
    instance2.heartbeatsAndCheckView();
    instance1.heartbeatsAndCheckView();
    instance2.heartbeatsAndCheckView();
    logger.info("testDescriptorSeqNumChange: listener should get an event within 2sec from now at latest");
    assertEquals(0, discoveryService.getViewStateManager().waitForAsyncEvents(2000));
    assertEquals(1, listener.countEvents());
    ResourceResolverFactory factory = instance1.getResourceResolverFactory();
    ResourceResolver resolver = factory.getServiceResourceResolver(null);
    instance1.heartbeatsAndCheckView();
    assertEquals(0, discoveryService.getViewStateManager().waitForAsyncEvents(2000));
    assertEquals(1, listener.countEvents());
    // increment the seqNum by 2 - simulating a coming and going instance
    // while we were sleeping
    SimulatedLeaseCollection c = builder1.getSimulatedLeaseCollection();
    c.incSeqNum(2);
    logger.info("testDescriptorSeqNumChange: incremented seqnum by 2 - issuing another heartbeat should trigger a topology change");
    instance1.heartbeatsAndCheckView();
    // due to the nature of the syncService/minEventDelay we now explicitly first sleep 2sec before waiting for async events for another 2sec
    logger.info("testDescriptorSeqNumChange: sleeping 2sec for topology change to happen");
    Thread.sleep(2000);
    logger.info("testDescriptorSeqNumChange: ensuring no async events are still in the pipe - for another 2sec");
    assertEquals(0, discoveryService.getViewStateManager().waitForAsyncEvents(2000));
    logger.info("testDescriptorSeqNumChange: now listener should have received 3 events, it got: " + listener.countEvents());
    assertEquals(3, listener.countEvents());
}
Also used : OakVirtualInstanceBuilder(org.apache.sling.discovery.oak.its.setup.OakVirtualInstanceBuilder) SimulatedLeaseCollection(org.apache.sling.discovery.oak.its.setup.SimulatedLeaseCollection) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) VirtualInstance(org.apache.sling.discovery.base.its.setup.VirtualInstance) DummyListener(org.apache.sling.discovery.commons.providers.base.DummyListener) Test(org.junit.Test)

Example 3 with OakVirtualInstanceBuilder

use of org.apache.sling.discovery.oak.its.setup.OakVirtualInstanceBuilder in project sling by apache.

the class OakDiscoveryServiceTest 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);
    OakVirtualInstanceBuilder builder = newBuilder();
    builder.setDebugName("firstInstanceA").newRepository("/var/discovery/oak/", true).setConnectorPingTimeout(3).setMinEventDelay(3);
    instance1 = builder.build();
    instance1.stopViewChecker();
    //        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");
    builder.getSimulatedLeaseCollection().setFinal(false);
    l1.addExpected(Type.TOPOLOGY_CHANGING);
    VirtualInstanceBuilder builder2 = newBuilder();
    builder2.setDebugName("secondInstanceB").useRepositoryOf(instance1).setConnectorPingTimeout(3).setMinEventDelay(3);
    instance2 = builder2.build();
    instance2.stopViewChecker();
    //        instance2.stopVoting();
    logger.info("testOldView: instance2 created, now issuing one heartbeat with instance2 first, so that instance1 can take note of it");
    instance2.getViewChecker().checkView();
    OakDiscoveryService oakDisco1 = (OakDiscoveryService) instance2.getDiscoveryService();
    oakDisco1.checkForTopologyChange();
    logger.info("testOldView: now instance1 is also doing a heartbeat and should see that a new instance is there");
    instance1.getViewChecker().checkView();
    OakDiscoveryService oakDisco2 = (OakDiscoveryService) instance1.getDiscoveryService();
    oakDisco2.checkForTopologyChange();
    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());
    builder.getSimulatedLeaseCollection().setFinal(true);
    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
    builder.getSimulatedLeaseCollection().setFinal(false);
    l1.addExpected(Type.TOPOLOGY_CHANGING);
    for (int i = 0; i < 8; i++) {
        instance1.heartbeatsAndCheckView();
        Thread.sleep(500);
    }
    // INIT, CHANGING, CHANGED and CHANGING
    assertEquals(4, 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
    assertFalse(t1.isCurrent());
    // and contain both instances now
    assertEquals(2, t1.getInstances().size());
}
Also used : VirtualInstanceBuilder(org.apache.sling.discovery.base.its.setup.VirtualInstanceBuilder) OakVirtualInstanceBuilder(org.apache.sling.discovery.oak.its.setup.OakVirtualInstanceBuilder) OakVirtualInstanceBuilder(org.apache.sling.discovery.oak.its.setup.OakVirtualInstanceBuilder) AssertingTopologyEventListener(org.apache.sling.discovery.base.its.setup.mock.AssertingTopologyEventListener) TopologyView(org.apache.sling.discovery.TopologyView) OakDiscoveryService(org.apache.sling.discovery.oak.OakDiscoveryService) Test(org.junit.Test) AbstractDiscoveryServiceTest(org.apache.sling.discovery.base.its.AbstractDiscoveryServiceTest)

Aggregations

OakVirtualInstanceBuilder (org.apache.sling.discovery.oak.its.setup.OakVirtualInstanceBuilder)3 Test (org.junit.Test)3 DummyListener (org.apache.sling.discovery.commons.providers.base.DummyListener)2 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 ResourceResolverFactory (org.apache.sling.api.resource.ResourceResolverFactory)1 TopologyView (org.apache.sling.discovery.TopologyView)1 AbstractDiscoveryServiceTest (org.apache.sling.discovery.base.its.AbstractDiscoveryServiceTest)1 VirtualInstance (org.apache.sling.discovery.base.its.setup.VirtualInstance)1 VirtualInstanceBuilder (org.apache.sling.discovery.base.its.setup.VirtualInstanceBuilder)1 AssertingTopologyEventListener (org.apache.sling.discovery.base.its.setup.mock.AssertingTopologyEventListener)1 DiscoveryLiteDescriptorBuilder (org.apache.sling.discovery.commons.providers.spi.base.DiscoveryLiteDescriptorBuilder)1 DummySlingSettingsService (org.apache.sling.discovery.commons.providers.spi.base.DummySlingSettingsService)1 IdMapService (org.apache.sling.discovery.commons.providers.spi.base.IdMapService)1 OakDiscoveryService (org.apache.sling.discovery.oak.OakDiscoveryService)1 SimulatedLeaseCollection (org.apache.sling.discovery.oak.its.setup.SimulatedLeaseCollection)1