Repository navigation
GH-40653: [C++][Dataset] Stop scanning when a scan iterator or reader is closed early - #55660
Open
prawalpokharel wants to merge 2 commits into
Open
prawalpokharel wants to merge 2 commits into
prawalpokharel wants to merge 2 commits into
Conversation
Without threads, closing or destroying the reader from Scanner::ToRecordBatchReader(), or destroying a ScanBatches() iterator early, reads every remaining batch. Close() does the same with threads. These tests fail on main.
…arly ScanBatches(), ScanBatchesUnordered() and ToRecordBatchReader() now return an iterator that stops the scan's plan when it is destroyed before the end. The reader's Close() stops the plan before draining it, as PlanReader::Close() in Acero does. Before, destroying the iterator without threads ran the rest of the scan, and Close() did so in both modes. The plan is held through a weak pointer, so it and the files it reads are still released at the end of the scan.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rationale for this change
Ending a dataset scan early can block until the rest of the dataset has been read (#40653):
ScannerRecordBatchReader::Close()reads the rest of the stream, with or without threads.SerialIterator, whose destructor runs the generator to the end. This affects dropping the reader fromToRecordBatchReader(), as well asHead(),TakeRows()and pyarrow'sto_batches(), which all stop reading theScanBatches()iterator early.Measured with one pyarrow build, swapping only
libarrow_dataset, on 4 GiB of Parquet in 32 files with 4 numeric columns (default readahead, 16-core Linux), stopping after 10 batches. Each cell is the median of 10 cycles in 2 processes: the time to close or drop, then the data read after the stop (rcharfrom/proc/self/io):reader.close()reader.close(),use_threads=Falsedel reader,use_threads=Falsedelato_batches(use_threads=False)generatordataset.head(5, use_threads=False), whole callWith 24 string columns instead,
reader.close()went from 1199 ms and 3.4 GiB to 76 ms and 127 MiB. Dropping a threaded reader was already fast, and neither it nor full reads changed measurably. In an interleaved A/B over 12 rounds, the median ratio of the time to read everything throughto_reader()with this PR to the time on main was 0.999 and 1.014 for two schemas (sign-flip permutation test, p = 0.98 and 0.87).What changes are included in this PR?
ScanBatches(),ScanBatchesUnordered()andToRecordBatchReader()now return an iterator that holds aweak_ptrto the scan'sExecPlanand callsStopProducing()when it is destroyed before the end. TheSerialIteratordrain is unchanged, so the iterator still waits for the plan to finish, which now happens right after the stop.ScannerRecordBatchReader::Close()stops the plan the same way and then reads until the plan has finished, treatingCancelledas the end, likePlanReader::Close()in Acero (GH-35935).Close()also releases the iterator, including on error, so the reader no longer holds the scan onceClose()returns.The iterator and the reader both hold the plan through a
weak_ptr, so neither keeps it, or its files, alive after the end of the scan (GH-41771). A scan that is read to the end behaves as before.Not changed: Acero's own
DeclarationToReader()reader, which still drains when it is destroyed withoutClose()and without threads.Are these changes tested?
Yes,
TestScanEarlyStopinscanner_test.cc, with and without threads: closing a reader, destroying a reader, destroying aScanBatches()iterator, and closing a reader after reading everything. On main, closing a reader early (in both modes) and destroying a reader or iterator without threads fail, because all 1000 batches are requested. The threaded destroy cases already pass on main. With the change, a serial scan requests 16 batches and a threaded one 8 to 13.On Linux x86_64:
arrow_datasetandarrow_aceroctest labels pass.Are there any user-facing changes?
ScanBatches()orScanBatchesUnordered()iterator early,Head()andTakeRows()no longer read the rest of the dataset.Close()returns. In the runs above, right after an early close or drop without threads, up to 4 dataset files were still open and medians of 391 to 643 MiB still allocated, all released within about 0.2 s (medians of 21 to 107 ms). Dropping a threaded reader on main already behaves this way.Close()used to return with nothing in flight only because it had read everything. The behaviour is documented inscanner.h.Close()no longer reports errors from data it doesn't read. If the scan was cancelled,Close()returns OK instead ofCancelled.Was AI used for this PR?
In accordance to the AI generation guidelines, please disclose below whether and how AI was used in this PR.
PR code and description written by:
Reviewed before submission by:
I used an AI coding assistant to help write the change and the tests and to run the measurements. I've reviewed all of it and will handle the review myself.