Search in sources :

Example 26 with InstanceDescription

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

the class InventoryPlugin method printJson.

private void printJson(final PrintWriter pw) {
    pw.println("{");
    Statistics s = this.jobManager.getStatistics();
    pw.println("  \"statistics\" : {");
    pw.printf("    \"startTime\" : %s,%n", s.getStartTime());
    pw.printf("    \"startTimeText\" : \"%s\",%n", formatDate(s.getStartTime()));
    pw.printf("    \"lastActivatedJobTime\" : %s,%n", s.getLastActivatedJobTime());
    pw.printf("    \"lastActivatedJobTimeText\" : \"%s\",%n", formatDate(s.getLastActivatedJobTime()));
    pw.printf("    \"lastFinishedJobTime\" : %s,%n", s.getLastFinishedJobTime());
    pw.printf("    \"lastFinishedJobTimeText\" : \"%s\",%n", formatDate(s.getLastFinishedJobTime()));
    pw.printf("    \"numberOfQueuedJobs\" : %s,%n", s.getNumberOfQueuedJobs());
    pw.printf("    \"numberOfActiveJobs\" : %s,%n", s.getNumberOfActiveJobs());
    pw.printf("    \"numberOfJobs\" : %s,%n", s.getNumberOfJobs());
    pw.printf("    \"numberOfFinishedJobs\" : %s,%n", s.getNumberOfFinishedJobs());
    pw.printf("    \"numberOfFailedJobs\" : %s,%n", s.getNumberOfFailedJobs());
    pw.printf("    \"numberOfCancelledJobs\" : %s,%n", s.getNumberOfCancelledJobs());
    pw.printf("    \"numberOfProcessedJobs\" : %s,%n", s.getNumberOfProcessedJobs());
    pw.printf("    \"averageProcessingTime\" : %s,%n", s.getAverageProcessingTime());
    pw.printf("    \"averageProcessingTimeText\" : \"%s\",%n", formatTime(s.getAverageProcessingTime()));
    pw.printf("    \"averageWaitingTime\" : %s,%n", s.getAverageWaitingTime());
    pw.printf("    \"averageWaitingTimeText\" : \"%s\"%n", formatTime(s.getAverageWaitingTime()));
    pw.print("  }");
    final TopologyCapabilities cap = this.configuration.getTopologyCapabilities();
    if (cap != null) {
        pw.println(",");
        pw.println("  \"capabilities\" : [");
        final Map<String, List<InstanceDescription>> instanceCaps = cap.getInstanceCapabilities();
        final Iterator<Map.Entry<String, List<InstanceDescription>>> iter = instanceCaps.entrySet().iterator();
        while (iter.hasNext()) {
            final Map.Entry<String, List<InstanceDescription>> entry = iter.next();
            final List<String> instances = new ArrayList<>();
            for (final InstanceDescription id : entry.getValue()) {
                if (id.isLocal()) {
                    instances.add("local");
                } else {
                    instances.add(id.getSlingId());
                }
            }
            pw.println("    {");
            pw.printf("       \"topic\" : \"%s\",%n", entry.getKey());
            pw.printf("       \"instances\" : %s%n", formatArrayAsJson(instances.toArray(new String[instances.size()])));
            if (iter.hasNext()) {
                pw.println("    },");
            } else {
                pw.println("    }");
            }
        }
        pw.print("  ]");
    }
    boolean first = true;
    for (final Queue q : this.jobManager.getQueues()) {
        pw.println(",");
        if (first) {
            pw.println("  \"queues\" : [");
            first = false;
        }
        pw.println("    {");
        pw.printf("      \"name\" : \"%s\",%n", q.getName());
        pw.printf("      \"suspended\" : %s,%n", q.isSuspended());
        s = q.getStatistics();
        pw.println("      \"statistics\" : {");
        pw.printf("        \"startTime\" : %s,%n", s.getStartTime());
        pw.printf("        \"startTimeText\" : \"%s\",%n", formatDate(s.getStartTime()));
        pw.printf("        \"lastActivatedJobTime\" : %s,%n", s.getLastActivatedJobTime());
        pw.printf("        \"lastActivatedJobTimeText\" : \"%s\",%n", formatDate(s.getLastActivatedJobTime()));
        pw.printf("        \"lastFinishedJobTime\" : %s,%n", s.getLastFinishedJobTime());
        pw.printf("        \"lastFinishedJobTimeText\" : \"%s\",%n", formatDate(s.getLastFinishedJobTime()));
        pw.printf("        \"numberOfQueuedJobs\" : %s,%n", s.getNumberOfQueuedJobs());
        pw.printf("        \"numberOfActiveJobs\" : %s,%n", s.getNumberOfActiveJobs());
        pw.printf("        \"numberOfJobs\" : %s,%n", s.getNumberOfJobs());
        pw.printf("        \"numberOfFinishedJobs\" : %s,%n", s.getNumberOfFinishedJobs());
        pw.printf("        \"numberOfFailedJobs\" : %s,%n", s.getNumberOfFailedJobs());
        pw.printf("        \"numberOfCancelledJobs\" : %s,%n", s.getNumberOfCancelledJobs());
        pw.printf("        \"numberOfProcessedJobs\" : %s,%n", s.getNumberOfProcessedJobs());
        pw.printf("        \"averageProcessingTime\" : %s,%n", s.getAverageProcessingTime());
        pw.printf("        \"averageProcessingTimeText\" : \"%s\",%n", formatTime(s.getAverageProcessingTime()));
        pw.printf("        \"averageWaitingTime\" : %s,%n", s.getAverageWaitingTime());
        pw.printf("        \"averageWaitingTimeText\" : \"%s\"%n", formatTime(s.getAverageWaitingTime()));
        pw.print("      },");
        final QueueConfiguration c = q.getConfiguration();
        pw.printf("      \"stateInfo\" : \"%s\",%n", q.getStateInfo());
        pw.println("      \"configuration\" : {");
        pw.printf("        \"type\" : \"%s\",%n", c.getType());
        pw.printf("        \"topics\" : \"%s\",%n", formatArrayAsJson(c.getTopics()));
        pw.printf("        \"maxParallel\" : %s,%n", c.getMaxParallel());
        pw.printf("        \"maxRetries\" : %s,%n", c.getMaxRetries());
        pw.printf("        \"retryDelayInMs\" : %s,%n", c.getRetryDelayInMs());
        pw.printf("        \"priority\" : \"%s\"%n", c.getThreadPriority());
        pw.println("      }");
        pw.print("    }");
    }
    if (!first) {
        pw.print("  ]");
    }
    first = true;
    for (final TopicStatistics ts : this.jobManager.getTopicStatistics()) {
        pw.println(",");
        if (first) {
            pw.println("  \"topicStatistics\" : [");
            first = false;
        }
        pw.println("    {");
        pw.printf("      \"topic\" : \"%s\",%n", ts.getTopic());
        pw.printf("      \"lastActivatedJobTime\" : %s,%n", ts.getLastActivatedJobTime());
        pw.printf("      \"lastActivatedJobTimeText\" : \"%s\",%n", formatDate(ts.getLastActivatedJobTime()));
        pw.printf("      \"lastFinishedJobTime\" : %s,%n", ts.getLastFinishedJobTime());
        pw.printf("      \"lastFinishedJobTimeText\" : \"%s\",%n", formatDate(ts.getLastFinishedJobTime()));
        pw.printf("      \"numberOfFinishedJobs\" : %s,%n", ts.getNumberOfFinishedJobs());
        pw.printf("      \"numberOfFailedJobs\" : %s,%n", ts.getNumberOfFailedJobs());
        pw.printf("      \"numberOfCancelledJobs\" : %s,%n", ts.getNumberOfCancelledJobs());
        pw.printf("      \"numberOfProcessedJobs\" : %s,%n", ts.getNumberOfProcessedJobs());
        pw.printf("      \"averageProcessingTime\" : %s,%n", ts.getAverageProcessingTime());
        pw.printf("      \"averageProcessingTimeText\" : \"%s\",%n", formatTime(ts.getAverageProcessingTime()));
        pw.printf("      \"averageWaitingTime\" : %s,%n", ts.getAverageWaitingTime());
        pw.printf("      \"averageWaitingTimeText\" : \"%s\"%n", formatTime(ts.getAverageWaitingTime()));
        pw.print("    }");
    }
    if (!first) {
        pw.print("  ]");
    }
    pw.println(",");
    pw.println("  \"configurations\" : [");
    this.printQueueConfigurationJson(pw, this.configuration.getQueueConfigurationManager().getMainQueueConfiguration());
    final InternalQueueConfiguration[] configs = this.configuration.getQueueConfigurationManager().getConfigurations();
    for (final InternalQueueConfiguration c : configs) {
        pw.println(",");
        this.printQueueConfigurationJson(pw, c);
    }
    pw.println();
    pw.println("  ]");
    pw.println("}");
}
Also used : ArrayList(java.util.ArrayList) TopologyCapabilities(org.apache.sling.event.impl.jobs.config.TopologyCapabilities) TopicStatistics(org.apache.sling.event.jobs.TopicStatistics) Statistics(org.apache.sling.event.jobs.Statistics) TopicStatistics(org.apache.sling.event.jobs.TopicStatistics) InternalQueueConfiguration(org.apache.sling.event.impl.jobs.config.InternalQueueConfiguration) QueueConfiguration(org.apache.sling.event.jobs.QueueConfiguration) InternalQueueConfiguration(org.apache.sling.event.impl.jobs.config.InternalQueueConfiguration) ArrayList(java.util.ArrayList) List(java.util.List) InstanceDescription(org.apache.sling.discovery.InstanceDescription) Map(java.util.Map) Queue(org.apache.sling.event.jobs.Queue)

