Repository navigation
Description
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
- Whether
get_file()downloads ranges ofblock_sizewith up tomax_workersrequests at a time, asopen().read()does, and writes them to the local file at their offsets or in order. - Memory: how many ranges may be held before they are written; s3fs's download of 1 GiB with
max_concurrency40 peaked at 0.9 to 2.1 GiB, against 0.3 GiB for PyAthena's sequential read. - Consistency of a multi-request download: keep the
IfMatchcondition on the ETag thatopen()sends today, so that an object replaced during a multi-request download fails instead of mixing two versions. - Failure handling: no partial local file after a failed download, and
callbackprogress 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
laughingman7743 commentedon Nov 12, 2026
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 primitiveS3Executor.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 withopen(rpath, "rb", **kwargs)and copies it withremote.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 withself._executor.map(), at mostmax_workersat once in the request pool, with the failure, stop, interrupt, and F5 behavior ofmap().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 whenmax_workersis 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, andIfMatchon the ETag that it looked up, also with a version, unless the caller gaveIfMatch. Its lookup sends the parameters that authorize the access, such as SSE-C._open()andS3File.__init__()prepare its arguments: the defaults ofblock_sizeandmax_workers, the check thatmax_workersis positive,version_idandsize, and the request parameters, in the order of precedence: the filesystem'ss3_additional_kwargs, the call'ss3_additional_kwargs, and the call's other parameters. fsspec'sopen()appliescompressionby wrapping the file in a decompressor, which has nosizeorblocksize, so today'sget_file(..., compression=...)fails withAttributeErrorafter truncating the destination; fsspec's defaultget_file()usesgetattr(f1, "size", None)and works._get_range), andget_file()checks before each local write. In a synchronousget(), each file getsmax_workers = max(1, pool // n), or the caller's smaller value (O2).AioS3FileSystem._get_file()runsS3FileSystem.get_file()in a thread of the filesystem, whose pool hasmax_workersthreads.AioS3FileSystem._get()runs fsspec's_get(), which passes no per-file window, so each of its concurrent downloads gets the filesystem'smax_workers; at mostmax_workersof 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, whatevermax_workersis (not_get_ranges(), which returns one range for one worker), so that a body never exceeds a block. All of them resolve the object withS3FileTarget.resolve()instead of opening a file, so that no data cache is constructed. The argument handling of_open()andS3File.__init__()described in section 1 moves into one helper thatopen()andget_file()share, so that the defaults, the check,version_id,size, and the precedence of the request parameters stay those ofopen();cache_typeandcache_optionsare accepted and unused. Withcompression,get_file()keeps a sequential path throughopen(), 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 defaultget_file()does; on that path the file's data cache is constructed as withopen(), and the bounds of section 3 do not apply. Each range checks the enclosing stop scopes when it starts, asS3File._get_range()does, through a range read that both share, sinceS3Core.get_object()does not andmap()only checks the stop of its own batch.A. Waves through
map(), written body by body.get_file()maps the range read overwindowranges at a time withS3Executor.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 ofmap(). 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 mostwindowbodies ofblock_sizeare held.B. A sliding window inside the primitive. A new ordered operation of
S3Executorkeeps at mostwindowitems 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 mostwindowbodies 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
outfileor file-like needs A or B as a second path. The progress callback can count written bytes, which stays monotonic. Holds at mostwindowbodies, 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_sizebytes of bodies (a budget for bodies, not a prediction of the process RSS).windowis the file'smax_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_sizeforcdownloads at once. The synchronousget()setswindow = max(1, pool // c), wherecis the smaller of itsbatch_sizeand the number of files, or keeps the caller's smallermax_workers(O2): aboutpool × block_sizewhilecis at most the pool, andc × block_sizeabove 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, wherecis 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): thebatch_sizeargument, even an explicitNone, which takes fsspec's default, else the filesystem'sbatch_size; fsspec's default isgather_batch_sizeof its configuration, else an eighth of the soft limit of open files, else 128, and-1runs every file at once. Without the rule, at mostpooldownloads ofpoolranges each run at once:pool² × block_size, 500 MiB with the defaults.4. Q3 (consistency)
Every range carries the version ID and the
IfMatchthatS3FileTarget.resolve()returns, with the parameters of the call, as a read ofopen()does today. A download pinned to an immutable version ID reads that version whatever replaces the key. Otherwise, including anullversion, which a write to a bucket with suspended versioning overwrites (s3.py:580), a range sent after the ETag changed fails with theOSError(EINVAL) of the failed precondition (s3_errors.py:95), instead of mixing two objects. A caller'sIfMatchis sent as given. No change of behavior.5. Q4 (failures and progress)
lpathtruncated and partly written, as fsspec's defaultget_file()does (fsspec/spec.py:991). Options:os.replace()it ontolpathon success; the temporary file is removed after a failure, and the original file is kept. Changes for callers: a symlink atlpathis 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.lpathafter 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 atlpathmeanwhile.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.
callback.set_size()gets the object size, andrelative_update()runs as each body is written, in order, so it is monotonic, as today.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 itget()andAioS3FileSystem._get_file(); the per-file window and pairing ofAioS3FileSystem._get(); the argument helper shared withopen()and the range read shared withS3File._fetch_range()andAioS3StreamedFilereads; 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
windowranges in flight, and at mostwindowbodies held, including with a slow writer; waves refill only after their bodies are written.max_workers, including 1 (a multi-block object, also through a synchronous batchget()whose window is 1).cache_type="all".nullversion, whose later range is held until the object is replaced, fails; one pinned to an immutable version succeeds; a caller'sIfMatch, the SSE-C parameters,version_id, andsizereach every range as withopen(), with the precedence of section 1.None, and-1; a caller's smallermax_workerskept, and a larger one capped at the pool, also for a single download,batch_size=1, andoutfile.compression: the object is decompressed into the destination, with progress of an unknown size (it failed withAttributeErrorbefore).get/get_file, and the filesystem suite'sgetcases 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 thatopen()constructs (S3FileTarget.resolve()instead); block-sized ranges for one worker; the per-file window and effective concurrency ofAioS3FileSystem._get(); the cap of the window at the pool, also for a single download; the argument handling ofopen()(precedence,size, defaults, check) in a shared helper; the stop check when each range starts; the version andIfMatchof a download, includingnullversions; the bodies held while a wave is written, including chained exceptions; the brokencompressionpath; the trade-offs of B and C, and the bounded-wave option itself; and the risks of removinglpathafter a failure, which is not an option.Infer no codec for a bucket path in get_file() (GH-1323)
Address the independent review of parallel downloads (GH-1323)
Normalize the block size of decompressed downloads (GH-1323)
Fall back to a positive block size for decompressed downloads (GH-1323)
Download in parallel ranges in get_file() (GH-1323)
Infer no codec for a bucket path in get_file() (GH-1323)
Address the independent review of parallel downloads (GH-1323)
Normalize the block size of decompressed downloads (GH-1323)
Fall back to a positive block size for decompressed downloads (GH-1323)
Serve the keys of the aio download tests with serve_s3_keys() (GH-1323)
Merge pull request #1399 from pyathena-dev/feat/1323-parallel-get-file
laughingman7743 commentedon Nov 16, 2026
The re-measurement of #1332 ran at
1d9e5039on onec8gn.2xlargeSpot host; the history entry is in #1420.Throughput.
getof one object with 8 MiB ranges:S3FileSystemAioS3FileSystemget_file()read one block at a time.Memory. Peak RSS of the 1 GiB download: