activator, lease: fast same-box row refuses adoption-held hosts
Task 10 review round 1 (Astra P2, Fable P2 and P3s). The same-box fast row trusted acquiredHere, which the departure-adoption acquire never marks. sameBoxState gains a live-only adoptionHeld set, marked once the adoption acquire succeeds and cleared once its release succeeds (a failed release keeps it: the lock may still be ours until it expires). sameBoxEmptyLive refuses while the host is in it; expired-lock recovery does not read it. Witnesses: under fast, an unexpired generations lock of this box is held when the host is acquiredHere or adoption-held, and taken when a previous process wrote it. The lease witnesses' held check now counts every mutation request in its own phase (A's held uploads aside) and compares the bucket's versions and lock.json's current version, so an overwrite or delete fails it. Prose in hasamebox.go, lease/ha.go and haadopt.go names the fast exception.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
7 files changed,
+206
−14
internal/activator/haadopt.go+16 −3internal/activator/haadopt_test.go+46 −0internal/activator/hasamebox.go+42 −1internal/activator/hatakeover_test.go+47 −0internal/lease/gentakeover_test.go+51 −8internal/lease/ha.go+3 −1internal/lease/unclean_marker_test.go+1 −1
CI failed — run details
diff --git a/internal/activator/haadopt.go b/internal/activator/haadopt.go| index 035b426b1..50ff596b0 100644 |
| --- a/internal/activator/haadopt.go |
| +++ b/internal/activator/haadopt.go |
| @@ -304,7 +304,10 @@ func (a *Service) AdoptHost(ctx context.Context, rt registry.Route, req Adoption |
| } |
| } |
| // Persist the expected generation before the conditional write. Recovery of |
| - // this process's held lock still waits for expiry and SameBoxEmpty. |
| + // this process's held lock waits for expiry and SameBoxEmpty; a wake under |
| + // the switch's `fast` reads the adoption-held mark below instead and holds |
| + // the unexpired lock (sameBoxEmptyLive), since this acquire never marks |
| + // acquiredHere. |
| expected := view.Lock.Generation + 1 |
| if err = phase("acquiring", expected, nil); err != nil { |
| return Deferred, err |
| @@ -332,12 +335,22 @@ func (a *Service) AdoptHost(ctx context.Context, rt registry.Route, req Adoption |
| if err != nil { |
| return Deferred, err |
| } |
| + // Adoption-held until a release succeeds: a failed one may leave the |
| + // lock this process's until it expires. |
| + b.sameBox.markAdoptionHeld(rt.Host) |
| + release := func(ctx context.Context) error { |
| + err := held.Release(ctx) |
| + if err == nil { |
| + b.sameBox.clearAdoptionHeld(rt.Host) |
| + } |
| + return err |
| + } |
| released := false |
| defer func() { |
| if !released { |
| releaseCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| defer cancel() |
| - _ = held.Release(releaseCtx) |
| + _ = release(releaseCtx) |
| } |
| }() |
| if err = phase("acquired", expected, nil); err != nil { |
| @@ -366,7 +379,7 @@ func (a *Service) AdoptHost(ctx context.Context, rt registry.Route, req Adoption |
| return Deferred, err |
| } |
| released = true |
| - if err = held.Release(ctx); err != nil { |
| + if err = release(ctx); err != nil { |
| return Deferred, err |
| } |
| view, err = read() |
diff --git a/internal/activator/haadopt_test.go b/internal/activator/haadopt_test.go| index 6dc77357b..24b1dee19 100644 |
| --- a/internal/activator/haadopt_test.go |
| +++ b/internal/activator/haadopt_test.go |
| @@ -9,6 +9,7 @@ import ( |
| "log" |
| "path/filepath" |
| "strings" |
| + "sync" |
| "testing" |
| "time" |
| @@ -137,6 +138,51 @@ func TestHAAdoptAuthority(t *testing.T) { |
| }) |
| } |
| } |
| + |
| +// The adoption acquire marks the host adoption-held for the same-box fast |
| +// row (sameBoxEmptyLive) and clears it once its release succeeds; a |
| +// release that fails leaves it marked, since the lock may still be this |
| +// process's until it expires. |
| +func TestHAAdoptMarksTheHostAdoptionHeld(t *testing.T) { |
| + for _, c := range []struct { |
| + name string |
| + fail bool |
| + }{{"released", false}, {"release failed", true}} { |
| + t.Run(c.name, func(t *testing.T) { |
| + s, req := haAdoptionFixture(t) |
| + ctx := context.Background() |
| + if c.fail { |
| + s.fx.FailNextMatching("PUT", 503, func(k string, b []byte) bool { return strings.Contains(string(b), "carlos_release") }) |
| + } |
| + // The release's marker write on lock.json comes while the |
| + // adoption still holds the lock. |
| + var mu sync.Mutex |
| + during := false |
| + s.fx.BeforeWrite(func(method, key string) { |
| + if strings.HasSuffix(key, "/lock.json") && s.b.sameBox.isAdoptionHeld(s.rt.Host) { |
| + mu.Lock() |
| + during = true |
| + mu.Unlock() |
| + } |
| + }) |
| + got, err := s.a.AdoptHost(ctx, s.rt, req) |
| + if c.fail != (got == Deferred) { |
| + t.Fatalf("adopt %v %v", got, err) |
| + } |
| + mu.Lock() |
| + defer mu.Unlock() |
| + // A failed release's marker PUT is refused before the write |
| + // hook runs; the mark left set below shows it there. |
| + if !during && !c.fail { |
| + t.Fatal("the host was not adoption-held while the adoption held the lock") |
| + } |
| + if after := s.b.sameBox.isAdoptionHeld(s.rt.Host); after != c.fail { |
| + t.Fatalf("adoption-held after the adoption: %v, want %v", after, c.fail) |
| + } |
| + }) |
| + } |
| +} |
| + |
| func TestHAAdoptReplay(t *testing.T) { |
| s, req := haAdoptionFixture(t) |
| ctx := context.Background() |
diff --git a/internal/activator/hasamebox.go b/internal/activator/hasamebox.go| index c07dd767c..9dba3c30f 100644 |
| --- a/internal/activator/hasamebox.go |
| +++ b/internal/activator/hasamebox.go |
| @@ -24,6 +24,12 @@ import ( |
| // crash), and only on proof that nothing of the host still runs here: |
| // hafresh.Proofs.SameBoxEmpty, which is sameBoxEmpty. |
| // |
| +// The exception is a generations lock under the switch's `fast` (storage |
| +// generations slice 4): there the same proof (sameBoxEmptyLive, which also |
| +// refuses a host a departure adoption of this process holds) takes the |
| +// lock before it expires, and the other box's takeover needs only the |
| +// termination proof. Without the proof such a lock is held until expiry. |
| +// |
| // The proof is root's existing stop-and-prove for nested units; P1a adds |
| // none. sameBoxEmpty answers true only when all of these hold: |
| // |
| @@ -80,8 +86,16 @@ func (b *ExecBackend) sameBoxEmpty(ctx context.Context, rt registry.Route) func( |
| // unexpired generations lock of this box (hafresh.Proofs.LiveSameBoxEmpty): |
| // the same proof, but a refusal raises no alarm, since the lock is then |
| // held until its expiry as without the flag. |
| +// |
| +// It also refuses a host a departure adoption of this process holds |
| +// (sameBoxState.adoptionHeld): that acquire never marks acquiredHere, and |
| +// before the lock expires nothing else tells its lock from a previous |
| +// process's. |
| func (b *ExecBackend) sameBoxEmptyLive(rt registry.Route) func() (bool, error) { |
| return func() (bool, error) { |
| + if b.sameBox.isAdoptionHeld(rt.Host) { |
| + return false, errors.New("a departure adoption of this agent process holds the host") |
| + } |
| if err := b.sameBoxProven(rt); err != nil { |
| return false, err |
| } |
| @@ -141,11 +155,38 @@ const sameBoxAlarmEvery = time.Minute |
| type sameBoxState struct { |
| mu sync.Mutex |
| acquired map[string]bool |
| - alarmed map[string]time.Time |
| + // adoptionHeld is the hosts whose lock a departure adoption of this |
| + // process holds (haadopt.go): marked once its acquire succeeds, |
| + // cleared once its release succeeds. Only the `fast` same-box row |
| + // reads it (sameBoxEmptyLive): the adoption acquire never marks |
| + // acquiredHere, and an expired lock's recovery is left as it was. |
| + adoptionHeld map[string]bool |
| + alarmed map[string]time.Time |
| // flatEvents reads a host's flat cgroup. Nil: under Cgroups' root. |
| flatEvents func(host string) (exists, populated bool, err error) |
| } |
| +func (s *sameBoxState) markAdoptionHeld(host string) { |
| + s.mu.Lock() |
| + defer s.mu.Unlock() |
| + if s.adoptionHeld == nil { |
| + s.adoptionHeld = map[string]bool{} |
| + } |
| + s.adoptionHeld[host] = true |
| +} |
| + |
| +func (s *sameBoxState) clearAdoptionHeld(host string) { |
| + s.mu.Lock() |
| + defer s.mu.Unlock() |
| + delete(s.adoptionHeld, host) |
| +} |
| + |
| +func (s *sameBoxState) isAdoptionHeld(host string) bool { |
| + s.mu.Lock() |
| + defer s.mu.Unlock() |
| + return s.adoptionHeld[host] |
| +} |
| + |
| func (s *sameBoxState) markAcquired(host string) { |
| s.mu.Lock() |
| defer s.mu.Unlock() |
diff --git a/internal/activator/hatakeover_test.go b/internal/activator/hatakeover_test.go| index 68b847456..5e1fc8b48 100644 |
| --- a/internal/activator/hatakeover_test.go |
| +++ b/internal/activator/hatakeover_test.go |
| @@ -398,3 +398,50 @@ func TestFastLiveHolderRaisesNoAlarm(t *testing.T) { |
| } |
| }) |
| } |
| + |
| +// Under `fast`, an unexpired generations lock of this box is taken on the |
| +// same-box proof only when a previous agent process wrote it. A lock this |
| +// process holds, by a wake (acquiredHere) or by a departure adoption |
| +// (whose acquire never marks acquiredHere), is held until its expiry, with |
| +// no alarm. |
| +func TestFastSameBoxLiveLockOnlyFromAPreviousProcess(t *testing.T) { |
| + ctx := context.Background() |
| + mine := hafresh.OwnerFor(hafresh.Self{Ref: "a", IID: "i-a"}, 7, true) |
| + for _, c := range []struct { |
| + name string |
| + mark func(b *ExecBackend, host string) |
| + taken bool |
| + }{ |
| + {"written by a previous process", func(*ExecBackend, string) {}, true}, |
| + {"acquired by this process", func(b *ExecBackend, host string) { markAcquiredHere(b, host) }, false}, |
| + {"held by this process's adoption", func(b *ExecBackend, host string) { b.sameBox.markAdoptionHeld(host) }, false}, |
| + } { |
| + t.Run(c.name, func(t *testing.T) { |
| + t.Setenv("CARLOS_HA_GENERATIONS", "fast") |
| + logs := captureLog(t) |
| + b, rt := haBackend(t, "shop.example.com") |
| + sameBoxReady(b) |
| + c.mark(b, rt.Host) |
| + body := genLeaseBody(t, mine, time.Now().Add(10*time.Minute)) |
| + putLockFor(t, b, rt, body) |
| + l, err := b.Acquire(ctx, rt) |
| + if c.taken { |
| + if err != nil { |
| + t.Fatalf("a previous process's live lock was not taken on the proof: %v", err) |
| + } |
| + releaseHALease(t, l) |
| + if !strings.Contains(logs.String(), "same-box recovery before the lease expires") { |
| + t.Fatalf("the live same-box take's log:\n%s", logs) |
| + } |
| + return |
| + } |
| + var held *errLeaseHeld |
| + if !errors.As(err, &held) || held.holder != mine { |
| + t.Fatalf("this process's own live lock: %v, want held by %s", err, mine) |
| + } |
| + if strings.Contains(logs.String(), "activator: ALERT") || lockWrites(t, b, rt) != 0 || !bytes.Equal(lockBodyOf(b, rt), body) { |
| + t.Fatalf("the held wake alarmed or wrote:\n%s", logs) |
| + } |
| + }) |
| + } |
| +} |
diff --git a/internal/lease/gentakeover_test.go b/internal/lease/gentakeover_test.go| index dcc60014f..0f5f57206 100644 |
| --- a/internal/lease/gentakeover_test.go |
| +++ b/internal/lease/gentakeover_test.go |
| @@ -137,17 +137,60 @@ func acquireB(fx *genFixture, ttl time.Duration, p hafresh.Proofs) (*lease.Held, |
| } |
| // requireHeldNothingWritten asserts that box b's acquire with p is held |
| -// until want and wrote nothing. |
| -func requireHeldNothingWritten(t *testing.T, fx *genFixture, ttl time.Duration, p hafresh.Proofs, want time.Time, what string) { |
| +// until want and mutated nothing. It runs in its own phase and counts |
| +// every mutation request (PUT, COPY, DELETE, POST ?delete) the server got |
| +// in it, leaving out A's uploads held at the partition (lw: A's monitor |
| +// may still send one); and the bucket's versions under the host, the |
| +// lock's current version included, are exactly the ones before. |
| +func requireHeldNothingWritten(t *testing.T, fx *genFixture, lw *lateWrites, ttl time.Duration, p hafresh.Proofs, want time.Time, what string) { |
| t.Helper() |
| - before := len(fx.keys("", false)) |
| + phase := "held: " + what |
| + fx.setPhase(phase) |
| + defer fx.setPhase("b") |
| + versions := func() []string { |
| + vs, err := fx.m.ListVersions(ctx, fx.bucket, fx.loc+"/") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var out []string |
| + for _, v := range vs { |
| + out = append(out, fmt.Sprintf("%s@%s latest=%t marker=%t", v.Key, v.VersionID, v.IsLatest, v.DeleteMarker)) |
| + } |
| + slices.Sort(out) |
| + return out |
| + } |
| + lockVersion := func() string { |
| + for _, v := range versions() { |
| + if strings.HasPrefix(v, fx.loc+"/lock.json@") && strings.Contains(v, "latest=true") { |
| + return v |
| + } |
| + } |
| + t.Fatal("no current lock.json version") |
| + return "" |
| + } |
| + before, lockBefore := versions(), lockVersion() |
| _, err := acquireB(fx, ttl, p) |
| var he *lease.HeldError |
| if !errors.As(err, &he) || !he.ExpiresAt.Equal(want) { |
| t.Fatalf("%s: %v, want held until %v", what, err, want) |
| } |
| - if after := fx.keys("", false); len(after) != before { |
| - t.Fatalf("%s: a held answer wrote %v", what, after[before:]) |
| + held := lw.heldKeys() |
| + fx.mu.Lock() |
| + var mutations []string |
| + for _, r := range fx.sent { |
| + if r.phase == phase && !slices.Contains(held, r.key) { |
| + mutations = append(mutations, r.method+" "+r.key) |
| + } |
| + } |
| + fx.mu.Unlock() |
| + if len(mutations) != 0 { |
| + t.Fatalf("%s: a held answer sent %v", what, mutations) |
| + } |
| + if after := lockVersion(); after != lockBefore { |
| + t.Fatalf("%s: lock.json's current version moved: %s, was %s", what, after, lockBefore) |
| + } |
| + if after := versions(); !slices.Equal(after, before) { |
| + t.Fatalf("%s: the bucket's versions changed:\nbefore %v\nafter %v", what, before, after) |
| } |
| } |
| @@ -331,12 +374,12 @@ func TestTerminatedHolderTakenOnProofAlone(t *testing.T) { |
| terminated := func(iid string) (bool, error) { asked = append(asked, iid); return iid == "i-a", nil } |
| running := func(string) (bool, error) { return false, nil } |
| eligible := func() bool { return true } |
| - requireHeldNothingWritten(t, fx, ttl, hafresh.Proofs{LocalEligible: eligible, OwnerTerminated: terminated}, v.Lock.ExpiresAt, "B without fast") |
| + requireHeldNothingWritten(t, fx, lw, ttl, hafresh.Proofs{LocalEligible: eligible, OwnerTerminated: terminated}, v.Lock.ExpiresAt, "B without fast") |
| if len(asked) != 0 { |
| t.Fatalf("without fast the termination proof was asked about an unexpired lock: %v", asked) |
| } |
| - requireHeldNothingWritten(t, fx, ttl, hafresh.Proofs{LocalEligible: eligible, OwnerTerminated: running, GenerationsFast: true}, v.Lock.ExpiresAt, "B with fast, A not terminated") |
| - requireHeldNothingWritten(t, fx, ttl, hafresh.Proofs{LocalEligible: eligible, GenerationsFast: true}, v.Lock.ExpiresAt, "B with fast, no proof wired") |
| + requireHeldNothingWritten(t, fx, lw, ttl, hafresh.Proofs{LocalEligible: eligible, OwnerTerminated: running, GenerationsFast: true}, v.Lock.ExpiresAt, "B with fast, A not terminated") |
| + requireHeldNothingWritten(t, fx, lw, ttl, hafresh.Proofs{LocalEligible: eligible, GenerationsFast: true}, v.Lock.ExpiresAt, "B with fast, no proof wired") |
| bHeld, err := acquireB(fx, ttl, hafresh.Proofs{LocalEligible: eligible, OwnerTerminated: terminated, GenerationsFast: true}) |
| if err != nil { |
diff --git a/internal/lease/ha.go b/internal/lease/ha.go| index 2588b5eb7..480062cdd 100644 |
| --- a/internal/lease/ha.go |
| +++ b/internal/lease/ha.go |
| @@ -45,7 +45,9 @@ var ErrNoSelf = errors.New("lease: this box's identity (reference, instance id, |
| // 3. A marker, this box's expired lock with Proofs.SameBoxEmpty, or |
| // another box's expired lock with Proofs.OwnerTerminated: a take, |
| // If-Match the ETag classified, generation+1, the HA owner. The log |
| -// names the rule that won. |
| +// names the rule that won. With Proofs.GenerationsFast and a |
| +// generations lock, an unsettled marker is taken at once and an |
| +// unexpired owner on the same live proofs (hafresh.DecideAcquire). |
| // 4. Held: *HeldError. Refused: *RefusedError. Nothing written. |
| // 5. A lost condition re-reads and reclassifies, at most three rounds. |
| // |
diff --git a/internal/lease/unclean_marker_test.go b/internal/lease/unclean_marker_test.go| index 6f01fe94a..4813306e5 100644 |
| --- a/internal/lease/unclean_marker_test.go |
| +++ b/internal/lease/unclean_marker_test.go |
| @@ -410,7 +410,7 @@ func TestUnsettledGenMarkerTakenAtOnce(t *testing.T) { |
| boxB() |
| fx.setPhase("b-held") |
| eligible := func() bool { return true } |
| - requireHeldNothingWritten(t, fx, ttl, hafresh.Proofs{LocalEligible: eligible}, m.At.Add(ttl), "B without fast") |
| + requireHeldNothingWritten(t, fx, lw, ttl, hafresh.Proofs{LocalEligible: eligible}, m.At.Add(ttl), "B without fast") |
| bHeld, err := acquireB(fx, ttl, hafresh.Proofs{LocalEligible: eligible, GenerationsFast: true}) |
| if err != nil { |