Java Code Examples for org.apache.brooklyn.api.mgmt.Task#getSubmittedByTask()

The following examples show how to use org.apache.brooklyn.api.mgmt.Task#getSubmittedByTask() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DynamicTasks.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
/** adds the given task to the nearest task addition context,
 * either set as a thread-local, or in the current task, or the submitter of the task, etc
 * <p>
 * throws if it cannot add or addition/execution would fail including if calling thread is interrupted */
public static <T> Task<T> queueInTaskHierarchy(Task<T> task) {
    Preconditions.checkNotNull(task, "Task to queue cannot be null");
    Preconditions.checkState(!Tasks.isQueuedOrSubmitted(task), "Task to queue must not yet be submitted: {}", task);
    
    if (Tasks.tryQueueing(getTaskQueuingContext(), task)) {
        log.debug("Queued task {} at context {} (no hierarchy)", task, getTaskQueuingContext());
        return task;
    }
    
    Task<?> t = Tasks.current();        
    while (t!=null) {
        if (t instanceof TaskQueueingContext) {
            if (Tasks.tryQueueing((TaskQueueingContext)t, task)) {
                log.debug("Queued task {} at hierarchical context {}", task, t);
                return task;
            }
        }
        t = t.getSubmittedByTask();
    }
    
    throw new IllegalStateException("No task addition context available in current task hierarchy for adding task "+task);
}
 
Example 2
Source File: Entitlements.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
/** 
 * Finds the currently applicable {@link EntitlementContext} by examining the current thread
 * then by investigating the current task, its submitter, etc. */
// NOTE: entitlements are propagated to tasks whenever they are created, as tags
// (see BrooklynTaskTags.tagForEntitlement and BasicExecutionContext.submitInternal).
// It might be cheaper to only do this lookup, not to propagate as tags, and to ensure
// all entitlement operations are wrapped in a task at source; but currently we do not
// do that so we need at least to set entitlement on the outermost task.
// Setting it on tasks submitted by a task is not strictly necessary (i.e. in BasicExecutionContext)
// but seems cheap enough, and means checking entitlements is fast, if we choose to do that more often.
public static EntitlementContext getEntitlementContext() {
    EntitlementContext context;
    context = PerThreadEntitlementContextHolder.perThreadEntitlementsContextHolder.get();
    if (context!=null) return context;
    
    Task<?> task = Tasks.current();
    while (task!=null) {
        context = BrooklynTaskTags.getEntitlement(task);
        if (context!=null) return context;
        task = task.getSubmittedByTask();
    }
    
    // no entitlements set -- assume entitlements not used, or system internal
    return null;
}
 
Example 3
Source File: BrooklynTaskTags.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
/**
 * checks if the given task is part of the given effector call on the given entity;
 * @param task  the task to check (false if null)
 * @param entity  the entity where this effector task should be running, or any entity if null
 * @param effector  the effector (matching name) where this task should be running, or any effector if null
 * @param allowNestedEffectorCalls  whether to match ancestor effector calls, e.g. if eff1 calls eff2,
 *   and we are checking eff2, whether to match eff1
 * @return whether the given task is part of the given effector
 */
public static boolean isInEffectorTask(Task<?> task, @Nullable Entity entity, @Nullable Effector<?> effector, boolean allowNestedEffectorCalls) {
    Task<?> t = task;
    while (t!=null) {
        Set<Object> tags = t.getTags();
        if (tags.contains(EFFECTOR_TAG)) {
            for (Object tag: tags) {
                if (tag instanceof EffectorCallTag) {
                    EffectorCallTag et = (EffectorCallTag)tag;
                    if (entity!=null && !et.getEntityId().equals(entity.getId()))
                        continue;
                    if (effector!=null && !et.getEffectorName().equals(effector.getName()))
                        continue;
                    return true;
                }
            }
            if (!allowNestedEffectorCalls) return false;
        }
        t = t.getSubmittedByTask();
    }
    return false;
}
 
