recursively(e, new Predicate<EntityInternal>() { public boolean apply(EntityInternal it) { if (shouldSkipUnmanagement(it)) return false; it.getManagementSupport().onManagementStopping(info); return true; } });
recursively(e, new Predicate<EntityInternal>() { public boolean apply(EntityInternal it) { if (it.getManagementSupport().isDeployed()) { return false; } else { preManageNonRecursive(it); it.getManagementSupport().onManagementStarting(info); return manageNonRecursive(it); } } });
/** returns false if deleted, throws exception if invalid state, otherwise true. * okay if entity is not yet managed. */ protected boolean check(Entity producer) { if (destroyed.get()) return false; if (entity==null) throw new IllegalStateException(this+" cannot subscribe to "+producer+" because it is not associated to an entity"); if (((EntityInternal)entity).getManagementSupport().isNoLongerManaged()) throw new IllegalStateException(this+" cannot subscribe to "+producer+" because the associated entity "+entity+" is no longer managed"); return true; }
protected synchronized SubscriptionTracker getSubscriptionTracker() { if (_subscriptionTracker!=null) return _subscriptionTracker; if (entity==null) return null; _subscriptionTracker = new SubscriptionTracker(((EntityInternal)entity).getManagementSupport().getSubscriptionContext()); return _subscriptionTracker; }
@Override public T call() throws Exception { final ExecutionContext executionContext = ((EntityInternal) entity).getManagementSupport().getExecutionContext(); return executionContext.submit(Maps.newHashMap(), job).get(); } }
@Override public T call() throws Exception { final ExecutionContext executionContext = ((EntityInternal) entity).getManagementSupport().getExecutionContext(); return executionContext.submit(Maps.newHashMap(), job).get(); } }
recursively(e, new Predicate<EntityInternal>() { public boolean apply(EntityInternal it) { if (it.getManagementSupport().isFullyManaged()) { return false; } else { it.getManagementSupport().onManagementStarted(info); managementContext.getRebindManager().getChangeListener().onManaged(it); return true; } } }); }
public static boolean isManaged(Entity e) { return ((EntityInternal)e).getManagementSupport().isDeployed() && ((EntityInternal)e).getManagementContext().isRunning(); }
/** submits a task to run at the entity */ public static <T extends TaskAdaptable<?>> T submit(final Entity entity, final T task) { final ExecutionContext executionContext = ((EntityInternal)entity).getManagementSupport().getExecutionContext(); executionContext.submit(task.asTask()); return task; }
/** submits a task factory to construct its task at the entity (in a precursor task) and then to submit it; * important if e.g. task construction relies on an entity being in scope (in tags, via {@link BrooklynTasks}) */ public static <T extends TaskAdaptable<?>> T submit(final Entity entity, final TaskFactory<T> taskFactory) { // TODO it is messy to have to do this, but not sure there is a cleaner way :( final Semaphore s = new Semaphore(0); final AtomicReference<T> result = new AtomicReference<T>(); final ExecutionContext executionContext = ((EntityInternal)entity).getManagementSupport().getExecutionContext(); executionContext.execute(new Runnable() { // TODO could give this task a name, like "create task from factory" @Override public void run() { T t = taskFactory.newTask(); result.set(t); s.release(); } }); try { s.acquire(); } catch (InterruptedException e) { throw Exceptions.propagate(e); } executionContext.submit(result.get().asTask()); return result.get(); }
public static void unmanage(Entity entity) { if (((EntityInternal)entity).getManagementSupport().isDeployed()) { ((EntityInternal)entity).getManagementContext().getEntityManager().unmanage(entity); } }
@Override public ClassLoader apply(@Nullable Object input) { if (input instanceof EntityInternal) return apply(((EntityInternal)input).getManagementSupport()); if (input instanceof EntityManagementSupport) return apply(((EntityManagementSupport)input).getManagementContext()); if (input instanceof AbstractManagementContext) return ((AbstractManagementContext)input).getCatalog().getRootClassLoader(); return null; } });
public SshPollValue call() throws Exception { ProcessTaskWrapper<Integer> taskWrapper = taskFactory.newTask(); final ExecutionContext executionContext = ((EntityInternal) entity).getManagementSupport().getExecutionContext(); executionContext.submit(taskWrapper); taskWrapper.block(); Optional<Integer> exitCode = Optional.fromNullable(taskWrapper.getExitCode()); return new SshPollValue(null, exitCode.or(-1), taskWrapper.getStdout(), taskWrapper.getStderr()); } };
recursively(e, new Predicate<EntityInternal>() { public boolean apply(EntityInternal it) { if (shouldSkipUnmanagement(it)) return false; boolean result = unmanageNonRecursive(it); it.getManagementSupport().onManagementStopped(info); managementContext.getRebindManager().getChangeListener().onUnmanaged(it); if (managementContext.gc != null) managementContext.gc.onUnmanaged(it); return result; } }); }
public SshPollValue call() throws Exception { ProcessTaskWrapper<String> taskWrapper = knifeTaskFactory.newTask(); final ExecutionContext executionContext = ((EntityInternal) entity).getManagementSupport().getExecutionContext(); log.debug("START: Running knife to query attributes of Chef node {}", nodeName); executionContext.submit(taskWrapper); taskWrapper.block(); log.debug("DONE: Running knife to query attributes of Chef node {}", nodeName); return new SshPollValue(null, taskWrapper.getExitCode(), taskWrapper.getStdout(), taskWrapper.getStderr()); } };
public synchronized void attemptLegacyAutodeployment(String effectorName) { if (managementContext!=null) { log.warn("Autodeployment suggested but not required for "+entity+"."+effectorName); return; } if (entity instanceof Application) { log.warn("Autodeployment with new management context triggered for "+entity+"."+effectorName+" -- will not be supported in future. Explicit manage call required."); if (initialManagementContext != null) { initialManagementContext.getEntityManager().manage(entity); } else { Entities.startManagement(entity); } return; } if ("start".equals(effectorName)) { Entity e=entity; if (e.getParent()!=null && ((EntityInternal)e.getParent()).getManagementSupport().isDeployed()) { log.warn("Autodeployment in parent's management context triggered for "+entity+"."+effectorName+" -- will not be supported in future. Explicit manage call required."); ((EntityInternal)e.getParent()).getManagementContext().getEntityManager().manage(entity); return; } } log.warn("Autodeployment not available for "+entity+"."+effectorName); }
public static <T> Task<T> invokeEffectorAsync(Entity entity, Effector<T> eff, Map<String,?> parameters) { String id = entity.getId(); String name = eff.getName(); if (log.isDebugEnabled()) log.debug("Invoking-async effector {} on {}", new Object[] {name, entity}); if (log.isTraceEnabled()) log.trace("Invoking-async effector {} on {} with args {}", new Object[] {name, entity, parameters}); EntityManagementSupport mgmtSupport = ((EntityInternal)entity).getManagementSupport(); if (!mgmtSupport.isDeployed()) { mgmtSupport.attemptLegacyAutodeployment(name); } ManagementContextInternal mgmtContext = (ManagementContextInternal) ((EntityInternal)entity).getManagementContext(); // FIXME seems brittle to have the listeners in the Utils method; better to move into the context.invokeEff // (or whatever the last mile before invoking the effector is) mgmtSupport.getEntityChangeListener().onEffectorStarting(eff); try { return mgmtContext.invokeEffector(entity, eff, parameters); } finally { // FIXME this is really Effector submitted mgmtSupport.getEntityChangeListener().onEffectorCompleted(eff); } }
/** * Invokes a method effector so that its progress is tracked. For internal use only, when we know the effector is backed by a method which is local. */ public static <T> T invokeMethodEffector(Entity entity, Effector<T> eff, Object[] args) { String name = eff.getName(); try { if (log.isDebugEnabled()) log.debug("Invoking effector {} on {}", new Object[] {name, entity}); if (log.isTraceEnabled()) log.trace("Invoking effector {} on {} with args {}", new Object[] {name, entity, args}); EntityManagementSupport mgmtSupport = ((EntityInternal)entity).getManagementSupport(); if (!mgmtSupport.isDeployed()) { mgmtSupport.attemptLegacyAutodeployment(name); } ManagementContextInternal mgmtContext = (ManagementContextInternal) ((EntityInternal)entity).getManagementContext(); mgmtSupport.getEntityChangeListener().onEffectorStarting(eff); try { return mgmtContext.invokeEffectorMethodSync(entity, eff, args); } finally { mgmtSupport.getEntityChangeListener().onEffectorCompleted(eff); } } catch (Exception e) { handleEffectorException(entity, eff, e); // (won't return below) return null; } }
public static <T> Task<T> invokeEffector(EntityLocal callingEntity, Entity entityToCall, final Effector<T> effector, final Map<String,?> parameters) { Task<T> t = Effectors.invocation(entityToCall, effector, parameters).asTask(); // we pass to callingEntity for consistency above, but in exec-context it should be // re-dispatched to targetEntity ((EntityInternal)callingEntity).getManagementSupport().getExecutionContext().submit( MutableMap.of("tag", BrooklynTasks.tagForCallerEntity(callingEntity)), t); return t; } @SuppressWarnings("unchecked")
/** * activates management when effector invoked, warning unless context is acceptable * (currently only acceptable context is "start") */ void manageIfNecessary(Entity entity, Object context) { if (!isRunning()) { return; // TODO Still a race for terminate being called, and then isManaged below returning false } else if (((EntityInternal)entity).getManagementSupport().wasDeployed()) { return; } else if (isManaged(entity)) { return; } else if (isPreManaged(entity)) { return; } else { Entity rootUnmanaged = entity; while (true) { Entity candidateUnmanagedParent = rootUnmanaged.getParent(); if (candidateUnmanagedParent == null || isManaged(candidateUnmanagedParent) || isPreManaged(candidateUnmanagedParent)) break; rootUnmanaged = candidateUnmanagedParent; } if (context == Startable.START.getName()) log.info("Activating local management for {} on start", rootUnmanaged); else log.warn("Activating local management for {} due to effector invocation on {}: {}", new Object[]{rootUnmanaged, entity, context}); manage(rootUnmanaged); } }