Repository navigation
feat(types)!: cache coordination on DistributedCoordinator, with stubs that refuse (#129) - #387
Merged
Merged
Conversation
…s that refuse (#129) Three methods beside the two the interface had — AnnounceKey, QueryKeyOwnership, and InvalidateKey(ctx, key, etag) — plus the types.KeyAnnouncement they carry. This is the surface the whole v0.13.0 warming chain (#140, #141, #142, #143) was blocked on: none of it could begin while a coordinator had no way to say "this node holds these bytes at this version." The issue as filed said stub implementations returning nil were acceptable. Revised, because that is the defect #284 just deleted. Its CacheReplicator added bytes to BytesReplicated when gossip was not running and it had sent nothing, and it survived four releases because the only test covering it asserted its field was non-nil. So AnnounceKey and QueryKeyOwnership return types.ErrNotSupported until #140. QueryKeyOwnership in particular must not return an empty slice with a nil error: that is the *correct* answer for a key no peer has cached, so returning it from a method that cannot query is indistinguishable from a working query against a cold cluster, and an operator watching peer-fetch hit rates would read a flat zero as "warming does not help" rather than "warming is not built." InvalidateKey is real — #284 built the message, the sender, and the receiver's replay-suppressing ledger. Its etag parameter is why the signature is not InvalidateKey(ctx, key) as filed: gossip retransmits and reorders, so a bare key leaves a receiver unable to tell an invalidation it has already applied from one it has not (R4, docs/design/conditional-writes-vs-raft.md §1). Empty stays legal, means the caller cannot name a version, and is applied every time — a redundant eviction, never a stale read. KeyAnnouncement's ETag is required rather than optional, which is the one place it differs from an invalidation: an unversioned invalidation is safe, since "evict whatever you hold" cannot serve wrong bytes, but an unversioned announcement invites a peer to fetch bytes it cannot place relative to the object in the bucket and hand them to a reading process as file content. Its Offset/Length pair is deliberately not omitempty, unlike Precondition's fields: Offset 0 with Length 0 means "the whole object from the start" and is the commonest announcement there is, so omitting it would be indistinguishable on the wire from a message carrying no range at all. JSON tags are pinned by a test for the reason Precondition's were added in #284 — that type shipped untagged and put `Absent` beside `created_at`. BREAKING, and found by one of the new tests rather than by inspection: broadcastMessage returned nil unconditionally, fanning out into goroutines that discarded every send error. NewClusterManager builds a GossipProtocol and only Start opens its socket, and every send dials its own UDP socket — so broadcasting from a constructed-but-unstarted cluster dialed from an unbound port, reached nobody, and reported success. A node that had just replaced an object's bytes could not tell "every peer was told" from "nothing was sent," and the difference is whether peers keep serving stale content. It now refuses with ErrNotSupported when not listening, and InvalidateCacheKey / InvalidateDeletedCacheKey return error where they returned nothing. A nil still means only "handed to the network for each peer," never "received" — gossip has no acknowledgement to give, so convergence rests on retransmission. A running cluster with no peers is nil and no sends: nobody to tell, nothing wrong. Per-peer failures stay in NetworkErrors and MessagesOversize, since one unreachable peer is the normal state of a gossip cluster. The test this replaced asserted only that invalidating with gossip down did not panic, which passed identically whether the message was broadcast or dropped on the floor. Mutation-verified, eight mutations, each killing the assertion that names it: nil from either stub; an empty slice from QueryKeyOwnership; removing the not-listening guard; dropping the ETag from the broadcast; a silently-succeeding wrapper InvalidateKey; omitempty on the range fields; a renamed wire field; and a removed interface method against the compile-time assertion. Dropping the ETag is the interesting one — the invalidation still arrives, and three then apply where two should, which is the R4 defect rather than a delivery failure. internal/distributed coverage 80.6% → 81.2%, pkg/types 100%. Three green -tags=distributed runs. BREAKING CHANGE: ClusterManager.InvalidateCacheKey and InvalidateDeletedCacheKey return error where they returned nothing, and GossipProtocol.broadcastMessage now refuses when gossip is not listening instead of reporting success having sent nothing. types.DistributedCoordinator gains AnnounceKey, QueryKeyOwnership and InvalidateKey, so any external implementation of that interface must add all three.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #129. The issue body was revised before implementing — two of its items no longer held after #284, and the two comments on it are the record of why. What is built here is the revised version.
Breaking, and found by a test rather than by inspection
GossipProtocol.broadcastMessagereturnednilunconditionally. It fanned out into goroutines that discarded every send error, andNewClusterManagerbuilds aGossipProtocolwhile onlyStartopens its socket — and every send dials its own UDP socket. So broadcasting from a constructed-but-unstarted cluster dialed from an unbound port, reached nobody, and reported success.A node that had just replaced an object's bytes could not distinguish "every peer was told to evict the version before" from "nothing was sent", and the difference is whether peers keep serving stale content. It now refuses with
types.ErrNotSupportedwhen not listening, andClusterManager.InvalidateCacheKey/InvalidateDeletedCacheKeyreturnerrorwhere they returned nothing.What a nil does and does not mean, stated in the doc comment rather than left to inference: the message was handed to the network for each peer, never that any peer received it. Gossip is unreliable datagrams with no acknowledgement, so convergence rests on retransmission, not on this return value. A running cluster with no peers is a nil error and no sends — nobody to tell, nothing wrong. Per-peer failures stay counted in
NetworkErrors/MessagesOversize, since one unreachable peer is the normal state of a gossip cluster.LeaveClusteris the only other caller whose error reaches outward, and it has no callers, so nothing else changes behaviour.The interface
This is what the whole v0.13.0 warming chain (#140, #141, #142, #143) was blocked on. None of it could begin while a coordinator had no way to say "this node holds these bytes at this version."
Stubs return
ErrNotSupported, notnilThe issue as filed said nil stubs were acceptable. Revised, because that is the defect #284 deleted: its
CacheReplicatoradded bytes toBytesReplicatedwhen gossip was not running and it had sent nothing, and it survived four releases because the only test covering it asserted its field was non-nil.QueryKeyOwnershipin particular must not return an empty slice with a nil error. That is the correct answer for a key no peer has cached, so returning it from a method that cannot query is indistinguishable from a working query against a cold cluster — an operator watching peer-fetch hit rates would read a flat zero as "warming does not help" rather than "warming is not built."InvalidateKeytakes the ETagNot
InvalidateKey(ctx, key)as filed. Gossip retransmits and reorders, so a bare key leaves a receiver unable to tell an invalidation it has already applied from one it has not — requirement R4 ofdocs/design/conditional-writes-vs-raft.md§1. #284 built the receiving half (markInvalidationApplied, a bounded set of applied(key, ETag)pairs); this is the interface those messages travel under, so the signature has to admit the version or the plumbing stops at the boundary.Empty stays legal, means the caller cannot name a version — a delete, or an unconditional put — and is applied every time. A redundant eviction, never a stale read.
KeyAnnouncementIts
ETagis required rather than optional, which is the one place it differs from an invalidation. An unversioned invalidation is safe, since "evict whatever you hold" cannot serve wrong bytes. An unversioned announcement is not: it invites a peer to fetch bytes it cannot place relative to the object in the bucket, and hand them to a reading process as file content.Offset/Lengthare deliberately notomitempty, unlikePrecondition's fields. Offset 0 with Length 0 means "the whole object from the start" and is the commonest announcement there is, so omitting the pair would be indistinguishable on the wire from a message carrying no range at all.Sizeis the full object's size rather than the range's, because a peer weighing a fetch needs both — the range says what it can get here, the size says what fraction of the object that is.JSON tags are pinned by a test for the reason
Precondition's were added in #284: that type shipped untagged and putAbsentbesidecreated_at, and it was free to fix only because it had not been released.Mutation verification
Eight mutations, each killing the assertion that names it.
AnnounceKeyreturns nilAnnounceKey = <nil>, want ErrNotSupportedQueryKeyOwnershipreturns[]KeyAnnouncement{}, nilInvalidateCacheKey with gossip not listening = <nil>cm2 applied 3 invalidations, want 2InvalidateKeyreturns nil silentlycache.Delete("warm/key") was not called within 2somitemptyonOffset/Lengthnode_id→NodeIDDropping the ETag is the interesting one: the invalidation still arrives, and three of them then apply where two should. That is the R4 defect itself rather than a delivery failure, which is what makes it worth asserting on the count rather than on the eviction.
The two-node test asserts by receiving on a second node rather than by inspecting the call, because the bug it guards against is a broadcast assembled correctly and never sent — which is exactly what
InvalidateCacheKeydid before this change. Its predecessor asserted only that invalidating with gossip down did not panic, which passed identically whether the message was broadcast or dropped on the floor.Also extracted
startGossipPairandwaitForDeletionfrom the existing two-node test rather than copying its 40 lines of setup, and raised its 200ms deadline to 2s — the failure it catches is a message that is never sent, which no amount of waiting fixes, so a tight deadline buys nothing and flakes on a loaded runner.Verification
Coverage:
internal/distributed80.6% → 81.2%,pkg/types100%.Not in scope
AnnounceKeyandQueryKeyOwnershiphave no implementation — that is #140, and they say so by returningErrNotSupportedwith the issue number in the message. Theinternal/distributedpackage doc gains a "Cache Coordination" section stating which of the three works and what the invalidation ledger deliberately cannot do: suppress an invalidation older than what a peer holds, sincetypes.Cachestores bytes at offsets with no version beside them. That needs a per-key version in the cache, which is #141's work, and is documented rather than guessed at.