use of org.apache.sling.discovery.commons.providers.base.DummyListener in project sling by apache.
the class TestOakSyncTokenService method testTwoNodesOneLeaving.
@Test
public void testTwoNodesOneLeaving() throws Exception {
logger.info("testTwoNodesOneLeaving: start");
String slingId2 = UUID.randomUUID().toString();
DummyTopologyView two1 = TestHelper.newView(true, slingId1, slingId1, slingId1, slingId2);
Lock lock1 = new ReentrantLock();
OakBacklogClusterSyncService cs1 = OakBacklogClusterSyncService.testConstructorAndActivate(new SimpleCommonsConfig(), idMapService1, new DummySlingSettingsService(slingId1), factory1);
ViewStateManager vsm1 = ViewStateManagerFactory.newViewStateManager(lock1, cs1);
DummyListener l = new DummyListener();
vsm1.bind(l);
vsm1.handleActivated();
vsm1.handleNewView(two1);
cs1.triggerBackgroundCheck();
assertEquals(0, l.countEvents());
DescriptorHelper.setDiscoveryLiteDescriptor(factory1, new DiscoveryLiteDescriptorBuilder().setFinal(true).me(1).seq(1).activeIds(1).deactivatingIds(2));
cs1.triggerBackgroundCheck();
assertEquals(0, l.countEvents());
// make an assertion that the background runnable is at this stage - even with
// a 2sec sleep - waiting for the deactivating instance to disappear
logger.info("testTwoNodesOneLeaving: sync service should be waiting for backlog to disappear");
Thread.sleep(2000);
BackgroundCheckRunnable backgroundCheckRunnable = cs1.backgroundCheckRunnable;
assertNotNull(backgroundCheckRunnable);
assertFalse(backgroundCheckRunnable.isDone());
assertFalse(backgroundCheckRunnable.cancelled());
// release the deactivating instance by removing it from the clusterView
logger.info("testTwoNodesOneLeaving: freeing backlog - sync service should finish up");
DescriptorHelper.setDiscoveryLiteDescriptor(factory1, new DiscoveryLiteDescriptorBuilder().setFinal(true).me(1).seq(2).activeIds(1));
cs1.triggerBackgroundCheck();
// now give this thing 2 sec to settle
Thread.sleep(2000);
// after that, the backgroundRunnable should be done and no events stuck in vsm
backgroundCheckRunnable = cs1.backgroundCheckRunnable;
assertNotNull(backgroundCheckRunnable);
assertFalse(backgroundCheckRunnable.cancelled());
assertTrue(backgroundCheckRunnable.isDone());
assertEquals(0, vsm1.waitForAsyncEvents(1000));
logger.info("testTwoNodesOneLeaving: setting up 2nd node");
Lock lock2 = new ReentrantLock();
IdMapService idMapService2 = IdMapService.testConstructor(new SimpleCommonsConfig(), new DummySlingSettingsService(slingId2), factory2);
OakBacklogClusterSyncService cs2 = OakBacklogClusterSyncService.testConstructorAndActivate(new SimpleCommonsConfig(), idMapService2, new DummySlingSettingsService(slingId2), factory2);
ViewStateManager vsm2 = ViewStateManagerFactory.newViewStateManager(lock2, cs2);
cs1.triggerBackgroundCheck();
cs2.triggerBackgroundCheck();
assertEquals(1, l.countEvents());
DescriptorHelper.setDiscoveryLiteDescriptor(factory2, new DiscoveryLiteDescriptorBuilder().setFinal(true).me(2).seq(3).activeIds(1, 2));
cs1.triggerBackgroundCheck();
cs2.triggerBackgroundCheck();
assertEquals(1, l.countEvents());
DescriptorHelper.setDiscoveryLiteDescriptor(factory1, new DiscoveryLiteDescriptorBuilder().setFinal(true).me(1).seq(3).activeIds(1, 2));
cs1.triggerBackgroundCheck();
cs2.triggerBackgroundCheck();
assertEquals(1, l.countEvents());
vsm2.handleActivated();
assertTrue(idMapService1.waitForInit(5000));
assertTrue(idMapService2.waitForInit(5000));
DummyTopologyView two2 = TestHelper.newView(two1.getLocalClusterSyncTokenId(), two1.getLocalInstance().getClusterView().getId(), true, slingId1, slingId1, slingId1, slingId2);
vsm2.handleNewView(two2);
cs1.triggerBackgroundCheck();
cs1.triggerBackgroundCheck();
cs2.triggerBackgroundCheck();
cs2.triggerBackgroundCheck();
assertEquals(0, vsm1.waitForAsyncEvents(1000));
assertEquals(1, l.countEvents());
logger.info("testTwoNodesOneLeaving: removing instance2 from the view - even though vsm1 didn't really know about it, it should send a TOPOLOGY_CHANGING - we leave it as deactivating for now...");
DummyTopologyView oneLeaving = two1.clone();
oneLeaving.removeInstance(slingId2);
DescriptorHelper.setDiscoveryLiteDescriptor(factory1, new DiscoveryLiteDescriptorBuilder().setFinal(true).me(1).seq(1).activeIds(1).deactivatingIds(2));
vsm1.handleNewView(oneLeaving);
cs1.triggerBackgroundCheck();
cs2.triggerBackgroundCheck();
// wait for TOPOLOGY_CHANGING to be received by vsm1
assertEquals(0, vsm1.waitForAsyncEvents(5000));
assertEquals(2, l.countEvents());
logger.info("testTwoNodesOneLeaving: marking instance2 as no longer deactivating, so vsm1 should now send a TOPOLOGY_CHANGED");
DescriptorHelper.setDiscoveryLiteDescriptor(factory1, new DiscoveryLiteDescriptorBuilder().setFinal(true).me(1).seq(2).activeIds(1).inactiveIds(2));
cs1.triggerBackgroundCheck();
cs2.triggerBackgroundCheck();
// wait for TOPOLOGY_CHANGED to be received by vsm1
assertEquals(0, vsm1.waitForAsyncEvents(5000));
RepositoryTestHelper.dumpRepo(factory1);
assertEquals(3, l.countEvents());
}
use of org.apache.sling.discovery.commons.providers.base.DummyListener in project sling by apache.
the class TestOakSyncTokenService method testOneNode.
@Test
public void testOneNode() throws Exception {
logger.info("testOneNode: start");
DummyTopologyView one = TestHelper.newView(true, slingId1, slingId1, slingId1);
Lock lock = new ReentrantLock();
OakBacklogClusterSyncService cs = OakBacklogClusterSyncService.testConstructorAndActivate(new SimpleCommonsConfig(), idMapService1, new DummySlingSettingsService(slingId1), factory1);
ViewStateManager vsm = ViewStateManagerFactory.newViewStateManager(lock, cs);
DummyListener l = new DummyListener();
assertEquals(0, l.countEvents());
vsm.bind(l);
cs.triggerBackgroundCheck();
assertEquals(0, l.countEvents());
vsm.handleActivated();
cs.triggerBackgroundCheck();
assertEquals(0, l.countEvents());
vsm.handleNewView(one);
cs.triggerBackgroundCheck();
assertEquals(0, l.countEvents());
cs.triggerBackgroundCheck();
DescriptorHelper.setDiscoveryLiteDescriptor(factory1, new DiscoveryLiteDescriptorBuilder().me(1).seq(1).activeIds(1).setFinal(true));
assertTrue(idMapService1.waitForInit(5000));
cs.triggerBackgroundCheck();
assertEquals(0, vsm.waitForAsyncEvents(1000));
assertEquals(1, l.countEvents());
logger.info("testOneNode: end");
}
use of org.apache.sling.discovery.commons.providers.base.DummyListener 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());
}
use of org.apache.sling.discovery.commons.providers.base.DummyListener 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());
}
use of org.apache.sling.discovery.commons.providers.base.DummyListener in project sling by apache.
the class DiscoveryServiceImplTest method testLocalClusterSyncTokenIdChange.
@Test
public void testLocalClusterSyncTokenIdChange() throws Exception {
logger.info("testLocalClusterSyncTokenIdChange: start");
logger.info("testLocalClusterSyncTokenIdChange: creating instance1...");
FullJR2VirtualInstanceBuilder builder1 = (FullJR2VirtualInstanceBuilder) new FullJR2VirtualInstanceBuilder().setDebugName("instance1").newRepository("/var/testLocalClusterSyncTokenIdChange/", true).setConnectorPingInterval(999).setConnectorPingTimeout(999).setMinEventDelay(0);
VirtualInstance instance1 = builder1.build();
logger.info("testLocalClusterSyncTokenIdChange: creating instance2...");
FullJR2VirtualInstanceBuilder builder2 = (FullJR2VirtualInstanceBuilder) new FullJR2VirtualInstanceBuilder().setDebugName("instance2").useRepositoryOf(instance1).setConnectorPingInterval(999).setConnectorPingTimeout(999).setMinEventDelay(0);
VirtualInstance instance2 = builder2.build();
logger.info("testLocalClusterSyncTokenIdChange: registering listener...");
DummyListener listener = new DummyListener();
DiscoveryServiceImpl discoveryService = (DiscoveryServiceImpl) instance1.getDiscoveryService();
discoveryService.bindTopologyEventListener(listener);
assertEquals(0, discoveryService.getViewStateManager().waitForAsyncEvents(2000));
assertEquals(0, listener.countEvents());
logger.info("testLocalClusterSyncTokenIdChange: doing some heartbeating...");
instance1.heartbeatsAndCheckView();
instance2.heartbeatsAndCheckView();
Thread.sleep(1000);
instance1.heartbeatsAndCheckView();
instance2.heartbeatsAndCheckView();
Thread.sleep(2000);
logger.info("testLocalClusterSyncTokenIdChange: expecting to have received the INIT...");
assertEquals(0, discoveryService.getViewStateManager().waitForAsyncEvents(2000));
assertEquals(1, listener.countEvents());
ResourceResolverFactory factory = instance1.getResourceResolverFactory();
ResourceResolver resolver = factory.getServiceResourceResolver(null);
instance1.heartbeatsAndCheckView();
instance2.heartbeatsAndCheckView();
Thread.sleep(1000);
logger.info("testLocalClusterSyncTokenIdChange: after another heartbeat nothing more should have been triggered...");
assertEquals(0, discoveryService.getViewStateManager().waitForAsyncEvents(2000));
assertEquals(1, listener.countEvents());
// simulate a change in the establishedView's viewId - which can be
// achieved by triggering a revoting - which should result with the
// same view cos the instances have not changed
HeartbeatHandler heartbeatHandler = (HeartbeatHandler) instance1.getViewChecker();
logger.info("testLocalClusterSyncTokenIdChange: forcing a new voting to start...");
heartbeatHandler.startNewVoting();
logger.info("testLocalClusterSyncTokenIdChange: doing some heartbeats to finish the voting...");
instance1.heartbeatsAndCheckView();
instance2.heartbeatsAndCheckView();
Thread.sleep(1000);
instance1.heartbeatsAndCheckView();
instance2.heartbeatsAndCheckView();
Thread.sleep(3000);
logger.info("testLocalClusterSyncTokenIdChange: now we should have gotten a CHANGING/CHANGED pair additionally...");
assertEquals(0, discoveryService.getViewStateManager().waitForAsyncEvents(2000));
assertEquals(3, listener.countEvents());
}
Aggregations