Search in sources :

Example 1 with DefaultInstanceDescription

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

the class DefaultTopologyViewTest method createSingleInstanceTopology.

private DefaultTopologyView createSingleInstanceTopology(String slingId, String clusterViewId, String syncTokenId) {
    LocalClusterView clusterView = new LocalClusterView(clusterViewId, syncTokenId);
    DefaultInstanceDescription instance = TopologyHelper.createInstanceDescription(slingId, true, clusterView);
    DefaultTopologyView t = new DefaultTopologyView();
    t.setLocalClusterView(clusterView);
    return t;
}
Also used : LocalClusterView(org.apache.sling.discovery.commons.providers.spi.LocalClusterView) DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription)

Example 2 with DefaultInstanceDescription

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

the class DefaultTopologyViewTest method testForcedLeaderChangeCompare.

@Test
public void testForcedLeaderChangeCompare() throws Exception {
    // create view 1 with first instance the leader
    final String slingId1 = UUID.randomUUID().toString();
    final DefaultTopologyView view1 = TopologyHelper.createTopologyView(UUID.randomUUID().toString(), slingId1);
    final DefaultInstanceDescription id2 = TopologyHelper.addInstanceDescription(view1, TopologyHelper.createInstanceDescription(view1.getClusterViews().iterator().next()));
    final String slingId2 = id2.getSlingId();
    final DefaultInstanceDescription id3 = TopologyHelper.addInstanceDescription(view1, TopologyHelper.createInstanceDescription(view1.getClusterViews().iterator().next()));
    final String slingId3 = id3.getSlingId();
    // now create view 2 with exactly the same instances as above, but the second instance the leader
    DefaultTopologyView view2 = TopologyHelper.cloneTopologyView(view1, slingId2);
    // make sure we've chosen a new leader:
    assertNotEquals(view1.getClusterViews().iterator().next().getLeader().getSlingId(), view2.getClusterViews().iterator().next().getLeader().getSlingId());
    // and now test the compare method which should catch the leader change
    assertTrue(view1.compareTopology(view2) == Type.TOPOLOGY_CHANGED);
    // same thing now with view3 which takes slingId3 as the leader
    DefaultTopologyView view3 = TopologyHelper.cloneTopologyView(view1, slingId3);
    // make sure we've chosen a new leader:
    assertNotEquals(view1.getClusterViews().iterator().next().getLeader().getSlingId(), view3.getClusterViews().iterator().next().getLeader().getSlingId());
    // and now test the compare method which should catch the leader change
    assertTrue(view1.compareTopology(view3) == Type.TOPOLOGY_CHANGED);
}
Also used : DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription) Test(org.junit.Test)

Example 3 with DefaultInstanceDescription

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

the class DefaultTopologyViewTest method testFind.

@Test
public void testFind() throws Exception {
    DefaultTopologyView newView = TopologyHelper.createTopologyView(UUID.randomUUID().toString(), UUID.randomUUID().toString());
    TopologyHelper.createAndAddInstanceDescription(newView, newView.getClusterViews().iterator().next());
    try {
        newView.findInstances(null);
        fail("should complain");
    } catch (IllegalArgumentException iae) {
    // ok
    }
    final DefaultInstanceDescription id = TopologyHelper.createAndAddInstanceDescription(newView, newView.getClusterViews().iterator().next());
    TopologyHelper.createAndAddInstanceDescription(newView, newView.getClusterViews().iterator().next());
    assertEquals(4, newView.findInstances(new InstanceFilter() {

        public boolean accept(InstanceDescription instance) {
            return true;
        }
    }).size());
    assertEquals(1, newView.findInstances(new InstanceFilter() {

        public boolean accept(InstanceDescription instance) {
            return instance.getSlingId().equals(id.getSlingId());
        }
    }).size());
    assertEquals(1, newView.findInstances(new InstanceFilter() {

        public boolean accept(InstanceDescription instance) {
            return instance.isLeader();
        }
    }).size());
    assertEquals(1, newView.findInstances(new InstanceFilter() {

        boolean first = true;

        public boolean accept(InstanceDescription instance) {
            if (!first) {
                return false;
            }
            first = false;
            return true;
        }
    }).size());
}
Also used : InstanceFilter(org.apache.sling.discovery.InstanceFilter) InstanceDescription(org.apache.sling.discovery.InstanceDescription) DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription) DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription) Test(org.junit.Test)

Example 4 with DefaultInstanceDescription

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

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

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