Help Center> FunctionGraph> User Guide> Reserved Instance Management (Old)
Updated on 2023-12-21 GMT+08:00

Reserved Instance Management (Old)

What Is a Reserved Instance?

FunctionGraph provides on-demand and reserved instances.

  • On-demand instances are created and released by FunctionGraph based on actual function usage. When receiving requests to call functions, FunctionGraph automatically allocates execution resources to the requests.
  • Reserved instances can be created and released by you as required. After you create reserved instances for a function, FunctionGraph preferentially forwards requests to the reserved instances. If the number of requests exceeds the processing capability of the reserved instances, FunctionGraph will forward the excessive requests to on-demand instances and automatically allocates execution resources to these requests.

    After reserved instances are created for a function, the code, dependencies, and initializer of the function are automatically loaded. Reserved instances are always alive in the execution environment, eliminating the influence of cold starts on your services.

    To use reserved instances, submit a service ticket to add your account to the whitelist.

You can directly create reserved instances or create them on the function configuration page. Differences between the two modes are described in the following table.

Table 1 Differences between the two modes

Mode

Advantage

Disadvantage

Directly creating reserved instances

You can create a reserved instance with just a few clicks.

Only a fixed number of reserved instances can be created. The reserved instances may be insufficient during peak hours and become idle during off-peak hours.

Creating reserved instances on the function configuration page

You can create different numbers of reserved instances for different periods, preventing waste of reserved instances during off-peak hours and ensuring enough reserved instances during peak hours.

The procedure is complex.

Directly Creating a Fixed Number of Reserved Instances

Ensure that the function, for example, Objective-func, for which you want to create reserved instances already exists on the FunctionGraph console.

  1. Log in to the FunctionGraph console, and choose Functions > Reserved Instances in the navigation pane.
  2. Click Configure Reserved Instance.
  3. Set the following parameters:
    Table 2 Reserved instance information

    Parameter

    Description

    App

    Select the app to which the Objective-func function belongs.

    Function

    Select Objective-func.

    Version

    Select a version of the Objective-func function.

    RI Quantity

    Enter the number of reserved instances to be created.

    To determine how many reserved instances to create, view the number of used instances in the Instance Statistics area or check the usage of the Objective-func function.

  4. Click OK.

    After creating reserved instances for a function, you can only change the reserved instance quantity.

Creating Reserved Instances on the Function Configuration Page

The number of function instances varies between different periods. To ensure enough reserved instances during peak hours and prevent waste of reserved instances during off-peak hours, you can create a timer for invoking the corresponding function with different number of reserved instances in different periods.

Ensure that the function, for example, Objective-func, for which you want to create reserved instances already exists on the FunctionGraph console.

  1. Return to the FunctionGraph console, and choose Functions > Function List in the navigation pane.
  2. Click Create Function.
  3. Set the following information.

    Parameter

    Description

    Template

    Select Create from scratch.

    Function Name

    Enter a name to identify the function.

    App

    Select default.

    Agency

    Select Use no agency.

    Description

    Enter a description for the function. This parameter is optional.

    Runtime

    Select Python 2.7.

    Handler

    Enter index.handler.

    Code Entry Mode

    When creating a function, select Default code. On the Configuration tab page, select Edit code inline and enter the following code:

    # -*- coding:utf-8 -*-
    import json
    import requests
    def handler (event, context):
        domainId = "https://{Endpoint}"
        url = "/v2/{project_id}/fgs/functions/{func-urn}/reservedinstances"
        token = context.getToken()
        requrl = domainId + url
        headerdata = {"Content-Type":"application/json","x-auth-token":token}
        r = requests.put(requrl, data=event["user_event"], headers=headerdata,verify=False)
        return r.json

    Replace the following parameters with the actual values:

    • Endpoint: Endpoint of the Objective-func function. For details, see Regions and Endpoints.
    • project_id: ID of the project to which the Objective-func function belongs. For details, see Obtaining a Project ID.
    • func-urn: URN of the Objective-func function.
  4. Click Create Function.
  5. On the Configuration tab page, click Create Agency.
  6. Create an agency and grant it the FunctionGraph User permission. For details, see Configuring Agency Permissions.
  7. On the Configuration tab page, select the agency created in 6, and click Save.
  8. On the Triggers tab page, click Create Trigger.
  9. Set the following information.

    Parameter

    Description

    Trigger Type

    Select Timer.

    Timer Name

    Enter a name to identify the timer.

    Rule

    Select Cron expression and enter a cron expression as required.

    Enable Trigger

    By default, this function is enabled. Retain the default setting.

    Additional Information

    Enter the number of reserved instances required in different periods based on the trigger rule.

    Figure 1 Creating a timer trigger

    Figure 1 shows a timer trigger that requests FunctionGraph to create two reserved instances every 3 minutes.

  10. Click OK.

    After trigger creation, different numbers of reserved instances will be created for the Objective-func function in different periods based on the trigger rule.