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

public abstract class Procedure<Env> extends Object implements Comparable<Procedure<Env>>
Abstract class of all procedures.
  • Field Details

  • Constructor Details

    • Procedure

      public Procedure()
  • Method Details

    • hasLock

      public final boolean hasLock()
    • execute

      protected abstract Procedure<Env>[] execute(Env env) throws 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.
      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 later
      InterruptedException - the procedure will be added back to the queue and retried later
      ProcedureException
    • serialize

      public void serialize(DataOutputStream stream) throws IOException
      Throws:
      IOException
    • deserialize

      public void deserialize(ByteBuffer byteBuffer)
    • deserializeTypeInfoForCompatibility

      @Deprecated public static void deserializeTypeInfoForCompatibility(ByteBuffer byteBuffer)
      Deprecated.
      Deserialize class Name and load class
      Parameters:
      byteBuffer - bytebuffer
    • acquireLock

      protected ProcedureLockState acquireLock(Env env)
      Acquire a lock, user should override it if necessary.
      Parameters:
      env - environment
      Returns:
      state of lock
    • releaseLock

      protected void releaseLock(Env env)
      Release a lock, user should override it if necessary.
      Parameters:
      env - env
    • holdLock

      protected boolean holdLock(Env env)
      Used to keep procedure lock even when the procedure is yielded or suspended.
      Parameters:
      env - env
      Returns:
      true if hold the lock
    • isYieldAfterExecution

      protected boolean isYieldAfterExecution(Env env)
      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

      protected Procedure<Env>[] doExecute(Env env) throws InterruptedException
      Internal method called by the ProcedureExecutor that starts the user-level code execute().
      Parameters:
      env - execute environment
      Returns:
      sub procedures
      Throws:
      InterruptedException
    • doRollback

      public void doRollback(Env env) throws IOException, InterruptedException, ProcedureException
      Internal method called by the ProcedureExecutor that starts the user-level code rollback().
      Parameters:
      env - execute environment
      Throws:
      IOException - ioe
      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().
      Parameters:
      env - environment
      store - ProcedureStore
      Returns:
      ProcedureLockState
    • doReleaseLock

      public final void doReleaseLock(Env env, IProcedureStore store)
      Presist lock state of the procedure
      Parameters:
      env - environment
      store - ProcedureStore
    • restoreLock

      public final void restoreLock(Env env)
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toStringSimpleSB

      protected StringBuilder toStringSimpleSB()
      Build the StringBuilder for the simple form of procedure string.
      Returns:
      the StringBuilder
    • toStringDetails

      public String toStringDetails()
      Extend the toString() information with more procedure details
    • toStringState

      protected void toStringState(StringBuilder builder)
      Called from toString() when interpolating Procedure State. Allows decorating generic Procedure State with Procedure particulars.
      Parameters:
      builder - Append current ProcedureState
    • toStringClassDetails

      protected void toStringClassDetails(StringBuilder builder)
      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

      public String 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 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()
      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

      protected void setState(ProcedureState state)
    • getState

      public ProcedureState getState()
    • setFailure

      protected void setFailure(String source, Throwable cause)
    • setFailure

      protected void setFailure(ProcedureException exception)
    • setTimeoutFailure

      protected boolean setTimeoutFailure(Env env)
      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

      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(List<Integer> stackIndexes)
      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

      public static boolean haveSameParent(Procedure<?> a, Procedure<?> b)
      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

      public int compareTo(Procedure<Env> other)
      Specified by:
      compareTo in interface Comparable<Env>
    • updateMetricsOnSubmit

      protected void updateMetricsOnSubmit(Env env)
      This function will be called just when procedure is submitted for execution.
      Parameters:
      env - The environment passed to the procedure executor
    • updateMetricsOnFinish

      protected void updateMetricsOnFinish(Env env, long runtime, boolean success)
      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 executor
      runtime - Runtime of the procedure in milliseconds
      success - true if procedure is completed successfully