Skip to content

feat(cluster): a write on one node evicts what its peers cached (#141) - #403

Merged
scttfrdmn merged 1 commit into
mainfrom
feat/141-announce-on-cache-write
Aug 9, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
feat/141-announce-on-cache-write

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #141. Third of the warming chain, after #399 and #140.

What this is

#140 gave the cluster the vocabulary — AnnounceKey, QueryKeyOwnership, InvalidateKey over gossip. This is the read and write paths using it:

  • a read that misses and fetches from S3 announces the range it cached, so a peer wanting those bytes can weigh asking against reading S3
  • a flush, an unlink, an rmdir, a mkdir, a create and both halves of a rename invalidate on every peer as well as locally

Until this, every part of the invalidation machinery existed and nothing called it. A two-node deployment served a file's previous contents for up to five minutes after another node overwrote it, with nothing in the logs to say so.

Three decisions worth review

The invalidation carries the ETag the write itself reported. This is why vfs.Writer grows FlushReportingETag and FileHandle.Flush asks for it. A receiver's replay ledger is keyed on (key, version), so a version fetched from a later HeadObject could name a third node's subsequent write and suppress an invalidation that was never applied. A delete has no version to name and sends an empty one — legal, means "evict whatever you hold", and cannot serve wrong bytes. An empty ETag with a nil error is a legitimate answer from the flush and is documented as such in three places, because reading it as a failure would fail writes that succeeded.

Local and remote eviction go through a single call. Every mutation already called invalidate; a cluster call sitting beside each of those as a separate statement is a pair a future mutation path can add one half of, and the half easier to forget is the remote one — invisible on a single-node mount and on every test that does not run two nodes.

An announcement without a known version is not sent. The ETag comes from the metadata cache, populated by the stat the kernel must issue before any read. With nothing there, nothing is announced rather than a version being invented: a peer that fetches bytes it cannot place against an object version hands them to a reading process as file content. The gap that remains — an overwrite between the stat and the GET — costs a peer a rejected fetch and an S3 read, which is what it would have had with no announcement at all. That reasoning is in the code, at announceCached.

Both directions are fire-and-forget and neither fails a syscall. They are not logged alike: a lost announcement costs a peer an S3 read, which is slower and nothing more (Debug), while a lost invalidation means peers serve bytes this node replaced until their cache TTL expires and nothing else in the system reports it (Warn).

Tests

internal/fuse/coordinate_test.go — ten tests against a recording coordinator, a real S3 endpoint via internal/testaws, a real byte-range cache and a real write path. The coordinator is the only mock and the file says why: what is under test is which announcements and invalidations this package makes with which fields, and a call whose errors are logged and never returned cannot be observed any other way. The metadata cache is deliberately not mocked — the announcement's ETag comes from it, so a fake there would let the announce path pass while reading a version production does not have.

Covered: the announcement's Size is the object's and its Length is the range's (not collapsed); NodeID is left empty because this package does not know it and the coordinator overwrites it; a read with no preceding stat announces nothing; a failing announce or invalidate still returns errno 0 with the right bytes read and stored; the metadata key is invalidated too, with an empty ETag; every delete-shaped operation invalidates with no version; a nil coordinator changes neither the bytes read nor the bytes stored.

internal/distributed/fuse_twonode_test.go — the two-node criterion, and nothing here is mocked but the second host. Two real filesystems, two real gossip sockets on loopback via the existing startGossipPair, one shared bucket. Node A writes through Create → Write → Flush (the node entry points, so a test cannot pass with #141 reverted) and node B's cached bytes are gone; then the same for Unlink, which is worth its own test because an unversioned invalidation takes a different branch in handleCacheInvalidate and bypasses the applied-once ledger. It lives in internal/distributed because internal/fuse cannot reach startGossipPair or a *ClusterManager's unexported gossip field, and the absence of an import cycle was verified by compiling a probe rather than reasoned about.

Mutation testing. Fifteen mutations applied to the production code, fifteen killed: announce field swaps, a zeroed CachedAt, a fabricated ETag, a zero-length announce, the dropped metadata-key invalidation, an empty and a bogus flush ETag, all four per-operation invalidations, both rename halves, and two end-to-end (flush and unlink downgraded to local-only, caught by the two-node tests).

One of those found a real weakness rather than confirming a strength: the create subtest released its handle, Release flushes, and the flush invalidates the same key — so it passed with Create's own invalidation removed. The handle is now left unreleased and the finding is recorded in the test.

Benchmarks

#141's criterion asks for no regression with a nil coordinator, and there was nothing anywhere in the repository to compare against: its benchmarks cover the cache, the S3 backend and URI validation, none of which run the FUSE read path. So the comparison is established rather than asserted against a number nobody measured.

ns/op allocs/op
cached read, no coordinator 4747 2
cached read, with coordinator 4884 2
uncached read, no coordinator 10586 15
uncached read, with coordinator 13778 25

The cached pair is the criterion and the two are indistinguishable — a hit never reaches the announce call. The miss pair is where the work is: ~3.2 µs and 10 allocations, nearly all of it the metadata-cache read the ETag comes from, against a call that in production is followed by a UDP fan-out.

Three guards keep those figures honest and all three are mutation-killed: the cached benchmark asserts zero backend requests inside its timed loop, the cached-with-coordinator one asserts zero announcements, and the uncached-with-coordinator one asserts one announcement per iteration — without that last, it would happily report a figure for the early-return path, which is identical to the nil-coordinator path by construction.

They use an in-memory types.Backend rather than internal/testaws, which is a departure from this package's convention and is justified in the file. emulator.StartTestServer takes a *testing.T concretely and testing.TB has an unexported method, so no cast bridges a *testing.B to it — filed upstream as substrate#605. It does not weaken the measurement: the cached pair proves the backend is never reached inside its loop, and for the uncached pair an in-memory GET makes any coordinator overhead a larger fraction of the total than it would be against S3, so "no meaningful difference" holds a fortiori.

Verification

  • go build ./..., gofmt -l ., go vet ./... — clean
  • go test -race -count=1 ./... — clean
  • go test -race -count=1 -tags=distributed ./internal/distributed/... — clean
  • golangci-lint run --new-from-merge-base=origin/main — 0 issues
  • coverage: internal/fuse 69.4%, internal/vfs 98.4%, internal/distributed 81.8%
  • benchmarks at -count=5, figures above

One known mutation survivor, pre-existing and documented in both the code and internal/vfs/flush_etag_test.go: the if n.Dirty() guard in FlushReportingETag is unreachable by design.

#140 gave the cluster the vocabulary. This is the read and write paths
using it: a read that misses announces the range it cached, and a flush,
an unlink, an rmdir, a mkdir, a create and both halves of a rename
invalidate on every peer as well as locally.

Until this, every part of the invalidation machinery existed and nothing
called it, so a two-node deployment served a file's previous contents for
up to five minutes after another node overwrote it.

The invalidation carries the ETag the write itself reported, which is why
vfs.Writer grows FlushReportingETag: a receiver's replay ledger is keyed
on (key, version), so a version from a later HeadObject could name a
third node's subsequent write and suppress an invalidation that was never
applied. A delete sends an empty version, which means "evict whatever you
hold" and can never serve wrong bytes. Local and remote eviction go
through one call, because the half a future mutation path would forget is
the remote one — invisible on a single-node mount.

An announcement is sent only when this node already knows the object's
version, from the stat the kernel issues before any read. With none
known, nothing is announced rather than a version invented: a peer that
fetches bytes it cannot place against an object version hands them to a
reading process as file content.

Both directions are fire-and-forget and neither fails a syscall, but a
lost announcement costs a peer an S3 read (Debug) while a lost
invalidation means peers serve replaced bytes until their TTL expires
with nothing else reporting it (Warn).

Tests. Ten in internal/fuse against a recording coordinator, a real S3
endpoint and a real cache — including that a read with no known version
announces nothing, that a failing announce or invalidate still returns
errno 0 with the right bytes stored, and that a nil coordinator changes
neither the bytes read nor the bytes written. Two in
internal/distributed drive two real filesystems over two real gossip
sockets and assert node B's cached bytes are gone after node A writes and
after node A unlinks; that placement is verified by compiling an
import-cycle probe, not assumed.

Fifteen mutations were applied to the production code and fifteen were
killed, one of which found a real weakness rather than confirming a
strength: the create subtest released its handle, and Release flushes,
so it passed with Create's own invalidation removed.

Benchmarks establish the "no regression with a nil coordinator"
criterion, which had nothing in the repository to compare against — a
cached 128 KiB read is 4.8 µs and 2 allocations with or without a
coordinator, since a hit never reaches the announce call. The three
guards that keep those benchmarks honest are mutation-killed too. They
use an in-memory backend rather than internal/testaws because
emulator.StartTestServer takes a *testing.T concretely, filed upstream
as substrate#605.

Closes #141
@scttfrdmn
scttfrdmn merged commit 8e924ed into main Aug 9, 2026
35 checks passed
@scttfrdmn
scttfrdmn deleted the feat/141-announce-on-cache-write branch August 9, 2026 05:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A2-3: Hook cache write path — announce to peers and invalidate on S3 write

1 participant