Search in sources :

Example 1 with ViewStateManager

use of org.apache.sling.discovery.commons.providers.ViewStateManager 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());
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) DummyTopologyView(org.apache.sling.discovery.commons.providers.DummyTopologyView) BackgroundCheckRunnable(org.apache.sling.discovery.commons.providers.spi.base.AbstractServiceWithBackgroundCheck.BackgroundCheckRunnable) ViewStateManager(org.apache.sling.discovery.commons.providers.ViewStateManager) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) DummyListener(org.apache.sling.discovery.commons.providers.base.DummyListener) Test(org.junit.Test)

Example 2 with ViewStateManager

use of org.apache.sling.discovery.commons.providers.ViewStateManager 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");
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) DummyTopologyView(org.apache.sling.discovery.commons.providers.DummyTopologyView) ViewStateManager(org.apache.sling.discovery.commons.providers.ViewStateManager) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) DummyListener(org.apache.sling.discovery.commons.providers.base.DummyListener) Test(org.junit.Test)

Aggregations

Lock (java.util.concurrent.locks.Lock)2 ReentrantLock (java.util.concurrent.locks.ReentrantLock)2 DummyTopologyView (org.apache.sling.discovery.commons.providers.DummyTopologyView)2 ViewStateManager (org.apache.sling.discovery.commons.providers.ViewStateManager)2 DummyListener (org.apache.sling.discovery.commons.providers.base.DummyListener)2 Test (org.junit.Test)2 BackgroundCheckRunnable (org.apache.sling.discovery.commons.providers.spi.base.AbstractServiceWithBackgroundCheck.BackgroundCheckRunnable)1