<#1937 Add Ray Autoscaler to the Flyte-Ray plugin>...
# flyte-github
a
#1937 Add Ray Autoscaler to the Flyte-Ray plugin Pull request opened by Yicheng-Lu-llll TL;DR NOTE:The ray CI test failed because we need to first merge flyteorg/flyte#4363 to update flytecli. Currently, the Flyte-Ray plugin utilizes Rayjob. However, there are cases where Rayjob may require an autoscaler. 1. After completing a workload with Rayjob, a user might want to retain all the information, logs, past tasks, and actor execution history for a period. As of now, Ray lacks a mechanism to persist these data, necessitating the continuous operation of the Ray cluster even after workload completion. With an autoscaler, the Ray cluster will maintain only the head pod while scaling down all worker pods. 2. User does not need to pre estimate the need of the resource. Autoscaler will care everything. So, this PR adds Ray Autoscaler config to the Flyte-Ray plugin. Also see flyteorg/flyte#4363. Below is an example:
Copy code
import typing

import ray
from flytekitplugins.ray import HeadNodeConfig, RayJobConfig, WorkerNodeConfig

from flytekit import Resources, task, workflow


@ray.remote
def f(x):
    return x * x


ray_config = RayJobConfig(
    head_node_config=HeadNodeConfig(ray_start_params={"log-color": "True"}),
    # The behavior will be:
    # 1. Create a head node and a worker node.
    # 2. The worker node will be scaled to 2.
    # 3. The worker node will be scaled to 0.
    worker_node_config=[WorkerNodeConfig(group_name="ray-group", replicas=1, min_replicas=0, max_replicas=2)],
    enable_in_tree_autoscaling=True,
)


@task(
    task_config=ray_config,
    requests=Resources(mem="1Gi", cpu="2"),
)
def ray_task() -> int:
    import time

    # Import placement group APIs.
    from ray.util.placement_group import (
        placement_group,
        placement_group_table,
        remove_placement_group,
    )
    from ray.util.scheduling_strategies import PlacementGroupSchedulingStrategy

    # use the `placement_group` to trigger autoscaling.
    # each node have two cpus, so this will create 2 workers when executing.
    # Once the task is done, the two workers will be removed.
    pg = placement_group([{"CPU": 2} for i in range(3)])
    ray.get(pg.ready(), timeout=50)

    return 1
  

@workflow
def ray_workflow() -> int:
    return ray_task()
init(replicas=1):
Copy code
(flytekit) ubuntu@ip-172-31-92-18:~/flytekit$ kubectl get pod -n flytesnacks-development
NAME                                                      READY   STATUS    RESTARTS   AGE
f2067d40c9d134daaa6f-n0-0-raycluster-k4klf-head-vgq9z     2/2     Running   0          7s
d134daaa6f-n0-0-raycluster-k4klf-worker-ray-group-wdtqq   1/1     Running   0          7s
task executing(max_replicas=2):
Copy code
(flytekit) ubuntu@ip-172-31-92-18:~/flytekit$ kubectl get pod -n flytesnacks-development
NAME                                                      READY   STATUS            RESTARTS   AGE
f2067d40c9d134daaa6f-n0-0-raycluster-k4klf-head-vgq9z     2/2     Running           0          18s
d134daaa6f-n0-0-raycluster-k4klf-worker-ray-group-wdtqq   1/1     Running           0          18s
d134daaa6f-n0-0-raycluster-k4klf-worker-ray-group-tqg7n   0/1     PodInitializing   0          4s
after(min_replicas=0):
Copy code
(flytekit) ubuntu@ip-172-31-92-18:~/flytekit$ kubectl get pod -n flytesnacks-development
NAME                                                    READY   STATUS    RESTARTS   AGE
f2067d40c9d134daaa6f-n0-0-raycluster-k4klf-head-vgq9z   2/2     Running   0          2m57s
Type ☐ Bug Fix ☐ Feature ☐ Plugin Are all requirements met? ☐ Code completed ☐ Smoke tested ☐ Unit tests added ☐ Code documentation added ☐ Any pending items have an associated Issue Complete description How did you fix the bug, make the feature etc. Link to any design docs etc Tracking Issue flyteorg/flyte#4187 Follow-up issue NA OR https://github.com/flyteorg/flyte/issues/ flyteorg/flytekit Codecov: 62.57% (-0.25%) compared to 63e6632 GitHub Actions: build-plugins (3.8, flytekit-snowflake) ✅ 28 other checks have passed 28/30 successful checks