Search in sources :

Example 1 with State

use of com.google_voltpatches.common.util.concurrent.Service.State in project voltdb by VoltDB.

the class AbstractService method notifyFailed.

/**
   * Invoke this method to transition the service to the {@link State#FAILED}. The service will
   * <b>not be stopped</b> if it is running. Invoke this method when a service has failed critically
   * or otherwise cannot be started nor stopped.
   */
protected final void notifyFailed(Throwable cause) {
    checkNotNull(cause);
    monitor.enter();
    try {
        State previous = state();
        switch(previous) {
            case NEW:
            case TERMINATED:
                throw new IllegalStateException("Failed while in state:" + previous, cause);
            case RUNNING:
            case STARTING:
            case STOPPING:
                snapshot = new StateSnapshot(FAILED, false, cause);
                failed(previous, cause);
                break;
            case FAILED:
                // Do nothing
                break;
            default:
                throw new AssertionError("Unexpected state: " + previous);
        }
    } finally {
        monitor.leave();
        executeListeners();
    }
}
Also used : Preconditions.checkState(com.google_voltpatches.common.base.Preconditions.checkState) State(com.google_voltpatches.common.util.concurrent.Service.State)

Example 2 with State

use of com.google_voltpatches.common.util.concurrent.Service.State in project voltdb by VoltDB.

the class AbstractService method notifyStopped.

/**
   * Implementing classes should invoke this method once their service has stopped. It will cause
   * the service to transition from {@link State#STOPPING} to {@link State#TERMINATED}.
   *
   * @throws IllegalStateException if the service is neither {@link State#STOPPING} nor {@link
   *     State#RUNNING}.
   */
protected final void notifyStopped() {
    monitor.enter();
    try {
        // We check the internal state of the snapshot instead of state() directly so we don't allow
        // notifyStopped() to be called while STARTING, even if stop() has already been called.
        State previous = snapshot.state;
        if (previous != STOPPING && previous != RUNNING) {
            IllegalStateException failure = new IllegalStateException("Cannot notifyStopped() when the service is " + previous);
            notifyFailed(failure);
            throw failure;
        }
        snapshot = new StateSnapshot(TERMINATED);
        terminated(previous);
    } finally {
        monitor.leave();
        executeListeners();
    }
}
Also used : Preconditions.checkState(com.google_voltpatches.common.base.Preconditions.checkState) State(com.google_voltpatches.common.util.concurrent.Service.State)

Example 3 with State

use of com.google_voltpatches.common.util.concurrent.Service.State in project voltdb by VoltDB.

the class AbstractService method stopAsync.

@CanIgnoreReturnValue
@Override
public final Service stopAsync() {
    if (monitor.enterIf(isStoppable)) {
        try {
            State previous = state();
            switch(previous) {
                case NEW:
                    snapshot = new StateSnapshot(TERMINATED);
                    terminated(NEW);
                    break;
                case STARTING:
                    snapshot = new StateSnapshot(STARTING, true, null);
                    stopping(STARTING);
                    break;
                case RUNNING:
                    snapshot = new StateSnapshot(STOPPING);
                    stopping(RUNNING);
                    doStop();
                    break;
                case STOPPING:
                case TERMINATED:
                case FAILED:
                    // These cases are impossible due to the if statement above.
                    throw new AssertionError("isStoppable is incorrectly implemented, saw: " + previous);
                default:
                    throw new AssertionError("Unexpected state: " + previous);
            }
        } catch (Throwable shutdownFailure) {
            notifyFailed(shutdownFailure);
        } finally {
            monitor.leave();
            executeListeners();
        }
    }
    return this;
}
Also used : Preconditions.checkState(com.google_voltpatches.common.base.Preconditions.checkState) State(com.google_voltpatches.common.util.concurrent.Service.State) CanIgnoreReturnValue(com.google_voltpatches.errorprone.annotations.CanIgnoreReturnValue)

Example 4 with State

use of com.google_voltpatches.common.util.concurrent.Service.State in project voltdb by VoltDB.

the class ServiceManager method startAsync.

/**
   * Initiates service {@linkplain Service#startAsync startup} on all the services being managed. It
   * is only valid to call this method if all of the services are {@linkplain State#NEW new}.
   *
   * @return this
   * @throws IllegalStateException if any of the Services are not {@link State#NEW new} when the
   *     method is called.
   */
@CanIgnoreReturnValue
public ServiceManager startAsync() {
    for (Service service : services) {
        State state = service.state();
        checkState(state == NEW, "Service %s is %s, cannot start it.", service, state);
    }
    for (Service service : services) {
        try {
            state.tryStartTiming(service);
            service.startAsync();
        } catch (IllegalStateException e) {
            // This can happen if the service has already been started or stopped (e.g. by another
            // service or listener). Our contract says it is safe to call this method if
            // all services were NEW when it was called, and this has already been verified above, so we
            // don't propagate the exception.
            logger.log(Level.WARNING, "Unable to start Service " + service, e);
        }
    }
    return this;
}
Also used : Preconditions.checkState(com.google_voltpatches.common.base.Preconditions.checkState) State(com.google_voltpatches.common.util.concurrent.Service.State) CanIgnoreReturnValue(com.google_voltpatches.errorprone.annotations.CanIgnoreReturnValue)

Aggregations

Preconditions.checkState (com.google_voltpatches.common.base.Preconditions.checkState)4 State (com.google_voltpatches.common.util.concurrent.Service.State)4 CanIgnoreReturnValue (com.google_voltpatches.errorprone.annotations.CanIgnoreReturnValue)2