Search in sources :

Example 1 with InstanceDescription

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

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

the class AnnouncementRegistryImpl method listAnnouncementsInSameCluster.

@Override
public synchronized Collection<Announcement> listAnnouncementsInSameCluster(final ClusterView localClusterView) {
    logger.debug("listAnnouncementsInSameCluster: start. localClusterView: {}", localClusterView);
    if (localClusterView == null) {
        throw new IllegalArgumentException("clusterView must not be null");
    }
    ResourceResolver resourceResolver = null;
    final Collection<Announcement> incomingAnnouncements = new LinkedList<Announcement>();
    final InstanceDescription localInstance = getLocalInstanceDescription(localClusterView);
    try {
        resourceResolver = resourceResolverFactory.getServiceResourceResolver(null);
        Resource clusterInstancesResource = ResourceHelper.getOrCreateResource(resourceResolver, config.getClusterInstancesPath());
        Iterator<Resource> it0 = clusterInstancesResource.getChildren().iterator();
        while (it0.hasNext()) {
            Resource aClusterInstanceResource = it0.next();
            final String instanceId = aClusterInstanceResource.getName();
            logger.debug("listAnnouncementsInSameCluster: handling clusterInstance: {}", instanceId);
            if (localInstance != null && localInstance.getSlingId().equals(instanceId)) {
                // this is the local instance then - which we serve from the cache only
                logger.debug("listAnnouncementsInSameCluster: matched localInstance, filling with cache: {}", instanceId);
                fillWithCachedAnnouncements(incomingAnnouncements);
                continue;
            }
            //TODO: add ClusterView.contains(instanceSlingId) for convenience to next api change
            if (!contains(localClusterView, instanceId)) {
                logger.debug("listAnnouncementsInSameCluster: instance is not in my view, ignoring: {}", instanceId);
                // (corresponds to earlier expiry-handling)
                continue;
            }
            final Resource announcementsResource = aClusterInstanceResource.getChild("announcements");
            if (announcementsResource == null) {
                logger.debug("listAnnouncementsInSameCluster: instance has no announcements: {}", instanceId);
                continue;
            }
            logger.debug("listAnnouncementsInSameCluster: instance has announcements: {}", instanceId);
            Iterator<Resource> it = announcementsResource.getChildren().iterator();
            Announcement topologyAnnouncement;
            while (it.hasNext()) {
                Resource anAnnouncement = it.next();
                topologyAnnouncement = Announcement.fromJSON(anAnnouncement.adaptTo(ValueMap.class).get("topologyAnnouncement", String.class));
                logger.debug("listAnnouncementsInSameCluster: found announcement: {}", topologyAnnouncement);
                incomingAnnouncements.add(topologyAnnouncement);
            // SLING-3389: no longer check for expired announcements -
            // instead make use of the fact that this instance
            // has a clusterView and that every live instance
            // is responsible of cleaning up expired announcements
            // with the repository
            }
        }
    // since SLING-3389 this method does only read operations, hence
    // no commit necessary anymore - close happens in below finally block
    } catch (LoginException e) {
        logger.error("listAnnouncementsInSameCluster: could not log in administratively: " + e, e);
        throw new RuntimeException("Could not log in to repository (" + e + ")", e);
    } catch (PersistenceException e) {
        logger.error("listAnnouncementsInSameCluster: got a PersistenceException: " + e, e);
        throw new RuntimeException("Exception while talking to repository (" + e + ")", e);
    } catch (JsonException e) {
        logger.error("listAnnouncementsInSameCluster: got a JSONException: " + e, e);
        throw new RuntimeException("Exception while converting json (" + e + ")", e);
    } finally {
        if (resourceResolver != null) {
            resourceResolver.close();
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("listAnnouncementsInSameCluster: result: " + incomingAnnouncements.size());
    }
    return incomingAnnouncements;
}
Also used : JsonException(javax.json.JsonException) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) LinkedList(java.util.LinkedList) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) PersistenceException(org.apache.sling.api.resource.PersistenceException) LoginException(org.apache.sling.api.resource.LoginException) InstanceDescription(org.apache.sling.discovery.InstanceDescription)

Example 3 with InstanceDescription

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

the class BaseDiscoveryService method getTopology.

/**
     * @see DiscoveryService#getTopology()
     */
public TopologyView getTopology() {
    // create a new topology view
    final DefaultTopologyView topology = new DefaultTopologyView();
    LocalClusterView localClusterView = null;
    try {
        ClusterViewService clusterViewService = getClusterViewService();
        if (clusterViewService == null) {
            throw new UndefinedClusterViewException(Reason.REPOSITORY_EXCEPTION, "no ClusterViewService available at the moment");
        }
        localClusterView = clusterViewService.getLocalClusterView();
        topology.setLocalClusterView(localClusterView);
    } catch (UndefinedClusterViewException e) {
        // SLING-5030 : when we're cut off from the local cluster we also
        // treat it as being cut off from the entire topology, ie we don't
        // update the announcements but just return
        // the previous oldView marked as !current
        logger.info("getTopology: undefined cluster view: " + e.getReason() + "] " + e);
        oldView.setNotCurrent();
        if (e.getReason() == Reason.ISOLATED_FROM_TOPOLOGY) {
            handleIsolatedFromTopology();
        }
        return oldView;
    }
    Collection<InstanceDescription> attachedInstances = getAnnouncementRegistry().listInstances(localClusterView);
    topology.addInstances(attachedInstances);
    return topology;
}
Also used : LocalClusterView(org.apache.sling.discovery.commons.providers.spi.LocalClusterView) InstanceDescription(org.apache.sling.discovery.InstanceDescription)

Example 4 with InstanceDescription

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

the class DefaultTopologyView method getClusterViews.

/**
     * @see org.apache.sling.discovery.TopologyView#getClusterViews()
     */
public Set<ClusterView> getClusterViews() {
    Set<ClusterView> result = new HashSet<ClusterView>();
    for (Iterator<InstanceDescription> it = instances.iterator(); it.hasNext(); ) {
        InstanceDescription instance = it.next();
        ClusterView cluster = instance.getClusterView();
        if (cluster != null) {
            result.add(cluster);
        }
    }
    return new HashSet<ClusterView>(result);
}
Also used : LocalClusterView(org.apache.sling.discovery.commons.providers.spi.LocalClusterView) ClusterView(org.apache.sling.discovery.ClusterView) InstanceDescription(org.apache.sling.discovery.InstanceDescription) HashSet(java.util.HashSet)

Example 5 with InstanceDescription

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

the class DefaultTopologyView method hashCode.

@Override
public int hashCode() {
    int code = 0;
    for (Iterator<InstanceDescription> it = instances.iterator(); it.hasNext(); ) {
        InstanceDescription instance = it.next();
        code += instance.hashCode();
    }
    return code;
}
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