@Override public boolean apply(@Nullable Entity input) { return input != null && Entities.isManaged(input); } };
/** * Specifies that the launcher should manage the given Brooklyn application. * The application must not yet be managed. * The application will not be started as part of this call (callers can * subsequently call {@link start()} or {@link getApplications()}. * * @see application(ApplicationBuilder) */ public BrooklynLauncher application(Application app) { if (Entities.isManaged(app)) throw new IllegalArgumentException("Application must not already be managed"); appsToManage.add(checkNotNull(app, "app")); return this; }
/** * Starts managing the given (unmanaged) app, using the given management context. * * @see #startManagement(Entity) */ public static ManagementContext startManagement(Application app, ManagementContext mgmt) { if (isManaged(app)) { throw new IllegalStateException("Application "+app+" is already managed, so can't set brooklyn properties"); } mgmt.getEntityManager().manage(app); return mgmt; }
public boolean isViableSeed(Entity member) { // TODO would be good to reuse the better logic in ServiceFailureDetector // (e.g. if that didn't just emit a notification but set a sensor as well?) boolean managed = Entities.isManaged(member); String hostname = member.getAttribute(Attributes.HOSTNAME); boolean serviceUp = Boolean.TRUE.equals(member.getAttribute(Attributes.SERVICE_UP)); Lifecycle serviceState = member.getAttribute(Attributes.SERVICE_STATE); boolean hasFailed = !managed || (serviceState == Lifecycle.ON_FIRE) || (serviceState == Lifecycle.RUNNING && !serviceUp) || (serviceState == Lifecycle.STOPPED); boolean result = (hostname != null && !hasFailed); if (log.isTraceEnabled()) log.trace("Node {} in Cluster {}: viableSeed={}; hostname={}; serviceUp={}; serviceState={}; hasFailed={}", new Object[] {member, this, result, hostname, serviceUp, serviceState, hasFailed}); return result; } public boolean isRunningSeed(Entity member) {
public boolean isViableSeed(Entity member) { // TODO remove duplication from CassandraClusterImpl.SeedTracker.isViableSeed boolean managed = Entities.isManaged(member); String hostname = member.getAttribute(Attributes.HOSTNAME); boolean serviceUp = Boolean.TRUE.equals(member.getAttribute(Attributes.SERVICE_UP)); Lifecycle serviceState = member.getAttribute(Attributes.SERVICE_STATE); boolean hasFailed = !managed || (serviceState == Lifecycle.ON_FIRE) || (serviceState == Lifecycle.RUNNING && !serviceUp) || (serviceState == Lifecycle.STOPPED); boolean result = (hostname != null && !hasFailed); if (log.isTraceEnabled()) log.trace("Node {} in Cluster {}: viableSeed={}; hostname={}; serviceUp={}; serviceState={}; hasFailed={}", new Object[] {member, this, result, hostname, serviceUp, serviceState, hasFailed}); return result; } };
/** * Starts managing the given (unmanaged) app, setting the given brooklyn properties on the new * management context. * * @see #startManagement(Entity) */ public static ManagementContext startManagement(Application app, BrooklynProperties props) { if (isManaged(app)) { throw new IllegalStateException("Application "+app+" is already managed, so can't set brooklyn properties"); } ManagementContext mgmt = new LocalManagementContext(props); mgmt.getEntityManager().manage(app); return mgmt; }
/** 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"); } }
/** brings this entity under management iff its ancestor is managed, returns true in that case; * otherwise returns false in the expectation that the ancestor will become managed, * or throws exception if it has no parent or a non-application root * (will throw if e is an Application; see also {@link #startManagement(Entity)} ) */ public static boolean manage(Entity e) { Entity o = e.getParent(); Entity eum = e; //highest unmanaged ancestor if (o==null) throw new IllegalStateException("Can't manage "+e+" because it is an orphan"); while (o.getParent()!=null) { if (!isManaged(o)) eum = o; o = o.getParent(); } if (isManaged(o)) { ((EntityInternal)o).getManagementContext().getEntityManager().manage(eum); return true; } if (!(o instanceof Application)) throw new IllegalStateException("Can't manage "+e+" because it is not rooted at an application"); return false; }
/** 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(); }
/** stops, destroys, and unmanages all apps in the given context, * and then terminates the management context */ public static void destroyAll(ManagementContext mgmt) { Exception error = null; if (mgmt instanceof NonDeploymentManagementContext) { // log here because it is easy for tests to destroyAll(app.getMgmtContext()) // which will *not* destroy the mgmt context if the app has been stopped! log.warn("Entities.destroyAll invoked on non-deployment "+mgmt+" - not likely to have much effect! " + "(This usually means the mgmt context has been taken from entity has been destroyed. " + "To destroy other things on the management context ensure you keep a handle to the context " + "before the entity is destroyed, such as by creating the management context first.)"); } if (!mgmt.isRunning()) return; log.debug("destroying all apps in "+mgmt+": "+mgmt.getApplications()); for (Application app: mgmt.getApplications()) { log.debug("destroying app "+app+" (managed? "+isManaged(app)+"; mgmt is "+mgmt+")"); try { destroy(app); log.debug("destroyed app "+app+"; mgmt now "+mgmt); } catch (Exception e) { log.warn("problems destroying app "+app+" (mgmt now "+mgmt+", will rethrow at least one exception): "+e); if (error==null) error = e; } } if (mgmt instanceof ManagementContextInternal) ((ManagementContextInternal)mgmt).terminate(); if (error!=null) throw Exceptions.propagate(error); }
/** * 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(); } }
/** brings this entity under management, creating a local management context if necessary * (assuming root is an application). * returns existing management context if there is one (non-deployment), * or new local mgmt context if not, * or throwing exception if root is not an application * <p> * callers are recommended to use {@link #manage(Entity)} instead unless they know * a plain-vanilla non-root management context is sufficient (e.g. in tests) * <p> * this method may change, but is provided as a stop-gap to prevent ad-hoc things * being done in the code which are even more likely to break! */ public static ManagementContext startManagement(Entity e) { Entity o = e; Entity eum = e; //highest unmanaged ancestor while (o.getParent()!=null) { if (!isManaged(o)) eum = o; o = o.getParent(); } if (isManaged(o)) { ManagementContext mgmt = ((EntityInternal)o).getManagementContext(); mgmt.getEntityManager().manage(eum); return mgmt; } if (!(o instanceof Application)) throw new IllegalStateException("Can't manage "+e+" because it is not rooted at an application"); ManagementContext mgmt = new LocalManagementContext(); mgmt.getEntityManager().manage(o); return mgmt; }