Search in sources :

Example 26 with ClusterView

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

the class OakBacklogClusterSyncService method getBacklogStatus.

private BacklogStatus getBacklogStatus(BaseTopologyView view) {
    logger.trace("getBacklogStatus: start");
    ResourceResolver resourceResolver = null;
    try {
        resourceResolver = getResourceResolver();
        DiscoveryLiteDescriptor descriptor = DiscoveryLiteDescriptor.getDescriptorFrom(resourceResolver);
        // backlog-free means:
        // 1) 'deactivating' must be empty
        //     (otherwise we indeed have a backlog)
        // 2) all active ids of the descriptor must have a mapping to slingIds
        //     (otherwise the init failed or is pending for some instance(s))
        // 3) all 'active' instances must be in the view
        //     (otherwise discovery lite might not yet consider
        //     an instance dead but discovery-service does)
        // instead what is fine from a backlog point of view
        // * instances in the view but listed as 'inactive'
        //     (this might be the case for just-started instances)
        // * instances in the view but not contained in the descriptor at all
        //     (this might be the case for just-started instances)
        int[] activeIds = descriptor.getActiveIds();
        int[] deactivatingIds = descriptor.getDeactivatingIds();
        // 1) 'deactivating' must be empty
        if (deactivatingIds.length != 0) {
            logger.info("getBacklogStatus: there are deactivating instances: " + Arrays.toString(deactivatingIds));
            return BacklogStatus.HAS_BACKLOG;
        }
        ClusterView cluster = view.getLocalInstance().getClusterView();
        Set<String> slingIds = new HashSet<>();
        for (InstanceDescription instance : cluster.getInstances()) {
            slingIds.add(instance.getSlingId());
        }
        for (int i = 0; i < activeIds.length; i++) {
            int activeId = activeIds[i];
            String slingId = idMapService.toSlingId(activeId, resourceResolver);
            // 2) all ids of the descriptor must have a mapping to slingIds
            if (slingId == null) {
                logger.info("getBacklogStatus: no slingId found for active id: " + activeId);
                return BacklogStatus.UNDEFINED;
            }
            // 3) all 'active' instances must be in the view
            if (!slingIds.contains(slingId)) {
                logger.info("getBacklogStatus: active instance's (" + activeId + ") slingId (" + slingId + ") not found in cluster (" + cluster + ")");
                return BacklogStatus.HAS_BACKLOG;
            }
        }
        logger.info("getBacklogStatus: no backlog (anymore)");
        return BacklogStatus.NO_BACKLOG;
    } catch (Exception e) {
        logger.info("getBacklogStatus: failed to determine backlog status: " + e);
        return BacklogStatus.UNDEFINED;
    } finally {
        logger.trace("getBacklogStatus: end");
        if (resourceResolver != null) {
            resourceResolver.close();
        }
    }
}
Also used : ClusterView(org.apache.sling.discovery.ClusterView) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) InstanceDescription(org.apache.sling.discovery.InstanceDescription) LoginException(org.apache.sling.api.resource.LoginException) HashSet(java.util.HashSet)

Example 27 with ClusterView

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

the class AbstractClusterTest method createFromAnnouncement.

private Announcement createFromAnnouncement(final VirtualInstance from) throws UndefinedClusterViewException {
    // TODO: refactor TopologyConnectorClient to avoid duplicating code from there (ping())
    Announcement topologyAnnouncement = new Announcement(from.slingId);
    topologyAnnouncement.setServerInfo(from.slingId);
    final ClusterView clusterView = from.getClusterViewService().getLocalClusterView();
    topologyAnnouncement.setLocalCluster(clusterView);
    from.getAnnouncementRegistry().addAllExcept(topologyAnnouncement, clusterView, new AnnouncementFilter() {

        @Override
        public boolean accept(final String receivingSlingId, final Announcement announcement) {
            // filter out announcements that are of old cluster instances
            // which I dont really have in my cluster view at the moment
            final Iterator<InstanceDescription> it = clusterView.getInstances().iterator();
            while (it.hasNext()) {
                final InstanceDescription instance = it.next();
                if (instance.getSlingId().equals(receivingSlingId)) {
                    // all fine then
                    return true;
                }
            }
            // then I should also not propagate that announcement anywhere
            return false;
        }
    });
    return topologyAnnouncement;
}
Also used : ClusterView(org.apache.sling.discovery.ClusterView) Announcement(org.apache.sling.discovery.base.connectors.announcement.Announcement) Iterator(java.util.Iterator) AnnouncementFilter(org.apache.sling.discovery.base.connectors.announcement.AnnouncementFilter) InstanceDescription(org.apache.sling.discovery.InstanceDescription)

