edge: cert warm review round 2 fixes (#386)
Astra round 2 found three partial fixes; each is now structural: - Order cap is a rolling window: at most warmOrdersPerPass new orders per warmCappedDelay across every pass, and a pass that spends the budget comes back in warmCappedDelay even if it skipped nothing. - DNS re-checks are a DNS-only probe on a per-host schedule (1 min for 10 min, 5 min to 1 h, then hourly). It reads no outcome records; a full pass runs only once a pending host points here. An empty edge set read is reused for 30 s so a bucket outage costs one read per burst, not one per host. - The loop re-checks the in-memory issuer gate every minute and runs a pass when the lease arrives, covering a wake dropped while this box was briefly not the issuer. Stand-down passes return to hourly. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Human
Prompt
Can we see why certs are taking 20 mins to be assigned? Yes
From this commit's own message. The prompt ledger has no entries for it — which is what a squashed, rebased or amended commit looks like, and what every commit looks like in a repo without a ledger.
Summary
Rolling order budget, DNS-only probe on a per-host schedule, and a gate tick for lease arrival.
Agent notes
Eight mutations, each caught by a named test. Census re-proposed (envelope internal/edge).
4 files changed,
+439
−163
internal/edge/certwarm.go+196 −87internal/edge/certwarm_test.go+2 −5internal/edge/certwarm_wake_test.go+240 −70internal/provision/testdata/plan14-root-reads-certwarm-wake.tsv+1 −1
CI failed — run details
diff --git a/internal/edge/certwarm.go b/internal/edge/certwarm.go| index 145bed3ee..c8e0da74a 100644 |
| --- a/internal/edge/certwarm.go |
| +++ b/internal/edge/certwarm.go |
| @@ -55,35 +55,45 @@ const ( |
| // limit are what turn a transient limit into a sustained one, and every |
| // host in the deployment shares the one ACME account's budget. |
| warmRateLimitBackoff = 12 * time.Hour |
| - // warmOrdersPerPass bounds NEW orders per pass. Renewal re-arms are not |
| + // warmOrdersPerPass bounds NEW orders per warmCappedDelay, across every |
| + // pass in that window and not merely within one: since passes can be |
| + // woken (#386), a per-pass count alone would let back-to-back wakes spend |
| + // the shared account as fast as reloads arrive. Renewal re-arms are not |
| // orders and are never capped — they are the reason the pass exists. |
| // When the cap bites, pass() returns a short delay so a backlog drains |
| // in minutes rather than trickling out over days (the wildcard pass() |
| // precedent). |
| warmOrdersPerPass = 10 |
| - // warmCappedDelay is how soon to come back with capped work remaining. |
| + // warmCappedDelay is how soon to come back with capped work remaining, |
| + // and the window warmOrdersPerPass is counted over. |
| warmCappedDelay = time.Minute |
| // The DNS-miss schedule for a NEW host (#386). A customer usually |
| // finishes their DNS a few minutes after attaching, so the first ten |
| // minutes of misses are re-checked every minute and the rest of the first |
| - // hour every five. After an hour the host waits for the hourly pass like |
| - // everything else, so an attach whose owner never points it costs the |
| - // issuer nothing past its first hour. |
| + // hour every five. The re-checks are DNS-only (probeDNS): they read |
| + // nothing from the bucket beyond the edge set's own reuse window, and a |
| + // full pass runs only once a pending host points here. After an hour the |
| + // host waits for the hourly pass like everything else. |
| warmDNSRetryFast = time.Minute |
| warmDNSFastFor = 10 * time.Minute |
| warmDNSRetrySlow = 5 * time.Minute |
| warmDNSSlowFor = time.Hour |
| - // warmGateRecheck is how soon a box that is not the issuer looks at the |
| - // gate again (#386). The gate is the housekeeper lease, held in memory, |
| - // so a stand-down pass reads nothing from the bucket and costs nothing; |
| - // checking it every minute means a box that takes over the lease starts |
| - // ordering within a minute, not up to an hour later. |
| + // warmGateRecheck is how often the loop looks at the issuer gate between |
| + // passes (#386). The gate is the housekeeper lease, held in memory, so a |
| + // look costs nothing; a box that takes the lease over (or takes it BACK |
| + // during its hourly wait, having dropped wakes while it was not the |
| + // issuer) runs a pass within a minute instead of at its next hourly one. |
| warmGateRecheck = time.Minute |
| // warmEdgeSetTTL is how long the DNS gate reuses the deployment's edge |
| // set. Reading it lists and fetches every box's snapshot and edges file, |
| // and the gate runs once per new host per pass; without the reuse the |
| // fast DNS-miss schedule would multiply that by every pending host. |
| warmEdgeSetTTL = 5 * time.Minute |
| + // warmEdgeSetEmptyTTL is how long an EMPTY edge-set read is reused. Short, |
| + // because empty means the read failed and the gate is failing closed; long |
| + // enough that one pass over many new hosts reads the bucket once, not once |
| + // per host, while the bucket is down. |
| + warmEdgeSetEmptyTTL = 30 * time.Second |
| ) |
| // certWarm proactively orders and renews per-host certificates. |
| @@ -134,16 +144,22 @@ type certWarm struct { |
| // so a first order can be told from a renewal re-arm for the order cap |
| // and for logging. |
| warmed map[string]bool |
| - // dnsMissSince is when each never-warmed host FIRST failed the DNS gate, |
| - // for the DNS-miss schedule. Cleared when the host passes the gate and |
| - // pruned with the set. |
| - dnsMissSince map[string]time.Time |
| + // dnsMisses is the DNS-miss schedule: each never-warmed host that has |
| + // failed the DNS gate, with when it first did and when to look again. |
| + // Cleared when the host's order goes out and pruned with the set. |
| + dnsMisses map[string]dnsMiss |
| + // recentOrders stamps the new orders placed in the last warmCappedDelay, |
| + // the window warmOrdersPerPass is counted over. |
| + recentOrders []time.Time |
| // wake asks the loop for an early pass (nudge). Buffered one deep: a |
| // wake already pending covers any that arrive behind it. |
| wake chan struct{} |
| // now is the clock; a field so the DNS-miss schedule is testable. |
| now func() time.Time |
| + // gateTick is the loop's gate re-check cadence, warmGateRecheck outside |
| + // tests. |
| + gateTick time.Duration |
| // passDone, when set, runs after every pass the loop runs. Tests only. |
| passDone func() |
| @@ -159,19 +175,26 @@ type certWarm struct { |
| func newCertWarm(hosts func() []string, getCert func(*tls.ClientHelloInfo) (*tls.Certificate, error), pointsHere func(string) bool, issuer IssuerGate, store release.Store) *certWarm { |
| return &certWarm{ |
| - hosts: hosts, |
| - getCert: getCert, |
| - pointsHere: pointsHere, |
| - issuer: issuer, |
| - store: store, |
| - nextAttempt: map[string]time.Time{}, |
| - warmed: map[string]bool{}, |
| - dnsMissSince: map[string]time.Time{}, |
| - wake: make(chan struct{}, 1), |
| - now: time.Now, |
| + hosts: hosts, |
| + getCert: getCert, |
| + pointsHere: pointsHere, |
| + issuer: issuer, |
| + store: store, |
| + nextAttempt: map[string]time.Time{}, |
| + warmed: map[string]bool{}, |
| + dnsMisses: map[string]dnsMiss{}, |
| + wake: make(chan struct{}, 1), |
| + now: time.Now, |
| + gateTick: warmGateRecheck, |
| } |
| } |
| +// dnsMiss is one host's place on the DNS-miss schedule. A zero next means |
| +// the host has aged out of the schedule and waits for the hourly pass. |
| +type dnsMiss struct { |
| + first, next time.Time |
| +} |
| + |
| // issuerGate is this warm's read of the deployment's gate, ungated default |
| // applied (see IssuerGate, certsync.go). |
| func (c *certWarm) issuerGate() (context.Context, bool) { |
| @@ -184,41 +207,61 @@ func (c *certWarm) issuerGate() (context.Context, bool) { |
| func (c *certWarm) run() { c.loop(nil) } |
| // loop runs a pass, then waits for whichever comes first: the delay that pass |
| -// asked for, or a wake that brings a host the warm has never seen (#386). |
| +// asked for, a wake that brings a host the warm has never seen (#386), a |
| +// pending host's DNS re-check, or the gate tick. |
| // |
| // Without the wake a custom domain attached between passes waited up to an |
| // hour for its certificate: only the issuer orders, latency steering keeps |
| // visitors on the other boxes, so no first handshake ever reached the order |
| -// path either. A wake carrying nothing new is dropped and leaves the timer |
| +// path either. A wake carrying nothing new is dropped and leaves the schedule |
| // alone, because wakes come from every route reload and every home-map change |
| -// in the fleet, and a pass costs a records LIST and a re-arm of every host. |
| +// in the fleet, and a pass costs a records LIST and a re-arm of every host. A |
| +// wake is also dropped when the next pass is due within warmCappedDelay |
| +// anyway, which is the backlog case: the capped hosts read as new, and that |
| +// pass is already coming for them. |
| // |
| -// A wake is also dropped when the next pass is due within warmCappedDelay |
| -// anyway. That is what keeps the per-pass order cap a RATE: hosts the cap |
| -// skipped look new to hasNewHost, so without it every reload during a |
| -// backlog would start another capped batch and spend the shared ACME |
| -// account faster than one batch a minute. |
| +// The gate tick runs a pass when this box has just become the issuer, since |
| +// a wake that arrived while it was not was dropped. A DNS re-check runs a |
| +// pass only when a pending host now points here. |
| // |
| // stop ends the loop; nil runs it for the life of the process. |
| func (c *certWarm) loop(stop <-chan struct{}) { |
| - d := c.runPass() |
| - due := time.Now().Add(d) |
| - timer := time.NewTimer(d) |
| + passDue := time.Now().Add(c.runPass()) |
| + issuer := c.isIssuer() |
| + timer := time.NewTimer(time.Hour) |
| defer timer.Stop() |
| for { |
| + wait := min(time.Until(passDue), c.gateTick) |
| + // Only the issuer has DNS re-checks to run: probeDNS stands down |
| + // without touching the schedule otherwise, and a due re-check it |
| + // cannot advance would spin this loop. |
| + if d, ok := c.dnsWait(); ok && issuer { |
| + wait = min(wait, d) |
| + } |
| + timer.Reset(max(wait, 0)) |
| + full := false |
| select { |
| case <-stop: |
| return |
| - case <-timer.C: |
| case <-c.wake: |
| - if time.Until(due) <= warmCappedDelay || !c.hasNewHost() { |
| - continue |
| - } |
| timer.Stop() |
| + full = time.Until(passDue) > warmCappedDelay && c.hasNewHost() |
| + case <-timer.C: |
| + now := c.isIssuer() |
| + switch { |
| + case !time.Now().Before(passDue): |
| + full = true |
| + case now && !issuer: |
| + full = true |
| + default: |
| + full = c.probeDNS() |
| + } |
| + issuer = now |
| + } |
| + if full { |
| + passDue = time.Now().Add(c.runPass()) |
| + issuer = c.isIssuer() |
| } |
| - d = c.runPass() |
| - due = time.Now().Add(d) |
| - timer.Reset(d) |
| } |
| } |
| @@ -230,6 +273,11 @@ func (c *certWarm) runPass() time.Duration { |
| return d |
| } |
| +func (c *certWarm) isIssuer() bool { |
| + _, ok := c.issuerGate() |
| + return ok |
| +} |
| + |
| // nudge asks for an early pass. It never blocks: it is called from the |
| // edge's reload path and the home map's refresh. Safe on a nil warm, which is |
| // what a deployment without one has. |
| @@ -248,7 +296,7 @@ func (c *certWarm) nudge() { |
| // never been warmed, is not backing off, and is not already on the DNS-miss |
| // schedule. Everything else in the set has its own next pass already. |
| func (c *certWarm) hasNewHost() bool { |
| - if _, ok := c.issuerGate(); !ok { |
| + if !c.isIssuer() { |
| return false |
| } |
| now := c.now() |
| @@ -262,7 +310,7 @@ func (c *certWarm) hasNewHost() bool { |
| if until, ok := c.nextAttempt[h]; ok && now.Before(until) { |
| continue |
| } |
| - if _, ok := c.dnsMissSince[h]; ok { |
| + if _, ok := c.dnsMisses[h]; ok { |
| continue |
| } |
| return true |
| @@ -270,23 +318,86 @@ func (c *certWarm) hasNewHost() bool { |
| return false |
| } |
| -// dnsMissed records that a never-warmed host failed the DNS gate and returns |
| -// how soon to check it again, 0 meaning the ordinary hourly pass. |
| -func (c *certWarm) dnsMissed(host string, now time.Time) time.Duration { |
| +// dnsMissed puts host on the DNS-miss schedule, or moves it along: its first |
| +// miss is kept, and its next check is set by how long it has been missing. |
| +func (c *certWarm) dnsMissed(host string, now time.Time) { |
| c.mu.Lock() |
| - first, ok := c.dnsMissSince[host] |
| + defer c.mu.Unlock() |
| + m, ok := c.dnsMisses[host] |
| if !ok { |
| - first = now |
| - c.dnsMissSince[host] = now |
| + m.first = now |
| } |
| - c.mu.Unlock() |
| - switch missing := now.Sub(first); { |
| + m.next = time.Time{} |
| + switch missing := now.Sub(m.first); { |
| case missing < warmDNSFastFor: |
| - return warmDNSRetryFast |
| + m.next = now.Add(warmDNSRetryFast) |
| case missing < warmDNSSlowFor: |
| - return warmDNSRetrySlow |
| + m.next = now.Add(warmDNSRetrySlow) |
| + } |
| + c.dnsMisses[host] = m |
| +} |
| + |
| +// dnsWait is how long until the soonest scheduled DNS re-check, and false |
| +// when nothing is scheduled. |
| +func (c *certWarm) dnsWait() (time.Duration, bool) { |
| + now := c.now() |
| + c.mu.Lock() |
| + defer c.mu.Unlock() |
| + var soonest time.Time |
| + for _, m := range c.dnsMisses { |
| + if !m.next.IsZero() && (soonest.IsZero() || m.next.Before(soonest)) { |
| + soonest = m.next |
| + } |
| + } |
| + if soonest.IsZero() { |
| + return 0, false |
| + } |
| + return soonest.Sub(now), true |
| +} |
| + |
| +// probeDNS re-checks the DNS of every pending host whose turn has come, and |
| +// reports whether any of them now points here, the only case worth a full |
| +// pass. It reads nothing from the bucket but the edge set, and that only once |
| +// per reuse window. Every host it checks moves along the schedule whatever |
| +// the answer, so a host that points but cannot be ordered yet (the order cap) |
| +// is looked at again on its schedule rather than in a loop. |
| +func (c *certWarm) probeDNS() bool { |
| + if !c.isIssuer() { |
| + return false |
| + } |
| + now := c.now() |
| + c.mu.Lock() |
| + var due []string |
| + for h, m := range c.dnsMisses { |
| + if !m.next.IsZero() && !now.Before(m.next) { |
| + due = append(due, h) |
| + } |
| + } |
| + c.mu.Unlock() |
| + sort.Strings(due) |
| + points := false |
| + for _, h := range due { |
| + if c.pointsHere(h) { |
| + points = true |
| + } |
| + c.dnsMissed(h, now) |
| + } |
| + return points |
| +} |
| + |
| +// orderBudget is how many new orders the window still allows, after |
| +// forgetting the stamps that have left it. |
| +func (c *certWarm) orderBudget(now time.Time) int { |
| + c.mu.Lock() |
| + defer c.mu.Unlock() |
| + kept := c.recentOrders[:0] |
| + for _, t := range c.recentOrders { |
| + if now.Sub(t) < warmCappedDelay { |
| + kept = append(kept, t) |
| + } |
| } |
| - return 0 |
| + c.recentOrders = kept |
| + return warmOrdersPerPass - len(kept) |
| } |
| // pass warms every host in the set once and returns how long to wait. |
| @@ -301,11 +412,11 @@ func (c *certWarm) pass() time.Duration { |
| ctx, ok := c.issuerGate() |
| if !ok { |
| c.logStandDown(len(hosts)) |
| - return warmGateRecheck |
| + return warmInterval |
| } |
| if len(hosts) == 0 { |
| c.mu.Lock() |
| - clear(c.dnsMissSince) |
| + clear(c.dnsMisses) |
| c.mu.Unlock() |
| c.sweep(ctx, time.Now()) |
| return warmInterval |
| @@ -322,9 +433,7 @@ func (c *certWarm) pass() time.Duration { |
| inSet[h] = true |
| } |
| orders, capped := 0, false |
| - // dnsRetry is the soonest DNS-miss re-check any new host asked for, 0 |
| - // when none did. |
| - var dnsRetry time.Duration |
| + budget := c.orderBudget(c.now()) |
| for _, h := range hosts { |
| now := c.now() |
| c.mu.Lock() |
| @@ -340,7 +449,7 @@ func (c *certWarm) pass() time.Duration { |
| gctx, ok := c.issuerGate() |
| if !ok { |
| c.logStandDown(len(hosts) - orders) |
| - return warmGateRecheck |
| + return warmInterval |
| } |
| // A host with no cert yet is a NEW order and is both capped and |
| // DNS-gated. A host already warmed is a renewal re-arm: uncapped and |
| @@ -348,18 +457,17 @@ func (c *certWarm) pass() time.Duration { |
| // re-arms the timer that renewal depends on. Gating the re-arm on DNS |
| // would strand exactly the domain whose resolver is briefly sick. |
| if !seen { |
| - if orders >= warmOrdersPerPass { |
| + if orders >= budget { |
| capped = true |
| continue |
| } |
| if !c.pointsHere(h) { |
| - if d := c.dnsMissed(h, now); d > 0 && (dnsRetry == 0 || d < dnsRetry) { |
| - dnsRetry = d |
| - } |
| + c.dnsMissed(h, now) |
| continue |
| } |
| c.mu.Lock() |
| - delete(c.dnsMissSince, h) |
| + delete(c.dnsMisses, h) |
| + c.recentOrders = append(c.recentOrders, now) |
| c.mu.Unlock() |
| orders++ |
| } |
| @@ -386,9 +494,9 @@ func (c *certWarm) pass() time.Duration { |
| } |
| c.prune(ctx, records, inSet) |
| c.mu.Lock() |
| - for h := range c.dnsMissSince { |
| + for h := range c.dnsMisses { |
| if !inSet[h] { |
| - delete(c.dnsMissSince, h) |
| + delete(c.dnsMisses, h) |
| } |
| } |
| c.mu.Unlock() |
| @@ -396,14 +504,14 @@ func (c *certWarm) pass() time.Duration { |
| // running it after the orders means a slow bucket delays litter removal |
| // rather than certificate issuance (certsweep.go). |
| c.sweep(ctx, time.Now()) |
| - delay := warmInterval |
| - if capped { |
| - delay = warmCappedDelay |
| - } |
| - if dnsRetry > 0 && dnsRetry < delay { |
| - delay = dnsRetry |
| - } |
| - return delay |
| + // Back soon when the window's budget is spent, whether or not this pass |
| + // met a host it had to skip: a host attached a moment later must not be |
| + // left for the hourly pass because its wake arrived while the budget was |
| + // empty. |
| + if capped || (orders > 0 && orders >= budget) { |
| + return warmCappedDelay |
| + } |
| + return warmInterval |
| } |
| // warmOne asks autocert for host's certificate, which mints it when absent |
| @@ -579,8 +687,9 @@ func wildcardCoverer(parents func() []string) func(string) bool { |
| // anybody's DNS, and the cost of waiting a pass is one hour of delay against |
| // the cost of burning a shared rate limit. |
| // |
| -// The edge set is reused for warmEdgeSetTTL. An empty read is never reused: |
| -// it is the fail-closed case above, and the next check should try again. |
| +// The edge set is reused for warmEdgeSetTTL, and an empty read (the |
| +// fail-closed case above) only for warmEdgeSetEmptyTTL, so a bucket outage |
| +// costs one read per half minute rather than one per host. |
| func dnsGate(resolve func(string) ([]string, error), edgeIPs func() []string) func(string) bool { |
| return dnsGateAt(resolve, edgeIPs, time.Now) |
| } |
| @@ -594,19 +703,19 @@ func dnsGateAt(resolve func(string) ([]string, error), edgeIPs func() []string, |
| edgeSet := func() map[string]bool { |
| mu.Lock() |
| defer mu.Unlock() |
| - if cached != nil && now().Sub(readAt) < warmEdgeSetTTL { |
| + ttl := warmEdgeSetTTL |
| + if len(cached) == 0 { |
| + ttl = warmEdgeSetEmptyTTL |
| + } |
| + if cached != nil && now().Sub(readAt) < ttl { |
| return cached |
| } |
| - set := map[string]bool{} |
| + cached = map[string]bool{} |
| for _, ip := range edgeIPs() { |
| - set[ip] = true |
| - } |
| - if len(set) == 0 { |
| - cached = nil |
| - return set |
| + cached[ip] = true |
| } |
| - cached, readAt = set, now() |
| - return set |
| + readAt = now() |
| + return cached |
| } |
| return func(host string) bool { |
| set := edgeSet() |
diff --git a/internal/edge/certwarm_test.go b/internal/edge/certwarm_test.go| index 1769f16e2..26b4d8160 100644 |
| --- a/internal/edge/certwarm_test.go |
| +++ b/internal/edge/certwarm_test.go |
| @@ -202,11 +202,8 @@ func TestWarmStandsDownWhenNotTheIssuer(t *testing.T) { |
| func() (context.Context, bool) { return nil, false }, |
| nil, |
| ) |
| - // Back within a minute, not an hour (#386): the gate is an in-memory |
| - // lease read, and a box that takes the lease over must start ordering |
| - // promptly. |
| - if d := w.pass(); d != warmGateRecheck { |
| - t.Fatalf("delay = %s, want %s", d, warmGateRecheck) |
| + if d := w.pass(); d != warmInterval { |
| + t.Fatalf("delay = %s", d) |
| } |
| if calls != 0 { |
| t.Fatalf("a non-issuer placed %d call(s)", calls) |
diff --git a/internal/edge/certwarm_wake_test.go b/internal/edge/certwarm_wake_test.go| index 6ac8c0146..9630c3832 100644 |
| --- a/internal/edge/certwarm_wake_test.go |
| +++ b/internal/edge/certwarm_wake_test.go |
| @@ -31,80 +31,132 @@ func (f *fakeClock) advance(d time.Duration) { |
| f.mu.Unlock() |
| } |
| -// A new host whose DNS does not point here yet is checked again within |
| -// minutes, not at the next hourly pass. The customer usually finishes their |
| -// DNS a few minutes after attaching, and every minute after that is a minute |
| -// their domain is down for no reason. The schedule slows as the miss ages, |
| -// and a host that has missed for an hour goes back to the hourly pass, so an |
| -// abandoned attach does not keep the issuer busy. |
| -func TestWarmRetriesANewHostsDNSMissWithinMinutes(t *testing.T) { |
| +// dnsWarm is a warm over one new host whose DNS answer the test controls, |
| +// on a fake clock, counting DNS checks and orders. |
| +func dnsWarm(t *testing.T, host string, pointing *atomic.Bool) (*certWarm, *fakeClock, *atomic.Int32, *atomic.Int32) { |
| + t.Helper() |
| clock := &fakeClock{t: time.Unix(1_800_000_000, 0)} |
| + var checks, orders atomic.Int32 |
| w := newCertWarm( |
| - func() []string { return []string{"www.customer.test"} }, |
| - func(*tls.ClientHelloInfo) (*tls.Certificate, error) { return &tls.Certificate{}, nil }, |
| - func(string) bool { return false }, |
| + func() []string { return []string{host} }, |
| + func(*tls.ClientHelloInfo) (*tls.Certificate, error) { orders.Add(1); return &tls.Certificate{}, nil }, |
| + func(string) bool { checks.Add(1); return pointing.Load() }, |
| nil, |
| nil, |
| ) |
| w.now = clock.now |
| - if d := w.pass(); d != warmDNSRetryFast { |
| - t.Fatalf("first miss: delay = %s, want %s", d, warmDNSRetryFast) |
| + return w, clock, &checks, &orders |
| +} |
| + |
| +// A new host whose DNS does not point here yet is checked again within |
| +// minutes, not at the next hourly pass. The customer usually finishes their |
| +// DNS a few minutes after attaching, and every minute after that is a minute |
| +// their domain is down for no reason. The schedule slows as the miss ages, |
| +// and a host that has missed for an hour goes back to the hourly pass, so an |
| +// abandoned attach does not keep the issuer busy. |
| +func TestWarmSchedulesANewHostsDNSMissWithinMinutes(t *testing.T) { |
| + var pointing atomic.Bool |
| + w, clock, _, _ := dnsWarm(t, "www.customer.test", &pointing) |
| + if d := w.pass(); d != warmInterval { |
| + t.Fatalf("pass delay = %s, want %s: the re-check is the probe's, not a pass", d, warmInterval) |
| + } |
| + if d, ok := w.dnsWait(); !ok || d != warmDNSRetryFast { |
| + t.Fatalf("first miss: re-check in %s (%v), want %s", d, ok, warmDNSRetryFast) |
| } |
| clock.advance(warmDNSFastFor + time.Second) |
| - if d := w.pass(); d != warmDNSRetrySlow { |
| - t.Fatalf("after %s of misses: delay = %s, want %s", warmDNSFastFor, d, warmDNSRetrySlow) |
| + w.probeDNS() |
| + if d, ok := w.dnsWait(); !ok || d != warmDNSRetrySlow { |
| + t.Fatalf("after %s of misses: re-check in %s (%v), want %s", warmDNSFastFor, d, ok, warmDNSRetrySlow) |
| } |
| clock.advance(warmDNSSlowFor) |
| - if d := w.pass(); d != warmInterval { |
| - t.Fatalf("after an hour of misses: delay = %s, want the hourly %s", d, warmInterval) |
| + w.probeDNS() |
| + if d, ok := w.dnsWait(); ok { |
| + t.Fatalf("after an hour of misses a re-check is still scheduled in %s; want the hourly pass only", d) |
| } |
| } |
| -// The miss clock starts at the FIRST miss, not at every pass: a host that |
| +// The miss clock starts at the FIRST miss, not at every check: a host that |
| // keeps missing must age into the slower schedule. |
| func TestWarmDNSMissClockStartsAtTheFirstMiss(t *testing.T) { |
| - clock := &fakeClock{t: time.Unix(1_800_000_000, 0)} |
| - w := newCertWarm( |
| - func() []string { return []string{"www.customer.test"} }, |
| - func(*tls.ClientHelloInfo) (*tls.Certificate, error) { return &tls.Certificate{}, nil }, |
| - func(string) bool { return false }, |
| - nil, |
| - nil, |
| - ) |
| - w.now = clock.now |
| - for i := 0; i < 11; i++ { |
| - w.pass() |
| - clock.advance(time.Minute) |
| + var pointing atomic.Bool |
| + w, clock, _, _ := dnsWarm(t, "www.customer.test", &pointing) |
| + w.pass() |
| + for i := 0; i < int(warmDNSFastFor/warmDNSRetryFast); i++ { |
| + clock.advance(warmDNSRetryFast) |
| + w.probeDNS() |
| + } |
| + // The last check came warmDNSFastFor after the first miss, so the host |
| + // is now on the slow schedule. Measured from each check instead, it |
| + // would still be on the fast one. |
| + if d, ok := w.dnsWait(); !ok || d != warmDNSRetrySlow { |
| + t.Fatalf("after %s of one-minute checks: re-check in %s (%v), want %s", warmDNSFastFor, d, ok, warmDNSRetrySlow) |
| + } |
| +} |
| + |
| +// The probe is DNS-only. A host not yet due is not looked up, a due host |
| +// still missing runs no pass, and only a due host that now points asks for |
| +// one. None of it orders anything: ordering is the pass's job. |
| +func TestWarmProbeChecksOnlyDueHostsAndOrdersNothing(t *testing.T) { |
| + var pointing atomic.Bool |
| + w, clock, checks, orders := dnsWarm(t, "www.customer.test", &pointing) |
| + w.pass() |
| + if checks.Load() != 1 { |
| + t.Fatalf("checks after the pass = %d, want 1", checks.Load()) |
| + } |
| + if w.probeDNS() || checks.Load() != 1 { |
| + t.Fatalf("a probe before the host was due looked it up (checks %d)", checks.Load()) |
| + } |
| + clock.advance(warmDNSRetryFast) |
| + if w.probeDNS() { |
| + t.Fatal("a probe of a host still missing asked for a pass") |
| } |
| - if d := w.pass(); d != warmDNSRetrySlow { |
| - t.Fatalf("after 11 one-minute passes: delay = %s, want %s", d, warmDNSRetrySlow) |
| + if checks.Load() != 2 { |
| + t.Fatalf("checks = %d, want 2", checks.Load()) |
| + } |
| + pointing.Store(true) |
| + clock.advance(warmDNSRetryFast) |
| + if !w.probeDNS() { |
| + t.Fatal("a probe of a host that now points did not ask for a pass") |
| + } |
| + if orders.Load() != 0 { |
| + t.Fatalf("the probe ordered %d time(s)", orders.Load()) |
| + } |
| + // It moved the host along even though it points: a host the pass then |
| + // cannot order (the cap) is looked at on its schedule, not in a loop. |
| + if d, ok := w.dnsWait(); !ok || d <= 0 { |
| + t.Fatalf("after a pointing probe: re-check in %s (%v), want a future one", d, ok) |
| + } |
| +} |
| + |
| +// A non-issuer's probe looks nothing up. |
| +func TestWarmProbeOnANonIssuerChecksNothing(t *testing.T) { |
| + var pointing atomic.Bool |
| + w, clock, checks, _ := dnsWarm(t, "www.customer.test", &pointing) |
| + w.pass() |
| + w.issuer = func() (context.Context, bool) { return nil, false } |
| + clock.advance(warmDNSRetryFast) |
| + if w.probeDNS() || checks.Load() != 1 { |
| + t.Fatalf("a non-issuer's probe looked up DNS (checks %d)", checks.Load()) |
| } |
| } |
| // Once the name points here and the order goes out, its miss history is |
| // gone: a later miss (the host leaves and comes back) starts a fresh fast |
| // schedule rather than inheriting an old one. |
| -func TestWarmDNSMissClockClearsWhenTheHostPoints(t *testing.T) { |
| - clock := &fakeClock{t: time.Unix(1_800_000_000, 0)} |
| - pointing := false |
| - w := newCertWarm( |
| - func() []string { return []string{"www.customer.test"} }, |
| - func(*tls.ClientHelloInfo) (*tls.Certificate, error) { return &tls.Certificate{}, nil }, |
| - func(string) bool { return pointing }, |
| - nil, |
| - nil, |
| - ) |
| - w.now = clock.now |
| +func TestWarmDNSMissClockClearsWhenTheOrderGoesOut(t *testing.T) { |
| + var pointing atomic.Bool |
| + w, clock, _, orders := dnsWarm(t, "www.customer.test", &pointing) |
| w.pass() |
| - pointing = true |
| + pointing.Store(true) |
| clock.advance(time.Minute) |
| - if d := w.pass(); d != warmInterval { |
| - t.Fatalf("ordered pass: delay = %s, want %s", d, warmInterval) |
| + w.pass() |
| + if orders.Load() != 1 { |
| + t.Fatalf("orders = %d, want 1", orders.Load()) |
| } |
| w.mu.Lock() |
| defer w.mu.Unlock() |
| - if len(w.dnsMissSince) != 0 { |
| - t.Fatalf("miss history survived the order: %v", w.dnsMissSince) |
| + if len(w.dnsMisses) != 0 { |
| + t.Fatalf("miss history survived the order: %v", w.dnsMisses) |
| } |
| } |
| @@ -123,15 +175,15 @@ func TestWarmDNSMissHistoryIsPrunedWithTheSet(t *testing.T) { |
| w.pass() |
| w.mu.Lock() |
| defer w.mu.Unlock() |
| - if _, ok := w.dnsMissSince["gone.customer.test"]; ok { |
| + if _, ok := w.dnsMisses["gone.customer.test"]; ok { |
| t.Fatal("a host that left the set kept its miss history") |
| } |
| } |
| -// The fast retry is for NEW orders only. A warmed host's re-arm ignores DNS |
| -// (TestWarmRenewalReArmIgnoresTheDNSGate), so its resolver being sick must not |
| -// shorten the pass either. |
| -func TestWarmDNSRetryDoesNotApplyToWarmedHosts(t *testing.T) { |
| +// The schedule is for NEW orders only. A warmed host's re-arm ignores DNS |
| +// (TestWarmRenewalReArmIgnoresTheDNSGate), so its resolver being sick must |
| +// not put it on the schedule either. |
| +func TestWarmDNSScheduleDoesNotApplyToWarmedHosts(t *testing.T) { |
| pointing := true |
| w := newCertWarm( |
| func() []string { return []string{"www.customer.test"} }, |
| @@ -142,8 +194,54 @@ func TestWarmDNSRetryDoesNotApplyToWarmedHosts(t *testing.T) { |
| ) |
| w.pass() |
| pointing = false |
| - if d := w.pass(); d != warmInterval { |
| - t.Fatalf("delay = %s, want %s — a warmed host is not DNS-gated", d, warmInterval) |
| + w.pass() |
| + if d, ok := w.dnsWait(); ok { |
| + t.Fatalf("a warmed host was put on the DNS schedule (re-check in %s)", d) |
| + } |
| +} |
| + |
| +// The order cap is a rate over warmCappedDelay, not a count per pass: a |
| +// second pass straight after a full one places nothing, and comes back soon. |
| +func TestWarmOrderBudgetSpansPasses(t *testing.T) { |
| + clock := &fakeClock{t: time.Unix(1_800_000_000, 0)} |
| + var hosts []string |
| + for i := 0; i < warmOrdersPerPass; i++ { |
| + hosts = append(hosts, string(rune('a'+i))+".customer.test") |
| + } |
| + // orders counts FIRST asks per host; later asks are renewal re-arms, |
| + // which are never capped. |
| + var mu sync.Mutex |
| + asked := map[string]bool{} |
| + var orders atomic.Int32 |
| + w := newCertWarm( |
| + func() []string { return hosts }, |
| + func(h *tls.ClientHelloInfo) (*tls.Certificate, error) { |
| + mu.Lock() |
| + defer mu.Unlock() |
| + if !asked[h.ServerName] { |
| + asked[h.ServerName] = true |
| + orders.Add(1) |
| + } |
| + return &tls.Certificate{}, nil |
| + }, |
| + func(string) bool { return true }, |
| + nil, |
| + nil, |
| + ) |
| + w.now = clock.now |
| + // Exactly the budget, nothing skipped: still back soon, because a host |
| + // attached a moment later would otherwise wait for the hourly pass. |
| + if d := w.pass(); d != warmCappedDelay || orders.Load() != warmOrdersPerPass { |
| + t.Fatalf("first pass: delay %s, orders %d; want %s and %d", d, orders.Load(), warmCappedDelay, warmOrdersPerPass) |
| + } |
| + hosts = append(hosts, "late.customer.test") |
| + clock.advance(time.Second) |
| + if d := w.pass(); d != warmCappedDelay || orders.Load() != warmOrdersPerPass { |
| + t.Fatalf("second pass inside the window: delay %s, orders %d; want %s and still %d", d, orders.Load(), warmCappedDelay, warmOrdersPerPass) |
| + } |
| + clock.advance(warmCappedDelay) |
| + if d := w.pass(); d != warmInterval || orders.Load() != warmOrdersPerPass+1 { |
| + t.Fatalf("pass after the window: delay %s, orders %d; want %s and %d", d, orders.Load(), warmInterval, warmOrdersPerPass+1) |
| } |
| } |
| @@ -362,43 +460,115 @@ func TestDNSGateReusesTheEdgeSet(t *testing.T) { |
| } |
| } |
| -// An empty edge set read is the fail-closed case, not an answer to keep: the |
| -// next check reads again instead of holding every order for the TTL. |
| -func TestDNSGateDoesNotReuseAnEmptyEdgeSet(t *testing.T) { |
| +// An empty edge set read is the fail-closed case. It is reused only briefly, |
| +// so one pass over many new hosts reads the bucket once while it is down, |
| +// and the gate opens again soon after the bucket comes back. |
| +func TestDNSGateReusesAnEmptyEdgeSetOnlyBriefly(t *testing.T) { |
| clock := &fakeClock{t: time.Unix(1_800_000_000, 0)} |
| var set []string |
| + reads := 0 |
| gate := dnsGateAt( |
| func(string) ([]string, error) { return []string{"203.0.113.7"}, nil }, |
| - func() []string { return set }, |
| + func() []string { reads++; return set }, |
| clock.now, |
| ) |
| - if gate("www.customer.test") { |
| - t.Fatal("an empty edge set opened the gate") |
| + for i := 0; i < 10; i++ { |
| + if gate("h" + string(rune('a'+i)) + ".customer.test") { |
| + t.Fatal("an empty edge set opened the gate") |
| + } |
| + } |
| + if reads != 1 { |
| + t.Fatalf("an empty edge set was read %d times in one burst, want 1", reads) |
| } |
| set = []string{"203.0.113.7"} |
| + clock.advance(warmEdgeSetEmptyTTL) |
| if !gate("www.customer.test") { |
| - t.Fatal("the empty read was reused: the gate stayed shut after the edge set came back") |
| + t.Fatal("the empty read was reused past its short window: the gate stayed shut after the edge set came back") |
| } |
| } |
| -// A box that takes the issuer lease over orders within warmGateRecheck: the |
| -// loop's stand-down delay, not the hourly pass, decides when it looks again. |
| -func TestWarmStartsOrderingWhenTheLeaseArrives(t *testing.T) { |
| +// loopWarm runs the loop with a fast gate tick, so a test can watch what the |
| +// tick does without waiting a minute. |
| +func loopWarm(t *testing.T, w *certWarm) *atomic.Int32 { |
| + t.Helper() |
| + w.gateTick = 5 * time.Millisecond |
| + var passes atomic.Int32 |
| + w.passDone = func() { passes.Add(1) } |
| + startWarm(t, w) |
| + waitCount(t, &passes, 1, "passes") |
| + return &passes |
| +} |
| + |
| +// A box that takes the issuer lease orders within a gate tick, not at its |
| +// next hourly pass. That covers the case a dropped wake leaves behind: a box |
| +// that lost the lease mid-wait, was told about a new host while it was not |
| +// the issuer, and then got the lease back. |
| +func TestWarmOrdersWhenTheLeaseComesBack(t *testing.T) { |
| + var mu sync.Mutex |
| + hosts := []string{"old.customer.test"} |
| var issuer atomic.Bool |
| + issuer.Store(true) |
| var orders atomic.Int32 |
| w := newCertWarm( |
| - func() []string { return []string{"www.customer.test"} }, |
| - func(*tls.ClientHelloInfo) (*tls.Certificate, error) { orders.Add(1); return &tls.Certificate{}, nil }, |
| + func() []string { |
| + mu.Lock() |
| + defer mu.Unlock() |
| + return append([]string(nil), hosts...) |
| + }, |
| + func(h *tls.ClientHelloInfo) (*tls.Certificate, error) { |
| + if h.ServerName == "new.customer.test" { |
| + orders.Add(1) |
| + } |
| + return &tls.Certificate{}, nil |
| + }, |
| func(string) bool { return true }, |
| func() (context.Context, bool) { return context.Background(), issuer.Load() }, |
| nil, |
| ) |
| - if d := w.pass(); d != warmGateRecheck { |
| - t.Fatalf("stand-down delay = %s, want %s", d, warmGateRecheck) |
| + passes := loopWarm(t, w) |
| + issuer.Store(false) |
| + time.Sleep(20 * time.Millisecond) // let a tick see the loss |
| + mu.Lock() |
| + hosts = append(hosts, "new.customer.test") |
| + mu.Unlock() |
| + w.nudge() // dropped: not the issuer |
| + time.Sleep(20 * time.Millisecond) |
| + if orders.Load() != 0 { |
| + t.Fatal("a non-issuer ordered") |
| } |
| issuer.Store(true) |
| - if d := w.pass(); d != warmInterval || orders.Load() != 1 { |
| - t.Fatalf("after the lease: delay = %s, orders = %d; want %s and 1", d, orders.Load(), warmInterval) |
| + waitCount(t, &orders, 1, "orders after the lease came back") |
| + if passes.Load() < 2 { |
| + t.Fatalf("passes = %d, want a second pass", passes.Load()) |
| + } |
| +} |
| + |
| +// The loop's DNS probe is what turns a customer finishing their DNS into an |
| +// order: once the pending host points here, a pass runs and orders it. |
| +func TestWarmLoopOrdersOnceAPendingHostPoints(t *testing.T) { |
| + var pointing atomic.Bool |
| + w, clock, _, orders := dnsWarm(t, "www.customer.test", &pointing) |
| + passes := loopWarm(t, w) |
| + time.Sleep(20 * time.Millisecond) |
| + if passes.Load() != 1 { |
| + t.Fatalf("passes = %d before the host was due, want 1", passes.Load()) |
| + } |
| + pointing.Store(true) |
| + clock.advance(warmDNSRetryFast) |
| + waitCount(t, orders, 1, "orders once the host points") |
| +} |
| + |
| +// A pending host that keeps missing never runs a pass: the probe is DNS-only. |
| +func TestWarmLoopProbeOfAMissingHostRunsNoPass(t *testing.T) { |
| + var pointing atomic.Bool |
| + w, clock, checks, _ := dnsWarm(t, "www.customer.test", &pointing) |
| + passes := loopWarm(t, w) |
| + for i := 0; i < 3; i++ { |
| + clock.advance(warmDNSRetryFast) |
| + waitCount(t, checks, int32(i+2), "DNS checks") |
| + } |
| + if passes.Load() != 1 { |
| + t.Fatalf("passes = %d, want 1 — probing a missing host ran a full pass", passes.Load()) |
| } |
| } |
diff --git a/internal/provision/testdata/plan14-root-reads-certwarm-wake.tsv b/internal/provision/testdata/plan14-root-reads-certwarm-wake.tsv| index e4c3ef758..184bb6766 100644 |
| --- a/internal/provision/testdata/plan14-root-reads-certwarm-wake.tsv |
| +++ b/internal/provision/testdata/plan14-root-reads-certwarm-wake.tsv |
| @@ -4,4 +4,4 @@ |
| @legacy c49a58e4a9e22fcb2a45b4a5d7318e95481fe7743a7ea2941e56b16dd950a802 internal/edge/edge.go RunWithActivatorReady legacy accepted-legacy accepted-legacy;rev3=J4;basis=root-input;rev3=census-ruling-certwarm-wake;basis=registers the cert warm's nudge on OnReload and on the home map's new served-hosts wake (#386); nudge only sends on a buffered channel; no read added, same 5 sites data-literal@129,data-literal@148,data-literal@284,data-literal@417,file:Open@1013 4508e59aa6f0b1107a9a87a5ae02fb1b8933f2a4c13a257d12fad20a8d66e9e8 |
| internal/edge/edge.go Edge.Reload legacy accepted-legacy accepted-legacy;rev3=J1;basis=root-route;rev3=census-ruling-certwarm-wake;basis=the single reload hook call becomes runReloadHooks(), which runs every registered hook in order (#386); no read added, same 56 sites registry:Account@18079,registry:Alias@16388,registry:Alias@16475,registry:Alias@16571,registry:FleetBox@5526,registry:Health@7689,registry:Health@7807,registry:Hibernate@12450,registry:Hibernate@14360,registry:Hibernate@6049,registry:Host@12498,registry:Host@14422,registry:Host@14730,registry:Host@14757,registry:Host@15525,registry:Host@15791,registry:Host@16514,registry:Host@6098,registry:Host@8290,registry:Host@8385,registry:Host@8665,registry:Host@8691,registry:Host@8743,registry:Kind@10975,registry:Kind@10997,registry:Kind@12426,registry:Kind@14336,registry:Kind@6025,registry:Kind@8831,registry:Kind@8891,registry:Name@18069,registry:Prefix@15766,registry:Region@2252,registry:Socket@14738,registry:Socket@5633,registry:Socket@6162,registry:Status@1835,registry:Target@15778,registry:Tunnel@4574,registry:Tunnel@4630,registry:Version@10956,registry:Version@11058,whole:Fleet@18019,whole:Fleet@18048,whole:Route@130,whole:Route@14790,whole:Route@14797,whole:Route@4692,whole:Route@5590,whole:Route@6218,whole:Route@648,whole:Route@8808,whole:Route@8881,whole:Route@8941,whole:RouteAlias@16359,whole:RoutePath@15503 664bc0f32795ef722fa17a035017a2dfb79394df7b8b2bd56e0d38cb761aa3cc |
| internal/edge/edge.go RunWithActivatorReady legacy accepted-legacy accepted-legacy;rev3=J4;basis=root-input;rev3=census-ruling-certwarm-wake;basis=registers the cert warm's nudge on OnReload and on the home map's new served-hosts wake (#386); nudge only sends on a buffered channel; no read added, same 5 sites data-literal@129,data-literal@148,data-literal@284,data-literal@417,file:Open@1013 4508e59aa6f0b1107a9a87a5ae02fb1b8933f2a4c13a257d12fad20a8d66e9e8 |
| -@coverage internal/edge 0ba750f11aa6700dd0238d47d6fcc104b99775002eba3a5521ba527420c0f98c |
| +@coverage internal/edge 10b8e5872523646a90abfcdef75214564da831d38c8f1d1473a4ea5437782af8 |