carlos / platform Public

activator: end a lost host's recovery when its entry goes (#1062)

A lost host's haLoss record kept l.recovery pointing at its recovery
entry after that entry left a.entries (a failed wake, or finishStop
after a failed post-check or a retirement). DrainQuietNow counts
l.recovery != nil as Recovering, and only a new recovery replaced it,
which needs a request within 60 s for a route that still exists, is
enabled and eligible. A trashed or removed host gets none, so the
drain's quiesce step waited on Recovering:1 forever.

endHARecoveryLocked clears l.recovery at both places an entry leaves.
The loss stays latched: local wakes are still refused, and the next
request starts a new recovery.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 4fbef4c1ff79011f8be96c7054d5356502f27ae3 parent 9eec023
3 files changed, +158 −0
  • internal/activator/activator.go +2 −0
  • internal/activator/hadrainquiet_test.go +142 −0
  • internal/activator/haleaseloss.go +14 −0

Intent

diff --git a/internal/activator/activator.go b/internal/activator/activator.go
index 4d9f131a2..2c9bf854a 100644
--- a/internal/activator/activator.go
+++ b/internal/activator/activator.go
@@ -2324,6 +2324,7 @@ func (a *Service) wake(e *actEntry) {
a.rememberStopLocked(e.host, outcome)
a.cancelHeldLegsLocked(e, 0)
delete(a.entries, e.host) // next request retries from scratch
+ a.endHARecoveryLocked(e)
} else {
e.state = actAwake
e.sidecar, e.sidecarWanted = w.sidecar, w.sidecarWanted
@@ -5482,6 +5483,7 @@ func (a *Service) finishStop(e *actEntry, outcome *stopObservation) {
republish := e.worker || a.publishesLocked(e)
a.cancelHeldLegsLocked(e, 0)
delete(a.entries, e.host)
+ a.endHARecoveryLocked(e)
// A resident's stop, as it is forgotten (Plan 3 Task 8): a crash or a
// lost lease backs off; any other stop is restarted on the next tick.
a.noteResidentStopLocked(e)
diff --git a/internal/activator/hadrainquiet_test.go b/internal/activator/hadrainquiet_test.go
new file mode 100644
index 000000000..8e4b53068
--- /dev/null
+++ b/internal/activator/hadrainquiet_test.go
@@ -0,0 +1,142 @@
+package activator
+
+import (
+ "sync/atomic"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/edgesfile"
+ "github.com/carlosframework/platform/internal/lease"
+)
+
+// assertRecoveryEnded is #1062's check: once a lost host's recovery entry
+// has left a.entries, nothing is recovering, the drain's Recovering count
+// is zero, and the loss itself is still latched (local wakes refused).
+func assertRecoveryEnded(t *testing.T, s *haSvc) {
+ t.Helper()
+ s.a.mu.Lock()
+ l := s.a.haLoss.hosts[s.rt.Host]
+ var stale bool
+ if l != nil {
+ stale = l.recovery != nil || l.running
+ }
+ s.a.mu.Unlock()
+ if l == nil {
+ t.Fatal("the loss record went with its recovery entry: the loss is discharged")
+ }
+ if stale {
+ t.Fatal("the loss record still names a recovery that has ended")
+ }
+ if q := s.a.DrainQuietNow(); q.Recovering != 0 {
+ t.Fatalf("Recovering = %d after the recovery ended (%+v)", q.Recovering, q)
+ }
+ assertLossLatched(t, s)
+}
+
+// The recovery wakes, then the route is removed (the edge's RetireExcept,
+// as on a route deletion or a trashed app) before the recovery's success
+// check: the recovery entry stops and goes. Field case #1062.
+func TestDrainQuietRecoveryRetiredRouteClears(t *testing.T) {
+ s, e, r := lostTestEntry(t)
+ finishLostTestEntry(t, s, e, r)
+ var recovered *actEntry
+ s.a.wakeEnded = func(host string) {
+ s.a.mu.Lock()
+ recovered = s.a.entries[host]
+ s.a.mu.Unlock()
+ s.a.RetireExcept(map[string]bool{})
+ }
+ s.a.retryHALostHost(s.rt)
+ s.a.haLoss.joined.Wait()
+ if recovered == nil {
+ t.Fatal("recovery did not wake")
+ }
+ s.a.mu.Lock()
+ stopped := recovered.stopped
+ s.a.mu.Unlock()
+ if stopped == nil {
+ t.Fatal("retirement did not stop the recovery entry")
+ }
+ select {
+ case <-stopped:
+ case <-time.After(10 * time.Second):
+ t.Fatal("retired recovery did not stop")
+ }
+ assertRecoveryEnded(t, s)
+ if q := s.a.DrainQuietNow(); !q.Zero() {
+ t.Fatalf("drain never quiesces: %+v", q)
+ }
+ // The loss is still owed a recovery: the next request starts one, and
+ // its healthy fresh acquisition discharges the loss.
+ s.a.wakeEnded = nil
+ clk := &fakeClock{t: time.Now().Add(haLossRetryInterval)}
+ s.a.now = clk.now
+ recoverLostTestHost(t, s)
+ s.a.mu.Lock()
+ left := s.a.haLoss.hosts[s.rt.Host]
+ s.a.mu.Unlock()
+ if left != nil {
+ t.Fatal("a recovered host kept its loss record")
+ }
+}
+
+// The recovery wakes, then fails its post-check (the pool withdrawn): the
+// worker stops it for the lease, and the failed recovery entry goes.
+func TestDrainQuietRecoveryFailedPostCheckClears(t *testing.T) {
+ s, e, r := lostTestEntry(t)
+ finishLostTestEntry(t, s, e, r)
+ var withdrawn atomic.Bool
+ original := s.b.ServedPools
+ s.b.ServedPools = func() edgesfile.PoolEvidence {
+ if withdrawn.Load() {
+ return edgesfile.PoolEvidence(`["another-pool"]`)
+ }
+ return original()
+ }
+ var recovered *actEntry
+ s.a.wakeEnded = func(host string) {
+ s.a.mu.Lock()
+ recovered = s.a.entries[host]
+ s.a.mu.Unlock()
+ withdrawn.Store(true)
+ }
+ s.a.retryHALostHost(s.rt)
+ s.a.haLoss.joined.Wait()
+ if recovered == nil {
+ t.Fatal("recovery did not wake")
+ }
+ s.a.mu.Lock()
+ stopped := recovered.stopped
+ s.a.mu.Unlock()
+ if stopped == nil {
+ t.Fatal("failed post-check left the recovery entry awake")
+ }
+ select {
+ case <-stopped:
+ case <-time.After(10 * time.Second):
+ t.Fatal("failed recovery did not stop")
+ }
+ assertRecoveryEnded(t, s)
+}
+
+// The recovery's wake itself fails (its owed marker cannot settle): the
+// failed wake's entry goes.
+func TestDrainQuietRecoveryFailedWakeClears(t *testing.T) {
+ s, e, r := lostTestEntry(t)
+ s.failMarkerPuts(2) // the release, then the recovery's settlement
+ finishLostTestEntry(t, s, e, r)
+ clk := &fakeClock{t: time.Now()}
+ s.a.now = clk.now
+ s.a.retryHALostHost(s.rt)
+ s.a.haLoss.joined.Wait()
+ s.a.mu.Lock()
+ current := s.a.entries[s.rt.Host]
+ s.a.mu.Unlock()
+ if current != nil {
+ t.Fatalf("failed recovery wake left an entry in state %v", current.state)
+ }
+ if len(lease.PendingObligations()) != 1 {
+ t.Fatal("failed settlement discarded the obligation")
+ }
+ assertRecoveryEnded(t, s)
+}
diff --git a/internal/activator/haleaseloss.go b/internal/activator/haleaseloss.go
index d3b6c2426..e1410f58b 100644
--- a/internal/activator/haleaseloss.go
+++ b/internal/activator/haleaseloss.go
@@ -85,6 +85,20 @@ func (a *Service) latchHALossLocked(e *actEntry) {
a.pokeMoveLocked(e.host)
}
+// endHARecoveryLocked is e leaving a.entries (a failed wake's cleanup, or
+// finishStop): if e was its loss's recovery entry, that recovery is over
+// (#1062). The loss stays latched, so local wakes are still refused, and
+// the next request starts a new recovery; only a recovery's healthy, newly
+// acquired replica discharges a loss. Left set, l.recovery named a gone
+// entry, so the drain counted it as recovering forever: only a new
+// recovery replaced it, and none starts without demand for a route that
+// is still there and eligible. Under a.mu.
+func (a *Service) endHARecoveryLocked(e *actEntry) {
+ if l := e.haRecovery; l != nil && l.recovery == e {
+ l.recovery = nil
+ }
+}
+
func haUnexpectedLoss(e *actEntry, err error) bool {
return err != nil && (!e.haReleasing || !errors.Is(err, lease.ErrSpent))
}