Function Compute automatically allocates one or more function instances to process user requests. Each instance runs in a secure and isolated runtime. Function Compute adopts a serverless architecture, in which instances are created and destroyed in a transient manner. As a result, traditional applications that are migrated to Function Compute may encounter issues such as untimely updates to monitoring data and delay or loss of metric data. To address these challenges, Function Compute offers lifecycle hooks for function instances to ensure real-time data accuracy and integrity.
Function instance lifecycle
Instances are dynamically created and destroyed based on incoming requests. The lifecycle of each instance comprises three phases: Creating, Invoke, and Destroy. The following figure shows the lifecycle of a function instance.
Creating
Function Compute creates instances based on your function configurations. During this phase, Function Compute performs the following tasks in sequence:
Instance Create: Function Compute loads code and layers, or pulls images, and then initializes an instance.
Runtime Init: Function Compute initializes the runtime.
Init Hook: Function Compute executes the configured Initializer hook. For more information, see Initializer hooks.
The following figure shows the Creating phase.
The Creating phase is triggered in the following scenarios:
Auto scaling
Function Compute creates new instances to process incoming requests if the existing instances are fully loaded. Once the Creating phase is finished, the new instances enter the Invoke phase. Auto scaling can lead to a cold start. For more information about how to mitigate cold starts, see Best practice for reducing cold start latencies.
Provisioned instance adjustment
Function Compute starts the Creating process immediately after you configure provisioned instances. The instances enter the Invoke phase only when they receive incoming requests. For more information, see Configure provisioned instances.
Function Compute ensures that the Initializer hook is executed only once throughout an instance's lifecycle. If the execution fails, the current instance is destroyed, and a new instance is created to retry the execution.
Invoke
Handlers act as entry points for your function instances to process internal and external requests. An instance that runs in a built-in runtime of Function Compute processes one request at a time, whereas an instance that runs in a custom runtime or Custom Container runtime can process concurrent requests. To allow an instance to process multiple requests at a time, you can set the Instance Concurrency parameter to a value greater than 1 when you create a function. For more information, see Create a web function.
You are charged only when your instances process requests and execute hooks. Instances that are not actively in use are frozen and thus do not generate any costs. For more information, see Billing.Function Compute
Destroy
If a function instance does not receive any invocations for a certain period of time, the Destroy phase is triggered. During this phase, Function Compute executes the PreStop hook first, which is typically used to perform cleanup activities.
Instances are destroyed in the following scenarios:
Idle instances: If an instance does not receive any invocations for a certain period of time, Function Compute automatically reclaims the instance.
Provisioned instance adjustment: When you decrease the number of provisioned instances, Function Compute immediately destroys any excess instances.
Instance exceptions: If an instance encounters an exception in the Creating or Invoke phase, Function Compute destroys the instance.
Instance freezing mechanism
Function Compute freezes instances that are not invoked by any requests, and unfreezes, or thaws, them when they receive requests.Function Compute The following figure shows the freezing mechanism.
The freezing mechanism is triggered in the following scenarios:
The instance is initialized but not yet invoked by any requests.
The instance completes processing its last request and is not receiving any new ones.
Function Compute freezes an instance after the Invoke phase. During the freeze period, all background processes, threads, and coroutines in the program cease to run, and asynchronous logs may fail to write.
Limits
All runtimes support the Initializer and PreStop hooks.
The input parameters of a PreStop hook do not contain an event parameter.
The PreStop hook does not support returning values, and any logic added to the hook to return values is ineffective.
If you use a Java runtime, you must update fc-java-core to 1.4.0 or later to utilize the PreStop hook functionality.
When a function returns responses, Function Compute freezes the function instance. At this point, you cannot assume that all asynchronous processes, threads, and coroutines are successfully executed, nor can you expect the asynchronous logs to be updated.
Billing
You are not charged for the number of times the lifecycle hooks are executed. All billable items involved during lifecycle hook invocations are billed in the same manner as regular invocations. The following figure shows the billing durations. For more information about billing, see Billing overview.
Prerequisite
Function creation is completed. For more information, see Create a function.
Configure lifecycle hooks
Configure hooks in the Function Compute console
You cannot configure a PreStop hook for your function when you create the function in the Function Compute console. You must configure the PreStop hook when you update the function.
On the Function Details page of your function, choose Configurations > Lifecycle > Modify.
In the Lifecycle panel, configure the hooks and timeout periods, and then click Deploy.
NoteThe format of a hook is [File name].[Hook name]. You must configure a timeout period for each hook. For example, when you configure the PreStop hook for a function in a Python runtime, if the file name is index.py and the hook name is preStop, set the PreStop Hook parameter to index.preStop.
After you configure a hook, you must implement the hook in the function code.
On the Function Details page of your function, choose Code. In the code editor, enter the code of the hook.
For example, after you set the PreStop Hook parameter to
index.preStop
, you must implement the preStop hook in your code. For more information about how to implement lifecycle hooks in different runtimes, see Lifecycle hooks for function instances in different runtimes.NoteThe online IDE supports PHP, Python, Node.js, and custom runtimes. However, it does not support compiled languages like Java, Go, and .NET, nor does it support Custom Container runtimes.
Click Deploy above the code editor, and then click Test Function.
Log on to the Function Compute console. In the left-side navigation pane, click Functions.
In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.
Configure hooks by using Serverless Devs
The following code snippet shows an example of the s.yaml
file if you use Serverless Devs to configure a PreStop hook:
codeUri: './code.zip'
......
instanceLifecycleConfig:
preStop:
handler: index.PreStop
timeout: 60
If you want to disable a hook, leave the handler
parameter of the hook empty. Otherwise, the hook remains active. For example, to disable a PreStop hook, you can run the following commands. Note that the timeout
parameter of the hook is considered invalid.
codeUri: './code.zip'
......
instanceLifecycleConfig:
preStop:
handler: ""
timeout: 60
For more information about how to implement lifecycle hooks in different runtimes, see Lifecycle hooks for function instances in different runtimes.
Configure hooks by using SDKs
You can use SDKs to deploy and update hooks. This section describes how to obtain the SDK code sample required by PreStop hook configuration during function creation.
On the CreateFunction page, click Debug to go to the OpenAPI portal.
On the Parameters tab, configure Request Parameters based on the basic information of the function.
After you configure the parameters, click the SDK Sample Code tab and choose the required language to obtain the corresponding sample code.
For more information about how to implement lifecycle hooks in different runtimes, see Lifecycle hooks for function instances in different runtimes.
Lifecycle hooks for function instances in different runtimes
All runtimes in Function Compute support both Initializer and PreStop hooks. The following table describes the methods to implement lifecycle hooks in different runtimes.
Runtime | Description | Reference |
Node.js | Implement lifecycle hooks for instances in Node.js runtimes. | |
Python | Implement lifecycle hooks for instances in Python runtimes. | |
PHP | Implement lifecycle hooks for instances in PHP runtimes. | |
Java | Implement lifecycle hooks for instances in Java runtimes. | |
C# | Implement lifecycle hooks for instances in C# runtimes. | |
Go | Implement lifecycle hooks for instances in Go runtimes. | |
Custom runtimes | Implement lifecycle hooks for instances in custom runtimes. | |
Custom Container runtimes | Implement lifecycle hooks for instances in Custom Container runtimes. | Lifecycle hooks for function instances in Custom Container runtimes |
Query hook-related logs
After you configure an instance lifecycle hook and run its code, you can query the logs related to the hook.
On the Function Details page of your function, click the Logs tab. On the Invocation Requests tab, find the request whose logs you want to view and click Advanced Logs in the Actions column.
You can use a copied instance ID to query the start and end logs of all lifecycle hooks. You can also use the instance ID together with a hook keyword to query the start and end logs of a specified hook, such as
c-62833f38-20f1629801fa4bd***** and PreStop
.You can also query the request logs based on the request ID in the start and end logs. If the user log does not contain a request ID, you can click the
icon to obtain the context log.
Log on to the Function Compute console. In the left-side navigation pane, click Functions.
In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.