carlos / platform Public

T1 review fixes: cancellation coverage, barrier tests, comment

Astra's review of cd1d8679. Tests for an already-ended ctx and for a ctx
cancelled while deletes are in flight: every key attempted once with the
caller's ctx, and no return while a delete is held. The sleep-based
overlap check is replaced by a blocked-delete barrier, and dropWorkers
gets a table test. The comment now says the drop returns once every
delete attempt has returned, not that the staging is gone.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev d5f0fd2439b66cc858871f690526110ebef1d392 parent cd1d867
2 files changed, +193 −41
  • internal/activator/backend_exec.go +5 −4
  • internal/activator/dropstaged_test.go +188 −37
diff --git a/internal/activator/backend_exec.go b/internal/activator/backend_exec.go
index ccb0ad92f..de121e97f 100644
--- a/internal/activator/backend_exec.go
+++ b/internal/activator/backend_exec.go
@@ -3539,10 +3539,11 @@ func (b *ExecBackend) promoteStagedReplica(ctx context.Context, host string) (ma
// objects one DeleteObject at a time was 5 to 12 s of the move's pause.
// What the serial loop did still holds. The same keys, each once, by key.
// No order: the loop ranged over a map. A failed delete is dropped and
-// cancels nothing, so every other key is still tried. And the call returns
-// only once every delete has, so the wake still spawns nothing until the
-// staging is gone (a staging copy left past a spawn could win a later
-// promotion over the writes taken since).
+// cancels nothing, so every other key is still tried, each with the
+// caller's ctx (an ended ctx is handed to every delete, as the loop did).
+// And the call returns only once every delete attempt has returned, so the
+// wake still spawns nothing while a delete is in flight. Best-effort as
+// before: a key whose delete failed stays staged.
func (b *ExecBackend) dropStagedReplica(ctx context.Context, staged map[string]lease.MirrorObject) {
keys := make(chan string)
var wg sync.WaitGroup
diff --git a/internal/activator/dropstaged_test.go b/internal/activator/dropstaged_test.go
index 72eb7b30f..ea1bf7be4 100644
--- a/internal/activator/dropstaged_test.go
+++ b/internal/activator/dropstaged_test.go
@@ -12,29 +12,45 @@ import (
)
// countingDeleteStore wraps fakeMirrorStore to watch dropStagedReplica's
-// deletes: how many run at once, which keys were asked for and how often,
-// and which keys fail. Each delete holds for a moment so that a pool really
-// overlaps them.
+// deletes: how many run at once, which keys were asked for, how often and
+// with which ctx, how many have returned, and which keys fail.
+//
+// The barrier: with release set, every delete announces itself on started
+// and then blocks until release is closed. A test can so hold a full batch
+// in flight and see that the drop has not returned, with no sleep.
+// respectCtx makes a delete whose ctx has ended refuse (and delete nothing),
+// the way an SDK call does.
type countingDeleteStore struct {
*fakeMirrorStore
- hold time.Duration
- fail map[string]bool
+ fail map[string]bool
+ release chan struct{}
+ started chan string
+ respectCtx bool
cmu sync.Mutex
inFlight int
maxSeen int
+ finished int
calls map[string]int
+ ctxs map[string][]context.Context
}
func newCountingDeleteStore() *countingDeleteStore {
return &countingDeleteStore{
fakeMirrorStore: newFakeMirrorStore(),
- hold: 2 * time.Millisecond,
fail: map[string]bool{},
calls: map[string]int{},
+ ctxs: map[string][]context.Context{},
}
}
+// withBarrier arms the barrier for a drop of n keys.
+func (s *countingDeleteStore) withBarrier(n int) *countingDeleteStore {
+ s.release = make(chan struct{})
+ s.started = make(chan string, n)
+ return s
+}
+
func (s *countingDeleteStore) Delete(ctx context.Context, bucket, key string) error {
s.cmu.Lock()
s.inFlight++
@@ -42,13 +58,21 @@ func (s *countingDeleteStore) Delete(ctx context.Context, bucket, key string) er
s.maxSeen = s.inFlight
}
s.calls[key]++
+ s.ctxs[key] = append(s.ctxs[key], ctx)
s.cmu.Unlock()
defer func() {
s.cmu.Lock()
s.inFlight--
+ s.finished++
s.cmu.Unlock()
}()
- time.Sleep(s.hold)
+ if s.release != nil {
+ s.started <- key
+ <-s.release
+ }
+ if s.respectCtx && ctx.Err() != nil {
+ return ctx.Err()
+ }
if s.fail[key] {
return errors.New("injected delete failure")
}
@@ -75,39 +99,98 @@ func stageKeys(b *ExecBackend, s *countingDeleteStore, host string, n int) map[s
return staged
}
+// dropInBackground runs the drop and closes the returned channel when it
+// returns.
+func dropInBackground(b *ExecBackend, ctx context.Context, staged map[string]lease.MirrorObject) chan struct{} {
+ done := make(chan struct{})
+ go func() {
+ defer close(done)
+ b.dropStagedReplica(ctx, staged)
+ }()
+ return done
+}
+
+// awaitStarted waits for n deletes to be held at the barrier.
+func awaitStarted(t *testing.T, s *countingDeleteStore, n int) {
+ t.Helper()
+ for i := 0; i < n; i++ {
+ select {
+ case <-s.started:
+ case <-time.After(10 * time.Second):
+ t.Fatalf("only %d of %d deletes reached the barrier", i, n)
+ }
+ }
+}
+
+// assertNotReturned fails if the drop returned while deletes are held.
+func assertNotReturned(t *testing.T, done chan struct{}) {
+ t.Helper()
+ select {
+ case <-done:
+ t.Fatal("the drop returned while deletes were still in flight")
+ default:
+ }
+}
+
+func awaitDone(t *testing.T, done chan struct{}) {
+ t.Helper()
+ select {
+ case <-done:
+ case <-time.After(10 * time.Second):
+ t.Fatal("the drop did not return once the deletes were released")
+ }
+}
+
+// assertEachOnceWith checks every staged key was attempted exactly once,
+// with ctx, and that no attempt was still running when the drop returned.
+func assertEachOnceWith(t *testing.T, s *countingDeleteStore, staged map[string]lease.MirrorObject, ctx context.Context) {
+ t.Helper()
+ s.cmu.Lock()
+ defer s.cmu.Unlock()
+ if s.inFlight != 0 || s.finished != len(staged) {
+ t.Fatalf("at return: %d in flight, %d of %d attempts finished", s.inFlight, s.finished, len(staged))
+ }
+ if len(s.calls) != len(staged) {
+ t.Fatalf("attempted %d distinct keys, want %d", len(s.calls), len(staged))
+ }
+ for k := range staged {
+ if s.calls[k] != 1 {
+ t.Fatalf("key %s attempted %d times, want 1", k, s.calls[k])
+ }
+ if got := s.ctxs[k]; len(got) != 1 || got[0] != ctx {
+ t.Fatalf("key %s was not deleted with the caller's ctx", k)
+ }
+ }
+}
+
// T1 (docs/superpowers/notes/offline-move-pause-trim.md, T1a): the drop
-// deletes exactly the staged keys, each once, several at a time but never
-// more than the promotion's own bound, and nothing is still in flight when
-// it returns (the wake goes on to spawn only after it).
+// deletes exactly the staged keys, each once, a full batch at a time, and
+// does not return while any delete is still in flight (the wake spawns
+// only after it).
func TestDropStagedReplicaDeletesEveryStagedKeyInParallel(t *testing.T) {
b := testExecBackend(t)
- s := newCountingDeleteStore()
+ const n = 300
+ s := newCountingDeleteStore().withBarrier(n)
b.Mirror = s
host := "moved.example.com"
- staged := stageKeys(b, s, host, 300)
+ staged := stageKeys(b, s, host, n)
own := b.Cfg.hibernatePrefix(host) + "/generations/0000/00000000.ltx"
s.put(b.Cfg.Bucket, own, "own")
+ ctx := context.Background()
- b.dropStagedReplica(context.Background(), staged)
+ done := dropInBackground(b, ctx, staged)
+ awaitStarted(t, s, lease.DefaultMirrorWorkers) // a full batch held at once
+ assertNotReturned(t, done)
+ close(s.release)
+ awaitDone(t, done)
+ assertEachOnceWith(t, s, staged, ctx)
s.cmu.Lock()
defer s.cmu.Unlock()
- if s.inFlight != 0 {
- t.Fatalf("%d delete(s) still in flight after the drop returned", s.inFlight)
- }
- if s.maxSeen < 2 {
- t.Fatalf("deletes ran one at a time (max in flight %d)", s.maxSeen)
- }
if s.maxSeen > lease.DefaultMirrorWorkers {
t.Fatalf("max in flight %d exceeds the bound %d", s.maxSeen, lease.DefaultMirrorWorkers)
}
- if len(s.calls) != len(staged) {
- t.Fatalf("deleted %d distinct keys, want %d", len(s.calls), len(staged))
- }
for k := range staged {
- if s.calls[k] != 1 {
- t.Fatalf("key %s deleted %d times, want 1", k, s.calls[k])
- }
if _, ok := s.buckets[b.Cfg.Bucket][k]; ok {
t.Fatalf("staged key %s survived the drop", k)
}
@@ -117,23 +200,49 @@ func TestDropStagedReplicaDeletesEveryStagedKeyInParallel(t *testing.T) {
}
}
+// The bound itself: the mirror round's rule, never more workers than keys.
+func TestDropWorkers(t *testing.T) {
+ plain := newCountingDeleteStore()
+ for _, c := range []struct {
+ m lease.MirrorStore
+ n int
+ want int
+ }{
+ {plain, 300, lease.DefaultMirrorWorkers},
+ {plain, 3, 3},
+ {plain, 0, 0},
+ {&workersStore{plain, 1}, 300, 1},
+ {&workersStore{plain, 3}, 300, 3},
+ {&workersStore{plain, 0}, 300, 1},
+ {&workersStore{plain, -2}, 300, 1},
+ } {
+ if got := dropWorkers(c.m, c.n); got != c.want {
+ t.Errorf("dropWorkers(%T, %d) = %d, want %d", c.m, c.n, got, c.want)
+ }
+ }
+}
+
// A store that sets its own concurrency gets it, as a mirror round does:
// one is strictly serial, as every drop was before T1.
func TestDropStagedReplicaHonoursTheStoresConcurrency(t *testing.T) {
for _, workers := range []int{1, 3} {
b := testExecBackend(t)
- s := &workersStore{countingDeleteStore: newCountingDeleteStore(), workers: workers}
+ const n = 40
+ s := &workersStore{countingDeleteStore: newCountingDeleteStore().withBarrier(n), workers: workers}
b.Mirror = s
- staged := stageKeys(b, s.countingDeleteStore, "moved.example.com", 40)
+ staged := stageKeys(b, s.countingDeleteStore, "moved.example.com", n)
+ ctx := context.Background()
- b.dropStagedReplica(context.Background(), staged)
+ done := dropInBackground(b, ctx, staged)
+ awaitStarted(t, s.countingDeleteStore, workers)
+ assertNotReturned(t, done)
+ close(s.release)
+ awaitDone(t, done)
+ assertEachOnceWith(t, s.countingDeleteStore, staged, ctx)
if s.maxSeen > workers {
t.Fatalf("workers=%d: max in flight %d", workers, s.maxSeen)
}
- if len(s.calls) != len(staged) {
- t.Fatalf("workers=%d: deleted %d keys, want %d", workers, len(s.calls), len(staged))
- }
}
}
@@ -151,14 +260,13 @@ func TestDropStagedReplicaFailureIsBestEffort(t *testing.T) {
break
}
s.fail[bad] = true
+ ctx := context.Background()
- b.dropStagedReplica(context.Background(), staged)
+ b.dropStagedReplica(ctx, staged)
+ assertEachOnceWith(t, s, staged, ctx)
s.cmu.Lock()
defer s.cmu.Unlock()
- if s.calls[bad] != 1 {
- t.Fatalf("failing key tried %d times, want 1", s.calls[bad])
- }
for k := range staged {
_, left := s.buckets[b.Cfg.Bucket][k]
if k == bad && !left {
@@ -167,8 +275,51 @@ func TestDropStagedReplicaFailureIsBestEffort(t *testing.T) {
if k != bad && left {
t.Fatalf("key %s was not deleted after another key's delete failed", k)
}
- if s.calls[k] != 1 {
- t.Fatalf("key %s tried %d times, want 1", k, s.calls[k])
+ }
+}
+
+// An already-ended ctx is handed to every delete, as the serial loop did:
+// each key is attempted once with it, and the drop still returns.
+func TestDropStagedReplicaWithAnEndedContext(t *testing.T) {
+ b := testExecBackend(t)
+ s := newCountingDeleteStore()
+ s.respectCtx = true
+ b.Mirror = s
+ staged := stageKeys(b, s, "moved.example.com", 50)
+ ctx, cancel := context.WithCancel(context.Background())
+ cancel()
+
+ b.dropStagedReplica(ctx, staged)
+
+ assertEachOnceWith(t, s, staged, ctx)
+ s.cmu.Lock()
+ defer s.cmu.Unlock()
+ for k := range staged {
+ if _, ok := s.buckets[b.Cfg.Bucket][k]; !ok {
+ t.Fatalf("key %s deleted through an ended ctx", k)
}
}
}
+
+// A ctx that ends while deletes are in flight: the drop does not return
+// until the held deletes have, and every remaining key is still attempted
+// once with the caller's ctx, as the serial loop would have.
+func TestDropStagedReplicaCancelledDuringTheDeletes(t *testing.T) {
+ b := testExecBackend(t)
+ const n = 60
+ s := newCountingDeleteStore().withBarrier(n)
+ s.respectCtx = true
+ b.Mirror = s
+ staged := stageKeys(b, s, "moved.example.com", n)
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+
+ done := dropInBackground(b, ctx, staged)
+ awaitStarted(t, s, lease.DefaultMirrorWorkers)
+ cancel()
+ assertNotReturned(t, done)
+ close(s.release)
+ awaitDone(t, done)
+
+ assertEachOnceWith(t, s, staged, ctx)
+}