Elect One Leader With a Lease
Three copies of the scheduler agent run so that one crash does not stop the schedule. Only one may act at a time. Implement LeaseLock(store, ttl), a lease on a shared store, with a term number that fences out a leader that does not know it has lost.
store is shared and starts empty: holder, expires, term, log, rejected. All times are ticks passed in by the caller.
try_acquire(node, now): succeeds when there is no holder, or the lease has expired (now >= store.expires), ornodeholds it right now. Every acquisition bumpsstore.termby one, except a holder re-acquiring its own unexpired lease, which keeps its term; a node that let its lease expire gets a new term like anyone else. On success setstore.expires = now + ttland return the term. On failure returnNoneand change nothing.renew(node, now): only the current holder, while unexpired, may extend the lease tonow + ttl. ReturnsTrueorFalse.holder(now): the holder, orNoneonce the lease has expired.act(node, term, now, value): appends(term, value)tostore.logonly whennodeis the unexpired holder andterm == store.term. Otherwise appends(node, term, value)tostore.rejectedand returnsFalse. ReturnsTrueon a write.
The term is the part people skip. A leader that paused for longer than its TTL will come back and act, believing it still leads; its stale term is what stops the write. Kubernetes' Lease objects and resourceVersion do the same job for its controllers.