Serverless Ray node

更新时间:
复制 MD 格式

DataWorks Serverless Ray nodes let you develop and periodically schedule Python jobs that use the Ray distributed framework on EMR Serverless Ray computing resources.

Introduction

EMR Serverless Ray provides managed Ray computing capabilities on top of Spark workspaces. It is compatible with open source Ray APIs and supports the Python programming model for distributed computing, machine learning, and data processing. With DataWorks Serverless Ray nodes, you can write Python code online and configure the ray job submit command to develop, debug, and schedule your jobs.

Limitations

  • Computing resources: You can only select a bound EMR Serverless Ray computing resource. The Serverless resource group must also have network connectivity with the computing resource.

  • Language: Only the Python language is supported.

  • Execution: You can only submit the entire script for execution. Running single lines or individual code blocks is not supported.

Prerequisites

  • Bind an EMR Serverless Ray computing resource to the target DataWorks workspace and ensure that the Ray cluster is available.

  • (Optional) If you are using a RAM user to develop tasks, add the RAM user to the workspace and grant it the Developer or Workspace Administrator role. The Workspace Administrator role has extensive permissions, so grant it with caution. For more information about how to add a member, see Add a member to a workspace.

    Note

    If you are using a root account, you can skip this step.

Create a Serverless Ray node

You create a Serverless Ray node the same way as other node types in Data Studio. For more information, see Nodes.

Develop a Serverless Ray node

Developing a Serverless Ray node involves writing Python code in the code editor and configuring the job submission command in the Submit Command section. When you create a file, the system automatically generates a submission command with a filename that matches the node name and has a .py extension.

Estimate Pi with Ray

The following example uses the Monte Carlo method to estimate Pi. It demonstrates how to use the code editor and the submission command together.

Step 1: Write the Python code

In the code editor, write the following Python code.

import ray
import random
import time
import sys

ray.init()

@ray.remote
def compute_points_in_circle(num_points: int) -> int:
    inside = 0
    for _ in range(num_points):
        x = random.uniform(0, 1)
        y = random.uniform(0, 1)
        if x * x + y * y <= 1:
            inside += 1
    return inside

def estimate_pi(total_points: int, num_workers: int = 4) -> float:
    start_time = time.time()
    points_per_worker = total_points // num_workers
    tasks = [compute_points_in_circle.remote(points_per_worker) for _ in range(num_workers)]
    results = ray.get(tasks)
    total_inside = sum(results)
    pi_estimate = 4.0 * total_inside / total_points
    print(f"Estimated π: {pi_estimate:.10f}, seconds: {time.time() - start_time:.4f}")
    return pi_estimate

if __name__ == "__main__":
    estimate_pi(total_points=int(sys.argv[1]), num_workers=4)
    ray.shutdown()

Step 2: Configure the Submit command

In the Submit Command section, configure the submission command. For example:

ray job submit \
--working-dir "." \
-- python ss_ray.py 1000000
Important
  • When using a local working directory (for example, --working-dir ".")

    • The script filename after -- python must match the node name with a .py extension.

    • The platform automatically controls and overwrites the --address, --submission-id, and --working-dir parameters. You do not need to configure them manually.

  • When using a remote OSS path (for example, --working-dir "https://<bucket>.<endpoint>/path/to/your.zip")

    • The platform does not overwrite the remote path you specified for --working-dir.

    • Permissions: You can use a private OSS file (typically a .zip package) as the working directory. The Serverless Ray node automatically uses the credentials of the user who runs the task (or the RAM role assumed by the user) to generate a signed temporary access URL for your private OSS file.

    • Prerequisite: You must ensure that the user who runs this node has read permissions (oss:GetObject) on the specified OSS path.

Node configuration reference

The following table describes the configuration parameters for a Serverless Ray node.

Section

Parameter

Description

Python code

Python code

Write Python code that uses the Ray framework. Ray APIs such as ray.init() for initialization and the @ray.remote decorator are supported.

Submit command

Submit command

Configure the submission command for the Ray job. The command format is ray job submit [options] -- python script.py [args].

runtime-env-json

Optional. Configure the runtime environment. For example, use the pip field to install additional Python dependency packages. Example: --runtime-env-json '{"pip": ["numpy", "pandas"]}'.

Parameters

Specify the parameters to pass to your code. You can configure a parameter as a dynamic parameter by using ${var}.

If your job depends on multiple Python files, you can create the dependency files as DataWorks Ray File resources, reference them in your code by using ##@resource_reference, and then organize the ray job submit command with --working-dir pointing to the working directory. For more information about creating resources, see Create an EMR resource.

Debug a Serverless Ray node

  1. Configure the run configuration.

    In the Run Configuration panel on the right side of the node, configure the following parameters.

    Parameter

    Description

    Compute Resource

    Select the Serverless Ray compute resource that you have associated.

    Resource Group

    Select a serverless resource group that has passed the network connectivity test. Serverless Ray nodes support only serverless resource groups.

    Script Parameters

    When configuring the node content, you can define variables by using ${parameter name}. Configure the Script Parameters section with the Parameter name and Parameter Value. These values are dynamically replaced with actual values at runtime. For more information, see Configure scheduling parameters.

  2. Debug and run the node.

    Click Save and then click Run to debug the node.

Next steps

  • Configure schedule settings: If you want nodes in the project directory to be periodically scheduled, configure the Scheduling Policy and related scheduling properties in the Scheduling Settings panel on the right side of the node.

  • Deploy a node: If you want to deploy the task to the production environment, click the image icon on the page to initiate the deployment process. Nodes in the project directory are periodically scheduled only after they are deployed to the production environment.

Reference

For details on referencing Ray File resources, see Reference a Ray File resource.