Skip to content

[Data] Count Materialized Data as Downstream Capacity - #66226

Merged
justinvyu merged 16 commits into
ray-project:masterfrom
rayhhome:materialized-and-backpressured
Sep 22, 2026
Merged

justinvyu merged 16 commits into
ray-project:masterfrom
rayhhome:materialized-and-backpressured

Conversation

@rayhhome

@rayhhome rayhhome commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Description

lightgbm_train_batch_inference_benchmark_100G times out at 600s with ReadParquet pinned at Tasks: 1 [backpressured:tasks(DownstreamCapacity)], turning a ~40s read into 400-560s.

Since #64456 the resource manager tracks object store memory with BlockRefCounter, which counts a block until every reference to it is gone. The train loop calls materialize() on its shard (train_batch_inference_benchmark.py:70), so those blocks stay in the producing operator's ref-counted output and DownstreamCapacityBackpressurePolicy reads them as output waiting to be consumed. The ratio only grows, and the producer is throttled for memory it cannot release by slowing down.

The consumer now reports what it holds, and the policy counts it as downstream capacity rather than as output waiting to be consumed. Accounting is unchanged; those bytes really are in the object store and still count toward every budget.

Additional information

We also now resets StreamingSplitDataIterator's iter_prefetched_bytes to 0 when _to_ref_bundle_iterator() is called on it. We made this decision because otherwise, this number will accumulate across executions for StreamingSplitDataIterator when materialize() is called multiple times, which means one iterator materialized twice can occupy two times total number of bytes and is thus incorrect. Please read #66226 (review) for full context.

The reported prefetched bytes total is a single global, so it goes into the single global term. _get_downstream_capacity_size_bytes reaches the external consumer's bytes only for an operator whose consumer is external; an upstream operator still paces against its downstream operator's in-flight inputs.

Because capacity includes what the consumer holds, a producer may build a larger queue during a materialize() than during ordinary iteration, bounded there by the object store budget rather than by the prefetch window.

Checks

Not a duplicate: #66095 targeted the same test and is closed. This uses a consumer-reported total instead of recomputing output pressure from queue metrics, avoiding a shared output queue walk that over-charges each parent of an ineligible multi-input operator.

AI assistance was used to write this change.

Signed-off-by: Sirui Huang <[email protected]>
@rayhhome rayhhome self-assigned this Sep 17, 2026
Copilot AI lite review requested due to automatic review settings September 17, 2026 06:35
@rayhhome
rayhhome requested a review from a team as a code owner September 17, 2026 06:35
@rayhhome rayhhome added data Ray Data-related issues go add ONLY when ready to merge, run all tests labels Sep 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 tracking for retained consumer bytes to prevent unnecessary backpressure when external consumers hold onto materialized data. By reporting these retained bytes back to the executor, the backpressure policy can subtract them from the operator's output size instead of treating them as queue backlog. The review feedback points out a potential leak where client retained bytes are not cleared if a consumer exits early, suggesting that they should also be cleared in notify_split_finished().

Copilot AI 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.

🔵 Needs a closer look

It changes core backpressure/resource-signaling behavior in the streaming executor path, which can have subtle performance and liveness implications that merit final human review even though unit tests were added.

Pull request overview

This PR updates Ray Data’s downstream-capacity backpressure signal so that blocks retained by external consumers (e.g., Train loops calling materialize() on iterators/split shards) are not misinterpreted as unconsumed output backlog, which can otherwise throttle producers indefinitely under DownstreamCapacityBackpressurePolicy.

Changes:

  • Add retained-by-consumer byte reporting from DataIterator.materialize() and propagate it through StreamingExecutor / ResourceManager.
  • Extend StreamSplitDataIterator / SplitCoordinator to report retained bytes via the existing per-bundle get() RPC path.
  • Subtract retained-consumer bytes from output-pressure computation only when the operator has no eligible downstream operator, and add tests covering the new reporting and policy behavior.
File summaries
File Description
python/ray/data/iterator.py Report cumulative retained bytes during materialize() and reset at the end of the collection.
python/ray/data/_internal/iterator/stream_split_iterator.py Store retained bytes client-side and send them to the SplitCoordinator on the next get; coordinator aggregates and reports totals to the executor.
python/ray/data/_internal/execution/streaming_executor.py Add a pass-through setter to forward retained-consumer bytes to the resource manager.
python/ray/data/_internal/execution/resource_manager.py Track retained-consumer bytes and expose getter/setter APIs.
python/ray/data/_internal/execution/backpressure_policy/downstream_capacity_backpressure_policy.py Subtract retained-consumer bytes from output-size only for ops with no eligible downstream when computing output pressure.
python/ray/data/tests/test_downstream_capacity_backpressure_policy.py Update mocks to include retained-consumer bytes and add coverage for the subtraction behavior.
python/ray/data/tests/test_iterator.py Add a test asserting materialize() reports monotonically increasing retained bytes and resets to zero.
python/ray/data/tests/test_split.py Add a streaming-split test that validates retained bytes are reported (non-zero peak) and cleared at completion.
Review details
  • Files reviewed: 8/8 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Signed-off-by: Sirui Huang <[email protected]>
@rayhhome rayhhome changed the title [Data] Exclude Materialized Data from Downstream Capacity Backpressure [Data] Count materialized data as downstream capacity Sep 17, 2026
@rayhhome rayhhome changed the title [Data] Count materialized data as downstream capacity [Data] Count Materialized Data as Downstream Capacity Sep 17, 2026
Comment thread python/ray/data/iterator.py Outdated

