brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [06/10] brooklyn-server git commit: add limit when retrieving tasks
Date Sat, 22 Jul 2017 02:14:45 GMT
add limit when retrieving tasks


Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/36fd318c
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/36fd318c
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/36fd318c

Branch: refs/heads/master
Commit: 36fd318c21a54493042cc985c13efeaeaa91a715
Parents: a6c4e13
Author: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Authored: Thu Jul 6 15:49:14 2017 +0100
Committer: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Committed: Thu Jul 6 15:49:14 2017 +0100

----------------------------------------------------------------------
 .../apache/brooklyn/rest/api/ActivityApi.java   | 12 ++++++--
 .../brooklyn/rest/api/ApplicationApi.java       |  9 +++---
 .../org/apache/brooklyn/rest/api/EntityApi.java |  6 ++--
 .../rest/resources/ActivityResource.java        | 30 ++++++++++++++++----
 .../brooklyn/rest/resources/EntityResource.java | 10 ++++---
 5 files changed, 48 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/36fd318c/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/ActivityApi.java
----------------------------------------------------------------------
diff --git a/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/ActivityApi.java b/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/ActivityApi.java
index 45cd25c..31c4d92 100644
--- a/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/ActivityApi.java
+++ b/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/ActivityApi.java
@@ -61,13 +61,21 @@ public interface ActivityApi {
     @GET
     @Path("/{task}/children/recurse")
     @ApiOperation(
-            value = "Fetch all child tasks details as Map<String,TaskSummary> map key
== Task ID",
+            value = "Fetch all child tasks and their descendants with details as Map<String,TaskSummary>
map key == Task ID",
             response = Map.class)
     @ApiResponses(value = {
             @ApiResponse(code = 404, message = "Could not find task")
     })
     public Map<String,TaskSummary> getAllChildrenAsMap(
-            @ApiParam(value = "Task ID", required = true) @PathParam("task") String taskId);
+            @ApiParam(value = "Task ID", required = true) @PathParam("task") String taskId,
+            @ApiParam(value = "Max number of tasks to include, or -1 for all (default 200)",
required = false) 
+            @QueryParam("limit") @DefaultValue("200") int limit,
+            @ApiParam(value = "Max depth to traverse, or -1 for all (default)", required
= false) 
+            @QueryParam("maxDepth") @DefaultValue("-1") int maxDepth);
+    
+    /** @deprecated since 0.12.0 use {@link #getAllChildrenAsMap(String, int, int)} with
depth -1 */
+    @Deprecated
+    public Map<String,TaskSummary> getAllChildrenAsMap(String taskId);
 
     @GET
     @Path("/{task}/stream/{streamId}")

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/36fd318c/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/ApplicationApi.java
----------------------------------------------------------------------
diff --git a/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/ApplicationApi.java
b/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/ApplicationApi.java
index edfdd7d..d0e4d19 100644
--- a/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/ApplicationApi.java
+++ b/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/ApplicationApi.java
@@ -18,7 +18,6 @@
  */
 package org.apache.brooklyn.rest.api;
 
-import io.swagger.annotations.Api;
 import java.util.List;
 import java.util.Map;
 
@@ -35,15 +34,15 @@ import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 
-import org.apache.brooklyn.rest.domain.ApplicationSpec;
 import org.apache.brooklyn.rest.domain.ApplicationSummary;
-import org.apache.brooklyn.rest.domain.EntitySummary;
 import org.apache.brooklyn.rest.domain.EntityDetail;
+import org.apache.brooklyn.rest.domain.EntitySummary;
 
-import io.swagger.annotations.ApiResponse;
-import io.swagger.annotations.ApiResponses;
+import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import io.swagger.annotations.ApiParam;
+import io.swagger.annotations.ApiResponse;
+import io.swagger.annotations.ApiResponses;
 
 @Path("/applications")
 @Api("Applications")

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/36fd318c/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/EntityApi.java
----------------------------------------------------------------------
diff --git a/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/EntityApi.java b/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/EntityApi.java
index 7330365..d0788fa 100644
--- a/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/EntityApi.java
+++ b/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/EntityApi.java
@@ -112,10 +112,12 @@ public interface EntityApi {
     public List<TaskSummary> listTasks(
             @ApiParam(value = "Application ID or name", required = true) @PathParam("application")
String applicationId,
             @ApiParam(value = "Entity ID or name", required = true) @PathParam("entity")
String entityId,
-            @ApiParam(value = "Whether to include subtasks recursively across different entities",
required = false)
+            @ApiParam(value = "Max number of tasks, or -1 for all (default 200)", required
= false) 
+            @QueryParam("limit") @DefaultValue("200") int limit,
+            @ApiParam(value = "Whether to include subtasks recursively across different entities
(default false)", required = false)
             @QueryParam("recurse") @DefaultValue("false") Boolean recurse);
 
-    /** @deprecated since 0.12.0 use {@link #listTasks(String, String, Boolean)} */
+    /** @deprecated since 0.12.0 use {@link #listTasks(String, String, Integer, Boolean)}
*/
     @Deprecated
     public List<TaskSummary> listTasks(
         String applicationId,

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/36fd318c/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/ActivityResource.java
----------------------------------------------------------------------
diff --git a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/ActivityResource.java
b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/ActivityResource.java
index af49856..2bc90ad 100644
--- a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/ActivityResource.java
+++ b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/ActivityResource.java
@@ -40,6 +40,7 @@ import org.apache.brooklyn.util.collections.MutableList;
 import org.apache.brooklyn.util.collections.MutableSet;
 
 import com.google.common.collect.Collections2;
+import com.google.common.collect.Iterables;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 
@@ -52,10 +53,15 @@ public class ActivityResource extends AbstractBrooklynRestResource implements
Ac
         return TaskTransformer.fromTask(ui.getBaseUriBuilder()).apply(t);
     }
 
-    @Override
+    @Override @Deprecated
     public Map<String, TaskSummary> getAllChildrenAsMap(final String taskId) {
+        return getAllChildrenAsMap(taskId, 200, -1);
+    }
+    
+    @Override
+    public Map<String, TaskSummary> getAllChildrenAsMap(final String taskId, final
int limit, final int maxDepth) {
         final Task<?> parentTask = findTask(taskId);
-        return getAllDescendantTasks(parentTask);
+        return getAllDescendantTasks(parentTask, limit, maxDepth);
     }
 
     protected Task<?> findTask(final String taskId) {
@@ -67,14 +73,26 @@ public class ActivityResource extends AbstractBrooklynRestResource implements
Ac
         return task;
     }
 
-    private LinkedHashMap<String, TaskSummary> getAllDescendantTasks(final Task<?>
parentTask) {
+    private LinkedHashMap<String, TaskSummary> getAllDescendantTasks(final Task<?>
parentTask, int limit, int maxDepth) {
         final LinkedHashMap<String, TaskSummary> result = Maps.newLinkedHashMap();
         if (!(parentTask instanceof HasTaskChildren)) {
             return result;
         }
-        for (final Task<?> childTask : ((HasTaskChildren) parentTask).getChildren())
{
-            result.put(childTask.getId(), TaskTransformer.fromTask(ui.getBaseUriBuilder()).apply(childTask));
-            result.putAll(getAllDescendantTasks(childTask));
+        Set<Task<?>> nextLayer = MutableSet.copyOf( ((HasTaskChildren) parentTask).getChildren()
);
+        outer: while (!nextLayer.isEmpty() && maxDepth-- != 0) {
+            Set<Task<?>> thisLayer = nextLayer;
+            nextLayer = MutableSet.of();
+            for (final Task<?> childTask : thisLayer) {
+                TaskSummary wasThere = result.put(childTask.getId(), TaskTransformer.fromTask(ui.getBaseUriBuilder()).apply(childTask));
+                if (wasThere==null) {
+                    if (limit-- == 0) {
+                        break outer;
+                    }
+                    if (childTask instanceof HasTaskChildren) {
+                        Iterables.addAll(nextLayer, ((HasTaskChildren)childTask).getChildren());
+                    }
+                }
+            }
         }
         return result;
     }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/36fd318c/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/EntityResource.java
----------------------------------------------------------------------
diff --git a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/EntityResource.java
b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/EntityResource.java
index 47ec3ee..7007a7f 100644
--- a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/EntityResource.java
+++ b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/EntityResource.java
@@ -24,11 +24,9 @@ import static javax.ws.rs.core.Response.Status.ACCEPTED;
 import static org.apache.brooklyn.rest.util.WebResourceUtils.serviceAbsoluteUriBuilder;
 
 import java.net.URI;
-import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
@@ -132,7 +130,8 @@ public class EntityResource extends AbstractBrooklynRestResource implements
Enti
     }
 
     @Override
-    public List<TaskSummary> listTasks(String applicationId, String entityId, Boolean
recurse) {
+    public List<TaskSummary> listTasks(String applicationId, String entityId, int limit,
Boolean recurse) {
+        int sizeRemaining = limit;
         Entity entity = brooklyn().getEntity(applicationId, entityId);
         List<Task<?>> tasksToScan = MutableList.copyOf(BrooklynTaskTags.getTasksInEntityContext(mgmt().getExecutionManager(),
entity));
         Map<String,Task<?>> tasksLoaded = MutableMap.of();
@@ -140,6 +139,9 @@ public class EntityResource extends AbstractBrooklynRestResource implements
Enti
         while (!tasksToScan.isEmpty()) {
             Task<?> t = tasksToScan.remove(0);
             if (tasksLoaded.put(t.getId(), t)==null) {
+                if (--sizeRemaining==0) {
+                    break;
+                }
                 if (Boolean.TRUE.equals(recurse)) {
                     if (t instanceof HasTaskChildren) {
                         Iterables.addAll(tasksToScan, ((HasTaskChildren) t).getChildren()
);
@@ -153,7 +155,7 @@ public class EntityResource extends AbstractBrooklynRestResource implements
Enti
     
     @Override @Deprecated
     public List<TaskSummary> listTasks(String applicationId, String entityId) {
-        return listTasks(applicationId, entityId, false);
+        return listTasks(applicationId, entityId, -1, false);
     }
 
     @Override


Mime
View raw message