[FIX] Parallelise per-document chunk listing in S3ChunkDownloader - #420
Open
noel-improv wants to merge 3 commits into
Open
[FIX] Parallelise per-document chunk listing in S3ChunkDownloader#420noel-improv wants to merge 3 commits into
noel-improv wants to merge 3 commits into
Conversation
…kDownloader
S3ChunkDownloader.download listed each source document's chunks with a separate
list_objects_v2 call issued one at a time on the main thread. On the 5,000-doc
WikiHow benchmark this listing took 255.7s of the 578s S3 readback; the other
~322s is the per-chunk downloads, which were already threaded and are unchanged
here.
Run the per-document list calls on a thread pool sized by
extraction_num_threads_per_worker (the setting the downloads already use).
executor.map preserves input order, so documents still yield in prefix order and
the listings prefetch while each document's chunks download. Listing and
downloading use separate pools. Also read Contents with .get('Contents', []) so
an empty prefix returns no keys instead of raising KeyError.
Measured against the same collection, listing only: 255.7s serial -> 16.5s at 16
threads, 8.3s at 32, 4.4s at 64. Listing is I/O-bound (no CPU, trivial memory),
so raising extraction_num_threads_per_worker above its default of 4 compounds
the gain. This addresses the listing half of readback; the download half is a
separate change (store an S3 reference in the graph instead of the chunk).
Tests: order preservation across documents, and a Barrier-based concurrency test
that passes on the parallel implementation and fails (BrokenBarrierError) on the
serial one.
… in S3ChunkDownloader The parallel listing used executor.map, which submits a listing task for every document up front and buffers each completed chunk-key list until the serial download consumer reaches it, so peak memory scaled with the whole collection instead of one document. Replace map with an explicit sliding window: at most extraction_num_threads_per_worker documents' listings are in flight, refilled one-per-consumed, so memory tracks the window. Each document's chunk downloads are dispatched onto a shared download executor as soon as its listing completes, so downloads for consecutive documents overlap. Reuse the collection-level paginator instead of rebuilding it per prefix. Document order is unchanged. Add a regression test asserting the window stays bounded when the consumer stalls on the first document.
noel-improv
marked this pull request as ready for review
July 23, 2026 23:23
Eager download dispatch in _list_and_dispatch submitted every chunk's download as soon as a document was listed, so the sliding window bounded listings but not downloaded payloads: up to num_threads look-ahead documents' chunk data was resident at once (OOM risk), and abandoning the generator early still drained those downloads. Split listing from downloading: list chunk keys on the bounded window, download only the current document's chunks before yielding. Restores the one-document memory bound and no-work-on-early-stop, keeps listing parallelism. Rework the window test to drive the generator from a consumer thread (removes the dead 10s-timeout handshake) and add a guard that fails on eager dispatch: no later-document download starts while stalled on doc 0.
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.
Description
S3ChunkDownloader.downloadreads extracted documents back from S3. It lists each document's chunks with a separatelist_objects_v2call, and those calls ran one at a time on the main thread. This runs them on a thread pool instead, so the listing no longer serialises. Documents still yield in the same order.Changes
extraction_num_threads_per_worker(the setting the chunk downloads already use). Documents yield in prefix order. Listing and downloading use separate pools so listings prefetch while the current document downloads.extraction_num_threads_per_workerdocuments in flight, refilled one per document consumed, so listing prefetches ahead without reading the whole collection.chunk_page.get('Contents', [])instead ofchunk_page['Contents'], so a prefix that lists empty returns no keys instead of raisingKeyError.Barrier-based test that the listing runs concurrently, a bounded-window test driven from a consumer thread, and a guard that no later document's chunks download while the consumer is stalled on the first document.Problem
Reading extracted documents back from S3 was slow. On a 5,000-document collection (16,536 chunk objects) the readback took 578s against 3s from local disk. The per-document listing accounted for 255.7s of that, running as 5,000 sequential round-trips on the main thread while the downloads already used a thread pool.
Related issue (if any): #
Testing
pytest)169 tests in
tests/unit/indexing/load/pass, including the four added here. The concurrency test fails withBrokenBarrierErroragainst serial listing, and the lazy-dispatch guard fails against eager download dispatch (later documents' chunks download while the consumer is stalled on document 0), so they guard the behaviour rather than passing vacuously.Measured against the same collection, listing isolated from downloads:
Listing is I/O-bound, so raising
extraction_num_threads_per_workerabove its default of 4 increases the gain; at the default it is smaller. This covers the listing part of readback. The chunk downloads (~322s) are unchanged.Checklist
Public behaviour is unchanged: the same
SourceDocumentobjects yield in the same order. No new files.By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.