region HA P1a final fix: F5 the tests' crash model stops the dead agent
haSvc.abandon is the agent's death: it stops and joins every background job the agent owns (the service lifetime, each entry's files publisher, each replica's renewer and replication through lease's Abandon) and writes nothing, leaving the fixture's unit and the lock as a crash would. restart abandons the old agent first; peer is a second agent beside a live one. Every haSvc and replacement is torn down with t.Cleanup, and the HA fixture restores lease's process globals (self, the obligation store) after its test (lease.ProcessStateForTest). Tests: an abandoned agent sends no request while its surviving unit writes on, and its lock is unchanged; the fixture's lease globals are restored.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+142
−5
internal/activator/harelease_test.go+60 −4internal/activator/hasamebox_test.go+43 −1internal/activator/hawake_test.go+20 −0internal/lease/owed.go+19 −0
diff --git a/internal/activator/harelease_test.go b/internal/activator/harelease_test.go| index ee52e89c9..63e352680 100644 |
| --- a/internal/activator/harelease_test.go |
| +++ b/internal/activator/harelease_test.go |
| @@ -193,6 +193,7 @@ func haService(t *testing.T, host string) *haSvc { |
| } |
| }) |
| s.a = New(Config{Bucket: b.Cfg.Bucket, Idle: time.Hour}, b, b.Registry) |
| + t.Cleanup(s.abandon) |
| return s |
| } |
| @@ -280,10 +281,23 @@ func (s *haSvc) stoppedFor(e *actEntry) bool { |
| } |
| } |
| -// restart is the agent restarted on the same box: a new backend and |
| -// Service over the same data, registry, index and fixture, and lease's |
| -// obligation store loaded again from its directory (memory dropped). |
| +// restart is the agent restarted on the same box: the old agent dies |
| +// first (abandon: its background work stopped and joined, its unit and its |
| +// lock left as they are), then a new backend and Service over the same |
| +// data, registry, index and fixture, and lease's obligation store loaded |
| +// again from its directory (memory dropped). The replacement is torn down |
| +// when the test ends. |
| func (s *haSvc) restart(t *testing.T) *haSvc { |
| + t.Helper() |
| + s.abandon() |
| + return s.peer(t) |
| +} |
| + |
| +// peer is a second agent over the same data, registry, index and fixture |
| +// while s's own agent runs on (as another box, for an acquire with another |
| +// self), with lease's obligation store loaded again from its directory. |
| +// It is torn down when the test ends. |
| +func (s *haSvc) peer(t *testing.T) *haSvc { |
| t.Helper() |
| if err := lease.RestartObligationsForTest(s.owedDir); err != nil { |
| t.Fatal(err) |
| @@ -298,9 +312,51 @@ func (s *haSvc) restart(t *testing.T) *haSvc { |
| s2 := &haSvc{t: t, b: b, rt: s.rt, fx: s.fx, ctl: s.ctl, unit: s.unit, owedDir: s.owedDir} |
| s2.wire(b) |
| s2.a = New(Config{Bucket: b.Cfg.Bucket, Idle: time.Hour}, b, b.Registry) |
| + t.Cleanup(s2.abandon) |
| return s2 |
| } |
| +// abandon is the agent's death, as these tests model it (final review |
| +// F5): every background job the agent owns is stopped and joined (the |
| +// service lifetime's work, each entry's files publisher, and each |
| +// replica's renewer and replication, by lease's Abandon: no final sync, no |
| +// marker, no delete), and nothing else changes. The fixture's unit, its |
| +// serving socket and the lock survive, as they outlive a crash. |
| +// Idempotent. |
| +func (s *haSvc) abandon() { |
| + s.a.mu.Lock() |
| + var entries []*actEntry |
| + for _, e := range s.a.entries { |
| + entries = append(entries, e) |
| + } |
| + var reps []Rep |
| + for _, u := range s.a.unprovenWakes { |
| + if u.rep != nil { |
| + reps = append(reps, u.rep) |
| + } |
| + } |
| + s.a.mu.Unlock() |
| + if s.a.lifeStop != nil { |
| + s.a.lifeStop() |
| + } |
| + for _, e := range entries { |
| + s.a.stopFiles(e) |
| + s.a.mu.Lock() |
| + r := e.rep |
| + s.a.mu.Unlock() |
| + if r != nil { |
| + reps = append(reps, r) |
| + } |
| + } |
| + for _, r := range reps { |
| + if ab, ok := r.(interface{ Abandon(context.Context) }); ok { |
| + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| + ab.Abandon(ctx) |
| + cancel() |
| + } |
| + } |
| +} |
| + |
| func (s *haSvc) lockKey() string { return lockKeyOf(s.b, s.rt) } |
| // lockWriteLog is every write that changed the host's lock, in order. |
| @@ -844,7 +900,7 @@ func TestFailedWakeKeepsLeaseWhileSliceHoldsAReplacement(t *testing.T) { |
| if u == nil || u.rep == nil { |
| t.Fatalf("no retained wake with its replica: %+v", u) |
| } |
| - other := s.restart(t) |
| + other := s.peer(t) |
| lease.SetSelf(hafresh.Self{Ref: "b", IID: "i-b"}) |
| _, err := other.b.Acquire(context.Background(), other.rt) |
| lease.SetSelf(hafresh.Self{Ref: "a", IID: "i-a"}) |
diff --git a/internal/activator/hasamebox_test.go b/internal/activator/hasamebox_test.go| index e5b4d9607..4be900a5e 100644 |
| --- a/internal/activator/hasamebox_test.go |
| +++ b/internal/activator/hasamebox_test.go |
| @@ -2,8 +2,11 @@ package activator |
| import ( |
| "context" |
| + "database/sql" |
| "encoding/json" |
| "errors" |
| + "os" |
| + "path/filepath" |
| "strings" |
| "sync" |
| "testing" |
| @@ -204,7 +207,10 @@ func TestRestartedAgentRetakesNestedHostAfterExpiry(t *testing.T) { |
| if _, err := s.wakeForTest(ctx); err != nil { |
| t.Fatalf("wake: %v", err) |
| } |
| - // The crash: the agent never runs again, and its lock stops renewing. |
| + // The crash: the agent never runs again (abandon: its renewer, its |
| + // replication and its files publisher stopped and joined, its unit |
| + // left running), and then its lock's TTL passes. |
| + s.abandon() |
| expireLock(t, s) |
| // The sweep failed: the dead agent's unit may still write. |
| @@ -232,3 +238,39 @@ func TestRestartedAgentRetakesNestedHostAfterExpiry(t *testing.T) { |
| t.Fatal("the restarted agent did not take the host's lock") |
| } |
| } |
| + |
| +// F5: the crash model. Once the agent is abandoned, nothing it owns runs: |
| +// the surviving unit goes on writing its database and its files, and no |
| +// request of the dead agent's (a replica upload, a files publish, a lease |
| +// renewal) reaches the store. The lock is left as the agent held it. |
| +func TestAbandonedAgentWritesNothing(t *testing.T) { |
| + s, _ := haServiceEntry(t, "shop.example.com") |
| + time.Sleep(1200 * time.Millisecond) // the publisher's first pass has run |
| + held := s.lockView(t) |
| + s.abandon() |
| + n := len(s.fx.Requests()) |
| + dir := filesDirOf(s.b, s.rt) |
| + if err := os.MkdirAll(dir, 0o700); err != nil { |
| + t.Fatal(err) |
| + } |
| + db, err := sql.Open("sqlite", s.b.pathsFor(s.rt).dbPath) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer db.Close() |
| + for i := 0; i < 5; i++ { // the surviving unit writes on |
| + if _, err := db.Exec("insert into t values (?)", i); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.WriteFile(filepath.Join(dir, "instance.key"), []byte(time.Now().String()), 0o600); err != nil { |
| + t.Fatal(err) |
| + } |
| + time.Sleep(500 * time.Millisecond) |
| + } |
| + if got := s.fx.Requests()[n:]; len(got) != 0 { |
| + t.Fatalf("the dead agent kept working: %v", got) |
| + } |
| + if v := s.lockView(t); v.ETag != held.ETag || v.Lock.Release != nil { |
| + t.Fatalf("the dead agent's lock changed: %+v", v.Lock) |
| + } |
| +} |
diff --git a/internal/activator/hawake_test.go b/internal/activator/hawake_test.go| index 929d65c02..fd117bd22 100644 |
| --- a/internal/activator/hawake_test.go |
| +++ b/internal/activator/hawake_test.go |
| @@ -65,6 +65,10 @@ func haRecord(host string, ha bool) provision.Record { |
| func haBackendEmptyIndex(t *testing.T, host string) (*ExecBackend, registry.Route) { |
| t.Helper() |
| fx := locktest.NewLockFixture(t) |
| + // The lease process globals this fixture sets (the box, the |
| + // obligation store) go back as they were after the test (final review |
| + // F5). |
| + t.Cleanup(lease.ProcessStateForTest()) |
| if err := lease.SetObligationDir(t.TempDir()); err != nil { |
| t.Fatal(err) |
| } |
| @@ -875,3 +879,19 @@ func TestLeaseRefusedAnswers503RetryAfter10(t *testing.T) { |
| t.Fatalf("%d, Retry-After %q", w.Code, w.Header().Get("Retry-After")) |
| } |
| } |
| + |
| +// F5: an HA fixture's lease process globals (the box it names, its |
| +// obligation store) go back as they were once its test ends. |
| +func TestHAFixtureRestoresLeaseGlobals(t *testing.T) { |
| + defer lease.ProcessStateForTest()() |
| + lease.SetSelf(hafresh.Self{Ref: "outer", IID: "i-outer"}) |
| + t.Run("fixture", func(t *testing.T) { |
| + haBackend(t, "shop.example.com") |
| + if got := lease.CurrentSelf(); got.Ref != "a" { |
| + t.Fatalf("the fixture names %+v", got) |
| + } |
| + }) |
| + if got := lease.CurrentSelf(); got.Ref != "outer" || got.IID != "i-outer" { |
| + t.Fatalf("after the fixture's test, the box is %+v", got) |
| + } |
| +} |
diff --git a/internal/lease/owed.go b/internal/lease/owed.go| index d95d741aa..d428b27b1 100644 |
| --- a/internal/lease/owed.go |
| +++ b/internal/lease/owed.go |
| @@ -15,6 +15,7 @@ import ( |
| "encoding/json" |
| "errors" |
| "fmt" |
| + "maps" |
| "os" |
| "path/filepath" |
| "runtime" |
| @@ -154,6 +155,24 @@ func RestartObligationsForTest(dir string) error { |
| return SetObligationDir(dir) |
| } |
| +// ProcessStateForTest snapshots this process's lease state, the box |
| +// SetSelf named and the obligation store (its directory and what is |
| +// pending), and returns the function that puts it back: for tests outside |
| +// this package that set them, so one test's box and store never leak into |
| +// the next. Production code never calls it. |
| +func ProcessStateForTest() (restore func()) { |
| + s := self.Load() |
| + owed.mu.Lock() |
| + dir, pending := owed.dir, maps.Clone(owed.pending) |
| + owed.mu.Unlock() |
| + return func() { |
| + self.Store(s) |
| + owed.mu.Lock() |
| + owed.dir, owed.pending = dir, pending |
| + owed.mu.Unlock() |
| + } |
| +} |
| + |
| func obligationStoreSet() bool { |
| owed.mu.Lock() |
| defer owed.mu.Unlock() |