类 Procedure<Env>
- java.lang.Object
-
- org.apache.iotdb.confignode.procedure.Procedure<Env>
-
- 类型参数:
Env-
- 所有已实现的接口:
java.lang.Comparable<Procedure<Env>>
- 直接已知子类:
InternalProcedure,StateMachineProcedure
public abstract class Procedure<Env> extends java.lang.Object implements java.lang.Comparable<Procedure<Env>>
Abstract class of all procedures.
-
-
字段概要
字段 修饰符和类型 字段 说明 static longNO_PROC_IDstatic longNO_TIMEOUT
-
构造器概要
构造器 构造器 说明 Procedure()
-
方法概要
所有方法 静态方法 实例方法 抽象方法 具体方法 修饰符和类型 方法 说明 protected abstract booleanabort(Env env)The abort() call is asynchronous and each procedure must decide how to deal with it, if they want to be abortable.protected ProcedureLockStateacquireLock(Env env)Acquire a lock, user should override it if necessary.protected voidaddStackIndex(int index)Called by the RootProcedureState on procedure execution.protected voidafterRecover(Env env)Called when the procedure is recovered and added into the queue.protected voidbeforeRecover(Env env)Called before the procedure is recovered and added into the queue.intcompareTo(Procedure<Env> other)protected voidcompletionCleanup(Env env)Called when the procedure is completed (success or rollback).voiddeserialize(java.nio.ByteBuffer byteBuffer)static java.lang.Class<?>deserializeTypeInfo(java.nio.ByteBuffer byteBuffer)Deserialize class Name and load classProcedureLockStatedoAcquireLock(Env env, IProcedureStore store)Internal method called by the ProcedureExecutor that starts the user-level code acquireLock().protected Procedure<Env>[]doExecute(Env env)Internal method called by the ProcedureExecutor that starts the user-level code execute().voiddoReleaseLock(Env env, IProcedureStore store)Presist lock state of the procedurevoiddoRollback(Env env)Internal method called by the ProcedureExecutor that starts the user-level code rollback().longelapsedTime()protected abstract Procedure<Env>[]execute(Env env)The main code of the procedure.protected intgetChildrenLatch()ProcedureExceptiongetException()longgetLastUpdate()longgetParentProcId()longgetProcId()java.lang.StringgetProcName()byte[]getResult()protected static longgetRootProcedureId(java.util.Map<java.lang.Long,Procedure> procedures, Procedure proc)Helper to lookup the root Procedure ID given a specified procedure.longgetRootProcId()protected int[]getStackIndexes()ProcedureStategetState()longgetSubmittedTime()longgetTimeout()protected longgetTimeoutTimestamp()Timeout of the next timeout.protected booleanhasChildren()booleanhasException()booleanhasLock()booleanhasParent()booleanhasTimeout()static booleanhaveSameParent(Procedure<?> a, Procedure<?> b)protected booleanholdLock(Env env)Used to keep procedure lock even when the procedure is yielded or suspended.protected voidincChildrenLatch()Called by the ProcedureExecutor on procedure-load to restore the latch statebooleanisFailed()booleanisFinished()booleanisInitializing()booleanisLockedWhenLoading()Can only be called when restarting, before the procedure actually being executed, as after we actually call thedoAcquireLock(Object, IProcedureStore)method, we will resetlockedWhenLoadingto false.booleanisRunnable()booleanisSuccess()booleanisWaiting()protected booleanisYieldAfterExecution(Env env)To make executor yield between each execution step to give other procedures a chance to run.booleanneedPersistance()static Procedure<?>newInstance(java.nio.ByteBuffer byteBuffer)protected voidreleaseLock(Env env)Release a lock, user should override it if necessary.protected booleanremoveStackIndex()voidresetPersistance()voidrestoreLock(Env env)protected abstract voidrollback(Env env)The code to undo what was done by the execute() code.voidserialize(java.io.DataOutputStream stream)protected voidsetAbortFailure(java.lang.String source, java.lang.String msg)protected voidsetChildrenLatch(int numChildren)Called by the ProcedureExecutor on procedure-load to restore the latch stateprotected voidsetFailure(java.lang.String source, java.lang.Throwable cause)protected voidsetFailure(ProcedureException exception)protected voidsetLastUpdate(long lastUpdate)Called on store load to initialize the Procedure internals after the creation/deserialization.protected voidsetParentProcId(long parentProcId)Called by the ProcedureExecutor to assign the parent to the newly created procedure.protected voidsetProcId(long procId)Called by the ProcedureExecutor to assign the ID to the newly created procedure.voidsetProcRunnable()protected voidsetResult(byte[] result)The procedure may leave a "result" on completion.voidsetRootProcedureId(long rootProcedureId)protected voidsetRootProcId(long rootProcId)protected voidsetStackIndexes(java.util.List<java.lang.Integer> stackIndexes)Called on store load to initialize the Procedure internals after the creation/deserialization.protected voidsetState(ProcedureState state)protected voidsetSubmittedTime(long submittedTime)Called on store load to initialize the Procedure internals after the creation/deserialization.protected voidsetTimeout(long timeout)protected booleansetTimeoutFailure(Env env)Called by the ProcedureExecutor when the timeout set by setTimeout() is expired.voidskipPersistance()java.lang.StringtoString()protected java.lang.StringtoStringClass()protected voidtoStringClassDetails(java.lang.StringBuilder builder)Extend the toString() information with the procedure details e.g. className and parametersjava.lang.StringtoStringDetails()Extend the toString() information with more procedure detailsprotected java.lang.StringBuildertoStringSimpleSB()Build the StringBuilder for the simple form of procedure string.protected voidtoStringState(java.lang.StringBuilder builder)Called fromtoString()when interpolatingProcedureState.protected voidupdateTimestamp()Called by ProcedureExecutor after each time a procedure step is executed.protected booleanwaitInitialized(Env env)ThedoAcquireLock(Object, IProcedureStore)will be split into two steps, first, it will call us to determine whether we need to wait for initialization, second, it will callacquireLock(Object)to actually handle the lock for this procedure.protected booleanwasExecuted()
-
-
-
方法详细资料
-
needPersistance
public boolean needPersistance()
-
resetPersistance
public void resetPersistance()
-
skipPersistance
public final void skipPersistance()
-
hasLock
public final boolean hasLock()
-
execute
protected abstract Procedure<Env>[] execute(Env env) throws ProcedureYieldException, ProcedureSuspendedException, java.lang.InterruptedException
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.- 参数:
env- the environment passed to the ProcedureExecutor- 返回:
- a set of sub-procedures to run or ourselves if there is more work to do or null if the procedure is done.
- 抛出:
ProcedureYieldException- the procedure will be added back to the queue and retried later.java.lang.InterruptedException- the procedure will be added back to the queue and retried later.ProcedureSuspendedException- Signal to the executor that Procedure has suspended itself and has set itself up waiting for an external event to wake it back up again.
-
rollback
protected abstract void rollback(Env env) throws java.io.IOException, java.lang.InterruptedException, ProcedureException
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.- 参数:
env- the environment passed to the ProcedureExecutor- 抛出:
java.io.IOException- temporary failure, the rollback will retry laterjava.lang.InterruptedException- the procedure will be added back to the queue and retried laterProcedureException
-
abort
protected abstract boolean abort(Env env)
The abort() call is asynchronous and each procedure must decide how to deal with it, if they want to be abortable. The simplest implementation is to have an AtomicBoolean set in the abort() method and then the execute() will check if the abort flag is set or not. abort() may be called multiple times from the client, so the implementation must be idempotent.NOTE: abort() is not like Thread.interrupt(). It is just a notification that allows the procedure implementor abort.
-
serialize
public void serialize(java.io.DataOutputStream stream) throws java.io.IOException- 抛出:
java.io.IOException
-
deserialize
public void deserialize(java.nio.ByteBuffer byteBuffer)
-
deserializeTypeInfo
public static java.lang.Class<?> deserializeTypeInfo(java.nio.ByteBuffer byteBuffer)
Deserialize class Name and load class- 参数:
byteBuffer- bytebuffer- 返回:
- Procedure
-
newInstance
public static Procedure<?> newInstance(java.nio.ByteBuffer byteBuffer)
-
waitInitialized
protected boolean waitInitialized(Env env)
ThedoAcquireLock(Object, IProcedureStore)will be split into two steps, first, it will call us to determine whether we need to wait for initialization, second, it will callacquireLock(Object)to actually handle the lock for this procedure.- 返回:
- true means we need to wait until the environment has been initialized, otherwise true.
-
acquireLock
protected ProcedureLockState acquireLock(Env env)
Acquire a lock, user should override it if necessary.- 参数:
env- environment- 返回:
- state of lock
-
releaseLock
protected void releaseLock(Env env)
Release a lock, user should override it if necessary.- 参数:
env- env
-
holdLock
protected boolean holdLock(Env env)
Used to keep procedure lock even when the procedure is yielded or suspended.- 参数:
env- env- 返回:
- true if hold the lock
-
beforeRecover
protected final void beforeRecover(Env env)
Called before the procedure is recovered and added into the queue.- 参数:
env- environment
-
afterRecover
protected final void afterRecover(Env env)
Called when the procedure is recovered and added into the queue.- 参数:
env- environment
-
completionCleanup
protected void completionCleanup(Env env)
Called when the procedure is completed (success or rollback). The procedure may use this method to clean up in-memory states. This operation will not be retried on failure.- 参数:
env- environment
-
isYieldAfterExecution
protected boolean isYieldAfterExecution(Env env)
To make executor yield between each execution step to give other procedures a chance to run.- 参数:
env- environment- 返回:
- return true if yield is allowed.
-
doExecute
protected Procedure<Env>[] doExecute(Env env) throws ProcedureYieldException, ProcedureSuspendedException, java.lang.InterruptedException
Internal method called by the ProcedureExecutor that starts the user-level code execute().- 参数:
env- execute environment- 返回:
- sub procedures
- 抛出:
ProcedureYieldExceptionProcedureSuspendedExceptionjava.lang.InterruptedException
-
doRollback
public void doRollback(Env env) throws java.io.IOException, java.lang.InterruptedException, ProcedureException
Internal method called by the ProcedureExecutor that starts the user-level code rollback().- 参数:
env- execute environment- 抛出:
java.io.IOException- ioejava.lang.InterruptedException- interrupted exceptionProcedureException
-
doAcquireLock
public final ProcedureLockState doAcquireLock(Env env, IProcedureStore store)
Internal method called by the ProcedureExecutor that starts the user-level code acquireLock().- 参数:
env- environmentstore- ProcedureStore- 返回:
- ProcedureLockState
-
doReleaseLock
public final void doReleaseLock(Env env, IProcedureStore store)
Presist lock state of the procedure- 参数:
env- environmentstore- ProcedureStore
-
restoreLock
public final void restoreLock(Env env)
-
toString
public java.lang.String toString()
- 覆盖:
toString在类中java.lang.Object
-
toStringSimpleSB
protected java.lang.StringBuilder toStringSimpleSB()
Build the StringBuilder for the simple form of procedure string.- 返回:
- the StringBuilder
-
toStringDetails
public java.lang.String toStringDetails()
Extend the toString() information with more procedure details
-
toStringClass
protected java.lang.String toStringClass()
-
toStringState
protected void toStringState(java.lang.StringBuilder builder)
Called fromtoString()when interpolatingProcedureState. Allows decorating generic Procedure State with Procedure particulars.- 参数:
builder- Append currentProcedureState
-
toStringClassDetails
protected void toStringClassDetails(java.lang.StringBuilder builder)
Extend the toString() information with the procedure details e.g. className and parameters- 参数:
builder- the string builder to use to append the proc specific information
-
getProcId
public long getProcId()
-
hasParent
public boolean hasParent()
-
getParentProcId
public long getParentProcId()
-
getRootProcId
public long getRootProcId()
-
getProcName
public java.lang.String getProcName()
-
getSubmittedTime
public long getSubmittedTime()
-
setProcId
protected void setProcId(long procId)
Called by the ProcedureExecutor to assign the ID to the newly created procedure.
-
setProcRunnable
public void setProcRunnable()
-
setParentProcId
protected void setParentProcId(long parentProcId)
Called by the ProcedureExecutor to assign the parent to the newly created procedure.
-
setRootProcId
protected void setRootProcId(long rootProcId)
-
setSubmittedTime
protected void setSubmittedTime(long submittedTime)
Called on store load to initialize the Procedure internals after the creation/deserialization.
-
setTimeout
protected void setTimeout(long timeout)
- 参数:
timeout- timeout interval in msec
-
hasTimeout
public boolean hasTimeout()
-
getTimeout
public long getTimeout()
- 返回:
- the timeout in msec
-
setLastUpdate
protected void setLastUpdate(long lastUpdate)
Called on store load to initialize the Procedure internals after the creation/deserialization.
-
updateTimestamp
protected void updateTimestamp()
Called by ProcedureExecutor after each time a procedure step is executed.
-
getLastUpdate
public long getLastUpdate()
-
getTimeoutTimestamp
protected long getTimeoutTimestamp()
Timeout of the next timeout. Called by the ProcedureExecutor if the procedure has timeout set and the procedure is in the waiting queue.- 返回:
- the timestamp of the next timeout.
-
elapsedTime
public long elapsedTime()
- 返回:
- the time elapsed between the last update and the start time of the procedure.
-
getResult
public byte[] getResult()
- 返回:
- the serialized result if any, otherwise null
-
setResult
protected void setResult(byte[] result)
The procedure may leave a "result" on completion.- 参数:
result- the serialized result that will be passed to the client
-
isLockedWhenLoading
public boolean isLockedWhenLoading()
Can only be called when restarting, before the procedure actually being executed, as after we actually call thedoAcquireLock(Object, IProcedureStore)method, we will resetlockedWhenLoadingto false.Now it is only used in the ProcedureScheduler to determine whether we should put a Procedure in front of a queue.
-
isRunnable
public boolean isRunnable()
- 返回:
- true if the procedure is in a RUNNABLE state.
-
isInitializing
public boolean isInitializing()
-
isFailed
public boolean isFailed()
- 返回:
- true if the procedure has failed. It may or may not have rolled back.
-
isSuccess
public boolean isSuccess()
- 返回:
- true if the procedure is finished successfully.
-
isFinished
public boolean isFinished()
- 返回:
- true if the procedure is finished. The Procedure may be completed successfully or rolledback.
-
isWaiting
public boolean isWaiting()
- 返回:
- true if the procedure is waiting for a child to finish or for an external event.
-
setState
protected void setState(ProcedureState state)
-
getState
public ProcedureState getState()
-
setFailure
protected void setFailure(java.lang.String source, java.lang.Throwable cause)
-
setFailure
protected void setFailure(ProcedureException exception)
-
setAbortFailure
protected void setAbortFailure(java.lang.String source, java.lang.String msg)
-
setTimeoutFailure
protected boolean setTimeoutFailure(Env env)
Called by the ProcedureExecutor when the timeout set by setTimeout() is expired.Another usage for this method is to implement retrying. A procedure can set the state to
WAITING_TIMEOUTby callingsetStatemethod, and throw aProcedureSuspendedExceptionto halt the execution of the procedure, and do not forget a callsetTimeout(long)method to set the timeout. And you should also override this method to wake up the procedure, and also return false to tell the ProcedureExecutor that the timeout event has been handled.- 返回:
- true to let the framework handle the timeout as abort, false in case the procedure handled the timeout itself.
-
hasException
public boolean hasException()
-
getException
public ProcedureException getException()
-
setChildrenLatch
protected void setChildrenLatch(int numChildren)
Called by the ProcedureExecutor on procedure-load to restore the latch state
-
incChildrenLatch
protected void incChildrenLatch()
Called by the ProcedureExecutor on procedure-load to restore the latch state
-
hasChildren
protected boolean hasChildren()
-
getChildrenLatch
protected int getChildrenLatch()
-
addStackIndex
protected void addStackIndex(int index)
Called by the RootProcedureState on procedure execution. Each procedure store its stack-index positions.
-
removeStackIndex
protected boolean removeStackIndex()
-
setStackIndexes
protected void setStackIndexes(java.util.List<java.lang.Integer> stackIndexes)
Called on store load to initialize the Procedure internals after the creation/deserialization.
-
wasExecuted
protected boolean wasExecuted()
-
getStackIndexes
protected int[] getStackIndexes()
-
getRootProcedureId
protected static long getRootProcedureId(java.util.Map<java.lang.Long,Procedure> procedures, Procedure proc)
Helper to lookup the root Procedure ID given a specified procedure.
-
setRootProcedureId
public void setRootProcedureId(long rootProcedureId)
-
haveSameParent
public static boolean haveSameParent(Procedure<?> a, Procedure<?> b)
- 参数:
a- the first procedure to be compared.b- the second procedure to be compared.- 返回:
- true if the two procedures have the same parent
-
-