activator: retire generations in the background after replicating
Once a generations wake's replicator is running (execLease.Replicate, once per lease), the backend runs one retirement pass for its take in the background, bounded at two minutes: Held.Retire over FilesStore and the versioned bucket, with the replicator's lease check before each generation. A failure logs ha_generation_retire_failed and never fails the wake; the next take finishes what is left.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+149
−1
internal/activator/backend_exec.go+11 −0internal/activator/hagenerations_test.go+90 −0internal/activator/hawake.go+48 −1
diff --git a/internal/activator/backend_exec.go b/internal/activator/backend_exec.go| index 10b6bb913..f4839182b 100644 |
| --- a/internal/activator/backend_exec.go |
| +++ b/internal/activator/backend_exec.go |
| @@ -482,6 +482,9 @@ type ExecBackend struct { |
| suspectHolders suspectHolderState |
| // haWakes is each host's last Acquire classification (hawake.go). |
| haWakes haWakes |
| + // retiring counts the generation retirements running in the |
| + // background (startRetirement, hawake.go); tests wait on it. |
| + retiring sync.WaitGroup |
| // entryFiles is the Service's lookup of an awake entry's files |
| // publisher (hasecrets.go), installed by New. Nil: none. |
| entryFiles func(host string) *hostFiles |
| @@ -944,6 +947,11 @@ type execLease struct { |
| rep atomic.Pointer[lease.Replicator] |
| localMu sync.Mutex |
| local bool |
| + // replicating, when set (an HA lease, hawake.go), runs once after the |
| + // first Replicate that succeeds, with its replicator: it starts the |
| + // take's retirement in the background (startRetirement). |
| + replicating func(*lease.Replicator) |
| + replicatingOnce sync.Once |
| } |
| func (l *execLease) Replicate(ctx context.Context, dbPath string, syncInterval time.Duration) (Rep, error) { |
| @@ -960,6 +968,9 @@ func (l *execLease) Replicate(ctx context.Context, dbPath string, syncInterval t |
| return nil, err |
| } |
| l.rep.Store(rep) |
| + if l.replicating != nil { |
| + l.replicatingOnce.Do(func() { l.replicating(rep) }) |
| + } |
| return rep, nil |
| } |
diff --git a/internal/activator/hagenerations_test.go b/internal/activator/hagenerations_test.go| index 0e3eab7c2..b385aec0c 100644 |
| --- a/internal/activator/hagenerations_test.go |
| +++ b/internal/activator/hagenerations_test.go |
| @@ -21,6 +21,7 @@ import ( |
| "github.com/carlosframework/platform/internal/hostfiles" |
| "github.com/carlosframework/platform/internal/lease" |
| "github.com/carlosframework/platform/internal/registry" |
| + "github.com/carlosframework/platform/internal/release" |
| "github.com/carlosframework/platform/internal/replicagen" |
| ) |
| @@ -820,3 +821,92 @@ func TestHAWakeTruncatedReplicaRefusesEveryWake(t *testing.T) { |
| refused(t, s, want, 2) |
| }) |
| } |
| + |
| +// Retirement (spec §8) runs once per take, in the background, after the |
| +// replicator has started: it never holds the wake, and its one ListDirs |
| +// comes after the switch. Here the take's switch drops @root from |
| +// history (ended eight days ago), so the root layout's LTX file goes; an |
| +// abandoned g-0 written just now, lock.json and head.json stay. |
| +func TestHAWakeRetiresAfterTheReplicatorStarts(t *testing.T) { |
| + t.Setenv("CARLOS_HA_GENERATIONS", "on") |
| + s, e := haServiceEntry(t, "shop.example.com") |
| + s.b.retiring.Wait() |
| + s.insertRows(t, 0, 3) |
| + s.stopForTest(e) |
| + s.loseLocalDB(t) |
| + h, _ := s.head(t) |
| + h.History = []lease.HeadHistory{{Generation: replicagen.Root, EndedAt: time.Now().Add(-8 * 24 * time.Hour)}} |
| + body, err := json.Marshal(h) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + s.fx.PutObject(s.headKey(), body) |
| + rootLTX := s.loc() + "/0000/0000000000000001-0000000000000001.ltx" |
| + stray := s.loc() + "/" + replicagen.Name(0) + "/0000/0000000000000001-0000000000000001.ltx" |
| + s.fx.PutObject(rootLTX, []byte("root")) |
| + s.fx.PutObject(stray, []byte("abandoned")) |
| + |
| + before := len(s.fx.Requests()) |
| + e, err = s.wakeForTest(context.Background()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + s.b.retiring.Wait() |
| + reqs := s.fx.Requests()[before:] |
| + switchAt := slices.Index(reqs, "PUT "+s.headKey()) |
| + var dirs []int |
| + for i, r := range reqs { |
| + if r == "LIST "+s.loc()+"/" { |
| + dirs = append(dirs, i) |
| + } |
| + } |
| + if switchAt < 0 || len(dirs) != 1 || dirs[0] < switchAt { |
| + t.Fatalf("switch at %d, ListDirs at %v; want one ListDirs after the switch: %v", switchAt, dirs, reqs) |
| + } |
| + if _, ok := s.fx.Object(rootLTX); ok { |
| + t.Fatal("the root LTX file survived @root leaving history") |
| + } |
| + for _, k := range []string{stray, s.loc() + "/lock.json", s.headKey()} { |
| + if _, ok := s.fx.Object(k); !ok { |
| + t.Fatalf("%s was retired", k) |
| + } |
| + } |
| + if n := countRows(t, s.b.pathsFor(s.rt).dbPath); n != 3 { |
| + t.Fatalf("%d rows, want 3", n) |
| + } |
| + s.stopForTest(e) |
| +} |
| + |
| +// listDirsFails fails ListDirs of one prefix, as a refused listing would. |
| +type listDirsFails struct { |
| + release.Store |
| + prefix string |
| +} |
| + |
| +func (l listDirsFails) ListDirs(ctx context.Context, prefix string) ([]string, error) { |
| + if prefix == l.prefix { |
| + return nil, errors.New("injected listing failure") |
| + } |
| + return l.Store.ListDirs(ctx, prefix) |
| +} |
| + |
| +// A retirement that fails is logged as ha_generation_retire_failed and |
| +// never fails the wake: the host serves, and the next take tries again. |
| +func TestHAWakeRetirementFailureNeverFailsTheWake(t *testing.T) { |
| + t.Setenv("CARLOS_HA_GENERATIONS", "on") |
| + lines := captureLogLines(t) |
| + s := haService(t, "shop.example.com") |
| + s.b.FilesStore = listDirsFails{Store: s.b.FilesStore, prefix: s.loc() + "/"} |
| + e, err := s.wakeForTest(context.Background()) |
| + if err != nil { |
| + t.Fatalf("wake: %v", err) |
| + } |
| + s.b.retiring.Wait() |
| + if n := lines.matching("ha_generation_retire_failed host=shop.example.com", "injected listing failure"); n != 1 { |
| + t.Fatalf("%d ha_generation_retire_failed lines, want 1", n) |
| + } |
| + if e == nil || e.state != actAwake { |
| + t.Fatalf("entry %+v, want awake", e) |
| + } |
| + s.stopForTest(e) |
| +} |
diff --git a/internal/activator/hawake.go b/internal/activator/hawake.go| index 943c09049..655e3b67c 100644 |
| --- a/internal/activator/hawake.go |
| +++ b/internal/activator/hawake.go |
| @@ -471,7 +471,8 @@ func (b *ExecBackend) acquireClassified(ctx context.Context, rt registry.Route) |
| if note, ok := ctx.Value(dutyAcquiredKey{}).(func(context.Context, registry.Route, hafresh.Marker)); ok { |
| note(ctx, rt, hafresh.Marker{Lifetime: entry.ID}) |
| } |
| - return &execLease{held: held, defaultDB: b.pathsFor(rt).dbPath}, nil |
| + return &execLease{held: held, defaultDB: b.pathsFor(rt).dbPath, |
| + replicating: func(rep *lease.Replicator) { b.startRetirement(host, held, rep) }}, nil |
| } |
| // haAcquired reports whether host's last Acquire took the HA path, and |
| @@ -816,6 +817,52 @@ func (b *ExecBackend) restoreHAGenerations(ctx context.Context, rt registry.Rout |
| return !restored, nil |
| } |
| +// retireTimeout bounds one retirement pass (spec §8): it runs in the |
| +// background, so a slow bucket costs storage for a while, never a wake. |
| +const retireTimeout = 2 * time.Minute |
| + |
| +// startRetirement runs one retirement pass (lease.Held.Retire, spec §8) |
| +// for the take on held, in the background, once its replicator is |
| +// running: the generations its switch dropped from head.json's history, |
| +// abandoned g-* prefixes over a day old, and the root layout's LTX files |
| +// once @root has left history. A held that is not a storage generations |
| +// lock does nothing. The pass checks the lease through rep (Replicate |
| +// spent held) before each generation, has its own two-minute bound, and |
| +// a failure is logged as ha_generation_retire_failed and never fails the |
| +// wake: the next take retries what is left. |
| +func (b *ExecBackend) startRetirement(host string, held *lease.Held, rep *lease.Replicator) { |
| + if held == nil || rep == nil || held.GenName() == "" { |
| + return |
| + } |
| + b.retiring.Add(1) |
| + go func() { |
| + defer b.retiring.Done() |
| + ctx, cancel := context.WithTimeout(context.Background(), retireTimeout) |
| + defer cancel() |
| + stats, err := b.retire(ctx, held, rep) |
| + if err != nil { |
| + log.Printf("ha_generation_retire_failed host=%s retired=%v objects=%d: %v", host, stats.Retired, stats.Objects, err) |
| + return |
| + } |
| + if len(stats.Retired) > 0 || len(stats.Young) > 0 { |
| + log.Printf("activator: %s: retired generations %v (%d objects); kept abandoned under a day old %v", host, stats.Retired, stats.Objects, stats.Young) |
| + } |
| + }() |
| +} |
| + |
| +// retire is startRetirement's pass over the hibernate bucket: head.json's |
| +// store (FilesStore) and the bucket by version (newTakeStore). |
| +func (b *ExecBackend) retire(ctx context.Context, held *lease.Held, rep *lease.Replicator) (lease.RetireStats, error) { |
| + if b.FilesStore == nil { |
| + return lease.RetireStats{}, errors.New("no files store configured") |
| + } |
| + vs, err := newTakeStore(ctx, b.Cfg.Bucket, b.Cfg.Region) |
| + if err != nil { |
| + return lease.RetireStats{}, err |
| + } |
| + return held.Retire(ctx, b.FilesStore, vs, rep.Err, time.Now()) |
| +} |
| + |
| // keptBaseline is the manifest a kept generations wake fences: the one |
| // its release kept beside the local record when that is the revision the |
| // marker names (gen; no request), else the manifest as it is now (one |