Example 4
Source File: BrooklynTaskTags.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
/**
 * finds the task up the {@code child} hierarchy handling the {@code effector} call,
 * returns null if one doesn't exist. 
 */
@Beta
public static Task<?> getClosestEffectorTask(Task<?> child, Effector<?> effector) {
    Task<?> t = child;
    while (t != null) {
        Set<Object> tags = t.getTags();
        if (tags.contains(EFFECTOR_TAG)) {
            for (Object tag: tags) {
                if (tag instanceof EffectorCallTag) {
                    EffectorCallTag et = (EffectorCallTag) tag;
                    if (effector != null && !et.getEffectorName().equals(effector.getName()))
                        continue;
                    return t;
                }
            }
        }
        t = t.getSubmittedByTask();
    }
    return null;
}
 
Example 5
Source File: BrooklynDslDeferredSupplier.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
protected void checkAndTagForRecursiveReference(Entity targetEntity, String tag) {
    Task<?> ancestor = Tasks.current();
    if (ancestor!=null) {
        ancestor = ancestor.getSubmittedByTask();
    }
    while (ancestor!=null) {
        if (TaskTags.hasTag(ancestor, tag)) {
            throw new IllegalStateException("Recursive reference "+tag); 
        }
        ancestor = ancestor.getSubmittedByTask();
    }
    
    Tasks.addTagDynamically(tag);
}
 
Example 6
Source File: BrooklynNodeImpl.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
private Task<?> getTopEntityTask(Task<?> stopEffectorTask) {
    Entity context = BrooklynTaskTags.getContextEntity(stopEffectorTask);
    Task<?> topTask = stopEffectorTask;
    while (true) {
        Task<?> parentTask = topTask.getSubmittedByTask();
        Entity parentContext = BrooklynTaskTags.getContextEntity(parentTask);
        if (parentTask == null || parentContext != context) {
            return topTask;
        } else {
            topTask = parentTask;
        }
    }
}
 
Example 7
Source File: ActivityRestTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
private int depthOf(Task<?> t) {
    int depth = -1;
    while (t!=null) {
        t = t.getSubmittedByTask();
        depth++;
    }
    return depth;
}
 
Example 8
Source File: BrooklynGarbageCollector.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
private boolean isSubmitterExpired(Task<?> task) {
    if (Strings.isBlank(task.getSubmittedByTaskId())) {
        return false;
    }
    Task<?> submitter = task.getSubmittedByTask();
    if (submitter!=null && (!submitter.isDone() || executionManager.getTask(submitter.getId())!=null)) {
        return false;
    }
    // submitter task is GC'd
    return true;
}
 
Example 9
Source File: BrooklynTaskTags.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/** finds the first {@link EffectorCallTag} tag on this tag, or optionally on submitters, or null */
public static EffectorCallTag getEffectorCallTag(Task<?> task, boolean recurse) {
    Task<?> t = task;
    while (t!=null) {
        for (Object tag: getTagsFast(task)) {
            if (tag instanceof EffectorCallTag)
                return (EffectorCallTag)tag;
        }
        if (!recurse)
            return null;
        t = t.getSubmittedByTask();
    }
    return null;
}
 
Example 10
Source File: EntityResource.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
@Override
public int compare(Task<?> o1, Task<?> o2) {
    // absolute pref for submitted items
    if (!Objects.equal(o1.isSubmitted(), o2.isSubmitted())) {
        return o1.isSubmitted() ? -1 : 1;
    }

    // big pref for top-level tasks (manual operations), where submitter null
    int weight = 0;
    Task<?> o1s = o1.getSubmittedByTask();
    Task<?> o2s = o2.getSubmittedByTask();
    if ("start".equals(o1.getDisplayName()) ||"start".equals(o2.getDisplayName())) {
        weight = 0;
    }
    if (!Objects.equal(o1s==null, o2s==null))
        weight += 2*60*60 * (o1s==null ? -1 : 1);
    
    // pretty big pref for things invoked by other entities
    if (context!=null && o1s!=null && o2s!=null) {
        boolean o1se = context.equals(BrooklynTaskTags.getContextEntity(o1s));
        boolean o2se = context.equals(BrooklynTaskTags.getContextEntity(o2s));
        if (!Objects.equal(o1se, o2se))
            weight += 10*60 *  (o2se ? -1 : 1);
    }
    // slight pref for things in progress
    if (!Objects.equal(o1.isBegun() && !o1.isDone(), o2.isBegun() && !o2.isDone()))
        weight += 60 * (o1.isBegun() && !o1.isDone() ? -1 : 1);
    // and very slight pref for things not begun
    if (!Objects.equal(o1.isBegun(), o2.isBegun())) 
        weight += 10 * (!o1.isBegun() ? -1 : 1);
    
    // sort based on how recently the task changed state
    long now = System.currentTimeMillis();
    long t1 = o1.isDone() ? o1.getEndTimeUtc() : o1.isBegun() ? o1.getStartTimeUtc() : o1.getSubmitTimeUtc();
    long t2 = o2.isDone() ? o2.getEndTimeUtc() : o2.isBegun() ? o2.getStartTimeUtc() : o2.getSubmitTimeUtc();
    long u1 = now - t1;
    long u2 = now - t2;
    // so smaller = more recent
    // and if there is a weight, increase the other side so it is de-emphasised
    // IE if weight was -10 that means T1 is "10 times more interesting"
    // or more precisely, a task T1 from 10 mins ago equals a task T2 from 1 min ago
    if (weight<0) u2 *= -weight;
    else if (weight>0) u1 *= weight;
    if (u1!=u2) return u1 > u2 ? 1 : -1;
    // if equal under mapping, use weight
    if (weight!=0) return weight < 0 ? -1 : 1;
    // lastly use ID to ensure canonical order
    return o1.getId().compareTo(o2.getId());
}
 
Example 11
Source File: ActivityRestTest.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetActivitiesRecursiveAndWithLimit() {
    Task<?> t = entity.invoke(effector, null);
    Task<?> leaf = waitForCompletedDescendantWithChildAndSibling(t, t, CountdownTimer.newInstanceStarted(Duration.ONE_SECOND), 0);
    Task<?> leafParent = leaf.getSubmittedByTask();
    Task<?> leafGrandparent = leafParent.getSubmittedByTask();
    
    Response response = client().path("/activities/"+leafGrandparent.getId()+"/children")
        .accept(MediaType.APPLICATION_JSON)
        .get();
    assertHealthy(response);
    List<TaskSummary> tasksL = response.readEntity(new GenericType<List<TaskSummary>>() {});
    Assert.assertFalse(tasksContain(tasksL, leaf), "non-recursive tasks should not have included leaf "+leaf+"; was "+tasksL);
    Assert.assertTrue(tasksContain(tasksL, leafParent), "non-recursive tasks should have included leaf parent "+leafParent+"; was "+tasksL);
    Assert.assertFalse(tasksContain(tasksL, leafGrandparent), "non-recursive tasks should not have included leaf grandparent "+leafGrandparent+"; was "+tasksL);
    
    response = client().path("/activities/"+leafGrandparent.getId()+"/children/recurse")
        .accept(MediaType.APPLICATION_JSON)
        .get();
    assertHealthy(response);
    Map<String,TaskSummary> tasks = response.readEntity(new GenericType<Map<String,TaskSummary>>() {});
    Assert.assertTrue(tasksContain(tasks, leaf), "recursive tasks should have included leaf "+leaf+"; was "+tasks);
    Assert.assertTrue(tasksContain(tasks, leafParent), "recursive tasks should have included leaf parent "+leafParent+"; was "+tasks);
    Assert.assertFalse(tasksContain(tasks, leafGrandparent), "recursive tasks should not have included leaf grandparent "+leafGrandparent+"; was "+tasks);
    
    response = client().path("/activities/"+leafGrandparent.getId()+"/children/recurse")
        .query("maxDepth", 1)
        .accept(MediaType.APPLICATION_JSON)
        .get();
    assertHealthy(response);
    tasks = response.readEntity(new GenericType<Map<String,TaskSummary>>() {});
    Assert.assertFalse(tasksContain(tasks, leaf), "depth 1 recursive tasks should nont have included leaf "+leaf+"; was "+tasks);
    Assert.assertTrue(tasksContain(tasks, leafParent), "depth 1 recursive tasks should have included leaf parent "+leafParent+"; was "+tasks);

    response = client().path("/activities/"+leafGrandparent.getId()+"/children/recurse")
        .query("maxDepth", 2)
        .accept(MediaType.APPLICATION_JSON)
        .get();
    assertHealthy(response);
    tasks = response.readEntity(new GenericType<Map<String,TaskSummary>>() {});
    Assert.assertTrue(tasksContain(tasks, leaf), "depth 2 recursive tasks should have included leaf "+leaf+"; was "+tasks);
    Assert.assertTrue(tasksContain(tasks, leafParent), "depth 2 recursive tasks should have included leaf parent "+leafParent+"; was "+tasks);
    Assert.assertFalse(tasksContain(tasks, leafGrandparent), "depth 2 recursive tasks should not have included leaf grandparent "+leafGrandparent+"; was "+tasks);
    
    Assert.assertTrue(children(leafGrandparent).size() >= 2, "children: "+children(leafGrandparent));
    response = client().path("/activities/"+leafGrandparent.getId()+"/children/recurse")
        .query("limit", children(leafGrandparent).size())
        .accept(MediaType.APPLICATION_JSON)
        .get();
    assertHealthy(response);
    tasks = response.readEntity(new GenericType<Map<String,TaskSummary>>() {});
    Assert.assertEquals(tasks.size(), children(leafGrandparent).size());
    Assert.assertTrue(tasksContain(tasks, leafParent), "count limited recursive tasks should have included leaf parent "+leafParent+"; was "+tasks);
    Assert.assertFalse(tasksContain(tasks, leaf), "count limited recursive tasks should not have included leaf "+leaf+"; was "+tasks);
    
    response = client().path("/activities/"+leafGrandparent.getId()+"/children/recurse")
        .query("limit", children(leafGrandparent).size()+1)
        .accept(MediaType.APPLICATION_JSON)
        .get();
    assertHealthy(response);
    tasks = response.readEntity(new GenericType<Map<String,TaskSummary>>() {});
    Assert.assertEquals(tasks.size(), children(leafGrandparent).size()+1);
    tasks = response.readEntity(new GenericType<Map<String,TaskSummary>>() {});
    Assert.assertTrue(tasksContain(tasks, leafParent), "count+1 limited recursive tasks should have included leaf parent "+leafParent+"; was "+tasks);
    Assert.assertTrue(tasksContain(tasks, leaf), "count+1 limited recursive tasks should have included leaf "+leaf+"; was "+tasks);
}
 
Example 12
Source File: BrooklynGarbageCollector.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
private boolean isChild(Task<?> task) {
    Task<?> parent = task.getSubmittedByTask();
    return (parent instanceof HasTaskChildren && Iterables.contains(((HasTaskChildren)parent).getChildren(), task));
}
 
Example 13
Source File: BrooklynTaskTags.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
public static boolean isTransient(Task<?> task) { 
    if (hasTag(task, TRANSIENT_TASK_TAG)) return true;
    if (hasTag(task, NON_TRANSIENT_TASK_TAG)) return false;
    if (task.getSubmittedByTask()!=null) return isTransient(task.getSubmittedByTask());
    return false;
}