public static Task<List<?>> parallel(Iterable<? extends TaskAdaptable<?>> tasks) { return parallel(asTasks(Iterables.toArray(tasks, TaskAdaptable.class))); } public static Task<List<?>> parallel(String name, Iterable<? extends TaskAdaptable<?>> tasks) {
/** returns an unsubmitted task which will invoke the given effector on the given entities in parallel; * return type is Task<List<T>> (but haven't put in the blood sweat toil and tears to make the generics work) */ public static TaskAdaptable<List<?>> invocationParallel(Effector<?> eff, Map<?,?> params, Iterable<? extends Entity> entities) { List<TaskAdaptable<?>> tasks = new ArrayList<TaskAdaptable<?>>(); for (Entity e: entities) tasks.add(invocation(e, eff, params)); return Tasks.parallel( "invoking " + eff + " on " + tasks.size() + " node" + (Strings.s(tasks.size())), tasks.toArray(new TaskAdaptable[tasks.size()])); }
/** * Utility method that will execute the given function on the given nodes. The executions will be done in a parallel. * * @param nodes the nodes to execute the function on. * @param fn the function to execute. * @param <T> the type of node. * @return a new pool of tasks. */ // TODO: Pass the task's name and description as parameter protected <T> Task<List<?>> parallelListenerTask(final Iterable<T> nodes, final Function<T, ?> fn) { List<Task<?>> tasks = Lists.newArrayList(); for (final T node : nodes) { Task<?> t = Tasks.builder() .name(node.toString()) .description("Invocation on " + node.toString()) .body(new FunctionRunningCallable<T>(node, fn)) .tag(BrooklynTaskTags.NON_TRANSIENT_TASK_TAG) .build(); tasks.add(t); } return Tasks.parallel("Parallel invocation of " + fn + " on ambari nodes", tasks); }
tasks.add(newThrottledEffectorTask(member, Startable.STOP, Collections.emptyMap())); Task<?> invoke = Tasks.parallel(tasks.build()); DynamicTasks.queueIfPossible(invoke).orSubmitAsync(); try {
@Override public void expungeMembers(boolean stopFirst) { Set<Entity> members = ImmutableSet.copyOf(getMembers()); RuntimeException exception = null; if (stopFirst) { Map<Entity, Task<?>> tasks = Maps.newLinkedHashMap(); for (Entity member : members) { if (member instanceof Startable) { Task<Void> task = Effectors.invocation(member, Startable.STOP, ImmutableMap.of()).asTask(); tasks.put(member, task); } } DynamicTasks.queueIfPossible(Tasks.parallel("stopping "+tasks.size()+" member"+Strings.s(tasks.size())+" (parallel)", tasks.values())).orSubmitAsync(this); try { waitForTasksOnExpungeMembers(tasks); } catch (RuntimeException e) { Exceptions.propagateIfFatal(e); exception = e; LOG.warn("Problem stopping members of quarantine group "+this+" (rethrowing after unmanaging members): "+e); } } for (Entity member : members) { removeMember(member); Entities.unmanage(member); } if (exception != null) { throw exception; } }
/** * Utility method that will execute the given function on the given nodes, only if they have one of the given components * installed on them. The executions will be done in a parallel. * @param nodes the nodes to execute the function on. * @param fn the function to execute. * @param components the list of components for which we want to function to be executed. * @return a new pool of tasks. */ // TODO: Pass the task's name and description as parameter protected Task<List<?>> parallelListenerTask(final Iterable<AmbariAgent> nodes, final Function<AmbariAgent, ?> fn, List<String> components) { Preconditions.checkNotNull(components); List<Task<?>> tasks = Lists.newArrayList(); for (final AmbariAgent ambariAgent : nodes) { Preconditions.checkNotNull(ambariAgent.getAttribute(AmbariAgent.COMPONENTS)); if (!CollectionUtils.containsAny(ambariAgent.getAttribute(AmbariAgent.COMPONENTS), components)) { continue; } Task<?> t = Tasks.builder() .name(ambariAgent.toString()) .description("Invocation on " + ambariAgent.toString()) .body(new FunctionRunningCallable<AmbariAgent>(ambariAgent, fn)) .tag(BrooklynTaskTags.NON_TRANSIENT_TASK_TAG) .build(); tasks.add(t); } return Tasks.parallel("Parallel invocation of " + fn + " on ambari agents", tasks); } }
@Test public void testComplex() throws Exception { Task<List<?>> t = Tasks.sequential( sayTask("1"), sayTask("2"), Tasks.parallel(sayTask("4"), sayTask("3")), sayTask("5") ); ec.submit(t); Assert.assertEquals(t.get().size(), 4); Asserts.assertEqualsIgnoringOrder((List<?>)t.get().get(2), ImmutableSet.of("3", "4")); Assert.assertTrue(messages.equals(Arrays.asList("1", "2", "3", "4", "5")) || messages.equals(Arrays.asList("1", "2", "4", "3", "5")), "messages="+messages); }
Task<List<?>> parallel = Tasks.parallel("starting "+tasks.size()+" node"+Strings.s(tasks.size())+" (parallel)", tasks.values()); TaskTags.markInessential(parallel); DynamicTasks.queueIfPossible(parallel).orSubmitAsync(this);
private Task<List<?>> createParallelTask(String taskName, final Function<ExtraService, ?> fn) { List<Task<?>> tasks = Lists.newArrayList(); for (final ExtraService extraService : getExtraServices()) { Task<?> t = Tasks.builder() .name(extraService.getEntityType().getSimpleName()) .description("pre-cluster-deploy tasks for " + extraService.getEntityType().getName() + " extra service") .body(new FunctionRunningCallable<ExtraService>(extraService, fn)) .tag(BrooklynTaskTags.NON_TRANSIENT_TASK_TAG) .build(); tasks.add(t); } return Tasks.parallel(taskName, tasks); }
stoppingProcess = Tasks.parallel("stopping", Tasks.create("stopping (process)", new StopProcessesAtMachineTask()), Tasks.create("stopping (feeds)", new StopFeedsAtMachineTask()));
final Task<List<?>> t = Tasks.parallel( submitting(t1), sayTask("2-wait", Duration.minutes(10), "2-done"));