stdin.write("\n".getBytes()); tb.tag(BrooklynTaskTags.tagForStreamSoft(BrooklynTaskTags.STREAM_STDIN, stdin)); } catch (IOException e) { log.warn("Error registering stream "+BrooklynTaskTags.STREAM_STDIN+" on "+tb+": "+e, e); tb.tag(BrooklynTaskTags.tagForEnvStream(BrooklynTaskTags.STREAM_ENV, env)); tb.tag(BrooklynTaskTags.tagForStreamSoft(BrooklynTaskTags.STREAM_STDOUT, stdout)); stderr = new ByteArrayOutputStream(); tb.tag(BrooklynTaskTags.tagForStreamSoft(BrooklynTaskTags.STREAM_STDERR, stderr));
@SuppressWarnings("unchecked") protected ProcessTaskWrapper(AbstractProcessTaskFactory<?,RET> constructor) { super(constructor); TaskBuilder<Object> tb = constructor.constructCustomizedTaskBuilder(); if (stdout!=null) tb.tag(BrooklynTaskTags.tagForStreamSoft(BrooklynTaskTags.STREAM_STDOUT, stdout)); if (stderr!=null) tb.tag(BrooklynTaskTags.tagForStreamSoft(BrooklynTaskTags.STREAM_STDERR, stderr)); task = (Task<RET>) tb.body(new ProcessTaskInternalJob()).build(); }
/** creates the TaskBuilder which can be further customized; typically invoked by the initial {@link #newTask()} */ public TaskBuilder<Object> constructCustomizedTaskBuilder() { TaskBuilder<Object> tb = TaskBuilder.builder().dynamic(false).displayName("ssh: "+getSummary()); tb.tag(BrooklynTaskTags.tagForStream(BrooklynTaskTags.STREAM_STDIN, Streams.byteArrayOfString(Strings.join(commands, "\n")))); tb.tag(BrooklynTaskTags.tagForEnvStream(BrooklynTaskTags.STREAM_ENV, shellEnvironment)); return tb; }
stdin.write(cmdIn.getBytes()); tb.tag(BrooklynTaskTags.tagForStreamSoft(BrooklynTaskTags.STREAM_STDIN, stdin)); } catch (IOException e) { LOG.warn("Error registering stream "+BrooklynTaskTags.STREAM_STDIN+" on "+tb+": "+e, e); tb.tag(BrooklynTaskTags.tagForStreamSoft(BrooklynTaskTags.STREAM_STDOUT, stdout)); ByteArrayOutputStream stderr = new ByteArrayOutputStream(); tb.tag(BrooklynTaskTags.tagForStreamSoft(BrooklynTaskTags.STREAM_STDERR, stderr));
/** * 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); }
/** * 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); } }
public Task<V> build() { validate(); return Tasks.<V>builder().dynamic(false) .displayName("waiting on "+sensor.getName()) .description("Waiting on sensor "+sensor.getName()+" from "+source) .tag("attributeWhenReady") .tag(BrooklynTaskTags.TRANSIENT_TASK_TAG) .body(new WaitInTaskForAttributeReady<T,V>(this)) .build(); }
@Override public Task<?> call() { return Tasks.builder().dynamic(false).body(job).displayName("HA poller task").tag(BrooklynTaskTags.TRANSIENT_TASK_TAG) .description("polls HA status to see whether this node should promote").build(); } };
@Test public void testHasTag() throws Exception { Task<?> task = execManager.submit(TaskBuilder.<Object>builder() .body(Callables.<Object>returning("val")) .tag("mytag") .build()); assertTrue(TaskPredicates.hasTag("mytag").apply(task)); assertFalse(TaskPredicates.hasTag("wrongtag").apply(task)); }
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); }
@Override public void onEvent(SensorEvent<Boolean> event) { final MySqlNode node = (MySqlNode) event.getSource(); if (Boolean.TRUE.equals(event.getValue()) && // We are interested in SERVICE_PROCESS_IS_RUNNING only while haven't come online yet. // Probably will get several updates while replication is initialized so an additional // check is needed whether we have already seen this. Boolean.FALSE.equals(node.getAttribute(MySqlNode.SERVICE_UP)) && !Boolean.TRUE.equals(node.getAttribute(NODE_REPLICATION_INITIALIZED))) { // Events executed sequentially so no need to synchronize here. node.sensors().set(NODE_REPLICATION_INITIALIZED, Boolean.TRUE); final Runnable nodeInitTaskBody; if (MySqlClusterUtils.IS_MASTER.apply(node)) { nodeInitTaskBody = new InitMasterTaskBody(cluster, node); } else { nodeInitTaskBody = new InitSlaveTaskBody(cluster, node, lock); } DynamicTasks.submitTopLevelTask(TaskBuilder.builder() .displayName("setup master-slave replication") .body(nodeInitTaskBody) .tag(BrooklynTaskTags.NON_TRANSIENT_TASK_TAG) .build(), node); } }
@Override public Task<Void> call() { return Tasks.<Void>builder().dynamic(false).tag(BrooklynTaskTags.TRANSIENT_TASK_TAG) .displayName("ssh-location cache cleaner").body(new Callable<Void>() { @Override public Void call() { try { LoadingCache<Map<String, ?>, Pool<SshTool>> sshPoolCacheOrNullRef = sshPoolCacheOrNull; Task<?> cleanupTaskRef = cleanupTask; if (sshPoolCacheOrNullRef != null) sshPoolCacheOrNullRef.cleanUp(); if (!SshMachineLocation.this.isManaged()) { if (sshPoolCacheOrNullRef != null) sshPoolCacheOrNullRef.invalidateAll(); if (cleanupTaskRef != null) cleanupTaskRef.cancel(false); sshPoolCacheOrNull = null; } return null; } catch (Exception e) { // Don't rethrow: the behaviour of executionManager is different from a scheduledExecutorService, // if we throw an exception, then our task will never get executed again LOG.warn("Problem cleaning up ssh-pool-cache", e); return null; } catch (Throwable t) { LOG.warn("Problem cleaning up ssh-pool-cache (rethrowing)", t); throw Exceptions.propagate(t); } }}).build(); } };
@Override public Task<Entity> newTask() { return TaskBuilder.<Entity>builder() .displayName(toString()) .tag(BrooklynTaskTags.TRANSIENT_TASK_TAG) .body(new Callable<Entity>() { @Override public Entity call() { EntityInternal entity = entity(); Collection<Entity> entitiesToSearch = entity.getManagementContext().getEntityManager().getEntities(); Optional<Entity> result = Iterables.tryFind(entitiesToSearch, EntityPredicates.configEqualTo(TAG, tag)); if (result.isPresent()) { return result.get(); } else { throw new NoSuchElementException("No entity matching id " + tag+" in "+entitiesToSearch); } }}) .build(); } }
private Task<?> scheduleRecursiveTemporaryTask(final AtomicBoolean stopCondition, final Entity e, final Object... additionalTags) { // TODO Could alternate the test with expiring tasks in addition to transient TaskBuilder<Object> tb = Tasks.builder() .displayName("recursive") .dynamic(false) .tag(ManagementContextInternal.TRANSIENT_TASK_TAG) .body(new Runnable() { @Override public void run() { if (!stopCondition.get()) { scheduleRecursiveTemporaryTask(stopCondition, e, additionalTags); } } }); for (Object t : additionalTags) { tb.tag(t); } return ((EntityInternal)e).getExecutionContext().submit(tb.build()); }
protected static Task<?> runEmptyTaskWithNameAndTags(Entity target, String name, Object ...tags) { TaskBuilder<Object> tb = newEmptyTask(name); for (Object tag: tags) tb.tag(tag); Task<?> task = ((EntityInternal)target).getExecutionContext().submit(tb.build()); task.getUnchecked(); return task; }
@Test public void testSingleExecutionContextEntityWithTask() { // Should cause an exception to be thrown in future releases. For now will log a warning. // Until then make sure the task is tagged only with the context of the executor. final TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); Task<Void> task = Tasks.<Void>builder() .tag(BrooklynTaskTags.tagForContextEntity(entity)) .body(new AssertContextRunnable(ImmutableList.of(app))).build(); app.getExecutionContext().submit(task).getUnchecked(); }
/** * For resolving a {@link StructuredConfigKey}, such as a {@link MapConfigKey}. Here we need to * execute the custom logic, as is done by {@link #get(ConfigKey)}, but non-blocking! */ protected <T> Maybe<T> getNonBlockingResolvingStructuredKey(final ConfigKey<T> key) { Callable<T> job = new Callable<T>() { @Override public T call() { try { return get(key); } catch (RuntimeInterruptedException e) { throw Exceptions.propagate(e); // expected; return gracefully } } }; Task<T> t = Tasks.<T>builder().body(job) .displayName("Resolving dependent value") .description("Resolving "+key.getName()) .tag(BrooklynTaskTags.TRANSIENT_TASK_TAG) .build(); try { return getContext().getImmediately(t); } catch (ImmediateUnsupportedException e) { return Maybe.absent(); } }
public void testGcDynamicTaskAtNormalTagLimit() throws Exception { TestEntity e = app.createAndManageChild(EntitySpec.create(TestEntity.class)); ((BrooklynProperties)app.getManagementContext().getConfig()).put( BrooklynGarbageCollector.MAX_TASKS_PER_TAG, 2); AtomicBoolean stopCondition = new AtomicBoolean(); scheduleRecursiveTemporaryTask(stopCondition, e, "foo"); scheduleRecursiveTemporaryTask(stopCondition, e, "foo"); for (int count=0; count<5; count++) { TaskBuilder<Object> tb = Tasks.builder().displayName("task-"+count).dynamic(true).body(new Runnable() { @Override public void run() {}}) .tag(ManagementContextInternal.NON_TRANSIENT_TASK_TAG).tag("foo"); ((EntityInternal)e).getExecutionContext().submit(tb.build()).getUnchecked(); } // Makes sure there's a GC while the transient tasks are running forceGc(); stopCondition.set(true); // might need an eventually here, if the internal job completion and GC is done in the background // (if there are no test failures for a few months, since Sept 2014, then we can remove this comment) assertTaskMaxCountForEntityEventually(e, 2); }
} else { return Tasks.<V2>builder().displayName(name).description(descriptionBase) .tag("attributeWhenReady") .body(new Callable<V2>() { @Override public V2 call() throws Exception {
Entities.submit(entity, Tasks.<Void>builder().displayName("Effector-invoker") .description("Invoke in task") .tag(BrooklynTaskTags.NON_TRANSIENT_TASK_TAG) .body(new Callable<Void>() { @Override