RI Management

What Is an RI?

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 (RIs) can be created and released by you as required. After you create RIs for a function, FunctionGraph preferentially forwards requests for invoking the function to the RIs. If the number of requests exceeds the processing capability of the RIs, FunctionGraph will forward the excessive requests to on-demand instances and automatically allocates execution resources to these requests.

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

    By default, you do not have the permissions to use the RI function. To use this function, submit a service ticket to add your account to the whitelist.

You can directly create RIs 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 RIs

You can create an RI with just a few clicks.

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

Creating RIs on the function configuration page

You can create different numbers of RIs for different periods, preventing waste of RIs during off-peak hours and ensuring sufficient RIs during peak hours.

The procedure is complex.

Directly Creating a Fixed Number of RIs

Before your create RIs, ensure that the function, for example, Objective-func, for which you want to create RIs 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. Specify the RI information.
    Table 2 RI 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 RIs to be created.

    To determine how many RIs you need 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 RIs for a function, you can only change the RI quantity.

Creating RIs on the Function Configuration Page

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

Before your create RIs, ensure that the function, for example, Objective-func, for which you want to create RIs already exists on the FunctionGraph console.

  1. Log in to the FunctionGraph console, and choose Functions > Function List in the navigation pane.
  2. Click Create Function.
  3. Specify the function 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

    Select Edit code inline and enter the code shown below this table.

    # -*- 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 Now.
  5. On the Configuration tab page, click Create Agency.
  6. Create an agency and grant it the FunctionGraph User permission. For details, see Creating an Agency.
  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. Specify the trigger 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 RIs 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 RIs every 3 minutes.

  10. Click OK.

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