All Products
Search
Document Center

Function Compute:Configure instance lifecycles

Last Updated:Jun 24, 2024

Functions of Function Compute are invoked in the form of instances, which provide secure and isolated runtime environments. Function Compute provides instance lifecycle hooks, which can be used to address pain points such as delay and loss of metric data if you migrate traditional applications to serverless architectures. This topic describes lifecycles of function instances, how to configure lifecycle hooks for function instances, and how to query logs of hooks.

Instance lifecycles

image.png

As shown in the preceding figure, the lifecycle of a Function Compute instance include the Create, Invoke, and Destroy phases.

  • Create

    During this phase, Function Compute performs the following tasks in sequence:

    • Create an instance.

    • Initialize a runtime.

    • Execute an Init hook.

    If you use provisioned instances, Function Compute allocates function instances and performs the Create process immediately after you configure provisioned instances. Function invocations may not be initiated right away. Therefore, a long time interval may exist between the runtime initialization, Init hook running, and invocation phases.

    If you use on-demand instances, Function Compute immediately performs the Create process when requests are submitted. If an Init hook is configured for the function, the Invoke process is performed after the Init hook is executed.

    For a function instance, the Init hook configured for the function is executed only once. If the execution fails, another function instance is allocated to execute the Init process. The instance whose Init hook fails is destroyed.

  • Invoke

    By default, instances that run in standard runtimes of Function Compute can execute only one request at a time. Instances that run in custom runtimes and Custom Container runtimes can execute multiple requests at a time. You can set the instance concurrency to a value greater than one 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 first. You can perform cleanup tasks in PreStop hooks.

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.png

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.

Note

Provisioned instances for which the idle mode feature is not enabled are not frozen when they are not processing requests.

Limits

  • All runtimes support the Init and PreStop hooks.

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

  • PreStop hooks have no return values. The logic that is appended to a 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.

Prerequisites

A function is created. For more information, see Create an event function.

Configure lifecycle hooks

Note

The billing for PreStop calls is the same as that for InvokeFunction calls. For more information, see Billing rules.

Configure hooks in the Function Compute console

When you create a function in the Function Compute console, you cannot configure a PreStop hook. You can configure a PreStop hook 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, click the Configuration tab. In the left-side navigation pane, click Lifecycle. Then, click Deploy.

  4. In the lifecycle panel, configure lifecycle hooks and timeout periods and click OK.

    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 code execution.

    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. 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 by default. For example, if you disable a 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 PreFreeze 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.

    You can configure the PreStop hook in the instanceLifecycleConfig field.

    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 the 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 runtimes

Implement lifecycle hooks for instances in custom runtimes.

Lifecycle hooks for function instances

Custom Container runtimes

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.

Billing rules

You are not charged for the number of invocations for instance lifecycle hooks. The billing of other billable items is the same those in the invocation phase. The following figure shows the billable phases.

image.png

For more information about billing, see Billing overview.