Search in sources :

Example 11 with InstanceDescription

use of org.apache.sling.discovery.InstanceDescription 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 12 with InstanceDescription

use of org.apache.sling.discovery.InstanceDescription in project sling by apache.

the class TopologyHelper method assertTopologyConsistsOf.

public static void assertTopologyConsistsOf(TopologyView topology, String... slingIds) {
    assertNotNull(topology);
    assertEquals(slingIds.length, topology.getInstances().size());
    for (int i = 0; i < slingIds.length; i++) {
        final String aSlingId = slingIds[i];
        final Set<?> instances = topology.getInstances();
        boolean found = false;
        for (Iterator<?> it = instances.iterator(); it.hasNext(); ) {
            InstanceDescription anInstance = (InstanceDescription) it.next();
            if (anInstance.getSlingId().equals(aSlingId)) {
                found = true;
                break;
            }
        }
        assertTrue(found);
    }
}
Also used : InstanceDescription(org.apache.sling.discovery.InstanceDescription) DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription)

Example 13 with InstanceDescription

use of org.apache.sling.discovery.InstanceDescription in project sling by apache.

the class VirtualInstance method getLocalInstanceDescription.

public InstanceDescription getLocalInstanceDescription() throws UndefinedClusterViewException {
    final Iterator<InstanceDescription> it = getClusterViewService().getLocalClusterView().getInstances().iterator();
    while (it.hasNext()) {
        final InstanceDescription id = it.next();
        if (slingId.equals(id.getSlingId())) {
            return id;
        }
    }
    fail("no local instanceDescription found");
    // never called:
    return null;
}
Also used : InstanceDescription(org.apache.sling.discovery.InstanceDescription)

Example 14 with InstanceDescription

use of org.apache.sling.discovery.InstanceDescription in project sling by apache.

the class TopologyTest method testTwoNodes.

@Test
public void testTwoNodes() throws Throwable {
    VirtualInstanceBuilder builder1 = newBuilder().newRepository("/var/discovery/impl/", true).setDebugName("instance1").setConnectorPingInterval(20).setConnectorPingTimeout(200);
    VirtualInstance instance1 = builder1.build();
    instances.add(instance1);
    VirtualInstanceBuilder builder2 = newBuilder().useRepositoryOf(builder1).setDebugName("instance2").setConnectorPingInterval(20).setConnectorPingTimeout(200);
    VirtualInstance instance2 = builder2.build();
    instances.add(instance2);
    instance1.getConfig().setViewCheckTimeout(8);
    instance1.getConfig().setViewCheckInterval(1);
    instance2.getConfig().setViewCheckTimeout(2);
    instance2.getConfig().setViewCheckInterval(1);
    for (int i = 0; i < 5; i++) {
        instance1.heartbeatsAndCheckView();
        instance2.heartbeatsAndCheckView();
        Thread.sleep(500);
    }
    Set<InstanceDescription> instances1 = instance1.getDiscoveryService().getTopology().getInstances();
    Set<InstanceDescription> instances2 = instance2.getDiscoveryService().getTopology().getInstances();
    assertEquals(1, instances1.size());
    assertEquals(1, instances2.size());
    assertEquals(instance1.getSlingId(), instances1.iterator().next().getSlingId());
    assertEquals(instance2.getSlingId(), instances2.iterator().next().getSlingId());
    new VirtualConnector(instance1, instance2);
    // check instance 1's announcements
    Collection<Announcement> instance1LocalAnnouncements = instance1.getAnnouncementRegistry().listLocalAnnouncements();
    assertEquals(1, instance1LocalAnnouncements.size());
    Announcement instance1LocalAnnouncement = instance1LocalAnnouncements.iterator().next();
    assertEquals(instance2.getSlingId(), instance1LocalAnnouncement.getOwnerId());
    assertEquals(true, instance1LocalAnnouncement.isInherited());
    // check instance 2's announcements
    Collection<Announcement> instance2LocalAnnouncements = instance2.getAnnouncementRegistry().listLocalAnnouncements();
    assertEquals(1, instance2LocalAnnouncements.size());
    Announcement instance2LocalAnnouncement = instance2LocalAnnouncements.iterator().next();
    assertEquals(instance1.getSlingId(), instance2LocalAnnouncement.getOwnerId());
    assertEquals(false, instance2LocalAnnouncement.isInherited());
    // check topology
    TopologyHelper.assertTopologyConsistsOf(instance1.getDiscoveryService().getTopology(), instance1.getSlingId(), instance2.getSlingId());
    TopologyHelper.assertTopologyConsistsOf(instance2.getDiscoveryService().getTopology(), instance1.getSlingId(), instance2.getSlingId());
    instance1LocalAnnouncements = instance1.getAnnouncementRegistry().listLocalAnnouncements();
    assertEquals(1, instance1LocalAnnouncements.size());
    instance2LocalAnnouncements = instance2.getAnnouncementRegistry().listLocalAnnouncements();
    assertEquals(1, instance2LocalAnnouncements.size());
    // sleep of 2.2sec ensures instance2's heartbeat timeout (which is 2sec) hits
    Thread.sleep(2200);
    instance1LocalAnnouncements = instance1.getAnnouncementRegistry().listLocalAnnouncements();
    assertEquals(1, instance1LocalAnnouncements.size());
    instance2LocalAnnouncements = instance2.getAnnouncementRegistry().listLocalAnnouncements();
    assertEquals(0, instance2LocalAnnouncements.size());
    logger.info("testTwoNodes: instance1: " + instance1.getSlingId());
    instance1.dumpRepo();
    logger.info("testTwoNodes: instance2: " + instance2.getSlingId());
    instance2.dumpRepo();
    TopologyHelper.assertTopologyConsistsOf(instance1.getDiscoveryService().getTopology(), instance1.getSlingId(), instance2.getSlingId());
    TopologyHelper.assertTopologyConsistsOf(instance2.getDiscoveryService().getTopology(), instance2.getSlingId());
    // another sleep 6s (2.2+6 = 8.2sec) ensures instance1's heartbeat timeout (which is 8sec) hits as well
    Thread.sleep(6000);
    instance1LocalAnnouncements = instance1.getAnnouncementRegistry().listLocalAnnouncements();
    assertEquals(0, instance1LocalAnnouncements.size());
    instance2LocalAnnouncements = instance2.getAnnouncementRegistry().listLocalAnnouncements();
    assertEquals(0, instance2LocalAnnouncements.size());
    TopologyHelper.assertTopologyConsistsOf(instance1.getDiscoveryService().getTopology(), instance1.getSlingId());
    TopologyHelper.assertTopologyConsistsOf(instance2.getDiscoveryService().getTopology(), instance2.getSlingId());
}
Also used : VirtualInstanceBuilder(org.apache.sling.discovery.base.its.setup.VirtualInstanceBuilder) DummyVirtualInstanceBuilder(org.apache.sling.discovery.base.connectors.DummyVirtualInstanceBuilder) VirtualConnector(org.apache.sling.discovery.base.its.setup.VirtualConnector) Announcement(org.apache.sling.discovery.base.connectors.announcement.Announcement) InstanceDescription(org.apache.sling.discovery.InstanceDescription) VirtualInstance(org.apache.sling.discovery.base.its.setup.VirtualInstance) Test(org.junit.Test)

Example 15 with InstanceDescription

use of org.apache.sling.discovery.InstanceDescription in project sling by apache.

the class InstancesDiffTest method testIsNotInClusterView.

@Test
public void testIsNotInClusterView() throws Exception {
    ClusterView clusterView = clusterView("yet-another-view");
    Collection<InstanceDescription> instances = new InstancesDiff(Arrays.asList(new Instance("one", true, false, Collections.<String, String>emptyMap(), "otherView"), new Instance("two", false, true, Collections.<String, String>emptyMap(), "viewId")), empty()).all(true).isInClusterView(clusterView).get();
    TestCase.assertEquals(0, instances.size());
}
Also used : ClusterView(org.apache.sling.discovery.ClusterView) InstanceDescription(org.apache.sling.discovery.InstanceDescription) Test(org.junit.Test)

Aggregations

InstanceDescription (org.apache.sling.discovery.InstanceDescription)59 ClusterView (org.apache.sling.discovery.ClusterView)16 DefaultInstanceDescription (org.apache.sling.discovery.commons.providers.DefaultInstanceDescription)11 Map (java.util.Map)10 PersistenceException (org.apache.sling.api.resource.PersistenceException)10 TopologyView (org.apache.sling.discovery.TopologyView)9 HashMap (java.util.HashMap)8 LoginException (org.apache.sling.api.resource.LoginException)8 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)8 Test (org.junit.Test)8 Announcement (org.apache.sling.discovery.base.connectors.announcement.Announcement)7 UndefinedClusterViewException (org.apache.sling.discovery.base.commons.UndefinedClusterViewException)6 DefaultClusterView (org.apache.sling.discovery.commons.providers.DefaultClusterView)6 HashSet (java.util.HashSet)5 Resource (org.apache.sling.api.resource.Resource)5 ValueMap (org.apache.sling.api.resource.ValueMap)5 LocalClusterView (org.apache.sling.discovery.commons.providers.spi.LocalClusterView)5 ArrayList (java.util.ArrayList)4 Iterator (java.util.Iterator)4 LinkedList (java.util.LinkedList)4