Search in sources :

Example 1 with BackgroundCheckRunnable

use of org.apache.sling.discovery.commons.providers.spi.base.AbstractServiceWithBackgroundCheck.BackgroundCheckRunnable 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 BackgroundCheckRunnable

use of org.apache.sling.discovery.commons.providers.spi.base.AbstractServiceWithBackgroundCheck.BackgroundCheckRunnable in project sling by apache.

the class TestOakSyncTokenService method getBackgroundCheckRunnable.

private BackgroundCheckRunnable getBackgroundCheckRunnable(IdMapService idMapService) throws NoSuchFieldException, IllegalAccessException {
    Field field = idMapService.getClass().getSuperclass().getDeclaredField("backgroundCheckRunnable");
    field.setAccessible(true);
    Object backgroundCheckRunnable = field.get(idMapService);
    return (BackgroundCheckRunnable) backgroundCheckRunnable;
}
Also used : Field(java.lang.reflect.Field) BackgroundCheckRunnable(org.apache.sling.discovery.commons.providers.spi.base.AbstractServiceWithBackgroundCheck.BackgroundCheckRunnable)

Example 3 with BackgroundCheckRunnable

use of org.apache.sling.discovery.commons.providers.spi.base.AbstractServiceWithBackgroundCheck.BackgroundCheckRunnable in project sling by apache.

the class TestOakSyncTokenService method testRapidIdMapServiceActivateDeactivate.

@Test
public void testRapidIdMapServiceActivateDeactivate() throws Exception {
    BackgroundCheckRunnable bgCheckRunnable = getBackgroundCheckRunnable(idMapService1);
    assertNotNull(bgCheckRunnable);
    assertFalse(bgCheckRunnable.isDone());
    idMapService1.deactivate();
    assertFalse(idMapService1.waitForInit(2500));
    bgCheckRunnable = getBackgroundCheckRunnable(idMapService1);
    assertNotNull(bgCheckRunnable);
    assertTrue(bgCheckRunnable.isDone());
}
Also used : BackgroundCheckRunnable(org.apache.sling.discovery.commons.providers.spi.base.AbstractServiceWithBackgroundCheck.BackgroundCheckRunnable) Test(org.junit.Test)

Aggregations

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