cert warm: one rule for held names, owed a pass at max(deadline, now) (#443, Paul's option 1)
settleUntil is the warm's single record of what it owes a held name, and owedWait is the only place it becomes the next pass's time. A deadline that passed during a pass counts as now, so a hold that ends while another order is in flight is retried at once (Astra R2 P1). An entry leaves only through discharge: ordered, failed, handed to the order cap or the DNS-miss schedule, or out of the set. Guard: a 300-seed simulation with a clock that moves inside orders checks the rule after every pass; plus the R2 scenario and the cap handoff.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+196
−26
internal/edge/certsettle_test.go+155 −0internal/edge/certwarm.go+40 −25internal/provision/testdata/plan14-root-reads-cert-warm-propagation-443.tsv+1 −1
diff --git a/internal/edge/certsettle_test.go b/internal/edge/certsettle_test.go| index a0b99557e..c6b98c900 100644 |
| --- a/internal/edge/certsettle_test.go |
| +++ b/internal/edge/certsettle_test.go |
| @@ -4,6 +4,8 @@ import ( |
| "context" |
| "crypto/tls" |
| "errors" |
| + "fmt" |
| + "math/rand/v2" |
| "sync" |
| "testing" |
| "time" |
| @@ -370,3 +372,156 @@ func TestWarmDoesNotTreatASettlingNameAsNew(t *testing.T) { |
| t.Fatal("a settling name read as new: every reload in the fleet would rerun the pass") |
| } |
| } |
| + |
| +// Round 2 (Astra, P1): a hold that ends WHILE a pass is still running (here, |
| +// another host's order takes 30 s) must still bring the pass back for the |
| +// held name, not leave it for the hourly pass. |
| +func TestWarmRetriesAHoldThatEndsDuringAPass(t *testing.T) { |
| + clk := &clockAt{t: time.Unix(1_800_000_000, 0)} |
| + holdUntil := clk.now().Add(45 * time.Second) |
| + slowFirst := true |
| + w := newCertWarm( |
| + func() []string { return []string{"held.test", "slow.test"} }, |
| + func(h *tls.ClientHelloInfo) (*tls.Certificate, error) { |
| + switch h.ServerName { |
| + case "held.test": |
| + if clk.now().Before(holdUntil) { |
| + return nil, &settlingError{left: holdUntil.Sub(clk.now())} |
| + } |
| + case "slow.test": |
| + if slowFirst { |
| + return nil, &settlingError{left: 20 * time.Second} |
| + } |
| + clk.advance(30 * time.Second) // the order itself takes 30 s |
| + } |
| + return &tls.Certificate{}, nil |
| + }, |
| + func(string) bool { return true }, |
| + nil, |
| + nil, |
| + ) |
| + w.now = clk.now |
| + w.pass() // t=0: both held |
| + clk.advance(20 * time.Second) |
| + slowFirst = false |
| + d := w.pass() // t=20: skips held.test, orders slow.test until t=50 |
| + if d > warmSettleSlack { |
| + t.Fatalf("the hold ended during the pass; next pass in %s, want at most %s", d, warmSettleSlack) |
| + } |
| + clk.advance(d) |
| + w.pass() |
| + w.mu.Lock() |
| + warmed := w.warmed["held.test"] |
| + w.mu.Unlock() |
| + if !warmed { |
| + t.Fatal("held.test was not ordered on the pass after its hold ended") |
| + } |
| +} |
| + |
| +// An owed name the order cap defers is handed to the cap's own schedule: |
| +// the pass comes back after warmCappedDelay, not in a tight loop. |
| +func TestWarmHandsADueHoldToTheCap(t *testing.T) { |
| + clk := &clockAt{t: time.Unix(1_800_000_000, 0)} |
| + var hosts []string |
| + for i := range warmOrdersPerPass { |
| + hosts = append(hosts, "fill"+string(rune('a'+i))+".test") |
| + } |
| + hosts = append(hosts, "zheld.test") // sorted last: the cap reaches it |
| + held := true |
| + w := newCertWarm( |
| + func() []string { return hosts }, |
| + func(h *tls.ClientHelloInfo) (*tls.Certificate, error) { |
| + if h.ServerName == "zheld.test" && held { |
| + return nil, &settlingError{left: 10 * time.Second} |
| + } |
| + return &tls.Certificate{}, nil |
| + }, |
| + func(string) bool { return true }, |
| + nil, |
| + nil, |
| + ) |
| + w.now = clk.now |
| + // First pass: zheld.test alone, so it is held (owed) with the budget |
| + // untouched. |
| + all := hosts |
| + hosts = []string{"zheld.test"} |
| + w.pass() |
| + hosts = all |
| + held = false |
| + clk.advance(10 * time.Second) |
| + // Now the fills spend the whole budget before zheld.test is reached. |
| + if d := w.pass(); d != warmCappedDelay { |
| + t.Fatalf("capped pass with a due hold = %s, want the cap's %s", d, warmCappedDelay) |
| + } |
| + w.mu.Lock() |
| + _, owed := w.settleUntil["zheld.test"] |
| + w.mu.Unlock() |
| + if owed { |
| + t.Fatal("a due hold the cap deferred is still owed: it would shorten every later pass") |
| + } |
| +} |
| + |
| +// The guard: across random hold deadlines, order durations and pass |
| +// timings, after every pass each name that is still held or due is owed a |
| +// pass no later than its deadline (plus slack), and every name is |
| +// eventually warmed. |
| +func TestWarmOwedRetryInvariantSimulation(t *testing.T) { |
| + for seed := range int64(300) { |
| + rng := rand.New(rand.NewPCG(uint64(seed), 7)) |
| + clk := &clockAt{t: time.Unix(1_800_000_000, 0)} |
| + n := 2 + rng.IntN(5) |
| + names := make([]string, n) |
| + until := map[string]time.Time{} |
| + cost := map[string]time.Duration{} |
| + for i := range names { |
| + names[i] = fmt.Sprintf("h%d.test", i) |
| + until[names[i]] = clk.now().Add(time.Duration(rng.IntN(90)) * time.Second) |
| + cost[names[i]] = time.Duration(rng.IntN(40)) * time.Second |
| + } |
| + w := newCertWarm( |
| + func() []string { return names }, |
| + func(h *tls.ClientHelloInfo) (*tls.Certificate, error) { |
| + if u := until[h.ServerName]; clk.now().Before(u) { |
| + return nil, &settlingError{left: u.Sub(clk.now())} |
| + } |
| + clk.advance(cost[h.ServerName]) |
| + return &tls.Certificate{}, nil |
| + }, |
| + func(string) bool { return true }, |
| + nil, |
| + nil, |
| + ) |
| + w.now = clk.now |
| + for step := 0; step < 50; step++ { |
| + d := w.pass() |
| + now := clk.now() |
| + w.mu.Lock() |
| + for _, h := range names { |
| + if w.warmed[h] { |
| + continue |
| + } |
| + due := max(until[h].Sub(now), 0) + warmSettleSlack |
| + if d > due { |
| + w.mu.Unlock() |
| + t.Fatalf("seed %d step %d: next pass in %s, but %s is owed one within %s", seed, step, d, h, due) |
| + } |
| + } |
| + done := len(w.warmed) == n |
| + w.mu.Unlock() |
| + if done { |
| + break |
| + } |
| + // Sometimes an unrelated wake runs a pass early. |
| + if rng.IntN(3) == 0 { |
| + d = time.Duration(rng.Int64N(int64(d) + 1)) |
| + } |
| + clk.advance(d) |
| + } |
| + w.mu.Lock() |
| + got := len(w.warmed) |
| + w.mu.Unlock() |
| + if got != n { |
| + t.Fatalf("seed %d: %d of %d names warmed after 50 passes", seed, got, n) |
| + } |
| + } |
| +} |
diff --git a/internal/edge/certwarm.go b/internal/edge/certwarm.go| index f9fd77434..789903d3e 100644 |
| --- a/internal/edge/certwarm.go |
| +++ b/internal/edge/certwarm.go |
| @@ -155,10 +155,13 @@ type certWarm struct { |
| // recentOrders stamps the new orders placed in the last warmCappedDelay, |
| // the window warmOrdersPerPass is counted over. |
| recentOrders []time.Time |
| - // settleUntil is each held name's absolute deadline (certsettle.go). |
| - // Kept across passes so that a pass run for some other reason, which |
| - // skips a held name on its nextAttempt floor, still comes back for it |
| - // when the hold ends. Cleared on success, on failure and with the set. |
| + // settleUntil is the warm's one rule for held names (certsettle.go, |
| + // #443): a name in it is OWED a pass at max(its deadline, now), and |
| + // owedWait is the only thing that turns it into the next pass's time. |
| + // An entry leaves only through discharge, which runs when the name is |
| + // ordered (success or failure), when it is handed to a schedule that |
| + // brings it back by itself (the order cap, the DNS-miss schedule), and |
| + // when it leaves the set. A new hold simply moves the deadline. |
| settleUntil map[string]time.Time |
| // wake asks the loop for an early pass (nudge). Buffered one deep: a |
| @@ -491,10 +494,14 @@ func (c *certWarm) pass() time.Duration { |
| // would strand exactly the domain whose resolver is briefly sick. |
| if !seen { |
| if orders >= budget { |
| + // Handed to the cap: the capped delay brings it back. |
| capped = true |
| + c.discharge(h) |
| continue |
| } |
| if !c.pointsHere(h) { |
| + // Handed to the DNS-miss schedule. |
| + c.discharge(h) |
| c.dnsMissed(h, now) |
| continue |
| } |
| @@ -522,15 +529,13 @@ func (c *certWarm) pass() time.Duration { |
| c.settling(h, left) |
| continue |
| } |
| - c.mu.Lock() |
| - delete(c.settleUntil, h) |
| - c.mu.Unlock() |
| + c.discharge(h) |
| c.fail(gctx, h, err, records[h]) |
| continue |
| } |
| + c.discharge(h) |
| c.mu.Lock() |
| delete(c.nextAttempt, h) |
| - delete(c.settleUntil, h) |
| first := !c.warmed[h] |
| c.warmed[h] = true |
| c.mu.Unlock() |
| @@ -554,7 +559,7 @@ func (c *certWarm) pass() time.Duration { |
| } |
| } |
| c.mu.Unlock() |
| - settleWait := c.earliestSettle(inSet) |
| + owed, isOwed := c.owedWait(inSet) |
| // Housekeeping last: it is the least urgent thing this pass does, and |
| // running it after the orders means a slow bucket delays litter removal |
| // rather than certificate issuance (certsweep.go). |
| @@ -567,10 +572,10 @@ func (c *certWarm) pass() time.Duration { |
| if capped || (orders > 0 && orders >= budget) { |
| delay = warmCappedDelay |
| } |
| - // Back just after the earliest held name settles, so a new host gets its |
| - // certificate within about a minute of being minted. |
| - if settleWait > 0 { |
| - delay = min(delay, settleWait+warmSettleSlack) |
| + // Back just after the soonest owed name's hold ends, so a new host gets |
| + // its certificate within about a minute of being minted. |
| + if isOwed { |
| + delay = min(delay, owed+warmSettleSlack) |
| } |
| return delay |
| } |
| @@ -578,7 +583,7 @@ func (c *certWarm) pass() time.Duration { |
| // settling puts a held name back for when its hold ends. nextAttempt is the |
| // floor pass() and hasNewHost already honour, so the name is neither retried |
| // early nor counted as new by a wake in the meantime; settleUntil is what |
| -// brings every later pass back for it on time (earliestSettle). |
| +// brings every later pass back for it on time (owedWait). |
| func (c *certWarm) settling(host string, left time.Duration) { |
| until := c.now().Add(left) |
| c.mu.Lock() |
| @@ -933,26 +938,36 @@ func (c *certWarm) prune(ctx context.Context, records map[string]certwarm.Record |
| } |
| } |
| -// earliestSettle is how long until the soonest hold still running ends, |
| -// among the names still in the set, and zero when there is none. It drops |
| -// holds for names that left the set. A hold that has already ended does not |
| -// count: this pass reached its name unless the order cap or the DNS gate |
| -// stopped it, and each of those brings the name back on its own schedule |
| -// (warmCappedDelay, dnsMissed). Counting it would bring the pass straight |
| -// back, over and over, with nothing new to do. |
| -func (c *certWarm) earliestSettle(inSet map[string]bool) time.Duration { |
| +// owedWait is the one place the warm's next pass is derived from held names: |
| +// how long until the soonest owed name may be ordered, counting a deadline |
| +// that has already passed as now, and false when nothing is owed. A due |
| +// name was not reached by the pass that just ran (it ended while another |
| +// order was in flight), so the next pass must come at once. That cannot |
| +// spin: the next pass reaches it, and every way out of that pass (ordered, |
| +// failed, held again, capped, DNS miss) discharges it or moves its deadline. |
| +// Names that left the set are discharged here. |
| +func (c *certWarm) owedWait(inSet map[string]bool) (time.Duration, bool) { |
| now := c.now() |
| c.mu.Lock() |
| defer c.mu.Unlock() |
| var soonest time.Duration |
| + found := false |
| for h, until := range c.settleUntil { |
| if !inSet[h] { |
| delete(c.settleUntil, h) |
| continue |
| } |
| - if left := until.Sub(now); left > 0 && (soonest == 0 || left < soonest) { |
| - soonest = left |
| + left := max(until.Sub(now), 0) |
| + if !found || left < soonest { |
| + soonest, found = left, true |
| } |
| } |
| - return soonest |
| + return soonest, found |
| +} |
| + |
| +// discharge ends what the warm owes a held name (see settleUntil). |
| +func (c *certWarm) discharge(host string) { |
| + c.mu.Lock() |
| + delete(c.settleUntil, host) |
| + c.mu.Unlock() |
| } |
diff --git a/internal/provision/testdata/plan14-root-reads-cert-warm-propagation-443.tsv b/internal/provision/testdata/plan14-root-reads-cert-warm-propagation-443.tsv| index 864c8a1cf..a1eb176fd 100644 |
| --- a/internal/provision/testdata/plan14-root-reads-cert-warm-propagation-443.tsv |
| +++ b/internal/provision/testdata/plan14-root-reads-cert-warm-propagation-443.tsv |
| @@ -1,3 +1,3 @@ |
| # Census overlay cert-warm-propagation-443, written by census-propose (#369). census-ruling-cert-warm-propagation-443, approved on #280 before the merge; the approval card is docs/superpowers/notes/census-proposal-cert-warm-propagation-443.md. |
| # 0 body, 0 sites, 1 envelope: each changed unit re-frozen at its existing disposition with its claim appended. |
| -@coverage internal/edge c82f99e34aa642e5c9b701f9505094a5f923c945f6c0281f92b9da7abd546667 |
| +@coverage internal/edge f89991c62c1ccdf40bbeff9194b2ff34a0adc571bea8d2f02df5a7d0cabfca3f |