All Products
Search
Document Center

Function Compute:Configure instance lifecycles

Last Updated:Dec 02, 2024

Function Compute automatically allocates one or more instances to process user requests. Each instance provides a secure and isolated runtime environment. Function Compute uses a serverless architecture, in which function instances are created and destroyed in a transient manner. Therefore, traditional applications that are migrated to Function Compute may encounter issues such as untimely update of monitoring data and delay or loss of metric data. To address these challenges, Function Compute offers lifecycle-based hooks for function instances to ensure the real-time accuracy and integrity of monitoring data.

Function instance lifecycle

Instances of a function are dynamically created and destroyed based on requests sent to the function. The lifecycle of each function instance includes the Creating, Invoke, and Destroy phases. The following figure shows the lifecycle of a function instance.

image

Creating

Function Compute creates instances based on function configurations. During this phase, Function Compute performs the following tasks in sequence:

  1. Instance Create: Function Compute loads code and layers, and pulls images to create an instance.

  2. Runtime Init: Function Compute initializes the runtime.

  3. Init Hook: Function Compute executes the configured Initializer hook. For more information, see Initializer hooks.

The following figure shows the phases.

image

Instances are created in the following scenarios:

  • Elastic scale-out

    Function Compute creates new instances to process received requests if the existing instances are fully loaded. The created instances enter the Invoke phase after they are created. Elastic scale-out may cause a cold start. For more information about how to mitigate cold starts, see Best practice for reducing cold start latencies.

  • Change of provisioned instance configurations

    Function Compute immediately starts the instance creation process after you configure provision instances. The instances enter the Invoke phase only when requests are received. For more information, see Configure provisioned instances.

For a function instance, the configured Initialize hook is executed only once. If the execution fails, another function instance is allocated to execute the Initializer hook. Instances whose Initializer hooks fail are destroyed.

Invoke

Function Compute uses function handlers to process internal and external requests. Each instance that runs in a standard runtime of Function Compute executes only one request at a time. For instances that run in custom runtimes and Custom Container runtimes, each instance can process multiple requests at a time. You can use the instance concurrency feature to allow one instance to execute multiple requests at a time. For more information, see Configure instance concurrency.

Destroy

This phase is triggered if a function instance does not receive any invocations for a period of time. During this phase, Function Compute executes the PreStop hook before it destroys an instance. You can perform cleanup tasks in PreStop hooks.

Instances are destroyed in the following scenarios:

  • Unoccupied instances: If an on-demand instance does not receive requests for a period of time, Function Compute automatically reclaims the instance.

  • Adjustment of provisioned instance configurations: Function Compute destroys excess provisioned instances if you reduce the number of provisioned instances in the provisioned instance configuration.

  • Instance exception: Function Compute destroys an instance if the instance encounters an exception during the creation or running process.

Instance freezing mechanism

Function Compute freezes an instance when no invocations are initiated for a period of time and unfreezes the instance when new requests are submitted. The following figure shows the process.

image

An instance is frozen in the following scenarios:

  • The instance is initialized and no invocations are made yet.

  • The instance is invoked to process requests and subsequent requests are not immediately submitted.

After invocations are complete, Function Compute freezes the function instances. In this case, the background processes, threads, and coroutines in the program cannot continue to run, and asynchronous logs may fail to write.

Limits

  • All runtimes support the Initializer and PreStop hooks.

  • The input parameter of a PreStop hook does not have the event parameter.

  • The PreStop hook have no return values. The logic that is appended to the PreStop hook to return values does not take effect.

  • If you use a Java runtime, you must update fc-java-core to 1.4.0 or later. Otherwise, you cannot use the PreStop hook.

  • When a function returns responses, Function Compute freezes the function instance. You cannot assume that all asynchronous processes, threads, and coroutines are successfully executed when responses are returned. You cannot assume that asynchronous logs are refreshed either.

Billing

You are not charged for the number of invocations for instance lifecycle hooks. Other billable items are the same as regular invocations. The following figure shows the billable phases. For more information about billing, see Billing overview.

image

Prerequisites

A function is created. For more information, see Create a function.

Configure lifecycle hooks

Configure hooks in the Function Compute console

The PreStop hook cannot be configured for a function when the function is created in the Function Compute console. You can configure a PreStop hook for the function when you update the function.

  1. Log on to the Function Compute console. In the left-side navigation pane, click Functions.

  2. In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.

  3. On the function details page, choose Configurations >. Lifecycle > Modify.

  4. In the Lifecycle panel, configure hooks and timeout periods, and then click Deploy.

    image.png

    Note

    You must configure a timeout period for each hook. The format of a hook is [File name].[Hook name]. For example, if you set the PreStop hook to index.preStop when you create a function in a Python runtime, the file name is index.py and the name of the PreStop hook is preStop.

  5. After you configure a hook, you must implement the hook in the function code.

    1. Click the Code tab. In the code editor, enter the code of the hook.

      For example, if you set the PreStop hook to index.preStop, you must implement the preStop hook in the code. For more information about how to implement lifecycle hooks in different runtimes of Function Compute, see Lifecycle hooks for function instances in different runtimes.

      Note

      The online IDE is supported by PHP, Python, Node.js, and custom runtimes but not supported by runtimes of compiled languages such as Java, Go, .NET, or Custom Container runtimes.

    2. Click Deploy in the upper part of the code editor, and then click Test Function.

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

To disable a hook, leave the handler parameter of the hook empty. Otherwise, the function is not updated. For example, if you want to disable the PreStop hook, you must configure the following parameters to update the deployment. In this case, the timeout parameter of the PreStop hook does not take effect.

  codeUri: './code.zip'
  ......
  instanceLifecycleConfig:
    preStop:
      handler: ""
      timeout: 60

For more information about how to implement lifecycle hooks in different runtimes of Function Compute, see Lifecycle hooks for function instances in different runtimes.

Use SDKs to configure hooks

You can use SDKs to deploy and update hooks. This section describes how to obtain the SDK sample code that is used to configure a preStop hook when you create a function.

  1. On the CreateFunction page, click Debug to go to the OpenAPI portal.

  2. On the Parameters tab, configure Request Parameters based on the basic information of the function.

    image

  3. After you configure the parameters, click the SDK Sample Code tab to obtain the SDK sample code in the required programming language.

For more information about how to implement lifecycle hooks in different runtimes of Function Compute, see Lifecycle hooks for function instances in different runtimes.

Lifecycle hooks for function instances in different runtimes

All runtimes in Function Compute support Init and PreStop hooks. The following table describes the methods to implement lifecycle hooks of instances in different runtimes.

Runtime

Description

References

Node.js

Implement lifecycle hooks for instances in Node.js runtimes.

Lifecycle hooks for function instances

Python

Implement lifecycle hooks for instances in Python runtimes.

Lifecycle hooks for function instances

PHP

Implement lifecycle hooks for instances in PHP runtimes.

Lifecycle hooks for function instances

Java

Implement lifecycle hooks for instances in Java runtimes.

Lifecycle hooks for function instances

C#

Implement lifecycle hooks for instances in C# runtimes.

Lifecycle hooks for function instances

Go

Implement lifecycle hooks for instances in Go runtimes.

Lifecycle hooks for function instances

Custom Runtime

Implement lifecycle hooks for instances in custom runtimes.

Lifecycle hooks for function instances

Custom Container

Implement lifecycle hooks for instances in Custom Container runtimes.

Lifecycle hooks for function instances

Query hook-related logs

After you configure an instance lifecycle hook and run the code of the hook, you can query the logs of the hook.

  1. Log on to the Function Compute console. In the left-side navigation pane, click Functions.

  2. In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.

  3. On the function details page, 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.db_lifecycle_log

    You can also query the request logs based on the request ID in the start logs and the end logs. If the user log does not contain a request ID, you can click the db_lifecycle_log_context icon to obtain the context log.