Skip to content

Download in parallel ranges in get_file() #1323

Closed

Description

@laughingman7743

Part of #1315.

Summary

S3FileSystem.get_file() reads the object through a file one block at a time, so a download runs at one stream's throughput whatever max_workers is. In the s3fs comparison (#1318, recorded by #1321), s3fs's get_file(), which downloads max_concurrency ranges at a time, was 2.2 to 3.7 times faster with 8 MiB chunks and 5 to 9 times faster with its default 50 MiB chunks for objects of 256 MiB and larger.
AioS3FileSystem._get_file() runs the synchronous method in a thread and has the same limit.

Measurements

One object on one c8gn.2xlarge, MiB/s (median of five trials), PyAthena with 8 MiB blocks and 8 or 40 workers:

Object S3FileSystem AioS3FileSystem s3fs, 8 MiB chunks s3fs, 50 MiB chunks (default)
64 MiB 72 to 73 68 to 73 162 to 181 103 to 104
256 MiB 54 to 69 67 to 70 199 to 225 349 to 356
1 GiB 68 to 70 69 to 70 221 to 243 451 to 632

The same objects read with open().read(), which requests the ranges in parallel, ran at 440 to 600 MiB/s for 1 GiB.

Cause

get_file() opens the remote file and copies it with remote.read(remote.blocksize) in a loop. The file's bytes cache requests the next block with the first read and one block at a time afterwards, so the file's executor never runs more than one range request.

Questions

  1. Whether get_file() downloads ranges of block_size with up to max_workers requests at a time, as open().read() does, and writes them to the local file at their offsets or in order.
  2. Memory: how many ranges may be held before they are written; s3fs's download of 1 GiB with max_concurrency 40 peaked at 0.9 to 2.1 GiB, against 0.3 GiB for PyAthena's sequential read.
  3. Consistency of a multi-request download: keep the IfMatch condition on the ETag that open() sends today, so that an object replaced during a multi-request download fails instead of mixing two versions.
  4. Failure handling: no partial local file after a failed download, and callback progress with out-of-order completion.

Validation

Re-run the filesystem suite's get cases at small, medium, and large and compare them with the s3fs cases of the entry above.

Activity

  1. laughingman7743 commented on Nov 12, 2026

    @laughingman7743
    MemberAuthor

    Design: parallel ranges in get_file() (#1323)

    Code references are to master at d03e034. Step 5 of the #1332 design left "the shared range reader and parallel get_file" to this issue. It builds on the request pool (#1359), the fan-out primitive S3Executor.map() (#1367), the batch window and cooperative stop of #1344, and the drain of a cancelled aio call (#1395).

    1. Today

    • get_file() (s3.py:2854) opens the object with open(rpath, "rb", **kwargs) and copies it with remote.read(remote.blocksize) in a loop. With fsspec's default "bytes" cache, the first read requests two blocks, which _fetch_range() reads as two parallel ranges, and every later read extends the cache by one block (fsspec/caching.py:621). A download therefore runs one range request at a time after the first.
    • S3File._fetch_range() (s3.py:4431) splits a range larger than the block size into ranges of the block size and reads them with self._executor.map(), at most max_workers at once in the request pool, with the failure, stop, interrupt, and F5 behavior of map(). open().read() of a whole 1 GiB object is one such map, which ran at 440 to 600 MiB/s in the comparison.
    • _get_ranges() (s3.py:4502) returns the whole interval as one range when max_workers is 1.
    • open() constructs the file's data cache; cache_type="all" reads the whole object while opening.
    • S3FileTarget.resolve() (s3.py:3772) pins the reads of a file: they carry the version ID when one is given, or observed on a version-aware filesystem, and IfMatch on the ETag that it looked up, also with a version, unless the caller gave IfMatch. Its lookup sends the parameters that authorize the access, such as SSE-C. _open() and S3File.__init__() prepare its arguments: the defaults of block_size and max_workers, the check that max_workers is positive, version_id and size, and the request parameters, in the order of precedence: the filesystem's s3_additional_kwargs, the call's s3_additional_kwargs, and the call's other parameters. fsspec's open() applies compression by wrapping the file in a decompressor, which has no size or blocksize, so today's get_file(..., compression=...) fails with AttributeError after truncating the destination; fsspec's default get_file() uses getattr(f1, "size", None) and works.
    • Since Run the batch operations of S3FileSystem concurrently (GH-1322) #1344, a range read checks the enclosing stop scopes when it starts (_get_range), and get_file() checks before each local write. In a synchronous get(), each file gets max_workers = max(1, pool // n), or the caller's smaller value (O2).
    • AioS3FileSystem._get_file() runs S3FileSystem.get_file() in a thread of the filesystem, whose pool has max_workers threads. AioS3FileSystem._get() runs fsspec's _get(), which passes no per-file window, so each of its concurrent downloads gets the filesystem's max_workers; at most max_workers of them run at once, in the threads.

    2. Options for Q1 (how ranges are requested and written)

    All options split the object into ranges of block_size, whatever max_workers is (not _get_ranges(), which returns one range for one worker), so that a body never exceeds a block. All of them resolve the object with S3FileTarget.resolve() instead of opening a file, so that no data cache is constructed. The argument handling of _open() and S3File.__init__() described in section 1 moves into one helper that open() and get_file() share, so that the defaults, the check, version_id, size, and the precedence of the request parameters stay those of open(); cache_type and cache_options are accepted and unused. With compression, get_file() keeps a sequential path through open(), which decompresses the object, fixed to read blocks of the resolved block size and to give the progress callback an unknown size, as fsspec's default get_file() does; on that path the file's data cache is constructed as with open(), and the bounds of section 3 do not apply. Each range checks the enclosing stop scopes when it starts, as S3File._get_range() does, through a range read that both share, since S3Core.get_object() does not and map() only checks the stop of its own batch.

    A. Waves through map(), written body by body. get_file() maps the range read over window ranges at a time with S3Executor.map() on a scope of the request pool, then writes the bodies of the wave in order, dropping each after it is written, and maps the next wave once all of them are written. While a wave is fetched, the failure, stop, interrupt, and F5 behavior is that of map(). While it is written, a failure of a write or of the progress callback, or an interrupt, propagates after the bodies are dropped: the list of outcomes and the current body are cleared, and the completed frames of its traceback and of the tracebacks of its chained exceptions (__cause__, __context__, each once) are cleared (traceback.clear_frames), so that a file-like that wraps a failure of its own does not keep the body either. (cat() drops its dictionary on a failure since #1344, without the chained tracebacks.) No new executor code. Cost: a barrier per wave, since the slowest range of a wave idles the other slots until the wave has been written. At most window bodies of block_size are held.

    B. A sliding window inside the primitive. A new ordered operation of S3Executor keeps at most window items submitted and not yet consumed, and calls a consumer, in the calling thread and in item order, with each outcome, refilling a slot once the consumer has returned. No wave barrier, although a stalled head range still holds the others until it ends (head-of-line). The consumer runs inside the operation, not after a suspended generator, so its errors and interrupts stop and drain the batch like a failed call. It needs a streaming state of its own, not _S3Batch: a re-armable signal instead of the one-shot _ended, consumed futures released before a refill, the first failure by completion order also for a range behind the head, bounded tracking of submitted items, and F5 for the bodies held by the consumer's frames. At most window bodies are held.

    C. Completion-order writes at offsets. The consumer (one thread) seeks and writes each body as it completes, for a seekable destination; a non-seekable outfile or file-like needs A or B as a second path. The progress callback can count written bytes, which stays monotonic. Holds at most window bodies, like A and B, and has no head-of-line wait, but a failure leaves gaps, and seekable file-likes differ in behavior at offsets past the end.

    Recommendation: A (decided). It removes the one-request bottleneck with the existing primitive and its reviewed lifecycle. Whether the wave barrier matters is measured in step 5, with open().read()'s single map (no barrier) as the reference; B is the follow-up if it does.

    3. Q2 (memory)

    A download holds at most window × block_size bytes of bodies (a budget for bodies, not a prediction of the process RSS). window is the file's max_workers, capped at the size of the request pool, since no more ranges run at once and a larger wave would only hold the bodies of the ranges that waited: by default the filesystem's, 10 with the default client pool, so 50 MiB with the default 5 MiB blocks; 320 MiB with the comparison's 8 MiB blocks and 40 workers, against 0.9 to 2.1 GiB for s3fs.

    In a batch, the bodies of all concurrent downloads count: c × window × block_size for c downloads at once. The synchronous get() sets window = max(1, pool // c), where c is the smaller of its batch_size and the number of files, or keeps the caller's smaller max_workers (O2): about pool × block_size while c is at most the pool, and c × block_size above it, since the window is at least 1.

    AioS3FileSystem._get() gets the same rule. It pairs the paths itself, as it already does for a version path, and passes each _get_file() max_workers = max(1, pool // c), or the caller's smaller value, where c is the smallest of the number of files, fsspec's batch size, and the threads of the filesystem (max_workers), which bound the downloads that run at once. fsspec's batch size is resolved by its rules (fsspec/asyn.py:285, :809): the batch_size argument, even an explicit None, which takes fsspec's default, else the filesystem's batch_size; fsspec's default is gather_batch_size of its configuration, else an eighth of the soft limit of open files, else 128, and -1 runs every file at once. Without the rule, at most pool downloads of pool ranges each run at once: pool² × block_size, 500 MiB with the defaults.

    4. Q3 (consistency)

    Every range carries the version ID and the IfMatch that S3FileTarget.resolve() returns, with the parameters of the call, as a read of open() does today. A download pinned to an immutable version ID reads that version whatever replaces the key. Otherwise, including a null version, which a write to a bucket with suspended versioning overwrites (s3.py:580), a range sent after the ETag changed fails with the OSError (EINVAL) of the failed precondition (s3_errors.py:95), instead of mixing two objects. A caller's IfMatch is sent as given. No change of behavior.

    5. Q4 (failures and progress)

    • Partial file. Today a failed download leaves lpath truncated and partly written, as fsspec's default get_file() does (fsspec/spec.py:991). Options:
      • (i) Keep it, and document it.
      • (ii) Download into a temporary file in the destination directory and os.replace() it onto lpath on success; the temporary file is removed after a failure, and the original file is kept. Changes for callers: a symlink at lpath is replaced by a regular file instead of having its target written; hard links to the old file keep the old contents; the new file gets default permissions instead of those of the old one; a temporary file is visible in the directory during the download; on Windows, os.replace() can fail while another process has the old file open without sharing it for deletion.
      • Removing lpath after a failure is not an option: it would delete a symlink and leave its target truncated, and could delete a file that another process put at lpath meanwhile.
        Recommendation: (i) (decided). It keeps the semantics of fsspec and of today, and is independent of the throughput change; (ii) can be a separate issue if wanted.
    • Progress. callback.set_size() gets the object size, and relative_update() runs as each body is written, in order, so it is monotonic, as today.
    • Stop and interrupt. A stopped download sends no further range and writes nothing more (Run the batch operations of S3FileSystem concurrently (GH-1322) #1344); a failure, an interrupt, or an aio cancellation (through the drain of run(), Align AioS3FileSystem with S3FileSystem for cancellation and arguments (GH-1337) #1395) waits for the running ranges of the wave before raising. The bodies of a wave are dropped before an error propagates, so that no traceback keeps them (F5).

    6. Scope

    • S3FileSystem.get_file(), and through it get() and AioS3FileSystem._get_file(); the per-file window and pairing of AioS3FileSystem._get(); the argument helper shared with open() and the range read shared with S3File.
    • Not here: B or C; _fetch_range() and AioS3StreamedFile reads; a larger default block size for downloads (s3fs's 50 MiB chunks were faster for 1 GiB objects); the partial-file option (ii). The first three are for the step 5 measurement.

    7. Verification

    • At most window ranges in flight, and at most window bodies held, including with a slow writer; waves refill only after their bodies are written.
    • Block-sized ranges for every max_workers, including 1 (a multi-block object, also through a synchronous batch get() whose window is 1).
    • Content and order to a path and to a file-like, for an empty object, less than a block, an exact multiple, and a remainder; no data cache constructed, including with cache_type="all".
    • A failed range: no further range starts, the running ones are waited for, the error is raised. A failed write and a failed progress callback, while a wave is written, including a write that wraps its failure in another exception, and an interrupt at the handoff of a wave: the bodies are not kept by a retained exception, with the cyclic GC disabled.
    • An interrupt during a wave and during a write; an aio cancellation while ranges wait behind other requests of the pool (no queued range sends its request); a stop between waves.
    • Consistency: an unversioned download, and one of a null version, whose later range is held until the object is replaced, fails; one pinned to an immutable version succeeds; a caller's IfMatch, the SSE-C parameters, version_id, and size reach every range as with open(), with the precedence of section 1.
    • The progress callback values, in order.
    • The window of each file in synchronous and aio batch downloads, for one file and for more files than the pool, with fsspec's batch size given, None, and -1; a caller's smaller max_workers kept, and a larger one capped at the pool, also for a single download, batch_size=1, and outfile.
    • compression: the object is decompressed into the destination, with progress of an unknown size (it failed with AttributeError before).
    • The existing AWS integration tests of get/get_file, and the filesystem suite's get cases at small, medium, and large in the step 5 measurement.

    8. Decisions

    Decided by the maintainer: Q1 is option A, waves through map() written body by body, with B as the follow-up if the step 5 measurement shows that the wave barrier matters; Q4 is (i), a failed download leaves the partly written file as today, documented. Q2 and Q3 follow from sections 3 and 4.

    9. Review record

    Codex (static, against d03e034), three rounds of 12, 6, and 3 findings, folded in: the actual reads of fsspec's "bytes" cache; the data cache that open() constructs (S3FileTarget.resolve() instead); block-sized ranges for one worker; the per-file window and effective concurrency of AioS3FileSystem._get(); the cap of the window at the pool, also for a single download; the argument handling of open() (precedence, size, defaults, check) in a shared helper; the stop check when each range starts; the version and IfMatch of a download, including null versions; the bodies held while a wave is written, including chained exceptions; the broken compression path; the trade-offs of B and C, and the bounded-wave option itself; and the risks of removing lpath after a failure, which is not an option.

  2. laughingman7743 commented on Nov 16, 2026

    @laughingman7743
    MemberAuthor

    The re-measurement of #1332 ran at 1d9e5039 on one c8gn.2xlarge Spot host; the history entry is in #1420.

    • Throughput. get of one object with 8 MiB ranges:

      API Throughput (MiB/s)
      synchronous S3FileSystem 311 to 613
      AioS3FileSystem 232 to 606
      s3fs, on the same host 246 to 546
      • s3fs ran at 0.77 to 1.06 times PyAthena's throughput in the same case.
      • Before this issue, the 2026-10-23 entry measured 54 to 73 MiB/s for PyAthena, when get_file() read one block at a time.
    • Memory. Peak RSS of the 1 GiB download:

      Workers PyAthena (MiB) s3fs (MiB)
      8 336 to 337 411 to 417
      40 906 to 938 917 to 1,143
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions