Search in sources :

Example 6 with QueueInfo

use of org.apache.sling.event.impl.jobs.config.QueueConfigurationManager.QueueInfo in project sling by apache.

the class QueueManager method handleEvent.

/**
     * @see org.osgi.service.event.EventHandler#handleEvent(org.osgi.service.event.Event)
     */
@Override
public void handleEvent(final Event event) {
    final String topic = (String) event.getProperty(NotificationConstants.NOTIFICATION_PROPERTY_JOB_TOPIC);
    if (this.isActive.get() && topic != null) {
        final QueueInfo info = this.configuration.getQueueConfigurationManager().getQueueInfo(topic);
        this.start(info, Collections.singleton(topic));
    }
}
Also used : QueueInfo(org.apache.sling.event.impl.jobs.config.QueueConfigurationManager.QueueInfo)

Example 7 with QueueInfo

use of org.apache.sling.event.impl.jobs.config.QueueConfigurationManager.QueueInfo in project sling by apache.

the class QueueManager method updateTopicMapping.

/**
     * Get the latest mapping from queue name to topics
     */
private Map<QueueInfo, Set<String>> updateTopicMapping(final Set<String> topics) {
    final Map<QueueInfo, Set<String>> mapping = new HashMap<>();
    for (final String topic : topics) {
        final QueueInfo queueInfo = this.configuration.getQueueConfigurationManager().getQueueInfo(topic);
        Set<String> queueTopics = mapping.get(queueInfo);
        if (queueTopics == null) {
            queueTopics = new HashSet<>();
            mapping.put(queueInfo, queueTopics);
        }
        queueTopics.add(topic);
    }
    this.logger.debug("Established new topic mapping: {}", mapping);
    return mapping;
}
Also used : QueueInfo(org.apache.sling.event.impl.jobs.config.QueueConfigurationManager.QueueInfo) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 8 with QueueInfo

use of org.apache.sling.event.impl.jobs.config.QueueConfigurationManager.QueueInfo in project sling by apache.

the class UpgradeTask method moveJobFromPreviousVersion.

/**
     * Move a single job
     */
private void moveJobFromPreviousVersion(final Resource jobResource) throws PersistenceException {
    final ResourceResolver resolver = jobResource.getResourceResolver();
    try {
        final ValueMap vm = ResourceHelper.getValueMap(jobResource);
        // check for binary properties
        Map<String, Object> binaryProperties = new HashMap<>();
        final ObjectInputStream ois = vm.get("slingevent:properties", ObjectInputStream.class);
        if (ois != null) {
            try {
                int length = ois.readInt();
                for (int i = 0; i < length; i++) {
                    final String key = (String) ois.readObject();
                    final Object value = ois.readObject();
                    binaryProperties.put(key, value);
                }
            } catch (final ClassNotFoundException cnfe) {
                throw new PersistenceException("Class not found.", cnfe);
            } catch (final java.io.InvalidClassException ice) {
                throw new PersistenceException("Invalid class.", ice);
            } catch (final IOException ioe) {
                throw new PersistenceException("Unable to deserialize job properties.", ioe);
            } finally {
                try {
                    ois.close();
                } catch (final IOException ioe) {
                    throw new PersistenceException("Unable to deserialize job properties.", ioe);
                }
            }
        }
        final Map<String, Object> properties = ResourceHelper.cloneValueMap(vm);
        final String topic = (String) properties.remove("slingevent:topic");
        properties.put(ResourceHelper.PROPERTY_JOB_TOPIC, topic);
        properties.remove(Job.PROPERTY_JOB_QUEUE_NAME);
        properties.remove(Job.PROPERTY_JOB_TARGET_INSTANCE);
        // and binary properties
        properties.putAll(binaryProperties);
        properties.remove("slingevent:properties");
        if (!properties.containsKey(Job.PROPERTY_JOB_RETRIES)) {
            // we put a dummy value here; this gets updated by the queue
            properties.put(Job.PROPERTY_JOB_RETRIES, 10);
        }
        if (!properties.containsKey(Job.PROPERTY_JOB_RETRY_COUNT)) {
            properties.put(Job.PROPERTY_JOB_RETRY_COUNT, 0);
        }
        final List<InstanceDescription> potentialTargets = caps.getPotentialTargets(topic);
        String targetId = null;
        if (potentialTargets != null && potentialTargets.size() > 0) {
            final QueueConfigurationManager qcm = configuration.getQueueConfigurationManager();
            if (qcm == null) {
                resolver.revert();
                return;
            }
            final QueueInfo info = qcm.getQueueInfo(topic);
            logger.debug("Found queue {} for {}", info.queueConfiguration, topic);
            targetId = caps.detectTarget(topic, vm, info);
            if (targetId != null) {
                properties.put(Job.PROPERTY_JOB_QUEUE_NAME, info.queueName);
                properties.put(Job.PROPERTY_JOB_TARGET_INSTANCE, targetId);
                properties.put(Job.PROPERTY_JOB_RETRIES, info.queueConfiguration.getMaxRetries());
            }
        }
        properties.put(Job.PROPERTY_JOB_CREATED_INSTANCE, "old:" + Environment.APPLICATION_ID);
        properties.put(ResourceResolver.PROPERTY_RESOURCE_TYPE, ResourceHelper.RESOURCE_TYPE_JOB);
        final String jobId = configuration.getUniqueId(topic);
        properties.put(ResourceHelper.PROPERTY_JOB_ID, jobId);
        properties.remove(Job.PROPERTY_JOB_STARTED_TIME);
        final String newPath = configuration.getUniquePath(targetId, topic, jobId, vm);
        this.logger.debug("Moving 'old' job from {} to {}", jobResource.getPath(), newPath);
        ResourceHelper.getOrCreateResource(resolver, newPath, properties);
        resolver.delete(jobResource);
        resolver.commit();
    } catch (final InstantiationException ie) {
        throw new PersistenceException("Exception while reading reasource: " + ie.getMessage(), ie.getCause());
    }
}
Also used : QueueInfo(org.apache.sling.event.impl.jobs.config.QueueConfigurationManager.QueueInfo) HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) IOException(java.io.IOException) 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) ObjectInputStream(java.io.ObjectInputStream)

Example 9 with QueueInfo

use of org.apache.sling.event.impl.jobs.config.QueueConfigurationManager.QueueInfo in project sling by apache.

the class CheckTopologyTask method reassignStaleJobs.

/**
     * Reassign stale jobs from this instance
     */
private void reassignStaleJobs() {
    if (caps.isActive()) {
        this.logger.debug("Checking for stale jobs...");
        final ResourceResolver resolver = this.configuration.createResourceResolver();
        if (resolver != null) {
            try {
                final Resource jobsRoot = resolver.getResource(this.configuration.getLocalJobsPath());
                // this resource should exist, but we check anyway
                if (jobsRoot != null) {
                    final Iterator<Resource> topicIter = jobsRoot.listChildren();
                    while (caps.isActive() && topicIter.hasNext()) {
                        final Resource topicResource = topicIter.next();
                        final String topicName = topicResource.getName().replace('.', '/');
                        this.logger.debug("Checking topic {}...", topicName);
                        final List<InstanceDescription> potentialTargets = caps.getPotentialTargets(topicName);
                        boolean reassign = true;
                        for (final InstanceDescription desc : potentialTargets) {
                            if (desc.isLocal()) {
                                reassign = false;
                                break;
                            }
                        }
                        if (reassign) {
                            final QueueConfigurationManager qcm = this.configuration.getQueueConfigurationManager();
                            if (qcm == null) {
                                break;
                            }
                            final QueueInfo info = qcm.getQueueInfo(topicName);
                            logger.info("Start reassigning stale jobs");
                            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);
                                        final Map<String, Object> props = new HashMap<>(vm);
                                        props.remove(Job.PROPERTY_JOB_STARTED_TIME);
                                        final String newPath;
                                        if (targetId != null) {
                                            newPath = configuration.getAssginedJobsPath() + '/' + targetId + '/' + topicResource.getName() + rsrc.getPath().substring(topicResource.getPath().length());
                                            props.put(Job.PROPERTY_JOB_QUEUE_NAME, info.queueName);
                                            props.put(Job.PROPERTY_JOB_TARGET_INSTANCE, targetId);
                                        } else {
                                            newPath = configuration.getUnassignedJobsPath() + '/' + topicResource.getName() + rsrc.getPath().substring(topicResource.getPath().length());
                                            props.remove(Job.PROPERTY_JOB_QUEUE_NAME);
                                            props.remove(Job.PROPERTY_JOB_TARGET_INSTANCE);
                                        }
                                        try {
                                            ResourceHelper.getOrCreateResource(resolver, newPath, props);
                                            resolver.delete(rsrc);
                                            resolver.commit();
                                            final String jobId = vm.get(ResourceHelper.PROPERTY_JOB_ID, String.class);
                                            if (targetId != null) {
                                                configuration.getAuditLogger().debug("REASSIGN OK {} : {}", targetId, jobId);
                                            } else {
                                                configuration.getAuditLogger().debug("REUNASSIGN OK : {}", jobId);
                                            }
                                        } catch (final PersistenceException pe) {
                                            logger.warn("Unable to move stale 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 stale job from " + rsrc.getPath(), ie);
                                        resolver.refresh();
                                        resolver.revert();
                                    }
                                    return caps.isActive();
                                }
                            });
                        }
                    }
                }
            } finally {
                resolver.close();
            }
        }
    }
}
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)

Aggregations

QueueInfo (org.apache.sling.event.impl.jobs.config.QueueConfigurationManager.QueueInfo)9 HashMap (java.util.HashMap)6 PersistenceException (org.apache.sling.api.resource.PersistenceException)6 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)5 ValueMap (org.apache.sling.api.resource.ValueMap)5 Resource (org.apache.sling.api.resource.Resource)4 QueueConfigurationManager (org.apache.sling.event.impl.jobs.config.QueueConfigurationManager)4 Map (java.util.Map)3 InstanceDescription (org.apache.sling.discovery.InstanceDescription)3 JobTopicTraverser (org.apache.sling.event.impl.jobs.JobTopicTraverser)3 TopologyCapabilities (org.apache.sling.event.impl.jobs.config.TopologyCapabilities)2 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)1 JobQueueImpl (org.apache.sling.event.impl.jobs.queues.JobQueueImpl)1