Skip to content

perf(s3): probe the fan-out with one chunk instead of a full wave (#514) - #528

Merged
scttfrdmn merged 4 commits into
mainfrom
perf/514-fanout-probe
Sep 18, 2026
Merged

scttfrdmn merged 4 commits into
mainfrom
perf/514-fanout-probe

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #514.

Measured, before and after

A 2 MiB read of an 8 MiB ~50%-compressible object, 1 MiB chunks, concurrency 4, stored in ~4.29 MB:

read offset before after
0 (inside the stored body) 4,221,321 bytes — 98% of the stored body 1,074,275
6 MiB (past the end of it) 1,075,247 1,074,326

The second row is what #514 says a framed read should cost. The first row now matches it to within 51
bytes, which is the point: both reads end up on the framed path, so they should cost the same thing,
and the difference was entirely bytes moved before the read knew where it was going.

Where the 98% came from

Two requests made to learn something already known.

  1. Every chunk was launched at once, so parallel_read_concurrency × read_chunk_size was in flight
    before any chunk's headers could report a Content-Encoding.
  2. The abandoned fan-out reported only that the object was encoded, so GetObject then issued a
    ranged GET to find the seekable descriptor — which the abandoned chunk's own response headers had
    already carried. That GET comes back encoded, so its bytes are stored bytes the read never uses. For
    the measured case it was half the waste: 2 MiB of chunks plus 2 MiB of rediscovery.

Neither was a mistake when written, and the issue says so. #228 chose attempt-then-fall-back over
HEAD-first so that establishing whether an object is encoded costs compressed objects rather than every
large read, and at the time a compressed object was already paying a whole-body fetch — the abandoned
chunks were genuinely free. #185 removed that fetch and left them as the entire cost. The premise
changed; the code did not.

The fix

Option 2 from the issue, all three parts:

  • Chunk 0 goes out alone, and chunks 1..N-1 wait on its response headers. A nil verdict means chunk
    0 produced no headers, so it failed — a 416 for a read starting past the end of a compressed stored
    body is the routine way that happens — and the rest are not launched then either.
  • A headerProbe declines chunk 0's own body once those headers say the object is encoded.
    getObjectRange gains an optional hook that runs where the headers are in hand and the body has not
    been touched.
  • The encoded signal carries the metadata forward (fanOutEncodedError), so the framed path starts
    from the descriptor already in hand instead of issuing the rediscovery GET.

What this pays is one round trip of first-wave parallelism on a large read. Not a HEAD — chunk 0 is
a chunk of the read itself, wanted whatever the answer turns out to be, which is the distinction
TestFanOutOnAnUncompressedObjectCostsNoExtraHead exists to hold and which the issue names as the test
any fix has to keep green. It is green.

The probe's decline is a success, not an error

Every non-nil return in that closure reaches s3-get's circuit breaker and the health tracker. A
routing decision is neither. Returning an error there would trip the breaker on every large read of a
compressed object and take a component degraded on reads that all succeed — the same class of mistake
TestFanOutFallbackLeavesNoHealthErrors was written for, and that test still passes.

One integrity restriction worth naming

Only chunk 0's own headers are handed forward, and only when chunk 0 is what saw the encoding. A
later chunk seeing an encoding chunk 0 did not means the object was overwritten mid-read, so the
descriptor in hand describes a generation that is gone; pointing the framed read at frame offsets from
the wrong object is worse than spending a request. That case returns the bare sentinel and falls back to
rediscovery, which is what every case did before this. The HEAD-resolved path is safe to hand forward,
because the HEAD is a fresh authoritative read of the current generation.

Mutation testing

Six mutations, each a compiling change, each caught:

mutation caught by
the whole first wave goes out before chunk 0's headers chunk 1's range asserted absent, and the byte delta
the probe never declines chunk 0's body the byte delta (+1,048,525)
the encoded signal carries no metadata the discovery range asserted absent
GetObject ignores the metadata handed back both offsets
the HEAD-resolved fallback carries no metadata the past-the-end offset
chunk 0 never publishes a verdict on the way out a 90s package timeout

Two of those changed the test rather than confirming it.

The first mutation I wrote survived, and it was the wrong mutation: it left the <-probed wait in
place and only dropped the verdict check, so the siblings were launched after chunk 0's headers and
cancelled microseconds later, transferring nothing. The real pre-fix behaviour launches them before
the wait. Rewritten that way it is caught — but a mutation that passes for the wrong reason is worth
recording, because it looks identical to a load-bearing line in a survivor list.

The byte assertion was originally a fixed budget (stored * 3/4) and that budget passes with the
probe's body-declining removed
— 2,122,851 against a 3.2 MB budget. Any budget loose enough to
survive the compression ratio moving is loose enough to hide a whole 1 MiB chunk. It is now calibrated
against the read past the end of the stored body: every chunk of that read is refused with a 416, so it
cannot transfer a chunk byte even in principle, and what it moves is the framed cost of a 2 MiB read
of this object. The allowance is a quarter of a chunk; the observed difference is 51 bytes.

The last mutation deadlocks rather than failing, and it deadlocks on the ordinary case. No test can turn
that into a clean failure — the line itself is the fix — so the code comment says so rather than leaving
it to be rediscovered from a timeout.

Verification

go test -race ./internal/storage/s3/ passes; so do internal/vfs, internal/fuse, and
internal/compression. golangci-lint run --new-from-merge-base=main on the package: 0 issues.
internal/adapter cannot be built on this machine — runtime/cgo refuses without an accepted Xcode
license — so CI is the first place that package compiles against this change.

An abandoned parallel read used to transfer 98% of the stored body before
learning it was on the wrong path — 4,221,321 of 4,293,520 bytes for a 2 MiB
read of an 8 MiB compressed object, which are precisely the bytes seekable
framing (#185) exists to avoid.

Two requests made to learn something already known added up to that:

  - every chunk was launched at once, so concurrency x chunk_size was in
    flight before any chunk's headers said the object was encoded;
  - the abandoned fan-out reported only *that* the object was encoded, so
    GetObject issued a ranged GET to find the seekable descriptor that the
    abandoned chunk's own response headers had already carried. That GET
    comes back encoded, so its bytes are stored bytes nobody uses.

Neither was a mistake when written. #228 chose attempt-then-fall-back over
HEAD-first so establishing whether an object is encoded costs compressed
objects rather than every large read, and at the time a compressed object was
already paying a whole-body fetch — the chunks were free. Framing removed that
fetch and left them as the entire cost.

Chunk 0 now goes out alone and the rest wait on its response headers, a
headerProbe declines that chunk's body once the headers say the object is
encoded, and the encoded signal carries the metadata so the framed path starts
from what is in hand. The 2 MiB read at offset 0 drops from 4,221,321 bytes to
1,074,275 — the same cost as the same read past the end of the stored body,
which is what framing says it should be.

What it pays instead is one round trip of first-wave parallelism, not a HEAD:
chunk 0 is a chunk of the read itself, wanted whatever the answer turns out to
be. TestFanOutOnAnUncompressedObjectCostsNoExtraHead still holds that.

The probe's decline is reported as a success, not an error. Every non-nil
return in that closure reaches s3-get's circuit breaker and the health
tracker, and a routing decision is neither — an error there would degrade the
component on reads that all succeed.
…ame (#514)

The type carrying the encoded signal's metadata is fanOutEncodedError, not
fanOutEncoded: errname requires the xxxError form for a type implementing
error, and only `--new-from-merge-base` sees it because the rest of this file
predates the linter.

The CHANGELOG entry lands under Changed rather than Fixed. Every read on this
path returned the right bytes before and after; what moved is the byte count.
@scttfrdmn scttfrdmn added type: performance Performance optimization or efficiency improvement priority: medium Important but not urgent area: s3 S3 backend integration and operations area: compression Compression algorithms (ZSTD, LZ4) labels Sep 18, 2026
#514)

CI measured 2,122,851 bytes on the read the local run measured at 1,074,275: the
same 1 MiB difference the "probe never declines the body" mutation produced. Both
runs declined the body. Only one could see it.

testaws counts bytes the emulator *wrote* toward the client, and closing a
response body mid-stream does not unwrite what a socket buffer already accepted.
darwin's write failed after 614 bytes; the Linux runner's loopback swallowed the
whole 1,048,576-byte chunk before the close landed. So the saving is real and the
measurement of it is not portable.

Assert it where it is deterministic instead. BackendMetrics gains FanOutProbes
and FanOutProbeDeclines, recorded where the verdict is acted on, in the shape
SeekableWholeFallbacks already uses and for the same reason: a silent failure
mode needs a number attached to it. The byte budget grows to tolerate one
abandoned chunk, which demotes it to a backstop — an abandoned body and an extra
chunk are the same 1 MiB — so the comment now says which assertion catches what.
…med to (#514)

6d874a8's message says it recorded the CHANGELOG entry and its diff touches two
Go files. The entry was never written, so a user-facing byte-count change and a
new pair of metric fields had no line in Unreleased.

Written now, with the metrics and the test under Added and the read itself under
Changed rather than Fixed: every read on this path returned the right bytes
before and after, and what moved is what it cost to get them.
@scttfrdmn

Copy link
Copy Markdown
Owner Author

CI caught the assertion, not the fix

The test and coverage jobs failed on one line:

parallel_read_encoding_test.go:406: a 2097152-byte read at offset 0 transferred 2122851 bytes;
the same-sized read past the end of the 4292603-byte stored body transferred 1074326

2,122,851 is 1,074,275 + 1,048,576 — the framed cost plus exactly one chunk. It is also, to the
byte, the number the "probe never declines chunk 0's body" mutation produced locally. But the probe
does decline it on the runner; the difference is what the measurement can see.

testaws.BytesRead sums Request.ResponseBytes, which is capturingWriter.written: bytes the
emulator wrote toward the client. Closing a response body mid-stream does not unwrite what a socket
buffer has already accepted. On darwin the write failed after 614 bytes. On the Linux runner the
loopback buffer took all 1,048,576 before the close landed. Both runs declined the body; only one
could count it.

So the saving is real and platform-independent, and the measurement of it is neither.

What changed

  • BackendMetrics gains fanout_probes / fanout_probe_declines, recorded in
    getObjectRangeProbed where the verdict is acted on, so the counter and the untransferred body are
    one event and cannot drift. Same shape and same reason as seekable_whole_fallbacks from Seekable zstd framing so a ranged read fetches only the frames it covers #185: a
    silent failure mode needs a number attached to it. The ratio is also the honest cost side of this
    PR — every fan-out now waits one round trip, and only the declines got anything for it.
  • The test asserts declines[0] >= 1, and asserts the past-the-end read did not move the counter
    (every chunk of it is refused with a 416, so no headers exist for a probe to be asked about).
  • The byte budget grows from chunkSize/4 to chunkSize + chunkSize/4 to tolerate the abandoned
    body, which demotes it from a gate to a backstop. The comment now says so plainly: an abandoned
    chunk body and an extra chunk requested are the same 1 MiB, so no budget that tolerates the first
    can catch the second. The exact detectors are the two range assertions and the counter; what is
    left to the bytes is the regression that costs most, a fall back to fetching the whole stored body.
  • The CHANGELOG entry for An abandoned parallel-read fan-out transfers most of the body seekable framing exists to avoid #514, which 6d874a8's message claims to have added and its diff does not
    contain.

Mutations, re-run against the new assertions

whole first wave launched before chunk 0's headers (pre-fix) CAUGHT — chunk-1 range
probe never declines the body CAUGHT — declined 0 chunk bodies
decline taken but recorded as false CAUGHT — same assertion
encoded signal carries no metadata CAUGHT — 2,097,101 extra bytes
GetObject ignores the handed-back metadata CAUGHT — 2,096,833 extra bytes
HEAD-resolved fallback carries no metadata CAUGHT
control, unmutated PASS

The third row is new and is the one that matters for a counter-based assertion: it confirms the
metric is wired to the verdict rather than to a literal.

@scttfrdmn
scttfrdmn merged commit fdae4db into main Sep 18, 2026
42 checks passed
@scttfrdmn
scttfrdmn deleted the perf/514-fanout-probe branch September 18, 2026 19:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: compression Compression algorithms (ZSTD, LZ4) area: s3 S3 backend integration and operations priority: medium Important but not urgent type: performance Performance optimization or efficiency improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

An abandoned parallel-read fan-out transfers most of the body seekable framing exists to avoid

1 participant