Search in sources :

Example 51 with InstanceDescription

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

the class TopologyCapabilitiesTest method setup.

@Before
public void setup() {
    // local cluster view
    final ClusterView cv = Mockito.mock(ClusterView.class);
    Mockito.when(cv.getId()).thenReturn("cluster");
    // local description
    final InstanceDescription local = Mockito.mock(InstanceDescription.class);
    Mockito.when(local.isLeader()).thenReturn(true);
    Mockito.when(local.getSlingId()).thenReturn("local");
    Mockito.when(local.getProperty(TopologyCapabilities.PROPERTY_TOPICS)).thenReturn("foo,bar/*,a/**,d/1/2,d/1/*,d/**");
    Mockito.when(local.getClusterView()).thenReturn(cv);
    // topology view
    final TopologyView tv = Mockito.mock(TopologyView.class);
    Mockito.when(tv.getInstances()).thenReturn(Collections.singleton(local));
    Mockito.when(tv.getLocalInstance()).thenReturn(local);
    final JobManagerConfiguration config = Mockito.mock(JobManagerConfiguration.class);
    caps = new TopologyCapabilities(tv, config);
}
Also used : ClusterView(org.apache.sling.discovery.ClusterView) InstanceDescription(org.apache.sling.discovery.InstanceDescription) TopologyView(org.apache.sling.discovery.TopologyView) Before(org.junit.Before)

Example 52 with InstanceDescription

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

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

the class SyncTokenService method seenAllSyncTokens.

private boolean seenAllSyncTokens(BaseTopologyView view) {
    logger.trace("seenAllSyncTokens: start");
    ResourceResolver resourceResolver = null;
    try {
        resourceResolver = getResourceResolver();
        Resource resource = ResourceHelper.getOrCreateResource(resourceResolver, getSyncTokenPath());
        ValueMap syncTokens = resource.adaptTo(ValueMap.class);
        String syncToken = view.getLocalClusterSyncTokenId();
        boolean success = true;
        StringBuffer historyEntry = new StringBuffer();
        for (InstanceDescription instance : view.getLocalInstance().getClusterView().getInstances()) {
            Object currentValue = syncTokens.get(instance.getSlingId());
            if (currentValue == null) {
                String msg = "no syncToken yet of " + instance.getSlingId();
                logger.info("seenAllSyncTokens: " + msg);
                if (historyEntry.length() != 0) {
                    historyEntry.append(",");
                }
                historyEntry.append(msg);
                success = false;
            } else if (!syncToken.equals(currentValue)) {
                String msg = "syncToken of " + instance.getSlingId() + " is " + currentValue + " waiting for " + syncToken;
                logger.info("seenAllSyncTokens: " + msg);
                if (historyEntry.length() != 0) {
                    historyEntry.append(",");
                }
                historyEntry.append(msg);
                success = false;
            }
        }
        if (!success) {
            logger.info("seenAllSyncTokens: not yet seen all expected syncTokens (see above for details)");
            clusterSyncHistory.addHistoryEntry(view, historyEntry.toString());
            return false;
        } else {
            clusterSyncHistory.addHistoryEntry(view, "seen all syncTokens");
        }
        resourceResolver.commit();
        logger.info("seenAllSyncTokens: seen all syncTokens!");
        return true;
    } catch (LoginException e) {
        logger.error("seenAllSyncTokens: could not login: " + e, e);
        return false;
    } catch (PersistenceException e) {
        logger.error("seenAllSyncTokens: got PersistenceException: " + e, e);
        return false;
    } finally {
        logger.trace("seenAllSyncTokens: end");
        if (resourceResolver != null) {
            resourceResolver.close();
        }
    }
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException) LoginException(org.apache.sling.api.resource.LoginException) InstanceDescription(org.apache.sling.discovery.InstanceDescription)

Example 54 with InstanceDescription

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

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

the class BaseTopologyView method toShortString.

public String toShortString() {
    StringBuffer sb = new StringBuffer();
    for (InstanceDescription instance : getInstances()) {
        if (sb.length() != 0) {
            sb.append(",");
        }
        sb.append(instance.getSlingId());
        sb.append("[");
        sb.append("local=");
        sb.append(instance.isLocal());
        sb.append(",leader=");
        sb.append(instance.isLeader());
        sb.append("]");
    }
    return "DefaultTopologyView[current=" + isCurrent() + ", num=" + getInstances().size() + ", instances=" + sb.toString() + "]";
}
Also used : InstanceDescription(org.apache.sling.discovery.InstanceDescription)

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