Search in sources :

Example 31 with InstanceDescription

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

the class CheckTopologyTask method assignJobs.

/**
     * Try to assign all jobs from the jobs root.
     * The jobs are stored by topic
     * @param jobsRoot The root of the jobs
     * @param unassign Whether to unassign the job if no instance is found.
     */
private void assignJobs(final Resource jobsRoot, final boolean unassign) {
    final ResourceResolver resolver = jobsRoot.getResourceResolver();
    final Iterator<Resource> topicIter = jobsRoot.listChildren();
    while (caps.isActive() && topicIter.hasNext()) {
        final Resource topicResource = topicIter.next();
        final String topicName = topicResource.getName().replace('.', '/');
        logger.debug("Found topic {}", topicName);
        // first check if there is an instance for these topics
        final List<InstanceDescription> potentialTargets = caps.getPotentialTargets(topicName);
        if (potentialTargets != null && potentialTargets.size() > 0) {
            final QueueConfigurationManager qcm = this.configuration.getQueueConfigurationManager();
            if (qcm == null) {
                break;
            }
            final QueueInfo info = qcm.getQueueInfo(topicName);
            logger.debug("Found queue {} for {}", info.queueConfiguration, topicName);
            JobTopicTraverser.traverse(this.logger, topicResource, new JobTopicTraverser.ResourceCallback() {

                @Override
                public boolean handle(final Resource rsrc) {
                    try {
                        final ValueMap vm = ResourceHelper.getValueMap(rsrc);
                        final String targetId = caps.detectTarget(topicName, vm, info);
                        if (targetId != null) {
                            final String newPath = configuration.getAssginedJobsPath() + '/' + targetId + '/' + topicResource.getName() + rsrc.getPath().substring(topicResource.getPath().length());
                            final Map<String, Object> props = new HashMap<>(vm);
                            props.put(Job.PROPERTY_JOB_QUEUE_NAME, info.queueName);
                            props.put(Job.PROPERTY_JOB_TARGET_INSTANCE, targetId);
                            props.remove(Job.PROPERTY_JOB_STARTED_TIME);
                            try {
                                ResourceHelper.getOrCreateResource(resolver, newPath, props);
                                resolver.delete(rsrc);
                                resolver.commit();
                                final String jobId = vm.get(ResourceHelper.PROPERTY_JOB_ID, String.class);
                                configuration.getAuditLogger().debug("REASSIGN OK {} : {}", targetId, jobId);
                            } catch (final PersistenceException pe) {
                                logger.warn("Unable to move unassigned job from " + rsrc.getPath() + " to " + newPath, pe);
                                resolver.refresh();
                                resolver.revert();
                            }
                        }
                    } catch (final InstantiationException ie) {
                        // something happened with the resource in the meantime
                        logger.warn("Unable to move unassigned job from " + rsrc.getPath(), ie);
                        resolver.refresh();
                        resolver.revert();
                    }
                    return caps.isActive();
                }
            });
        }
        // now unassign if there are still jobs
        if (caps.isActive() && unassign) {
            // we have to move everything to the unassigned area
            JobTopicTraverser.traverse(this.logger, topicResource, new JobTopicTraverser.ResourceCallback() {

                @Override
                public boolean handle(final Resource rsrc) {
                    try {
                        final ValueMap vm = ResourceHelper.getValueMap(rsrc);
                        final String newPath = configuration.getUnassignedJobsPath() + '/' + topicResource.getName() + rsrc.getPath().substring(topicResource.getPath().length());
                        final Map<String, Object> props = new HashMap<>(vm);
                        props.remove(Job.PROPERTY_JOB_QUEUE_NAME);
                        props.remove(Job.PROPERTY_JOB_TARGET_INSTANCE);
                        props.remove(Job.PROPERTY_JOB_STARTED_TIME);
                        try {
                            ResourceHelper.getOrCreateResource(resolver, newPath, props);
                            resolver.delete(rsrc);
                            resolver.commit();
                            final String jobId = vm.get(ResourceHelper.PROPERTY_JOB_ID, String.class);
                            configuration.getAuditLogger().debug("REUNASSIGN OK : {}", jobId);
                        } catch (final PersistenceException pe) {
                            logger.warn("Unable to unassigned job from " + rsrc.getPath() + " to " + newPath, pe);
                            resolver.refresh();
                            resolver.revert();
                        }
                    } catch (final InstantiationException ie) {
                        // something happened with the resource in the meantime
                        logger.warn("Unable to unassigned job from " + rsrc.getPath(), ie);
                        resolver.refresh();
                        resolver.revert();
                    }
                    return caps.isActive();
                }
            });
        }
    }
}
Also used : QueueInfo(org.apache.sling.event.impl.jobs.config.QueueConfigurationManager.QueueInfo) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) PersistenceException(org.apache.sling.api.resource.PersistenceException) QueueConfigurationManager(org.apache.sling.event.impl.jobs.config.QueueConfigurationManager) InstanceDescription(org.apache.sling.discovery.InstanceDescription) ValueMap(org.apache.sling.api.resource.ValueMap) HashMap(java.util.HashMap) Map(java.util.Map) JobTopicTraverser(org.apache.sling.event.impl.jobs.JobTopicTraverser)

Example 32 with InstanceDescription

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

the class JobManagerConfigurationTest method createView.

private TopologyView createView() {
    final TopologyView view = Mockito.mock(TopologyView.class);
    Mockito.when(view.isCurrent()).thenReturn(true);
    final InstanceDescription local = Mockito.mock(InstanceDescription.class);
    Mockito.when(local.isLeader()).thenReturn(true);
    Mockito.when(local.isLocal()).thenReturn(true);
    Mockito.when(local.getSlingId()).thenReturn("id");
    Mockito.when(view.getLocalInstance()).thenReturn(local);
    final ClusterView localView = Mockito.mock(ClusterView.class);
    Mockito.when(localView.getId()).thenReturn("1");
    Mockito.when(localView.getInstances()).thenReturn(Collections.singletonList(local));
    Mockito.when(view.getClusterViews()).thenReturn(Collections.singleton(localView));
    Mockito.when(local.getClusterView()).thenReturn(localView);
    return view;
}
Also used : ClusterView(org.apache.sling.discovery.ClusterView) InstanceDescription(org.apache.sling.discovery.InstanceDescription) TopologyView(org.apache.sling.discovery.TopologyView)

Example 33 with InstanceDescription

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

the class OakDiscoveryService method activate.

/**
     * Activate this service
     */
@Activate
protected void activate(final BundleContext bundleContext) {
    logger.debug("OakDiscoveryService activating...");
    if (settingsService == null) {
        throw new IllegalStateException("settingsService not found");
    }
    if (oakViewChecker == null) {
        throw new IllegalStateException("heartbeatHandler not found");
    }
    slingId = settingsService.getSlingId();
    ClusterSyncService consistencyService;
    if (config.getSyncTokenEnabled()) {
        //TODO: ConsistencyHistory is implemented a little bit hacky ..
        ClusterSyncHistory consistencyHistory = new ClusterSyncHistory();
        oakBacklogClusterSyncService.setConsistencyHistory(consistencyHistory);
        syncTokenService.setConsistencyHistory(consistencyHistory);
        consistencyService = new ClusterSyncServiceChain(oakBacklogClusterSyncService, syncTokenService);
    } else {
        consistencyService = oakBacklogClusterSyncService;
    }
    viewStateManager = ViewStateManagerFactory.newViewStateManager(viewStateManagerLock, consistencyService);
    if (config.getMinEventDelay() > 0) {
        viewStateManager.installMinEventDelayHandler(this, scheduler, config.getMinEventDelay());
    }
    final String isolatedClusterId = UUID.randomUUID().toString();
    {
        // create a pre-voting/isolated topologyView which would be used
        // until the first voting has finished.
        // this way for the single-instance case the clusterId can
        // remain the same between a getTopology() that is invoked before
        // the first TOPOLOGY_INIT and afterwards
        DefaultClusterView isolatedCluster = new DefaultClusterView(isolatedClusterId);
        Map<String, String> emptyProperties = new HashMap<String, String>();
        DefaultInstanceDescription isolatedInstance = new DefaultInstanceDescription(isolatedCluster, true, true, slingId, emptyProperties);
        Collection<InstanceDescription> col = new ArrayList<InstanceDescription>();
        col.add(isolatedInstance);
        final DefaultTopologyView topology = new DefaultTopologyView();
        topology.addInstances(col);
        topology.setNotCurrent();
        setOldView(topology);
    }
    setOldView((DefaultTopologyView) getTopology());
    getOldView().setNotCurrent();
    // make sure the first heartbeat is issued as soon as possible - which
    // is right after this service starts. since the two (discoveryservice
    // and heartbeatHandler need to know each other, the discoveryservice
    // is passed on to the heartbeatHandler in this initialize call).
    oakViewChecker.initialize(this);
    viewStateManagerLock.lock();
    try {
        viewStateManager.handleActivated();
        doUpdateProperties();
        DefaultTopologyView newView = (DefaultTopologyView) getTopology();
        if (newView.isCurrent()) {
            viewStateManager.handleNewView(newView);
        } else {
            // SLING-3750: just issue a log.info about the delaying
            logger.info("activate: this instance is in isolated mode and must yet finish voting before it can send out TOPOLOGY_INIT.");
        }
        activated = true;
        setOldView(newView);
        // bind them to the viewstatemanager too
        for (TopologyEventListener listener : pendingListeners) {
            viewStateManager.bind(listener);
        }
        pendingListeners.clear();
        viewStateManager.bind(changePropagationListener);
    } finally {
        if (viewStateManagerLock != null) {
            viewStateManagerLock.unlock();
        }
    }
    URL[] topologyConnectorURLs = config.getTopologyConnectorURLs();
    if (topologyConnectorURLs != null) {
        for (int i = 0; i < topologyConnectorURLs.length; i++) {
            final URL aURL = topologyConnectorURLs[i];
            if (aURL != null) {
                try {
                    logger.info("activate: registering outgoing topology connector to " + aURL);
                    connectorRegistry.registerOutgoingConnector(clusterViewService, aURL);
                } catch (final Exception e) {
                    logger.info("activate: could not register url: " + aURL + " due to: " + e, e);
                }
            }
        }
    }
    logger.debug("OakDiscoveryService activated.");
}
Also used : DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription) URL(java.net.URL) PersistenceException(org.apache.sling.api.resource.PersistenceException) LoginException(org.apache.sling.api.resource.LoginException) ClusterSyncHistory(org.apache.sling.discovery.commons.providers.spi.base.ClusterSyncHistory) DefaultTopologyView(org.apache.sling.discovery.base.commons.DefaultTopologyView) ClusterSyncServiceChain(org.apache.sling.discovery.commons.providers.spi.base.ClusterSyncServiceChain) ClusterSyncService(org.apache.sling.discovery.commons.providers.spi.ClusterSyncService) OakBacklogClusterSyncService(org.apache.sling.discovery.commons.providers.spi.base.OakBacklogClusterSyncService) DefaultClusterView(org.apache.sling.discovery.commons.providers.DefaultClusterView) Collection(java.util.Collection) DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription) InstanceDescription(org.apache.sling.discovery.InstanceDescription) Map(java.util.Map) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) HashMap(java.util.HashMap) TopologyEventListener(org.apache.sling.discovery.TopologyEventListener) Activate(org.apache.felix.scr.annotations.Activate)

Example 34 with InstanceDescription

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

the class TopologyWebConsolePlugin method renderProperties.

/**
     * Render the properties page of a particular instance
     */
private void renderProperties(final PrintWriter pw, final String contextPath, final String nodeId) {
    if (logger.isDebugEnabled()) {
        logger.debug("renderProperties: nodeId=" + nodeId);
    }
    final TopologyView tv = this.currentView;
    @SuppressWarnings("unchecked") Set<InstanceDescription> instances = (tv == null ? (Set<InstanceDescription>) Collections.EMPTY_SET : tv.findInstances(new InstanceFilter() {

        @Override
        public boolean accept(InstanceDescription instance) {
            String slingId = instance.getSlingId();
            if (logger.isDebugEnabled()) {
                logger.debug("renderProperties/picks: slingId={}", slingId);
            }
            return (slingId.equals(nodeId));
        }
    }));
    if (instances != null && instances.size() == 1) {
        InstanceDescription instance = instances.iterator().next();
        pw.println("Properties of " + instance.getSlingId() + ":<br/>");
        pw.println("<table class=\"adapters nicetable ui-widget tablesorter\">");
        pw.println("<thead>");
        pw.println("<tr>");
        pw.println("<th class=\"header ui-widget-header\">Key</th>");
        pw.println("<th class=\"header ui-widget-header\">Value</th>");
        pw.println("</tr>");
        pw.println("</thead>");
        pw.println("<tbody>");
        boolean odd = true;
        for (Iterator<Entry<String, String>> it = instance.getProperties().entrySet().iterator(); it.hasNext(); ) {
            Entry<String, String> entry = it.next();
            String oddEven = odd ? "odd" : "even";
            odd = !odd;
            pw.println("<tr class=\"" + oddEven + " ui-state-default\">");
            pw.println("<td>" + entry.getKey() + "</td>");
            pw.println("<td>" + entry.getValue() + "</td>");
            pw.println("</tr>");
        }
        pw.println("</tbody>");
        pw.println("</table>");
    }
}
Also used : InstanceFilter(org.apache.sling.discovery.InstanceFilter) Entry(java.util.Map.Entry) Set(java.util.Set) HashSet(java.util.HashSet) InstanceDescription(org.apache.sling.discovery.InstanceDescription) TopologyView(org.apache.sling.discovery.TopologyView)

Example 35 with InstanceDescription

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

the class TopologyWebConsolePlugin method renderCluster.

/**
     * Render a particular cluster (into table rows)
     */
private void renderCluster(final PrintWriter pw, final ClusterView renderCluster, final ClusterView localCluster, final boolean odd, final boolean current) {
    final Collection<Announcement> announcements = announcementRegistry.listAnnouncementsInSameCluster(localCluster);
    for (Iterator<InstanceDescription> it = renderCluster.getInstances().iterator(); it.hasNext(); ) {
        final InstanceDescription instanceDescription = it.next();
        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 String oddEven = odd ? "odd" : "even";
        if (current && (inLocalCluster || (parentAnnouncement != null))) {
            pw.println("<tr class=\"" + oddEven + " ui-state-default\">");
        } else {
            pw.println("<tr class=\"" + oddEven + " ui-state-error\">");
        }
        final boolean isLocal = instanceDescription.isLocal();
        final String slingId = instanceDescription.getSlingId();
        pw.print("<td>");
        if (isLocal) {
            pw.print("<b>");
        }
        pw.print("<a href=\"");
        pw.print(this.getLabel());
        pw.print('/');
        pw.print(slingId);
        pw.print("\">");
        pw.print(slingId);
        pw.print("</a>");
        if (isLocal) {
            pw.print("</b>");
        }
        pw.println("</td>");
        pw.println("<td>" + (instanceDescription.getClusterView() == null ? "null" : instanceDescription.getClusterView().getId()) + "</td>");
        pw.println("<td>" + (isLocal ? "<b>true</b>" : "false") + "</td>");
        pw.println("<td>" + (instanceDescription.isLeader() ? "<b>true</b>" : "false") + "</td>");
        if (inLocalCluster) {
            pw.println("<td>local</td>");
            pw.println("<td>n/a</td>");
        } else {
            pw.println("<td>remote</td>");
            if (parentAnnouncement != null) {
                pw.println("<td>" + parentAnnouncement.getOwnerId() + "</td>");
            } else {
                pw.println("<td><b>(changing)</b></td>");
            }
        }
        pw.println("</tr>");
    }
}
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)

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