incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1509558 - in /sling/trunk/bundles/commons/scheduler: ./ src/main/java/org/apache/sling/commons/scheduler/ src/main/java/org/apache/sling/commons/scheduler/impl/
Date Fri, 02 Aug 2013 06:41:37 GMT
Author: cziegeler
Date: Fri Aug  2 06:41:36 2013
New Revision: 1509558

URL: http://svn.apache.org/r1509558
Log:
SLING-2979 : Add support for running scheduled task only on the leader

Modified:
    sling/trunk/bundles/commons/scheduler/pom.xml
    sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/ScheduleOptions.java
    sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/Scheduler.java
    sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/QuartzJobExecutor.java
    sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/QuartzScheduler.java
    sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/TopologyHandler.java

Modified: sling/trunk/bundles/commons/scheduler/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/scheduler/pom.xml?rev=1509558&r1=1509557&r2=1509558&view=diff
==============================================================================
--- sling/trunk/bundles/commons/scheduler/pom.xml (original)
+++ sling/trunk/bundles/commons/scheduler/pom.xml Fri Aug  2 06:41:36 2013
@@ -136,12 +136,6 @@
             <scope>provided</scope>
         </dependency>
         <dependency>
-            <groupId>org.apache.sling</groupId>
-            <artifactId>org.apache.sling.settings</artifactId>
-            <version>1.0.0</version>
-            <scope>provided</scope>
-        </dependency>
-        <dependency>
             <groupId>org.quartz-scheduler</groupId>
             <artifactId>quartz</artifactId>
             <version>2.2.0</version>

Modified: sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/ScheduleOptions.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/ScheduleOptions.java?rev=1509558&r1=1509557&r2=1509558&view=diff
==============================================================================
--- sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/ScheduleOptions.java
(original)
+++ sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/ScheduleOptions.java
Fri Aug  2 06:41:36 2013
@@ -72,9 +72,11 @@ public interface ScheduleOptions {
 
     /**
      * List of Sling IDs this job should be run on.
+     * If no topology information is available (= no Apache Sling Discovery Implementation
active)
+     * this flag is ignored and the job is run on all instances!
      * If {@link #onLeaderOnly(boolean)} or {@link #onSingleInstanceOnly(boolean)} has been
called before,
      * that option is reset and overwritten by the value of this method.
-     * @param flag Whether this job should only be run on a single instance.
+     * @param slingIds Array of Sling IDs this job should run on
      */
-    ScheduleOptions onInstancesOnly(String[] slingIds);
+    ScheduleOptions onInstancesOnly(final String[] slingIds);
 }

Modified: sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/Scheduler.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/Scheduler.java?rev=1509558&r1=1509557&r2=1509558&view=diff
==============================================================================
--- sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/Scheduler.java
(original)
+++ sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/Scheduler.java
Fri Aug  2 06:41:36 2013
@@ -72,7 +72,7 @@ public interface Scheduler {
      * Default is to start the job on all instances. This property needs to be of type String
      * or String[].
      * If no topology information is available (= no Apache Sling Discovery Implementation
active)
-     * the values {@link #VALUE_RUN_ON_LEADER} and {@link #VALUE_RUN_ON_SINGLE} are ignored,
and the job is run on all instances.
+     * this option is ignored, and the job is run on all instances.
      * @since 2.3.0
      */
     String PROPERTY_SCHEDULER_RUN_ON = "scheduler.runOn";

Modified: sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/QuartzJobExecutor.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/QuartzJobExecutor.java?rev=1509558&r1=1509557&r2=1509558&view=diff
==============================================================================
--- sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/QuartzJobExecutor.java
(original)
+++ sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/QuartzJobExecutor.java
Fri Aug  2 06:41:36 2013
@@ -17,10 +17,12 @@
 package org.apache.sling.commons.scheduler.impl;
 
 import java.io.Serializable;
+import java.util.Arrays;
 import java.util.Map;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.apache.sling.commons.scheduler.JobContext;
+import org.apache.sling.commons.scheduler.Scheduler;
 import org.quartz.Job;
 import org.quartz.JobDataMap;
 import org.quartz.JobExecutionContext;
@@ -34,6 +36,12 @@ import org.slf4j.Logger;
  */
 public class QuartzJobExecutor implements Job {
 
+    /** Is discovery information available? */
+    public static final AtomicBoolean DISCOVERY_INFO_AVAILABLE = new AtomicBoolean(false);
+
+    /** The id of the current instance. */
+    public static String SLING_ID;
+
     /** Is this instance the leader? */
     public static final AtomicBoolean IS_LEADER = new AtomicBoolean(true);
 
@@ -43,16 +51,47 @@ public class QuartzJobExecutor implement
     public void execute(final JobExecutionContext context) throws JobExecutionException {
 
         final JobDataMap data = context.getJobDetail().getJobDataMap();
+        final Object job = data.get(QuartzScheduler.DATA_MAP_OBJECT);
+        final Logger logger = (Logger)data.get(QuartzScheduler.DATA_MAP_LOGGER);
 
-        // check leader/single
+        // check run on information
         final String[] runOn = (String[])data.get(QuartzScheduler.DATA_MAP_RUN_ON);
-        if (runOn != null && !IS_LEADER.get()) {
-            return;
+        if ( runOn != null ) {
+            if ( runOn.length == 1 && Scheduler.VALUE_RUN_ON_LEADER.equals(runOn[0])
+                 || runOn.length == 1 && Scheduler.VALUE_RUN_ON_SINGLE.equals(runOn[0])
) {
+                if ( DISCOVERY_INFO_AVAILABLE.get() ) {
+                    if ( !IS_LEADER.get() ) {
+                        logger.debug("Excluding job {} with name {} and config {}.",
+                                new Object[] {job, data.get(QuartzScheduler.DATA_MAP_NAME),
runOn[0]});
+                        return;
+                    }
+                } else {
+                    logger.warn("No discovery info available. Executing job {} with name
{} and config {} anyway.",
+                            new Object[] {job, data.get(QuartzScheduler.DATA_MAP_NAME), runOn[0]});
+                }
+            } else { // sling IDs
+                final String myId = SLING_ID;
+                boolean schedule = false;
+                if ( myId == null ) {
+                    logger.warn("No Sling ID available. Executing job {} with name {} and
config {} anyway.",
+                            new Object[] {job, data.get(QuartzScheduler.DATA_MAP_NAME), Arrays.toString(runOn)});
+                    schedule = true;
+                } else {
+                    for(final String id : runOn ) {
+                        if ( myId.equals(id) ) {
+                            schedule = true;
+                            break;
+                        }
+                    }
+                }
+                if ( !schedule ) {
+                    logger.debug("Excluding job {} with name {} and config {}.",
+                            new Object[] {job, data.get(QuartzScheduler.DATA_MAP_NAME), Arrays.toString(runOn)});
+                    return;
+                }
+            }
         }
 
-        final Object job = data.get(QuartzScheduler.DATA_MAP_OBJECT);
-        final Logger logger = (Logger)data.get(QuartzScheduler.DATA_MAP_LOGGER);
-
         try {
             logger.debug("Executing job {} with name {}", job, data.get(QuartzScheduler.DATA_MAP_NAME));
             if (job instanceof org.apache.sling.commons.scheduler.Job) {

Modified: sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/QuartzScheduler.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/QuartzScheduler.java?rev=1509558&r1=1509557&r2=1509558&view=diff
==============================================================================
--- sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/QuartzScheduler.java
(original)
+++ sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/QuartzScheduler.java
Fri Aug  2 06:41:36 2013
@@ -34,7 +34,6 @@ import org.apache.sling.commons.schedule
 import org.apache.sling.commons.scheduler.Scheduler;
 import org.apache.sling.commons.threads.ThreadPool;
 import org.apache.sling.commons.threads.ThreadPoolManager;
-import org.apache.sling.settings.SlingSettingsService;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceRegistration;
 import org.quartz.CronScheduleBuilder;
@@ -96,9 +95,6 @@ public class QuartzScheduler implements 
     @Property
     private static final String PROPERTY_POOL_NAME = "poolName";
 
-    @Reference
-    private SlingSettingsService settings;
-
     /**
      * Activate this component.
      * Start the scheduler.
@@ -594,29 +590,6 @@ public class QuartzScheduler implements 
             name = job.getClass().getName() + ':' + UUID.randomUUID();
         }
 
-        // check run on
-        if ( opts.runOn != null ) {
-            boolean schedule = false;
-            if ( opts.runOn.length == 1 && Scheduler.VALUE_RUN_ON_LEADER.equals(opts.runOn[0]))
{
-                schedule = true;
-            } else if ( opts.runOn.length == 1 && Scheduler.VALUE_RUN_ON_SINGLE.equals(opts.runOn[0]))
{
-                schedule = true;
-            } else { // sling IDs
-                final String myId = this.settings.getSlingId();
-                for(final String id : opts.runOn ) {
-                    if ( myId.equals(id) ) {
-                        schedule = true;
-                        break;
-                    }
-                }
-                opts.runOn = null;
-            }
-            if ( !schedule ) {
-                this.logger.warn("Not scheduling job {} with name {} - not in required Sling
ID set", job, name);
-                return;
-            }
-        }
-
         final Trigger trigger = opts.trigger.withIdentity(name).build();
 
         // create the data map

Modified: sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/TopologyHandler.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/TopologyHandler.java?rev=1509558&r1=1509557&r2=1509558&view=diff
==============================================================================
--- sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/TopologyHandler.java
(original)
+++ sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/TopologyHandler.java
Fri Aug  2 06:41:36 2013
@@ -22,6 +22,10 @@ import org.apache.sling.discovery.Topolo
 import org.apache.sling.discovery.TopologyEvent.Type;
 import org.apache.sling.discovery.TopologyEventListener;
 
+/**
+ * Optional service - if the Sling discovery service is running, additional features
+ *                    are available
+ */
 @Component
 @Service(value=TopologyEventListener.class)
 public class TopologyHandler implements TopologyEventListener {
@@ -31,9 +35,12 @@ public class TopologyHandler implements 
      */
     public void handleTopologyEvent(final TopologyEvent event) {
         if ( event.getType() == Type.TOPOLOGY_INIT || event.getType() == Type.TOPOLOGY_CHANGED
) {
+            QuartzJobExecutor.SLING_ID = event.getNewView().getLocalInstance().getSlingId();
             QuartzJobExecutor.IS_LEADER.set(event.getNewView().getLocalInstance().isLeader());
+            QuartzJobExecutor.DISCOVERY_INFO_AVAILABLE.set(true);
         } else if ( event.getType() == Type.TOPOLOGY_CHANGING ) {
             QuartzJobExecutor.IS_LEADER.set(false);
+            QuartzJobExecutor.DISCOVERY_INFO_AVAILABLE.set(false);
         }
     }
 }



Mime
View raw message