All Products
Search
Document Center

Function Compute:Function instance lifecycles

Last Updated:Feb 02, 2024

Function Compute invokes your functions in instances, which provide secure and isolated runtime environments. Function Compute provides hooks based on lifecycles of instances. Hooks can be used to address pain points such as metric data delay or loss when you migrate traditional applications to serverless architectures. This topic describes lifecycles of function instances, how to configure the 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 perform the following tasks in sequence:

    • Create an instance.

    • Initialize the runtime.

    • Run the 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 before runtime initialization, Init hook running, and invocations.

    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 failed 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 execute multiple requests at a time. You can configure a value greater than 1 to the instance concurrency to allow one instance to execute multiple requests at a time.

  • 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.Function Compute 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 is not enabled are not frozen when they are not processing requests.

Limits

  • All runtimes support Init and PreStop hooks.

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

  • A 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.

Prerequisites

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

Configure lifecycle hooks

Note

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

Configure hooks in the Function Compute console

When you use the Function Compute console to create a function, you cannot configure PreStop hooks. 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 Configurations tab. In the left-side navigation pane, click Lifecycle. Then, click Edit.

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

    image.png

    Note

    You must configure a hook and timeout period for each extension function. The format of a hook is [File name].[Extension function 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 function is preStop.

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

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

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

      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.

Lifecycle hooks for function instances

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

Runtime

Description

Reference

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 logs related to hooks

After you configure an instance lifecycle hook and execute 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 Requests tab, find the request that you want to use and click Advanced Logs in the Actions column.

    You can use the 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.