/** convenience - invokes in parallel if multiple, but otherwise invokes the item directly */ public static Task<?> invokeEffector(EntityLocal callingEntity, Iterable<? extends Entity> entitiesToCall, final Effector<?> effector) { return invokeEffector(callingEntity, entitiesToCall, effector, Collections.<String,Object>emptyMap()); }
/** convenience - invokes in parallel if multiple, but otherwise invokes the item directly */ public static Task<?> invokeEffector(EntityLocal callingEntity, Iterable<? extends Entity> entitiesToCall, final Effector<?> effector, final Map<String,?> parameters) { if (Iterables.size(entitiesToCall)==1) return invokeEffector(callingEntity, entitiesToCall.iterator().next(), effector, parameters); else return invokeEffectorList(callingEntity, entitiesToCall, effector, parameters); } /** convenience - invokes in parallel if multiple, but otherwise invokes the item directly */
public static <T> Task<T> invokeEffector(EntityLocal callingEntity, Entity entityToCall, final Effector<T> effector) { return invokeEffector(callingEntity, entityToCall, effector, Collections.<String,Object>emptyMap()); }
@SuppressWarnings("unchecked") public static <T> Task<T> invokeEffectorWithArgs(EntityLocal callingEntity, Entity entityToCall, final Effector<T> effector, Object ...args) { return invokeEffector(callingEntity, entityToCall, effector, EffectorUtils.prepareArgsForEffectorAsMapFromArray(effector, args)); } public static <T> Task<T> invokeEffector(EntityLocal callingEntity, Entity entityToCall,
/** Common implementation for stop in parent nodes; just invokes stop on all children of the entity */ public static void stop(EntityLocal e) { log.debug("Stopping entity "+e); Iterable<Entity> startables = filterStartableManagedEntities(e.getChildren()); if (!Iterables.isEmpty(startables)) { Entities.invokeEffector(e, startables, Startable.STOP).getUnchecked(); } if (log.isDebugEnabled()) log.debug("Stopped entity "+e); }
/** Common implementation for restart in parent nodes; just invokes stop on all children of the entity */ public static void restart(EntityLocal e) { log.debug("Restarting entity "+e); Iterable<Entity> startables = filterStartableManagedEntities(e.getChildren()); if (!Iterables.isEmpty(startables)) { Entities.invokeEffector(e, startables, Startable.RESTART).getUnchecked(); } if (log.isDebugEnabled()) log.debug("Restarted entity "+e); }
public Task<?> start(Application app, ApplicationSpec spec) { // Start all the managed entities by asking the app instance to start in background Function<String, Location> buildLocationFromId = new Function<String, Location>() { @Override public Location apply(String id) { id = fixLocation(id); return getLocationRegistry().resolve(id); } }; ArrayList<Location> locations = Lists.newArrayList(transform(spec.getLocations(), buildLocationFromId)); return Entities.invokeEffector((EntityLocal)app, app, Startable.START, MutableMap.of("locations", locations)); }
/** Common implementation for start in parent nodes; just invokes start on all children of the entity */ public static void start(EntityLocal e, Collection<? extends Location> locations) { log.debug("Starting entity "+e+" at "+locations); Iterable<Entity> startables = filterStartableManagedEntities(e.getChildren()); if (!Iterables.isEmpty(startables)) { Entities.invokeEffector(e, startables, Startable.START, MutableMap.of("locations", locations)).getUnchecked(); } }
/** convenience for starting an entity, esp a new Startable instance which has been created dynamically * (after the application is started) */ public static void start(Entity e, Collection<? extends Location> locations) { if (!isManaged(e) && !manage(e)) { log.warn("Using discouraged mechanism to start management -- Entities.start(Application, Locations) -- caller should create and use the preferred management context"); startManagement(e); } if (e instanceof Startable) Entities.invokeEffector((EntityLocal)e, e, Startable.START, MutableMap.of("locations", locations)).getUnchecked(); }
/** * Decreases the cluster size by the given number. * Called when synchronized on mutex, so overriders beware! */ protected void shrink(int delta) { Collection<Entity> removedEntities = pickAndRemoveMembers(delta * -1); // FIXME symmetry in order of added as child, managed, started, and added to group // FIXME assume stoppable; use logic of grow? Task<?> invoke = Entities.invokeEffector(this, removedEntities, Startable.STOP, Collections.<String,Object>emptyMap()); try { invoke.get(); } catch (Exception e) { throw Exceptions.propagate(e); } finally { for (Entity removedEntity : removedEntities) { discardNode(removedEntity); } } }
/** stops, destroys, and unmanages the given entity -- does as many as are valid given the type and state */ public static void destroy(Entity e) { if (isManaged(e)) { if (e instanceof Startable) Entities.invokeEffector((EntityLocal)e, e, Startable.STOP).getUnchecked(); if (e instanceof EntityInternal) ((EntityInternal)e).destroy(); unmanage(e); log.debug("destroyed and unmanaged "+e+"; mgmt now "+ (e.getApplicationId()==null ? "(no app)" : e.getApplication().getManagementContext())+" - managed? "+isManaged(e)); } else { log.debug("skipping destroy of "+e+": not managed"); } }
/** * stops, destroys, and unmanages the given application -- and terminates the mangaement context; * does as many as are valid given the type and state * @deprecated since 0.6.0 use destroy(Application) if you DONT want to destroy the mgmt context, * or destroy(app.getManagementContext()) if you want to destory everything in the app's mgmt context */ @Deprecated public static void destroyAll(Application app) { if (isManaged(app)) { ManagementContext managementContext = app.getManagementContext(); if (app instanceof Startable) Entities.invokeEffector((EntityLocal)app, app, Startable.STOP).getUnchecked(); if (app instanceof AbstractEntity) ((AbstractEntity)app).destroy(); unmanage(app); if (managementContext instanceof ManagementContextInternal) ((ManagementContextInternal)managementContext).terminate(); } }
protected synchronized void onDetectedFailure(SensorEvent<Object> event) { if (isSuspended()) { LOG.warn("ServiceRestarter suspended, so not acting on failure detected at "+entity+" ("+event.getValue()+")"); return; } LOG.warn("ServiceRestarter acting on failure detected at "+entity+" ("+event.getValue()+")"); long current = System.currentTimeMillis(); Long last = lastFailureTime.getAndSet(current); long elapsed = last==null ? -1 : current-last; if (elapsed>=0 && elapsed <= getConfig(FAIL_ON_RECURRING_FAILURES_IN_THIS_DURATION)) { onRestartFailed("Restart failure (failed again after "+Time.makeTimeStringRounded(elapsed)+") at "+entity+": "+event.getValue()); return; } try { entity.setAttribute(Attributes.SERVICE_STATE, Lifecycle.STARTING); Entities.invokeEffector(entity, entity, Startable.RESTART).get(); } catch (Exception e) { onRestartFailed("Restart failure (error "+e+") at "+entity+": "+event.getValue()); } }
@Override public void removeRegion(String id) { Entity entity = getManagementContext().getEntityManager().getEntity(id); Preconditions.checkNotNull(entity, "No entity found for %s", id); Preconditions.checkArgument(this.equals(entity.getParent()), "Wrong parent (%s) for %s", entity.getParent(), entity); Collection<Location> childLocations = entity.getLocations(); if (entity instanceof Startable) { try { Entities.invokeEffector(this, entity, Startable.STOP).get(); } catch (Exception e) { Exceptions.propagateIfFatal(e); log.warn("Error stopping "+entity+" ("+e+"); proceeding to remove it anyway"); log.debug("Error stopping "+entity+" ("+e+"); proceeding to remove it anyway", e); } } removeChild(entity); removeLocations(childLocations); }
@Override public void stop() { setAttribute(SERVICE_STATE, Lifecycle.STOPPING); try { Iterable<Entity> stoppableChildren = Iterables.filter(getChildren(), Predicates.instanceOf(Startable.class)); Task<?> invoke = Entities.invokeEffector(this, stoppableChildren, Startable.STOP); if (invoke != null) invoke.get(); setAttribute(SERVICE_STATE, Lifecycle.STOPPED); setAttribute(SERVICE_UP, false); } catch (Exception e) { setAttribute(SERVICE_STATE, Lifecycle.ON_FIRE); throw Exceptions.propagate(e); } }
@Override public void start(Collection<? extends Location> locations) { if (isLegacyConstruction()) { init(); } if (locations.isEmpty()) locations = getLocations(); Iterables.getOnlyElement(locations); // Assert just one addLocations(locations); List<Entity> childrenToStart = MutableList.<Entity>of(getCluster()); // Set the KafkaZookeeper entity as child of cluster, if it does not already have a parent if (getZookeeper().getParent() == null) { addChild(getZookeeper()); } // And only start zookeeper if we are parent if (Objects.equal(this, getZookeeper().getParent())) childrenToStart.add(getZookeeper()); Entities.invokeEffector(this, childrenToStart, Startable.START, ImmutableMap.of("locations", locations)).getUnchecked(); connectSensors(); }