Search in sources :

Example 1 with LocalClusterView

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

use of org.apache.sling.discovery.commons.providers.spi.LocalClusterView 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 3 with LocalClusterView

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

the class OakClusterViewService method asClusterView.

private LocalClusterView asClusterView(DiscoveryLiteDescriptor descriptor, ResourceResolver resourceResolver) throws Exception {
    if (descriptor == null) {
        throw new IllegalArgumentException("descriptor must not be null");
    }
    if (resourceResolver == null) {
        throw new IllegalArgumentException("resourceResolver must not be null");
    }
    logger.trace("asClusterView: start");
    String clusterViewId = descriptor.getViewId();
    if (clusterViewId == null || clusterViewId.length() == 0) {
        logger.trace("asClusterView: no clusterId provided by discovery-lite descriptor - reading from repo.");
        clusterViewId = readOrDefineClusterId(resourceResolver);
    }
    String localClusterSyncTokenId = /*descriptor.getViewId()+"_"+*/
    String.valueOf(descriptor.getSeqNum());
    if (!descriptor.isFinal()) {
        throw new UndefinedClusterViewException(Reason.NO_ESTABLISHED_VIEW, "descriptor is not yet final: " + descriptor);
    }
    LocalClusterView cluster = new LocalClusterView(clusterViewId, localClusterSyncTokenId);
    long me = descriptor.getMyId();
    int[] activeIds = descriptor.getActiveIds();
    if (activeIds == null || activeIds.length == 0) {
        throw new UndefinedClusterViewException(Reason.NO_ESTABLISHED_VIEW, "Descriptor contained no active ids: " + descriptor.getDescriptorStr());
    }
    // convert int[] to List<Integer>
    //TODO: could use Guava's Ints class here..
    List<Integer> activeIdsList = new LinkedList<Integer>();
    for (Integer integer : activeIds) {
        activeIdsList.add(integer);
    }
    // step 1: sort activeIds by their leaderElectionId
    //   serves two purposes: pos[0] is then leader
    //   and the rest are properly sorted within the cluster
    final Map<Integer, String> leaderElectionIds = new HashMap<Integer, String>();
    for (Integer id : activeIdsList) {
        String slingId = idMapService.toSlingId(id, resourceResolver);
        if (slingId == null) {
            idMapService.clearCache();
            throw new UndefinedClusterViewException(Reason.NO_ESTABLISHED_VIEW, "no slingId mapped for clusterNodeId=" + id);
        }
        String leaderElectionId = getLeaderElectionId(resourceResolver, slingId);
        leaderElectionIds.put(id, leaderElectionId);
    }
    Collections.sort(activeIdsList, new Comparator<Integer>() {

        @Override
        public int compare(Integer arg0, Integer arg1) {
            return leaderElectionIds.get(arg0).compareTo(leaderElectionIds.get(arg1));
        }
    });
    for (int i = 0; i < activeIdsList.size(); i++) {
        int id = activeIdsList.get(i);
        // thx to sorting above [0] is leader indeed
        boolean isLeader = i == 0;
        boolean isOwn = id == me;
        String slingId = idMapService.toSlingId(id, resourceResolver);
        if (slingId == null) {
            idMapService.clearCache();
            logger.info("asClusterView: cannot resolve oak-clusterNodeId {} to a slingId", id);
            throw new Exception("Cannot resolve oak-clusterNodeId " + id + " to a slingId");
        }
        Map<String, String> properties = readProperties(slingId, resourceResolver);
        // create a new instance (adds itself to the cluster in the constructor)
        new DefaultInstanceDescription(cluster, isLeader, isOwn, slingId, properties);
    }
    logger.trace("asClusterView: returning {}", cluster);
    InstanceDescription local = cluster.getLocalInstance();
    if (local != null) {
        return cluster;
    } else {
        logger.info("getClusterView: the local instance (" + getSlingId() + ") is currently not included in the existing established view! " + "This is normal at startup. At other times is pseudo-network-partitioning is an indicator for repository/network-delays or clocks-out-of-sync (SLING-3432). " + "(increasing the heartbeatTimeout can help as a workaround too) " + "The local instance will stay in TOPOLOGY_CHANGING or pre _INIT mode until a new vote was successful.");
        throw new UndefinedClusterViewException(Reason.ISOLATED_FROM_TOPOLOGY, "established view does not include local instance - isolated");
    }
}
Also used : HashMap(java.util.HashMap) DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription) LinkedList(java.util.LinkedList) PersistenceException(org.apache.sling.api.resource.PersistenceException) LoginException(org.apache.sling.api.resource.LoginException) UndefinedClusterViewException(org.apache.sling.discovery.base.commons.UndefinedClusterViewException) LocalClusterView(org.apache.sling.discovery.commons.providers.spi.LocalClusterView) UndefinedClusterViewException(org.apache.sling.discovery.base.commons.UndefinedClusterViewException) DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription) InstanceDescription(org.apache.sling.discovery.InstanceDescription)

Example 4 with LocalClusterView

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

the class DiscoveryServiceImpl method checkForLocalClusterViewChange.

/**
     * only checks for local clusterView changes.
     * thus eg avoids doing synchronized with annotationregistry
     **/
public void checkForLocalClusterViewChange() {
    viewStateManagerLock.lock();
    try {
        if (!activated) {
            logger.debug("checkForLocalClusterViewChange: not yet activated, ignoring");
            return;
        }
        try {
            ClusterViewService clusterViewService = getClusterViewService();
            if (clusterViewService == null) {
                throw new UndefinedClusterViewException(Reason.REPOSITORY_EXCEPTION, "no ClusterViewService available at the moment");
            }
            LocalClusterView localClusterView = clusterViewService.getLocalClusterView();
        } 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("checkForLocalClusterViewChange: undefined cluster view: " + e.getReason() + "] " + e);
            getOldView().setNotCurrent();
            viewStateManager.handleChanging();
            if (e.getReason() == Reason.ISOLATED_FROM_TOPOLOGY) {
                handleIsolatedFromTopology();
            }
        }
    } finally {
        if (viewStateManagerLock != null) {
            viewStateManagerLock.unlock();
        }
    }
}
Also used : LocalClusterView(org.apache.sling.discovery.commons.providers.spi.LocalClusterView) UndefinedClusterViewException(org.apache.sling.discovery.base.commons.UndefinedClusterViewException) ClusterViewService(org.apache.sling.discovery.base.commons.ClusterViewService)

Example 5 with LocalClusterView

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

the class ClusterViewServiceImpl method getLocalClusterView.

@Override
public LocalClusterView getLocalClusterView() throws UndefinedClusterViewException {
    if (resourceResolverFactory == null) {
        logger.warn("getClusterView: no resourceResolverFactory set at the moment.");
        throw new UndefinedClusterViewException(Reason.REPOSITORY_EXCEPTION, "no resourceResolverFactory set");
    }
    ResourceResolver resourceResolver = null;
    try {
        resourceResolver = resourceResolverFactory.getServiceResourceResolver(null);
        View view = ViewHelper.getEstablishedView(resourceResolver, config);
        if (view == null) {
            logger.debug("getClusterView: no view established at the moment. isolated mode");
            throw new UndefinedClusterViewException(Reason.NO_ESTABLISHED_VIEW, "no established view at the moment");
        }
        if (failedEstablishedViewId != null && failedEstablishedViewId.equals(view.getResource().getName())) {
            // SLING-5195 : the heartbeat-handler-self-check has declared the currently
            // established view as invalid - hence we should now treat this as
            // undefined clusterview
            logger.debug("getClusterView: current establishedView is marked as invalid: " + failedEstablishedViewId);
            throw new UndefinedClusterViewException(Reason.NO_ESTABLISHED_VIEW, "current established view was marked as invalid");
        }
        EstablishedClusterView clusterViewImpl = new EstablishedClusterView(config, view, getSlingId());
        InstanceDescription local = clusterViewImpl.getLocalInstance();
        if (local != null) {
            return clusterViewImpl;
        } else {
            logger.info("getClusterView: the local instance (" + getSlingId() + ") is currently not included in the existing established view! " + "This is normal at startup. At other times is pseudo-network-partitioning is an indicator for repository/network-delays or clocks-out-of-sync (SLING-3432). " + "(increasing the heartbeatTimeout can help as a workaround too) " + "The local instance will stay in TOPOLOGY_CHANGING or pre _INIT mode until a new vote was successful.");
            throw new UndefinedClusterViewException(Reason.ISOLATED_FROM_TOPOLOGY, "established view does not include local instance - isolated");
        }
    } catch (UndefinedClusterViewException e) {
        // pass through
        throw e;
    } catch (LoginException e) {
        logger.error("handleEvent: could not log in administratively: " + e, e);
        throw new UndefinedClusterViewException(Reason.REPOSITORY_EXCEPTION, "could not log in administratively: " + e);
    } catch (Exception e) {
        logger.error("handleEvent: got an exception: " + e, e);
        throw new UndefinedClusterViewException(Reason.REPOSITORY_EXCEPTION, "could not log in administratively: " + e);
    } finally {
        if (resourceResolver != null) {
            resourceResolver.close();
        }
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) LoginException(org.apache.sling.api.resource.LoginException) UndefinedClusterViewException(org.apache.sling.discovery.base.commons.UndefinedClusterViewException) InstanceDescription(org.apache.sling.discovery.InstanceDescription) EstablishedClusterView(org.apache.sling.discovery.impl.common.resource.EstablishedClusterView) View(org.apache.sling.discovery.impl.common.View) LocalClusterView(org.apache.sling.discovery.commons.providers.spi.LocalClusterView) EstablishedClusterView(org.apache.sling.discovery.impl.common.resource.EstablishedClusterView) LoginException(org.apache.sling.api.resource.LoginException) UndefinedClusterViewException(org.apache.sling.discovery.base.commons.UndefinedClusterViewException)

Aggregations

LocalClusterView (org.apache.sling.discovery.commons.providers.spi.LocalClusterView)5 InstanceDescription (org.apache.sling.discovery.InstanceDescription)3 UndefinedClusterViewException (org.apache.sling.discovery.base.commons.UndefinedClusterViewException)3 LoginException (org.apache.sling.api.resource.LoginException)2 DefaultInstanceDescription (org.apache.sling.discovery.commons.providers.DefaultInstanceDescription)2 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 PersistenceException (org.apache.sling.api.resource.PersistenceException)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 ClusterViewService (org.apache.sling.discovery.base.commons.ClusterViewService)1 View (org.apache.sling.discovery.impl.common.View)1 EstablishedClusterView (org.apache.sling.discovery.impl.common.resource.EstablishedClusterView)1