Repository navigation
Conversation
…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]>
There was a problem hiding this comment.
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.
| current_window_size += sum( | ||
| entry.metadata.num_rows for entry in next_ref_bundle.blocks | ||
| ) |
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_iteratorretains the peeked firstRefBundle, and suspended batching generators hold driver references or plasma buffer pins to already-consumed blocks while waiting on a straggler task. Those pins keepobject_store_memory > 0andidle_duration_ms == 0on worker nodes whose actors have already scaled down.After
Pop the peeked first
RefBundleon first yield inDataset._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 goodSubsystems
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 updatedray.datasources loaded on both driver and workers. Tests check first-bundle unpinning on initial yield, consumed-block release acrossprefetch_batches, zero-copy buffer safety under object-store spilling, multi-batch block splits, ordered/checkpointed/empty-slice paths, and workeridle_duration_ms > 0after actor-pool scale-down while a straggler task is still running.Pre-commit hooks (including
pyrefly-data) and pytest lint checks also passed.AI assistance was used.