carlos / platform Public

edge: cert warm review round 3 fixes (#386)

- Orders are stamped just before they are placed, after the DNS lookup,
  so a slow resolver cannot let one leave the budget window early.
- A wake refused because this box is not the issuer records that, so
  the lease returning between gate ticks reads as a transition and
  runs the pass the wake asked for.
- The gate check runs on an absolute deadline; wakes no longer restart
  its wait.
- The lease test waits for the pass counter instead of reading it (the
  CI failure on 905398dd was that race).

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

Late order stamps, refused wakes remember the lost lease, absolute gate deadline; lease test race fixed.
claude agent, operated by paul@keymail.dev 54ab8b4420fa57d7b9f8bc0ae4d9a3f47bb71d64 parent 905398d
3 files changed, +137 −33
  • internal/edge/certwarm.go +28 −9
  • internal/edge/certwarm_wake_test.go +108 −23
  • internal/provision/testdata/plan14-root-reads-certwarm-wake.tsv +1 −1

CI passed — run details

diff --git a/internal/edge/certwarm.go b/internal/edge/certwarm.go
index c8e0da74a..b1470feaa 100644
--- a/internal/edge/certwarm.go
+++ b/internal/edge/certwarm.go
@@ -228,10 +228,14 @@ func (c *certWarm) run() { c.loop(nil) }
func (c *certWarm) loop(stop <-chan struct{}) {
passDue := time.Now().Add(c.runPass())
issuer := c.isIssuer()
+ // gateDue is absolute, not a fresh wait per iteration: wakes arrive with
+ // every reload in the fleet, and a wait restarted by each of them would
+ // put the gate check off for as long as reloads keep coming.
+ gateDue := time.Now().Add(c.gateTick)
timer := time.NewTimer(time.Hour)
defer timer.Stop()
for {
- wait := min(time.Until(passDue), c.gateTick)
+ wait := min(time.Until(passDue), time.Until(gateDue))
// 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.
@@ -245,18 +249,29 @@ func (c *certWarm) loop(stop <-chan struct{}) {
return
case <-c.wake:
timer.Stop()
+ if !c.isIssuer() {
+ // Dropped, and remembered: this box is not the issuer
+ // right now, so when the lease comes back the gate check
+ // sees a transition and runs the pass this wake asked for.
+ issuer = false
+ break
+ }
full = time.Until(passDue) > warmCappedDelay && c.hasNewHost()
case <-timer.C:
- now := c.isIssuer()
- switch {
- case !time.Now().Before(passDue):
- full = true
- case now && !issuer:
+ if !time.Now().Before(gateDue) {
+ now := c.isIssuer()
+ if now && !issuer {
+ full = true
+ }
+ issuer = now
+ gateDue = time.Now().Add(c.gateTick)
+ }
+ if !time.Now().Before(passDue) {
full = true
- default:
+ }
+ if !full && issuer {
full = c.probeDNS()
}
- issuer = now
}
if full {
passDue = time.Now().Add(c.runPass())
@@ -467,7 +482,11 @@ func (c *certWarm) pass() time.Duration {
}
c.mu.Lock()
delete(c.dnsMisses, h)
- c.recentOrders = append(c.recentOrders, now)
+ // Stamped now, after the DNS lookup and just before the order,
+ // not with the pass's per-host now: a slow resolver would
+ // otherwise date the order early and let it leave the window
+ // before its time.
+ c.recentOrders = append(c.recentOrders, c.now())
c.mu.Unlock()
orders++
}
diff --git a/internal/edge/certwarm_wake_test.go b/internal/edge/certwarm_wake_test.go
index 9630c3832..239284634 100644
--- a/internal/edge/certwarm_wake_test.go
+++ b/internal/edge/certwarm_wake_test.go
@@ -200,6 +200,27 @@ func TestWarmDNSScheduleDoesNotApplyToWarmedHosts(t *testing.T) {
}
}
+// An order is dated when it is placed, after its DNS lookup, not when the
+// pass reached the host: a resolver that takes seconds must not let the
+// order leave the window early.
+func TestWarmOrderIsStampedAfterItsDNSLookup(t *testing.T) {
+ clock := &fakeClock{t: time.Unix(1_800_000_000, 0)}
+ w := newCertWarm(
+ func() []string { return []string{"slow.customer.test"} },
+ func(*tls.ClientHelloInfo) (*tls.Certificate, error) { return &tls.Certificate{}, nil },
+ func(string) bool { clock.advance(10 * time.Second); return true },
+ nil,
+ nil,
+ )
+ w.now = clock.now
+ w.pass()
+ // 61 s after the pass began, 51 s after the order went out.
+ clock.advance(51 * time.Second)
+ if got := w.orderBudget(clock.now()); got != warmOrdersPerPass-1 {
+ t.Fatalf("budget = %d, want %d: the order left the window early", got, warmOrdersPerPass-1)
+ }
+}
+
// 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) {
@@ -499,48 +520,112 @@ func loopWarm(t *testing.T, w *certWarm) *atomic.Int32 {
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) {
+// leaseWarm is a warm over one already-warmed host plus whatever the test
+// adds, with an issuer gate the test flips. lostSeen is signalled each time
+// the gate is read while the lease is away.
+func leaseWarm(t *testing.T) (w *certWarm, add func(string), issuer *atomic.Bool, lostSeen chan struct{}, orders *atomic.Int32) {
+ t.Helper()
var mu sync.Mutex
hosts := []string{"old.customer.test"}
- var issuer atomic.Bool
+ issuer = &atomic.Bool{}
issuer.Store(true)
- var orders atomic.Int32
- w := newCertWarm(
+ lostSeen = make(chan struct{}, 1)
+ orders = &atomic.Int32{}
+ w = newCertWarm(
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" {
+ if h.ServerName != "old.customer.test" {
orders.Add(1)
}
return &tls.Certificate{}, nil
},
func(string) bool { return true },
- func() (context.Context, bool) { return context.Background(), issuer.Load() },
+ func() (context.Context, bool) {
+ if !issuer.Load() {
+ select {
+ case lostSeen <- struct{}{}:
+ default:
+ }
+ return nil, false
+ }
+ return context.Background(), true
+ },
nil,
)
- passes := loopWarm(t, w)
+ add = func(h string) {
+ mu.Lock()
+ hosts = append(hosts, h)
+ mu.Unlock()
+ }
+ return w, add, issuer, lostSeen, orders
+}
+
+// A box that takes the issuer lease orders within a gate tick, not at its
+// next hourly pass. The hard case is the one a dropped wake leaves behind:
+// the lease goes away, a new host's wake arrives and is refused, and the
+// lease comes back before any gate tick saw it go. The refused wake itself
+// has to count as seeing it go.
+func TestWarmOrdersWhenTheLeaseComesBackBetweenTicks(t *testing.T) {
+ w, add, issuer, lostSeen, orders := leaseWarm(t)
+ w.gateTick = 100 * time.Millisecond
+ var passes atomic.Int32
+ w.passDone = func() { passes.Add(1) }
+ startWarm(t, w)
+ waitCount(t, &passes, 1, "passes")
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")
+ add("new.customer.test")
+ w.nudge()
+ select {
+ case <-lostSeen: // the wake read the gate while the lease was away
+ case <-time.After(2 * time.Second):
+ t.Fatal("the wake never read the gate")
}
issuer.Store(true)
- waitCount(t, &orders, 1, "orders after the lease came back")
- if passes.Load() < 2 {
- t.Fatalf("passes = %d, want a second pass", passes.Load())
- }
+ waitCount(t, orders, 1, "orders after the lease came back")
+ waitCount(t, &passes, 2, "passes")
+}
+
+// The gate check keeps its own deadline. Reloads all over the fleet wake the
+// loop far more often than once a minute; if each wake restarted the gate
+// wait, a box that had just taken the lease would never look, and a host
+// already on the DNS schedule (which a wake does not count as new) would
+// wait for the hourly pass.
+func TestWarmGateCheckIsNotPostponedByWakes(t *testing.T) {
+ w, add, issuer, _, orders := leaseWarm(t)
+ w.gateTick = 50 * time.Millisecond
+ var passes atomic.Int32
+ w.passDone = func() { passes.Add(1) }
+ pointing := atomic.Bool{}
+ w.pointsHere = func(h string) bool { return h == "old.customer.test" || pointing.Load() }
+ startWarm(t, w)
+ waitCount(t, &passes, 1, "passes")
+ // A host that misses DNS while this box is the issuer: it goes on the
+ // schedule, a minute out, so a wake does not count it as new.
+ add("pending.customer.test")
+ issuer.Store(false)
+ time.Sleep(150 * time.Millisecond) // a gate tick sees the lease go
+ w.mu.Lock()
+ w.dnsMisses["pending.customer.test"] = dnsMiss{first: time.Now(), next: time.Now().Add(time.Hour)}
+ w.mu.Unlock()
+ pointing.Store(true)
+ issuer.Store(true)
+ stop := make(chan struct{})
+ defer close(stop)
+ go func() {
+ for {
+ select {
+ case <-stop:
+ return
+ case <-time.After(10 * time.Millisecond):
+ w.nudge()
+ }
+ }
+ }()
+ waitCount(t, orders, 1, "orders while wakes kept arriving")
}
// The loop's DNS probe is what turns a customer finishing their DNS into an
diff --git a/internal/provision/testdata/plan14-root-reads-certwarm-wake.tsv b/internal/provision/testdata/plan14-root-reads-certwarm-wake.tsv
index 184bb6766..776fa0de5 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 10b8e5872523646a90abfcdef75214564da831d38c8f1d1473a4ea5437782af8
+@coverage internal/edge 96f40de3b6b8a6951f01855d26d73e581044a2c6b8e006bdfd144d57dcf2ac1f