ADR-140: The test-clock catch-up is a leader-leased role
Date: 2026-11-11
Status: Accepted
Supersedes: ADR-015’s in-process
queue, worker, start-up recovery and 503 catchup_.
Amends: ADR-114 (a sixth
role), ADR-028 and
ADR-029 (what drives the catch-up,
and its outer time bound).
Superseded: test clocks were removed in v0.11.0.
Summary
Section titled “Summary”Advancing a test clock only marks it advancing. The catch-up (the work
that fell due in the time the clock skipped) is run by one replica at a
time: the holder of the testclock_ leader lease. It runs every
tenant’s advancing clocks on a 2-second tick, one pass after another. Every
write it makes to a clock row proves the lease’s fence, so a replica that
lost the lease changes nothing. A replica that stops mid-pass leaves the
clock advancing, and the next leader runs the pass again; the passes are
safe to repeat. An advance no longer runs on two replicas at once, or waits
for a restart after a deploy.
Part 2, the stepped loop, builds on it: an advance runs its work in time
order, one due time at a time (#1276), within one pass, and each step is a
fenced write (MoveTo).
Context
Section titled “Context”ADR-015 ran the catch-up off the request on a buffered channel in each
process, drained by one goroutine, with RecoverInFlight re-queuing every
advancing clock when a process booted. The HA readiness review recorded
the cost as hazard 7 (docs/):
- A rolling deploy stops the old process while its advance runs. The new
process is already up, so no boot re-queues the advance: the clock stays
advancinguntil some replica restarts. - Every booting replica re-queues every
advancingclock with no claim, so two replicas can run one clock’s catch-up at once. - A full buffer parked the clock in
internal_and answeredfailure 503 catchup_(#1511, #1517).queue_ full
The test-clock design review of 2026-11-10 (testclock-design-v2. in the
code-health plan) chose to fix this before stepping the catch-up through
time, because a stepped advance runs longer and resumes more often. Its first
draft added a per-clock owner and lease beside the one the repo already has.
A reviewer pointed out that ADR-114 already gives one fenced runner per job;
the catch-up is one more job.
Decision
Section titled “Decision”- A sixth leader role,
testclock_(catchup leader., migration 00226 adds it to the role CHECK and seeds its row). Its loop isRoleTestClockCatchup testclock., aService. RunCatchupLoop scheduler.like the other five, started inRun main.gobesidebilling., not inside it.Scheduler - The tick (
CatchupTick, everyCatchupTickInterval= 2s) lists the advancing clocks withListAdvancingForCatchup: every tenant (TxBypass), the clock whose pass started longest ago first, at most 20, in one statement that proves the fence (leader_). It runs one pass on each. A pass starts withfence TouchAdvancing, and each step moves the clock (MoveTo); both move it to the back, so a clock whose pass keeps being cut off does not hold up the others. A pass runs to its target, so a long pass still holds up the clocks behind it. - Fenced writes. The catch-up’s writes to a clock row
(
TouchAdvancing,MoveTo,CompleteAdvance,MarkFailed) carryAND leader_.fence('testclock_ catchup', token) CompleteAdvanceandMarkFailedstore the advance’s summary in the same statement. A write that changes no row is one of two outcomes, and neither is a failure of the advance:ErrCatchupFenceLost: the clock is stilladvancingbut this run no longer holds the lease. The current leader runs the clock.ErrClockNoLongerAdvancing: the clock is gone, or out ofadvancing(another run completed or failed it, or the tenant deleted it). The run stops, logs at INFO, and leaves the clock as it is.Advance,RetryAdvanceandDeleteare API writes and stay unfenced.
- The pass’s time bound. A pass runs under
CatchupPassDeadline(10 minutes; one pass covers the whole advance). A pass that overruns it ends the advance ininternal_, with a reason that names the clock’s time. The tick’s own ctx can end first: on a shutdown, or when the lease is lost (the heartbeat cancels a led tick’s ctx withinfailure leader.). Then the pass stops and the clock staysAbandonAfter advancingfor the next leader, a panic included. Between phases the run checks its ctx only: the heartbeat and the fenced writes already cover a lost lease, so a fence query per phase would be a second guard on the same thing. - Where an advance started from. With no queue job there is nothing to
carry the pre-advance time, so
MarkAdvancingstores it: its statement setslast_from the row before it movesadvance_ summary = {"advanced_ from": frozen_ time} frozen_. The pass reads it back. The summary’stime advanced_is now right on a Retry (it was the target) and after a restart (it was zero). While the clock isfrom advancing, the summary describes the advance in progress (advanced_only). An advance that fails, a panic included, recordsfrom advanced_andto had_.errors - The billing runner is required.
testclock.takes it, so an advance cannot complete without billing. The other phase hooks are still setters, until the test-clock walk.NewService - Deleted:
- the channel queue and its worker,
RecoverInFlight,parkAfterEnqueueFailedandCatchupJob; SaveAdvanceSummary, a separate write before the transition;- the
503 catchup_answer;queue_ full - the inline catch-up
Advanceran when no queue was wired; - the 10-minute whole-run timeout;
- shutdown’s stage 2 (the catch-up’s tick now drains with the other loops
in the last stage). Tests that relied on the inline run call
testclocktest., which runs the tick on a leased ctx.Drain
- the channel queue and its worker,
Why this design
Section titled “Why this design”-
One mechanism. ADR-114’s lease already handles election, takeover, pause, cadence and fencing, with tests for each. The catch-up gets all of it by being one more role, with no second lease and no rescan ticker.
-
A stopped process is not a failure. The old worker could only finish a pass or abandon it until a boot. Now the next leader repeats a stopped pass. That is safe because every phase guards its own writes:
- the period compare-and-set (ADR-115);
- the charge claim and its hold (ADR-127, ADR-137);
- the dunning run’s state compare-and-set;
- the threshold fire-once probe;
- the trial flip’s and the pause resume’s own predicates;
- the grant retire’s
WHERE(ADR-071).
Today’s Retry advance already relies on the same guards.
-
The fence is on the clock row only. The phases write billing rows the way the wall-clock billing tick does, with the same guards, so they need no fence of their own. The clock row is what decides whether the advance is done, failed or still running, so only its writes prove the lease.
Alternatives considered
Section titled “Alternatives considered”- A per-clock owner and lease on
test_, with a rescan ticker on every replica (the design review’s v2). A second lease mechanism beside ADR-114’s, with its own TTL, takeover and tests. Rejected for the one the repo has.clocks - A session advisory lock per clock. Holds a pool connection for up to ten minutes per running clock, cannot be fenced (ADR-114 #797), and strands behind transaction-mode poolers.
- Keep the queue, add a periodic rescan. Still runs a clock twice (a rescan on each replica has no claim), and keeps the 503.
Consequences
Section titled “Consequences”- An advance starts within about one tick (2s) when no other catch-up is
running; otherwise it waits for the passes ahead of it, each up to 10
minutes. It used to start at once on the replica that took the request.
After a leader dies, the role moves within
LeaseTTL+MaxPoll(about 15s). - All tenants’ catch-ups run on one replica, one clock at a time. Before, each replica ran its own queue, so several replicas could run several clocks at once. A large clock now holds up the others for up to 10 minutes per pass, until the stepped catch-up gives each clock a bounded slice.
- A deposed leader’s phases stop before the next leader starts. Its
tick’s ctx is cancelled within
AbandonAfter(6s), and a successor can take the lease only afterLeaseTTL(10s), so two passes never run their phases at once. Only work on contexts detached from the tick can still be finishing: a commit’s 30-second budget, the 5-minute steps budget after a tax retry’s finalize, a charge’s outcome stamp. Each is a single write, covered by the same per-row guards as on the billing role after a takeover. The deposed run’s own writes to the clock row miss the fence. - Upgrading. Stop every replica of the earlier version first, as 00224
and 00225 already require. Its worker runs advances outside the role, with
unfenced writes, and two passes at once are not safe: their phases can
interleave and break the order of credit expiry after dunning retries
within a pass. A clock the earlier version left
advancingis picked up by the new leader; itsadvanced_may be wrong, since the earlier version did not store it.from - A restore pauses
billinganddunning, not this role: a restoredadvancingclock runs at once, asRecoverInFlightran it at boot before. Test clocks are test mode only. - The role can be paused and resumed like the others (
leader_), which the runbook uses.pause
References
Section titled “References”internal/,testclock/ catchup. go internal/(testclock/ service. go runClock),internal/(testclock/ postgres. go ListAdvancingForCatchup,transitionWithReason,fencedMiss).internal/.platform/ migrate/ sql/ 00226_ testclock_ catchup_ role. sql internal/.testclock/ catchup_ lease_ integration_ test. go - Runbook:
docs/.ops/ runbook/ test-clock-advance-hung. md - HA readiness record, hazard 7:
docs/.dev/ records/ ha-readiness-2026-07-06. md
Stuck, or found a bug? Ask in Discussions · Open an issue