Search in sources :

Example 6 with DefaultClusterView

use of org.apache.sling.discovery.commons.providers.DefaultClusterView in project sling by apache.

the class TestHelper method newView.

public static DummyTopologyView newView(String syncId, String clusterId, boolean isCurrent, String leaderId, String localId, String... slingIds) {
    DummyTopologyView topology = new DummyTopologyView(syncId);
    DefaultClusterView cluster = new DefaultClusterView(clusterId);
    for (String slingId : slingIds) {
        DefaultInstanceDescription id = new DefaultInstanceDescription(cluster, slingId.equals(leaderId), slingId.equals(localId), slingId, new HashMap<String, String>());
        topology.addInstanceDescription(id);
    }
    if (!isCurrent) {
        topology.setNotCurrent();
    }
    return topology;
}
Also used : DefaultClusterView(org.apache.sling.discovery.commons.providers.DefaultClusterView) DummyTopologyView(org.apache.sling.discovery.commons.providers.DummyTopologyView) DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription)

Example 7 with DefaultClusterView

use of org.apache.sling.discovery.commons.providers.DefaultClusterView in project sling by apache.

the class TestSlowViewStateManager method testClusterSyncService_withConcurrency.

//TODO test takes env 10sec
@Category(Slow.class)
@Test
public void testClusterSyncService_withConcurrency() throws Exception {
    final org.apache.log4j.Logger commonsLogger = LogManager.getRootLogger().getLogger("org.apache.sling.discovery.commons.providers");
    final org.apache.log4j.Level logLevel = commonsLogger.getLevel();
    // change here to DEBUG in case of issues with this test
    commonsLogger.setLevel(Level.INFO);
    final Semaphore serviceSemaphore = new Semaphore(0);
    final Semaphore testSemaphore = new Semaphore(0);
    final ReentrantLock lock = new ReentrantLock();
    final ClusterSyncServiceWithSemaphore cs = new ClusterSyncServiceWithSemaphore(lock, serviceSemaphore);
    mgr = new ViewStateManagerImpl(lock, cs);
    final DummyListener listener = new DummyListener();
    mgr.bind(listener);
    TestHelper.assertNoEvents(listener);
    mgr.handleActivated();
    TestHelper.assertNoEvents(listener);
    final String slingId1 = UUID.randomUUID().toString();
    final String slingId2 = UUID.randomUUID().toString();
    final String slingId3 = UUID.randomUUID().toString();
    final String clusterId = UUID.randomUUID().toString();
    final DefaultClusterView cluster = new DefaultClusterView(clusterId);
    final DummyTopologyView view1 = new DummyTopologyView().addInstance(slingId1, cluster, true, true).addInstance(slingId2, cluster, false, false).addInstance(slingId3, cluster, false, false);
    final DummyTopologyView view2 = DummyTopologyView.clone(view1).removeInstance(slingId2);
    final DummyTopologyView view3 = DummyTopologyView.clone(view1).removeInstance(slingId2).removeInstance(slingId3);
    async(new Runnable() {

        public void run() {
            mgr.handleNewView(view1);
        }
    });
    Thread.sleep(1000);
    TestHelper.assertNoEvents(listener);
    assertEquals("should have one thread now waiting", 1, serviceSemaphore.getQueueLength());
    // release the first one only
    serviceSemaphore.release(1);
    Thread.sleep(1000);
    assertEvents(listener, EventHelper.newInitEvent(view1));
    mgr.handleChanging();
    assertEquals(0, mgr.waitForAsyncEvents(500));
    assertEvents(listener, EventHelper.newChangingEvent(view1));
    async(new Runnable() {

        public void run() {
            mgr.handleNewView(view2);
        }
    });
    logger.debug("run: waiting 1sec");
    Thread.sleep(1000);
    logger.debug("run: asserting no events");
    TestHelper.assertNoEvents(listener);
    assertEquals("should have one thread now waiting", 1, serviceSemaphore.getQueueLength());
    assertFalse("should not be locked", lock.isLocked());
    logger.debug("run: issuing a second event");
    // before releasing, issue another event, lets do a combination of changing/changed
    async(new Runnable() {

        public void run() {
            logger.debug("run2: calling handleChanging...");
            mgr.handleChanging();
            try {
                logger.debug("run2: done with handleChanging, acquiring testSemaphore...");
                testSemaphore.acquire();
                logger.debug("run2: calling handleNewView...");
                mgr.handleNewView(view3);
                logger.debug("run2: done with handleNewView...");
            } catch (InterruptedException e) {
                // fail
                logger.error("interrupted: " + e, e);
            }
        }
    });
    logger.debug("run: waiting 1sec");
    Thread.sleep(1000);
    int remainingAsyncEvents = mgr.waitForAsyncEvents(2000);
    logger.info("run: result of waitForAsyncEvent is: " + remainingAsyncEvents);
    assertEquals("should have one thread now waiting", 1, serviceSemaphore.getQueueLength());
    assertEquals("should be acquiring (by thread2)", 1, testSemaphore.getQueueLength());
    // releasing the testSemaphore
    testSemaphore.release();
    logger.debug("run: waiting 1sec");
    Thread.sleep(1000);
    assertEquals("should have two async events now in the queue or being sent", 2, mgr.waitForAsyncEvents(500));
    assertEquals("but should only have 1 thread actually sitting on the semaphore waiting", 1, serviceSemaphore.getQueueLength());
    logger.debug("run: releasing consistencyService");
    // release the first one only
    serviceSemaphore.release(1);
    logger.debug("run: waiting 1sec");
    Thread.sleep(1000);
    assertFalse("should not be locked", lock.isLocked());
    // this should not have triggered any event 
    TestHelper.assertNoEvents(listener);
    // then release the 2nd one
    serviceSemaphore.release(1);
    logger.debug("run: waiting 1sec");
    Thread.sleep(1000);
    logger.debug("run: asserting 1 event");
    final TopologyEvent changedEvent = EventHelper.newChangedEvent(view1, view3);
    assertEvents(listener, changedEvent);
    // back to default
    commonsLogger.setLevel(Level.INFO);
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) DummyTopologyView(org.apache.sling.discovery.commons.providers.DummyTopologyView) TopologyEvent(org.apache.sling.discovery.TopologyEvent) Semaphore(java.util.concurrent.Semaphore) DefaultClusterView(org.apache.sling.discovery.commons.providers.DefaultClusterView) Level(org.apache.log4j.Level) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 8 with DefaultClusterView

use of org.apache.sling.discovery.commons.providers.DefaultClusterView 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 9 with DefaultClusterView

use of org.apache.sling.discovery.commons.providers.DefaultClusterView in project sling by apache.

the class TestViewStateManager method testOnlyDiffersInProperties.

@Test
public void testOnlyDiffersInProperties() throws Exception {
    final org.apache.log4j.Logger discoveryLogger = RootLogger.getLogger("org.apache.sling.discovery");
    // changed from Level.DEBUG
    discoveryLogger.setLevel(Level.INFO);
    logger.info("testOnlyDiffersInProperties: start");
    final String slingId1 = UUID.randomUUID().toString();
    final String slingId2 = UUID.randomUUID().toString();
    final String slingId3 = UUID.randomUUID().toString();
    final String clusterId = UUID.randomUUID().toString();
    final DefaultClusterView cluster = new DefaultClusterView(clusterId);
    final DummyTopologyView view1 = new DummyTopologyView().addInstance(slingId1, cluster, true, true).addInstance(slingId2, cluster, false, false).addInstance(slingId3, cluster, false, false);
    final DummyTopologyView view2 = DummyTopologyView.clone(view1).removeInstance(slingId2);
    final DummyTopologyView view3 = DummyTopologyView.clone(view1).removeInstance(slingId2).removeInstance(slingId3);
    DummyTopologyView view1Cloned = DummyTopologyView.clone(view1);
    logger.info("testOnlyDiffersInProperties: handleNewView(view1)");
    mgr.handleNewView(view1);
    logger.info("testOnlyDiffersInProperties: handleActivated()");
    mgr.handleActivated();
    assertEquals(0, mgr.waitForAsyncEvents(5000));
    assertFalse(mgr.onlyDiffersInProperties(view1));
    assertFalse(mgr.onlyDiffersInProperties(view2));
    assertFalse(mgr.onlyDiffersInProperties(view3));
    logger.info("testOnlyDiffersInProperties: handleNewView(view2)");
    mgr.handleNewView(view2);
    assertEquals(0, mgr.waitForAsyncEvents(5000));
    assertFalse(mgr.onlyDiffersInProperties(view1));
    assertFalse(mgr.onlyDiffersInProperties(view2));
    assertFalse(mgr.onlyDiffersInProperties(view3));
    logger.info("testOnlyDiffersInProperties: handleNewView(view3)");
    mgr.handleNewView(view3);
    assertEquals(0, mgr.waitForAsyncEvents(5000));
    assertFalse(mgr.onlyDiffersInProperties(view1));
    assertFalse(mgr.onlyDiffersInProperties(view2));
    assertFalse(mgr.onlyDiffersInProperties(view3));
    final DummyTopologyView view4 = DummyTopologyView.clone(view1Cloned);
    final DummyTopologyView view5 = DummyTopologyView.clone(view1Cloned);
    final DummyTopologyView view6 = DummyTopologyView.clone(view1Cloned);
    logger.info("testOnlyDiffersInProperties: handleNewView(view1cloned)");
    mgr.handleNewView(view1Cloned);
    assertEquals(0, mgr.waitForAsyncEvents(5000));
    DefaultInstanceDescription i4_1 = (DefaultInstanceDescription) view4.getInstance(slingId1);
    i4_1.setProperty("a", "b");
    logger.info("testOnlyDiffersInProperties: onlyDiffersInProperties(view4)");
    assertTrue(mgr.onlyDiffersInProperties(view4));
    DefaultInstanceDescription i5_1 = (DefaultInstanceDescription) view5.getInstance(slingId1);
    i5_1.setProperty("a", "b");
    logger.info("testOnlyDiffersInProperties: onlyDiffersInProperties(view5)");
    assertTrue(mgr.onlyDiffersInProperties(view5));
    DummyTopologyView view4Cloned = DummyTopologyView.clone(view4);
    mgr.handleNewView(view4);
    assertEquals(0, mgr.waitForAsyncEvents(5000));
    logger.info("testOnlyDiffersInProperties: onlyDiffersInProperties(view4Cloned)");
    assertFalse(mgr.onlyDiffersInProperties(view4Cloned));
    logger.info("testOnlyDiffersInProperties: onlyDiffersInProperties(view5)");
    assertFalse(mgr.onlyDiffersInProperties(view5));
    DefaultInstanceDescription i6_1 = (DefaultInstanceDescription) view6.getInstance(slingId1);
    i6_1.setProperty("a", "c");
    logger.info("testOnlyDiffersInProperties: onlyDiffersInProperties(view6)");
    assertTrue(mgr.onlyDiffersInProperties(view6));
    String originalId = view6.getLocalClusterSyncTokenId();
    view6.setId(UUID.randomUUID().toString());
    logger.info("testOnlyDiffersInProperties: onlyDiffersInProperties(view6) [2]");
    assertFalse(mgr.onlyDiffersInProperties(view6));
    view6.setId(originalId);
    logger.info("testOnlyDiffersInProperties: onlyDiffersInProperties(view6) [3]");
    assertTrue(mgr.onlyDiffersInProperties(view6));
    view6.setId(null);
    logger.info("testOnlyDiffersInProperties: onlyDiffersInProperties(view6) [4]");
    assertFalse(mgr.onlyDiffersInProperties(view6));
    view6.setId(originalId);
    logger.info("testOnlyDiffersInProperties: onlyDiffersInProperties(view6) [5]");
    assertTrue(mgr.onlyDiffersInProperties(view6));
    // hack: we're modifying the view *in the ViewStateManagerImpl* here!!:
    view4.setId(null);
    view6.setId(null);
    logger.info("testOnlyDiffersInProperties: onlyDiffersInProperties(view6) [6]");
    assertTrue(mgr.onlyDiffersInProperties(view6));
    view6.setId(originalId);
    logger.info("testOnlyDiffersInProperties: onlyDiffersInProperties(view6) [7]");
    assertFalse(mgr.onlyDiffersInProperties(view6));
}
Also used : DefaultClusterView(org.apache.sling.discovery.commons.providers.DefaultClusterView) DummyTopologyView(org.apache.sling.discovery.commons.providers.DummyTopologyView) DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription) Test(org.junit.Test)

Example 10 with DefaultClusterView

use of org.apache.sling.discovery.commons.providers.DefaultClusterView in project sling by apache.

the class TestViewStateManager method testClusterSyncService_noConcurrency.

@Test
public void testClusterSyncService_noConcurrency() throws Exception {
    final org.apache.log4j.Logger commonsLogger = RootLogger.getLogger("org.apache.sling.discovery.commons.providers");
    //        final org.apache.log4j.Level logLevel = commonsLogger.getLevel();
    // change here to DEBUG in case of issues with this test
    commonsLogger.setLevel(Level.INFO);
    final Semaphore serviceSemaphore = new Semaphore(0);
    final ReentrantLock lock = new ReentrantLock();
    final ClusterSyncServiceWithSemaphore cs = new ClusterSyncServiceWithSemaphore(lock, serviceSemaphore);
    mgr = new ViewStateManagerImpl(lock, cs);
    final DummyListener listener = new DummyListener();
    mgr.bind(listener);
    TestHelper.assertNoEvents(listener);
    mgr.handleActivated();
    TestHelper.assertNoEvents(listener);
    final String slingId1 = UUID.randomUUID().toString();
    final String slingId2 = UUID.randomUUID().toString();
    final String clusterId = UUID.randomUUID().toString();
    final DefaultClusterView cluster = new DefaultClusterView(clusterId);
    final DummyTopologyView view1 = new DummyTopologyView().addInstance(slingId1, cluster, true, true).addInstance(slingId2, cluster, false, false);
    async(new Runnable() {

        @Override
        public void run() {
            mgr.handleNewView(view1);
        }
    });
    Thread.sleep(1000);
    TestHelper.assertNoEvents(listener);
    serviceSemaphore.release(1);
    Thread.sleep(1000);
    assertEvents(listener, EventHelper.newInitEvent(view1));
    final DummyTopologyView view2 = view1.clone();
    mgr.handleChanging();
    assertEvents(listener, EventHelper.newChangingEvent(view1));
    view2.removeInstance(slingId2);
    async(new Runnable() {

        @Override
        public void run() {
            mgr.handleNewView(view2);
        }
    });
    logger.debug("run: waiting for 1sec");
    Thread.sleep(1000);
    logger.debug("run: asserting no events");
    TestHelper.assertNoEvents(listener);
    logger.debug("run: releasing consistencyService");
    serviceSemaphore.release(1);
    logger.debug("run: waiting 1sec");
    Thread.sleep(1000);
    logger.debug("run: asserting 1 event");
    assertEvents(listener, EventHelper.newChangedEvent(view1, view2));
    // back to default
    commonsLogger.setLevel(Level.INFO);
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) DummyTopologyView(org.apache.sling.discovery.commons.providers.DummyTopologyView) Semaphore(java.util.concurrent.Semaphore) DefaultClusterView(org.apache.sling.discovery.commons.providers.DefaultClusterView) Test(org.junit.Test)

Aggregations

DefaultClusterView (org.apache.sling.discovery.commons.providers.DefaultClusterView)15 DefaultInstanceDescription (org.apache.sling.discovery.commons.providers.DefaultInstanceDescription)11 InstanceDescription (org.apache.sling.discovery.InstanceDescription)6 Test (org.junit.Test)6 DefaultTopologyView (org.apache.sling.discovery.base.commons.DefaultTopologyView)5 DummyTopologyView (org.apache.sling.discovery.commons.providers.DummyTopologyView)5 URL (java.net.URL)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Semaphore (java.util.concurrent.Semaphore)2 ReentrantLock (java.util.concurrent.locks.ReentrantLock)2 Activate (org.apache.felix.scr.annotations.Activate)2 LoginException (org.apache.sling.api.resource.LoginException)2 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)2 PersistenceException (org.apache.sling.api.resource.PersistenceException)2 ClusterView (org.apache.sling.discovery.ClusterView)2 TopologyEventListener (org.apache.sling.discovery.TopologyEventListener)2 ClusterSyncService (org.apache.sling.discovery.commons.providers.spi.ClusterSyncService)2 StringReader (java.io.StringReader)1