| 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) |
| +} |