Skip to content

feat(distributed)!: replace the N-identical-PUT fan-out with per-key CAS (#284) - #386

Merged
scttfrdmn merged 1 commit into
mainfrom
feat/coord-cas3-284
Aug 8, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
feat/coord-cas3-284

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #284. Also closes #144 as not planned (done on the issue), and comments a correction onto #129.

The breaking change, first

cluster.consistency_level and cluster.cache_replication are removed from the config file. The loader decodes strictly, so a config that still sets either fails at startup naming the key and the line. That is intended rather than a regression — the alternative is accepting a setting that selects nothing.

Migration: delete the lines. There is no replacement value to set. consistency_level's replacement is per-operation (DistributedOperation.Precondition), and cache_replication was read by no code in the repository at all. A deployment that deletes the line gets the behaviour it already had, whichever value it had set.

ClusterManager.InvalidateCacheKey(key) also becomes InvalidateCacheKey(key, etag). No production caller existed.

What went, and why each was not fixable in place

ConsistencyLevel and its three constants. All three issued the same unconditional PutObject and differed only in how many nodes issued it, so what an operator picked changed the request count and not the guarantee. ConsistencyStrong fanned the identical bytes at the identical key to every target node and called majority success "linearizable" — every node writes the same key in the same bucket, S3 holds the single copy, so N-1 of those PUTs were billed requests that changed nothing, and a majority reporting success said only that a majority could reach S3.

Deleted rather than cut down to two levels. Under CAS the distinguishing fact is whether the operation carries a precondition, and a types.Precondition field already says that; a two-name enum beside it would be a second encoding of Precondition.IsZero().

CacheReplicator. Its worker sent N-1 peers a PUT of an object's own bytes back to the same key in the same bucket. When gossip was not running — which was every unit test — it sent nothing and added the byte count to BytesReplicated anyway. The one test covering it asserted c.replicator != nil, and that assertion is what let it survive: proving a field was assigned proves nothing about the subsystem, and the assignment was the only part of it that worked. Real cache warming is #141.

The proposal machinery. broadcastProposal slept 100ms and then voted for its own proposal, so a majority of one was always found and executeProposal called SetLeader. A node cannot be made leader by being named — the election path reaches the same setter having actually contested it. #133 was closed as not-the-direction with this stub still in the tree; #284's acceptance criteria said delete rather than fix.

Three of five EntryType constants. EntryTypeConfigChange, EntryTypeOperation and EntryTypeSnapshot had no producer anywhere — verified by grep, not assumed. applyLogEntry was a five-arm switch with every arm empty; with those gone the remaining branches were indistinguishable, so it collapsed to a log line. The surviving constants' strings are unchanged, so an entry of a removed type arriving from an older peer still decodes and applies as the no-op it always was.

What replaced it

Evaluated by the store, not voted on. A refused conditional write reports ConditionalLost in OperationResult.Conditional and wraps types.ErrPreconditionFailed, and is never retried internally: the answer is definitive, and the recovery is to re-read, recompute and CAS again, which only the caller can do. A precondition attached to anything but a put is refused with ErrInvalidPrecondition rather than silently dropped — a caller that means "delete only if unchanged" and gets an unconditional delete has no way to find out.

OperationResult.ETag carries the new version so a CAS loop can chain without a re-read.

Cache invalidations carry that ETag (R4). The gossip payload was {Key, From} and carried no version, so a receiver could not tell an invalidation for a version it had already moved past from one for the version it holds — and gossip both retransmits and reorders, so that is the normal case. A first attempt held one ETag per key, and a test caught it: that suppresses a retransmission of the latest version but applies a replay of the one before it, since an older ETag merely differs and ETags carry no order to compare. The ledger is a bounded set of applied (key, ETag) pairs.

It deliberately does not claim to know the cached bytes are newer than an incoming invalidation. types.Cache stores bytes at offsets with no version alongside them, so there is nothing to compare against; that needs #129's KeyAnnouncement plumbing and #141's warming work, and is not guessed at here.

Tests — the point of the issue, verified by mutation

assertion mutation that kills it
TestMultiNodeCluster: exactly 1 PUT of the key, counted across one substrate endpoint per node any fan-out
two coordinators racing If-Match → one success, one ErrPreconditionFailed, winner's bytes stored forcing the path unconditional
CAS chain accepts each reported ETag, refuses every earlier one forcing unconditional; suppressing the returned ETag
each invalidation version applied once removing the check; reverting to latest-per-key
ledger bounded removing the bound

The write-count assertion replaced result.Success on a ConsistencyStrong write, which could not tell one PUT from two. Writes are summed across endpoints rather than checked per-node, because routing is the load balancer's choice — pinning it would assert the routing instead of the request count.

The success half of the never-disagree test runs against a real testaws backend rather than the package's mockBackend, whose PutObjectIf returns ErrNotSupported deliberately: a stub cannot both refuse to fake a CAS and serve as the success case for one.

Two defects found while verifying, fixed here

  • TestClusterManager_BasicOperations was a coin flip. It slept exactly 3s against ElectionTimeout: 2s, whose jitter puts the first election in [2s, 4s). Observed failing 1 run in 4; now polls to the upper bound. Three consecutive -race -tags=distributed runs green after.
  • types.Precondition had no JSON tags, so it crossed the wire as Absent/ETag beside key and created_at. Tagged while the type is unreleased and it is free rather than a wire break later; TestPreconditionJSON pins the names and that a zero precondition is omitted entirely.

Verification

go build ./...                          ok
go vet ./...                            ok
gofmt -l .                              clean
make lint                               0 issues
go test -race ./internal/... ./pkg/...  all pass
go test -race -tags=distributed ./tests/  3/3 green

Coverage on the changed packages: internal/distributed 80.6%, internal/config 91.3%, pkg/types 100%.

Out of scope, filed rather than absorbed

Verifying that the strict loader genuinely rejects a stale consistency_level (it does, naming key and line) surfaced that the Python SDK's to_yaml emits three cluster timeout keys the Go schema has never had — they live on the disjoint internal/distributed.ClusterConfig (#139) — so that block fails at load independently of this change. Filed as #385.

…CAS (#284)

BREAKING CHANGE: `cluster.consistency_level` and `cluster.cache_replication` are
removed from the configuration file. Because the loader decodes strictly, a config
that still sets either now fails at startup naming the key and line — which is the
intended outcome, since the alternative is accepting a setting that selects nothing.
Delete the lines; there is no replacement to set, because what replaced the first is
per-operation (`DistributedOperation.Precondition`) and the second was read by no
code at all. `ClusterManager.InvalidateCacheKey` also gains an `etag` parameter.

Four things go, and what connects them is that each named a distributed guarantee
the code did not provide.

`ConsistencyLevel` and its three constants all issued the same unconditional
PutObject and differed only in how many nodes issued it, so what an operator picked
changed the request count and not the guarantee. `ConsistencyStrong` fanned the
identical bytes at the identical key to every target node and called majority
success "linearizable"; every node writes the same key in the same bucket and S3
holds the single copy, so N-1 of those PUTs were billed requests that changed
nothing. Deleted rather than cut to two levels: under CAS the distinguishing fact
is whether the operation carries a precondition, and a `types.Precondition` field
already says that, so a two-name enum beside it would be a second encoding of
`Precondition.IsZero()`.

`CacheReplicator` sent N-1 peers a PUT of an object's own bytes back to the same
key, and when gossip was not running — every unit test — sent nothing and added the
byte count to `BytesReplicated` anyway. The one test covering it asserted
`c.replicator != nil`, which is what let it survive: proving a field was assigned
proves nothing about the subsystem, and that assignment was the only part of it that
worked.

The proposal machinery's `broadcastProposal` slept 100ms and then voted for its own
proposal, so a majority of one was always found and `executeProposal` called
`SetLeader`. A node cannot be made leader by being named. #133 was closed as
not-the-direction with the stub still in the tree; #284's criteria said delete.

Three of five `EntryType` constants had no producer anywhere — verified by grep, not
assumed — so they and the empty switch arms naming them are gone. The surviving
strings are unchanged, so an entry of a removed type from an older peer still
decodes and applies as the no-op it always was.

What replaced the taxonomy is evaluated by the store rather than voted on. A
refused conditional write reports `ConditionalLost` and wraps
`types.ErrPreconditionFailed`, and is never retried internally: the answer is
definitive and only the caller knows what the new bytes should be. A precondition on
anything but a put is refused with `ErrInvalidPrecondition` rather than dropped — a
caller meaning "delete only if unchanged" that gets an unconditional delete has no
way to find out.

Cache invalidations now carry the ETag of the write that caused them (R4). The
payload was `{Key, From}`, so a receiver could not tell an invalidation for a
version it had moved past from one for the version it holds, and gossip both
retransmits and reorders. A first attempt held one ETag per key and a test caught
it: that suppresses a retransmission of the latest version but applies a replay of
the one before it, since an older ETag merely differs and ETags carry no order. The
ledger is a bounded set of applied (key, ETag) pairs.

Tests are the point of this issue and are verified by mutation. `TestMultiNodeCluster`
gives each node its own substrate endpoint and asserts exactly one PUT of the key —
the assertion it replaced was `result.Success`, which could not tell one PUT from
two. Two coordinators racing an If-Match must produce one success and one
`ErrPreconditionFailed` with the winner's bytes stored; a CAS chain must accept each
reported ETag and refuse every earlier one. Forcing the path unconditional kills all
of them, suppressing the ETag kills two, and removing the invalidation dedup or
reverting it to latest-per-key each fail a named assertion.

The success half of these runs against a real backend rather than `mockBackend`,
whose `PutObjectIf` returns `ErrNotSupported` deliberately — a stub cannot both
refuse to fake a CAS and serve as the success case for one.

Also fixed, both found while verifying the above rather than by inspection:

- `TestClusterManager_BasicOperations` slept 3s against a 2s election timeout whose
  jitter puts the election in [2s, 4s), so it was a coin flip. Observed failing 1 run
  in 4. It now polls to the upper bound.
- `types.Precondition` had no JSON tags, so it crossed the wire as `Absent`/`ETag`
  beside `key` and `created_at`. Tagged now while the type is unreleased and it is
  free; a `TestPreconditionJSON` pins both the names and that a zero precondition is
  omitted entirely.

#144 (implement NegotiateConsistency) is closed as not planned — the type it would
negotiate over no longer exists, and its rules all keyed on leadership, which
confers no read guarantee when the bytes live in the bucket. #129's
`NegotiateConsistency` and `ConsistencyLevel` promotion are corrected in a comment.
The Python SDK's three unloadable cluster timeout keys, surfaced while verifying the
strict loader, are filed as #385 rather than folded in here.
@scttfrdmn
scttfrdmn merged commit 5fe1370 into main Aug 8, 2026
34 checks passed
@scttfrdmn
scttfrdmn deleted the feat/coord-cas3-284 branch August 8, 2026 18:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant