Skip to content

feat(cluster): peers learn which keys are cached elsewhere (#140) - #402

Merged
scttfrdmn merged 1 commit into
mainfrom
feat/140-announce-key
Aug 9, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
feat/140-announce-key

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #140.

AnnounceKey and QueryKeyOwnership were stubs returning ErrNotSupported. Both are real now: a node broadcasts a cache_announce gossip message naming the key, its ETag, its size and the byte range it holds; receivers record the claim against the sending peer, expire it after cluster.announcement_ttl (default five minutes), and answer a local lookup with the holders freshest-first.

Metadata only. No object bytes cross gossip, because a datagram cannot carry a 128 KiB read (#399). What this buys is knowing which keys are worth warming — a peer holding a key at the current ETag is evidence it is hot in this cluster, which a cold node cannot learn on its own. The bytes still come from S3, which is #142.

Three departures from the issue's written spec

Each is recorded in the code, not just here.

  1. A node does not record its own announcements. The spec says to. But QueryKeyOwnership is called because a read missed this node's own cache, so a self-entry is a holder guaranteed not to hold, returned in place of a peer that might. The local cache is already the authority on what this node has.

  2. Expiry uses a locally stamped recordedAt, never the wire's CachedAt. That field's own documentation forbids comparing it against a local deadline: clocks across nodes are not synchronized, so a peer running an hour behind would have every announcement expire on arrival, and one running fast would sort itself to the front of every key in the cluster. CachedAt is still carried, for a recipient comparing two holders of one ETag — advisory for that, unusable for expiry.

  3. No applied-once ledger, unlike handleCacheInvalidate, which was otherwise the precedent followed closely. An invalidation is an action, so applying a retransmission of an older one throws away bytes legitimately re-cached since. An announcement is a fact about a (key, node) pair, and recordAnnouncement replaces that node's entry rather than appending — a retransmission overwrites an identical value.

The integrity boundary

An announcement missing its ETag is refused rather than sent, with the field named in the error. An invalidation with no version is still safe, since "evict whatever you hold" cannot serve wrong bytes; an announcement with no version is not, because a peer that fetches bytes it cannot place against an object version hands them to a reading process as file content.

Announcing with gossip not running returns ErrNotSupported rather than nil — a caller told nil believes the cluster knows something it was never sent, which is exactly the defect #284 deleted a CacheReplicator for. And the receiver credits the envelope's sender over the payload's NodeID: both are authenticated by the same MAC, so this is not a defense against an outsider but against a member announcing that some third node holds a key, which would send peers to fetch bytes that node never cached and has no way to correct.

Also

  • cluster.announcement_ttl on distributed.ClusterConfig, with no internal/config key — tuning for a subsystem whose defaults are measured, like the gossip triple beside it. Its default is applied at the point of use rather than in applyConfigDefaults, because a zero TTL there is not a slower cluster but a silently inert one.
  • announced_keys in the coordinator's statistics, counting keys retained rather than queryable, so the gap between it and a query is how far behind the expiry sweep is running.
  • TestCoordinatorWrapper_UnimplementedMethodsRefuse asserted both stubs refuse, and is rewritten as TestCoordinatorWrapper_CacheCoordinationOnAnUnstartedCluster — where the two now answer differently, which is the point: announcing with no socket open is a lie if it succeeds, while querying a cluster that has heard from nobody honestly holds nothing, and that is the documented empty answer rather than a failure.

Verification

  • go build / gofmt / go vet clean; go test -race -count=1 ./... green; go test -race -tags=distributed ./internal/distributed/... green.
  • golangci-lint run --new-from-merge-base=origin/main: 0 issues.
  • internal/distributed coverage 81.8%.
  • A2-2: Implement AnnounceKey and QueryKeyOwnership in coordinator #140's own acceptance criterion, over loopback UDP: node A announces, node B queries and sees A's entry — via the existing startGossipPair harness rather than a second one.
  • Twenty mutations, all killed: a removed dispatch arm, local self-recording, append-instead-of-replace, dropped field validation, an admitted self-claim, ordering by CachedAt, a missing read-side expiry filter, an unbounded map, eviction skipping its expired pass, a nil slice for a miss, an inert sweep, absent stats, an accepted empty ETag, a nil answer with gossip down, a removed envelope-sender override, a recorded malformed payload, an error that drops the key it names, and both wrapper nil guards.

AnnounceKey and QueryKeyOwnership were stubs returning ErrNotSupported. Both are real: a node
broadcasts a cache_announce gossip message naming the key, its ETag, its size and the byte range it
holds; receivers record the claim against the sending peer, expire it after cluster.announcement_ttl
(default five minutes), and answer a local lookup freshest-first.

Metadata only. No object bytes cross gossip, because a datagram cannot carry a 128 KiB read (#399),
so what this buys is knowing *which* keys are worth warming — the bytes still come from S3.

Three departures from the issue's written spec, each for a reason the code records:

  - A node does not record its own announcements. QueryKeyOwnership is called because a read missed
    this node's own cache, so a self-entry is a holder guaranteed not to hold, returned in place of a
    peer that might.

  - Expiry uses a locally stamped recordedAt, never the wire's CachedAt, whose own contract forbids
    comparison against a local deadline. A peer whose clock runs an hour behind would otherwise have
    every announcement expire on arrival, and one running fast would sort itself to the front of every
    key in the cluster. CachedAt is still carried, for a recipient comparing two holders of one ETag.

  - No applied-once ledger, unlike handleCacheInvalidate. An invalidation is an action, so applying a
    retransmission of an older one throws away bytes legitimately re-cached since. An announcement is
    a fact about a (key, node) pair, and recordAnnouncement replaces that node's entry rather than
    appending — a retransmission overwrites an identical value.

An announcement missing its ETag is refused rather than sent, with the field named. That is the
integrity boundary: an invalidation with no version is safe, since "evict whatever you hold" cannot
serve wrong bytes, while a peer that fetches bytes it cannot place against an object version hands
them to a reading process as file content. Announcing with gossip not running returns ErrNotSupported
rather than nil, which is the defect #284 deleted a CacheReplicator for. The receiver credits the
envelope's sender over the payload's NodeID, so a member cannot send the cluster to fetch bytes a
third node never cached.

announced_keys joins the coordinator's statistics, counting keys retained rather than queryable, so
the gap between it and a query is how far behind the expiry sweep is running.

TestCoordinatorWrapper_UnimplementedMethodsRefuse asserted both stubs refuse and is rewritten as
TestCoordinatorWrapper_CacheCoordinationOnAnUnstartedCluster, where the two now answer differently:
announcing with no socket is a lie if it succeeds, while querying a cluster that has heard from nobody
honestly holds nothing, which is the documented empty answer rather than a failure.

Twenty mutations verified against the new tests, each killed: a removed dispatch arm, local
self-recording, append-instead-of-replace, dropped field validation, an admitted self-claim, ordering
by CachedAt, a missing read-side expiry filter, an unbounded map, eviction skipping its expired pass,
a nil slice for a miss, an inert sweep, absent stats, an accepted empty ETag, a nil answer with gossip
down, a removed envelope-sender override, a recorded malformed payload, and both wrapper nil guards.

internal/distributed coverage 81.8%.
@scttfrdmn
scttfrdmn merged commit 0df3b9e into main Aug 9, 2026
35 checks passed
@scttfrdmn
scttfrdmn deleted the feat/140-announce-key branch August 9, 2026 03:42
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-2: Implement AnnounceKey and QueryKeyOwnership in coordinator

1 participant