Class Procedure<Env>
java.lang.Object
org.apache.iotdb.confignode.procedure.Procedure<Env>
- Type Parameters:
Env-
- All Implemented Interfaces:
Comparable<Procedure<Env>>
- Direct Known Subclasses:
InternalProcedure,StateMachineProcedure
Abstract class of all procedures.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected ProcedureLockStateacquireLock(Env env) Acquire a lock, user should override it if necessary.protected voidaddStackIndex(int index) Called by the RootProcedureState on procedure execution.intvoiddeserialize(ByteBuffer byteBuffer) static voiddeserializeTypeInfoForCompatibility(ByteBuffer byteBuffer) Deprecated.final ProcedureLockStatedoAcquireLock(Env env, IProcedureStore store) Internal method called by the ProcedureExecutor that starts the user-level code acquireLock().Internal method called by the ProcedureExecutor that starts the user-level code execute().final 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().longThe main code of the procedure.protected intlonglonglongbyte[]longprotected int[]getState()longlongprotected longTimeout of the next timeout.protected booleanbooleanfinal booleanhasLock()booleanbooleanstatic booleanhaveSameParent(Procedure<?> a, Procedure<?> b) protected booleanUsed to keep procedure lock even when the procedure is yielded or suspended.protected voidCalled by the ProcedureExecutor on procedure-load to restore the latch statebooleanisFailed()booleanbooleanbooleanCan only be called when restarting, before the procedure actually being executed, as after we actually call thedoAcquireLock(Object, IProcedureStore)method, we will resetlockedWhenLoadingto false.booleanbooleanbooleanprotected booleanisYieldAfterExecution(Env env) To make executor yield between each execution step to give other procedures a chance to run.protected voidreleaseLock(Env env) Release a lock, user should override it if necessary.protected booleanfinal voidrestoreLock(Env env) protected abstract voidThe code to undo what was done by the execute() code.voidserialize(DataOutputStream stream) protected voidsetChildrenLatch(int numChildren) Called by the ProcedureExecutor on procedure-load to restore the latch stateprotected voidsetFailure(String source, 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.voidsetProcId(long procId) Called by the ProcedureExecutor to assign the ID to the newly created procedure.voidprotected voidsetResult(byte[] result) The procedure may leave a "result" on completion.voidsetRootProcedureId(long rootProcedureId) protected voidsetRootProcId(long rootProcId) protected voidsetStackIndexes(List<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.toString()protected voidtoStringClassDetails(StringBuilder builder) Extend the toString() information with the procedure details e.g.Extend the toString() information with more procedure detailsprotected StringBuilderBuild the StringBuilder for the simple form of procedure string.protected voidtoStringState(StringBuilder builder) Called fromtoString()when interpolatingProcedureState.protected voidupdateMetricsOnFinish(Env env, long runtime, boolean success) This function will be called just after procedure execution is finished.protected voidupdateMetricsOnSubmit(Env env) This function will be called just when procedure is submitted for execution.protected voidCalled by ProcedureExecutor after each time a procedure step is executed.protected boolean
-
Field Details
-
NO_PROC_ID
public static final long NO_PROC_ID- See Also:
-
NO_TIMEOUT
public static final long NO_TIMEOUT- See Also:
-
-
Constructor Details
-
Procedure
public Procedure()
-
-
Method Details
-
hasLock
public final boolean hasLock() -
execute
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.- 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 abstract void rollback(Env env) throws IOException, 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.- 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
-
serialize
- Throws:
IOException
-
deserialize
-
deserializeTypeInfoForCompatibility
Deprecated.Deserialize class Name and load class- Parameters:
byteBuffer- bytebuffer
-
acquireLock
Acquire a lock, user should override it if necessary.- Parameters:
env- environment- Returns:
- state of lock
-
releaseLock
Release a lock, user should override it if necessary.- Parameters:
env- env
-
holdLock
Used to keep procedure lock even when the procedure is yielded or suspended.- Parameters:
env- env- Returns:
- true if hold the lock
-
isYieldAfterExecution
To make executor yield between each execution step to give other procedures a chance to run.- Parameters:
env- environment- Returns:
- return true if yield is allowed.
-
doExecute
Internal method called by the ProcedureExecutor that starts the user-level code execute().- Parameters:
env- execute environment- Returns:
- sub procedures
- Throws:
InterruptedException
-
doRollback
Internal method called by the ProcedureExecutor that starts the user-level code rollback().- Parameters:
env- execute environment- Throws:
IOException- ioeInterruptedException- interrupted exceptionProcedureException
-
doAcquireLock
Internal method called by the ProcedureExecutor that starts the user-level code acquireLock().- Parameters:
env- environmentstore- ProcedureStore- Returns:
- ProcedureLockState
-
doReleaseLock
Presist lock state of the procedure- Parameters:
env- environmentstore- ProcedureStore
-
restoreLock
-
toString
-
toStringSimpleSB
Build the StringBuilder for the simple form of procedure string.- Returns:
- the StringBuilder
-
toStringDetails
Extend the toString() information with more procedure details -
toStringState
Called fromtoString()when interpolatingProcedureState. Allows decorating generic Procedure State with Procedure particulars.- Parameters:
builder- Append currentProcedureState
-
toStringClassDetails
Extend the toString() information with the procedure details e.g. className and parameters- 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() -
getProcType
-
getSubmittedTime
public long getSubmittedTime() -
setProcId
public 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) - Parameters:
timeout- timeout interval in msec
-
hasTimeout
public boolean hasTimeout() -
getTimeout
public long getTimeout()- Returns:
- 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.- Returns:
- the timestamp of the next timeout.
-
elapsedTime
public long elapsedTime()- Returns:
- the time elapsed between the last update and the start time of the procedure.
-
getResult
public byte[] getResult()- Returns:
- the serialized result if any, otherwise null
-
setResult
protected void setResult(byte[] result) The procedure may leave a "result" on completion.- Parameters:
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()- Returns:
- true if the procedure is in a RUNNABLE state.
-
isInitializing
public boolean isInitializing() -
isFailed
public boolean isFailed()- Returns:
- true if the procedure has failed. It may or may not have rolled back.
-
isSuccess
public boolean isSuccess()- Returns:
- true if the procedure is finished successfully.
-
isFinished
public boolean isFinished()- Returns:
- true if the procedure is finished. The Procedure may be completed successfully or rolledback.
-
isWaiting
public boolean isWaiting()- Returns:
- true if the procedure is waiting for a child to finish or for an external event.
-
setState
-
getState
-
setFailure
-
setFailure
-
setTimeoutFailure
Called by the ProcedureExecutor when the timeout set by setTimeout() is expired.- Returns:
- true to let the framework handle the timeout as abort, false in case the procedure handled the timeout itself.
-
hasException
public boolean hasException() -
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
Called on store load to initialize the Procedure internals after the creation/deserialization. -
wasExecuted
protected boolean wasExecuted() -
getStackIndexes
protected int[] getStackIndexes() -
setRootProcedureId
public void setRootProcedureId(long rootProcedureId) -
haveSameParent
- Parameters:
a- the first procedure to be compared.b- the second procedure to be compared.- Returns:
- true if the two procedures have the same parent
-
compareTo
- Specified by:
compareToin interfaceComparable<Env>
-
updateMetricsOnSubmit
This function will be called just when procedure is submitted for execution.- Parameters:
env- The environment passed to the procedure executor
-
updateMetricsOnFinish
This function will be called just after procedure execution is finished. Override this method to update metrics at the end of the procedure. The default implementation adds runtime of a procedure to a time histogram for successfully completed procedures. Increments failed counter for failed procedures.- Parameters:
env- The environment passed to the procedure executorruntime- Runtime of the procedure in millisecondssuccess- true if procedure is completed successfully
-