Java Code Examples for org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic#setExpectedState()

The following examples show how to use org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic#setExpectedState() . 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: FailingEntityImpl.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Override
public void stop() {
    getConfig(LISTENER).onEvent(this, "stop", new Object[0]);
    if (getConfig(FAIL_ON_STOP) || (getConfig(FAIL_ON_STOP_CONDITION) != null && getConfig(FAIL_ON_STOP_CONDITION).apply(this))) {
        if (Boolean.TRUE.equals(getConfig(SET_SERVICE_DOWN_ON_FAILURE))) {
            ServiceStateLogic.setExpectedState(this, Lifecycle.STOPPING);
            sensors().set(SERVICE_UP, false);
            ServiceStateLogic.setExpectedState(this, Lifecycle.STOPPED);
        }
        
        callHistory.add("stop");
        getConfig(EXEC_ON_FAILURE).apply(this);
        throw fail("Simulating entity stop failure for test");
    }
    super.stop();
}
 
Example 2
Source File: DependentConfigurationTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Test
public void testAttributeWhenReadyAbortsWhenAlreadyOnFireByDefault() throws Exception {
    ServiceStateLogic.setExpectedState(entity, Lifecycle.ON_FIRE);
    EntityAsserts.assertAttributeEqualsEventually(entity, Attributes.SERVICE_STATE_ACTUAL, Lifecycle.ON_FIRE);
    
    final Task<String> t = submit(DependentConfiguration.builder()
            .attributeWhenReady(entity, TestEntity.NAME)
            .build());

    try {
        assertDoneEventually(t);
        fail();
    } catch (Exception e) {
        if (!e.toString().contains("Aborted waiting for ready")) throw e;
    }
}
 
Example 3
Source File: SaltEntitySshDriver.java    From brooklyn-library with Apache License 2.0 6 votes vote down vote up
@Override
public void restart() {
    ServiceStateLogic.setExpectedState(getEntity(), Lifecycle.STOPPING);

    try {
        final Set<? extends String> restartStates = getEntity().config().get(SaltConfig.RESTART_STATES);
        LOG.debug("Executing Salt restart with states {}", restartStates);
        if (restartStates.isEmpty()) {
            restartBasedOnStartStates();
        } else {
            applyStates(restartStates);
        }
        ServiceStateLogic.setExpectedState(getEntity(), Lifecycle.RUNNING);
    } catch (Exception e) {
        getEntity().sensors().set(ServiceStateLogic.SERVICE_NOT_UP_DIAGNOSTICS,
                ImmutableMap.<String, Object>of("restart", e.getMessage()));
        ServiceStateLogic.setExpectedState(getEntity(), Lifecycle.ON_FIRE);
    }
}
 
Example 4
Source File: SoftwareProcessEntityRebindTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Test
public void testDoesNotCreateDriverAfterRebind() throws Exception {
    MyService origE = origApp.createAndManageChild(EntitySpec.create(MyService.class)
            .configure(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION, true));
            //the entity skips enricher initialization, do it explicitly
    origE.enrichers().add(ServiceStateLogic.newEnricherForServiceStateFromProblemsAndUp());
    
    MyProvisioningLocation origLoc = mgmt().getLocationManager().createLocation(LocationSpec.create(MyProvisioningLocation.class)
            .displayName("mylocname"));
    origApp.start(ImmutableList.of(origLoc));
    assertEquals(origE.getAttribute(Attributes.SERVICE_STATE_EXPECTED).getState(), Lifecycle.RUNNING);
    EntityAsserts.assertAttributeEqualsEventually(origE, Attributes.SERVICE_STATE_ACTUAL, Lifecycle.RUNNING);

    ServiceStateLogic.setExpectedState(origE, Lifecycle.ON_FIRE);
    EntityAsserts.assertAttributeEqualsEventually(origE, Attributes.SERVICE_STATE_ACTUAL, Lifecycle.ON_FIRE);

    newApp = rebind();
    MyService newE = (MyService) Iterables.getOnlyElement(newApp.getChildren());
    assertNull(newE.getDriver(), "driver should not be initialized because entity is in a permanent failure");
}
 
Example 5
Source File: ControlledDynamicWebAppClusterImpl.java    From brooklyn-library with Apache License 2.0 6 votes vote down vote up
@Override
public void stop() {
    ServiceStateLogic.setExpectedState(this, Lifecycle.STOPPING);

    try {
        List<Startable> tostop = Lists.newArrayList();
        if (this.equals(getController().getParent())) tostop.add(getController());
        tostop.add(getCluster());

        StartableMethods.stopSequentially(tostop);

        clearLocations();

        ServiceStateLogic.setExpectedState(this, Lifecycle.STOPPED);
    } catch (Exception e) {
        ServiceStateLogic.setExpectedState(this, Lifecycle.ON_FIRE);
        throw Exceptions.propagate(e);
    }
}
 
Example 6
Source File: MongoDBShardedDeploymentImpl.java    From brooklyn-library with Apache License 2.0 6 votes vote down vote up
@Override
public void start(Collection<? extends Location> locations) {
    ServiceStateLogic.setExpectedState(this, Lifecycle.STARTING);
    try {
        final MongoDBRouterCluster routers = getAttribute(ROUTER_CLUSTER);
        final MongoDBShardCluster shards = getAttribute(SHARD_CLUSTER);
        List<DynamicCluster> clusters = ImmutableList.of(getAttribute(CONFIG_SERVER_CLUSTER), routers, shards);
        Entities.invokeEffectorList(this, clusters, Startable.START, ImmutableMap.of("locations", locations))
                .get();

        if (config().getRaw(MongoDBShardedDeployment.CO_LOCATED_ROUTER_GROUP).isPresent()) {
            policies().add(PolicySpec.create(ColocatedRouterTrackingPolicy.class)
                    .displayName("Co-located router tracker")
                    .configure("group", getConfig(MongoDBShardedDeployment.CO_LOCATED_ROUTER_GROUP)));
        }

        ServiceNotUpLogic.clearNotUpIndicator(this, Attributes.SERVICE_STATE_ACTUAL);
        ServiceStateLogic.setExpectedState(this, Lifecycle.RUNNING);
    } catch (Exception e) {
        ServiceStateLogic.setExpectedState(this, Lifecycle.ON_FIRE);
        // no need to log here; the effector invocation should do that
        throw Exceptions.propagate(e);
    }
}
 
Example 7
Source File: RiakClusterImpl.java    From brooklyn-library with Apache License 2.0 6 votes vote down vote up
@Override
protected void doStart() {
    super.doStart();
    connectSensors();

    try {
        Duration delay = getConfig(DELAY_BEFORE_ADVERTISING_CLUSTER);
        Tasks.setBlockingDetails("Sleeping for "+delay+" before advertising cluster available");
        Time.sleep(delay);
    } finally {
        Tasks.resetBlockingDetails();
    }

    //FIXME: add a quorum to tolerate failed nodes before setting on fire.
    @SuppressWarnings("unchecked")
    Optional<Entity> anyNode = Iterables.tryFind(getMembers(), Predicates.and(
            Predicates.instanceOf(RiakNode.class),
            EntityPredicates.attributeEqualTo(RiakNode.RIAK_NODE_HAS_JOINED_CLUSTER, true),
            EntityPredicates.attributeEqualTo(RiakNode.SERVICE_UP, true)));
    if (anyNode.isPresent()) {
        sensors().set(IS_CLUSTER_INIT, true);
    } else {
        log.warn("No Riak Nodes are found on the cluster: {}. Initialization Failed", getId());
        ServiceStateLogic.setExpectedState(this, Lifecycle.ON_FIRE);
    }
}
 
Example 8
Source File: BasicStartableImpl.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Override
public void stop() {
    ServiceStateLogic.setExpectedState(this, Lifecycle.STOPPING);
    sensors().set(SERVICE_UP, false);
    try {
        StartableMethods.stop(this);
        ServiceStateLogic.setExpectedState(this, Lifecycle.STOPPED);
    } catch (Exception e) {
        ServiceStateLogic.setExpectedState(this, Lifecycle.ON_FIRE);
        throw Exceptions.propagate(e);
    }
}
 
Example 9
Source File: StubContainerImpl.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Override
public void start(Collection<? extends Location> locations) {
    ServiceStateLogic.setExpectedState(this, Lifecycle.STARTING);

    Map<String, ?> flags = MutableMap.copyOf(config().get(LOCATION_FLAGS));
    createLocation(flags);

    super.start(locations);

    ServiceStateLogic.setExpectedState(this, Lifecycle.RUNNING);
}
 
Example 10
Source File: TestEntityImpl.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Override
public void stop() { 
    LOG.trace("Stopping {}", this);
    callHistory.add("stop");
    ServiceStateLogic.setExpectedState(this, Lifecycle.STOPPING);
    counter.decrementAndGet();
    sensors().set(SERVICE_UP, false);
    ServiceStateLogic.setExpectedState(this, Lifecycle.STOPPED);
}
 
Example 11
Source File: GeoscalingDnsServiceImpl.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
@Override
public void onManagementStarted() {
    super.onManagementStarted();
    try {
        applyConfig();
    } catch (Exception e) {
        // don't prevent management coming up, but do mark it as on fire
        log.error("Geoscaling did not come up correctly: "+e, e);
        ServiceStateLogic.setExpectedState(this, Lifecycle.ON_FIRE);
    }
}
 
Example 12
Source File: OpenShiftEntityImpl.java    From SeaCloudsPlatform with Apache License 2.0 5 votes vote down vote up
@Override
public void onManagementStarting() {
    super.onManagementStarting();

    Lifecycle state = getAttribute(SERVICE_STATE_ACTUAL);
    if (state == null || state == Lifecycle.CREATED) {
        // Expect this is a normal start() sequence (i.e. start() will subsequently be called)
        setAttribute(SERVICE_UP, false);
        ServiceStateLogic.setExpectedState(this, Lifecycle.CREATED);
        // force actual to be created because this is expected subsequently
        setAttribute(SERVICE_STATE_ACTUAL, Lifecycle.CREATED);
    }
}
 
Example 13
Source File: TestJavaWebAppEntity.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
@Override
public void stop(ConfigBag parameters) {
    ServiceStateLogic.setExpectedState(entity(), Lifecycle.STOPPING);
    LOG.trace("Stopping {}", this);
    entity().sensors().set(Attributes.SERVICE_UP, false);
    entity().sensors().set(SERVICE_PROCESS_IS_RUNNING, false);
    ServiceStateLogic.setExpectedState(entity(), Lifecycle.STOPPED);
}
 
Example 14
Source File: ServiceFailureDetectorTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testNotifiedOfFailureOnProblem() throws Exception {
    e1.enrichers().add(EnricherSpec.create(ServiceFailureDetector.class));
    
    e1.sensors().set(TestEntity.SERVICE_UP, true);
    ServiceStateLogic.setExpectedState(e1, Lifecycle.RUNNING);
    
    assertEquals(events.size(), 0, "events="+events);
    
    ServiceProblemsLogic.updateProblemsIndicator(e1, "test", "foo");

    assertHasEventEventually(HASensors.ENTITY_FAILED, Predicates.<Object>equalTo(e1), null);
    assertEquals(events.size(), 1, "events="+events);
    EntityAsserts.assertAttributeEqualsEventually(e1, TestEntity.SERVICE_STATE_ACTUAL, Lifecycle.ON_FIRE);
}
 
Example 15
Source File: ServiceFailureDetectorTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testDisablingPreviouslyUpRequirementForEntityFailed() throws Exception {
    e1.enrichers().add(EnricherSpec.create(ServiceFailureDetector.class)
        .configure(ServiceFailureDetector.ENTITY_FAILED_ONLY_IF_PREVIOUSLY_UP, false));
    
    e1.sensors().set(TestEntity.SERVICE_UP, false);
    ServiceStateLogic.setExpectedState(e1, Lifecycle.RUNNING);

    EntityAsserts.assertAttributeEqualsEventually(e1, TestEntity.SERVICE_STATE_ACTUAL, Lifecycle.ON_FIRE);
    assertHasEventEventually(HASensors.ENTITY_FAILED, Predicates.<Object>equalTo(e1), null);
}
 
Example 16
Source File: DynamicClusterImpl.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Override
public void start(Collection<? extends Location> locsO) {
    addLocations(locsO);
    Location loc = getLocation(false);

    EntitySpec<?> spec = getConfig(MEMBER_SPEC);
    if (spec!=null) {
        setDefaultDisplayName("Cluster of "+JavaClassNames.simpleClassName(spec.getType()) +
            (loc!=null ? " ("+loc+")" : ""));
    }

    if (isAvailabilityZoneEnabled()) {
        if (loc==null) throw new IllegalStateException("When using availability zones, a location must be specified on the cluster");
        sensors().set(SUB_LOCATIONS, findSubLocations(loc));
    }

    ServiceStateLogic.setExpectedState(this, Lifecycle.STARTING);
    ServiceProblemsLogic.clearProblemsIndicator(this, START);
    try {
        doStart();
        DynamicTasks.waitForLast();
    } catch (Exception e) {
        ServiceProblemsLogic.updateProblemsIndicator(this, START, "start failed with error: "+e);
        ServiceStateLogic.setExpectedStateRunningWithErrors(this);
        throw Exceptions.propagate(e);
    }

    // Don't set problem-indicator if it's just our waitForServiceUp that fails;
    // we want to be able to recover if the indicator is subsequently cleared.
    try {
        waitForServiceUp();
    } finally {
        ServiceStateLogic.setExpectedState(this, Lifecycle.RUNNING);
    }
}
 
Example 17
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 18
Source File: EntityAsyncTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Override
public void onCallback(boolean success) {
    if (success) {
        ServiceStateLogic.ServiceNotUpLogic.clearNotUpIndicator(this, START.getName());
    } else {
        ServiceStateLogic.ServiceNotUpLogic.updateNotUpIndicator(this, START.getName(), "callback reported failure");
    }
    
    Transition expectedState = sensors().get(Attributes.SERVICE_STATE_EXPECTED);
    if (expectedState != null && expectedState.getState() == Lifecycle.STARTING) {
        ServiceStateLogic.setExpectedState(this, Lifecycle.RUNNING);
    }
}
 
Example 19
Source File: OpenShiftEntityImpl.java    From SeaCloudsPlatform with Apache License 2.0 4 votes vote down vote up
/**
 * Copied direcly from {@link brooklyn.entity.basic.SoftwareProcessImpl}
 */
// TODO Find a better way to detect early death of process.
public void waitForEntityStart() {
    if (log.isDebugEnabled()) {
        log.debug("waiting to ensure {} doesn't abort prematurely", this);
    }
    Duration startTimeout = getConfig(START_TIMEOUT);
    CountdownTimer timer = startTimeout.countdownTimer();
    boolean isRunningResult = false;
    long delay = 100;
    while (!isRunningResult && !timer.isExpired()) {
        Time.sleep(delay);
        try {
            isRunningResult = driver.isRunning();
        } catch (Exception e) {
            ServiceStateLogic.setExpectedState(this, Lifecycle.ON_FIRE);
            // provide extra context info, as we're seeing this happen in strange circumstances
            if (driver == null) {
                throw new IllegalStateException(this +
                        " concurrent start and shutdown detected");
            }
            throw new IllegalStateException("Error detecting whether " + this +
                    " is running: " + e, e);
        }
        if (log.isDebugEnabled()) {
            log.debug("checked {}, is running returned: {}", this, isRunningResult);
        }
        // slow exponential delay -- 1.1^N means after 40 tries and 50s elapsed, it reaches
        // the max of 5s intervals
        // TODO use Repeater
        delay = Math.min(delay * 11 / 10, 5000);
    }
    if (!isRunningResult) {
        String msg = "Software process entity " + this + " did not pass is-running " +
                "check within the required " + startTimeout + " limit (" +
                timer.getDurationElapsed().toStringRounded() + " elapsed)";
        log.warn(msg + " (throwing)");
        ServiceStateLogic.setExpectedState(this, Lifecycle.RUNNING);
        throw new IllegalStateException(msg);
    }
}
 
Example 20
Source File: CassandraFabricTest.java    From brooklyn-library with Apache License 2.0 4 votes vote down vote up
@Override
public void stop() {
    ServiceStateLogic.setExpectedState(this, Lifecycle.STOPPING);
}