ref_bundles_iter, stats, _ = self._to_ref_bundle_iterator()
ref_bundles = list(ref_bundles_iter)
ref_bundles_iter, stats, executor = self._to_ref_bundle_iterator()

@justinvyu justinvyu Sep 18, 2026 •

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.

Can we just increment stats.iter_prefetched_bytes directly so we don't need to introduce a new concept? iter batches and materialized blocks both count as "prefetched bytes on the consumer."

stats.iter_prefetched_bytes += block_bytes

@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 452219f. Configure here.

Comment thread python/ray/data/iterator.py Outdated
ref_bundles.append(ref_bundle)
finally:
# Nothing else on this path resets the field.
stats.iter_prefetched_bytes = 0

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Local materialize drops capacity reports

Medium Severity

Incrementing iter_prefetched_bytes during materialize() only reaches the resource manager on the streaming-split path, where gen_blocks already reads that field. A regular DataIterator has no such reader, and the executor that previously received set_external_consumer_bytes is now discarded, so held blocks still look like producer backlog and can throttle the producer the same way this change is meant to prevent.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 452219f. Configure here.

Comment thread python/ray/data/iterator.py Outdated
stats.iter_prefetched_bytes += ref_bundle.size_bytes()
ref_bundles.append(ref_bundle)
finally:
stats.iter_prefetched_bytes = 0

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.

Technically the prefetched bytes stick around while the MaterializedDataset object lives. I think it shouldn't matter but semantically resetting this to zero while the ref_bundles are still around is slightly misleading.

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.

This should also make the test simpler

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.

Double-checked the logic; I think I mistook how to scope MaterializedDataset's accumulated bytes. I've removed the finally block so that we now keep the total number of bytes materialized.

…finally block which reset prefecteched bytes

Signed-off-by: Sirui Huang <[email protected]>

@justinvyu justinvyu 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!

Comment on lines +130 to +132
# The prefetched bytes stat should not accumulate across different executions
# because this stat is used by the StreamingExecutor for backpressure decisions.
self._iter_stats.iter_prefetched_bytes = 0

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.

Note for a followup: the reason we need to reset this value is that iter_prefetched_bytes is the only property of this stats object that's actually read by the executor to make backpressure decisions (downstream capacity backpressure)

If this property kept accumulating across each execution, then every execution after the first one would use an incorrect value to make backpressure decisions.

Another discrepancy: the stats object is reused across executions in the streaming split iterator case, but reset on each execution for a non streaming split iterator.

We may want to consider clearing/resetting the stats object on each execution for the streaming split case too. Or, remove this special case from the stats object.

@justinvyu
justinvyu enabled auto-merge (squash) September 22, 2026 00:27
@justinvyu
justinvyu merged commit b18315e into ray-project:master Sep 22, 2026
7 checks passed
elliot-barn added a commit that referenced this pull request Sep 22, 2026
…acity (#66226) (#66371)

Cherry-pick of #66226 (master commit
b18315e) onto releases/2.59.0.

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

Signed-off-by: Sirui Huang <[email protected]>
Signed-off-by: elliot-barn <[email protected]>
Co-authored-by: HFFuture <[email protected]>
@rayhhome
rayhhome deleted the materialized-and-backpressured branch September 22, 2026 16:30
owenowenisme pushed a commit to owenowenisme/ray that referenced this pull request Sep 23, 2026
)

`lightgbm_train_batch_inference_benchmark_100G` times out at 600s with
`ReadParquet` pinned at `Tasks: 1
[backpressured:tasks(DownstreamCapacity)]`, turning a ~40s read into
400-560s.

Since ray-project#64456 the resource manager tracks object store memory with
`BlockRefCounter`, which counts a block until every reference to it is
gone. The train loop calls `materialize()` on its shard
(`train_batch_inference_benchmark.py:70`), so those blocks stay in the
producing operator's ref-counted output and
`DownstreamCapacityBackpressurePolicy` reads them as output waiting to
be consumed. The ratio only grows, and the producer is throttled for
memory it cannot release by slowing down.

The consumer now reports what it holds, and the policy counts it as
downstream capacity rather than as output waiting to be consumed.
Accounting is unchanged; those bytes really are in the object store and
still count toward every budget.

---------

Signed-off-by: Sirui Huang <[email protected]>
nadongjun pushed a commit to nadongjun/ray that referenced this pull request Oct 6, 2026
)

`lightgbm_train_batch_inference_benchmark_100G` times out at 600s with
`ReadParquet` pinned at `Tasks: 1
[backpressured:tasks(DownstreamCapacity)]`, turning a ~40s read into
400-560s.

Since ray-project#64456 the resource manager tracks object store memory with
`BlockRefCounter`, which counts a block until every reference to it is
gone. The train loop calls `materialize()` on its shard
(`train_batch_inference_benchmark.py:70`), so those blocks stay in the
producing operator's ref-counted output and
`DownstreamCapacityBackpressurePolicy` reads them as output waiting to
be consumed. The ratio only grows, and the producer is throttled for
memory it cannot release by slowing down.

The consumer now reports what it holds, and the policy counts it as
downstream capacity rather than as output waiting to be consumed.
Accounting is unchanged; those bytes really are in the object store and
still count toward every budget.

---------

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

Labels

data Ray Data-related issues go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants