Search in sources :

Example 6 with DefaultInstanceDescription

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

the class TopologyHelper method cloneTopologyView.

public static DefaultTopologyView cloneTopologyView(DefaultTopologyView view, String newLeader) throws NoSuchFieldException {
    final DefaultTopologyView clone = cloneTopologyView(view);
    final DefaultClusterView cluster = (DefaultClusterView) clone.getClusterViews().iterator().next();
    for (Iterator it = cluster.getInstances().iterator(); it.hasNext(); ) {
        DefaultInstanceDescription id = (DefaultInstanceDescription) it.next();
        PrivateAccessor.setField(id, "isLeader", id.getSlingId().equals(newLeader));
    }
    return clone;
}
Also used : DefaultTopologyView(org.apache.sling.discovery.base.commons.DefaultTopologyView) DefaultClusterView(org.apache.sling.discovery.commons.providers.DefaultClusterView) Iterator(java.util.Iterator) DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription)

Example 7 with DefaultInstanceDescription

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

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

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

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

the class TestViewStateManager method testPropertiesChanged.

@Test
public void testPropertiesChanged() throws Exception {
    final DummyListener listener = new DummyListener();
    mgr.handleActivated();
    mgr.bind(listener);
    mgr.handleChanging();
    DummyTopologyView oldView = new DummyTopologyView().addInstance();
    DefaultInstanceDescription localInstance = (DefaultInstanceDescription) oldView.getLocalInstance();
    localInstance.setProperty("foo", "bar1");
    mgr.handleNewView(oldView);
    TopologyEvent initEvent = EventHelper.newInitEvent(oldView.clone());
    assertEvents(listener, initEvent);
    DummyTopologyView newView = oldView.clone();
    oldView.setNotCurrent();
    localInstance = (DefaultInstanceDescription) newView.getLocalInstance();
    localInstance.setProperty("foo", "bar2");
    mgr.handleNewView(newView);
    Thread.sleep(2000);
    TopologyEvent propertiesChangedEvent = EventHelper.newPropertiesChangedEvent(oldView.clone(), newView.clone());
    assertEvents(listener, propertiesChangedEvent);
}
Also used : DummyTopologyView(org.apache.sling.discovery.commons.providers.DummyTopologyView) TopologyEvent(org.apache.sling.discovery.TopologyEvent) DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription) Test(org.junit.Test)

Aggregations

DefaultInstanceDescription (org.apache.sling.discovery.commons.providers.DefaultInstanceDescription)16 DefaultClusterView (org.apache.sling.discovery.commons.providers.DefaultClusterView)11 InstanceDescription (org.apache.sling.discovery.InstanceDescription)8 Test (org.junit.Test)7 DefaultTopologyView (org.apache.sling.discovery.base.commons.DefaultTopologyView)4 DummyTopologyView (org.apache.sling.discovery.commons.providers.DummyTopologyView)4 HashMap (java.util.HashMap)3 LoginException (org.apache.sling.api.resource.LoginException)3 PersistenceException (org.apache.sling.api.resource.PersistenceException)3 URL (java.net.URL)2 Collection (java.util.Collection)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 Activate (org.apache.felix.scr.annotations.Activate)2 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)2 TopologyEventListener (org.apache.sling.discovery.TopologyEventListener)2 UndefinedClusterViewException (org.apache.sling.discovery.base.commons.UndefinedClusterViewException)2 ClusterSyncService (org.apache.sling.discovery.commons.providers.spi.ClusterSyncService)2 LocalClusterView (org.apache.sling.discovery.commons.providers.spi.LocalClusterView)2 Iterator (java.util.Iterator)1