feat: preserve stateless lifecycle across fallback and mode changes
Verify each selected fallback independently and restart database preparation when its mode changes. Keep local process ownership separate from lease health, project the serving mode, and use proven stop-before-start for storage transitions. Add refusal, placement, fallback and byte-preservation witnesses.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
16 files changed,
+378
−13
internal/activator/activator.go+18 −4internal/activator/cmd.go+1 −0internal/activator/rootfs_service.go+1 −1internal/activator/rootfsworkers.go+3 −0internal/activator/stateless.go+82 −1internal/activator/stateless_test.go+172 −0internal/activator/swap.go+42 −0internal/activator/swaphold.go+13 −0internal/activator/wakeplan.go+3 −3internal/ops/hibernate_verify.go+1 −1internal/registry/forward_writers_test.go+1 −0internal/registry/registry.go+11 −1internal/registry/remote_dispatch.go+2 −0internal/registry/remote_test.go+18 −0internal/registry/runtime.go+2 −2internal/rollout/manager.go+8 −0
diff --git a/internal/activator/activator.go b/internal/activator/activator.go| index cbbfcea0a..a6725f6b8 100644 |
| --- a/internal/activator/activator.go |
| +++ b/internal/activator/activator.go |
| @@ -496,7 +496,8 @@ type actEntry struct { |
| // Astra Task 8 R1 1; every rootfs row since Plan 6 Task 7a): made at the |
| // awake transition, closed by finishStop as the entry is forgotten. |
| // Under a.mu. |
| - ownerDone chan struct{} |
| + ownerDone chan struct{} |
| + lifecycleDone chan struct{} |
| // leaseStops counts, per one-shot role (instancectl.RoleCmd, |
| // instancectl.RoleTTY), the lease-loss and lease-short one-shot stops |
| // captured for this entry (stopOneShotsAtLeaseLocked) that have not |
| @@ -2216,6 +2217,7 @@ func (a *Service) wake(e *actEntry) { |
| // bucket Put must not hold e.ready closed any longer than the |
| // wake itself already takes). |
| rt := e.route |
| + rt.Stateless = e.gen.Stateless |
| go a.OnWake(rt) |
| } |
| } |
| @@ -2373,7 +2375,7 @@ type wokenInstance struct { |
| // spawn call and the co-spawn — and those facts (e.worker, e.workerToken) are |
| // fixed at entry construction, before the wake goroutine starts, so reading |
| // them here without a.mu is safe by construction. |
| -func (a *Service) doWake(ctx context.Context, e *actEntry) (*wokenInstance, error) { |
| +func (a *Service) doWakeOnce(ctx context.Context, e *actEntry) (*wokenInstance, error) { |
| if a.cfg.HostWrites.Enabled { |
| // The rootfs admission before the claim path's lease too (INV-3): |
| // an unknown runtime, or a rootfs row this box refuses, takes |
| @@ -2546,7 +2548,12 @@ func (a *Service) doWake(ctx context.Context, e *actEntry) (*wokenInstance, erro |
| // swap's pinned wake (e.pin) already knows; everything else asks the |
| // planner, off a.mu, before the lease: a refused plan takes no lease. |
| var plan WakePlan |
| - if a.Planner != nil && e.pin == nil { |
| + if selected, ok := ctx.Value(wakeFallbackKey{}).(WakePlan); ok { |
| + plan = selected |
| + a.mu.Lock() |
| + e.plan = selected |
| + a.mu.Unlock() |
| + } else if a.Planner != nil && e.pin == nil { |
| // An earlier planned wake's outcome still on its way to the |
| // scheduler is waited for, bounded (final review F3): planning |
| // before its failure record exists would pick the failed build. A |
| @@ -2658,6 +2665,7 @@ func (a *Service) doWake(ctx context.Context, e *actEntry) (*wokenInstance, erro |
| if modeErr != nil { |
| return w, modeErr |
| } |
| + ctx = withStorageMode(ctx, rt, modeID, mode, digest) |
| if mode { |
| ctx = context.WithValue(ctx, statelessWakeKey{}, statelessWakeInput{rt: rt, plan: plan, w: w, digest: digest}) |
| return a.doStatelessWake(ctx, e, modeID) |
| @@ -3206,7 +3214,9 @@ func (a *Service) installProcLocked(e *actEntry, rep Rep, proc Proc, g Gen) { |
| } |
| e.proc = proc |
| e.gen = g |
| - e.route.Stateless = g.Stateless |
| + if e.lifecycleDone == nil { |
| + e.lifecycleDone = make(chan struct{}) |
| + } |
| if e.route.Rootfs() { |
| a.watchResidentLeaseLocked(e) |
| } |
| @@ -5004,6 +5014,10 @@ func (a *Service) finishStop(e *actEntry, outcome *stopObservation) { |
| // A resident's stop, as it is forgotten (Plan 3 Task 8): a crash or a |
| // lost lease backs off; any other stop is restarted on the next tick. |
| a.noteResidentStopLocked(e) |
| + if e.lifecycleDone != nil { |
| + close(e.lifecycleDone) |
| + e.lifecycleDone = nil |
| + } |
| if e.ownerDone != nil { |
| close(e.ownerDone) |
| e.ownerDone = nil |
diff --git a/internal/activator/cmd.go b/internal/activator/cmd.go| index 114c0af25..90ad98c64 100644 |
| --- a/internal/activator/cmd.go |
| +++ b/internal/activator/cmd.go |
| @@ -871,6 +871,7 @@ func (a *Service) RunCmd(ctx context.Context, rt registry.Route, spec CmdSpec) ( |
| // (cmdStartInFlightLocked). |
| h.starting = true |
| h.gen = e.gen |
| + rt.Stateless = h.gen.Stateless |
| h.onProven = func() { |
| // The pin for h.gen is this run's alone once that generation is |
| // no longer the entry's current one. |
diff --git a/internal/activator/rootfs_service.go b/internal/activator/rootfs_service.go| index 720ecd7f9..000f8a15b 100644 |
| --- a/internal/activator/rootfs_service.go |
| +++ b/internal/activator/rootfs_service.go |
| @@ -589,7 +589,7 @@ func (a *Service) residentWaitingLocked(st *residentState) bool { |
| // resident; any other runtime is refused by rootfsRefusalLocked, so it is |
| // reported and never started. |
| func residentShaped(rt registry.Route) bool { |
| - return rt.Kind == "instance" && !rt.Hibernate && rt.Runtime != "" |
| + return rt.Kind == "instance" && !rt.Hibernate && (rt.Runtime != "" || rt.Stateless) |
| } |
| // errResidentNotRunnable refuses a proactive wake whose row, read again |
diff --git a/internal/activator/rootfsworkers.go b/internal/activator/rootfsworkers.go| index 26172e682..08bf97859 100644 |
| --- a/internal/activator/rootfsworkers.go |
| +++ b/internal/activator/rootfsworkers.go |
| @@ -384,6 +384,9 @@ func (a *Service) installRootfsWorkerLocked(e *actEntry, wk *rootfsWorker, proc |
| // for one, so the tick's rep.Err check is its watch (as |
| // watchResidentLeaseLocked decides). The caller holds a.mu. |
| func residentWatchedLocked(e *actEntry) bool { |
| + if e.gen.Stateless { |
| + return e.lifecycleDone != nil |
| + } |
| if e.ownerDone != nil { |
| return true |
| } |
diff --git a/internal/activator/stateless.go b/internal/activator/stateless.go| index 93018d04a..b7c9f18f6 100644 |
| --- a/internal/activator/stateless.go |
| +++ b/internal/activator/stateless.go |
| @@ -69,6 +69,12 @@ func (b *ExecBackend) coordinationAuthority(host string) replicas.Authority { |
| var errStorageDeclaration = errors.New("could not verify this build's storage declaration, so it was not started") |
| func (a *Service) storageMode(ctx context.Context, rt registry.Route, id release.StagedIdentity) (bool, string, error) { |
| + if observed, ok := ctx.Value(storageModeKey{}).(storageModeObservation); ok && observed.id == id && observed.account == rt.Account && observed.app == rt.App { |
| + if err := release.VerifyStaged(id); err != nil { |
| + return false, "", fmt.Errorf("%w: %w", errStorageDeclaration, err) |
| + } |
| + return observed.mode, observed.digest, nil |
| + } |
| if a.StorageDeclaration == nil { |
| if rt.Stateless { |
| return false, "", errStorageDeclaration |
| @@ -114,8 +120,16 @@ func (a *Service) doStatelessWake(ctx context.Context, e *actEntry, id release.S |
| if ctx.Err() != nil || !a.wakeGoesOn(e) { |
| return ErrFenced |
| } |
| + if in.plan.planned() { |
| + if err := a.planGate(ctx, e, nil, in.plan, id.Version)(); err != nil { |
| + return err |
| + } |
| + } |
| if e.pinCheck != nil { |
| - if err := e.pinCheck(); err != nil { |
| + a.mu.Lock() |
| + err := e.pinCheck() |
| + a.mu.Unlock() |
| + if err != nil { |
| return err |
| } |
| } |
| @@ -143,6 +157,10 @@ func (a *Service) doStatelessWake(ctx context.Context, e *actEntry, id release.S |
| if w.proc != nil || w.genUnproven || w.legacyUncertain { |
| a.cleanupFailedWake(ctx, e, rt, w, nil) |
| } |
| + if in.plan.FirstWake && in.plan.Mode == ModeOverlap && in.plan.Fallback != nil && w.bootRan && classifyStep(ctx, err) == stepFailed && !w.genUnproven && !w.legacyUncertain && w.proc != nil && a.failedWakeStopped(rt, w.gen, w.proc, w.launchLayout) { |
| + w.failed, w.failReason = &id, err.Error() |
| + return w, &wakeModeChange{id: *in.plan.Fallback} |
| + } |
| return w, err |
| } |
| if !a.wakeAllowed(rt.Host) || ctx.Err() != nil { |
| @@ -183,3 +201,66 @@ func storageVars(rt registry.Route, env map[string]string) map[string]string { |
| } |
| return env |
| } |
| + |
| +type storageModeKey struct{} |
| +type storageModeObservation struct { |
| + id release.StagedIdentity |
| + account, app string |
| + mode bool |
| + digest string |
| +} |
| + |
| +func withStorageMode(ctx context.Context, rt registry.Route, id release.StagedIdentity, mode bool, digest string) context.Context { |
| + return context.WithValue(ctx, storageModeKey{}, storageModeObservation{id, rt.Account, rt.App, mode, digest}) |
| +} |
| + |
| +type wakeFallbackKey struct{} |
| +type wakeModeChange struct{ id release.StagedIdentity } |
| + |
| +func (*wakeModeChange) Error() string { return errStorageDeclaration.Error() } |
| + |
| +func (a *Service) doWake(ctx context.Context, e *actEntry) (*wokenInstance, error) { |
| + w, err := a.doWakeOnce(ctx, e) |
| + var change *wakeModeChange |
| + if !errors.As(err, &change) || w == nil { |
| + return w, err |
| + } |
| + a.mu.Lock() |
| + unproven := a.unprovenWakes[e.host] != nil || len(a.straySidecars[e.host]) > 0 || a.releaseOps[e.host] != nil |
| + original := e.plan |
| + a.mu.Unlock() |
| + if unproven || (w.proc != nil && !a.failedWakeStopped(e.route, w.gen, w.proc, w.launchLayout)) { |
| + return w, errWakeUnproven |
| + } |
| + if !w.gen.Stateless && w.stopOutcome != nil && !w.stopOutcome.leaseReleased { |
| + return w, errWakeUnproven |
| + } |
| + next := original |
| + next.Boot, next.Fallback, next.FirstWake = change.id, nil, false |
| + nextCtx := context.WithValue(ctx, wakeFallbackKey{}, next) |
| + nextCtx = WithPlannedSelection(nextCtx, PlannedSelection{Build: change.id.Version, InPlaceOf: original.Boot.Version}) |
| + result, nextErr := a.doWakeOnce(nextCtx, e) |
| + a.mu.Lock() |
| + e.plan = original |
| + if w.pend != 0 { |
| + a.dropPendingGenerationLocked(w.pend) |
| + } |
| + a.mu.Unlock() |
| + if result != nil { |
| + result.failed, result.failReason, result.bootRan = w.failed, w.failReason, w.bootRan |
| + result.fellBack = nextErr == nil && w.failed != nil |
| + result.untried = nextErr == nil && w.failed == nil |
| + } |
| + return result, nextErr |
| +} |
| + |
| +func (a *Service) statelessObservation(ctx context.Context, rt registry.Route, id release.StagedIdentity, digest string) (context.Context, error) { |
| + if a.cfg.Record == nil { |
| + return ctx, ErrFenced |
| + } |
| + facts, err := a.cfg.Record(ctx, rt) |
| + if err != nil { |
| + return ctx, err |
| + } |
| + return WithReplicaReleaseObservation(ctx, facts, replicas.Build{Version: id.Version, Digest: digest}, true), nil |
| +} |
diff --git a/internal/activator/stateless_test.go b/internal/activator/stateless_test.go| index 3eb375a7b..132089543 100644 |
| --- a/internal/activator/stateless_test.go |
| +++ b/internal/activator/stateless_test.go |
| @@ -1,11 +1,14 @@ |
| package activator |
| import ( |
| + "bytes" |
| "context" |
| "errors" |
| + "fmt" |
| "github.com/carlosframework/platform/internal/instancectl" |
| "os" |
| "path/filepath" |
| + "runtime" |
| "slices" |
| "strings" |
| "testing" |
| @@ -255,3 +258,172 @@ func TestStatelessBinaryArgumentsAtExec(t *testing.T) { |
| } |
| }) |
| } |
| + |
| +func TestStatelessFallbackUsesOwnManifest(t *testing.T) { |
| + for _, targetStateless := range []bool{false, true} { |
| + t.Run(fmt.Sprint(targetStateless), func(t *testing.T) { |
| + r := newWakeRelRig(t) |
| + wireStatelessRig(r) |
| + r.b.procs["v2"] = map[string]release.Process{release.ProcessWeb: {Command: []string{"serve"}}} |
| + r.a.StorageDeclaration = func(_ context.Context, _ registry.Route, id release.StagedIdentity) (bool, string, error) { |
| + return (id.Version == "v2") == targetStateless, "canonical-" + id.Version, nil |
| + } |
| + r.a.genHealth = func(_ context.Context, g Gen) error { |
| + if g.Build == "v2" { |
| + return errors.New("unhealthy target") |
| + } |
| + return nil |
| + } |
| + if err := waitErr(t, r.wake(r.a)); err != nil { |
| + t.Fatal(err) |
| + } |
| + r.a.mu.Lock() |
| + g := r.a.entries[r.rt.Host].gen |
| + rep := r.a.entries[r.rt.Host].rep |
| + r.a.mu.Unlock() |
| + if g.Build != "v1" || g.Stateless == targetStateless || (rep == nil) != g.Stateless { |
| + t.Fatalf("fallback=%+v rep=%T", g, rep) |
| + } |
| + if n, restore, _ := r.b.counts(); n != 1 || restore != 1 { |
| + t.Fatalf("acquire=%d restore=%d", n, restore) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestStatelessMovedBeforeSpawn(t *testing.T) { |
| + r := newWakeRelRig(t) |
| + wireStatelessRig(r) |
| + r.b.procs["v2"] = map[string]release.Process{release.ProcessWeb: {Command: []string{"serve"}}} |
| + r.a.BeforeExec = func(context.Context, string, string) error { |
| + rt := r.routes.get(r.rt.Host) |
| + rt.Assigned = "moved" |
| + r.routes.set(rt) |
| + return nil |
| + } |
| + if err := waitErr(t, r.wake(r.a)); err == nil { |
| + t.Fatal("moved route served") |
| + } |
| + if starts := r.b.started(); len(starts) != 0 { |
| + t.Fatalf("started on old box: %+v", starts) |
| + } |
| +} |
| + |
| +func TestStatelessModeTransitionPreservesDatabase(t *testing.T) { |
| + for _, oldMode := range []bool{false, true} { |
| + t.Run(fmt.Sprint(oldMode), func(t *testing.T) { |
| + r := newWakeRelRig(t) |
| + wireStatelessRig(r) |
| + r.a.StorageDeclaration = func(_ context.Context, _ registry.Route, id release.StagedIdentity) (bool, string, error) { |
| + return (id.Version == "v1") == oldMode, "canonical-" + id.Version, nil |
| + } |
| + r.b.procs["v2"] = map[string]release.Process{release.ProcessWeb: {Command: []string{"serve"}}} |
| + r.setPlan(WakePlan{Boot: r.good, Mode: ModeOverlap}) |
| + files := map[string][]byte{} |
| + for _, suffix := range []string{"", "-wal", "-shm", ".assigned", ".first-boot", ".replica"} { |
| + path := r.rt.DBPath + suffix |
| + body := []byte("preserved " + suffix) |
| + if err := os.WriteFile(path, body, 0600); err != nil { |
| + t.Fatal(err) |
| + } |
| + files[path] = body |
| + } |
| + if err := waitErr(t, r.wake(r.a)); err != nil { |
| + t.Fatal(err) |
| + } |
| + r.a.mu.Lock() |
| + old := r.a.entries[r.rt.Host].proc.(*fakeProc) |
| + r.a.mu.Unlock() |
| + blocked := make(chan struct{}) |
| + old.setStopGate(blocked) |
| + defer func() { |
| + select { |
| + case <-blocked: |
| + default: |
| + close(blocked) |
| + } |
| + }() |
| + 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 }) |
| + }() |
| + waitFor(t, "old generation stop", func() bool { return old.stopEntered() > 0 || len(r.b.started()) > 1 }) |
| + if len(r.b.started()) != 1 { |
| + t.Fatal("new storage mode started before old mode stopped") |
| + } |
| + close(blocked) |
| + select { |
| + case got := <-result: |
| + if got.Outcome != SwapDone { |
| + t.Fatalf("swap: %+v", got) |
| + } |
| + case <-time.After(5 * time.Second): |
| + t.Fatal("transition did not finish") |
| + } |
| + r.a.mu.Lock() |
| + g := r.a.entries[r.rt.Host].gen |
| + r.a.mu.Unlock() |
| + if g.Stateless == oldMode || g.Build != "v2" { |
| + t.Fatalf("new mode=%+v", g) |
| + } |
| + for path, want := range files { |
| + got, err := os.ReadFile(path) |
| + if err != nil || !bytes.Equal(got, want) { |
| + t.Fatalf("database/marker changed: %s %v", path, err) |
| + } |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestStatelessModeSourceVerification(t *testing.T) { |
| + for _, bad := range []string{"projection-only", "unreadable", "corrupt-artifact", "canonical-stateful", "canonical-stateless"} { |
| + t.Run(bad, func(t *testing.T) { |
| + r := newWakeRelRig(t) |
| + wireStatelessRig(r) |
| + r.b.procs["v2"] = map[string]release.Process{release.ProcessWeb: {Command: []string{"serve"}}} |
| + store := release.NewDirStore(t.TempDir()) |
| + data, err := os.ReadFile(r.next.StagedPath) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + platform := runtime.GOOS + "-" + runtime.GOARCH |
| + if bad != "projection-only" && bad != "unreadable" { |
| + m := release.Manifest{App: r.rt.App, Version: r.next.Version, Kind: "binary", Stateless: bad != "canonical-stateful", Artifacts: map[string]release.Artifact{platform: {File: "shop"}}} |
| + if err := release.Ship(context.Background(), store, r.rt.Account, m, map[string][]byte{platform: data}); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + if bad == "corrupt-artifact" { |
| + if err := os.WriteFile(r.next.StagedPath, []byte("changed"), 0600); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + r.rt.Stateless = bad != "canonical-stateless" |
| + r.routes.set(r.rt) |
| + r.a.StorageDeclaration = func(ctx context.Context, rt registry.Route, id release.StagedIdentity) (bool, string, error) { |
| + return release.CanonicalStatelessDigest(ctx, store, rt.Account, rt.App, id) |
| + } |
| + err = waitErr(t, r.wake(r.a)) |
| + valid := strings.HasPrefix(bad, "canonical-") |
| + if !valid { |
| + if !errors.Is(err, errStorageDeclaration) { |
| + t.Fatalf("want storage refusal, got %v", err) |
| + } |
| + if n, _, _ := r.b.counts(); n != 0 || len(r.b.started()) != 0 { |
| + t.Fatal("unverified mode reached database/start") |
| + } |
| + return |
| + } |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + r.a.mu.Lock() |
| + g := r.a.entries[r.rt.Host].gen |
| + r.a.mu.Unlock() |
| + if g.Stateless != (bad == "canonical-stateless") { |
| + t.Fatalf("projection overrode canonical declaration: %+v", g) |
| + } |
| + }) |
| + } |
| +} |
diff --git a/internal/activator/swap.go b/internal/activator/swap.go| index 239bcf581..948ee4004 100644 |
| --- a/internal/activator/swap.go |
| +++ b/internal/activator/swap.go |
| @@ -17,6 +17,7 @@ import ( |
| "github.com/carlosframework/platform/internal/hostwrite" |
| "github.com/carlosframework/platform/internal/instancectl" |
| "github.com/carlosframework/platform/internal/registry" |
| + "github.com/carlosframework/platform/internal/release" |
| ) |
| // Cause is why an attempt exists (spec §6.3). |
| @@ -324,6 +325,34 @@ func (a *Service) Swap(ctx context.Context, at Attempt, newest func(host string) |
| if open { |
| return abandon(errReleaseOpen) |
| } |
| + |
| + a.mu.Lock() |
| + current := a.entries[at.Host] |
| + var route registry.Route |
| + var oldStateless bool |
| + if current != nil { |
| + route = current.route |
| + oldStateless = current.gen.Stateless |
| + } |
| + a.mu.Unlock() |
| + id := release.StagedIdentity{Version: at.Build, StagedPath: at.StagedPath, Digest: at.Digest, Demand: at.Demand} |
| + mode, digest, modeErr := a.storageMode(ctx, route, id) |
| + if modeErr != nil { |
| + return abandon(modeErr) |
| + } |
| + ctx = withStorageMode(ctx, route, id, mode, digest) |
| + if mode != oldStateless { |
| + at.Mode = ModeHold |
| + } |
| + if mode && at.Mode == ModeOverlap && !a.foreignRow(at.Host) { |
| + observed, err := a.statelessObservation(ctx, route, id, digest) |
| + if err != nil { |
| + return abandon(err) |
| + } |
| + res := a.swapOverlap(observed, gb, at, newest, nil, false) |
| + res.Mode = ModeOverlap |
| + return res |
| + } |
| switch at.Mode { |
| case ModeOverlap: |
| if a.foreignRow(at.Host) { |
| @@ -415,6 +444,19 @@ func (a *Service) genHealthy(ctx context.Context, rt registry.Route, g Gen) erro |
| // as a claims admission (NoOwnership, the attempt abandoned), and a |
| // claims-on spawn is never sent down the claims-off StartStaged. |
| func (a *Service) spawnSuccessor(ctx context.Context, gb SwapBackend, e *actEntry, rt registry.Route, work hostwrite.Work, g Gen, gate func() error) (Gen, Proc, error) { |
| + if mode, ok := ctx.Value(storageModeKey{}).(storageModeObservation); ok { |
| + g.Stateless = mode.mode |
| + if mode.mode { |
| + previous := gate |
| + gate = func() error { |
| + if err := previous(); err != nil { |
| + return err |
| + } |
| + return a.statelessCurrent(ctx, rt) |
| + } |
| + } |
| + } |
| + |
| if hostwrite.Legacy(work) { |
| if err := gate(); err != nil { |
| return g, nil, err |
diff --git a/internal/activator/swaphold.go b/internal/activator/swaphold.go| index 5ae109bab..57217839f 100644 |
| --- a/internal/activator/swaphold.go |
| +++ b/internal/activator/swaphold.go |
| @@ -513,6 +513,19 @@ func (a *Service) startPinnedAt(ctx context.Context, rt registry.Route, pin Gen, |
| if err := verifyStaged(pin.StagedPath, pin.Digest); err != nil { |
| return nil, "", pin, fmt.Errorf("staging: %w", err) |
| } |
| + id, idErr := stagedIdentityOf(pin) |
| + if idErr != nil { |
| + return nil, "", pin, idErr |
| + } |
| + mode, digest, modeErr := a.storageMode(ctx, rt, id) |
| + if modeErr != nil { |
| + return nil, "", pin, modeErr |
| + } |
| + if mode != rt.Stateless { |
| + return nil, "", pin, &wakeModeChange{id: id} |
| + } |
| + pin.Stateless = mode |
| + ctx = withStorageMode(ctx, rt, id, mode, digest) |
| pin.N = nextGen() |
| if pin.Tier == (cgroup.Limit{}) { |
| pin.Tier = gb.GenTier(rt, pin.Build) // the pinned artifact's own tier (spec §7.3) |
diff --git a/internal/activator/wakeplan.go b/internal/activator/wakeplan.go| index 343e5a5c3..fda908b15 100644 |
| --- a/internal/activator/wakeplan.go |
| +++ b/internal/activator/wakeplan.go |
| @@ -475,7 +475,7 @@ func (a *Service) spawnPlanned(ctx context.Context, e *actEntry, rt registry.Rou |
| // shutdown, a lost lease or entry since B's exec). B's failure is |
| // still recorded (w.failed); its process, proven gone, is what |
| // doWake's cleanup proves once more. |
| - return proc, ver, g, fmt.Errorf("%w; the good build was not started: %v", err, ferr) |
| + return proc, ver, g, fmt.Errorf("%w; the good build was not started: %w", err, ferr) |
| } |
| return fproc, fver, fg, fmt.Errorf("%w; the good build %s failed too: %w", err, good.Version, ferr) |
| } |
| @@ -516,7 +516,7 @@ func (a *Service) spawnPlannedRelease(ctx context.Context, e *actEntry, rt regis |
| fproc, fver, fg, ferr := a.startPinnedAt(fctx, rt, genOf(good), a.planGate(ctx, e, lease, plan, good.Version), time.Time{}) |
| if ferr != nil { |
| if fproc == nil && !genStartUnproven(ferr) { |
| - return nil, "", Gen{}, fmt.Errorf("%w; the good build was not started: %v", err, ferr) |
| + return nil, "", Gen{}, fmt.Errorf("%w; the good build was not started: %w", err, ferr) |
| } |
| return fproc, fver, fg, fmt.Errorf("%w; the good build %s failed too: %w", err, good.Version, ferr) |
| } |
| @@ -616,7 +616,7 @@ func (a *Service) spawnPlannedWorker(ctx context.Context, e *actEntry, rt regist |
| if fproc == nil { |
| // Nothing of the good build ran: B's failure is still recorded, |
| // and B's child, proven gone, is what doWake's cleanup proves. |
| - return proc, id, fmt.Errorf("%w; the good build was not started: %v", err, ferr) |
| + return proc, id, fmt.Errorf("%w; the good build was not started: %w", err, ferr) |
| } |
| return fproc, fid, fmt.Errorf("%w; the good build %s failed too: %w", err, plan.Fallback.Version, ferr) |
| } |
diff --git a/internal/ops/hibernate_verify.go b/internal/ops/hibernate_verify.go| index a1078e3db..4f649c022 100644 |
| --- a/internal/ops/hibernate_verify.go |
| +++ b/internal/ops/hibernate_verify.go |
| @@ -57,7 +57,7 @@ func HibernatingHosts(reg *registry.Registry) ([]HibernatingRoute, error) { |
| } |
| // FleetDBsWithSkipped's exclusion, the same expression: a resident |
| // rootfs row is drilled here, never by box-wide litestream. |
| - if r.ActivatorLeased() && r.DBPath != "" { |
| + if !r.Stateless && r.ActivatorLeased() && r.DBPath != "" { |
| hosts = append(hosts, HibernatingRoute{Host: r.Host, DBPath: r.DBPath}) |
| } |
| } |
diff --git a/internal/registry/forward_writers_test.go b/internal/registry/forward_writers_test.go| index 461657e3f..458cf0911 100644 |
| --- a/internal/registry/forward_writers_test.go |
| +++ b/internal/registry/forward_writers_test.go |
| @@ -379,6 +379,7 @@ func routeWriterCases() []writerCase { |
| errCase("SetGoodBuild", true, nil, func(r *Registry, h string) error { |
| return r.SetGoodBuild(h, GoodBuild{}, GoodBuild{Version: "v1", Digest: "aa", Demand: "swap"}) |
| }), |
| + errCase("SetServingMode", false, nil, func(r *Registry, h string) error { return r.SetServingMode(h, "v1", true) }), |
| errCase("SetServingVersion", false, nil, func(r *Registry, h string) error { return r.SetServingVersion(h, "v1") }), |
| errCase("SetHibernate", true, nil, func(r *Registry, h string) error { return r.SetHibernate(h, true) }), |
| {name: "SetHibernate off", encloses: []string{"SetHibernate"}, refuses: true, |
diff --git a/internal/registry/registry.go b/internal/registry/registry.go| index 22f8848eb..1d501ed07 100644 |
| --- a/internal/registry/registry.go |
| +++ b/internal/registry/registry.go |
| @@ -3110,6 +3110,16 @@ WHERE host = ? AND kind <> 'forward' AND good_version = ? AND good_digest = ? AN |
| return fmt.Errorf("%w: %s now holds %+v", ErrGoodBuildChanged, host, cur) |
| } |
| +// SetServingMode projects the actual serving generation in one update. |
| +func (r *Registry) SetServingMode(host, version string, stateless bool) error { |
| + if r.remote != nil { |
| + _, err := r.remote.call(nil, remoteRequest{Method: "SetServingMode", Host: host, Ver: version, On: stateless}, nil) |
| + return err |
| + } |
| + _, err := r.sql.Exec(`UPDATE routes SET serving_version = ?, stateless = ? WHERE host = ? AND kind <> 'forward'`, version, b2iDB(stateless), host) |
| + return err |
| +} |
| + |
| // SetServingVersion records the build of the generation that just became |
| // current (spec §3), as a narrow UPDATE. |
| func (r *Registry) SetServingVersion(host, ver string) error { |
| @@ -3271,7 +3281,7 @@ func (r *Registry) ResidentInstances() ([]Route, error) { |
| } |
| rows, err := r.sql.Query(`SELECT ` + routeCols + ` FROM routes |
| -WHERE kind = 'instance' AND runtime = '` + RuntimeRootfsV1 + `' AND hibernate = 0 AND (status = '' OR status = 'ready') |
| +WHERE kind = 'instance' AND (runtime = '` + RuntimeRootfsV1 + `' OR (runtime = '' AND stateless = 1)) AND hibernate = 0 AND (status = '' OR status = 'ready') |
| AND hold_op = '' AND import_gate = '' AND socket != '' AND backing = '' AND tunnel = '' ORDER BY host`) |
| if err != nil { |
| return nil, err |
diff --git a/internal/registry/remote_dispatch.go b/internal/registry/remote_dispatch.go| index 3c2d4725c..3c076b8f7 100644 |
| --- a/internal/registry/remote_dispatch.go |
| +++ b/internal/registry/remote_dispatch.go |
| @@ -320,6 +320,8 @@ func (r *Registry) dispatchRemote(ctx context.Context, q remoteRequest, callback |
| err = r.SetVersions(q.Host, q.Ver, q.Prev) |
| case "SetGoodBuild": |
| err = r.SetGoodBuild(q.Host, q.ExpectGood, q.NextGood) |
| + case "SetServingMode": |
| + err = r.SetServingMode(q.Host, q.Ver, q.On) |
| case "SetServingVersion": |
| err = r.SetServingVersion(q.Host, q.Ver) |
| case "SetHibernate": |
diff --git a/internal/registry/remote_test.go b/internal/registry/remote_test.go| index 1adc80165..479875c50 100644 |
| --- a/internal/registry/remote_test.go |
| +++ b/internal/registry/remote_test.go |
| @@ -272,3 +272,21 @@ func TestRemoteCancellationDuringReconnect(t *testing.T) { |
| t.Fatalf("call after startup cancellation: %v", err) |
| } |
| } |
| + |
| +func TestStatelessServingProjectionRemote(t *testing.T) { |
| + r, _ := remoteTestRegistry(t) |
| + if err := r.Upsert(Route{Host: "shop.example.com", Kind: "instance", Socket: "/run/shop.sock"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := r.SetServingMode("shop.example.com", "v2", true); err != nil { |
| + t.Fatal(err) |
| + } |
| + got, err := r.Get("shop.example.com") |
| + if err != nil || !got.Stateless || got.ServingVersion != "v2" || !got.Resident() { |
| + t.Fatalf("projection=%+v %v", got, err) |
| + } |
| + rows, err := r.ResidentInstances() |
| + if err != nil || len(rows) != 1 { |
| + t.Fatalf("residents=%v %v", rows, err) |
| + } |
| +} |
diff --git a/internal/registry/runtime.go b/internal/registry/runtime.go| index 825745476..67dbc6285 100644 |
| --- a/internal/registry/runtime.go |
| +++ b/internal/registry/runtime.go |
| @@ -28,13 +28,13 @@ var ErrRootfsRow = errors.New("registry: the host's row runs a rootfs image, whi |
| // (and refuses to start it, Plan 2), so nothing else ever runs it. A sidecar |
| // is never owned here; its worker path is separate. |
| func (t Route) ActivatorOwned() bool { |
| - return t.Kind == "instance" && (t.Hibernate || t.Runtime != "") |
| + return t.Kind == "instance" && (t.Hibernate || t.Runtime != "" || t.Stateless) |
| } |
| // Resident reports whether t is an always-awake rootfs instance: the |
| // activator owns it and keeps it running without waiting for a request. |
| func (t Route) Resident() bool { |
| - return t.Kind == "instance" && t.Rootfs() && !t.Hibernate |
| + return t.Kind == "instance" && (t.Rootfs() || (t.Runtime == "" && t.Stateless)) && !t.Hibernate |
| } |
| // ActivatorLeased reports whether the activator holds t's lease and |
diff --git a/internal/rollout/manager.go b/internal/rollout/manager.go| index f80314788..1721d7fb2 100644 |
| --- a/internal/rollout/manager.go |
| +++ b/internal/rollout/manager.go |
| @@ -1411,6 +1411,14 @@ func (m *Manager) setServingLocked(host string, g activator.Gen) { |
| if g.Worker { |
| return |
| } |
| + if projection, ok := m.d.Routes.(interface { |
| + SetServingMode(string, string, bool) error |
| + }); ok { |
| + if err := projection.SetServingMode(host, g.Build, g.Stateless); err != nil { |
| + m.d.Logf("rollout: %s: record serving build %s: %v", host, g.Build, err) |
| + } |
| + return |
| + } |
| if err := m.d.Routes.SetServingVersion(host, g.Build); err != nil { |
| m.d.Logf("rollout: %s: record serving build %s: %v", host, g.Build, err) |
| } |