Example 27 with InstanceDescription

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

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

the class TopologyCapabilities method detectTarget.

/**
     * Detect the target instance.
     */
public String detectTarget(final String jobTopic, final Map<String, Object> jobProperties, final QueueInfo queueInfo) {
    final List<InstanceDescription> potentialTargets = this.getPotentialTargets(jobTopic);
    logger.debug("Potential targets for {} : {}", jobTopic, potentialTargets);
    String createdOn = null;
    if (jobProperties != null) {
        createdOn = (String) jobProperties.get(org.apache.sling.event.jobs.Job.PROPERTY_JOB_CREATED_INSTANCE);
    }
    if (createdOn == null) {
        createdOn = Environment.APPLICATION_ID;
    }
    final InstanceDescription createdOnInstance = this.instanceMap.get(createdOn);
    if (potentialTargets != null && potentialTargets.size() > 0) {
        if (createdOnInstance != null) {
            // create a list with local targets first.
            final List<InstanceDescription> localTargets = new ArrayList<InstanceDescription>();
            for (final InstanceDescription desc : potentialTargets) {
                if (desc.getClusterView().getId().equals(createdOnInstance.getClusterView().getId())) {
                    if (!this.jobManagerConfiguration.disableDistribution() || desc.isLeader()) {
                        localTargets.add(desc);
                    }
                }
            }
            if (localTargets.size() > 0) {
                potentialTargets.clear();
                potentialTargets.addAll(localTargets);
                logger.debug("Potential targets filtered for {} : {}", jobTopic, potentialTargets);
            }
        }
        // check prefer run on creation instance
        if (queueInfo.queueConfiguration.isPreferRunOnCreationInstance()) {
            InstanceDescription creationDesc = null;
            for (final InstanceDescription desc : potentialTargets) {
                if (desc.getSlingId().equals(createdOn)) {
                    creationDesc = desc;
                    break;
                }
            }
            if (creationDesc != null) {
                potentialTargets.clear();
                potentialTargets.add(creationDesc);
                logger.debug("Potential targets reduced to creation instance for {} : {}", jobTopic, potentialTargets);
            }
        }
        if (queueInfo.queueConfiguration.getType() == QueueConfiguration.Type.ORDERED) {
            // for ordered queues we always pick the first as we have to pick the same target on each cluster view
            // on all instances (TODO - we could try to do some round robin of the whole queue)
            final String result = potentialTargets.get(0).getSlingId();
            logger.debug("Target for {} : {}", jobTopic, result);
            return result;
        }
        // TODO - this is a simple round robin which is not based on the actual load
        //        of the instances
        Integer index = this.roundRobinMap.get(jobTopic);
        if (index == null) {
            index = 0;
        }
        if (index >= potentialTargets.size()) {
            index = 0;
        }
        this.roundRobinMap.put(jobTopic, index + 1);
        final String result = potentialTargets.get(index).getSlingId();
        logger.debug("Target for {} : {}", jobTopic, result);
        return result;
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) InstanceDescription(org.apache.sling.discovery.InstanceDescription)

Example 29 with InstanceDescription

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

the class NoClusterDiscoveryService method createNewView.

private void createNewView(final Type eventType, boolean inform) {
    final TopologyEventListener[] registeredServices;
    final TopologyView newView;
    final TopologyView oldView;
    synchronized (lock) {
        // invalidate old view
        if (this.currentTopologyView != null) {
            this.currentTopologyView.invalidate();
            oldView = currentTopologyView;
        } else {
            oldView = null;
        }
        final InstanceDescription myInstanceDescription = new InstanceDescriptionImpl(this.settingsService.getSlingId(), this.cachedProperties);
        this.currentTopologyView = new TopologyViewImpl(myInstanceDescription);
        registeredServices = this.listeners;
        newView = this.currentTopologyView;
        if (inform) {
            for (final TopologyEventListener da : registeredServices) {
                da.handleTopologyEvent(new TopologyEvent(eventType, oldView, newView));
            }
        }
    }
}
Also used : TopologyEvent(org.apache.sling.discovery.TopologyEvent) InstanceDescription(org.apache.sling.discovery.InstanceDescription) TopologyView(org.apache.sling.discovery.TopologyView) TopologyEventListener(org.apache.sling.discovery.TopologyEventListener)

Example 30 with InstanceDescription

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

the class TopologyWebConsolePlugin method printCluster.

/**
     * Render a particular cluster
     */
private void printCluster(final PrintWriter pw, final ClusterView renderCluster, final ClusterView localCluster) {
    final Collection<Announcement> announcements = announcementRegistry.listAnnouncementsInSameCluster(localCluster);
    for (final InstanceDescription instanceDescription : renderCluster.getInstances()) {
        final boolean inLocalCluster = renderCluster == localCluster;
        Announcement parentAnnouncement = null;
        for (Iterator<Announcement> it2 = announcements.iterator(); it2.hasNext(); ) {
            Announcement announcement = it2.next();
            for (Iterator<InstanceDescription> it3 = announcement.listInstances().iterator(); it3.hasNext(); ) {
                InstanceDescription announcedInstance = it3.next();
                if (announcedInstance.getSlingId().equals(instanceDescription.getSlingId())) {
                    parentAnnouncement = announcement;
                    break;
                }
            }
        }
        final boolean isLocal = instanceDescription.isLocal();
        final String slingId = instanceDescription.getSlingId();
        pw.print("Sling ID : ");
        pw.print(slingId);
        pw.println();
        pw.print("Cluster View ID : ");
        pw.print(instanceDescription.getClusterView() == null ? "null" : instanceDescription.getClusterView().getId());
        pw.println();
        pw.print("Local instance : ");
        pw.print(isLocal);
        pw.println();
        pw.print("Leader instance : ");
        pw.print(instanceDescription.isLeader());
        pw.println();
        pw.print("In local cluster : ");
        if (inLocalCluster) {
            pw.print("local");
        } else {
            pw.print("remote");
        }
        pw.println();
        pw.print("Announced by : ");
        if (inLocalCluster) {
            pw.print("n/a");
        } else {
            if (parentAnnouncement != null) {
                pw.print(parentAnnouncement.getOwnerId());
            } else {
                pw.print("(changing)");
            }
        }
        pw.println();
        pw.println("Properties:");
        for (final Map.Entry<String, String> entry : instanceDescription.getProperties().entrySet()) {
            pw.print("- ");
            pw.print(entry.getKey());
            pw.print(" : ");
            pw.print(entry.getValue());
            pw.println();
        }
        pw.println();
        pw.println();
    }
}
Also used : Announcement(org.apache.sling.discovery.base.connectors.announcement.Announcement) CachedAnnouncement(org.apache.sling.discovery.base.connectors.announcement.CachedAnnouncement) InstanceDescription(org.apache.sling.discovery.InstanceDescription) Map(java.util.Map)

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