Repository navigation
Description
Context
Second issue in the CAS series adopted from #169
/ docs/design/conditional-writes-vs-raft.md. Depends on #282.
This is its own issue rather than a footnote on #282 for one reason, stated in §2 of the doc as the
single most important thing to get right if CAS is adopted:
A node that loses network but keeps running believes it holds a lease it has actually lost, and the
next holder can take it while the first is still acting. Raft has the same problem — a partitioned
leader also does not know it — but Raft at least fails closed on writes, because it cannot reach a
quorum. A CAS design fails closed only if every guarded action itself re-asserts the CAS. That
is a design obligation, not a property you get for free.
An action guarded by "I checked my lease a moment ago" is not guarded. That is the whole content
of this issue; the acquire/renew/release mechanics are the easy part.
What to implement
A lease type in internal/distributed (or internal/coord if that reads better) over
Backend.PutObjectIf:
- Acquire —
PutObjectIf(key, holder+expiry, Precondition{Absent: true}), or{ETag: observed}
when taking over an expired lease.ErrPreconditionFailedmeans "someone else holds it," which is
a normal outcome and must not be logged as an error. - Renew —
PutObjectIfwith{ETag: <the ETag acquire returned>}. A failed renew means the
lease was lost, and the holder must stop acting immediately, not retry-and-continue. - Release — conditional delete on the held ETag, so a stale holder cannot delete the current
holder's lease. - The re-assert rule — the API must make the safe usage the easy one. A guarded action takes the
lease handle and performs its own write as a CAS on the lease's current ETag, so a partitioned
holder's write fails at S3 rather than at a local timer check. Prefer a shape where the unguarded
form is unrepresentable over one where it is merely discouraged by a comment.
Also honour §2's cost note: AWS bills failed conditional requests at the normal rate ("charged
existing rates for the applicable requests, including for failed requests"). A hot key with N
contenders is O(N) requests, not O(1). So: bounded retry with jittered backoff, no unbounded spin,
and coordination keys chosen per-resource rather than one global lock key. A single global lock key
would be the wrong shape.
Clock handling: expiry is compared against S3's view where possible (Last-Modified /
Date response header) rather than the local wall clock, since two nodes with skewed clocks
disagreeing about expiry is exactly how two holders arise. Document what is assumed either way —
Date.now()-style local-clock trust is the failure mode to avoid.
Testing
Against internal/testaws, -race, table-driven with t.Parallel():
- N goroutines contend for one lease: exactly one holds it at any instant. Assert with a shared
counter that a second holder would trip, not merely by checking return values. - A holder that stops renewing loses the lease after expiry, and the next acquirer gets it.
- The load-bearing test: a holder whose renew fails (simulate by having another node steal the
lease) cannot subsequently perform a guarded action. This is the test that would catch the
partitioned-holder bug, and it must fail if the re-assert is removed. - Release by a stale holder does not delete the current holder's lease.
- A backend reporting
ErrNotSupportedfrom the CAS-1: Backend.PutObjectIf — preconditions, sentinel errors, and a capability probe #282 probe makes lease acquisition refuse to start,
with an error naming the backend limitation — never a silent unconditional fallback. - Mutation-verify: delete the re-assert from the guarded-action path and confirm a named test fails.
Per the doc this is the property most likely to be quietly regressed later, so the test has to be
explicit about why it exists.
Acceptance criteria
- Lease type with acquire / renew / release over
PutObjectIf - Guarded actions re-assert the CAS; the unguarded form is hard or impossible to write
- Bounded, jittered retry — no unbounded contention loop; per-resource keys, no global lock key
- Documented clock assumption, preferring S3's view over local wall clock
-
ErrNotSupportedfrom the probe ⇒ refuse to start, never run unguarded - Exactly-one-holder test under contention, with
-race - Partitioned-holder test: a lost lease blocks the guarded action
- Mutation-verified against removal of the re-assert
- Package doc stating the liveness limitation plainly — a lease is a timeout, not a guarantee
Dependencies
Blocked by #282.
Reference: docs/design/conditional-writes-vs-raft.md §2 "The ceiling", items 2 and 4.
Activity
docs: record the CAS decision where the code claims otherwise (#286)
test(coord): a config fuzzer, which found a panic before it was commi…
test(coord): cover the one renewal failure that must not drop the cla…