Skip to content

[FIX] Parallelise per-document chunk listing in S3ChunkDownloader - #420

Open
noel-improv wants to merge 3 commits into
awslabs:mainfrom
noel-improv:feat/parallelise-s3-doc-readback
Open

[FIX] Parallelise per-document chunk listing in S3ChunkDownloader#420
noel-improv wants to merge 3 commits into
awslabs:mainfrom
noel-improv:feat/parallelise-s3-doc-readback

Conversation

@noel-improv

@noel-improv noel-improv commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator

Description

S3ChunkDownloader.download reads extracted documents back from S3. It lists each document's chunks with a separate list_objects_v2 call, 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

  • List each document's chunk keys concurrently, sized by 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.
  • Bound the listing with a sliding window of at most extraction_num_threads_per_worker documents in flight, refilled one per document consumed, so listing prefetches ahead without reading the whole collection.
  • Download each document's chunks lazily, when the consumer reaches that document, rather than dispatching them at listing time. At most one document's chunk data is resident at a time (the same bound as the original serial code), and abandoning the generator early dispatches no downloads for unconsumed documents.
  • Reuse the collection-level paginator for the per-document listing calls instead of rebuilding it per prefix.
  • Read chunk_page.get('Contents', []) instead of chunk_page['Contents'], so a prefix that lists empty returns no keys instead of raising KeyError.
  • Tests: document and chunk order preservation, a 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

  • Unit tests added/updated
  • Integration tests added (as appropriate)
  • Existing tests pass (pytest)
  • Tested manually (describe below)

169 tests in tests/unit/indexing/load/ pass, including the four added here. The concurrency test fails with BrokenBarrierError against 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:

threads listing time
serial 255.7s
16 16.5s
32 8.3s
64 4.4s

Listing is I/O-bound, so raising extraction_num_threads_per_worker above 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

  • Code follows existing style and conventions
  • License headers present on new files
  • Documentation updated (if applicable)
  • No breaking changes (or clearly documented)

Public behaviour is unchanged: the same SourceDocument objects 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.

…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
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant