feat: prepare stateless releases before wake and rollout execution
Carry the canonical selection into each attempt and use the rollout failure book for durable deploy IDs. Prepare before serving or stopping an incumbent, retain successful preparation across a hold, and skip the stateful release dispatcher for stateless builds. Cover ordering, terminal failures, restart reuse and one canonical read.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
15 files changed,
+450
−29
internal/activator/activator.go+13 −1internal/activator/rootfsrelease.go+9 −0internal/activator/stateless.go+82 −5internal/activator/stateless_test.go+143 −2internal/activator/swap.go+37 −12internal/activator/swaphold.go+7 −5internal/activator/wakeplan.go+1 −0internal/host/rollout.go+6 −3internal/host/rolloutdeclined.go+1 −0internal/host/stagerelay.go+5 −0internal/host/stateless_runtime_test.go+40 −0internal/release/runtime_selection.go+48 −0internal/release/runtime_selection_test.go+36 −0internal/rollout/manager.go+12 −1internal/rollout/wakeplan.go+10 −0
diff --git a/internal/activator/activator.go b/internal/activator/activator.go| index a6725f6b8..cabc70f24 100644 |
| --- a/internal/activator/activator.go |
| +++ b/internal/activator/activator.go |
| @@ -479,7 +479,8 @@ const ( |
| ) |
| type actEntry struct { |
| - claimResult *wokenInstance |
| + statelessPrepared *statelessPreparation |
| + claimResult *wokenInstance |
| // rootfsAdmittedKey: begin made this entry right after the backend's |
| // rootfs gates passed for the row with this key (rootfsGateKey), so |
| // doWake asks only the cheap locked half again for that same row and |
| @@ -2037,6 +2038,7 @@ func (a *Service) wake(e *actEntry) { |
| } |
| a.mu.Unlock() |
| wakeStart := a.now() |
| + ctx = carryStatelessPreparation(ctx, e.statelessPrepared) |
| w, err := a.doWake(ctx, e) |
| if w != nil { |
| w.wakeTook = a.now().Sub(wakeStart) |
| @@ -2234,6 +2236,15 @@ func (a *Service) wake(e *actEntry) { |
| republish = true |
| } |
| a.mu.Unlock() |
| + if err == nil { |
| + if projection, ok := a.Routes.(interface { |
| + SetServingMode(string, string, bool) error |
| + }); ok { |
| + if perr := projection.SetServingMode(e.host, w.spawnVersion, w.gen.Stateless); perr != nil { |
| + log.Printf("rollout: %s: record serving build %s: %v", e.host, w.spawnVersion, perr) |
| + } |
| + } |
| + } |
| close(e.ready) |
| // A planned wake's outcome, for the scheduler: off a.mu, since OnGen |
| // reaches its goodMu, which calls back into Current. |
| @@ -2653,6 +2664,7 @@ func (a *Service) doWakeOnce(ctx context.Context, e *actEntry) (*wokenInstance, |
| } |
| w.steps.mark(a.now(), "identity") |
| + ctx = withDeclarations(ctx, rt, plan.Declarations) |
| modeID := id |
| if plan.planned() { |
| modeID = plan.Boot |
diff --git a/internal/activator/rootfsrelease.go b/internal/activator/rootfsrelease.go| index 737b80520..a03cf85fe 100644 |
| --- a/internal/activator/rootfsrelease.go |
| +++ b/internal/activator/rootfsrelease.go |
| @@ -405,6 +405,15 @@ func (a *Service) runReleaseAdmitted(ctx context.Context, e *actEntry, rt regist |
| // this call returns. |
| lead, recheck := releaseStopLead, releaseLeaseRecheck |
| own := func() error { |
| + if rt.Stateless { |
| + original, ok := ctx.Value(statelessPlacementKey{}).(registry.Route) |
| + if !ok { |
| + return ErrFenced |
| + } |
| + if err := a.statelessCurrent(ctx, original); err != nil { |
| + return err |
| + } |
| + } |
| if err := gate(); err != nil { |
| return err |
| } |
diff --git a/internal/activator/stateless.go b/internal/activator/stateless.go| index b7c9f18f6..a99ff17d0 100644 |
| --- a/internal/activator/stateless.go |
| +++ b/internal/activator/stateless.go |
| @@ -75,6 +75,16 @@ func (a *Service) storageMode(ctx context.Context, rt registry.Route, id release |
| } |
| return observed.mode, observed.digest, nil |
| } |
| + if selections, ok := ctx.Value(storageDeclarationsKey{}).(storageDeclarations); ok && selections.account == rt.Account && selections.app == rt.App { |
| + for _, selection := range selections.values { |
| + if selection.Identity == id { |
| + if err := release.VerifyStaged(id); err != nil { |
| + return false, "", fmt.Errorf("%w: %w", errStorageDeclaration, err) |
| + } |
| + return selection.Stateless, selection.ManifestDigest, nil |
| + } |
| + } |
| + } |
| if a.StorageDeclaration == nil { |
| if rt.Stateless { |
| return false, "", errStorageDeclaration |
| @@ -105,14 +115,11 @@ func (a *Service) doStatelessWake(ctx context.Context, e *actEntry, id release.S |
| return nil, errStorageDeclaration |
| } |
| rt, w := in.rt, in.w |
| - if a.cfg.Record == nil { |
| - return w, ErrFenced |
| - } |
| - facts, err := a.cfg.Record(ctx, rt) |
| + prepared, err := a.prepareStateless(ctx, rt, id, in.digest) |
| if err != nil { |
| return w, err |
| } |
| - ctx = WithReplicaReleaseObservation(ctx, facts, replicas.Build{Version: id.Version, Digest: in.digest}, true) |
| + ctx = prepared |
| if err := a.wakeHostProven(ctx, e, rt); err != nil { |
| return w, err |
| } |
| @@ -255,6 +262,10 @@ func (a *Service) doWake(ctx context.Context, e *actEntry) (*wokenInstance, erro |
| } |
| func (a *Service) statelessObservation(ctx context.Context, rt registry.Route, id release.StagedIdentity, digest string) (context.Context, error) { |
| + if _, target, _, err := ObservedReplicaRelease(ctx, rt); err == nil && target.Version == id.Version && target.Digest == digest { |
| + return ctx, nil |
| + } |
| + |
| if a.cfg.Record == nil { |
| return ctx, ErrFenced |
| } |
| @@ -264,3 +275,69 @@ func (a *Service) statelessObservation(ctx context.Context, rt registry.Route, i |
| } |
| return WithReplicaReleaseObservation(ctx, facts, replicas.Build{Version: id.Version, Digest: digest}, true), nil |
| } |
| + |
| +type storageDeclarationsKey struct{} |
| +type storageDeclarations struct { |
| + account, app string |
| + values []release.RuntimeSelection |
| +} |
| + |
| +func withDeclarations(ctx context.Context, rt registry.Route, values []release.RuntimeSelection) context.Context { |
| + if len(values) == 0 { |
| + return ctx |
| + } |
| + return context.WithValue(ctx, storageDeclarationsKey{}, storageDeclarations{rt.Account, rt.App, values}) |
| +} |
| + |
| +type statelessPlacementKey struct{} |
| + |
| +func (a *Service) prepareStateless(ctx context.Context, rt registry.Route, id release.StagedIdentity, digest string) (context.Context, error) { |
| + if prepared, ok := ctx.Value(statelessPreparedKey{}).(*statelessPreparation); ok && prepared.id == id && prepared.route.Host == rt.Host && prepared.route.Account == rt.Account && prepared.route.App == rt.App && prepared.digest == digest { |
| + observed := context.WithValue(ctx, replicaObservationKey{}, prepared.observation) |
| + if err := a.statelessCurrent(observed, prepared.route); err != nil { |
| + return ctx, err |
| + } |
| + return observed, nil |
| + } |
| + |
| + observed, err := a.statelessObservation(ctx, rt, id, digest) |
| + if err != nil { |
| + return ctx, err |
| + } |
| + identity, target, old, err := ObservedReplicaRelease(observed, rt) |
| + if err != nil { |
| + return ctx, err |
| + } |
| + if a.ReplicaDeployID == nil { |
| + return ctx, ErrFenced |
| + } |
| + deployID, err := a.ReplicaDeployID(identity, target, old) |
| + if err != nil { |
| + return ctx, err |
| + } |
| + observed = context.WithValue(observed, statelessPlacementKey{}, rt) |
| + launch := rt |
| + launch.Stateless = true |
| + if err := a.PrepareStatelessRelease(observed, launch, id, deployID); err != nil { |
| + return ctx, err |
| + } |
| + observation, _ := observed.Value(replicaObservationKey{}).(replicaObservation) |
| + prepared := &statelessPreparation{route: rt, id: id, digest: digest, observation: observation} |
| + return context.WithValue(observed, statelessPreparedKey{}, prepared), nil |
| +} |
| + |
| +type statelessPreparedKey struct{} |
| +type statelessPreparation struct { |
| + route registry.Route |
| + id release.StagedIdentity |
| + digest string |
| + observation replicaObservation |
| +} |
| + |
| +func carryStatelessPreparation(ctx context.Context, p *statelessPreparation) context.Context { |
| + if p == nil { |
| + return ctx |
| + } |
| + ctx = withStorageMode(ctx, p.route, p.id, true, p.digest) |
| + return context.WithValue(ctx, statelessPreparedKey{}, p) |
| +} |
diff --git a/internal/activator/stateless_test.go b/internal/activator/stateless_test.go| index 132089543..86f797f3e 100644 |
| --- a/internal/activator/stateless_test.go |
| +++ b/internal/activator/stateless_test.go |
| @@ -126,13 +126,22 @@ func (b *statelessWakeBackend) RootfsWorkerGone(ctx context.Context, rt registry |
| return b.workers.RootfsWorkerGone(ctx, rt, name) |
| } |
| -func wireStatelessRig(r *wakeRelRig) { |
| +func wireStatelessRig(r *wakeRelRig) *replicaReleaseStore { |
| + store := &replicaReleaseStore{held: newOwnedLease(time.Now().Add(time.Hour), false)} |
| + r.a.Coordinator = store |
| + r.a.ObserveReplicaRelease = ObservedReplicaRelease |
| + r.a.ReplicaDeployID = func(_ replicas.Identity, target replicas.Build, _ *replicas.Deploy) (string, error) { |
| + return "deploy-" + target.Version, nil |
| + } |
| r.a.StorageDeclaration = func(context.Context, registry.Route, release.StagedIdentity) (bool, string, error) { |
| return true, "canonical", nil |
| } |
| r.a.cfg.Record = func(context.Context, registry.Route) (RecordFacts, error) { |
| - return RecordFactsOf(provision.Record{Account: r.rt.Account, App: r.rt.App, Host: r.rt.Host, CreatedAt: time.Unix(1, 0)}) |
| + facts, err := RecordFactsOf(provision.Record{Account: r.rt.Account, App: r.rt.App, Host: r.rt.Host, CreatedAt: time.Unix(1, 0)}) |
| + facts.ReplicaDeploy = store.snapshot() |
| + return facts, err |
| } |
| + return store |
| } |
| func TestStatelessWakeHasNoDatabaseWork(t *testing.T) { |
| @@ -427,3 +436,135 @@ func TestStatelessModeSourceVerification(t *testing.T) { |
| }) |
| } |
| } |
| + |
| +func TestStatelessReleaseBeforeServeAndRestart(t *testing.T) { |
| + r := newWakeRelRig(t) |
| + store := wireStatelessRig(r) |
| + cold := r.wake(r.a) |
| + unit := r.nextUnit(t) |
| + if len(r.b.started()) != 0 { |
| + t.Fatal("web started before release") |
| + } |
| + if d := store.snapshot(); d == nil || d.Phase != "releasing" { |
| + t.Fatalf("release evidence=%+v", d) |
| + } |
| + unit.done("exited", "0", "success") |
| + if err := waitErr(t, cold); err != nil { |
| + t.Fatal(err) |
| + } |
| + id := store.snapshot().ID |
| + if len(r.b.releaseStarts()) != 1 || store.snapshot().Phase != "released" { |
| + t.Fatal("release did not run once") |
| + } |
| + r.a.Shutdown(context.Background()) |
| + restarted := r.service(t) |
| + restarted.StorageDeclaration = r.a.StorageDeclaration |
| + restarted.cfg.Record = r.a.cfg.Record |
| + restarted.ReplicaDeployID = r.a.ReplicaDeployID |
| + restarted.ObserveReplicaRelease = ObservedReplicaRelease |
| + restarted.Coordinator = store |
| + before := store.acquires |
| + if err := waitErr(t, r.wake(restarted)); err != nil { |
| + t.Fatal(err) |
| + } |
| + if store.snapshot().ID != id || len(r.b.releaseStarts()) != 1 || store.acquires != before { |
| + t.Fatal("restart changed ID, reran release, or acquired coordination for released evidence") |
| + } |
| +} |
| + |
| +func TestStatelessFailedReleaseKeepsOldBuild(t *testing.T) { |
| + for _, terminal := range []string{"failed", "unknown"} { |
| + t.Run(terminal, func(t *testing.T) { |
| + r := newWakeRelRig(t) |
| + store := wireStatelessRig(r) |
| + r.setPlan(WakePlan{Boot: r.good, Mode: ModeOverlap}) |
| + if err := waitErr(t, r.wake(r.a)); err != nil { |
| + t.Fatal(err) |
| + } |
| + result := make(chan SwapResult, 1) |
| + attempt := Attempt{Host: r.rt.Host, Build: r.next.Version, StagedPath: r.next.StagedPath, Digest: r.next.Digest, Mode: ModeOverlap, SchedRev: 1} |
| + go func() { result <- r.a.Swap(context.Background(), attempt, func(string) uint64 { return 1 }) }() |
| + unit := r.nextUnit(t) |
| + before := store.acquires |
| + if err := waitErr(t, r.wake(r.a)); err != nil { |
| + t.Fatal(err) |
| + } |
| + if store.acquires != before { |
| + t.Fatal("serving request waited on coordination") |
| + } |
| + unit.done("exited", "7", "exit-code") |
| + select { |
| + case got := <-result: |
| + if got.Outcome == SwapDone { |
| + t.Fatalf("failed release served: %+v", got) |
| + } |
| + case <-time.After(5 * time.Second): |
| + t.Fatal("release did not finish") |
| + } |
| + store.mu.Lock() |
| + store.d.Phase = terminal |
| + store.mu.Unlock() |
| + got := r.a.Swap(context.Background(), attempt, func(string) uint64 { return 1 }) |
| + current, ok := r.a.Current(r.rt.Host) |
| + if got.Outcome == SwapDone || !ok || current.Build != "v1" || len(r.b.releaseStarts()) != 1 { |
| + t.Fatalf("terminal release replaced old build: %+v current=%+v", got, current) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestStatelessReleaseBeforeSwap(t *testing.T) { |
| + for _, oldStateless := range []bool{false, true} { |
| + t.Run(fmt.Sprint(oldStateless), func(t *testing.T) { |
| + r := newWakeRelRig(t) |
| + store := wireStatelessRig(r) |
| + r.a.StorageDeclaration = func(_ context.Context, _ registry.Route, id release.StagedIdentity) (bool, string, error) { |
| + return id.Version == "v2" || oldStateless, "canonical-" + id.Version, nil |
| + } |
| + r.setPlan(WakePlan{Boot: r.good, Mode: ModeOverlap}) |
| + if err := waitErr(t, r.wake(r.a)); err != nil { |
| + t.Fatal(err) |
| + } |
| + result := make(chan SwapResult, 1) |
| + go func() { |
| + result <- r.a.Swap(context.Background(), Attempt{Host: r.rt.Host, Build: r.next.Version, StagedPath: r.next.StagedPath, Digest: r.next.Digest, Mode: ModeOverlap, SchedRev: 1}, func(string) uint64 { return 1 }) |
| + }() |
| + unit := r.nextUnit(t) |
| + if current, ok := r.a.Current(r.rt.Host); !ok || current.Build != "v1" { |
| + t.Fatalf("old build stopped before release: %+v", current) |
| + } |
| + if err := waitErr(t, r.wake(r.a)); err != nil { |
| + t.Fatal(err) |
| + } |
| + unit.done("exited", "0", "success") |
| + select { |
| + case got := <-result: |
| + if got.Outcome != SwapDone || !got.Gen.Stateless { |
| + t.Fatalf("swap=%+v", got) |
| + } |
| + case <-time.After(5 * time.Second): |
| + t.Fatal("swap did not finish") |
| + } |
| + if len(r.b.releaseStarts()) != 1 || store.snapshot().Phase != "released" { |
| + t.Fatal("release reran or did not publish success") |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestStatelessMovedWhileReleaseWaits(t *testing.T) { |
| + r := newWakeRelRig(t) |
| + wireStatelessRig(r) |
| + pending := r.wake(r.a) |
| + unit := r.nextUnit(t) |
| + moved := r.routes.get(r.rt.Host) |
| + moved.Assigned = "moved" |
| + r.routes.set(moved) |
| + unit.done("exited", "0", "success") |
| + if err := waitErr(t, pending); err == nil { |
| + t.Fatal("moved build served") |
| + } |
| + if len(r.b.started()) != 0 { |
| + t.Fatal("web started on the old box") |
| + } |
| +} |
diff --git a/internal/activator/swap.go b/internal/activator/swap.go| index 948ee4004..8fbefdd1e 100644 |
| --- a/internal/activator/swap.go |
| +++ b/internal/activator/swap.go |
| @@ -41,17 +41,18 @@ const ( |
| // Attempt is one try at moving one instance to a new generation (spec §6.3). |
| type Attempt struct { |
| - Host string |
| - Cause Cause |
| - Build string |
| - StagedPath string |
| - Digest string // expected sha256 of StagedPath, verified before exec |
| - Demand string // release demand for the artifact: "hold" or "" |
| - Mode SwapMode |
| - ConfigRev string |
| - RestartID string |
| - SchedRev uint64 // scheduler revision; the newest-attempt check compares it |
| - Canary bool // observe the 10 s settle |
| + Declaration *release.RuntimeSelection |
| + Host string |
| + Cause Cause |
| + Build string |
| + StagedPath string |
| + Digest string // expected sha256 of StagedPath, verified before exec |
| + Demand string // release demand for the artifact: "hold" or "" |
| + Mode SwapMode |
| + ConfigRev string |
| + RestartID string |
| + SchedRev uint64 // scheduler revision; the newest-attempt check compares it |
| + Canary bool // observe the 10 s settle |
| } |
| // SwapOutcome is how an attempt ended. |
| @@ -335,12 +336,32 @@ func (a *Service) Swap(ctx context.Context, at Attempt, newest func(host string) |
| oldStateless = current.gen.Stateless |
| } |
| a.mu.Unlock() |
| + if a.Routes != nil && current != nil { |
| + fresh, err := a.Routes.Get(at.Host) |
| + if err != nil { |
| + return abandon(err) |
| + } |
| + if fresh == nil { |
| + return abandon(ErrFenced) |
| + } |
| + route = *fresh |
| + } |
| id := release.StagedIdentity{Version: at.Build, StagedPath: at.StagedPath, Digest: at.Digest, Demand: at.Demand} |
| + if at.Declaration != nil { |
| + ctx = withDeclarations(ctx, route, []release.RuntimeSelection{*at.Declaration}) |
| + } |
| mode, digest, modeErr := a.storageMode(ctx, route, id) |
| if modeErr != nil { |
| return abandon(modeErr) |
| } |
| ctx = withStorageMode(ctx, route, id, mode, digest) |
| + if mode { |
| + prepared, err := a.prepareStateless(ctx, route, id, digest) |
| + if err != nil { |
| + return abandon(err) |
| + } |
| + ctx = prepared |
| + } |
| if mode != oldStateless { |
| at.Mode = ModeHold |
| } |
| @@ -452,7 +473,11 @@ func (a *Service) spawnSuccessor(ctx context.Context, gb SwapBackend, e *actEntr |
| if err := previous(); err != nil { |
| return err |
| } |
| - return a.statelessCurrent(ctx, rt) |
| + check := rt |
| + if prepared, ok := ctx.Value(statelessPreparedKey{}).(*statelessPreparation); ok { |
| + check = prepared.route |
| + } |
| + return a.statelessCurrent(ctx, check) |
| } |
| } |
| } |
diff --git a/internal/activator/swaphold.go b/internal/activator/swaphold.go| index 57217839f..619fd2594 100644 |
| --- a/internal/activator/swaphold.go |
| +++ b/internal/activator/swaphold.go |
| @@ -27,10 +27,11 @@ var ( |
| // finishStop answers on woke exactly once, unless swapHold withdraws the |
| // hold first (under a.mu, by clearing the entry's holdNext). |
| type holdSpec struct { |
| - gen Gen // the attempt's build, staged path, digest and demand; N is drawn at spawn |
| - at Attempt |
| - newest func(string) uint64 |
| - woke chan holdWoke // buffered 1 |
| + statelessPrepared *statelessPreparation |
| + gen Gen // the attempt's build, staged path, digest and demand; N is drawn at spawn |
| + at Attempt |
| + newest func(string) uint64 |
| + woke chan holdWoke // buffered 1 |
| // settleCancel ends a canary's settle observation. holdWakeLocked puts |
| // it on the pinned entry, so an intentional stop of that entry cancels |
| // the observation before anything is stopped (spec §6.4). |
| @@ -99,6 +100,7 @@ func (a *Service) swapHold(ctx context.Context, at Attempt, newest func(string) |
| newest: newest, |
| woke: make(chan holdWoke, 1), |
| } |
| + h.statelessPrepared, _ = ctx.Value(statelessPreparedKey{}).(*statelessPreparation) |
| if at.Canary { |
| h.settleCancel = scancel |
| } |
| @@ -353,7 +355,7 @@ func (a *Service) holdResult(sctx context.Context, at Attempt, newest func(strin |
| func (a *Service) holdWakeLocked(e *actEntry, h *holdSpec) (*actEntry, error) { |
| pin := h.gen |
| next := &actEntry{host: e.host, route: e.route, state: actWaking, ready: make(chan struct{}), pin: &pin, |
| - settleCancel: h.settleCancel} |
| + settleCancel: h.settleCancel, statelessPrepared: h.statelessPrepared} |
| // The same gate here, at the stop's end, and again at the pinned wake's |
| // exec (spec §6.3, §7.4): the lease and the restore come between. |
| next.pinCheck = func() error { return a.attemptCurrentLocked(next, h.at, h.newest, true) } |
diff --git a/internal/activator/wakeplan.go b/internal/activator/wakeplan.go| index fda908b15..dd76b0fc6 100644 |
| --- a/internal/activator/wakeplan.go |
| +++ b/internal/activator/wakeplan.go |
| @@ -21,6 +21,7 @@ import ( |
| // WakePlan is the scheduler's answer to "which build does this wake boot" |
| // (spec §8.2). The zero plan keeps today's wake. |
| type WakePlan struct { |
| + Declarations []release.RuntimeSelection |
| // Boot is the artifact to exec, by its immutable staged path. |
| Boot release.StagedIdentity |
| Mode SwapMode |
diff --git a/internal/host/rollout.go b/internal/host/rollout.go| index 6ae5ee5b6..9fa34a72e 100644 |
| --- a/internal/host/rollout.go |
| +++ b/internal/host/rollout.go |
| @@ -6,7 +6,6 @@ import ( |
| "log" |
| "os" |
| "path/filepath" |
| - "runtime" |
| "sync" |
| "sync/atomic" |
| "time" |
| @@ -377,11 +376,15 @@ func armRollout(ctx context.Context, getenv func(string) string, svc *activator. |
| refuseManaged(svc, rolloutBookUnreadable) |
| return nil |
| } |
| - platform := runtime.GOOS + "-" + runtime.GOARCH |
| + svc.ReplicaDeployID = book.ReplicaDeployID |
| m := rollout.NewManager(ctx, rollout.Deps{ |
| Act: svc, Routes: reg, Book: book, OptDir: release.OptDir(), DataDir: dd, Box: BoxLabel(), |
| Demand: func(ctx context.Context, acct, app, ver, path string) (string, string, error) { |
| - return release.CanonicalDemand(ctx, store, acct, app, ver, platform, path) |
| + selection, err := release.CanonicalRuntimeSelection(ctx, store, acct, app, ver, path) |
| + return selection.Identity.Demand, selection.Identity.Digest, err |
| + }, |
| + Selection: func(ctx context.Context, rt registry.Route, ver, path string) (release.RuntimeSelection, error) { |
| + return release.CanonicalRuntimeSelection(ctx, store, release.RouteAccount(rt), rt.App, ver, path) |
| }, |
| Policy: rolloutPolicy(store), |
| // The raw signed read (PF4): only its Version and PromotedAt are |
diff --git a/internal/host/rolloutdeclined.go b/internal/host/rolloutdeclined.go| index 0938e0807..b71c01bbf 100644 |
| --- a/internal/host/rolloutdeclined.go |
| +++ b/internal/host/rolloutdeclined.go |
| @@ -86,6 +86,7 @@ func honourDeclinedFailures(svc *activator.Service, reg *registry.Registry, dd, |
| refuseManaged(svc, rolloutBookUnreadable) |
| return |
| } |
| + svc.ReplicaDeployID = book.ReplicaDeployID |
| if n := book.Reconciled(); n > 0 { |
| // An armed run's executions left unrecorded (Task 5): their records |
| // are honoured below whether or not the save below lands, since the |
diff --git a/internal/host/stagerelay.go b/internal/host/stagerelay.go| index 277b43e70..991676b38 100644 |
| --- a/internal/host/stagerelay.go |
| +++ b/internal/host/stagerelay.go |
| @@ -217,6 +217,11 @@ func armStageRelay(ctx context.Context, reg *registry.Registry, dd string) { |
| func armRolloutAndRelay(ctx context.Context, getenv func(string) string, svc *activator.Service, reg *registry.Registry, dd string, |
| sink activator.EventSink, openStore func(context.Context) (release.Store, error)) *rollout.Manager { |
| m := armRollout(ctx, getenv, svc, reg, dd, sink, openStore) |
| + if svc != nil && svc.ReplicaDeployID == nil && !rollout.Enabled(getenv) { |
| + if book, err := rollout.OpenFailureBook(dd); err == nil { |
| + svc.ReplicaDeployID = book.ReplicaDeployID |
| + } |
| + } |
| if m != nil { |
| armStageRelay(ctx, reg, dd) |
| } |
diff --git a/internal/host/stateless_runtime_test.go b/internal/host/stateless_runtime_test.go| new file mode 100644 |
| index 000000000..7a424217b |
| --- /dev/null |
| +++ b/internal/host/stateless_runtime_test.go |
| @@ -0,0 +1,40 @@ |
| +package host |
| + |
| +import ( |
| + "context" |
| + "path/filepath" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/activator" |
| + "github.com/carlosframework/platform/internal/registry" |
| + "github.com/carlosframework/platform/internal/replicas" |
| +) |
| + |
| +func TestStatelessRuntimeUsesDurableFailureBook(t *testing.T) { |
| + dir := t.TempDir() |
| + reg, err := registry.Open(filepath.Join(dir, "registry.db")) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer reg.Close() |
| + id := replicas.Identity{Account: "bab", App: "shop", Host: "shop.example.com", Incarnation: "lifetime"} |
| + target := replicas.Build{Version: "v1", Digest: "manifest"} |
| + var first string |
| + for restart := 0; restart < 2; restart++ { |
| + svc := activator.New(activator.Config{}, &activator.ExecBackend{}, reg) |
| + armRolloutAndRelay(context.Background(), func(string) string { return "" }, svc, reg, dir, nil, nil) |
| + if svc.ReplicaDeployID == nil { |
| + t.Fatal("runtime has no durable deploy ID carrier") |
| + } |
| + got, err := svc.ReplicaDeployID(id, target, nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if restart == 0 { |
| + first = got |
| + } else if got != first { |
| + t.Fatal("restart changed durable release ID") |
| + } |
| + svc.Shutdown(context.Background()) |
| + } |
| +} |
diff --git a/internal/release/runtime_selection.go b/internal/release/runtime_selection.go| new file mode 100644 |
| index 000000000..2e29d5106 |
| --- /dev/null |
| +++ b/internal/release/runtime_selection.go |
| @@ -0,0 +1,48 @@ |
| +package release |
| + |
| +import ( |
| + "context" |
| + "encoding/json" |
| +) |
| + |
| +// RuntimeSelection carries the canonical declaration alongside its verified |
| +// artifact. It belongs to one planning attempt, never a process-wide cache. |
| +type RuntimeSelection struct { |
| + Identity StagedIdentity |
| + Stateless bool |
| + ManifestDigest string |
| +} |
| + |
| +type selectionStore struct { |
| + Store |
| + raw []byte |
| +} |
| + |
| +func (s *selectionStore) Get(ctx context.Context, key string) ([]byte, string, error) { |
| + raw, etag, err := s.Store.Get(ctx, key) |
| + if err == nil { |
| + s.raw = raw |
| + } |
| + return raw, etag, err |
| +} |
| + |
| +// CanonicalRuntimeSelection keeps the swap demand from the same manifest read |
| +// CanonicalStatelessDigest validates. Failed evidence refuses either mode. |
| +func CanonicalRuntimeSelection(ctx context.Context, s Store, acct, app, version, path string) (RuntimeSelection, error) { |
| + digest, err := FileSHA256(path) |
| + if err != nil { |
| + return RuntimeSelection{}, err |
| + } |
| + id := StagedIdentity{Version: version, StagedPath: path, Digest: digest} |
| + capture := &selectionStore{Store: s} |
| + mode, canonical, err := CanonicalStatelessDigest(ctx, capture, acct, app, id) |
| + if err != nil { |
| + return RuntimeSelection{}, err |
| + } |
| + var manifest Manifest |
| + if err := json.Unmarshal(capture.raw, &manifest); err != nil { |
| + return RuntimeSelection{}, err |
| + } |
| + id.Demand = NormalizeDemand(manifest.Swap) |
| + return RuntimeSelection{Identity: id, Stateless: mode, ManifestDigest: canonical}, nil |
| +} |
diff --git a/internal/release/runtime_selection_test.go b/internal/release/runtime_selection_test.go| new file mode 100644 |
| index 000000000..b9c0ec124 |
| --- /dev/null |
| +++ b/internal/release/runtime_selection_test.go |
| @@ -0,0 +1,36 @@ |
| +package release |
| + |
| +import ( |
| + "context" |
| + "os" |
| + "path/filepath" |
| + "testing" |
| +) |
| + |
| +type selectionCountingStore struct { |
| + Store |
| + gets int |
| +} |
| + |
| +func (s *selectionCountingStore) Get(ctx context.Context, key string) ([]byte, string, error) { |
| + s.gets++ |
| + return s.Store.Get(ctx, key) |
| +} |
| +func TestStatelessSelectionSharesCanonicalRead(t *testing.T) { |
| + ctx := context.Background() |
| + store := NewDirStore(t.TempDir()) |
| + data := []byte("executable") |
| + path := filepath.Join(t.TempDir(), "shop") |
| + if err := os.WriteFile(path, data, 0600); err != nil { |
| + t.Fatal(err) |
| + } |
| + m := Manifest{App: "shop", Version: "v1", Kind: "binary", Stateless: true, Swap: DemandHold, Artifacts: map[string]Artifact{adoptPlatform: {File: "shop"}}} |
| + if err := Ship(ctx, store, "bab", m, map[string][]byte{adoptPlatform: data}); err != nil { |
| + t.Fatal(err) |
| + } |
| + counted := &selectionCountingStore{Store: store} |
| + got, err := CanonicalRuntimeSelection(ctx, counted, "bab", "shop", "v1", path) |
| + if err != nil || !got.Stateless || got.ManifestDigest == "" || got.Identity.Demand != DemandHold || counted.gets != 1 { |
| + t.Fatalf("selection=%+v gets=%d err=%v", got, counted.gets, err) |
| + } |
| +} |
diff --git a/internal/rollout/manager.go b/internal/rollout/manager.go| index 1721d7fb2..3995432e8 100644 |
| --- a/internal/rollout/manager.go |
| +++ b/internal/rollout/manager.go |
| @@ -59,7 +59,8 @@ type Deps struct { |
| Box string |
| // Demand is release.CanonicalDemand bound to the deploy store and this |
| // box's platform (spec §4.2). |
| - Demand func(ctx context.Context, acct, app, version, stagedPath string) (demand, digest string, err error) |
| + Demand func(ctx context.Context, acct, app, version, stagedPath string) (demand, digest string, err error) |
| + Selection func(context.Context, registry.Route, string, string) (release.RuntimeSelection, error) |
| // Policy reads the app's default config bundle (spec §4.1). |
| Policy func(ctx context.Context, acct, app string) (*appconfig.Manifest, error) |
| // Pointer reads a channel's signed pointer, verified (release.Channel). |
| @@ -1320,6 +1321,16 @@ func (m *Manager) resolve(ctx context.Context, d dispatch, rt registry.Route) (a |
| a.Mode = RecoveryMode(policy, rt.GoodDemand) |
| return a, nil |
| } |
| + if m.d.Selection != nil { |
| + selection, err := m.d.Selection(ctx, rt, d.ID.Build, path) |
| + if err != nil { |
| + return a, err |
| + } |
| + a.Declaration = &selection |
| + a.StagedPath, a.Digest, a.Demand = selection.Identity.StagedPath, selection.Identity.Digest, selection.Identity.Demand |
| + a.Mode = AttemptMode(policy, a.Demand) |
| + return a, nil |
| + } |
| demand, digest, err := m.stagedDemand(ctx, rt, d.ID.Build, path) |
| if err != nil { |
| return a, err |
diff --git a/internal/rollout/wakeplan.go b/internal/rollout/wakeplan.go| index bf4544ea1..bd71f2344 100644 |
| --- a/internal/rollout/wakeplan.go |
| +++ b/internal/rollout/wakeplan.go |
| @@ -335,10 +335,19 @@ func (m *Manager) beginWake(rt registry.Route, plan activator.WakePlan, tok uint |
| func (m *Manager) planRow(ctx context.Context, rt registry.Route, tok uint64) activator.WakePlan { |
| m.notePlanning(rt, tok) |
| acct, app := release.RouteAccount(rt), rt.App |
| + var declarations []release.RuntimeSelection |
| in := wakeInputs{Route: rt, OptDir: m.d.OptDir, Policy: m.policies.get(ctx, acct, app), Book: m.d.Book, |
| Demand: func(ver, path string) (string, string, error) { |
| // The same reading an adoption attempt gets (resolve): only a |
| // staging fault is an error, and anything unverifiable is hold. |
| + if m.d.Selection != nil { |
| + selection, err := m.d.Selection(ctx, rt, ver, path) |
| + if err != nil { |
| + return "", "", err |
| + } |
| + declarations = append(declarations, selection) |
| + return selection.Identity.Demand, selection.Identity.Digest, nil |
| + } |
| return m.stagedDemand(ctx, rt, ver, path) |
| }} |
| // The delivered revision now, before the wake's spawn reads the file. |
| @@ -348,6 +357,7 @@ func (m *Manager) planRow(ctx context.Context, rt registry.Route, tok uint64) ac |
| rev := m.track(rt.Host).delivered |
| m.mu.Unlock() |
| plan, err := decideWake(in) |
| + plan.Declarations = declarations |
| plan.ConfigRev = rev |
| if err != nil { |
| m.stagingRefused(rt, rt.Version, err) |