Skip to content

_wait_generators_bulk perf - #63808

Closed
rueian wants to merge 1 commit into
ray-project:masterfrom
rueian:worker-wait-fetch-perf
Closed

rueian wants to merge 1 commit into
ray-project:masterfrom
rueian:worker-wait-fetch-perf

Conversation

@rueian

@rueian rueian commented Jun 2, 2026 •

Copy link
Copy Markdown
Contributor

DO NOT MERGE. See #62741

@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 bulk consumption and peeking APIs for streaming generators in Ray, including _wait_and_fetch and _wait_generators_bulk in Python, along with corresponding C++ CoreWorker implementations (WaitAndFetch, TryReadObjectRefStreamN, and PeekObjectRefStreamN). These changes are integrated into Ray Data's streaming executor to optimize task completion waiting. The review feedback highlights several critical issues: potential runtime NameErrors due to a missing time import and an un-namespaced exception reference, a bug in TryReadObjectRefStreamN where backpressure_threshold defaults to 0 instead of -1 (which could cause generator hangs), a potential crash from a strict RAY_CHECK_EQ on mismatched task IDs, and the risky use of assert False for control flow validation.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

core_worker = self.worker.core_worker
try:
core_worker.try_read_next_object_ref_stream_n(self._generator_ref, num_refs)
except ObjectRefStreamEndOfStreamError:

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 exception ObjectRefStreamEndOfStreamError is referenced here but it may not be imported in this file's namespace, which would cause a NameError at runtime. It is safer to reference it via ray.exceptions.ObjectRefStreamEndOfStreamError or explicitly import it.

Suggested change
except ObjectRefStreamEndOfStreamError:
except ray.exceptions.ObjectRefStreamEndOfStreamError:

from typing import TYPE_CHECKING, Dict, List, Optional, Tuple

import ray
from ray._private.worker import _wait_generators_bulk

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 time module is used in this file (e.g., time.monotonic()) but is not imported at the top of the file. This will result in a NameError at runtime. Please import time.

Suggested change
from ray._private.worker import _wait_generators_bulk
import time
from ray._private.worker import _wait_generators_bulk

Comment thread src/ray/core_worker/task_manager.cc Resolved
Comment thread src/ray/core_worker/task_manager.cc Resolved
except ray.exceptions.ObjectRefStreamEndOfStreamError:
try:
ray.get(self._pending_block_ref)
assert False, "Above ray.get should raise an exception."

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 assert False for control flow validation is risky because assertions can be optimized away when Python is run with the -O flag. If optimized away, this block will silently succeed instead of raising an exception. It is better to explicitly raise a RuntimeError.

Suggested change
assert False, "Above ray.get should raise an exception."
raise RuntimeError("Above ray.get should raise an exception.")

@rueian
rueian force-pushed the worker-wait-fetch-perf branch from adf9211 to c31abae Compare June 8, 2026 21:05
@rueian
rueian force-pushed the worker-wait-fetch-perf branch 4 times, most recently from cbfab1f to 5384b20 Compare June 16, 2026 01:16
rueian added a commit that referenced this pull request Jun 29, 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
#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]>
@rueian
rueian force-pushed the worker-wait-fetch-perf branch from 9d6bf8a to ce108fd Compare June 29, 2026 21:58
Rebased onto master, keeping only the python/ray/data changes from the
worker-wait-fetch-perf branch (all core/_raylet/task_manager changes are
already in master). Reconciled map_operator.py: kept master's
custom_op_stats reporting while preserving the grouped-yield perf
optimization (yield block + metadata together via _num_objects_per_yield=2).

Signed-off-by: Rueian Huang <[email protected]>
@rueian
rueian force-pushed the worker-wait-fetch-perf branch from ce108fd to 443e068 Compare June 29, 2026 22:01
@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had
any activity for 14 days. It will be closed in another 14 days if no further activity occurs.
Thank you for your contributions.

You can always ask for help on our discussion forum or Ray's public slack channel.

If you'd like to keep this open, just leave any comment, and the stale label will be removed.

@github-actions github-actions Bot added the stale The issue is stale. It will be closed within 7 days unless there are further conversation label Jul 14, 2026
@github-actions

Copy link
Copy Markdown

This pull request has been automatically closed because there has been no more activity in the 14 days
since being marked stale.

Please feel free to reopen or open a new pull request if you'd still like this to be addressed.

Again, you can always ask for help on our discussion forum or Ray's public slack channel.

Thanks again for your contribution!

@github-actions github-actions Bot closed this Jul 28, 2026
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

stale The issue is stale. It will be closed within 7 days unless there are further conversation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant