apache/hadoop · error · ServiceStateException

${name}: for this operation, the current service state must

Error message

${name}: for this operation, the current service state must be ${expectedState} instead of ${state}

What it means

ServiceStateModel.ensureCurrentState(expected) is a hard state assertion used by Service operations that are only legal in one lifecycle state; if the model's current state differs it throws ServiceStateException "<name>: for this operation, the current service state must be <expected> instead of <state>". Unlike enterState it never attempts a transition - it is a precondition check, so hitting it means the call was made at the wrong point in the lifecycle.

Source

Thrown at hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/service/ServiceStateModel.java:99

  /**
   * Query that the state is in a specific state
   * @param proposed proposed new state
   * @return the state
   */
  public boolean isInState(Service.STATE proposed) {
    return state.equals(proposed);
  }

  /**
   * Verify that that a service is in a given state.
   * @param expectedState the desired state
   * @throws ServiceStateException if the service state is different from
   * the desired state
   */
  public void ensureCurrentState(Service.STATE expectedState) {
    if (state != expectedState) {
      throw new ServiceStateException(name+ ": for this operation, the " +
                                      "current service state must be "
                                      + expectedState
                                      + " instead of " + state);
    }
  }

  /**
   * Enter a state -thread safe.
   *
   * @param proposed proposed new state
   * @return the original state
   * @throws ServiceStateException if the transition is not permitted
   */
  public synchronized Service.STATE enterState(Service.STATE proposed) {
    checkStateTransition(name, state, proposed);
    Service.STATE oldState = state;
    //atomic write of the new state
    state = proposed;

View on GitHub (pinned to 2add963021)

Solutions

  1. Guard the call: check service.isInState(expected) (or ServiceStateException-safe APIs) before invoking the state-guarded operation.
  2. Fix the ordering: complete the prior transition (e.g. stop()) before invoking stop-only operations.
  3. In custom services, prefer enterState()/getFailureState-style APIs which tolerate legal transitions instead of asserting.

Example fix

// before
model.ensureCurrentState(Service.STATE.STOPPED);
// after
if (service.isInState(Service.STATE.STOPPED)) {
  model.ensureCurrentState(Service.STATE.STOPPED);
  // ... stop-only work
}
Defensive patterns

Strategy: validation

Validate before calling

// Narrow before state-guarded operations
if (!service.isInState(Service.STATE.STOPPED)) {
  throw new IllegalStateException("post-mortem read requires STOPPED, state="
      + service.getServiceState());
}
// proceed; ensureCurrentState(STOPPED) will now pass

Type guard

boolean isStopped(Service s) {
  return s != null && s.isInState(Service.STATE.STOPPED);
}

Try / catch

try {
  model.ensureCurrentState(expected);
} catch (ServiceStateException e) {
  // read e.getMessage() for actual vs expected state; re-order lifecycle and retry
}

Prevention

When it happens

Trigger: Invoking a state-guarded operation on a service in another state: reading post-mortem state after STOPPED on a still-RUNNING instance, direct ServiceStateModel.ensureCurrentState calls in custom services, or assertions in lifecycle callbacks invoked at unexpected times.

Common situations: Custom AbstractService subclasses calling ensureCurrentState in serviceStop/serviceInit without guarding; tests asserting on services whose async stop has not completed; composite services touching children mid-transition.

Related errors


AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22). Data as JSON: /api/errors/106ece26b10b321a. Report an issue: GitHub.