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
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.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanMark whether this procedure is called by a pipe forwarded request.Fields inherited from class org.apache.iotdb.confignode.procedure.Procedure
NO_PROC_ID, NO_TIMEOUT -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedprotectedStateMachineProcedure(boolean isGeneratedByPipe) -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddChildProcedure(Procedure<Env> childProcedure) Add a child procedure to execute.voiddeserialize(ByteBuffer byteBuffer) The main code of the procedure.protected abstract StateMachineProcedure.FlowexecuteFromState(Env env, TState state) Called to perform a single step of the specified 'state' of the procedure.protected TStateprotected final intprotected abstract TStateReturn the initial state object that will be used for the first call to executeFromState().protected abstract TStategetState(int stateId) Convert an ordinal (or state id) to an Enum (or more descriptive) state object.protected abstract intgetStateId(TState state) Convert the Enum (or more descriptive) state object to an ordinal (or state id).protected booleanprotected booleanisRollbackSupported(TState state) Used by the default implementation of abort() to know if the current state can be aborted and rollback can be triggered.booleanThe isStateDeserialized indicates whether the current stage of this procedure was generated by deserialization.protected voidThe code to undo what was done by the execute() code.protected abstract voidrollbackState(Env env, TState state) Called to perform the rollback of the specified state.voidserialize(DataOutputStream stream) protected voidsetNextState(TState state) Set the next state for the procedure.protected voidtoStringState(StringBuilder builder) Called fromProcedure.toString()when interpolatingProcedureState.Methods inherited from class org.apache.iotdb.confignode.procedure.Procedure
acquireLock, addStackIndex, compareTo, deserializeTypeInfoForCompatibility, doAcquireLock, doExecute, doReleaseLock, doRollback, elapsedTime, getChildrenLatch, getException, getLastUpdate, getParentProcId, getProcId, getProcType, getResult, getRootProcId, getStackIndexes, getState, getSubmittedTime, getTimeout, getTimeoutTimestamp, hasChildren, hasException, hasLock, hasParent, hasTimeout, haveSameParent, holdLock, incChildrenLatch, isFailed, isFinished, isInitializing, isLockedWhenLoading, isRunnable, isSuccess, isWaiting, isYieldAfterExecution, releaseLock, removeStackIndex, restoreLock, setChildrenLatch, setFailure, setFailure, setLastUpdate, setParentProcId, setProcId, setProcRunnable, setResult, setRootProcedureId, setRootProcId, setStackIndexes, setState, setSubmittedTime, setTimeout, setTimeoutFailure, toString, toStringClassDetails, toStringDetails, toStringSimpleSB, updateMetricsOnFinish, updateMetricsOnSubmit, updateTimestamp, wasExecuted
-
Field Details
-
isGeneratedByPipe
protected boolean isGeneratedByPipeMark 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 laterInterruptedExceptionProcedureException
-
getState
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
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
Return the initial state object that will be used for the first call to executeFromState().- Returns:
- the initial state enum object
-
setNextState
Set the next state for the procedure.- Parameters:
state- the state enum object
-
addChildProcedure
Add a child procedure to execute.- Parameters:
childProcedure- the child procedure
-
execute
Description copied from class:ProcedureThe 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:
executein classProcedure<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
Description copied from class:ProcedureThe 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:
rollbackin classProcedure<Env>- Parameters:
env- the environment passed to the ProcedureExecutor- Throws:
IOException- temporary failure, the rollback will retry laterInterruptedException- the procedure will be added back to the queue and retried laterProcedureException
-
isEofState
protected boolean isEofState() -
isRollbackSupported
Used by the default implementation of abort() to know if the current state can be aborted and rollback can be triggered. -
getCurrentState
-
toStringState
Description copied from class:ProcedureCalled fromProcedure.toString()when interpolatingProcedureState. Allows decorating generic Procedure State with Procedure particulars.- Overrides:
toStringStatein classProcedure<Env>- Parameters:
builder- Append currentProcedureState
-
serialize
- Overrides:
serializein classProcedure<Env>- Throws:
IOException
-
deserialize
- Overrides:
deserializein classProcedure<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.
-