类 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.
    • 字段详细资料

      • NO_PROC_ID

        public static final long NO_PROC_ID
        另请参阅:
        常量字段值
      • NO_TIMEOUT

        public static final long NO_TIMEOUT
        另请参阅:
        常量字段值
    • 构造器详细资料

      • Procedure

        public Procedure()
    • 方法详细资料

      • 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 later
        java.lang.InterruptedException - the procedure will be added back to the queue and retried later
        ProcedureException
      • 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)
        The doAcquireLock(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 call acquireLock(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.
      • 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 - ioe
        java.lang.InterruptedException - interrupted exception
        ProcedureException
      • doAcquireLock

        public final ProcedureLockState doAcquireLock​(Env env,
                                                      IProcedureStore store)
        Internal method called by the ProcedureExecutor that starts the user-level code acquireLock().
        参数:
        env - environment
        store - ProcedureStore
        返回:
        ProcedureLockState
      • doReleaseLock

        public final void doReleaseLock​(Env env,
                                        IProcedureStore store)
        Presist lock state of the procedure
        参数:
        env - environment
        store - 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 from toString() when interpolating Procedure State. Allows decorating generic Procedure State with Procedure particulars.
        参数:
        builder - Append current ProcedureState
      • 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 the doAcquireLock(Object, IProcedureStore) method, we will reset lockedWhenLoading to 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.
      • setFailure

        protected void setFailure​(java.lang.String source,
                                  java.lang.Throwable cause)
      • 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_TIMEOUT by calling setState method, and throw a ProcedureSuspendedException to halt the execution of the procedure, and do not forget a call setTimeout(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()
      • 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
      • compareTo

        public int compareTo​(Procedure<Env> other)
        指定者:
        compareTo 在接口中 java.lang.Comparable<Env>