region-ha p1b-1: late uploads stay within a running agent's lease; termination cohorts
Task 5 review r1. P2: only a running agent with time left on its lease has an upload in flight at its death, and the upload lands from now up to the lease's last step, never moved forward past its deadline. A hung agent past expiry sends nothing (TestLateUploadOnlyWithinLease). P3: the sweep splits by seed, odd seeds terminate and even seeds keep P1a's walk; TestFinalAllTerminated covers a pool with no live box: no restart, no I5.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+111
−13
internal/sim/regionha/actions_test.go+20 −12internal/sim/regionha/sim_test.go+84 −0internal/sim/regionha/world_test.go+7 −1
diff --git a/internal/sim/regionha/actions_test.go b/internal/sim/regionha/actions_test.go| index 81b2ab04a..97df872d7 100644 |
| --- a/internal/sim/regionha/actions_test.go |
| +++ b/internal/sim/regionha/actions_test.go |
| @@ -795,14 +795,16 @@ type lateWrite struct { |
| // the commit of an in-flight files snapshot. The replica upload is of the |
| // position not yet uploaded; with none, of the tenant's last transaction, |
| // committed just before the death (not once the release has stopped the |
| -// unit). A scripted world draws nothing; its witness calls lateUpload. |
| +// unit). Only a running agent sends, and only while its lease has time |
| +// left: a hung agent, or one whose lease has expired, has nothing in |
| +// flight. A scripted world draws nothing; its witness calls lateUpload. |
| func (w *world) inFlight(b *box) { |
| - if w.scripted { |
| + if w.scripted || b.Agent != agentUp { |
| return |
| } |
| for _, h := range w.hosts { |
| e, l := b.Entries[h], b.Local[h] |
| - if e == nil { |
| + if e == nil || stepOf(e.Expires) <= w.step { |
| continue |
| } |
| if l != nil && (l.TXID > e.Uploaded || e.Releasing == nil) && w.pct(50) { |
| @@ -817,20 +819,25 @@ func (w *world) inFlight(b *box) { |
| } |
| } |
| -// landing is a seeded step from 0 up to TTL - 1 steps after the lease's |
| -// last renewal. |
| +// landing is a seeded step from now up to the last step of the lease, at |
| +// most TTL - 1 steps after its last renewal: an upload sent now completes |
| +// within the lease, never before it was sent and never after expiry. |
| func (w *world) landing(e *entry) int { |
| - return stepOf(e.Expires) - ttlTicks + w.rng.Intn(ttlTicks) |
| + return w.step + w.rng.Intn(stepOf(e.Expires)-w.step) |
| } |
| // lateUpload is an upload b's agent process sent for h before it died, a |
| // replica upload of its local position (replica) or the commit of its |
| -// in-flight files snapshot, landing at step at (at once if that has |
| -// passed) through replicaWrite or manifestWrite with the dead pid. It |
| +// in-flight files snapshot, landing at step at (now or later) through |
| +// replicaWrite or manifestWrite with the dead pid. It |
| // models P1a's and the legacy path's assumption: an upload already sent |
| // settles within the lease. The writer oracle (checkHolder) fails I1 if it |
| // lands after another process took the lock. |
| func (w *world) lateUpload(b *box, h string, replica bool, at int) { |
| + if at < w.step { |
| + w.fail("MODEL", "box%d: %s: an upload sent at step %d cannot complete at step %d", b.ID, h, w.step, at) |
| + return |
| + } |
| lw := &lateWrite{Box: b, PID: b.PID, Host: h, At: at} |
| what := "manifest commit" |
| if replica { |
| @@ -1045,12 +1052,13 @@ func init() { |
| w.crash(b) |
| return true |
| }}, |
| - // The instance is terminated, at the rate of a crash. At most one a |
| - // world: a terminated box never comes back, so more would leave |
| - // most worlds with one box for most of the walk. |
| + // The instance is terminated, at the rate of a crash, in the odd |
| + // seeds' worlds only (w.terminations). At most one a world: a |
| + // terminated box never comes back, so more would leave most worlds |
| + // with one box for most of the walk. |
| action{"terminate", 2, func(w *world) bool { |
| b := w.randomBox() |
| - if w.calm() || len(w.terminated) > 0 { |
| + if !w.terminations || w.calm() || len(w.terminated) > 0 { |
| return false |
| } |
| w.terminate(b) |
diff --git a/internal/sim/regionha/sim_test.go b/internal/sim/regionha/sim_test.go| index 524412621..b101d39d7 100644 |
| --- a/internal/sim/regionha/sim_test.go |
| +++ b/internal/sim/regionha/sim_test.go |
| @@ -2,6 +2,7 @@ package regionha |
| import ( |
| "fmt" |
| + "math/rand" |
| "os" |
| "os/exec" |
| "reflect" |
| @@ -222,3 +223,86 @@ func TestEveryActionFires(t *testing.T) { |
| } |
| t.Logf("completed: %v; events: %v", done, events) |
| } |
| + |
| +// TestLateUploadOnlyWithinLease: only a running agent with time left on |
| +// its lease has an upload in flight when it dies, and that upload lands |
| +// within the lease. An agent that hung past its lease's expiry sends |
| +// nothing: its death creates no late upload (Task 5 review r1, P2). |
| +func TestLateUploadOnlyWithinLease(t *testing.T) { |
| + saved := mutated |
| + mutated = "" |
| + defer func() { mutated = saved }() |
| + sent := 0 |
| + for seed := int64(1); seed <= 40; seed++ { |
| + for _, hung := range []bool{true, false} { |
| + w := newWorldN(seed, rand.New(rand.NewSource(seed)), 2, []string{sh}) |
| + A := w.boxes[0] |
| + if r := w.wake(A, sh, wakeOpts{}, nil); r != "served" { |
| + t.Fatalf("seed %d: A's wake: %s", seed, r) |
| + } |
| + w.tenantWrite(A, sh, true) |
| + if hung { |
| + w.hang(A, ttlTicks+5) |
| + w.advance(ttlTicks + 3) // the lease expired at step 15; A still hangs |
| + } else { |
| + w.advance(3) |
| + } |
| + expires := stepOf(A.Entries[sh].Expires) |
| + if seed%2 == 0 { |
| + w.crash(A) |
| + } else { |
| + w.terminate(A) |
| + } |
| + if hung { |
| + if n := w.events["late-upload"]; n != 0 || w.s3[sh].MaxTXID != 0 { |
| + t.Errorf("seed %d: the death after an expired hang sent %d uploads (replica head %d)", seed, n, w.s3[sh].MaxTXID) |
| + } |
| + } else { |
| + sent += w.events["late-upload"] |
| + for _, lw := range w.late { |
| + if lw.At < w.step || lw.At >= expires { |
| + t.Errorf("seed %d: an upload sent at step %d lands at step %d, outside the lease ending at step %d", seed, w.step, lw.At, expires) |
| + } |
| + } |
| + } |
| + w.advance(ttlTicks) |
| + if len(w.violations) > 0 { |
| + t.Errorf("seed %d (hung %v): %v", seed, hung, w.violations) |
| + } |
| + } |
| + } |
| + if sent == 0 { |
| + t.Fatal("no running agent's death had an upload in flight over 40 seeds; the check proves nothing") |
| + } |
| +} |
| + |
| +// TestFinalAllTerminated: with every box of the host's pool terminated, |
| +// the finalization restarts none of them and I5 does not apply to the |
| +// host stranded behind the dead box's lease. |
| +func TestFinalAllTerminated(t *testing.T) { |
| + saved := mutated |
| + mutated = "" |
| + defer func() { mutated = saved }() |
| + w := newWitness() |
| + A, _, _ := w.abc() |
| + w.serve(t, A) |
| + w.write(A, false) |
| + w.syncNow(A, false) |
| + for _, b := range w.boxes { |
| + w.terminate(b) |
| + } |
| + lock := w.lockString(sh) |
| + w.final() |
| + for _, b := range w.boxes { |
| + if b.Agent != agentDown { |
| + t.Errorf("box%d is %s after the finalization; a terminated box never restarts", b.ID, b.Agent) |
| + } |
| + } |
| + if w.events["restart"] != 0 || w.events["takeover"] != 0 || w.lockString(sh) != lock { |
| + t.Errorf("restarts %d, takeovers %d, lock %s (was %s)", w.events["restart"], w.events["takeover"], w.lockString(sh), lock) |
| + } |
| + if len(w.violations) > 0 { |
| + dump(t, w) |
| + t.Fatalf("with no live box of the pool, I5 does not apply: %v", w.violations) |
| + } |
| +} |
diff --git a/internal/sim/regionha/world_test.go b/internal/sim/regionha/world_test.go| index e7423591e..7dc8e7ca6 100644 |
| --- a/internal/sim/regionha/world_test.go |
| +++ b/internal/sim/regionha/world_test.go |
| @@ -242,6 +242,11 @@ type world struct { |
| // faultsOff is set by the finalization: no fault from then on. |
| faultsOff bool |
| + // terminations: the terminate action is enabled. The sweep splits by |
| + // seed: odd seeds terminate, even seeds keep P1a's walk, every box |
| + // alive throughout. |
| + terminations bool |
| + |
| clock, nextETag, nextPID, nextInode, nextVer int64 |
| // scripted is a witness's world: the timers run (expiry, hang ends, |
| @@ -276,6 +281,7 @@ func newWorld(seed int64, verbose bool) *world { |
| n := 2 + rng.Intn(4) |
| w := newWorldN(seed, rng, n, []string{"blog", "docs", "shop"}) |
| w.verbose = verbose |
| + w.terminations = seed%2 == 1 |
| // A stale negative index entry: the box indexed an older, legacy |
| // incarnation of the host (r3.1). |
| for _, b := range w.boxes { |
| @@ -285,7 +291,7 @@ func newWorld(seed int64, verbose bool) *world { |
| } |
| } |
| } |
| - w.logf("world seed %d: %d boxes, hosts %v, stale negative entries %s", seed, n, w.hosts, w.staleString()) |
| + w.logf("world seed %d: %d boxes, hosts %v, stale negative entries %s, terminations %v", seed, n, w.hosts, w.staleString(), w.terminations) |
| return w |
| } |