brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [21/31] git commit: fix and clarify queueing of tasks which have already been submitted
Date Tue, 29 Jul 2014 19:32:16 GMT
fix and clarify queueing of tasks which have already been submitted


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/40ce411c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/40ce411c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/40ce411c

Branch: refs/heads/master
Commit: 40ce411cadca61c5ff3b61e22a020f10e0f8a33f
Parents: a5c2987
Author: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Authored: Tue Jul 29 13:31:03 2014 -0400
Committer: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Committed: Tue Jul 29 14:39:24 2014 -0400

----------------------------------------------------------------------
 .../brooklyn/management/TaskQueueingContext.java |  6 +++++-
 .../main/java/brooklyn/util/task/BasicTask.java  |  9 +++++++--
 .../java/brooklyn/util/task/DynamicTasks.java    | 19 ++++++++++++++-----
 .../java/brooklyn/util/task/ForwardingTask.java  |  5 +++++
 .../java/brooklyn/util/task/TaskInternal.java    |  2 +-
 core/src/main/java/brooklyn/util/task/Tasks.java | 10 +++++++++-
 6 files changed, 41 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/40ce411c/api/src/main/java/brooklyn/management/TaskQueueingContext.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/brooklyn/management/TaskQueueingContext.java b/api/src/main/java/brooklyn/management/TaskQueueingContext.java
index 813ed19..431ee17 100644
--- a/api/src/main/java/brooklyn/management/TaskQueueingContext.java
+++ b/api/src/main/java/brooklyn/management/TaskQueueingContext.java
@@ -33,7 +33,11 @@ import com.google.common.annotations.Beta;
 @Beta
 public interface TaskQueueingContext {
 
-    /** queues the task for submission as part of this queueing context; should mark it as
submitted */
+    /** queues the task for submission as part of this queueing context
+     * <p>
+     * implementations should mark it as queued but not yet submitted.
+     * note the task may have already been submitted, and is being queued here for informational
purposes,
+     * in which case the implementation should not run it. */
     public void queue(Task<?> t);
     
     /** returns a list of queued tasks (immutable copy) */

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/40ce411c/core/src/main/java/brooklyn/util/task/BasicTask.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/util/task/BasicTask.java b/core/src/main/java/brooklyn/util/task/BasicTask.java
index 2e0da38..8370512 100644
--- a/core/src/main/java/brooklyn/util/task/BasicTask.java
+++ b/core/src/main/java/brooklyn/util/task/BasicTask.java
@@ -231,13 +231,18 @@ public class BasicTask<T> implements TaskInternal<T> {
     // basic fields --------------------
 
     @Override
+    public boolean isQueued() {
+        return (queuedTimeUtc >= 0);
+    }
+
+    @Override
     public boolean isQueuedOrSubmitted() {
-        return (queuedTimeUtc >= 0) || isSubmitted();
+        return isQueued() || isSubmitted();
     }
 
     @Override
     public boolean isQueuedAndNotSubmitted() {
-        return (queuedTimeUtc >= 0) && (!isSubmitted());
+        return isQueued() && (!isSubmitted());
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/40ce411c/core/src/main/java/brooklyn/util/task/DynamicTasks.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/util/task/DynamicTasks.java b/core/src/main/java/brooklyn/util/task/DynamicTasks.java
index 33780d2..4a08d6f 100644
--- a/core/src/main/java/brooklyn/util/task/DynamicTasks.java
+++ b/core/src/main/java/brooklyn/util/task/DynamicTasks.java
@@ -226,7 +226,7 @@ public class DynamicTasks {
     public static <V extends TaskAdaptable<?>> V queue(V task) {
         try {
             Preconditions.checkNotNull(task, "Task to queue cannot be null");
-            Preconditions.checkState(!Tasks.isQueuedOrSubmitted(task), "Task to queue must
not yet be submitted: %s", task);
+            Preconditions.checkState(!Tasks.isQueued(task), "Task to queue must not yet be
queued: %s", task);
             TaskQueueingContext adder = getTaskQueuingContext();
             if (adder==null) 
                 throw new IllegalStateException("Task "+task+" cannot be queued here; no
queueing context available");
@@ -267,14 +267,23 @@ public class DynamicTasks {
         return DynamicTasks.queue(Tasks.<T>builder().name(name).body(job).build());
     }
 
+    /** queues the task if needed, i.e. if it is not yet submitted (so it will run), 
+     * or if it is submitted but not queued and we are in a queueing context (so it is available
for informational purposes) */
     public static <T extends TaskAdaptable<?>> T queueIfNeeded(T task) {
-        if (!Tasks.isQueuedOrSubmitted(task))
-            queue(task);
+        if (!Tasks.isQueued(task)) {
+            if (Tasks.isSubmitted(task) && getTaskQueuingContext()==null) {
+                // already submitted and not in a queueing context, don't try to queue
+            } else {
+                // needs submitting, put it in the queue
+                // (will throw an error if we are not a queueing context)
+                queue(task);
+            }
+        }
         return task;
     }
     
-    /** submits the given task if needed, and gets the result (unchecked) 
-     * only permitted in a queueing context (ie a DST main job) */
+    /** submits/queues the given task if needed, and gets the result (unchecked) 
+     * only permitted in a queueing context (ie a DST main job) if the task is not yet submitted
*/
     // things get really confusing if you try to queueInTaskHierarchy -- easy to cause deadlocks!
     public static <T> T get(TaskAdaptable<T> t) {
         return queueIfNeeded(t).asTask().getUnchecked();

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/40ce411c/core/src/main/java/brooklyn/util/task/ForwardingTask.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/util/task/ForwardingTask.java b/core/src/main/java/brooklyn/util/task/ForwardingTask.java
index 729e28e..e54ebef 100644
--- a/core/src/main/java/brooklyn/util/task/ForwardingTask.java
+++ b/core/src/main/java/brooklyn/util/task/ForwardingTask.java
@@ -193,6 +193,11 @@ public abstract class ForwardingTask<T> extends ForwardingObject
implements Task
     }
 
     @Override
+    public boolean isQueued() {
+        return delegate().isQueued();
+    }
+
+    @Override
     public boolean isQueuedOrSubmitted() {
         return delegate().isQueuedOrSubmitted();
     }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/40ce411c/core/src/main/java/brooklyn/util/task/TaskInternal.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/util/task/TaskInternal.java b/core/src/main/java/brooklyn/util/task/TaskInternal.java
index 5a797bc..d3a4ce8 100644
--- a/core/src/main/java/brooklyn/util/task/TaskInternal.java
+++ b/core/src/main/java/brooklyn/util/task/TaskInternal.java
@@ -53,8 +53,8 @@ public interface TaskInternal<T> extends Task<T> {
     Future<T> getResult();
     
     boolean isQueuedOrSubmitted();
-
     boolean isQueuedAndNotSubmitted();
+    boolean isQueued();
 
     /** marks the task as queued for execution */
     void markQueued();

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/40ce411c/core/src/main/java/brooklyn/util/task/Tasks.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/util/task/Tasks.java b/core/src/main/java/brooklyn/util/task/Tasks.java
index d423c86..9a7a062 100644
--- a/core/src/main/java/brooklyn/util/task/Tasks.java
+++ b/core/src/main/java/brooklyn/util/task/Tasks.java
@@ -327,6 +327,14 @@ public class Tasks {
         return isAncestorCancelled(t.getSubmittedByTask());
     }
 
+    public static boolean isQueued(TaskAdaptable<?> task) {
+        return ((TaskInternal<?>)task.asTask()).isQueued();
+    }
+
+    public static boolean isSubmitted(TaskAdaptable<?> task) {
+        return ((TaskInternal<?>)task.asTask()).isSubmitted();
+    }
+    
     public static boolean isQueuedOrSubmitted(TaskAdaptable<?> task) {
         return ((TaskInternal<?>)task.asTask()).isQueuedOrSubmitted();
     }
@@ -336,7 +344,7 @@ public class Tasks {
      * @return true if the task was added, false otherwise.
      */
     public static boolean tryQueueing(TaskQueueingContext adder, TaskAdaptable<?> task)
{
-        if (task==null || isQueuedOrSubmitted(task))
+        if (task==null || isQueued(task))
             return false;
         try {
             adder.queue(task.asTask());


Mime
View raw message