Skip to content

[core] private worker._wait_generators_bulk - #62741

Merged
rueian merged 35 commits into
ray-project:masterfrom
rueian:worker-wait-fetch
Jun 29, 2026
Merged

rueian merged 35 commits into
ray-project:masterfrom
rueian:worker-wait-fetch

Conversation

@rueian

@rueian rueian commented Apr 17, 2026 •

Copy link
Copy Markdown
Contributor

This PR adds a new private _wait_generators_bulk API for waiting for batches of next refs from streaming generators to address the challenges of the current _next_sync API.

The current challenges of the _next_sync API are:

  1. _next_sync is blocking on waiting for the next ref from a generator because it is where we signal the backpressure of a streaming generator by advancing the read index of the generator.
  2. _next_sync doesn't allow waiting for multiple refs with different fetch_local configurations.

These 2 challenges are inconvenient to Ray Data's use cases because:

  1. Ray Data needs to wait for multiple generators in parallel to keep the data pipeline running instead of idling.
  2. Ray Data needs to wait for 2 refs from each generator each time, a block ref, and a metadata ref, with different fetch_local configurations, so they can't just use ray.wait(..., fetch_local=True).

Alternative solutions considered:

  1. We could have a peek_n API that returns the next N refs from a generator without consuming them, but then Ray Data needs to manually advance the read index of the generator to consume the refs through another ack_n API.
  2. Or we could let the peek_n API advance the previous index automatically, but this would require Ray Data to do one more peek to make sure the index is advanced correctly. This solution is really not ideal because the new requirement is implicit, and the backpressure can be broken easily if the peek_n api is called without proper waiting on the previous refs.

Proposed solution:

Add a new private _wait_generators_bulk API for waiting on multiple next refs from streaming generators. The example usage is as follows:

ready = _wait_generators_bulk(
    [
        (gen1, [True, False]),
        (gen2, [False, True])
    ],
    num_return=1,
    timeout=2,
)
assert ready == [(gen1, [ref1, ref2])]

The API will return a list of tuples, each containing a generator and a list of refs. The refs in the list are the next refs from the generator that are all ready and pulled locally if the corresponding fetch_local is true.

The API works by waiting for the last ref of each generator first, with a new _wait_and_fetch helper, and then picking the ready generators from there to issue a second ray.wait if their remaining refs are required to be local.

Benchmark

The initial integration is done at the PR #63808 and is benchmarked on the worker_scaling_{{num_workers}}_{{worker_type}} release tests with the matrix:

num_workers: [500, 1000, 2000, 5000]
worker_type: [actors, tasks]
bulk:        [0, 1] # whether _wait_generators_bulk path is enabled or not

Each config was run 5 times.

E2E Execution Time

workers type bulk=0 bulk=1 delta
500 actors 67.375 +- 0.097 65.240 +- 0.979 -3.17%
500 tasks 61.225 +- 0.529 53.870 +- 0.234 -12.01%
1000 actors 123.388 +- 0.790 110.373 +- 0.534 -10.55%
1000 tasks 115.860 +- 0.811 95.560 +- 0.607 -17.52%
2000 actors 261.420 +- 2.266 216.533 +- 2.723 -17.17%
2000 tasks 228.154 +- 4.027 178.552 +- 0.491 -21.74%
5000 actors 555.034 +- 9.703 340.000 +- 10.101 -38.74%
5000 tasks 479.263 +- 12.886 395.351 +- 15.269 -17.51%

@rueian rueian added the go add ONLY when ready to merge, run all tests label Apr 17, 2026

@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 a new private API, _wait_and_fetch, which allows for per-object fetch_local settings during wait operations, extending the capabilities of the standard ray.wait. The changes include the Python API implementation, Cython bindings, and the underlying C++ CoreWorker::WaitAndFetch logic. Feedback highlights a critical potential integer overflow in the C++ timeout calculation, a request to align default timeout constants with existing APIs for consistency, and a fix for a missing time module import in the test suite.

Comment thread src/ray/core_worker/core_worker.cc Resolved
Comment thread python/ray/_private/worker.py Resolved
Comment thread python/ray/tests/test_wait.py Resolved
@rueian
rueian force-pushed the worker-wait-fetch branch from 4aed807 to 9ae09a8 Compare April 18, 2026 06:42
@rueian
rueian marked this pull request as ready for review April 21, 2026 23:59
@rueian
rueian requested a review from a team as a code owner April 21, 2026 23:59
@ray-gardener ray-gardener Bot added the core Issues that should be addressed in Ray Core label Apr 22, 2026
Comment thread src/ray/core_worker/core_worker.cc Resolved
@rueian
rueian marked this pull request as draft May 4, 2026 03:59
@rueian
rueian force-pushed the worker-wait-fetch branch from cbdda98 to d33e4ca Compare May 4, 2026 22:58
@rueian rueian changed the title [core] private worker._wait_and_fetch [core] private worker._wait_generators_bulk May 4, 2026
@rueian
rueian marked this pull request as ready for review May 5, 2026 01:24

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d33e4cace3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread python/ray/_private/worker.py Resolved
@rueian
rueian force-pushed the worker-wait-fetch branch from d33e4ca to 9f9aae9 Compare May 5, 2026 03:09
@rueian
rueian force-pushed the worker-wait-fetch branch from 9f9aae9 to c254375 Compare May 5, 2026 04:22
Comment thread src/ray/core_worker/task_manager.cc Resolved
@rueian
rueian force-pushed the worker-wait-fetch branch from c254375 to a0095e7 Compare May 5, 2026 05:08
Comment thread src/ray/core_worker/task_manager.cc Resolved
Comment thread python/ray/_private/worker.py Resolved
Comment thread src/ray/core_worker/task_manager.cc Resolved
Signed-off-by: Rueian Huang <[email protected]>
@rueian
rueian force-pushed the worker-wait-fetch branch from 275962f to 9a6652d Compare June 16, 2026 00:13
Signed-off-by: Rueian Huang <[email protected]>
Comment thread python/ray/_private/worker.py Resolved
Comment thread python/ray/_private/worker.py Resolved
Signed-off-by: Rueian Huang <[email protected]>
@rueian
rueian requested a review from Kunchd June 16, 2026 21:42

@Kunchd Kunchd 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.

Thanks for answering all my previous questions! I left some final minor nits.

Comment thread python/ray/_private/worker.py Resolved
Comment thread python/ray/_private/worker.py Resolved
Comment thread python/ray/_private/worker.py Outdated
if not fetch_local:
continue
required_local_refs.append(ref)
if ref not in ready_local_ref_set and ref not in refs_seen:

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.

is refs_seen necessary? Is it possible for different generators to output the same refs? Or are we trying to catch the case where the user passes the same generator twice.

@rueian rueian Jun 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, it is for catching duplicated generators, but agreed that it is not really necessary.

Comment thread python/ray/_private/worker.py Resolved
Comment thread python/ray/_raylet.pyx Resolved
auto num_items_to_consume = num_items;
if (end_of_stream_index_ != -1) {
num_items_to_consume = std::min(num_items, end_of_stream_index_ - next_index_);
}

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.

Should we surface a python error to the caller if they violate our assumption?

Comment thread python/ray/tests/test_wait.py Resolved
Comment thread python/ray/tests/test_wait.py Resolved
rueian added 2 commits June 18, 2026 14:38
Signed-off-by: Rueian Huang <[email protected]>
@rueian
rueian force-pushed the worker-wait-fetch branch from 145311b to 82065b1 Compare June 23, 2026 22:12
@rueian
rueian requested a review from Kunchd June 24, 2026 00:18

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit aa61d0e. Configure here.

Comment thread src/ray/core_worker/task_manager.cc Resolved
@rueian
rueian force-pushed the worker-wait-fetch branch 2 times, most recently from b8c0451 to c7f69a9 Compare June 25, 2026 18:24
@rueian
rueian force-pushed the worker-wait-fetch branch from c7f69a9 to 00b858f Compare June 25, 2026 21:46

@Kunchd Kunchd 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.

LGTM! I left only one comment that I'll leave up to your judgement on.

Args:
num_refs: The number of references to consume, starting from the
current head of the stream. Must be positive.
"""

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.

Only concern I have left is if someone trying to use the consume_next_ref_n function doesn't have context that the refs needs to be read before consume can be called, so unready refs are silently consumed.

We chatted offline already so I'll leave it up to your judgement for how we should handle this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Makes sense. A guard is added in the latest commit.

@Kunchd Kunchd 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.

Nice thanks for adding the guard!

@rueian
rueian enabled auto-merge (squash) June 29, 2026 18:49
@rueian
rueian merged commit 20f1f17 into ray-project:master Jun 29, 2026
4 checks passed
400Ping pushed a commit to 400Ping/ray that referenced this pull request Aug 18, 2026
This PR adds a new private `_wait_generators_bulk` API for waiting for
batches of next refs from streaming generators to address the challenges
of the current `_next_sync` API.

### The current challenges of the `_next_sync` API are:
1. `_next_sync` is blocking on waiting for the next ref from a generator
because it is where we signal the backpressure of a streaming generator
by advancing the read index of the generator.
2. `_next_sync` doesn't allow waiting for multiple refs with different
`fetch_local` configurations.

These 2 challenges are inconvenient to Ray Data's use cases because:
1. Ray Data needs to wait for multiple generators in parallel to keep
the data pipeline running instead of idling.
2. Ray Data needs to wait for 2 refs from each generator each time, a
block ref, and a metadata ref, with different fetch_local
configurations, so they can't just use `ray.wait(...,
fetch_local=True)`.

### Proposed solution:
Add a new private `_wait_generators_bulk` API for waiting on multiple
next refs from streaming generators. The example usage is as follows:
```python
ready = _wait_generators_bulk(
    [
        (gen1, [True, False]),
        (gen2, [False, True])
    ],
    num_return=1,
    timeout=2,
)
assert ready == [(gen1, [ref1, ref2])]
```
The API will return a list of tuples, each containing a generator and a
list of refs. The refs in the list are the next refs from the generator
that are all ready and pulled locally if the corresponding `fetch_local`
is true.

The API works by waiting for the last ref of each generator first, with
a new `_wait_and_fetch` helper, and then picking the ready generators
from there to issue a second `ray.wait` if their remaining refs are
required to be local.

## Benchmark

The initial integration is done at the PR
ray-project#63808 and is benchmarked on the
`worker_scaling_{{num_workers}}_{{worker_type}}` release tests with the
matrix:
```
num_workers: [500, 1000, 2000, 5000]
worker_type: [actors, tasks]
bulk:        [0, 1] # whether _wait_generators_bulk path is enabled or not
```
Each config was run 5 times.

### E2E Execution Time

workers | type | bulk=0 | bulk=1 | delta
-- | -- | -- | -- | --
500 | actors | 67.375 +- 0.097 | 65.240 +- 0.979 | -3.17%
500 | tasks | 61.225 +- 0.529 | 53.870 +- 0.234 | -12.01%
1000 | actors | 123.388 +- 0.790 | 110.373 +- 0.534 | -10.55%
1000 | tasks | 115.860 +- 0.811 | 95.560 +- 0.607 | -17.52%
2000 | actors | 261.420 +- 2.266 | 216.533 +- 2.723 | -17.17%
2000 | tasks | 228.154 +- 4.027 | 178.552 +- 0.491 | -21.74%
5000 | actors | 555.034 +- 9.703 | 340.000 +- 10.101 | -38.74%
5000 | tasks | 479.263 +- 12.886 | 395.351 +- 15.269 | -17.51%

---------

Signed-off-by: Rueian Huang <[email protected]>
Signed-off-by: 400Ping <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Issues that should be addressed in Ray Core go add ONLY when ready to merge, run all tests unstale A PR that has been marked unstale. It will not get marked stale again if this label is on it.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants