Skip to content

[Data] Release consumed block references during streaming batch iteration - #68626

Open
m4ru1 wants to merge 1 commit into
ray-project:masterfrom
m4ru1:fix/iter-batches-object-pins
Open

m4ru1 wants to merge 1 commit into
ray-project:masterfrom
m4ru1:fix/iter-batches-object-pins

Conversation

@m4ru1

@m4ru1 m4ru1 commented Nov 28, 2026

Copy link
Copy Markdown

Summary

Release consumed block and buffer references during streaming batch iteration so scaled-down actor-pool worker nodes can go idle, addressing #66903. This complements #67788, which garbage-collected executor lineage references.

Before

During streaming consumption such as ds.iter_batches(), Dataset._execute_to_iterator retains the peeked first RefBundle, and suspended batching generators hold driver references or plasma buffer pins to already-consumed blocks while waiting on a straggler task. Those pins keep object_store_memory > 0 and idle_duration_ms == 0 on worker nodes whose actors have already scaled down.

After

Pop the peeked first RefBundle on first yield in Dataset._execute_to_iterator, and clear yielded block and batch locals across the streaming batching and checkpointing generators before waiting for upstream input. Ordered and empty-slice paths also drop transferred buffer references, allowing emptied worker nodes to start their idle timer.

When prefetch_batches > 0, unyielded blocks within the configured lookahead window remain prefetched, while consumed blocks are unpinned as iteration advances.

flowchart LR
  subgraph Before
    B1["Dataset / BatchIterator (retains yielded refs)"] -. keeps primary pins .-> B2["Scaled-down worker: pinned, never idle"]
  end
  subgraph After
    A1["Dataset / BatchIterator (pops 1st bundle & clears locals)"] -. releases pins on yield .-> A2["Scaled-down worker: unpinned, goes idle"]
  end
  classDef bad fill:#ffcdd2,stroke:#c62828,color:#000
  classDef changed fill:#fff9c4,stroke:#f9a825,color:#000
  classDef good fill:#c8e6c9,stroke:#2e7d32,color:#000
  class B1,B2 bad
  class A1 changed
  class A2 good
Loading

Subsystems

Block / data format and API.

Testing

Local validation passed on Python 3.13.5 / Ray 2.60.1 and Python 3.11.15 / Ray 3.0.0.dev0 (a872863f2b, PyArrow 26.0.1) with the updated ray.data sources loaded on both driver and workers. Tests check first-bundle unpinning on initial yield, consumed-block release across prefetch_batches, zero-copy buffer safety under object-store spilling, multi-batch block splits, ordered/checkpointed/empty-slice paths, and worker idle_duration_ms > 0 after actor-pool scale-down while a straggler task is still running.

for test in \
  block_batching/test_iter_batches.py \
  block_batching/test_iter_batches_node_idle.py \
  block_batching/test_util.py \
  block_batching/test_block_batching.py
do
  python -m pytest -q "python/ray/data/tests/$test" || exit 1
done

Pre-commit hooks (including pyrefly-data) and pytest lint checks also passed.

AI assistance was used.

…tion

Drop the peeked first RefBundle after yielding it and clear consumed block and batch references before iterator stages wait for more input. This prevents the driver's iterator state from pinning outputs on scaled-down actor-pool worker nodes while a straggler is still running, allowing emptied nodes to start their idle timeout. Preserve the configured prefetch window for blocks not yet yielded to the consumer.

Related to ray-project#66903

Signed-off-by: mora <[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 focuses on optimizing memory management in Ray Data by explicitly deleting (del) blocks, batches, and reference bundles across various iterators and batching utilities. These changes ensure that zero-copy views and plasma buffers are released as early as possible, preventing memory from being pinned unnecessarily. Additionally, _execute_to_iterator is refactored to immediately release the first bundle, and new tests are added to verify block release behavior during stalled execution and node downscaling. The review feedback suggests a minor improvement in iter_batches.py to use next_ref_bundle.num_rows() instead of manually summing the rows of the blocks for better consistency and efficiency.

Comment on lines +545 to +547
current_window_size += sum(
entry.metadata.num_rows for entry in next_ref_bundle.blocks
)

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 consistency and efficiency, you can use next_ref_bundle.num_rows() instead of summing the rows manually via a generator expression. This is already done on line 527 in the same function.

                    current_window_size += next_ref_bundle.num_rows()

@ray-gardener ray-gardener Bot added data Ray Data-related issues community-contribution Contributed by the community labels Nov 28, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community data Ray Data-related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant