当前位置:网站首页>Decryption function calculates "task state and lifecycle management" of asynchronous task capability

Decryption function calculates "task state and lifecycle management" of asynchronous task capability

2022-07-05 04:28:00 Alibaba cloud native

author : Gradually meaning

Preface

There is a very important concept in the task system , That is, the state of the task and the life management cycle . Its essence is the life cycle management of tasks . The subdivided state helps to understand what happened to the system more clearly when using , It is convenient to operate according to the business situation . Function calculation Serverless Task Provides a variety of queriable statuses , And provide the time point of transition between States . At the level of function execution , Functional computing also provides the concept of task lifecycle management , Users can decide when the system initializes the task instance according to their needs 、 The execution logic of a series of actions such as recycling , Achieve complete runtime lifecycle management . This paper will introduce the two aspects of task running state and runtime management .

Management of task status and life cycle

Task status management

 Insert picture description here

When the user submits the task and receives the return of successful submission , The task has entered the management process of the system for its life cycle . The state change of a task is managed by an internal state machine , It also supports real-time query of external disclosure status . The whole state transition diagram is shown below :

 Insert picture description here

chart 1

  1. After the user triggers a task , Task in the team , Changed to: Enqueued state , And return trigger success ;
  2. The task is queued in the back-end service of function calculation , The task becomes Dequeued state ;
  3. The backend will check the task configuration . If
    1. The asynchronous message validity function is configured , And the difference between the time of leaving the team and the time of joining the team has exceeded the validity period , Then the task is discarded , Changed to: Expired state . Task to terminate ;
    2. The function corresponding to the task has been deleted , Or there is an error creating the instance , Then discard the message , The task is changed to Invalid state ;
  1. After the inspection, the task officially enters Running state . At this time, the task has triggered the actual execution ;
  2. When the task is completed , According to the return, it will change to the following states :
    1. Retrying: The user has configured the number of retries ( The default is 3), And the task execution failed , At this time, it will enter the retry state , Then it will be changed to Running state ;
    2. Failed: Task execution failed , And the number of retries has exceeded . The task status will be changed to Failed;
    3. Succeeded: The task was executed successfully .
  1. If the user triggers during the whole state flow Cancel, Then the task will be changed to Stopping state , And try to stop the task . When the task stops successfully , Mission entry Stopped state .

Task runtime management and life cycle

When the task status enters Running after , The actual execution of the task has been handed over to the runtime of function calculation . In terms of security , Function calculation will follow VM Isolate different accounts , Functions under the same account may run in the same VM in .VM There is a client in charge of managing containers , To actually trigger the execution of the function , And collect the execution results .

The running instance of the user has several different states :

 Insert picture description here

chart 2

Function calculation provides interfaces for the process of state changes of all the above instances , Support user side configuration of corresponding logic .

  1. The creation is complete -> Execute request phase : Support Initializer function , Support initializing instance operations . Users can configure such as global variables 、 Connection pool initialization and other related operations ;
  2. In execution -> After execution Pause example : Support PreFreeze Interface , Support in function Pause Execute user-defined logic before the instance ;
  3. In execution -> external cancel: Function evaluation will force Restart User instance , stay Restart Pre support PreStop Interface . The user can configure the logic of graceful stop , To support Cancel Custom behavior when ;
  4. After completion Pause -> Destroy instance : When there is no request for a period of time , Function calculation will destroy the instance . This will call PreStop Interface , Users can configure the behavior of destroying containers ( Such as closing the connection pool ).

 Insert picture description here

chart 3

Stop the task

At present, function calculation supports the operation of stopping a single task . When the user operation stops , The supporting configuration PreStop Interface , Carry out a series of resource recovery work before stopping . Stop the operation, and the user can use SDK Or console . With Go Language as an example , The pseudocode that stops execution once is shown below :


import fc "github.com/aliyun/fc-go-sdk"

func CancelJob() {
    stopInput := fc.NewStopStatefulAsyncInvocationInput("ServiceName", "FunctionName", "TaskUUID")
    output, err := fcClient.StopStatefulAsyncInvocation(stopInput)
    ...
}

summary

Serverless Task Provides status details for each task , These details will be persisted in real time . Users can query these status information in real time according to their needs , And carry out corresponding operations according to the implementation and business conditions . In the running phase of the task , Function calculation provides relevant interfaces in the process of state transition of all instances , Support user-defined logic before and after task execution . combination PreStop Function and Cancel Stop task function , Users can easily realize the elegant operation of tasks .

Looking back

Asynchronous task processing system , How to solve the problem of long and time-consuming business 、 High and difficult questions ?

Function calculation asynchronous task capability introduction - Task trigger de duplication


1 minute Serverless Build real websites at top speed

 Insert picture description here

Use Serverless 1 Minutes to easily build your first personal website !

Free Quota , Pick up ! Xiaobai can also set up a station at a high speed : No need to consider the server and website source code , We provide you with free computing resources , Operation and maintenance management server . Complete the scene experience during the activity , I have the chance to get Tmall supermarket 10 Yuan voucher .( Suggest pc The experience )

Activity time : 2022 year 6 month 20 Japan -7 month 1 Japan ( Collect during working days )

Q & a group : Nail search “44700570”

Experience address : Click to read the original text or Scan QR code

 Insert picture description here

Click on here , Direct experience !

原网站

版权声明
本文为[Alibaba cloud native]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050420458555.html