You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2014/10/18 18:02:52 UTC
svn commit: r1632813 - in /sling/trunk/bundles/extensions/event: ./
src/main/java/org/apache/sling/event/impl/jobs/queues/
Author: cziegeler
Date: Sat Oct 18 16:02:52 2014
New Revision: 1632813
URL: http://svn.apache.org/r1632813
Log:
SLING-4048 : Avoid keeping jobs in memory. Another refactoring: move all configuration code into config package (WiP)
Removed:
sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/queues/TopicManager.java
Modified:
sling/trunk/bundles/extensions/event/pom.xml
sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/queues/AbstractJobQueue.java
sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/queues/OrderedJobQueue.java
sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/queues/ParallelJobQueue.java
sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/queues/QueueJobCache.java
sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/queues/QueueManager.java
sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/queues/QueueServices.java
Modified: sling/trunk/bundles/extensions/event/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/pom.xml?rev=1632813&r1=1632812&r2=1632813&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/event/pom.xml (original)
+++ sling/trunk/bundles/extensions/event/pom.xml Sat Oct 18 16:02:52 2014
@@ -116,7 +116,7 @@
-Xmx2048m -XX:MaxPermSize=512m
</argLine>
<includes>
- <include>**/it/Foo*</include>
+ <include>**/it/ChaosTest*</include>
</includes>
</configuration>
</plugin>
Modified: sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/queues/AbstractJobQueue.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/queues/AbstractJobQueue.java?rev=1632813&r1=1632812&r2=1632813&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/queues/AbstractJobQueue.java (original)
+++ sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/queues/AbstractJobQueue.java Sat Oct 18 16:02:52 2014
@@ -112,6 +112,9 @@ public abstract class AbstractJobQueue
/** A marker for closing the queue. */
private final AtomicBoolean closeMarker = new AtomicBoolean(false);
+ /** The job cache. */
+ private final QueueJobCache cache;
+
/**
* Create a new queue
* @param name The queue name
@@ -119,7 +122,8 @@ public abstract class AbstractJobQueue
*/
public AbstractJobQueue(final String name,
final InternalQueueConfiguration config,
- final QueueServices services) {
+ final QueueServices services,
+ final Set<String> topics) {
if ( config.getOwnThreadPoolSize() > 0 ) {
this.threadPool = new EventingThreadPool(services.threadPoolManager, config.getOwnThreadPoolSize());
} else {
@@ -130,6 +134,7 @@ public abstract class AbstractJobQueue
this.services = services;
this.logger = LoggerFactory.getLogger(this.getClass().getName() + '.' + name);
this.running = true;
+ this.cache = new QueueJobCache(services.configuration, config.getType(), topics);
}
/**
@@ -333,7 +338,7 @@ public abstract class AbstractJobQueue
this.isWaitingForNextJob = true;
while ( this.isWaitingForNextJob && !this.isOutdated()) {
- result = this.services.cache.getNextJob();
+ result = this.cache.getNextJob();
if ( result != null ) {
isWaitingForNextJob = false;
} else {
@@ -373,7 +378,7 @@ public abstract class AbstractJobQueue
* @param topic A new topic.
*/
public void wakeUpQueue(final Set<String> topics) {
- this.services.cache.handleNewJob(topics);
+ this.cache.handleNewJob(topics);
this.stopWaitingForNextJob();
}
@@ -382,7 +387,7 @@ public abstract class AbstractJobQueue
* @param handler The job handler
*/
private void requeue(final JobHandler handler) {
- this.services.cache.reschedule(handler);
+ this.cache.reschedule(handler);
synchronized ( this.nextJobLock ) {
this.nextJobLock.notify();
}
@@ -848,7 +853,7 @@ public abstract class AbstractJobQueue
*/
@Override
public synchronized void removeAll() {
- final Set<String> topics = this.services.cache.getTopics();
+ final Set<String> topics = this.cache.getTopics();
logger.debug("Removing all jobs for queue {} : {}", queueName, topics);
if ( !topics.isEmpty() ) {
Modified: sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/queues/OrderedJobQueue.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/queues/OrderedJobQueue.java?rev=1632813&r1=1632812&r2=1632813&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/queues/OrderedJobQueue.java (original)
+++ sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/queues/OrderedJobQueue.java Sat Oct 18 16:02:52 2014
@@ -18,6 +18,8 @@
*/
package org.apache.sling.event.impl.jobs.queues;
+import java.util.Set;
+
import org.apache.sling.event.impl.jobs.JobHandler;
import org.apache.sling.event.impl.jobs.config.InternalQueueConfiguration;
@@ -37,8 +39,9 @@ public final class OrderedJobQueue exten
public OrderedJobQueue(final String name,
final InternalQueueConfiguration config,
- final QueueServices services) {
- super(name, config, services);
+ final QueueServices services,
+ final Set<String> topics) {
+ super(name, config, services, topics);
}
@Override
Modified: sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/queues/ParallelJobQueue.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/queues/ParallelJobQueue.java?rev=1632813&r1=1632812&r2=1632813&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/queues/ParallelJobQueue.java (original)
+++ sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/queues/ParallelJobQueue.java Sat Oct 18 16:02:52 2014
@@ -19,6 +19,7 @@
package org.apache.sling.event.impl.jobs.queues;
import java.util.Date;
+import java.util.Set;
import org.apache.sling.event.impl.jobs.JobHandler;
import org.apache.sling.event.impl.jobs.config.InternalQueueConfiguration;
@@ -35,8 +36,9 @@ public final class ParallelJobQueue exte
public ParallelJobQueue(final String name,
final InternalQueueConfiguration config,
- final QueueServices services) {
- super(name, config, services);
+ final QueueServices services,
+ final Set<String> topics) {
+ super(name, config, services, topics);
}
@Override
Modified: sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/queues/QueueJobCache.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/queues/QueueJobCache.java?rev=1632813&r1=1632812&r2=1632813&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/queues/QueueJobCache.java (original)
+++ sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/queues/QueueJobCache.java Sat Oct 18 16:02:52 2014
@@ -33,7 +33,7 @@ import org.apache.sling.event.impl.jobs.
import org.apache.sling.event.impl.jobs.JobImpl;
import org.apache.sling.event.impl.jobs.JobTopicTraverser;
import org.apache.sling.event.impl.jobs.config.JobManagerConfiguration;
-import org.apache.sling.event.impl.jobs.config.QueueConfigurationManager.QueueInfo;
+import org.apache.sling.event.jobs.QueueConfiguration;
import org.apache.sling.event.jobs.QueueConfiguration.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -63,8 +63,8 @@ public class QueueJobCache {
/** The cache of current objects. */
private final List<JobImpl> cache = new ArrayList<JobImpl>();
- /** The queue info. */
- private final QueueInfo info;
+ /** The queue type. */
+ private final QueueConfiguration.Type queueType;
/**
* Create a new queue job cache
@@ -73,23 +73,15 @@ public class QueueJobCache {
* @param topics The topics handled by this queue.
*/
public QueueJobCache(final JobManagerConfiguration configuration,
- final QueueInfo info,
+ final QueueConfiguration.Type queueType,
final Set<String> topics) {
this.configuration = configuration;
- this.info = info;
+ this.queueType = queueType;
this.topics = new ConcurrentSkipListSet<String>(topics);
this.topicsWithNewJobs.addAll(topics);
}
/**
- * Return the queue info for this queue.
- * @return The queue info
- */
- public QueueInfo getQueueInfo() {
- return this.info;
- }
-
- /**
* All topics of this queue.
* @return The topics.
*/
@@ -163,8 +155,8 @@ public class QueueJobCache {
* @param topicCache The topic based cache
*/
private void orderTopics(final Map<String, List<JobImpl>> topicCache) {
- if ( this.info.queueConfiguration.getType() == Type.ORDERED
- || this.info.queueConfiguration.getType() == Type.UNORDERED) {
+ if ( this.queueType == Type.ORDERED
+ || this.queueType == Type.UNORDERED) {
for(final List<JobImpl> list : topicCache.values()) {
this.cache.addAll(list);
}
@@ -222,7 +214,7 @@ public class QueueJobCache {
public void reschedule(final JobHandler handler) {
synchronized ( this.cache ) {
if ( handler.reschedule() ) {
- if ( this.info.queueConfiguration.getType() == Type.ORDERED ) {
+ if ( this.queueType == Type.ORDERED ) {
this.cache.add(0, handler.getJob());
} else {
this.cache.add(handler.getJob());
Modified: sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/queues/QueueManager.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/queues/QueueManager.java?rev=1632813&r1=1632812&r2=1632813&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/queues/QueueManager.java (original)
+++ sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/queues/QueueManager.java Sat Oct 18 16:02:52 2014
@@ -19,11 +19,15 @@
package org.apache.sling.event.impl.jobs.queues;
import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
@@ -32,9 +36,12 @@ import org.apache.felix.scr.annotations.
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.scheduler.Scheduler;
import org.apache.sling.commons.threads.ThreadPoolManager;
import org.apache.sling.event.impl.jobs.JobConsumerManager;
+import org.apache.sling.event.impl.jobs.config.ConfigurationChangeListener;
import org.apache.sling.event.impl.jobs.config.InternalQueueConfiguration;
import org.apache.sling.event.impl.jobs.config.JobManagerConfiguration;
import org.apache.sling.event.impl.jobs.config.QueueConfigurationManager;
@@ -44,10 +51,14 @@ import org.apache.sling.event.impl.jobs.
import org.apache.sling.event.impl.jobs.stats.StatisticsManager;
import org.apache.sling.event.impl.support.Environment;
import org.apache.sling.event.impl.support.ResourceHelper;
+import org.apache.sling.event.jobs.NotificationConstants;
import org.apache.sling.event.jobs.Queue;
import org.apache.sling.event.jobs.QueueConfiguration;
import org.apache.sling.event.jobs.jmx.QueuesMBean;
+import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
+import org.osgi.service.event.EventConstants;
+import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -56,13 +67,14 @@ import org.slf4j.LoggerFactory;
* Implementation of the job manager.
*/
@Component(immediate=true)
-@Service(value={Runnable.class, QueueManager.class})
+@Service(value={Runnable.class, QueueManager.class, EventHandler.class})
@Properties({
@Property(name="scheduler.period", longValue=60, propertyPrivate=true),
- @Property(name="scheduler.concurrent", boolValue=false, propertyPrivate=true)
+ @Property(name="scheduler.concurrent", boolValue=false, propertyPrivate=true),
+ @Property(name=EventConstants.EVENT_TOPIC, value=NotificationConstants.TOPIC_JOB_ADDED)
})
public class QueueManager
- implements Runnable {
+ implements Runnable, EventHandler, ConfigurationChangeListener {
/** Default logger. */
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@@ -89,9 +101,6 @@ public class QueueManager
@Reference
private StatisticsManager statisticsManager;
- @Reference
- private QueueConfigurationManager queueConfigurationManager;
-
/** Lock object for the queues map - we don't want to sync directly on the concurrent map. */
private final Object queuesLock = new Object();
@@ -101,6 +110,12 @@ public class QueueManager
/** We count the scheduler runs. */
private volatile long schedulerRuns;
+ /** Flag whether the manager is active or suspended. */
+ private final AtomicBoolean isActive = new AtomicBoolean(false);
+
+ /** The queue services. */
+ private QueueServices queueServices;
+
/**
* Activate this component.
* @param props Configuration properties
@@ -108,6 +123,14 @@ public class QueueManager
@Activate
protected void activate(final Map<String, Object> props) {
logger.info("Apache Sling Queue Manager started on instance {}", Environment.APPLICATION_ID);
+ this.configuration.addListener(this);
+ this.queueServices = new QueueServices();
+ queueServices.configuration = this.configuration;
+ queueServices.eventAdmin = this.eventAdmin;
+ queueServices.jobConsumerManager = this.jobConsumerManager;
+ queueServices.scheduler = this.scheduler;
+ queueServices.threadPoolManager = this.threadPoolManager;
+ queueServices.statisticsManager = statisticsManager;
}
/**
@@ -117,6 +140,7 @@ public class QueueManager
protected void deactivate() {
logger.debug("Apache Sling Queue Manager stopping on instance {}", Environment.APPLICATION_ID);
+ this.configuration.removeListener(this);
final Iterator<AbstractJobQueue> i = this.queues.values().iterator();
while ( i.hasNext() ) {
final AbstractJobQueue jbq = i.next();
@@ -178,7 +202,7 @@ public class QueueManager
* @param queueInfo The queue info
* @param topics The topics
*/
- public void start(final QueueInfo queueInfo,
+ private void start(final QueueInfo queueInfo,
final Set<String> topics) {
final InternalQueueConfiguration config = queueInfo.queueConfiguration;
// get or create queue
@@ -193,20 +217,12 @@ public class QueueManager
queue = null;
}
if ( queue == null ) {
- final QueueServices services = new QueueServices();
- services.configuration = this.configuration;
- services.eventAdmin = this.eventAdmin;
- services.jobConsumerManager = this.jobConsumerManager;
- services.scheduler = this.scheduler;
- services.threadPoolManager = this.threadPoolManager;
- services.statisticsManager = statisticsManager;
- services.cache = new QueueJobCache(configuration, queueInfo, topics);
if ( config.getType() == QueueConfiguration.Type.ORDERED ) {
- queue = new OrderedJobQueue(queueInfo.queueName, config, services);
+ queue = new OrderedJobQueue(queueInfo.queueName, config, queueServices, topics);
} else if ( config.getType() == QueueConfiguration.Type.UNORDERED ) {
- queue = new ParallelJobQueue(queueInfo.queueName, config, services);
+ queue = new ParallelJobQueue(queueInfo.queueName, config, queueServices, topics);
} else if ( config.getType() == QueueConfiguration.Type.TOPIC_ROUND_ROBIN ) {
- queue = new ParallelJobQueue(queueInfo.queueName, config, services);
+ queue = new ParallelJobQueue(queueInfo.queueName, config, queueServices, topics);
}
// this is just a sanity check, actually we always have a queue instance here
if ( queue != null ) {
@@ -257,7 +273,7 @@ public class QueueManager
/**
* Outdate all queues.
*/
- public void restart() {
+ private void restart() {
// let's rename/close all queues and clear them
synchronized ( queuesLock ) {
final List<AbstractJobQueue> queues = new ArrayList<AbstractJobQueue>(this.queues.values());
@@ -304,4 +320,83 @@ public class QueueManager
}
};
}
+
+ /**
+ * This method is called whenever the topology or queue configurations change.
+ * @param caps The new topology capabilities or {@code null} if currently unknown.
+ */
+ @Override
+ public void configurationChanged(final boolean active) {
+ logger.debug("Topology changed {}", active);
+ if ( active ) {
+ final Set<String> topics = this.initialScan();
+ final Map<QueueInfo, Set<String>> mapping = this.updateTopicMapping(topics);
+ // start queues
+ for(final Map.Entry<QueueInfo, Set<String>> entry : mapping.entrySet() ) {
+ this.start(entry.getKey(), entry.getValue());
+ }
+ this.isActive.set(true);
+ } else {
+ this.isActive.set(false);
+ this.restart();
+ }
+ }
+
+ /**
+ * Scan the resource tree for topics.
+ */
+ private Set<String> initialScan() {
+ logger.debug("Scanning repository for existing topics...");
+ final Set<String> topics = new HashSet<String>();
+
+ final ResourceResolver resolver = this.configuration.createResourceResolver();
+ try {
+ final Resource baseResource = resolver.getResource(this.configuration.getLocalJobsPath());
+
+ // sanity check - should never be null
+ if ( baseResource != null ) {
+ final Iterator<Resource> topicIter = baseResource.listChildren();
+ while ( topicIter.hasNext() ) {
+ final Resource topicResource = topicIter.next();
+ final String topic = topicResource.getName().replace('.', '/');
+ logger.debug("Found topic {}", topic);
+ topics.add(topic);
+ }
+ }
+ } finally {
+ resolver.close();
+ }
+ return topics;
+ }
+
+ /**
+ * @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));
+ }
+ }
+
+ /**
+ * 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<QueueConfigurationManager.QueueInfo, Set<String>>();
+ 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<String>();
+ mapping.put(queueInfo, queueTopics);
+ }
+ queueTopics.add(topic);
+ }
+
+ this.logger.debug("Established new topic mapping: {}", mapping);
+ return mapping;
+ }
}
Modified: sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/queues/QueueServices.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/queues/QueueServices.java?rev=1632813&r1=1632812&r2=1632813&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/queues/QueueServices.java (original)
+++ sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/queues/QueueServices.java Sat Oct 18 16:02:52 2014
@@ -38,6 +38,4 @@ public class QueueServices {
public Scheduler scheduler;
public StatisticsManager statisticsManager;
-
- public QueueJobCache cache;
}