Offline move trim T1: drop the staged replica in parallel
The first wake after an offline move deleted the 270-305 staged keys under moves/<host>/ one DeleteObject at a time before spawning. They now run side by side on the promotion's own worker count (DefaultMirrorWorkers, 8, or the store's MirrorConcurrency answer). Same keys, each once, by key; no order (the loop ranged over a map); still best-effort, a failed delete cancels nothing; and the drop returns only once every delete has, so it still finishes before the app starts.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+268
−1
docs/superpowers/plans/2026-10-25-offline-move-trim-t1.md+57 −0internal/activator/backend_exec.go+37 −1internal/activator/dropstaged_test.go+174 −0
diff --git a/docs/superpowers/plans/2026-10-25-offline-move-trim-t1.md b/docs/superpowers/plans/2026-10-25-offline-move-trim-t1.md| new file mode 100644 |
| index 000000000..306cf5f52 |
| --- /dev/null |
| +++ b/docs/superpowers/plans/2026-10-25-offline-move-trim-t1.md |
| @@ -0,0 +1,57 @@ |
| +# Offline move trim T1: drop the staged replica in parallel |
| + |
| +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. |
| + |
| +**Goal:** The first wake after an offline move deletes the 270-305 staged keys under `moves/<host>/` with bounded parallelism instead of one `DeleteObject` at a time, still before the app starts. |
| + |
| +**Architecture:** Only `(*ExecBackend).dropStagedReplica` in `internal/activator/backend_exec.go` changes. It keeps its signature and its one caller (`Restore`, after a clean restore of a copy promoted on this call). The loop becomes a fixed pool of workers fed the same keys; the function returns only when every delete has returned, so the wake still does nothing more until the drop is over. |
| + |
| +**Tech Stack:** Go, `lease.MirrorStore`. |
| + |
| +**Spec:** `docs/superpowers/notes/offline-move-pause-trim.md` §4, item "T1a"; row T1 of `docs/superpowers/plans/2026-10-25-claims-cleanup.md`. |
| + |
| +## Global Constraints |
| + |
| +- Same keys: exactly the keys of the `staged` map `promoteStagedReplica` returned, each deleted once with `b.Mirror.Delete(ctx, b.Cfg.Bucket, key)`. Today's code deletes by key, not by version id; that stays. |
| +- Same failure semantics as the serial loop: best-effort. A failed delete is ignored, does not stop or cancel the other deletes, is not returned, and does not fail the wake. A leftover staged key is storage, not correctness (the existing comment), and the next landing or promotion owns it. |
| +- Order: none. The serial loop ranges over a Go map, whose order is unspecified and randomised per run, so no caller can depend on it. |
| +- Bound: the promotion's own worker count, `lease.DefaultMirrorWorkers` (8), or the store's `lease.MirrorConcurrency` answer (floored at 1), matching `lease.mirrorWorkers`. Never more workers than keys. |
| +- Do not edit `internal/lease/mirror.go` (changed on self-serve-moves-2 and -3). |
| +- No user-facing string changes. |
| + |
| +## Review Focus |
| + |
| +- A store whose `Delete` fails for one key: every other key is still deleted and the wake still succeeds (Task 1 test). |
| +- A store that answers `MirrorWorkers() == 1`: strictly serial, as before (Task 1 test, bound assertion). |
| +- The drop must finish before `Restore` returns, or the app could start with staging present and "staging wins" could put an old copy back after a crash (Task 1 test: no delete in flight once the call returns). |
| +- An empty or nil `staged` map: no goroutines, no calls (caller already guards `len(promoted) > 0`; the function handles it anyway). |
| +- A cancelled ctx: each key is still handed to `Delete` with that ctx, as the serial loop did. |
| + |
| +--- |
| + |
| +### Task 1: Parallel drop with the promotion's bound |
| + |
| +**Files:** |
| +- Modify: `internal/activator/backend_exec.go` (`dropStagedReplica`) |
| +- Test: `internal/activator/dropstaged_test.go` (new) |
| + |
| +**Interfaces:** |
| +- Consumes: `lease.MirrorStore`, `lease.MirrorConcurrency`, `lease.DefaultMirrorWorkers`. |
| +- Produces: `func (b *ExecBackend) dropStagedReplica(ctx context.Context, staged map[string]lease.MirrorObject)` (unchanged signature) and `func dropWorkers(m lease.MirrorStore, n int) int`. |
| + |
| +- [ ] **Step 1: Write the failing tests** in `dropstaged_test.go` with a counting store wrapping `fakeMirrorStore` (in-flight counter with max, a delay per delete, a key set that fails, a record of every key deleted): |
| + - `TestDropStagedReplicaDeletesEveryStagedKeyInParallel`: 300 staged keys plus one unrelated key under `instances/`; after the call every staged key is gone, each deleted exactly once, the unrelated key remains, max in flight is > 1 and <= `lease.DefaultMirrorWorkers`, and in-flight is 0 when the call returns. |
| + - `TestDropStagedReplicaHonoursTheStoresConcurrency`: a store answering `MirrorWorkers() == 1` sees max in flight 1; one answering 3 sees at most 3. |
| + - `TestDropStagedReplicaFailureIsBestEffort`: one key's delete fails; every other key is deleted, the failing key is attempted exactly once and stays, and the call returns normally. |
| +- [ ] **Step 2: Run** `nice -n 19 go test ./internal/activator/ -run TestDropStagedReplica -count=1`. Expected: FAIL on the in-flight > 1 assertion (serial loop). |
| +- [ ] **Step 3: Implement** `dropWorkers` (the `mirrorWorkers` rule, capped at n) and the worker pool in `dropStagedReplica`: collect keys, feed a channel, `sync.WaitGroup`, errors dropped, no cancellation. |
| +- [ ] **Step 4: Run** the Step 2 command plus `-run 'Staged|Promot|Boomerang'`. Expected: PASS. |
| +- [ ] **Step 5: Commit.** |
| + |
| +### Task 2: Gates, census, push |
| + |
| +**Files:** census overlay files only if `TestPlan14RootReadCensus` fails. |
| + |
| +- [ ] **Step 1:** `go build ./...`, `GOOS=darwin go build ./...`, `GOOS=windows go build ./...`, `go vet ./internal/activator/`, `gofmt -l internal/activator`, `nice -n 19 go test -race ./internal/activator/ -count=1`. Expected: all clean. |
| +- [ ] **Step 2:** `nice -n 19 go test ./internal/provision/ -run TestPlan14RootReadCensus -count=1`. If it fails: `make census-card`; a CLAIMS file; `make census-propose NAME=offline-move-trim-t1 CLAIMS=<file>`. If the card shows a decision class (`reads`, `new`, `removed`, `other`), stop and report. |
| +- [ ] **Step 3:** Commit, push to origin, `amadan ci status carlos/platform -branch offline-move-trim-t1`. No merge, no review request. |
diff --git a/internal/activator/backend_exec.go b/internal/activator/backend_exec.go| index ae034bcc0..ccb0ad92f 100644 |
| --- a/internal/activator/backend_exec.go |
| +++ b/internal/activator/backend_exec.go |
| @@ -3533,8 +3533,44 @@ func (b *ExecBackend) promoteStagedReplica(ctx context.Context, host string) (ma |
| // third listing of a prefix this restore has already walked twice. |
| // Best-effort: a leftover staging copy is storage, not a correctness |
| // problem. |
| +// |
| +// The deletes run side by side (offline-move trim T1), on as many workers |
| +// as the promotion just before it used (dropWorkers): 270 to 305 staged |
| +// 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). |
| func (b *ExecBackend) dropStagedReplica(ctx context.Context, staged map[string]lease.MirrorObject) { |
| + keys := make(chan string) |
| + var wg sync.WaitGroup |
| + for w := dropWorkers(b.Mirror, len(staged)); w > 0; w-- { |
| + wg.Add(1) |
| + go func() { |
| + defer wg.Done() |
| + for k := range keys { |
| + _ = b.Mirror.Delete(ctx, b.Cfg.Bucket, k) |
| + } |
| + }() |
| + } |
| for k := range staged { |
| - _ = b.Mirror.Delete(ctx, b.Cfg.Bucket, k) |
| + keys <- k |
| + } |
| + close(keys) |
| + wg.Wait() |
| +} |
| + |
| +// dropWorkers is how many deletes dropStagedReplica runs at once: the |
| +// mirror round's own rule (lease.DefaultMirrorWorkers, or the store's |
| +// lease.MirrorConcurrency answer floored at one), so the drop never puts |
| +// more requests in flight than the promotion it follows did. Never more |
| +// workers than keys. |
| +func dropWorkers(m lease.MirrorStore, n int) int { |
| + w := lease.DefaultMirrorWorkers |
| + if c, ok := m.(lease.MirrorConcurrency); ok { |
| + w = max(c.MirrorWorkers(), 1) |
| } |
| + return min(w, n) |
| } |
diff --git a/internal/activator/dropstaged_test.go b/internal/activator/dropstaged_test.go| new file mode 100644 |
| index 000000000..72eb7b30f |
| --- /dev/null |
| +++ b/internal/activator/dropstaged_test.go |
| @@ -0,0 +1,174 @@ |
| +package activator |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "fmt" |
| + "sync" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/lease" |
| +) |
| + |
| +// 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. |
| +type countingDeleteStore struct { |
| + *fakeMirrorStore |
| + hold time.Duration |
| + fail map[string]bool |
| + |
| + cmu sync.Mutex |
| + inFlight int |
| + maxSeen int |
| + calls map[string]int |
| +} |
| + |
| +func newCountingDeleteStore() *countingDeleteStore { |
| + return &countingDeleteStore{ |
| + fakeMirrorStore: newFakeMirrorStore(), |
| + hold: 2 * time.Millisecond, |
| + fail: map[string]bool{}, |
| + calls: map[string]int{}, |
| + } |
| +} |
| + |
| +func (s *countingDeleteStore) Delete(ctx context.Context, bucket, key string) error { |
| + s.cmu.Lock() |
| + s.inFlight++ |
| + if s.inFlight > s.maxSeen { |
| + s.maxSeen = s.inFlight |
| + } |
| + s.calls[key]++ |
| + s.cmu.Unlock() |
| + defer func() { |
| + s.cmu.Lock() |
| + s.inFlight-- |
| + s.cmu.Unlock() |
| + }() |
| + time.Sleep(s.hold) |
| + if s.fail[key] { |
| + return errors.New("injected delete failure") |
| + } |
| + return s.fakeMirrorStore.Delete(ctx, bucket, key) |
| +} |
| + |
| +// workersStore is a countingDeleteStore that answers lease.MirrorConcurrency. |
| +type workersStore struct { |
| + *countingDeleteStore |
| + workers int |
| +} |
| + |
| +func (s *workersStore) MirrorWorkers() int { return s.workers } |
| + |
| +// stageKeys puts n staged objects for host and returns them in the shape |
| +// promoteStagedReplica hands to dropStagedReplica. |
| +func stageKeys(b *ExecBackend, s *countingDeleteStore, host string, n int) map[string]lease.MirrorObject { |
| + staged := map[string]lease.MirrorObject{} |
| + for i := 0; i < n; i++ { |
| + k := fmt.Sprintf("%sgenerations/0000/%08d.ltx", MovesPrefix(host), i) |
| + s.put(b.Cfg.Bucket, k, fmt.Sprintf("e%d", i)) |
| + staged[k] = lease.MirrorObject{ETag: fmt.Sprintf("e%d", i)} |
| + } |
| + return staged |
| +} |
| + |
| +// 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). |
| +func TestDropStagedReplicaDeletesEveryStagedKeyInParallel(t *testing.T) { |
| + b := testExecBackend(t) |
| + s := newCountingDeleteStore() |
| + b.Mirror = s |
| + host := "moved.example.com" |
| + staged := stageKeys(b, s, host, 300) |
| + own := b.Cfg.hibernatePrefix(host) + "/generations/0000/00000000.ltx" |
| + s.put(b.Cfg.Bucket, own, "own") |
| + |
| + b.dropStagedReplica(context.Background(), staged) |
| + |
| + 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) |
| + } |
| + } |
| + if _, ok := s.buckets[b.Cfg.Bucket][own]; !ok { |
| + t.Fatal("the drop deleted a key it was not handed") |
| + } |
| +} |
| + |
| +// 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} |
| + b.Mirror = s |
| + staged := stageKeys(b, s.countingDeleteStore, "moved.example.com", 40) |
| + |
| + b.dropStagedReplica(context.Background(), staged) |
| + |
| + 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)) |
| + } |
| + } |
| +} |
| + |
| +// The drop stays best-effort, exactly as the serial loop was: a failed |
| +// delete neither stops nor cancels the others, is tried once, and leaves |
| +// only its own key behind. |
| +func TestDropStagedReplicaFailureIsBestEffort(t *testing.T) { |
| + b := testExecBackend(t) |
| + s := newCountingDeleteStore() |
| + b.Mirror = s |
| + staged := stageKeys(b, s, "moved.example.com", 100) |
| + var bad string |
| + for k := range staged { |
| + bad = k |
| + break |
| + } |
| + s.fail[bad] = true |
| + |
| + b.dropStagedReplica(context.Background(), staged) |
| + |
| + 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 { |
| + t.Fatal("the failing key was removed") |
| + } |
| + 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]) |
| + } |
| + } |
| +} |