Skip to content

perf(fileservice): avoid buffering S3 uploads for disk cache on 4.2 - #32783

Merged
XuPeng-SH merged 2 commits into
4.2-devfrom
codex/32751-s3-cache-replay-proto-42
Nov 15, 2026
Merged

XuPeng-SH merged 2 commits into
4.2-devfrom
codex/32751-s3-cache-replay-proto-42

Conversation

@XuPeng-SH

@XuPeng-SH XuPeng-SH commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Problem

When an object writer uses S3FS with disk-cache writes enabled, the upload is teed into a growing bytes.Buffer, which is then copied into the disk cache. The entire object is retained in a second Go heap buffer during the write. Buffer growth can allocate more than the object's size cumulatively.

Change

  • Object writers opt in through a cache policy that promises their prepared Data entries and owners remain stable until the synchronous Write returns. Ordinary FileService.Write calls and wrapper side effects are preserved.
  • For known-size, contiguous, exact-sized in-memory entries, S3FS reads the entries with a cursor for both upload and disk-cache fill. It replays only the prefix actually consumed by the upload. Streams, gaps, invalid sizes, disabled cache writes and callers without the policy retain the existing path.
  • On 4.2 the cache-copy reader implements WriterTo: it writes large spans directly and batches small entries using the existing 32 KiB I/O buffer pool. DiskCache avoids taking an unused second buffer when the source already implements WriterTo. This preserves the prior io.Copy source dispatch while avoiding extra small disk writes.

This adds a policy bit visible to custom file-service implementations. In-tree consumers use policy masks, and tests exercise forwarding wrappers; downstream implementations that compare the complete policy value should review the new bit. No SQL type or object format change is intended.

Evidence and limits

  • On a local disk-backed S3FS, a 4 MiB, two-entry write allocated 8,395,368 B → 39,096 B in Go TotalAlloc during Write. A 160 KiB write split into 10,000 entries allocated 2,455,504 B → 38,816 B. Cache contents and disk-cache hits were verified. Across earlier repeated runs, the 4 MiB object plus its cache copy used 129 write syscalls and 8,388,608 bytes on both versions; the final run saw one additional 8-byte background write on base. These are cumulative allocations and process syscall counts, not peak RSS or physical-disk traffic.
  • A single-thread in-memory benchmark draining the actual reader/capture components to a discard sink measured 160 KiB / 10,000 entries at a median 472 µs → 141 µs over three repetitions. It excludes disk, network and most file-service overhead. Four paired no-op Sync benchmarks were approximately 20.5 ns on both versions with no allocations; the isolated buffer-constructor median was 4.247 → 4.369 µs, with unchanged allocations. These microbenchmarks do not establish end-to-end latency.
  • A local 1-CN/1-TN/1-log comparison used a disk-backed S3FS, a 64 MiB memory cache, a 128 MiB disk cache, and a test-only 512 KiB CN spill threshold. Each side loaded 32 rows totaling 5.94 MiB, then altered a column; all row hashes matched after both phases and each phase performed six S3 writes. Sampled LOAD RSS rise from phase start was 80.94 MiB → 68.76 MiB, but shared-memory-cache maxima differed (5.95 MiB on base and 11.89 MiB on head), so the RSS difference cannot be attributed solely to this change. In one sampled cumulative heap profile, bytes.growSlice under S3FS.write accounted for 37.40 MB on base and was not sampled on head.
  • Local-disk fragmentation measurements varied from milliseconds to hundreds of milliseconds on both versions. The evidence does not establish an end-to-end latency improvement or rule out a regression.

This optimization applies only when S3FS disk-cache writes are enabled. The production incident's cache configuration is not established here, a real S3 service and production network conditions were not exercised, and this change alone does not establish that a 251 MiB row can be processed safely.

Validation

  • Focused fileservice replay, stream, cancellation, partial-upload, cache-failure, wrapper and parallel-upload tests: -race -count=10.
  • AWS SDK multipart upload to a local mock HTTP server, with two parts crossing entry boundaries and a cancellation that must not publish a cache entry: -race -count=3.
  • WriterTo tests cover large and fragmented vectors, exact prefixes, partial/short writes, errors and cancellation.
  • Existing TestDiskCache*, TestS3FS and subpath tests under -race.
  • Complete pkg/objectio tests, normal and -race; includes object readback after arena release and a file-exists retry. The legacy test's hard-coded temporary root was redirected to a task-owned directory for these local runs; this fixture adjustment is not part of the patch.
  • make static-check-selected STATIC_CHECK_PACKAGES='./pkg/fileservice ./pkg/objectio'.
  • Local S3FS/cache content checks and the small CN comparison described above.

Part of #32751. Counterpart: #32782.

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@mergify

mergify Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

This pull request is merged.

@XuPeng-SH

XuPeng-SH commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor Author

mo-bot review · mo-bot-local · done

Commit: 8eeb65af154acc51795f7343c6bc78852560a87c

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mo-bot review · mo-bot-local · review

Commit: 8eeb65af154acc51795f7343c6bc78852560a87c

Assessment: approve

Reviewed all 12 changed files, synchronous write/cache ownership, object-writer retries and buffer lifetimes, wrappers, and multipart consumers. The stable-data cursor removes full-object capture; bounded WriterTo coalescing reuses the existing cache buffer pool and avoids fragmented-entry syscall growth without new asynchronous state. Fallback and failure semantics remain intact. No actionable correctness or design defect remains. All discussion entries are informational; #32751's broader integration work remains open.

Model: gpt-6.1-sol / max

Session: 01a1a004-90c3-73b1-acc9-3d4636452003 · slot 0 · generation 0

Validation:

  • Preflight passed: private /tmp and loopback; preflight.stdout and checks/1794776695167994337-2.json.

  • Native prerequisites matched all 170 frozen inputs and 71 retained artifact hashes; native-reuse.json and checks/1794776714442176366-2.json. Go 1.26.6, Linux/amd64, assigned caches and supplied test-exec used.

  • go build and go vet passed for ./pkg/fileservice ./pkg/objectio; build-vet.stdout/stderr and checks/1794776885848719464-2.json.

  • All 20 added test roots passed in normal execution and ten focused race repetitions, including actual AWS SDK multipart against local HTTP, cancellation, short/error uploads, streams, wrappers, concurrent writes, WriterTo boundaries, retry and arena-release readback; owning-normal.jsonl, focused-race.jsonl, checks/1794777027539205523-2.json and validation-summary.json.

  • Complete normal objectio passed. The complete normal fileservice run failed only TestMinioSDK/file_service/basic with Access Denied; the exact MinIO test then passed with explicit local fixture credentials, normal and race. Logs owning-normal.jsonl, minio-fixture-{normal,race}.jsonl; receipts 1794777050300151223-2, 1794777090768918446-2 and 1794777156071290818-2.

  • Owning fileservice and objectio race runs passed with the separately validated MinIO fixture excluded from that command; owning-race.jsonl and checks/1794777244313395312-2.json.

  • Review audit passed: frozen patch matches, source remains clean, gofmt clean, every discussion key covered once, all added test roots selected, and claimed execution reconciled with source/command/exit receipts; review-audit.stdout, validation-summary.json and checks/1794777312057018786-2.json.

Limitations:

  • Frozen CI includes pending checks; skipped external/optional tests are not treated as passes. Validation used local disk S3, local MinIO and mock HTTP, not a real S3 service or production network.

  • PR allocation, syscall and small-CN measurements are author-reported; raw performance artifacts are absent from local_validation. Source confirms removal of the capture buffer, but this review does not establish end-to-end latency, peak-RSS benefit or safe processing of a 251 MiB row.

  • golangci-lint is unavailable locally; gofmt and owning-package vet passed.

XuPeng-SH added a commit that referenced this pull request Nov 15, 2026
## Problem

When an object writer uses S3FS with disk-cache writes enabled, the
upload is currently teed into a growing `bytes.Buffer`, which is then
copied into the disk cache. The entire object is retained in a second Go
heap buffer during the write. Buffer growth can allocate more than the
object's size cumulatively.

## Change

- Object writers opt in through a cache policy that promises their
prepared `Data` entries and owners remain stable until the synchronous
`Write` returns. Ordinary `FileService.Write` calls and wrapper side
effects are preserved.
- For known-size, contiguous, exact-sized in-memory entries, S3FS reads
the entries with a cursor for both upload and disk-cache fill. It
replays only the prefix actually consumed by the upload. Streams, gaps,
invalid sizes, disabled cache writes and callers without the policy
retain the existing path.
- Main's disk-cache copy reads the data synchronously; only file
finalization can be asynchronous. A test blocks the finalizer, changes
the caller's buffer after `Write` returns, and verifies the published
cache. This checks for later reads of that buffer; it does not measure
GC reachability or immediate heap reclamation.

This adds a policy bit visible to custom file-service implementations.
In-tree consumers use policy masks, and tests exercise forwarding
wrappers; downstream implementations that compare the complete policy
value should review the new bit. No SQL type or object format change is
intended.

## Evidence and limits

- On a local disk-backed S3FS with synchronous cache publication, a 4
MiB, two-entry write allocated **8,397,512 B → 41,128 B** in Go
`TotalAlloc` during `Write`. A 160 KiB write split into 10,000 entries
allocated **2,932,688 B → 40,648 B**. Cache contents and disk-cache hits
were verified. In this run, both versions issued 256 write syscalls and
wrote 8,388,608 bytes for the 4 MiB object plus its cache copy. These
are cumulative allocations and process syscall counts, not peak RSS or
physical-disk traffic.
- A single-thread in-memory benchmark draining the actual reader/capture
components to a discard sink measured 160 KiB / 10,000 entries at a
median **642 µs → 182 µs** over three repetitions. It excludes disk,
network and most file-service overhead. Four paired no-op `Sync`
benchmarks were approximately 24.45 ns on both versions with no
allocations; the isolated buffer-constructor median was 30.94 → 31.88
ns, also with unchanged allocations. These microbenchmarks do not
establish end-to-end latency.
- Two opposite-order local 1-CN/1-TN/1-log comparisons used a
disk-backed S3FS, a 64 MiB memory cache, a 128 MiB disk cache, and a
test-only 512 KiB CN spill threshold. Each loaded 32 rows totaling 5.94
MiB, then altered a column; all row hashes matched after both phases and
each phase performed six S3 writes. Sampled LOAD RSS rises from phase
start were **72.89 / 73.03 MiB** on base and **63.94 / 66.38 MiB** on
head; shared-memory-cache maxima were approximately 5.97 MiB on both
sides. These are observations in a small local setup and include other
CN/GC activity. In one sampled cumulative heap profile,
`bytes.growSlice` under `S3FS.write` accounted for 41.19 MB on base and
was not sampled on head.
- LOAD timings changed direction across those two pairs: base/head
**2.50/3.81 s** and **2.61/1.90 s**. A separate local-disk fragmentation
test also varied from milliseconds to hundreds of milliseconds on both
versions. The evidence does **not** establish an end-to-end latency
improvement or rule out a regression.

This optimization applies only when S3FS disk-cache writes are enabled.
The production incident's cache configuration is not established here, a
real S3 service and production network conditions were not exercised,
and this change alone does not establish that a 251 MiB row can be
processed safely.

## Validation

- Focused fileservice replay, stream, cancellation, partial-upload,
cache-failure, wrapper, parallel-upload and async-finalizer tests:
`-race -count=10`.
- AWS SDK multipart upload to a local mock HTTP server, with two parts
crossing entry boundaries and a cancellation that must not publish a
cache entry: `-race -count=3`.
- Complete `pkg/objectio` tests, normal and `-race`; includes object
readback after arena release and a file-exists retry.
- `make static-check-selected STATIC_CHECK_PACKAGES='./pkg/fileservice
./pkg/objectio'`.
- Local S3FS/cache content checks and the two small CN comparisons
described above.

Part of #32751. Counterpart: #32783.

This branch was successfully deployed

No deployments
ci — 95594d98 Deployed Nov 15, 2026 by XuPeng-SH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants