fix: renew replica coordination outside the fast tick
Start one owned renewal worker with the agent and signal it from the existing fast tick. Coalesce signals without waiting for console requests. Close cancels and joins the worker before releasing held authorities; no periodic bucket read or new renewal schedule is introduced. A hanging-console witness checks the five-minute renewal, uninterrupted serial convergence, retained authority before its deadline and shutdown cancellation. Existing transient retry and term-fencing tests pass.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+122
−5
internal/activator/coordinator.go+47 −1internal/host/agent.go+4 −3internal/host/replicacoordinator_test.go+71 −1
diff --git a/internal/activator/coordinator.go b/internal/activator/coordinator.go| index a12ae027c..e24fe8d38 100644 |
| --- a/internal/activator/coordinator.go |
| +++ b/internal/activator/coordinator.go |
| @@ -26,6 +26,9 @@ type CoordinationClient struct { |
| exchange CoordinatorExchange |
| release ReleaseExchange |
| now func() time.Time |
| + wake chan struct{} |
| + cancel context.CancelFunc |
| + done chan struct{} |
| closed bool |
| held map[replicas.Identity]*coordinatorAuthority |
| retry map[replicas.Identity]time.Time |
| @@ -70,7 +73,43 @@ func (c *CoordinationClient) HeldForHost(host string) replicas.Authority { |
| return result |
| } |
| -// Tick is called by the agent's existing tick. It renews only held terms. |
| +// Start owns the renewal worker until Close or ctx cancellation. |
| +func (c *CoordinationClient) Start(ctx context.Context) { |
| + c.mu.Lock() |
| + defer c.mu.Unlock() |
| + if c.closed || c.done != nil { |
| + return |
| + } |
| + ctx, c.cancel = context.WithCancel(ctx) |
| + c.wake = make(chan struct{}, 1) |
| + c.done = make(chan struct{}) |
| + go func() { |
| + defer close(c.done) |
| + for { |
| + select { |
| + case <-ctx.Done(): |
| + return |
| + case <-c.wake: |
| + c.Tick(ctx) |
| + } |
| + } |
| + }() |
| +} |
| + |
| +// SignalTick coalesces agent ticks without waiting for a console request. |
| +func (c *CoordinationClient) SignalTick() { |
| + c.mu.Lock() |
| + defer c.mu.Unlock() |
| + if c.closed || c.wake == nil { |
| + return |
| + } |
| + select { |
| + case c.wake <- struct{}{}: |
| + default: |
| + } |
| +} |
| + |
| +// Tick renews only held terms. Production calls it on the renewal worker. |
| func (c *CoordinationClient) Tick(ctx context.Context) { |
| c.opMu.Lock() |
| defer c.opMu.Unlock() |
| @@ -212,6 +251,10 @@ func (c *CoordinationClient) Close(ctx context.Context) { |
| return |
| } |
| c.closed = true |
| + if c.cancel != nil { |
| + c.cancel() |
| + } |
| + done := c.done |
| pending := make([]*coordinatorAuthority, 0, len(c.held)) |
| for id, a := range c.held { |
| a.loseLocked() |
| @@ -219,6 +262,9 @@ func (c *CoordinationClient) Close(ctx context.Context) { |
| delete(c.held, id) |
| } |
| c.mu.Unlock() |
| + if done != nil { |
| + <-done |
| + } |
| c.opMu.Lock() |
| defer c.opMu.Unlock() |
| for _, a := range pending { |
diff --git a/internal/host/agent.go b/internal/host/agent.go| index 1c90e117f..48d2536f4 100644 |
| --- a/internal/host/agent.go |
| +++ b/internal/host/agent.go |
| @@ -600,6 +600,7 @@ func runAgent(args []string, buildClaims ClaimFactory) { |
| } |
| } |
| if coordinator != nil { |
| + coordinator.Start(context.Background()) |
| svc.Coordinator = coordinator |
| eb.Coordination = coordinator |
| } |
| @@ -2444,7 +2445,7 @@ const ( |
| // fastDeps is one fastConvergeLoop's state and seams — a struct so the tick |
| // is testable without goroutines, env vars, or sleeps. |
| type fastDeps struct { |
| - coordinator interface{ Tick(context.Context) } |
| + coordinator interface{ SignalTick() } |
| reg *registry.Registry |
| store release.Store |
| pub ed25519.PublicKey |
| @@ -2929,7 +2930,7 @@ func logSlow(what string, start time.Time, detail string) { |
| // backoff state. |
| func fastTick(ctx context.Context, d *fastDeps, now time.Time) { |
| if d.coordinator != nil { |
| - d.coordinator.Tick(ctx) |
| + d.coordinator.SignalTick() |
| } |
| // The index is observed BEFORE any pass this tick runs, so a pass covers |
| // at least what was observed and indexActed can take it afterwards. |
| @@ -3243,7 +3244,7 @@ func fastConvergeLoop(reg *registry.Registry, svc *activator.Service, sd *schedu |
| // remaining fields are filled in right below. |
| d := &fastDeps{reg: reg, store: store, pub: pub, optDir: optDir, serialNotify: serialNotify, fullDue: &backstopFullDue} |
| if svc != nil { |
| - d.coordinator, _ = svc.Coordinator.(interface{ Tick(context.Context) }) |
| + d.coordinator, _ = svc.Coordinator.(interface{ SignalTick() }) |
| } |
| restart := d.observeRestart(makeRestartRoute(reg, systemctlTryRestart, agentCheckedUnitRestart(systemdUnits{}, optDir, os.Getenv, nil), adoptBounceFn(svc))) |
| // Built once, used by both pass shapes below — see fastPassOpts for the |
diff --git a/internal/host/replicacoordinator_test.go b/internal/host/replicacoordinator_test.go| index cf42cea5b..066ad0118 100644 |
| --- a/internal/host/replicacoordinator_test.go |
| +++ b/internal/host/replicacoordinator_test.go |
| @@ -166,7 +166,7 @@ func TestCoordinatorTrustedStoreAdapter(t *testing.T) { |
| type coordinatorTickProbe struct{ calls int } |
| -func (p *coordinatorTickProbe) Tick(context.Context) { p.calls++ } |
| +func (p *coordinatorTickProbe) SignalTick() { p.calls++ } |
| func TestCoordinatorAgentTick(t *testing.T) { |
| f := newFastFixture(t) |
| @@ -470,3 +470,73 @@ func TestCoordinatorFleetTransientRenewal(t *testing.T) { |
| } |
| }) |
| } |
| + |
| +func TestCoordinatorHangingConsoleDoesNotDelayFastTick(t *testing.T) { |
| + f := newFastFixture(t) |
| + synctest.Test(t, func(t *testing.T) { |
| + ctx := context.Background() |
| + renewing := make(chan struct{}) |
| + cancelled := make(chan struct{}) |
| + client := activator.NewCoordinatorClient("box", "boot", func(ctx context.Context, _ replicas.Identity, c replicas.CoordinatorChange) (replicas.CoordinatorReply, error) { |
| + if c.Op == "renew" { |
| + select { |
| + case <-renewing: |
| + default: |
| + close(renewing) |
| + } |
| + <-ctx.Done() |
| + select { |
| + case <-cancelled: |
| + default: |
| + close(cancelled) |
| + } |
| + return replicas.CoordinatorReply{}, ctx.Err() |
| + } |
| + return replicas.CoordinatorReply{Lease: replicas.Coordinator{Term: "term", Box: "box", BootID: "boot"}, RemainingMS: replicas.CoordinatorTTL.Milliseconds()}, nil |
| + }, nil) |
| + client.Start(ctx) |
| + defer client.Close(ctx) |
| + a, err := client.Acquire(ctx, replicas.Identity{Account: "bab", App: "shop", Host: "shop.example.com", Incarnation: "life"}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + passes := 0 |
| + d := f.deps(func() { passes++ }) |
| + d.coordinator = client |
| + fastTick(ctx, d, time.Now()) |
| + synctest.Wait() |
| + select { |
| + case <-renewing: |
| + t.Fatal("renewed before five minutes") |
| + default: |
| + } |
| + time.Sleep(replicas.CoordinatorRenewAfter) |
| + fastTick(ctx, d, time.Now()) |
| + synctest.Wait() |
| + select { |
| + case <-renewing: |
| + default: |
| + t.Fatal("five-minute renewal was not signalled") |
| + } |
| + if err := release.BumpSerial(ctx, f.store, time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + start := time.Now() |
| + fastTick(ctx, d, start.Add(2*time.Second)) |
| + if time.Since(start) != 0 || passes != 1 { |
| + t.Fatalf("fast tick waited: elapsed=%s passes=%d", time.Since(start), passes) |
| + } |
| + if a.Err() != nil { |
| + t.Fatal("transient outage lost authority early") |
| + } |
| + client.Close(ctx) |
| + select { |
| + case <-cancelled: |
| + default: |
| + t.Fatal("shutdown did not cancel renewal") |
| + } |
| + if a.Err() == nil { |
| + t.Fatal("shutdown retained authority") |
| + } |
| + }) |
| +} |