Example 28 with ClusterView

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

the class AbstractSingleInstanceTest method testGetters.

@Test
public void testGetters() throws UndefinedClusterViewException, InterruptedException {
    logger.info("testGetters: start");
    assertNotNull(instance);
    logger.info("sling id=" + instance.getSlingId());
    try {
        instance.getClusterViewService().getLocalClusterView();
        // SLING-5030
        fail("should complain");
    } catch (UndefinedClusterViewException e) {
    // ok
    }
    instance.heartbeatsAndCheckView();
    // wait 4000ms for the vote to happen
    Thread.sleep(4000);
    assertNotNull(instance.getClusterViewService().getLocalClusterView());
    ClusterView cv = instance.getClusterViewService().getLocalClusterView();
    logger.info("cluster view: id=" + cv.getId());
    assertNotNull(cv.getId());
    assertNotSame(cv.getId(), "");
    List<InstanceDescription> instances = cv.getInstances();
    assertNotNull(instances);
    assertTrue(instances.size() == 1);
    InstanceDescription myInstance = instances.get(0);
    assertNotNull(myInstance);
    assertTrue(myInstance.getClusterView() == cv);
    logger.info("instance id: " + myInstance.getSlingId());
    assertEquals(instance.getSlingId(), myInstance.getSlingId());
    Map<String, String> properties = myInstance.getProperties();
    assertNotNull(properties);
    assertNull(myInstance.getProperty("foo"));
    assertTrue(myInstance.isLeader());
    assertTrue(myInstance.isLocal());
    logger.info("testGetters: end");
}
Also used : ClusterView(org.apache.sling.discovery.ClusterView) UndefinedClusterViewException(org.apache.sling.discovery.base.commons.UndefinedClusterViewException) InstanceDescription(org.apache.sling.discovery.InstanceDescription) Test(org.junit.Test)

Example 29 with ClusterView

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

the class InstancesDiffTest method testIsInClusterView.

@Test
public void testIsInClusterView() throws Exception {
    ClusterView clusterView = clusterView("viewId");
    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(1, instances.size());
    TestCase.assertEquals("two", instances.iterator().next().getSlingId());
}
Also used : ClusterView(org.apache.sling.discovery.ClusterView) InstanceDescription(org.apache.sling.discovery.InstanceDescription) Test(org.junit.Test)

Example 30 with ClusterView

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

the class InstancesDiffTest method clusterView.

private ClusterView clusterView(String clusterViewId) {
    ClusterView clusterView = Mockito.mock(ClusterView.class);
    Mockito.when(clusterView.getId()).thenReturn(clusterViewId);
    return clusterView;
}
Also used : ClusterView(org.apache.sling.discovery.ClusterView)

Aggregations

ClusterView (org.apache.sling.discovery.ClusterView)30 InstanceDescription (org.apache.sling.discovery.InstanceDescription)16 TopologyView (org.apache.sling.discovery.TopologyView)8 Announcement (org.apache.sling.discovery.base.connectors.announcement.Announcement)6 DefaultClusterView (org.apache.sling.discovery.commons.providers.DefaultClusterView)6 Test (org.junit.Test)6 Iterator (java.util.Iterator)4 UndefinedClusterViewException (org.apache.sling.discovery.base.commons.UndefinedClusterViewException)4 AnnouncementFilter (org.apache.sling.discovery.base.connectors.announcement.AnnouncementFilter)4 HashSet (java.util.HashSet)3 GZIPOutputStream (java.util.zip.GZIPOutputStream)2 JsonException (javax.json.JsonException)2 CachedAnnouncement (org.apache.sling.discovery.base.connectors.announcement.CachedAnnouncement)2 TopologyConnectorClientInformation (org.apache.sling.discovery.base.connectors.ping.TopologyConnectorClientInformation)2 LocalClusterView (org.apache.sling.discovery.commons.providers.spi.LocalClusterView)2 ClusterSyncHistory (org.apache.sling.discovery.commons.providers.spi.base.ClusterSyncHistory)2 Before (org.junit.Before)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1