region HA P1a Task 14: fix round 1 (absent-lock legacy wake modelled faithfully behind ReuseHostnames, with a known-gap test; keep-without-marker narrowed to markerless recovery after a crash before restore)
- The stale negative index entry is consulted over an absent lock when ReuseHostnames is on: the legacy acquire creates a legacy lease, as acquireClassified does. The sweep runs with it off (#199 never-reuse). TestKnownGapReusedHostnameLegacyWake asserts the observed outcome: I1 and I2 hold, the host ends stranded (I5). - A wake may crash after its acquire PUT and before the restore. keep-without-marker now trusts the release record only for a markerless view; its witness re-takes the box's own lease after such a crash over a stale record and must restore.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
6 files changed,
+117
−14
internal/sim/regionha/actions_test.go+32 −7internal/sim/regionha/invariants_test.go+1 −1internal/sim/regionha/rules_test.go+1 −1internal/sim/regionha/sim_test.go+1 −1internal/sim/regionha/witness_test.go+74 −4internal/sim/regionha/world_test.go+8 −0
diff --git a/internal/sim/regionha/actions_test.go b/internal/sim/regionha/actions_test.go| index 9359781e5..c84175d51 100644 |
| --- a/internal/sim/regionha/actions_test.go |
| +++ b/internal/sim/regionha/actions_test.go |
| @@ -123,14 +123,20 @@ type wakeOpts struct { |
| FailAfterRepl bool // the wake fails after replication has started |
| SettleFails bool // the owed marker's retry before the acquire fails |
| MarkerFails bool // the unclean release's marker PUT fails |
| + |
| + // CrashBeforeRestore: the agent dies after the acquire's PUT landed |
| + // and before the restore read or touched the local copy. The lock is |
| + // left as this box's lease, with no release marker. |
| + CrashBeforeRestore bool |
| } |
| // wake is a request's wake of h at b, which has no entry for it. pre is a |
| // round 0 the box already read (wake-begin). It reports what happened. |
| func (w *world) wake(b *box, h string, o wakeOpts, pre *pending) string { |
| - if pre == nil && b.StaleNeg[h] && w.s3[h].ETag != "" { |
| - // The box's index says legacy: the legacy acquire (takeLegacy) |
| - // classifies the lock first. |
| + if pre == nil && w.consultsStaleIndex(b, h) { |
| + // The box's index says legacy: the legacy acquire classifies the |
| + // lock first (takeLegacy over a present lock, createLease over an |
| + // absent one, which writes a legacy lease). |
| w.event("legacy-path") |
| v := w.view(h) |
| if v.HAOn() && on("legacy-takes-ha-lock") { |
| @@ -177,6 +183,11 @@ func (w *world) wake(b *box, h string, o wakeOpts, pre *pending) string { |
| w.event("same-box-retake") |
| } |
| w.logf("box%d: %s: acquired over %s", b.ID, h, viewWord(e.View)) |
| + if o.CrashBeforeRestore { |
| + w.event("crash-before-restore") |
| + w.crash(b) |
| + return "crashed before restore" |
| + } |
| if o.LateAck { |
| // The acquire's own cleanup: the lock is ours, the deadline is not. |
| w.event("late-ack") |
| @@ -228,10 +239,23 @@ func viewWord(v hafresh.View) string { |
| return "an expired lease of " + v.Lock.Owner |
| } |
| +// consultsStaleIndex reports whether b's wake of h goes through its stale |
| +// negative index entry, the legacy acquire (activator.acquireClassified, |
| +// step 3). With hostnames reused, always. With hostnames never reused (the |
| +// default, #199), a name with no lock.json has never been woken under any |
| +// lifetime, so no box can hold an entry for it: the entry is consulted only |
| +// once the host's HA lock exists. The absent-lock case is the known gap |
| +// TestKnownGapReusedHostnameLegacyWake documents. |
| +func (w *world) consultsStaleIndex(b *box, h string) bool { |
| + return b.StaleNeg[h] && (w.ReuseHostnames || w.s3[h].ETag != "") |
| +} |
| + |
| // decideKeep is hafresh.DecideKeep over the view the acquire classified. |
| func (w *world) decideKeep(b *box, h string, v hafresh.View, now hafresh.LocalNow) (bool, string) { |
| - if !on("keep-without-marker") && now.HaveRecord { |
| - // The box's own release record stands in for the lock's marker. |
| + if !on("keep-without-marker") && !v.Absent && v.Lock.Release == nil && now.HaveRecord { |
| + // Markerless reuse: with no release marker in the lock (this box's |
| + // own lease, re-taken after a crash), the box's own release record |
| + // stands in for the marker. |
| m := now.Record.Marker |
| v = hafresh.View{Lock: hafresh.Lock{Owner: hafresh.MarkerOwner(m), Release: &m}} |
| } |
| @@ -731,6 +755,7 @@ func (w *world) randomWakeOpts() wakeOpts { |
| o.LateAck = n < 5 |
| o.RestoreFails = n >= 5 && n < 10 |
| o.FailAfterRepl = n >= 10 && n < 15 |
| + o.CrashBeforeRestore = n >= 15 && n < 18 |
| } |
| o.SettleFails = w.fault(30) |
| o.MarkerFails = w.fault(15) |
| @@ -777,7 +802,7 @@ func init() { |
| action{"wake-begin", 5, func(w *world) bool { |
| b, h := w.randomBox(), w.randomHost() |
| settle := w.fault(30) |
| - if b.Agent != agentUp || !idle(b, h) || (b.StaleNeg[h] && w.s3[h].ETag != "") { |
| + if b.Agent != agentUp || !idle(b, h) || w.consultsStaleIndex(b, h) { |
| return false |
| } |
| if ob := b.Owed[h]; ob != nil && !w.retryObligation(b, ob, settle) { |
| @@ -902,7 +927,7 @@ func init() { |
| var ps []bh |
| for _, b := range w.boxes { |
| for _, h := range w.hosts { |
| - if b.Agent == agentUp && b.StaleNeg[h] && idle(b, h) && w.s3[h].ETag != "" { |
| + if b.Agent == agentUp && idle(b, h) && w.consultsStaleIndex(b, h) { |
| ps = append(ps, bh{b, h}) |
| } |
| } |
diff --git a/internal/sim/regionha/invariants_test.go b/internal/sim/regionha/invariants_test.go| index bb0a771a2..12cc94853 100644 |
| --- a/internal/sim/regionha/invariants_test.go |
| +++ b/internal/sim/regionha/invariants_test.go |
| @@ -109,7 +109,7 @@ func (w *world) checkLiveness() { |
| p := w.s3[h] |
| if p.ETag == "" { |
| if !p.empty() && !p.Planted { |
| - w.fail("I5", "%s is stranded: no lock.json over a prefix an HA lease wrote (txid %d, manifest gen %d)", h, p.MaxTXID, p.Gen) |
| + w.fail("I5", "%s is stranded: no lock.json over a prefix a lease wrote (txid %d, manifest gen %d)", h, p.MaxTXID, p.Gen) |
| } |
| continue |
| } |
diff --git a/internal/sim/regionha/rules_test.go b/internal/sim/regionha/rules_test.go| index 582def2de..d3fe3f381 100644 |
| --- a/internal/sim/regionha/rules_test.go |
| +++ b/internal/sim/regionha/rules_test.go |
| @@ -41,7 +41,7 @@ func on(rule string) bool { |
| var allRules = []string{ |
| // the acquire table and the keep decision (I1, I2; spec §9.4, §5.3) |
| "expiry-authorizes", // DecideAcquire: another box's expired HA lease is refused, never taken |
| - "keep-without-marker", // DecideKeep reads the lock's marker, never the box's own release record |
| + "keep-without-marker", // no marker in the lock (a re-taken own lease) restores; the release record alone never keeps |
| "keep-unclean", // DecideKeep: an unclean marker never keeps |
| "same-box-no-proof", // DecideAcquire: this box's own expired lease only with Proofs.SameBoxEmpty |
| "marker-before-final", // releaseHAClean: Final and Finish, then the record, then the marker; no write after it |
diff --git a/internal/sim/regionha/sim_test.go b/internal/sim/regionha/sim_test.go| index c4d9f1f5b..788821c8c 100644 |
| --- a/internal/sim/regionha/sim_test.go |
| +++ b/internal/sim/regionha/sim_test.go |
| @@ -211,7 +211,7 @@ func TestEveryActionFires(t *testing.T) { |
| for _, e := range []string{ |
| "served", "fresh", "kept", "restored", "same-box-retake", |
| "refused-held", "refused-other-expired", "refused-same-box", "refused-leftover-data", |
| - "reclassified", "late-ack", "failed-restore", "failed-after-replication", |
| + "reclassified", "late-ack", "failed-restore", "failed-after-replication", "crash-before-restore", |
| "marker-clean", "marker-unclean", "marker-owed", "obligation-discharged", |
| "release-held", "ha-lock-on-legacy-path", "hang-resumed", "manifest-committed", |
| } { |
diff --git a/internal/sim/regionha/witness_test.go b/internal/sim/regionha/witness_test.go| index 34c2c63d9..a18208fa0 100644 |
| --- a/internal/sim/regionha/witness_test.go |
| +++ b/internal/sim/regionha/witness_test.go |
| @@ -35,7 +35,7 @@ var witnesses = []witness{ |
| {"expiry-authorizes", "expiry-authorizes", "I1", |
| "plan: a hang past the TTL, then another box's wake, is refused; taken on expiry, the new box runs beside the hung one", wHangPastTTL}, |
| {"keep-without-marker", "keep-without-marker", "I2", |
| - "A releases clean, B wakes, writes and releases clean; A's own release record still matches its copy, so trusting it in place of the lock's marker (B's) serves A's old copy", wKeepWithoutMarker}, |
| + "A releases clean; B wakes, writes and releases clean; A takes B's marker and crashes before its restore, so its stale copy and its own release record are untouched; after a restart, the sweep and expiry, A re-takes its own lease, which carries no marker; trusting the record serves A's old copy", wMarkerlessRecovery}, |
| {"keep-unclean", "keep-unclean", "I2", |
| "plan: a matching but unclean marker restores; the final files publish was incomplete, so keeping serves files S3 does not hold", wUncleanMarkerRestores}, |
| {"same-box-no-proof", "same-box-no-proof", "I1", |
| @@ -293,7 +293,7 @@ func wHangPastTTL(t *testing.T, w *world) { |
| w.advance(3) // A resumes: it syncs, and renews |
| } |
| -func wKeepWithoutMarker(t *testing.T, w *world) { |
| +func wMarkerlessRecovery(t *testing.T, w *world) { |
| A, B, _ := w.abc() |
| w.serve(t, A) |
| w.write(A, false) |
| @@ -303,9 +303,22 @@ func wKeepWithoutMarker(t *testing.T, w *world) { |
| w.write(B, false) |
| w.syncNow(B, true) |
| w.sleepNow(B, sleepOpts{}) |
| + if r := w.req(A, wakeOpts{CrashBeforeRestore: true}); r != "crashed before restore" { |
| + t.Errorf("setup: %s", r) |
| + } |
| + w.restart(A, true) |
| + w.check() |
| + w.advance(ttlTicks + 1) |
| + v := w.view(sh) |
| + if v.Lock.Release != nil { |
| + t.Errorf("setup: the lock carries a marker: %s", w.lockString(sh)) |
| + } |
| restored := w.events["restored"] |
| - w.req(A, wakeOpts{}) |
| - expect(t, "keep-without-marker", w.events["restored"] == restored+1, "A, under B's marker, did not restore") |
| + r := w.req(A, wakeOpts{}) |
| + if !strings.HasPrefix(r, "served") || w.events["same-box-retake"] != 1 { |
| + t.Errorf("setup: A's recovery of its own lease: %s", r) |
| + } |
| + expect(t, "keep-without-marker", w.events["restored"] == restored+1, "A, re-taking its own markerless lease, did not restore") |
| } |
| func wUncleanMarkerRestores(t *testing.T, w *world) { |
| @@ -467,3 +480,60 @@ func wStragglerHoldsRelease(t *testing.T, w *world) { |
| w.advance(6) |
| expect(t, "marker-before-slice-empty", w.view(sh).Lock.Release != nil, "the held release never finished: %s", w.lockString(sh)) |
| } |
| + |
| +// TestKnownGapReusedHostnameLegacyWake documents a known gap, not a rule: |
| +// with hostnames reused (#199's never-reuse rule not in force), a box whose |
| +// lifetime index still holds a negative entry from the name's earlier |
| +// legacy lifetime wakes the re-created born-HA host before any HA lock |
| +// exists. Production's acquireClassified then takes lease.Acquire, which |
| +// creates a legacy lease; the box runs the host by the legacy path and its |
| +// legacy sleep deletes the lock. Every HA box then refuses the host |
| +// (leftover data). The test asserts that outcome: single writer holds (no |
| +// I1, no I2), and the host ends stranded (I5). It runs with every rule in |
| +// place; the sweep runs with ReuseHostnames off. |
| +func TestKnownGapReusedHostnameLegacyWake(t *testing.T) { |
| + saved := mutated |
| + mutated = "" |
| + defer func() { mutated = saved }() |
| + w := newWitness() |
| + w.ReuseHostnames = true |
| + A, B, C := w.abc() |
| + B.StaleNeg[sh] = true |
| + if r := w.req(B, wakeOpts{}); r != "legacy served" { |
| + t.Fatalf("B's wake by its stale index over an absent lock: %s", r) |
| + } |
| + if v := w.view(sh); v.HAOn() { |
| + t.Fatalf("B wrote an HA-format lock: %s", w.lockString(sh)) |
| + } |
| + for _, x := range []*box{A, C} { |
| + if r := w.req(x, wakeOpts{}); r != "refused: "+hafresh.ReasonLeftoverLock { |
| + t.Errorf("box%d while B holds a legacy lease: %s", x.ID, r) |
| + } |
| + } |
| + w.write(B, true) |
| + w.syncNow(B, true) |
| + w.sleepNow(B, sleepOpts{}) |
| + if !w.view(sh).Absent { |
| + t.Fatalf("B's legacy sleep left %s, not an absent lock", w.lockString(sh)) |
| + } |
| + for _, x := range []*box{A, C} { |
| + if r := w.req(x, wakeOpts{}); r != "refused: leftover data" { |
| + t.Errorf("box%d after B's legacy sleep: %s", x.ID, r) |
| + } |
| + } |
| + w.checkLiveness() |
| + if len(w.violations) != 1 || invariantOf(w.violations[0]) != "I5" { |
| + dump(t, w) |
| + t.Fatalf("want exactly the stranded host (I5), got %v", w.violations) |
| + } |
| + t.Logf("known gap, as asserted: %s", w.violations[0]) |
| + |
| + // The same sequence with hostnames never reused: the stale entry is |
| + // not consulted while no lock exists, and B takes the HA path. |
| + w = newWitness() |
| + _, B, _ = w.abc() |
| + B.StaleNeg[sh] = true |
| + if r := w.req(B, wakeOpts{}); r != "served" || !w.view(sh).HAOn() { |
| + t.Fatalf("never-reuse: B's wake: %s, lock %s", r, w.lockString(sh)) |
| + } |
| +} |
diff --git a/internal/sim/regionha/world_test.go b/internal/sim/regionha/world_test.go| index 697a50a4c..2a08e6e0f 100644 |
| --- a/internal/sim/regionha/world_test.go |
| +++ b/internal/sim/regionha/world_test.go |
| @@ -234,6 +234,14 @@ type world struct { |
| // obligation retry and orphan restart itself. |
| scripted bool |
| verbose bool |
| + |
| + // ReuseHostnames lets a hostname be re-created after a legacy lifetime, |
| + // so a box's stale negative index entry can be consulted while the new |
| + // born-HA host has no lock.json yet. Off in the sweep: it runs under the |
| + // nearest-region never-reuse rule (#199), the precondition P1a relies on |
| + // for that case. TestKnownGapReusedHostnameLegacyWake turns it on to |
| + // show what production does without it. |
| + ReuseHostnames bool |
| } |
| // action is one thing the random walk can do. run reports whether the |