Java Code Examples for org.apache.brooklyn.core.entity.lifecycle.Lifecycle#STOPPED

The following examples show how to use org.apache.brooklyn.core.entity.lifecycle.Lifecycle#STOPPED . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NginxSshDriver.java    From brooklyn-library with Apache License 2.0 6 votes vote down vote up
private void reloadImpl() {
    // Note that previously, if serviceUp==false then we'd restart nginx.
    // That caused a race on stop()+reload(): nginx could simultaneously be stopping and also reconfiguring
    // (e.g. due to a cluster-resize), the restart() would leave nginx running even after stop() had returned.
    //
    // Now we rely on NginxController always calling update (and thus reload) once it has started. This is
    // done in AbstractController.postActivation().
    //
    // If our blocking check sees that !isRunning() (and if a separate thread is starting it, and subsequently
    // calling waitForEntityStart()), we can guarantee that the start-thread's call to update will happen after
    // this call to reload. So we this can be a no-op, and just rely on that subsequent call to update.

    Lifecycle lifecycle = entity.getAttribute(NginxController.SERVICE_STATE_ACTUAL);
    if (lifecycle==Lifecycle.STOPPING || lifecycle==Lifecycle.STOPPED || !isRunning()) {
        log.debug("Ignoring reload of nginx "+entity+", because service is not running (state "+lifecycle+")");
        return;
    }

    doReloadNow();
}
 
Example 2
Source File: EntityAdjuncts.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Beta
public static Lifecycle inferAdjunctStatus(EntityAdjunct a) {
    if (a.isRunning()) return Lifecycle.RUNNING;
    if (a.isDestroyed()) return Lifecycle.DESTROYED;
    
    // adjuncts don't currently support an "error" state; though that would be useful!
    
    if (a instanceof Policy) {
        if (((Policy)a).isSuspended()) return Lifecycle.STOPPED;
        return Lifecycle.CREATED;
    }
    if (a instanceof Feed) {
        if (((Feed)a).isSuspended()) return Lifecycle.STOPPED;
        if (((Feed)a).isActivated()) return Lifecycle.STARTING;
        return Lifecycle.CREATED;
    }

    // Enricher doesn't support suspend so if not running or destroyed then
    // it is just created
    return Lifecycle.CREATED;
}
 
Example 3
Source File: SshCommandEffector.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
public String callMany(Collection<Entity> targets, ConfigBag params) {
    TaskBuilder<Object> ptb = Tasks.builder().parallel(true).displayName("effector "+effector.getName()+" ssh to targets");
    for (Entity target: targets) {
        if (Entities.isNoLongerManaged(target)) continue;
        
        Lifecycle state = target.getAttribute(Attributes.SERVICE_STATE_ACTUAL);
        if (state==Lifecycle.STOPPING || state==Lifecycle.STOPPED) continue;

        Maybe<SshMachineLocation> machine = Locations.findUniqueSshMachineLocation(target.getLocations());
        if (machine.isAbsent()) continue;
        
        SshEffectorTaskFactory<String> t = makePartialTaskFactory(params, target);
        t.summary("effector "+effector.getName()+" at "+target); 
        t.machine( machine.get() );
        
        ptb.add(t.newTask());
    }
    queue(ptb.build()).getUnchecked();
    return null;
}
 
Example 4
Source File: CassandraFabricImpl.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
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_ACTUAL);
    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 Fabric {}: viableSeed={}; hostname={}; serviceUp={}; serviceState={}; hasFailed={}", new Object[] {member, CassandraFabricImpl.this, result, hostname, serviceUp, serviceState, hasFailed});
    return result;
}
 
Example 5
Source File: CassandraDatacenterImpl.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
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_ACTUAL);
    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;
}
 
Example 6
Source File: MachineLifecycleEffectorTasks.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Override
public MachineLocation call() throws Exception {
    // Blocks if a latch was configured.
    entity().getConfig(BrooklynConfigKeys.PROVISION_LATCH);
    final Map<String, Object> flags = obtainProvisioningFlags(location);
    if (!(location instanceof LocalhostMachineProvisioningLocation))
        log.info("Starting {}, obtaining a new location instance in {} with ports {}", new Object[]{entity(), location, flags.get("inboundPorts")});
    entity().sensors().set(SoftwareProcess.PROVISIONING_LOCATION, location);
    Transition expectedState = entity().sensors().get(Attributes.SERVICE_STATE_EXPECTED);
    
    // BROOKLYN-263: see corresponding code in doStop()
    if (expectedState != null && (expectedState.getState() == Lifecycle.STOPPING || expectedState.getState() == Lifecycle.STOPPED)) {
        throw new IllegalStateException("Provisioning aborted before even begun for "+entity()+" in "+location+" (presumably by a concurrent call to stop");
    }
    entity().sensors().set(AttributesInternal.INTERNAL_PROVISIONING_TASK_STATE, ProvisioningTaskState.RUNNING);
    
    MachineLocation machine;
    try {
        machine = Tasks.withBlockingDetails("Provisioning machine in " + location, new ObtainLocationTask(location, flags));
        entity().sensors().set(INTERNAL_PROVISIONED_MACHINE, machine);
    } finally {
        entity().sensors().remove(AttributesInternal.INTERNAL_PROVISIONING_TASK_STATE);
    }
    
    if (machine == null) {
        throw new NoMachinesAvailableException("Failed to obtain machine in " + location.toString());
    }
    if (log.isDebugEnabled()) {
        log.debug("While starting {}, obtained new location instance {}", entity(),
                (machine instanceof SshMachineLocation
                        ? machine + ", details " + ((SshMachineLocation) machine).getUser() + ":" + Sanitizer.sanitize(((SshMachineLocation) machine).config().getLocalBag())
                        : machine));
    }
    return machine;
}
 
Example 7
Source File: MachineLifecycleEffectorTasks.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Override
public String call() {
    if (entity().getAttribute(SoftwareProcess.SERVICE_STATE_ACTUAL) == Lifecycle.STOPPED) {
        log.debug("Skipping stop of entity " + entity() + " when already stopped");
        return "Already stopped";
    }
    ServiceStateLogic.setExpectedState(entity(), Lifecycle.STOPPING);
    entity().sensors().set(SoftwareProcess.SERVICE_UP, false);
    preStopCustom();
    return null;
}
 
Example 8
Source File: SoftwareProcessImpl.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
protected void onUpdated() {
    Boolean up = entity.getAttribute(SERVICE_UP);
    Lifecycle state = entity.getAttribute(SERVICE_STATE_ACTUAL);
    if (up == null || up) {
        entity.sensors().set(ServiceStateLogic.SERVICE_NOT_UP_DIAGNOSTICS, ImmutableMap.<String, Object>of());
    } else if (state == null || state == Lifecycle.CREATED || state == Lifecycle.STARTING) {
        // not yet started; do nothing
    } else if (state == Lifecycle.STOPPING || state == Lifecycle.STOPPED || state == Lifecycle.DESTROYED) {
        // stopping/stopped, so expect not to be up; get rid of the diagnostics.
        entity.sensors().set(ServiceStateLogic.SERVICE_NOT_UP_DIAGNOSTICS, ImmutableMap.<String, Object>of());
    } else {
        ((SoftwareProcess)entity).populateServiceNotUpDiagnostics();
    }
}
 
Example 9
Source File: MachineLifecycleEffectorTasks.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
public static boolean canStop(StopMode stopMode, Entity entity) {
    boolean isEntityStopped = entity.getAttribute(SoftwareProcess.SERVICE_STATE_ACTUAL)==Lifecycle.STOPPED;
    return canStop(stopMode, isEntityStopped);
}
 
Example 10
Source File: MachineLifecycleEffectorTasksTest.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
public static boolean canStop(Application parent, StopMode stopMode, boolean isEntityStopped) {
    Lifecycle state = isEntityStopped ? Lifecycle.STOPPED : Lifecycle.RUNNING;
    BasicEntity entity = parent.addChild(EntitySpec.create(BasicEntity.class));
    entity.sensors().set(SoftwareProcess.SERVICE_STATE_ACTUAL, state);
    return MachineLifecycleEffectorTasks.canStop(stopMode, entity);
}
 
Example 11
Source File: SshCommandMembershipTrackingPolicy.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private void execute(Entity target, String command, String type, String memberId, boolean highlight) {
    if (Entities.isNoLongerManaged(target)) return;
    Lifecycle state = target.getAttribute(Attributes.SERVICE_STATE_ACTUAL);
    if (state==Lifecycle.STOPPING || state==Lifecycle.STOPPED) return;
    
    Collection<? extends Location> locations = Locations.getLocationsCheckingAncestors(target.getLocations(), target);
    Maybe<SshMachineLocation> machine = Machines.findUniqueMachineLocation(locations, SshMachineLocation.class);
    if (machine.isAbsentOrNull()) {
        LOG.debug("No machine available to execute command");
        return;
    }

    LOG.info("Executing command on {}: {}", machine.get(), command);
    String executionDir = config().get(EXECUTION_DIR);
    String sshCommand = SshCommandSensor.makeCommandExecutingInDirectory(command, executionDir, target);

    // Set things from the entities defined shell environment, overriding with our config
    Map<String, Object> env = MutableMap.of();
    env.putAll(MutableMap.copyOf(entity.config().get(BrooklynConfigKeys.SHELL_ENVIRONMENT)));
    env.putAll(MutableMap.copyOf(config().get(BrooklynConfigKeys.SHELL_ENVIRONMENT)));

    // Add variables describing this invocation
    env.put(EVENT_TYPE, type);
    env.put(MEMBER_ID, memberId);

    // Try to resolve the configuration in the env Map
    try {
        env = (Map<String, Object>) Tasks.resolveDeepValue(env, Object.class, getExecutionContext());
    } catch (InterruptedException | ExecutionException e) {
        throw Exceptions.propagate(e);
    }

    // Execute the command with the serialized environment strings
    ShellEnvironmentSerializer serializer = new ShellEnvironmentSerializer(getManagementContext());
    SshEffectorTasks.SshEffectorTaskFactory<String> task = SshEffectorTasks.ssh(sshCommand)
            .machine(machine.get())
            .requiringZeroAndReturningStdout()
            .summary("group-" + CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_HYPHEN, type))
            .environmentVariables(serializer.serialize(env));

    Task<String> taskI = DynamicTasks.submit(task.newTask(), target);
    if (highlight) {
        highlightAction("Run at "+machine.get().getAddress().getHostAddress(), taskI);
    }
    String output = taskI.getUnchecked();
    LOG.trace("Command returned: {}", output);
}
 
Example 12
Source File: AttributePollHandler.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
protected boolean isTransitioningOrStopped() {
    if (entity==null) return false;
    Transition expected = entity.getAttribute(Attributes.SERVICE_STATE_EXPECTED);
    if (expected==null) return false;
    return (expected.getState()==Lifecycle.STARTING || expected.getState()==Lifecycle.STOPPING || expected.getState()==Lifecycle.STOPPED);
}