Search in sources :

Example 1 with DefaultClusterView

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

the class Announcement method asClusterView.

/** create a clusterview from json **/
private static ClusterView asClusterView(final String localClusterViewJSON) {
    JsonObject obj = jsonReaderFactory.createReader(new StringReader(localClusterViewJSON)).readObject();
    DefaultClusterView clusterView = new DefaultClusterView(obj.getString("id"));
    JsonArray instancesObj = obj.getJsonArray("instances");
    for (int i = 0; i < instancesObj.size(); i++) {
        JsonObject anInstance = instancesObj.getJsonObject(i);
        clusterView.addInstanceDescription(asInstance(anInstance));
    }
    return clusterView;
}
Also used : JsonArray(javax.json.JsonArray) DefaultClusterView(org.apache.sling.discovery.commons.providers.DefaultClusterView) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject)

Example 2 with DefaultClusterView

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

the class AnnouncementRegistryImplTest method testLists.

@Test
public void testLists() throws Exception {
    try {
        registry.listAnnouncementsInSameCluster(null);
        fail("should complain");
    } catch (IllegalArgumentException iae) {
    // ok
    }
    try {
        registry.listAnnouncementsInSameCluster(null);
        fail("should complain");
    } catch (IllegalArgumentException iae) {
    // ok
    }
    assertEquals(0, registry.listLocalAnnouncements().size());
    assertEquals(0, registry.listLocalIncomingAnnouncements().size());
    DefaultClusterView localCluster = new DefaultClusterView(UUID.randomUUID().toString());
    DefaultInstanceDescription instance = TopologyHelper.createInstanceDescription(slingId, true, localCluster);
    assertEquals(0, registry.listAnnouncementsInSameCluster(localCluster).size());
    assertEquals(0, registry.listLocalAnnouncements().size());
    assertEquals(0, registry.listLocalIncomingAnnouncements().size());
    Announcement ann = new Announcement(slingId);
    ann.setLocalCluster(localCluster);
    ann.setInherited(true);
    registry.registerAnnouncement(ann);
    assertEquals(1, registry.listAnnouncementsInSameCluster(localCluster).size());
    assertEquals(1, registry.listLocalAnnouncements().size());
    assertEquals(0, registry.listLocalIncomingAnnouncements().size());
    ann.setInherited(true);
    assertEquals(0, registry.listLocalIncomingAnnouncements().size());
    assertTrue(registry.hasActiveAnnouncement(slingId));
    assertFalse(registry.hasActiveAnnouncement(UUID.randomUUID().toString()));
    registry.unregisterAnnouncement(slingId);
    assertEquals(0, registry.listAnnouncementsInSameCluster(localCluster).size());
    assertEquals(0, registry.listLocalAnnouncements().size());
    assertEquals(0, registry.listLocalIncomingAnnouncements().size());
    assertFalse(registry.hasActiveAnnouncement(slingId));
    assertFalse(registry.hasActiveAnnouncement(UUID.randomUUID().toString()));
    ann.setInherited(false);
    registry.registerAnnouncement(ann);
    assertEquals(1, registry.listAnnouncementsInSameCluster(localCluster).size());
    assertEquals(1, registry.listLocalAnnouncements().size());
    assertEquals(1, registry.listLocalIncomingAnnouncements().size());
    assertTrue(registry.hasActiveAnnouncement(slingId));
    assertFalse(registry.hasActiveAnnouncement(UUID.randomUUID().toString()));
    registry.unregisterAnnouncement(slingId);
    assertEquals(0, registry.listAnnouncementsInSameCluster(localCluster).size());
    assertEquals(0, registry.listLocalAnnouncements().size());
    assertEquals(0, registry.listLocalIncomingAnnouncements().size());
    assertFalse(registry.hasActiveAnnouncement(slingId));
    assertFalse(registry.hasActiveAnnouncement(UUID.randomUUID().toString()));
    assertEquals(1, ann.listInstances().size());
    registry.addAllExcept(ann, localCluster, new AnnouncementFilter() {

        public boolean accept(String receivingSlingId, Announcement announcement) {
            assertNotNull(receivingSlingId);
            assertNotNull(announcement);
            return true;
        }
    });
    assertEquals(1, ann.listInstances().size());
    registry.registerAnnouncement(createAnnouncement(createCluster(3), 1, false));
    assertEquals(1, registry.listAnnouncementsInSameCluster(localCluster).size());
    assertEquals(3, registry.listInstances(localCluster).size());
    registry.addAllExcept(ann, localCluster, new AnnouncementFilter() {

        public boolean accept(String receivingSlingId, Announcement announcement) {
            assertNotNull(receivingSlingId);
            assertNotNull(announcement);
            return true;
        }
    });
    assertEquals(4, ann.listInstances().size());
    registry.registerAnnouncement(ann);
    assertEquals(2, registry.listAnnouncementsInSameCluster(localCluster).size());
}
Also used : DefaultClusterView(org.apache.sling.discovery.commons.providers.DefaultClusterView) DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription) Test(org.junit.Test)

Example 3 with DefaultClusterView

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

the class TopologyHelper method createTopologyView.

public static DefaultTopologyView createTopologyView(String clusterViewId, String slingId) {
    DefaultTopologyView t = new DefaultTopologyView();
    DefaultClusterView c = new DefaultClusterView(clusterViewId);
    DefaultInstanceDescription i = new DefaultInstanceDescription(c, true, false, slingId, new HashMap<String, String>());
    Collection<InstanceDescription> instances = new LinkedList<InstanceDescription>();
    instances.add(i);
    t.addInstances(instances);
    return t;
}
Also used : DefaultTopologyView(org.apache.sling.discovery.base.commons.DefaultTopologyView) DefaultClusterView(org.apache.sling.discovery.commons.providers.DefaultClusterView) InstanceDescription(org.apache.sling.discovery.InstanceDescription) DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription) DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription) LinkedList(java.util.LinkedList)

Example 4 with DefaultClusterView

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

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

the class TopologyHelper method cloneTopologyView.

public static DefaultTopologyView cloneTopologyView(DefaultTopologyView original) {
    DefaultTopologyView t = new DefaultTopologyView();
    Iterator<ClusterView> it = original.getClusterViews().iterator();
    while (it.hasNext()) {
        DefaultClusterView c = (DefaultClusterView) it.next();
        t.addInstances(clone(c).getInstances());
    }
    return t;
}
Also used : ClusterView(org.apache.sling.discovery.ClusterView) DefaultClusterView(org.apache.sling.discovery.commons.providers.DefaultClusterView) DefaultTopologyView(org.apache.sling.discovery.base.commons.DefaultTopologyView) DefaultClusterView(org.apache.sling.discovery.commons.providers.DefaultClusterView)

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