Skip to content

[Data][Autoscaler][1/N] Add AutoscalingCoordinator - #59353

Merged
bveeramani merged 5 commits into
masterfrom
autoscaling-coordinator
Dec 11, 2025
Merged

bveeramani merged 5 commits into
masterfrom
autoscaling-coordinator

Conversation

@bveeramani

Copy link
Copy Markdown
Member

When multiple Ray Data or Ray Train jobs run concurrently on a cluster, they each independently request resources from the Ray Autoscaler without awareness of each other. This can lead to inefficient resource utilization and conflicts.

This PR introduces an AutoscalingCoordinator that acts as a central point for coordinating resource requests across components.

Signed-off-by: Balaji Veeramani <[email protected]>
Signed-off-by: Balaji Veeramani <[email protected]>
@bveeramani
bveeramani requested a review from a team as a code owner December 10, 2025 19:09
Signed-off-by: Balaji Veeramani <[email protected]>

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces an AutoscalingCoordinator to centralize resource requests, which is a significant improvement for managing resources in concurrent job scenarios. The implementation is robust, featuring a resilient actor-based design with timeout handling, caching, and lazy initialization. The tests are comprehensive, covering unit, integration, and failure cases effectively.

My feedback highlights a few areas for improvement:

  • A potential high-severity issue regarding the over-allocation of remaining resources.
  • A medium-severity issue concerning an in-place modification of an argument list, which could lead to unexpected side effects.
  • Minor suggestions for improving code style and test hygiene.

Overall, this is a well-executed feature addition. Addressing these points will further enhance the reliability and maintainability of the new coordinator.

Comment on lines +407 to +409
for ongoing_req in ongoing_reqs:
if ongoing_req.request_remaining:
ongoing_req.allocated_resources.extend(cluster_node_resources)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current implementation allocates all remaining cluster resources to every requester with request_remaining=True. As noted in the code comment, this can lead to double-allocation and overallocation if multiple such requesters exist, potentially causing resource contention and task scheduling failures. While this is for parity with previous behavior, it's a significant correctness risk. A more robust approach would be to divide the remaining resources among the requesters, for example, proportionally to their initial requests, or on a first-come, first-served basis.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bveeramani is this valid?

requester_id = kwargs.get(requester_id_param)
if requester_id is None:
# Try to get from args by checking function signature
import inspect

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better code style, to avoid potential overhead from repeated imports, and to make dependencies explicit, it's recommended to move the import inspect statement to the top of the file with other imports.

Comment on lines +283 to +285
for r in resources:
for k in r:
r[k] = math.ceil(r[k])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This loop modifies the resources list in-place, which can be an unexpected side effect for the caller. To avoid this, consider creating a new list of resource dictionaries with the rounded-up values using a list comprehension. This makes the function's behavior clearer and safer as it avoids mutating its inputs.

        resources = [{k: math.ceil(v) for k, v in r.items()} for r in resources]

Comment on lines +40 to +46
MOCKED_TIME = 0


def mock_time():
global MOCKED_TIME

return MOCKED_TIME

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using a global variable MOCKED_TIME for mocking time can lead to test flakiness, especially if tests are run in parallel. It would be more robust to encapsulate the mock time state, for example, by using a class instance or a fixture that doesn't rely on global state.

For example, you could define a callable class:

class MockTime:
    def __init__(self, initial_time=0):
        self.current_time = initial_time

    def __call__(self):
        return self.current_time

    def set_time(self, new_time):
        self.current_time = new_time

And then in your test:

mock_time = MockTime()
with patch("time.time", mock_time):
    # ... test logic ...
    mock_time.set_time(10)
    # ... more test logic ...

@bveeramani bveeramani changed the title [Data][Autoscaling][1/N] Add AutoscalingCoordinator [Data][Autoscaler][1/N] Add AutoscalingCoordinator Dec 10, 2025
# Call tick() as an actor task,
# so we don't need to handle multi-threading.
time.sleep(self.TICK_INTERVAL_S)
ray.get(self._self_handle.tick.remote())

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Tick thread silently crashes on unhandled exceptions

The tick_thread_run function has no exception handling around the ray.get(self._self_handle.tick.remote()) call. If any exception occurs (network issues, actor errors, or other runtime errors), the thread will terminate silently without any notification since it's a daemon thread. This causes the periodic maintenance operations (purging expired requests, updating cluster resources, reallocating resources) to stop functioning permanently until the actor restarts, potentially leading to stale allocation information and memory growth from accumulated expired requests.

Fix in Cursor Fix in Web

return False
for key in res2:
res1[key] -= res2[key]
return True

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: KeyError when subtracting zero-valued resources not present in node

The _maybe_subtract_resources function raises a KeyError if a requested resource bundle contains a key with value 0 that doesn't exist in the node's resources. The check on line 352 uses res1.get(key, 0) < res2[key], which evaluates to 0 < 0 (False) when res2[key] is 0, so the function proceeds to line 355 which attempts res1[key] -= res2[key]. Since key doesn't exist in res1, this raises a KeyError. This could occur if a resource request includes resource types with zero values that aren't present on cluster nodes.

Fix in Cursor Fix in Web

Signed-off-by: Balaji Veeramani <[email protected]>
@bveeramani
bveeramani enabled auto-merge (squash) December 10, 2025 21:41
@github-actions github-actions Bot added the go add ONLY when ready to merge, run all tests label Dec 10, 2025
@github-actions
github-actions Bot disabled auto-merge December 10, 2025 21:41
@bveeramani
bveeramani enabled auto-merge (squash) December 10, 2025 22:23
Signed-off-by: Balaji Veeramani <[email protected]>
@github-actions
github-actions Bot disabled auto-merge December 10, 2025 23:57
@bveeramani
bveeramani merged commit fcba103 into master Dec 11, 2025
@bveeramani
bveeramani deleted the autoscaling-coordinator branch December 11, 2025 00:46
justinvyu pushed a commit that referenced this pull request May 14, 2026
…rces (#63306)

#59353 open-sourced the `AutoscalingCoordinator` including a `math.ceil`
block on incoming resource bundles. The block traces back to that
validator was added back in #26626 to fix a *shape* bug (#23166 ). The
literal `int` came from an informal type annotation;
It seems that neither v1 (KV → load_metrics → bin-packing) nor v2 (proto
`map<string, double>`) requires it.

Drop the value-type check in the SDK and remove the `math.ceil`
workaround in Ray Data

---------

Signed-off-by: Lehui Liu <[email protected]>
TruongQuangPhat pushed a commit to cyhapun/ray-fix-issue that referenced this pull request May 27, 2026
…rces (ray-project#63306)

ray-project#59353 open-sourced the `AutoscalingCoordinator` including a `math.ceil`
block on incoming resource bundles. The block traces back to that
validator was added back in ray-project#26626 to fix a *shape* bug (ray-project#23166 ). The
literal `int` came from an informal type annotation;
It seems that neither v1 (KV → load_metrics → bin-packing) nor v2 (proto
`map<string, double>`) requires it.

Drop the value-type check in the SDK and remove the `math.ceil`
workaround in Ray Data

---------

Signed-off-by: Lehui Liu <[email protected]>
Signed-off-by: phattruong <[email protected]>
alexandrplashchinsky pushed a commit to alexandrplashchinsky/ray-alex that referenced this pull request May 29, 2026
…rces (ray-project#63306)

ray-project#59353 open-sourced the `AutoscalingCoordinator` including a `math.ceil`
block on incoming resource bundles. The block traces back to that
validator was added back in ray-project#26626 to fix a *shape* bug (ray-project#23166 ). The
literal `int` came from an informal type annotation;
It seems that neither v1 (KV → load_metrics → bin-packing) nor v2 (proto
`map<string, double>`) requires it.

Drop the value-type check in the SDK and remove the `math.ceil`
workaround in Ray Data

---------

Signed-off-by: Lehui Liu <[email protected]>
Signed-off-by: Alexandr Plashchinsky <[email protected]>
pseudo-rnd-thoughts added a commit that referenced this pull request Oct 30, 2026
## Summary

Runs the shared AutoscalingCoordinator actor on the head node instead of
the first caller's node, so losing a worker node no longer takes the
coordinator with it for every Ray Data and Ray Train requester on the
cluster.

## Before

`get_or_create_autoscaling_coordinator` created the detached actor with
hard node affinity to the caller's node and `max_restarts=-1`. The
original PR (#59353) noted "Create the actor on the local node, to
reduce network overhead".

The problem is that the coordinator, though, is shared by every
requester on the cluster. The first caller can be on any node, for
example a Train controller or a Ray Data executor inside a Train worker.
If that node dies, the actor can't be rescheduled
(`ActorUnschedulableError`). Requesters that already hold a handle, such
as Train's coordinator client, lose it for good, so a running elastic
run can't resize or restart.

Soft affinity doesn't help:
`ClusterResourceScheduler::GetBestSchedulableNode` places an actor that
requests no resources on a random node unless its affinity is hard, so
it could land on an autoscaled worker and restart elsewhere every time
that worker scales down.

## After

The actor requests `node:__internal_head__: 0.001` and uses
`scheduling_strategy="DEFAULT"`. This is the same pattern as the Serve
controller (`serve/_private/default_impl.py`) and both Train state
actors (`train/_internal/state/state_actor.py`,
`train/v2/_internal/state/state_actor.py`). The head node lives as long
as the cluster. With GCS fault tolerance, a restarted head has the same
resource, so the actor can restart there.

The explicit `"DEFAULT"` strategy matters. Without it, a caller in a
placement group that captures child tasks (Tune trials, Train V1
workers) makes the actor join that group. The bundles don't hold the
head-node resource, so creation fails with `ValueError: Cannot schedule
_AutoscalingCoordinatorActor with the placement group` and a detached
actor shouldn't die with someone else's group anyway.

## New tests 

In `test_autoscaling_coordinator.py`:
- `test_coordinator_actor_runs_on_the_head_node`: created from a worker
node, the actor runs on the head and survives that worker's removal.
- `test_coordinator_actor_escapes_the_callers_placement_group`: created
from a task in a placement group that captures child tasks, the actor
runs on the head and survives the group's removal. It fails without
`scheduling_strategy="DEFAULT"`.

The end-to-end case,
`test_run_restarts_after_the_coordinators_first_caller_dies`, is in
#67481.

AI assistance (Claude Code) was used to write these changes and tests,
run the suites, and write this description.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: Mark Towers <[email protected]>
Signed-off-by: Mark Towers <[email protected]>
Co-authored-by: Mark Towers <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Chi-Sheng Liu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants