Skip to content

fix: min_sequential is the threshold, not one of two - #281

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/read-ahead-min-sequential
Aug 3, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/read-ahead-min-sequential

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #247.

The prefetch gate required sequentialHits >= MinSequential and confidence > 0.5, where confidence was assigned sequentialHits / 10.0 a few lines above. So the second condition was sequentialHits > 5 — the same quantity in different units — and the stricter always won. The effective gate was max(MinSequential, 6).

Measured directly, driving OnRead with a stub queue:

min_sequential first prefetch on read #
1 6
2 6
3 6
4 6
5 6
6 6
7 7

The shipped default is 3, so the documented default did not describe the default behavior, and the setting worked as documented in exactly the upper part of its range.

Why drop the floor rather than raise the defaults to 6

The issue left this open as a behavior decision wanting measurement. Here is the measurement, taken against a real S3 endpoint with the request recorder, asserting bytes transferred.

A continuing traversal costs the same either way. 3 MiB read sequentially at the kernel's 128 KiB buffer:

min_sequential GETs bytes ratio
1, 2, 3, 4, 5, 6, 7, 10 24 3,145,728 1.0000x

Identical at every value, because FileSystem.fetch shares one flight between a prefetch and the read it anticipates — a prefetch that is going to be read adds no bytes at all. So the "prefetches sooner and more often" concern does not cost anything on a reader that keeps going.

What prefetching sooner costs is a run that stops. A reader that goes sequential for N reads and then walks away, with the floor removed:

run length min_sequential: 3 min_sequential: 6
3 reads 1.3333x (waste 131,072) 1.0000x
4 reads 1.2500x (waste 131,072) 1.0000x
5 reads 1.2000x (waste 131,072) 1.0000x
6 reads 1.1667x (waste 131,072) 1.1667x (waste 131,072)

The waste is exactly one prefetch, not a growing tail, and min_sequential: 6 shows the same single waste once a run reaches 6. The floor never prevented a class of waste — it only moved the point at which the single unredeemed prefetch became possible, from read 6 to read 3. Trading that for prefetching from the third read instead of the sixth is worth it, and the number a user sets is now the number the detector uses.

Scope

The confidence field is deleted rather than left computed-and-unread. It was read at exactly one place, the gate, and a score derived from the threshold it guards is not independent evidence and cannot become any.

The paragraph in examples/config.yaml explaining why the number did not mean what it said is deleted too — the issue's own stated test of whether this was fixed rather than documented: "that comment should be deletable."

internal/config/doc.go's high-throughput profile no longer recommends min_sequential: 6 with the comment "6 is the effective floor whatever is set." It recommends 2, because committing to a prefetch early is the actual advice for a reader that does not stop.

The test

The gate test now drives both sides of the threshold — nothing on the read before it, a prefetch on the read at it. It previously read six times at min_sequential: 3 and asserted a prefetch, with a comment explaining that six was required; so it documented the defect instead of catching it, and would have passed identically if MinSequential were ignored altogether. A gate test that only drives the satisfying case cannot tell a threshold of 3 from a threshold of 6.

Mutation-verified in both directions:

mutation result
restore the confidence floor "no prefetch scheduled on read 3 at min_sequential=3, want one"
replace the gate with >= 1 "2 prefetch(es) scheduled after 2 sequential reads at min_sequential=3, want 0"

One detail worth recording: the threshold lands on read N rather than N+1, because the first read of a file counts as sequential — a fresh ReadPattern has lastOffset and lastSize both zero, so offset == lastOffset+lastSize holds at offset 0. That makes min_sequential: 3 mean the third read of a file, which is what the configuration documentation says. I had the arithmetic off by one on the first attempt and the test caught it.

Two stale comments elsewhere were corrected rather than left: ReadAheadConfig's note on the removed MaxDistance field, and TestPrefetchStopsAtEndOfFile's explanation of why its file is 16 steps long — which had relied on the seven-read figure, and which the new threshold makes generous rather than marginal.

Gates

go build ./... · go vet ./... · gofmt -l . — clean
go test -count=1 -race -covermode=atomic ./... — exit 0
./scripts/coverage-gate.sh — 35 packages at or above floor
golangci-lint --new-from-merge-base=origin/main — 0 issues
gosec ./internal/fuse/ ./internal/config/ — no findings
go test ./internal/config/ (doc/changelog gates) — ok

The prefetch gate required sequentialHits >= MinSequential *and*
confidence > 0.5, where confidence was assigned sequentialHits/10 a few lines
above. So the second condition was sequentialHits > 5 — the same quantity in
different units — and the stricter always won. The effective gate was
max(MinSequential, 6): 1, 2, 3, 4 and 5 all prefetched first on the sixth
sequential read, and the shipped default is 3, so the documented default did
not describe the default behavior. Above 6 the floor was redundant, meaning the
setting worked as documented in exactly the upper part of its range.

Dropping the floor rather than raising the defaults to 6, and the measurement
is why.

A continuing traversal costs the same either way. 3 MiB read sequentially at
the kernel's 128 KiB buffer transfers exactly 3,145,728 bytes at every
MinSequential from 1 to 10, because FileSystem.fetch shares one flight between
a prefetch and the read it anticipates — a prefetch that is going to be read
adds no bytes at all.

What prefetching sooner actually costs is a run that *stops* before consuming
what was fetched, and that cost is exactly one prefetch rather than a growing
tail: a reader that goes sequential and then walks away wastes 131,072 bytes
whether it stops after three reads at min_sequential: 3 or after six at
min_sequential: 6. The floor never prevented a class of waste, it only moved
the point at which the single unredeemed prefetch became possible. Trading one
prefetch's exposure for prefetching from the third read instead of the sixth is
worth it, and the number a user sets is now the number the detector uses.

The confidence field is deleted rather than left computed-and-unread. It was
read at exactly one place, the gate, and a score derived from the threshold it
guards is not independent evidence and cannot become any.

The paragraph in examples/config.yaml explaining why the number did not mean
what it said is deleted too, which is the test of whether this was fixed rather
than documented. doc.go's high-throughput profile no longer recommends 6 as
"the effective floor whatever is set"; it recommends 2, because committing to a
prefetch early is the actual advice for a reader that does not stop.

The gate test now drives both sides of the threshold — nothing on the read
before it, a prefetch on the read at it. It previously read six times at
min_sequential: 3 and asserted a prefetch, with a comment explaining that six
was required, so it documented the defect instead of catching it and would have
passed identically if MinSequential were ignored altogether. A gate test that
only drives the satisfying case cannot tell a threshold of 3 from one of 6.
Verified by mutation in both directions: restoring the confidence floor fails
it, and replacing the gate with `>= 1` fails it.

The threshold lands on read N rather than N+1, because the first read of a file
counts as sequential: a fresh ReadPattern has lastOffset and lastSize both
zero, so `offset == lastOffset+lastSize` holds at offset 0. That makes
"min_sequential: 3" mean the third read of a file, which is what the
configuration documentation says.

Closes #247
@scttfrdmn scttfrdmn added type: bug Something isn't working as expected priority: low Nice to have, low urgency area: fuse FUSE layer and filesystem interface area: config Configuration management and validation labels Aug 3, 2026
@scttfrdmn
scttfrdmn enabled auto-merge (squash) August 3, 2026 18:48
@scttfrdmn
scttfrdmn merged commit ced363d into main Aug 3, 2026
28 checks passed
@scttfrdmn
scttfrdmn deleted the fix/read-ahead-min-sequential branch September 20, 2026 05:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: config Configuration management and validation area: fuse FUSE layer and filesystem interface priority: low Nice to have, low urgency type: bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

read-ahead has two sequential thresholds and only one is configurable

1 participant