Class StateMachineProcedure<Env,TState>

java.lang.Object
org.apache.iotdb.confignode.procedure.Procedure<Env>
org.apache.iotdb.confignode.procedure.impl.StateMachineProcedure<Env,TState>
All Implemented Interfaces:
Comparable<Procedure<Env>>
Direct Known Subclasses:
AbstractNodeProcedure, AddNeverFinishSubProcedureProcedure, AlterLogicalViewProcedure, CreateManyDatabasesProcedure, CreatePipeProcedure, CreateRegionGroupsProcedure, DeactivateTemplateProcedure, DeleteDatabaseProcedure, DeleteLogicalViewProcedure, DeleteTimeSeriesProcedure, DropPipeProcedure, NeverFinishProcedure, RegionOperationProcedure, SetTemplateProcedure, SetTTLProcedure, StartPipeProcedure, StopPipeProcedure, UnsetTemplateProcedure

public abstract class StateMachineProcedure<Env,TState> extends Procedure<Env>
Procedure described by a series of steps.

The procedure implementor must have an enum of 'states', describing the various step of the procedure. Once the procedure is running, the procedure-framework will call executeFromState() using the 'state' provided by the user. The first call to executeFromState() will be performed with 'state = null'. The implementor can jump between states using setNextState(MyStateEnum.ordinal()). The rollback will call rollbackState() for each state that was executed, in reverse order.

  • Field Details

    • isGeneratedByPipe

      protected boolean isGeneratedByPipe
      Mark whether this procedure is called by a pipe forwarded request.
  • Constructor Details

    • StateMachineProcedure

      protected StateMachineProcedure()
    • StateMachineProcedure

      protected StateMachineProcedure(boolean isGeneratedByPipe)
  • Method Details

    • getCycles

      protected final int getCycles()
    • executeFromState

      protected abstract StateMachineProcedure.Flow executeFromState(Env env, TState state) throws InterruptedException
      Called to perform a single step of the specified 'state' of the procedure.
      Parameters:
      state - state to execute
      Returns:
      Flow.NO_MORE_STATE if the procedure is completed, Flow.HAS_MORE_STATE if there is another step.
      Throws:
      InterruptedException
    • rollbackState

      protected abstract void rollbackState(Env env, TState state) throws IOException, InterruptedException, ProcedureException
      Called to perform the rollback of the specified state.
      Parameters:
      state - state to rollback
      Throws:
      IOException - temporary failure, the rollback will retry later
      InterruptedException
      ProcedureException
    • getState

      protected abstract TState getState(int stateId)
      Convert an ordinal (or state id) to an Enum (or more descriptive) state object.
      Parameters:
      stateId - the ordinal() of the state enum (or state id)
      Returns:
      the state enum object
    • getStateId

      protected abstract int getStateId(TState state)
      Convert the Enum (or more descriptive) state object to an ordinal (or state id).
      Parameters:
      state - the state enum object
      Returns:
      stateId the ordinal() of the state enum (or state id)
    • getInitialState

      protected abstract TState getInitialState()
      Return the initial state object that will be used for the first call to executeFromState().
      Returns:
      the initial state enum object
    • setNextState

      protected void setNextState(TState state)
      Set the next state for the procedure.
      Parameters:
      state - the state enum object
    • addChildProcedure

      protected void addChildProcedure(Procedure<Env> childProcedure)
      Add a child procedure to execute.
      Parameters:
      childProcedure - the child procedure
    • execute

      protected Procedure<Env>[] execute(Env env) throws InterruptedException
      Description copied from class: Procedure
      The main code of the procedure. It must be idempotent since execute() may be called multiple times in case of machine failure in the middle of the execution.
      Specified by:
      execute in class Procedure<Env>
      Parameters:
      env - the environment passed to the ProcedureExecutor
      Returns:
      a set of sub-procedures to run or ourselves if there is more work to do or null if the procedure is done.
      Throws:
      InterruptedException - the procedure will be added back to the queue and retried later.
    • rollback

      protected void rollback(Env env) throws IOException, InterruptedException, ProcedureException
      Description copied from class: Procedure
      The code to undo what was done by the execute() code. It is called when the procedure or one of the sub-procedures failed or an abort was requested. It should cleanup all the resources created by the execute() call. The implementation must be idempotent since rollback() may be called multiple time in case of machine failure in the middle of the execution.
      Specified by:
      rollback in class Procedure<Env>
      Parameters:
      env - the environment passed to the ProcedureExecutor
      Throws:
      IOException - temporary failure, the rollback will retry later
      InterruptedException - the procedure will be added back to the queue and retried later
      ProcedureException
    • isEofState

      protected boolean isEofState()
    • isRollbackSupported

      protected boolean isRollbackSupported(TState state)
      Used by the default implementation of abort() to know if the current state can be aborted and rollback can be triggered.
    • getCurrentState

      protected TState getCurrentState()
    • toStringState

      protected void toStringState(StringBuilder builder)
      Description copied from class: Procedure
      Called from Procedure.toString() when interpolating Procedure State. Allows decorating generic Procedure State with Procedure particulars.
      Overrides:
      toStringState in class Procedure<Env>
      Parameters:
      builder - Append current ProcedureState
    • serialize

      public void serialize(DataOutputStream stream) throws IOException
      Overrides:
      serialize in class Procedure<Env>
      Throws:
      IOException
    • deserialize

      public void deserialize(ByteBuffer byteBuffer)
      Overrides:
      deserialize in class Procedure<Env>
    • isStateDeserialized

      public boolean isStateDeserialized()
      The isStateDeserialized indicates whether the current stage of this procedure was generated by deserialization. If true, this means the procedure has undergone a leader switch or a restart recovery at this stage. After the procedure is recovered, you may not want to re-execute all the code in this stage, which is the purpose of this variable.