Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@

All notable changes to this project will be documented in this file.

## Unreleased

### Added

- `ONEIO_S3_MAX_CONCURRENCY` - number of multipart parts uploaded in parallel
(default 4, capped at 16).
- `ONEIO_S3_REQUEST_TIMEOUT_SECS` - per-request timeout for S3 upload requests
(default 300, the previous hardcoded value).

### Changed

- S3 multipart uploads now run parts in parallel, reading each part's byte
range from a single open handle before uploading it. Previously parts were
uploaded one at a time, so a single TCP stream set the ceiling and one stalled part blocked
everything until the request timeout. Part retries, abort-on-failure and
part ordering are unchanged.
- Peak memory for a multipart upload is `concurrency * part_size` (4 x 8 MiB
with the defaults).
- Multipart parts are read from one open handle instead of reopening the path
in each worker, so a file replaced at the same path mid-upload can no longer
contribute bytes from two versions to a single object.

## v0.26.1 -- 2026-09-09

### Changed
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ Note: Async compression is limited to gz, bz, zstd. LZ4/XZ return `NotSupported`
use oneio::s3::*;

// Upload file to S3
// Files < 8MB use single PUT; larger files use multipart upload
// Files under the multipart threshold (5MB by default) use a single PUT;
// larger files are uploaded as parallel parts
s3_upload("my-bucket", "path/to/file.txt", "local/file.txt")?;

// Download file from S3
Expand Down Expand Up @@ -306,6 +307,8 @@ Optional environment variables for S3:
- `AWS_SESSION_TOKEN` - Temporary session token
- `ONEIO_S3_CHUNK_SIZE` - Multipart part size in bytes (default: 8MB)
- `ONEIO_S3_MULTIPART_THRESHOLD` - File size threshold for multipart upload (default: 5MB)
- `ONEIO_S3_MAX_CONCURRENCY` - Multipart parts uploaded in parallel (default: 4, max: 16)
- `ONEIO_S3_REQUEST_TIMEOUT_SECS` - Per-request timeout for upload requests (default: 300)

### Error Handling

Expand Down
100 changes: 100 additions & 0 deletions specs/04-s3-concurrent-multipart/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Spec: Concurrent S3 multipart upload

**Status**: Complete
**Author**: Mingwei Zhang
**Created**: 2026-10-23
**Target Branch**: `dev/s3-concurrent-multipart`

## 1. Overview

- Goal: make `oneio::s3_upload` upload multi-GB files at link speed instead of
one part at a time, and stop a single stalled part request from costing the
caller a 300 s timeout before anything is retried.
- Non-goals: async API, conditional writes, progress callbacks, changing
`s3_upload`'s signature or the `S3Config` struct.
- Success criteria:
- [x] 4.3 GB upload to Cloudflare R2 completes in well under the 6 minutes
the sequential path needs (measured 90 s with 4 workers, 420 s with 1)
- [x] part order in `CompleteMultipartUpload` is always part-number order
- [x] one failing part aborts the multipart upload and returns that error
- [x] `cargo fmt --check`, `cargo clippy --all-features -- -D warnings`,
`cargo test --all-features` all pass
- [x] no new dependency

## 2. Current State

`upload_multipart` uploads parts strictly sequentially, one 8 MiB part per
request, each with a hardcoded 300 s request timeout. Measured against
Cloudflare R2, one TCP stream caps at about 12 MiB/s while four concurrent
streams reach about 90 MiB/s, so the sequential loop leaves most of the link
idle. When a part request stalls,
nothing else is in flight, so the whole upload waits for the timeout.

## 3. Proposed Solution

- Keep the blocking design. Concurrency comes from `std::thread::scope`.
- N worker threads (default 4) share an `AtomicUsize` part counter and read
their part ranges from one open handle. Reads are serialized through that
handle's lock; the uploads that follow run in parallel.
- Each worker signs its own part URL and uploads with the existing retry
helper; the ETag is written into a pre-sized slot `Vec<Option<String>>` at the
part index, so the completion list is ordered by construction.
- On the first unrecoverable error a shared `AtomicBool` stops the other
workers at their next part boundary; after all workers join, the multipart
upload is aborted exactly once and the first error is returned.
- Tuning is read from the environment through cached `OnceLock`s, mirroring the
existing `s3_retry_config` pattern, so no public struct or signature changes:
- `ONEIO_S3_MAX_CONCURRENCY` (default 4, clamped to 1..=16)
- `ONEIO_S3_REQUEST_TIMEOUT_SECS` (default 300, unchanged behavior)
- Memory: peak per upload is `concurrency * part_size` (4 x 8 MiB default).

## 4. Implementation Plan

1. Add `s3_upload_tuning()` (concurrency + request timeout) next to
`s3_retry_config`; replace the `S3_UPLOAD_REQUEST_TIMEOUT` constant uses.
2. Rewrite `upload_multipart` around a scoped worker pool; extract
`collect_part_etags` so ordering and missing-part handling are unit-testable.
3. Unit tests: concurrency clamp, ETag ordering, missing part error.
4. Verify against R2 with a 4.3 GB local file.

## 5. Testing Strategy

- Unit: pure helpers only (no network).
- Integration: `tests/s3_integration.rs` multipart cases (5 MB, 10 MB, 80 MB)
already cover correctness against R2; they now exercise the concurrent path.
- Manual: 4.3 GB upload, compare wall clock with the sequential measurement
(420 s) and check the object size with a HEAD (both runs stored exactly
4,334,649,344 bytes).

## 6. Risks

- More sockets per upload: default 4, bounded, and only for multipart.
- A part failing late can leave other parts already uploaded: acceptable, the
upload is aborted and S3 bills parts only until then.
- `thread::scope` panics propagate: workers return `Result` instead of panicking
so the abort path always runs.
- Env-based tuning is cached: a test or embedding process cannot change it after
the first upload, same as the existing retry settings.

## 7. Decision Log

- 2026-10-23: kept the blocking API; concurrency via `thread::scope` rather than
adding tokio, because oneio's S3 path has no async runtime and pulling one in
would be a much larger change than the problem requires.
- 2026-10-23: tuned via environment rather than new `S3Config` fields, because
`S3Config` has public fields and adding one breaks struct literals.
- 2026-10-23: no per-part logging in the library; oneio has no logging facade in
library code today, so callers time the whole call instead.
- 2026-10-23: workers report failure and cancellation themselves through shared
state, because the coordinator joins them in creation order and would
otherwise keep uploading queued parts behind a failure.
- 2026-10-23: workers are started with `Builder::spawn_scoped`; a thread the OS
refuses is reported as an error so the multipart upload is still aborted,
instead of panicking out of `Scope::spawn` and leaving it behind.
- 2026-10-23: the source file is opened once and each part reads a range from
that handle under a lock. Opening the path in each worker could mix two
versions of the file into one object when a writer swaps the path atomically
between worker startups, and cloned handles would share a seek position.
- 2026-10-23: four workers by default, 16 as the ceiling; a failing part is
reported after the other workers stop at their next part boundary, so the
caller never waits for parts queued behind a failure.
13 changes: 9 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,15 @@ Optional:
- `AWS_SESSION_TOKEN` - Temporary session token
- `ONEIO_S3_CHUNK_SIZE` - Multipart part size in bytes (default: 8MB)
- `ONEIO_S3_MULTIPART_THRESHOLD` - File size threshold for multipart upload (default: 5MB)

R2 supports single PUT uploads up to 300 MiB. The default threshold of 5MB
(the S3 minimum part size) uses single-PUT for small files and multipart
for larger files where retry-per-part improves reliability.
- `ONEIO_S3_MAX_CONCURRENCY` - Multipart parts uploaded in parallel (default: 4, max: 16)
- `ONEIO_S3_REQUEST_TIMEOUT_SECS` - Per-request timeout for upload requests (default: 300)
- `ONEIO_S3_MAX_RETRIES` - Retry attempts after the initial request (default: 3)
- `ONEIO_S3_RETRY_BACKOFF_MS` - Initial retry backoff in ms, doubles each attempt (default: 1000)

The default threshold of 5MB (the S3 minimum part size) uses single PUT for
small files and multipart for larger ones, where parts are uploaded in
parallel and each part retries independently. Peak memory for a multipart
upload is `concurrency * part_size`.

# TLS and Corporate Proxies

Expand Down
Loading
Loading