What Is Initializer?
Introduction
An initializer is a logic entry for initializing functions. For a function with an initializer, FunctionGraph invokes the initializer to initialize the function and then invokes the handler to process function requests. For a function without an initializer, FunctionGraph only invokes the handler to process function requests.
Application Scenarios
FunctionGraph executes a function in the following steps:
- Allocate container resources to the function.
- Download the function code.
- Use the runtime to load the function code.
- Initialize the function.
- Process the function request and return the result.
Steps 1, 2, and 3 are performed during a systematic cold start, ensuring a stable latency through proper resource scheduling and process optimization. Step 4 is performed during an application-layer cold start in complex scenarios, such as loading large models for deep learning, building database connection pools, and loading function dependencies.
To reduce the latency caused by an application-layer cold start, FunctionGraph provides the initializer to identify function initialization logic for proper resource scheduling.
Benefits of the Initializer
- Function initialization and request processing are isolated to enable clearer program logic and structure better and higher-performance code.
- Smooth function upgrade prevents performance loss caused by cold start initialization on the application layer. After new function instances are started, FunctionGraph automatically executes the initialization logic and then processes requests.
- The overhead of application layer initialization is identified to accurately determine when resource scaling will be performed and how many resources will be required. This feature makes request latency more stable when the application load increases and more function instances are required.
- If there are continuous requests and the function is not updated, the system may still reclaim or update existing containers. Although no code starts on the platform side, there are cold starts on the service side. The initializer can be used to ensure that requests can be processed properly.
Features of the Initializer
The initializer of each runtime has the following features:
- No custom parameters
The initializer does not support the definition of custom parameters, but only uses the variables in context for logic processing.
- No return values
- Initialization timeout
You can set an initialization timeout (≤ 300s) different from the timeout for invoking the handler.
- Time for execution
Function instances are processes that execute function logic in a container and automatically scale if the number of requests changes. When a new function instance is generated, the system invokes the initializer and then executes the handler logic if the invocation is successful.
- One-time execution
After each function instance starts, the initializer can only be executed once. If an instance fails to execute the initializer, the instance is abandoned and another instance is generated. A maximum of three attempts are allowed. If the initializer is executed successfully, the instance will only process requests upon invocation and will no longer execute the initializer again within its lifecycle.
- Naming rule
For all runtimes except Java, the initializer can be named in the format of [File name].[Initializer name], which is similar with the format of a handler name. For Java, a class needs to be defined to implement the predefined initializer.
- Billing
The initializer execution duration will be billed at the same rate as the function execution duration.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot