activator: StartStaged starts rootfs generations; the rootfs health bound in swaps and planned wakes; CmdRootfs types root's refusals; KillHost, EmptyHost and HasSidecar refuse rootfs rows
Rootfs apps Plan 3 Task N1. StartStaged asks the runtime first: a rootfs row takes startStagedRootfs (full staged identity, rootfsGates, digest, spawnRootfs; returns before health), and a row with a runtime this agent does not know is refused, as it now is by Start, StartGen, StartGenExe, StartSidecar and both binary launch funnels (nestedGate, spawnInCgroupGated). Every rootfs Gen carries its image's identity (stagedIdentityOf). genHealthy and verdictFloorGate take healthBoundFor.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
13 files changed,
+1,072
−40
internal/activator/backend_exec.go+54 −4internal/activator/binary_golden_test.go+2 −2internal/activator/genbackend.go+50 −2internal/activator/identity.go+16 −0internal/activator/rootfs.go+60 −13internal/activator/rootfs_invariants_test.go+60 −3internal/activator/rootfs_service.go+32 −0internal/activator/rootfs_staged_test.go+771 −0internal/activator/rootfs_test.go+3 −2internal/activator/swap.go+6 −4internal/activator/swaphold.go+3 −3internal/activator/wakeplan.go+6 −5internal/instancectl/client.go+9 −2
diff --git a/internal/activator/backend_exec.go b/internal/activator/backend_exec.go| index 50b8edef3..bb8efb6e3 100644 |
| --- a/internal/activator/backend_exec.go |
| +++ b/internal/activator/backend_exec.go |
| @@ -480,6 +480,10 @@ func (b *ExecBackend) spawnInCgroupGated(rt registry.Route, tier cgroup.Limit, b |
| // runs as a flat child (spec anti-goal "a rootfs app running flat"). |
| return nil, nil, refuseRootfs(fmt.Errorf("%s: %s", rt.Host, rootfsNotNestedText)) |
| } |
| + // Nor does a row with a runtime this agent does not know (Plan 3 N1). |
| + if err := refuseUnknownRuntime(rt); err != nil { |
| + return nil, nil, err |
| + } |
| if tier == (cgroup.Limit{}) { |
| tier = b.tier(rt) |
| } |
| @@ -1873,7 +1877,11 @@ func (b *ExecBackend) Start(ctx context.Context, rt registry.Route) (Proc, strin |
| if err := b.rootfsGates(rt); err != nil { |
| return nil, "", err |
| } |
| - return b.startRootfsFromLive(ctx, rt, rt.Socket, 0) |
| + proc, id, err := b.startRootfsFromLive(ctx, rt, rt.Socket, 0) |
| + return proc, id.Version, err |
| + } |
| + if err := refuseUnknownRuntime(rt); err != nil { |
| + return nil, "", err |
| } |
| p := b.pathsFor(rt) |
| switch p.layout { |
| @@ -1905,6 +1913,9 @@ func (b *ExecBackend) StartGen(ctx context.Context, rt registry.Route, n uint64, |
| if rt.Runtime == registry.RuntimeRootfsV1 { |
| return b.startRootfsGen(ctx, rt, n, perGen, nil) |
| } |
| + if err := refuseUnknownRuntime(rt); err != nil { |
| + return nil, Gen{}, err |
| + } |
| return b.startGenFrom(ctx, rt, n, perGen, nil, cgroup.Limit{}) |
| } |
| @@ -1926,6 +1937,9 @@ func (b *ExecBackend) StartGenExe(ctx context.Context, rt registry.Route, n uint |
| if rt.Runtime == registry.RuntimeRootfsV1 { |
| return b.startRootfsGen(ctx, rt, n, perGen, &id) |
| } |
| + if err := refuseUnknownRuntime(rt); err != nil { |
| + return nil, Gen{}, err |
| + } |
| if err := release.VerifyStaged(id); err != nil { |
| return nil, Gen{}, fmt.Errorf("%s: not started on %s: %w", rt.Host, id.Version, err) |
| } |
| @@ -2736,12 +2750,34 @@ func (b *ExecBackend) killFailedGen(ctx context.Context, rt registry.Route, gen |
| // refuses before anything else runs. Shared so seedUnit and |
| // startSidecarUnit gate on NestedOK exactly like startUnit does. Every |
| // caller is a binary unit's start, so a rootfs row is refused here: its |
| -// processes start only through the rootfs verbs (rootfs.go). |
| +// processes start only through the rootfs verbs (rootfs.go). So is a row |
| +// with a runtime this agent does not know (Plan 3 N1). |
| func (b *ExecBackend) nestedGate(rt registry.Route) error { |
| + if err := refuseRuntimeBinaryLaunch(rt); err != nil { |
| + return err |
| + } |
| + return b.nestedReady(rt) |
| +} |
| + |
| +// refuseUnknownRuntime refuses a row whose runtime this agent does not |
| +// know (errRuntimeUnknown), marked as a refusal; nil for a binary or a |
| +// rootfs-v1 row. A start path asks it before its layout switch, so no |
| +// future runtime value degrades to a binary launch. |
| +func refuseUnknownRuntime(rt registry.Route) error { |
| + if registry.ValidRuntime(rt.Runtime) { |
| + return nil |
| + } |
| + return refuseRootfs(fmt.Errorf("%s: runtime %q: %w", rt.Host, rt.Runtime, errRuntimeUnknown)) |
| +} |
| + |
| +// refuseRuntimeBinaryLaunch is a binary launch path's runtime-first check: |
| +// nil for a binary row; a rootfs-v1 row is errRootfsBinaryLaunch and any |
| +// other runtime errRuntimeUnknown, both marked as refusals. |
| +func refuseRuntimeBinaryLaunch(rt registry.Route) error { |
| if rt.Runtime == registry.RuntimeRootfsV1 { |
| return refuseRootfs(fmt.Errorf("%s: %w", rt.Host, errRootfsBinaryLaunch)) |
| } |
| - return b.nestedReady(rt) |
| + return refuseUnknownRuntime(rt) |
| } |
| // nestedReady is nestedGate's box half, which a rootfs start asks too |
| @@ -2982,13 +3018,27 @@ func mergeEnv(base, overlay map[string]string) map[string]string { |
| // to mint a native row for a host this method already reports true for. |
| // Retiring this file path is a follow-up gated on Eleven's own migration to |
| // native sidecar records. |
| +// |
| +// A row with a runtime has none, whatever its instance directory holds |
| +// (Plan 3 N1, pf 1.14): the file sits where the tenant writes, and the |
| +// co-spawn is a binary launch StartSidecar refuses for such a row, so a |
| +// planted file would only have the sweep retry a refusal for ever. |
| func (b *ExecBackend) HasSidecar(rt registry.Route) bool { |
| + if rt.Runtime != "" { |
| + return false |
| + } |
| _, err := os.Stat(b.pathsFor(rt).sidecarEnv) |
| return err == nil |
| } |
| -// StartSidecar dispatches by layout, the same way Start does. |
| +// StartSidecar dispatches by layout, the same way Start does. The runtime |
| +// first: the co-spawn execs the live link as a binary, so a rootfs row and |
| +// a row with a runtime this agent does not know are refused on every |
| +// layout, before anything is prepared or root is asked. |
| func (b *ExecBackend) StartSidecar(ctx context.Context, rt registry.Route) (Proc, error) { |
| + if err := refuseRuntimeBinaryLaunch(rt); err != nil { |
| + return nil, err |
| + } |
| p := b.pathsFor(rt) |
| switch p.layout { |
| case LayoutNested: |
diff --git a/internal/activator/binary_golden_test.go b/internal/activator/binary_golden_test.go| index 87e876a0a..428c5a943 100644 |
| --- a/internal/activator/binary_golden_test.go |
| +++ b/internal/activator/binary_golden_test.go |
| @@ -293,7 +293,7 @@ func TestGenHealthyBoundBinary(t *testing.T) { |
| }{{"answers before the bound", 50 * time.Millisecond}, {"answers after the bound", 2500 * time.Millisecond}} { |
| sock := filepath.Join(dir, fmt.Sprintf("g%d.sock", c.delay.Milliseconds())) |
| serveAfter(t, sock, c.delay) |
| - err := a.genHealthy(context.Background(), Gen{N: 7, Socket: sock, Control: sock + ".control"}) |
| + err := a.genHealthy(context.Background(), registry.Route{Host: "blog.example.com", Kind: "instance"}, Gen{N: 7, Socket: sock, Control: sock + ".control"}) |
| verdict := "healthy" |
| if err != nil { |
| verdict = "not healthy" |
| @@ -301,7 +301,7 @@ func TestGenHealthyBoundBinary(t *testing.T) { |
| fmt.Fprintf(&out, "genHealthy, %s: %s\n", c.name, verdict) |
| } |
| var left time.Duration |
| - gate := verdictFloorGate(func() error { return nil }, time.Now().Add(time.Hour), &left) |
| + gate := verdictFloorGate(func() error { return nil }, time.Now().Add(time.Hour), healthBoundFor(registry.Route{Host: "blog.example.com", Kind: "instance"}), &left) |
| err = gate() |
| window := "bounded by spawnHealthTimeout" |
| if err != nil || left > spawnHealthTimeout || left < spawnHealthTimeout-100*time.Millisecond { |
diff --git a/internal/activator/genbackend.go b/internal/activator/genbackend.go| index 6c13e7888..61993e6f5 100644 |
| --- a/internal/activator/genbackend.go |
| +++ b/internal/activator/genbackend.go |
| @@ -227,7 +227,19 @@ func (a *Service) SetLiveExecGate(gate func(rt registry.Route, build string) err |
| // (a cold, pinned or hold start), and Swap never overlaps it. A flat row |
| // whose generation path would not fit a unix socket path is handled the |
| // same way (genLayout, rev 19 rule 4). |
| +// |
| +// The runtime first, before the tier or the layout (Plan 3 N1, Task 10 |
| +// finding F2): a rootfs row starts its image through root |
| +// (startStagedRootfs, which refuses any layout but nested), and a row with |
| +// a runtime this agent does not know is refused. Neither ever reaches a |
| +// binary arm, where a flat row would exec its staged path as a program. |
| func (b *ExecBackend) StartStaged(ctx context.Context, rt registry.Route, g Gen) (Gen, Proc, error) { |
| + if rt.Runtime == registry.RuntimeRootfsV1 { |
| + return b.startStagedRootfs(ctx, rt, g) |
| + } |
| + if err := refuseUnknownRuntime(rt); err != nil { |
| + return g, nil, err |
| + } |
| if g.Tier == (cgroup.Limit{}) { |
| g.Tier = b.GenTier(rt, g.Build) |
| } |
| @@ -568,6 +580,23 @@ func (b *SplitBackend) GenPaths(rt registry.Route, n uint64) (Gen, bool) { |
| return Gen{}, false |
| } |
| +// errRootfsKillHost is KillHost's and EmptyHost's answer for a row with a |
| +// runtime (Plan 3 N1, pf 2.2). Both read carlos-exec@<host>, where an |
| +// absent cgroup counts as proven, and a rootfs process never lives there: |
| +// its proof is its own unit's cgroup (unitEvents). So the verbs fail |
| +// closed, before any cgroup is opened, and a path that reaches them with |
| +// such a row never proves a running process gone. |
| +var errRootfsKillHost = errors.New("activator: a rootfs row has no host cgroup to kill or prove empty") |
| + |
| +// refuseHostCgroupProof is errRootfsKillHost for a row with a runtime |
| +// (rootfs-v1, or one this agent does not know); nil for a binary row. |
| +func refuseHostCgroupProof(rt registry.Route) error { |
| + if rt.Runtime == "" { |
| + return nil |
| + } |
| + return fmt.Errorf("%s: runtime %q: %w", rt.Host, rt.Runtime, errRootfsKillHost) |
| +} |
| + |
| // hostCleanup is what KillHost and EmptyHost do with the host cgroup they |
| // open: *cgroup.Cleanup's population read, kill and close. |
| type hostCleanup interface { |
| @@ -606,7 +635,12 @@ func (b *ExecBackend) openHostCgroup(host string) (hostCleanup, error) { |
| // that took cgroup.kill is never spawned into again (amadan #272, |
| // internal/cgroup/recreate_linux.go). That |
| // holds with the flag off too, where only Task 15's F6 path reaches here. |
| +// |
| +// A row with a runtime is refused first (errRootfsKillHost). |
| func (b *ExecBackend) KillHost(ctx context.Context, rt registry.Route) error { |
| + if err := refuseHostCgroupProof(rt); err != nil { |
| + return err |
| + } |
| c, err := b.openHostCgroup(rt.Host) |
| if errors.Is(err, cgroup.ErrNoCgroup) { |
| return nil |
| @@ -622,8 +656,13 @@ func (b *ExecBackend) KillHost(ctx context.Context, rt registry.Route) error { |
| } |
| // KillHost dispatches like Start: a unit-backed row's one process tree is |
| -// systemd's, so there is no host cgroup of ours to kill. |
| +// systemd's, so there is no host cgroup of ours to kill. A row with a |
| +// runtime is refused first, whatever its backing and whatever the exec |
| +// side implements (errRootfsKillHost). |
| func (b *SplitBackend) KillHost(ctx context.Context, rt registry.Route) error { |
| + if err := refuseHostCgroupProof(rt); err != nil { |
| + return err |
| + } |
| if rt.Backing == registry.BackingUnit { |
| return nil // systemd owns the unit tenant's tree |
| } |
| @@ -643,7 +682,12 @@ func (b *SplitBackend) KillHost(ctx context.Context, rt registry.Route) error { |
| // cgroup.kill (cgroup.ErrNoKill, a kernel before 5.14): nothing could kill |
| // a descendant there, and reading it as an unproven kill would hold every |
| // flag-off stop's lease on such a box. |
| +// |
| +// A row with a runtime is refused first (errRootfsKillHost). |
| func (b *ExecBackend) EmptyHost(ctx context.Context, rt registry.Route) error { |
| + if err := refuseHostCgroupProof(rt); err != nil { |
| + return err |
| + } |
| c, err := b.openHostCgroup(rt.Host) |
| if errors.Is(err, cgroup.ErrNoCgroup) { |
| return nil |
| @@ -666,8 +710,12 @@ func (b *ExecBackend) EmptyHost(ctx context.Context, rt registry.Route) error { |
| } |
| // EmptyHost dispatches like KillHost: a unit-backed row has no host cgroup |
| -// of ours. An exec side without the read-first proof gets KillHost. |
| +// of ours. An exec side without the read-first proof gets KillHost. A row |
| +// with a runtime is refused first (errRootfsKillHost). |
| func (b *SplitBackend) EmptyHost(ctx context.Context, rt registry.Route) error { |
| + if err := refuseHostCgroupProof(rt); err != nil { |
| + return err |
| + } |
| if rt.Backing == registry.BackingUnit { |
| return nil // systemd owns the unit tenant's tree |
| } |
diff --git a/internal/activator/identity.go b/internal/activator/identity.go| index 4ca81c55a..af930a50b 100644 |
| --- a/internal/activator/identity.go |
| +++ b/internal/activator/identity.go |
| @@ -94,6 +94,22 @@ func (b *SplitBackend) BuildIdentity(rt registry.Route, build string) (release.S |
| return bi.BuildIdentity(rt, build) |
| } |
| +// errPartialStagedIdentity refuses a Gen that does not name a whole staged |
| +// build: no version, no staged path or no digest. |
| +var errPartialStagedIdentity = errors.New("activator: a generation without its build, staged path and digest names no staged image") |
| + |
| +// stagedIdentityOf is g's full staged identity (Astra R1 3): its version, |
| +// staged path, digest and demand, which the manifest digest check and |
| +// root's start name. A Gen missing the version, the path or the digest is |
| +// refused, so nothing downstream runs on a partial identity (genIdentity |
| +// answers only the version and the path). |
| +func stagedIdentityOf(g Gen) (release.StagedIdentity, error) { |
| + if g.Build == "" || g.StagedPath == "" || g.Digest == "" { |
| + return release.StagedIdentity{}, fmt.Errorf("%w (generation %d: build %q, path %q, digest %q)", errPartialStagedIdentity, g.N, g.Build, g.StagedPath, g.Digest) |
| + } |
| + return release.StagedIdentity{Version: g.Build, StagedPath: g.StagedPath, Digest: g.Digest, Demand: g.Demand}, nil |
| +} |
| + |
| // stagedExeOf is id as root's control channel names an executable. |
| func stagedExeOf(id release.StagedIdentity) *instancectl.StagedExe { |
| return &instancectl.StagedExe{Path: id.StagedPath, Version: id.Version, Digest: id.Digest} |
diff --git a/internal/activator/rootfs.go b/internal/activator/rootfs.go| index f756849c6..91c575a27 100644 |
| --- a/internal/activator/rootfs.go |
| +++ b/internal/activator/rootfs.go |
| @@ -164,21 +164,28 @@ func (b *ExecBackend) rootfsGateCheck(rt registry.Route) error { |
| // startRootfsFromLive is a rootfs start of what the live link names, once |
| // rootfsGates has passed: LiveIdentity (the Materialize hook and its |
| // root-stage relay run only now), the box's live exec gate when it has one, |
| -// the digest checked, then startRootfs. |
| -func (b *ExecBackend) startRootfsFromLive(ctx context.Context, rt registry.Route, sock string, n uint64) (Proc, string, error) { |
| +// the digest checked, then startRootfs. A started process comes back with |
| +// the identity root was asked to run (Astra R1 3), so its generation names |
| +// the image, not the link; a failure answers the zero identity. |
| +func (b *ExecBackend) startRootfsFromLive(ctx context.Context, rt registry.Route, sock string, n uint64) (Proc, release.StagedIdentity, error) { |
| id, err := b.LiveIdentity(ctx, rt) |
| if err != nil { |
| - return nil, "", err |
| + return nil, release.StagedIdentity{}, err |
| } |
| if b.LiveExecGate != nil { |
| if err := b.LiveExecGate(rt, id.Version); err != nil { |
| - return nil, "", err |
| + return nil, release.StagedIdentity{}, err |
| } |
| } |
| if err := release.VerifyStaged(id); err != nil { |
| - return nil, "", fmt.Errorf("%s: not started on %s: %w", rt.Host, id.Version, err) |
| + return nil, release.StagedIdentity{}, fmt.Errorf("%s: not started on %s: %w", rt.Host, id.Version, err) |
| + } |
| + proc, ver, err := b.startRootfs(ctx, rt, sock, n, id) |
| + if err != nil { |
| + return proc, release.StagedIdentity{}, err |
| } |
| - return b.startRootfs(ctx, rt, sock, n, id) |
| + id.Version = ver |
| + return proc, id, nil |
| } |
| // startRootfsGen is StartGen and StartGenExe for a rootfs row: the gates |
| @@ -186,6 +193,9 @@ func (b *ExecBackend) startRootfsFromLive(ctx context.Context, rt registry.Route |
| // the instance run dir, the sock rootfsVars hands the process) or the |
| // canonical one. A nil id is the live link (StartGen); otherwise it is the |
| // recorded image, checked against its digest before it is named to root. |
| +// Either way the started Gen carries the full identity of the image root |
| +// was asked to run (Astra R1 3), so stagedIdentityOf never refuses a rootfs |
| +// generation that is running. |
| func (b *ExecBackend) startRootfsGen(ctx context.Context, rt registry.Route, n uint64, perGen bool, id *release.StagedIdentity) (Proc, Gen, error) { |
| if err := b.rootfsGates(rt); err != nil { |
| return nil, Gen{}, err |
| @@ -196,16 +206,17 @@ func (b *ExecBackend) startRootfsGen(ctx context.Context, rt registry.Route, n u |
| } |
| var ( |
| proc Proc |
| - ver string |
| + ran release.StagedIdentity |
| err error |
| ) |
| if id == nil { |
| - proc, ver, err = b.startRootfsFromLive(ctx, rt, sock, n) |
| + proc, ran, err = b.startRootfsFromLive(ctx, rt, sock, n) |
| } else { |
| if verr := release.VerifyStaged(*id); verr != nil { |
| return nil, Gen{}, fmt.Errorf("%s: not started on %s: %w", rt.Host, id.Version, verr) |
| } |
| - proc, ver, err = b.startRootfs(ctx, rt, sock, n, *id) |
| + ran = *id |
| + proc, ran.Version, err = b.startRootfs(ctx, rt, sock, n, *id) |
| } |
| g := Gen{N: n, Socket: sock, Control: sock + ".control"} |
| if err != nil { |
| @@ -213,13 +224,49 @@ func (b *ExecBackend) startRootfsGen(ctx context.Context, rt registry.Route, n u |
| // number is what the caller proves it stopped by. |
| return proc, g, err |
| } |
| - g.Build = ver |
| - if id != nil { |
| - g.StagedPath, g.Digest, g.Demand, g.Tier = id.StagedPath, id.Digest, id.Demand, b.GenTier(rt, id.Version) |
| - } |
| + g.Build, g.StagedPath, g.Digest, g.Demand, g.Tier = ran.Version, ran.StagedPath, ran.Digest, ran.Demand, b.GenTier(rt, ran.Version) |
| return proc, g, nil |
| } |
| +// startStagedRootfs is StartStaged for a rootfs row (Task N1): a swap's |
| +// successor, a hold's pinned build and every planned wake. It keeps |
| +// StartStaged's contract (Astra R1 2): it returns the generation as soon |
| +// as its unit runs, and the caller does the one bounded health check |
| +// (genHealthy, at healthBoundFor), so it never waits for health itself and |
| +// never goes through startRootfs. In order: g's full staged identity (a |
| +// swap never starts the live link: a Gen with no staged path or digest is |
| +// refused), rootfsGates, the image checked against its digest, then |
| +// spawnRootfs on the generation's own socket. A refusal asks root nothing. |
| +// The caller's exec gate rides in on ctx (withExecGate) and spawnRootfs |
| +// asks it right before root is. Errors keep spawnRootfs's contract: a |
| +// start whose unit may have run says so (startMayHaveRun, |
| +// genStartUnproven); spawnRootfs hands back no process with an error. |
| +func (b *ExecBackend) startStagedRootfs(ctx context.Context, rt registry.Route, g Gen) (Gen, Proc, error) { |
| + id, err := stagedIdentityOf(g) |
| + if err != nil { |
| + return g, nil, refuseRootfs(fmt.Errorf("%s: a staged rootfs start: %w", rt.Host, err)) |
| + } |
| + if err := b.rootfsGates(rt); err != nil { |
| + return g, nil, err |
| + } |
| + if g.Tier == (cgroup.Limit{}) { |
| + g.Tier = b.GenTier(rt, g.Build) |
| + } |
| + if err := release.VerifyStaged(id); err != nil { |
| + return g, nil, fmt.Errorf("%s: not started on %s: %w", rt.Host, id.Version, err) |
| + } |
| + sock := nestedGenSocketIn(b.pathsFor(rt).runDir, g.N) |
| + up, ver, err := b.spawnRootfs(ctx, rt, sock, g.N, id) |
| + if err != nil { |
| + if up != nil { |
| + return g, up, err |
| + } |
| + return g, nil, err |
| + } |
| + g.Socket, g.Control, g.Build = sock, sock+".control", ver |
| + return g, up, nil |
| +} |
| + |
| // startRootfs is Start for a rootfs row after rootfsGates passed: the |
| // two-file launch, root's start of the image id names, and the web |
| // process's health wait, bounded by rootfsHealthTimeout. A failed wait |
diff --git a/internal/activator/rootfs_invariants_test.go b/internal/activator/rootfs_invariants_test.go| index 066930701..4a5624a22 100644 |
| --- a/internal/activator/rootfs_invariants_test.go |
| +++ b/internal/activator/rootfs_invariants_test.go |
| @@ -592,6 +592,54 @@ var rootfsProofHandles = map[string]func(t *testing.T, r *rootfsProofRig){ |
| t.Errorf("the unproven generation's launch file: %v, want it kept", err) |
| } |
| }, |
| + // N1: a generation StartStaged started (a swap's successor, a hold's |
| + // pin, a planned wake) is the same rootfs gen unit, stopped and proven |
| + // through it. |
| + "staged web generation unitProc": func(t *testing.T, r *rootfsProofRig) { |
| + id := stagedRootfsIdentity(t, r.b, "v2") |
| + _, p, err := r.b.StartStaged(context.Background(), r.rt, Gen{N: 7, Build: id.Version, StagedPath: id.StagedPath, Digest: id.Digest}) |
| + if err != nil { |
| + t.Fatalf("StartStaged: %v", err) |
| + } |
| + unit := instancectl.RootfsGenUnitName(r.rt.Host, 7) |
| + t.Cleanup(func() { |
| + r.setPopulated(t, unit, false) |
| + waitFor(t, "the generation to exit once its group is empty", p.Exited) |
| + }) |
| + if up := p.(*unitProc); up.unit != unit { |
| + t.Fatalf("the handle watches %q, want %q", up.unit, unit) |
| + } |
| + r.ctl.mustHaveCallPrefix(t, "start-rootfs "+r.rt.Host+" gen=7 ") |
| + ctx, cancel := context.WithTimeout(context.Background(), 300*time.Millisecond) |
| + defer cancel() |
| + if err := p.Stop(ctx); !errors.Is(err, ErrStillRunning) { |
| + t.Fatalf("Stop with %s's group populated = %v, want ErrStillRunning", unit, err) |
| + } |
| + if p.Exited() { |
| + t.Fatal("the generation read as exited while its rootfs unit's group is populated") |
| + } |
| + r.ctl.mustHaveCallPrefix(t, "stop-rootfs "+r.rt.Host+" role=run gen=7 worker= kill=false") |
| + r.noBinaryVerb(t) |
| + }, |
| + // N1, pf 2.2: the host cgroup verbs refuse a rootfs row before they |
| + // open anything, so the absent binary host cgroup proves nothing. |
| + "KillHost and EmptyHost": func(t *testing.T, r *rootfsProofRig) { |
| + opened := 0 |
| + r.b.openHostCleanup = func(string) (hostCleanup, error) { opened++; return nil, errors.New("absent") } |
| + sb := &SplitBackend{Exec: r.b, Unit: &UnitBackend{}} |
| + for name, err := range map[string]error{ |
| + "KillHost": r.b.KillHost(context.Background(), r.rt), "EmptyHost": r.b.EmptyHost(context.Background(), r.rt), |
| + "SplitBackend.KillHost": sb.KillHost(context.Background(), r.rt), "SplitBackend.EmptyHost": sb.EmptyHost(context.Background(), r.rt), |
| + } { |
| + if !errors.Is(err, errRootfsKillHost) { |
| + t.Errorf("%s on a rootfs row = %v, want errRootfsKillHost", name, err) |
| + } |
| + } |
| + if opened != 0 { |
| + t.Fatalf("the host cgroup was opened %d times for a rootfs row", opened) |
| + } |
| + r.noBinaryVerb(t) |
| + }, |
| "rootfs cmdUnit": func(t *testing.T, r *rootfsProofRig) { |
| old := roleEmptyBound |
| roleEmptyBound = 100 * time.Millisecond |
| @@ -822,6 +870,13 @@ var rootfsRefusals = []struct { |
| {"root's 403 (ErrStagedImage)", fmt.Errorf("%w: x", instancectl.ErrStagedImage)}, |
| {"root's pre-start refusal (ErrStartRefused, marked)", refuseRootfs(fmt.Errorf("%w: x", instancectl.ErrStartRefused))}, |
| {"errBuildStateUnwritten", errBuildStateUnwritten}, |
| + // N1: root's pre-start /cmd answers, as CmdRootfs types them and its |
| + // callers mark them; an unknown runtime and a staged start with a |
| + // partial identity, as StartStaged refuses them. |
| + {"root's pre-start /cmd refusal (CmdRootfs's ErrStartRefused, marked)", refuseRootfs(fmt.Errorf("start cmd: %w", fmt.Errorf("%w: x", instancectl.ErrStartRefused)))}, |
| + {"root's /cmd 403 (CmdRootfs's ErrStagedImage, marked)", refuseRootfs(fmt.Errorf("start cmd: %w", fmt.Errorf("%w: x", instancectl.ErrStagedImage)))}, |
| + {"an unknown runtime (errRuntimeUnknown, marked)", refuseRootfs(fmt.Errorf("x: %w", errRuntimeUnknown))}, |
| + {"a staged start with a partial identity (marked)", refuseRootfs(fmt.Errorf("x: %w", errPartialStagedIdentity))}, |
| } |
| func TestRootfsRefusalsAreNotBuildFailures(t *testing.T) { |
| @@ -954,9 +1009,11 @@ var ( |
| "github.com/carlosframework/platform/internal/instancectl.GenUnitName", |
| "github.com/carlosframework/platform/internal/instancectl.UnitName"}, |
| "rootfsSeamInterfaces": {"ProcessReader", "ReleaseBackend", "RootfsControl", "RootfsWorkerBackend", "rootfsDBPlacer", "rootfsStartable"}, |
| - "rootfsProofHandles": {"rootfs cmdUnit", "web generation unitProc"}, |
| - "rootfsRefusals": {"errBuildStateUnwritten", "errNoRootfsControl", "errRootfsBinaryLaunch", "errRootfsRefused", |
| - "refuseRootfs (a gate's refusal)", "root's 403 (ErrStagedImage)", "root's pre-start refusal (ErrStartRefused, marked)"}, |
| + "rootfsProofHandles": {"KillHost and EmptyHost", "rootfs cmdUnit", "staged web generation unitProc", "web generation unitProc"}, |
| + "rootfsRefusals": {"a staged start with a partial identity (marked)", "an unknown runtime (errRuntimeUnknown, marked)", |
| + "errBuildStateUnwritten", "errNoRootfsControl", "errRootfsBinaryLaunch", "errRootfsRefused", |
| + "refuseRootfs (a gate's refusal)", "root's /cmd 403 (CmdRootfs's ErrStagedImage, marked)", "root's 403 (ErrStagedImage)", |
| + "root's pre-start /cmd refusal (CmdRootfs's ErrStartRefused, marked)", "root's pre-start refusal (ErrStartRefused, marked)"}, |
| "rootfsLaunchRows": {"web"}, |
| "rootfsComposerRows": {"run"}, |
| "rootfsAgentOnlyEnv": {"AGENT_ONLY", "CARLOS_AGENT_ONLY", "HOME", "PATH"}, |
diff --git a/internal/activator/rootfs_service.go b/internal/activator/rootfs_service.go| new file mode 100644 |
| index 000000000..60fe2100c |
| --- /dev/null |
| +++ b/internal/activator/rootfs_service.go |
| @@ -0,0 +1,32 @@ |
| +// rootfs_service.go is the Service's half of rootfs apps Plan 3: what the |
| +// activator decides about a rootfs row before and around a start, as |
| +// opposed to the backend's half (rootfs.go). Task N1 adds the health |
| +// bound; Tasks 7, 8 and R3 add the start gates, residency and the wake |
| +// ceiling. |
| +package activator |
| + |
| +import ( |
| + "errors" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +// errRuntimeUnknown refuses a row whose runtime this agent does not know |
| +// (registry.ValidRuntime): it is never started as a binary nor as |
| +// rootfs-v1, so no future value degrades to either. Every start path that |
| +// meets one marks it with refuseRootfs, which verdictOf and classifyStep |
| +// read as a refusal, never the build's failure. |
| +var errRuntimeUnknown = errors.New("activator: this agent does not know the instance's runtime") |
| + |
| +// healthBoundFor is the bound on a generation's health check: the rootfs |
| +// web process's (rootfsHealthTimeout, 120 s, spec §5.2) for a rootfs row, |
| +// spawnHealthTimeout (30 s) otherwise. StartStaged returns a generation as |
| +// soon as it runs, so the caller's one check (genHealthy) and a planned |
| +// first wake's verdict window (verdictFloorGate) are sized by it. |
| +func healthBoundFor(rt registry.Route) time.Duration { |
| + if rt.Runtime == registry.RuntimeRootfsV1 { |
| + return rootfsHealthTimeout |
| + } |
| + return spawnHealthTimeout |
| +} |
diff --git a/internal/activator/rootfs_staged_test.go b/internal/activator/rootfs_staged_test.go| new file mode 100644 |
| index 000000000..5261b2806 |
| --- /dev/null |
| +++ b/internal/activator/rootfs_staged_test.go |
| @@ -0,0 +1,771 @@ |
| +package activator |
| + |
| +// Rootfs apps Plan 3 Task N1: a rootfs row swaps, holds and takes a |
| +// planned wake. The Service tests drive the real Service.Swap and |
| +// spawnPlanned over the real ExecBackend's StartStaged and KillGen (the |
| +// swap fixture's fakes stand in only for the lease, the restore, the |
| +// parent raise and the switch), with root played by fakeControl through |
| +// its RootfsControl half. |
| + |
| +import ( |
| + "context" |
| + "encoding/json" |
| + "errors" |
| + "fmt" |
| + "net" |
| + "net/http" |
| + "os" |
| + "os/exec" |
| + "path/filepath" |
| + "strings" |
| + "sync" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/cgroup" |
| + "github.com/carlosframework/platform/internal/instancectl" |
| + "github.com/carlosframework/platform/internal/provision" |
| + "github.com/carlosframework/platform/internal/registry" |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +// stagedRootfsCtl is fakeControl with each rootfs generation a unit of its |
| +// own: StartRootfs makes generation n's unit active and populated |
| +// (invocation inv-<n>) and then runs serve, where the web process would |
| +// bind its socket; StopRootfs leaves the unit it stops inactive and empty. |
| +// No binary unit exists in any answer. |
| +type stagedRootfsCtl struct { |
| + *fakeControl |
| + umu sync.Mutex |
| + units map[string][3]string // unit -> ActiveState, InvocationID, "populated" or "" |
| + starts []stagedRootfsStart |
| + serve func(gen uint64, img instancectl.StagedImage) |
| +} |
| + |
| +type stagedRootfsStart struct { |
| + gen uint64 |
| + memoryMax int64 |
| + img instancectl.StagedImage |
| +} |
| + |
| +func (c *stagedRootfsCtl) StartRootfs(ctx context.Context, host string, gen uint64, max int64, img instancectl.StagedImage) (string, error) { |
| + if _, err := c.fakeControl.StartRootfs(ctx, host, gen, max, img); err != nil { |
| + return "", err |
| + } |
| + inv := fmt.Sprintf("inv-%d", gen) |
| + c.umu.Lock() |
| + c.units[instancectl.RootfsGenUnitName(host, gen)] = [3]string{"active", inv, "populated"} |
| + c.starts = append(c.starts, stagedRootfsStart{gen: gen, memoryMax: max, img: img}) |
| + serve := c.serve |
| + c.umu.Unlock() |
| + if serve != nil { |
| + serve(gen, img) |
| + } |
| + return inv, nil |
| +} |
| + |
| +func (c *stagedRootfsCtl) StopRootfs(ctx context.Context, host string, role instancectl.Role, gen uint64, worker string, kill bool) error { |
| + if err := c.fakeControl.StopRootfs(ctx, host, role, gen, worker, kill); err != nil { |
| + return err |
| + } |
| + unit := instancectl.RootfsUnitName(role, host) |
| + if gen != 0 { |
| + unit = instancectl.RootfsGenUnitName(host, gen) |
| + } |
| + c.umu.Lock() |
| + if u, ok := c.units[unit]; ok { |
| + c.units[unit] = [3]string{"inactive", u[1], ""} |
| + } |
| + c.umu.Unlock() |
| + return nil |
| +} |
| + |
| +func (c *stagedRootfsCtl) state(_ context.Context, unit string) (string, string, error) { |
| + c.umu.Lock() |
| + defer c.umu.Unlock() |
| + if u, ok := c.units[unit]; ok { |
| + return u[0], u[1], nil |
| + } |
| + return "inactive", "", nil |
| +} |
| + |
| +func (c *stagedRootfsCtl) events(_ string, unit string) (bool, bool, error) { |
| + c.umu.Lock() |
| + defer c.umu.Unlock() |
| + u, ok := c.units[unit] |
| + return ok, ok && u[2] == "populated", nil |
| +} |
| + |
| +func (c *stagedRootfsCtl) setServe(f func(gen uint64, img instancectl.StagedImage)) { |
| + c.umu.Lock() |
| + c.serve = f |
| + c.umu.Unlock() |
| +} |
| + |
| +func (c *stagedRootfsCtl) started() []stagedRootfsStart { |
| + c.umu.Lock() |
| + defer c.umu.Unlock() |
| + return append([]stagedRootfsStart(nil), c.starts...) |
| +} |
| + |
| +// unitOf is generation gen's unit as the fake last left it. |
| +func (c *stagedRootfsCtl) unitOf(host string, gen uint64) [3]string { |
| + c.umu.Lock() |
| + defer c.umu.Unlock() |
| + return c.units[instancectl.RootfsGenUnitName(host, gen)] |
| +} |
| + |
| +// newStagedRootfsRig is newRootfsBackend with generations contained and |
| +// root played by stagedRootfsCtl, and the rootfs row. |
| +func newStagedRootfsRig(t *testing.T) (*ExecBackend, *stagedRootfsCtl, registry.Route) { |
| + t.Helper() |
| + eb, fc := newRootfsBackend(t) |
| + eb.GenContainment = true |
| + c := &stagedRootfsCtl{fakeControl: fc, units: map[string][3]string{}} |
| + eb.Control = c |
| + eb.UnitState = c.state |
| + eb.CgroupEvents = c.events |
| + return eb, c, rootfsRoute(eb) |
| +} |
| + |
| +// serveVersions binds each started generation's socket when its image is |
| +// one of versions, after delay; any other generation never answers. |
| +func serveVersions(t *testing.T, eb *ExecBackend, rt registry.Route, delay time.Duration, versions ...string) func(uint64, instancectl.StagedImage) { |
| + return func(gen uint64, img instancectl.StagedImage) { |
| + for _, v := range versions { |
| + if img.Version == v { |
| + serveAfter(t, nestedGenSocketIn(eb.pathsFor(rt).runDir, gen), delay) |
| + } |
| + } |
| + } |
| +} |
| + |
| +// rootfsLaunch is launchBackend over the rootfs row, with KillGen the real |
| +// backend's for every generation it started: the rootfs gen unit's kill |
| +// and its cgroup proof (killFailedRootfsGen). |
| +type rootfsLaunch struct{ *launchBackend } |
| + |
| +func (b *rootfsLaunch) KillGen(ctx context.Context, _ registry.Route, n uint64) error { |
| + b.lmu.Lock() |
| + _, ours := b.procs[n] |
| + b.lmu.Unlock() |
| + if !ours { |
| + return nil // the fixture's fake incumbent |
| + } |
| + return b.eb.KillGen(ctx, b.rt, n) |
| +} |
| + |
| +func newRootfsLaunch(t *testing.T, fb *fakeSwapBackend) (*rootfsLaunch, *stagedRootfsCtl) { |
| + t.Helper() |
| + eb, ctl, rt := newStagedRootfsRig(t) |
| + lb := &rootfsLaunch{&launchBackend{fakeSwapBackend: fb, procs: map[uint64]Proc{}, eb: eb, rt: rt}} |
| + t.Cleanup(lb.stopAll) |
| + return lb, ctl |
| +} |
| + |
| +// rootfsSwapFixture is the swap fixture on a rootfs row: the activator's |
| +// row has the runtime, StartStaged and KillGen are the real backend's, the |
| +// attempt's build is v2 staged as root stages an image, and every health |
| +// check is the real one. |
| +func rootfsSwapFixture(t *testing.T, mode SwapMode) (*swapFixture, *rootfsLaunch, *stagedRootfsCtl, Attempt) { |
| + t.Helper() |
| + f := newSwapFixture(t) |
| + lb, ctl := newRootfsLaunch(t, f.b) |
| + f.a.backend = lb |
| + f.a.genHealth = nil |
| + f.a.mu.Lock() |
| + f.a.entries[f.host].route.Runtime = registry.RuntimeRootfsV1 |
| + f.a.mu.Unlock() |
| + id := stagedRootfsIdentity(t, lb.eb, "v2") |
| + at := f.attempt(mode) |
| + at.StagedPath, at.Digest = id.StagedPath, id.Digest |
| + return f, lb, ctl, at |
| +} |
| + |
| +// rootfsPlanned is a Service whose every wake is plan, over b, on a rootfs |
| +// row whose socket and database are the test's own; every health check is |
| +// the real one. |
| +func rootfsPlanned(t *testing.T, b Backend, plan WakePlan, runtime string) (*Service, registry.Route, *genLog) { |
| + t.Helper() |
| + reg, err := registry.Open(filepath.Join(t.TempDir(), "registry.db")) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + a := New(Config{Bucket: "test-bucket", Idle: time.Hour}, b, reg) |
| + a.gens = true |
| + evs := &genLog{} |
| + a.OnGen = evs.add |
| + a.Planner = func(context.Context, registry.Route) WakePlan { return plan } |
| + t.Cleanup(func() { a.Shutdown(context.Background()) }) |
| + dir := t.TempDir() |
| + rt := testRoute("one.example.com") |
| + rt.Socket, rt.DBPath, rt.Runtime = filepath.Join(dir, "app.sock"), filepath.Join(dir, "app.db"), runtime |
| + return a, rt, evs |
| +} |
| + |
| +func shrinkHealthBounds(t *testing.T, binary, rootfs, floor time.Duration) { |
| + t.Helper() |
| + b, r, f := spawnHealthTimeout, rootfsHealthTimeout, firstWakeMinVerdict |
| + spawnHealthTimeout, rootfsHealthTimeout, firstWakeMinVerdict = binary, rootfs, floor |
| + t.Cleanup(func() { spawnHealthTimeout, rootfsHealthTimeout, firstWakeMinVerdict = b, r, f }) |
| +} |
| + |
| +// noBinaryStart fails t if root was asked for any binary unit's start. |
| +func noBinaryStart(t *testing.T, ctl *stagedRootfsCtl) { |
| + t.Helper() |
| + noCallStarts(t, ctl.fakeControl, "start ", "startgen", "startexe", "cmd ", "seed ") |
| +} |
| + |
| +// onlyStart is the one rootfs start root was asked for, which must name |
| +// id's image with web. |
| +func onlyStart(t *testing.T, ctl *stagedRootfsCtl, id release.StagedIdentity) stagedRootfsStart { |
| + t.Helper() |
| + starts := ctl.started() |
| + if len(starts) != 1 { |
| + t.Fatalf("root was asked for %d rootfs starts (%+v), want 1; calls:\n%s", len(starts), starts, ctl.joined()) |
| + } |
| + s := starts[0] |
| + if s.img.Path != id.StagedPath || s.img.Digest != id.Digest || s.img.Version != id.Version || s.img.Process != release.ProcessWeb || len(s.img.Argv) != 0 { |
| + t.Fatalf("root was asked for %+v, want %s's image %s (digest %s) with web", s.img, id.Version, id.StagedPath, id.Digest) |
| + } |
| + return s |
| +} |
| + |
| +func TestOverlapSwapStartsTheRootfsGeneration(t *testing.T) { |
| + f, lb, ctl, at := rootfsSwapFixture(t, ModeOverlap) |
| + ctl.setServe(serveVersions(t, lb.eb, lb.rt, 0, "v2")) |
| + res := f.a.Swap(context.Background(), at, f.newest) |
| + if res.Outcome != SwapDone { |
| + t.Fatalf("Swap = %+v, want done; calls:\n%s", res, ctl.joined()) |
| + } |
| + s := onlyStart(t, ctl, release.StagedIdentity{Version: "v2", StagedPath: at.StagedPath, Digest: at.Digest}) |
| + noBinaryStart(t, ctl) |
| + g, members := f.a.genStateForTest(f.host) |
| + if g.N != s.gen || g.N == f.incumbent.N || g.Build != "v2" || g.StagedPath != at.StagedPath || g.Digest != at.Digest || len(members) != 0 { |
| + t.Fatalf("current %+v, members %v: want generation %d of v2 serving", g, members, s.gen) |
| + } |
| + if want := nestedGenSocketIn(lb.eb.pathsFor(lb.rt).runDir, s.gen); g.Socket != want { |
| + t.Fatalf("the generation's socket is %q, want %q", g.Socket, want) |
| + } |
| +} |
| + |
| +func TestHoldSwapStartsTheRootfsGeneration(t *testing.T) { |
| + f, lb, ctl, at := rootfsSwapFixture(t, ModeHold) |
| + ctl.setServe(serveVersions(t, lb.eb, lb.rt, 0, "v2")) |
| + res := f.a.Swap(context.Background(), at, f.newest) |
| + if res.Outcome != SwapDone || !res.Executed { |
| + t.Fatalf("Swap = %+v, want done and executed; calls:\n%s", res, ctl.joined()) |
| + } |
| + s := onlyStart(t, ctl, release.StagedIdentity{Version: "v2", StagedPath: at.StagedPath, Digest: at.Digest}) |
| + noBinaryStart(t, ctl) |
| + if g, _ := f.a.genStateForTest(f.host); g.N != s.gen || g.Build != "v2" || g.StagedPath != at.StagedPath { |
| + t.Fatalf("current %+v, want the pinned generation %d of v2", g, s.gen) |
| + } |
| +} |
| + |
| +func TestPlannedWakeStartsTheRootfsGeneration(t *testing.T) { |
| + lb, ctl := newRootfsLaunch(t, &fakeSwapBackend{fakeBackend: &fakeBackend{}}) |
| + next, good := stagedRootfsIdentity(t, lb.eb, "v2"), stagedRootfsIdentity(t, lb.eb, "v3") |
| + ctl.setServe(serveVersions(t, lb.eb, lb.rt, 0, "v2")) |
| + a, rt, evs := rootfsPlanned(t, lb, WakePlan{Boot: next, Mode: ModeOverlap, FirstWake: true, Fallback: &good}, registry.RuntimeRootfsV1) |
| + done, err := a.beginRequest(context.Background(), rt) |
| + if err != nil { |
| + t.Fatalf("the planned wake: %v; calls:\n%s", err, ctl.joined()) |
| + } |
| + done() |
| + s := onlyStart(t, ctl, next) |
| + noBinaryStart(t, ctl) |
| + // Current hides a first wake under observation; the entry's own |
| + // generation is what serves. |
| + if g, _ := a.genStateForTest(rt.Host); g.N != s.gen || g.Build != "v2" || g.StagedPath != next.StagedPath || g.Digest != next.Digest { |
| + t.Fatalf("serving %+v, want generation %d of v2", g, s.gen) |
| + } |
| + evs.wait(t, GenHealthy) |
| +} |
| + |
| +func TestStartStagedRefusesARootfsLiveLink(t *testing.T) { |
| + t.Parallel() |
| + eb, ctl, rt := newStagedRootfsRig(t) |
| + id := stagedRootfsIdentity(t, eb, "v2") |
| + for name, g := range map[string]Gen{ |
| + "no staged path": {N: 7, Build: "v2"}, |
| + "no digest": {N: 7, Build: "v2", StagedPath: id.StagedPath}, |
| + "no build": {N: 7, StagedPath: id.StagedPath, Digest: id.Digest}, |
| + } { |
| + _, p, err := eb.StartStaged(context.Background(), rt, g) |
| + if !errors.Is(err, errRootfsRefused) || p != nil { |
| + t.Errorf("%s: StartStaged = %v, %v; want a refusal and no process", name, p, err) |
| + } |
| + if v := verdictOf(err); v != gateNoOwnership { |
| + t.Errorf("%s: verdictOf = %d, want gateNoOwnership", name, v) |
| + } |
| + } |
| + if calls := ctl.callList(); len(calls) != 0 { |
| + t.Fatalf("root was asked %v for a staged start with no full identity", calls) |
| + } |
| +} |
| + |
| +func TestStartStagedKeepsTheGenerationsTier(t *testing.T) { |
| + t.Parallel() |
| + eb, ctl, rt := newStagedRootfsRig(t) |
| + id := stagedRootfsIdentity(t, eb, "v2") |
| + g, p, err := eb.StartStaged(context.Background(), rt, Gen{N: 7, Build: "v2", StagedPath: id.StagedPath, Digest: id.Digest}) |
| + if err != nil { |
| + t.Fatalf("StartStaged: %v", err) |
| + } |
| + t.Cleanup(func() { p.Stop(context.Background()) }) //nolint:errcheck |
| + want := eb.GenTier(rt, "v2") |
| + if g.Tier != want || want.MemoryMax == 0 { |
| + t.Fatalf("the generation's tier is %+v, want GenTier's %+v", g.Tier, want) |
| + } |
| + if s := onlyStart(t, ctl, id); s.memoryMax != want.MemoryMax || s.gen != 7 { |
| + t.Fatalf("root was asked for generation %d memory_max=%d, want 7 at %d", s.gen, s.memoryMax, want.MemoryMax) |
| + } |
| +} |
| + |
| +// [Astra R1 2] StartStaged's contract: the process comes back as soon as |
| +// it runs, with the full identity on the Gen, and the caller does the one |
| +// bounded health check. The 120 s bound is left as it is and the web never |
| +// answers. |
| +func TestStagedRootfsStartReturnsBeforeHealth(t *testing.T) { |
| + t.Parallel() |
| + eb, ctl, rt := newStagedRootfsRig(t) |
| + id := stagedRootfsIdentity(t, eb, "v2") |
| + id.Demand = "hold" |
| + began := time.Now() |
| + g, p, err := eb.StartStaged(context.Background(), rt, Gen{N: 9, Build: "v2", StagedPath: id.StagedPath, Digest: id.Digest, Demand: id.Demand}) |
| + if err != nil || p == nil { |
| + t.Fatalf("StartStaged = %v, %v; want the running generation", p, err) |
| + } |
| + t.Cleanup(func() { p.Stop(context.Background()) }) //nolint:errcheck |
| + if took := time.Since(began); took > 5*time.Second { |
| + t.Fatalf("StartStaged took %v: it waited for health", took) |
| + } |
| + sock := nestedGenSocketIn(eb.pathsFor(rt).runDir, 9) |
| + if g.N != 9 || g.Socket != sock || g.Control != sock+".control" || g.Build != "v2" { |
| + t.Fatalf("Gen = %+v, want generation 9 on %s", g, sock) |
| + } |
| + got, err := stagedIdentityOf(g) |
| + if err != nil || got != id { |
| + t.Fatalf("stagedIdentityOf = %+v, %v; want %+v", got, err, id) |
| + } |
| + if up := p.(*unitProc); up.unit != instancectl.RootfsGenUnitName(rt.Host, 9) || p.Exited() { |
| + t.Fatalf("the handle watches %q (exited %v), want the running rootfs gen unit", up.unit, p.Exited()) |
| + } |
| + onlyStart(t, ctl, id) |
| +} |
| + |
| +// [Astra R1 2] Through the real planned-wake wiring: B's web never |
| +// answers, so the caller's health check fails it inside the rootfs bound; |
| +// B's failure is recorded, its unit is killed and proven empty, and the |
| +// good build boots in its place. |
| +func TestUnhealthyRootfsPlannedWakeFailsB(t *testing.T) { |
| + shrinkHealthBounds(t, 30*time.Second, 300*time.Millisecond, 50*time.Millisecond) |
| + lb, ctl := newRootfsLaunch(t, &fakeSwapBackend{fakeBackend: &fakeBackend{}}) |
| + next, good := stagedRootfsIdentity(t, lb.eb, "v2"), stagedRootfsIdentity(t, lb.eb, "v3") |
| + ctl.setServe(serveVersions(t, lb.eb, lb.rt, 0, "v3")) |
| + a, rt, evs := rootfsPlanned(t, lb, WakePlan{Boot: next, Mode: ModeOverlap, FirstWake: true, Fallback: &good}, registry.RuntimeRootfsV1) |
| + done, err := a.beginRequest(context.Background(), rt) |
| + if err != nil { |
| + t.Fatalf("the waiting request was not served by the good build: %v; calls:\n%s", err, ctl.joined()) |
| + } |
| + done() |
| + ev := evs.wait(t, GenFellBack) |
| + if ev.Failed.Version != "v2" || ev.Gen.Build != "v3" { |
| + t.Fatalf("fallback event = %+v, want v2 failed and v3 serving", ev) |
| + } |
| + starts := ctl.started() |
| + if len(starts) != 2 || starts[0].img.Version != "v2" || starts[1].img.Version != "v3" { |
| + t.Fatalf("rootfs starts %+v, want v2 then v3", starts) |
| + } |
| + b := starts[0].gen |
| + ctl.mustHaveCallPrefix(t, fmt.Sprintf("stop-rootfs %s role=run gen=%d worker= kill=true", lb.rt.Host, b)) |
| + if u := ctl.unitOf(lb.rt.Host, b); u[0] != "inactive" || u[2] != "" { |
| + t.Fatalf("B's unit is %v, want stopped and empty", u) |
| + } |
| + calls := ctl.callList() |
| + kill, second := -1, -1 |
| + for i, c := range calls { |
| + if strings.HasPrefix(c, fmt.Sprintf("stop-rootfs %s role=run gen=%d", lb.rt.Host, b)) && kill < 0 { |
| + kill = i |
| + } |
| + if strings.HasPrefix(c, "start-rootfs") && strings.Contains(c, "version=v3") { |
| + second = i |
| + } |
| + } |
| + if kill < 0 || second < kill { |
| + t.Fatalf("the good build started before B's kill:\n%s", ctl.joined()) |
| + } |
| + noBinaryStart(t, ctl) |
| + if g, ok := a.Current(rt.Host); !ok || g.Build != "v3" { |
| + t.Fatalf("Current = %+v %v, want the good build serving", g, ok) |
| + } |
| +} |
| + |
| +// [Astra R1 2] Through the real hold wiring: the pinned build's web never |
| +// answers. The hold is down with execution evidence (the build ran), never |
| +// a cancelled no-run, and its unit is stopped. |
| +func TestUnhealthyRootfsHoldReportsDown(t *testing.T) { |
| + shrinkHealthBounds(t, 30*time.Second, 300*time.Millisecond, 50*time.Millisecond) |
| + f, lb, ctl, at := rootfsSwapFixture(t, ModeHold) |
| + began := time.Now() |
| + res := f.a.Swap(context.Background(), at, f.newest) |
| + if res.Outcome != SwapDown || !res.Executed { |
| + t.Fatalf("Swap = %+v, want down with execution evidence; calls:\n%s", res, ctl.joined()) |
| + } |
| + if took := time.Since(began); took > 10*time.Second { |
| + t.Fatalf("the hold took %v: its health check was not the rootfs bound", took) |
| + } |
| + s := onlyStart(t, ctl, release.StagedIdentity{Version: "v2", StagedPath: at.StagedPath, Digest: at.Digest}) |
| + waitFor(t, "the failed pinned generation's stop", func() bool { |
| + return strings.Contains(ctl.joined(), fmt.Sprintf("stop-rootfs %s role=run gen=%d", lb.rt.Host, s.gen)) |
| + }) |
| + noBinaryStart(t, ctl) |
| +} |
| + |
| +// [Astra R1 3] Every rootfs Gen carries the image root was asked for: |
| +// the live-link start (StartGen) and the recorded image (StartGenExe). |
| +func TestEveryRootfsGenCarriesItsIdentity(t *testing.T) { |
| + t.Parallel() |
| + t.Run("live link", func(t *testing.T) { |
| + eb, ctl, rt := newStagedRootfsRig(t) |
| + ctl.setServe(serveVersions(t, eb, rt, 0, "v1")) |
| + p, g, err := eb.StartGen(context.Background(), rt, 7, true) |
| + if err != nil { |
| + t.Fatalf("StartGen: %v", err) |
| + } |
| + t.Cleanup(func() { p.Stop(context.Background()) }) //nolint:errcheck |
| + img := ctl.started()[0].img |
| + if img.Version != "v1" || img.Path == "" || img.Digest == "" { |
| + t.Fatalf("root was asked for %+v", img) |
| + } |
| + want := release.StagedIdentity{Version: img.Version, StagedPath: img.Path, Digest: img.Digest} |
| + got, err := stagedIdentityOf(g) |
| + if err != nil || got != want || g.Tier != eb.GenTier(rt, "v1") { |
| + t.Fatalf("Gen %+v: stagedIdentityOf = %+v, %v; want %+v at GenTier", g, got, err, want) |
| + } |
| + }) |
| + t.Run("recorded image", func(t *testing.T) { |
| + eb, ctl, rt := newStagedRootfsRig(t) |
| + ctl.setServe(serveVersions(t, eb, rt, 0, "v2")) |
| + id := stagedRootfsIdentity(t, eb, "v2") |
| + id.Demand = "hold" |
| + p, g, err := eb.StartGenExe(context.Background(), rt, 8, true, id) |
| + if err != nil { |
| + t.Fatalf("StartGenExe: %v", err) |
| + } |
| + t.Cleanup(func() { p.Stop(context.Background()) }) //nolint:errcheck |
| + if got, err := stagedIdentityOf(g); err != nil || got != id { |
| + t.Fatalf("stagedIdentityOf = %+v, %v; want %+v", got, err, id) |
| + } |
| + }) |
| + t.Run("partial", func(t *testing.T) { |
| + if _, err := stagedIdentityOf(Gen{N: 3, Build: "v2", StagedPath: "/opt/carlos/versions/acme/shop/v2/rootfs.img"}); err == nil { |
| + t.Fatal("stagedIdentityOf accepted a Gen with no digest") |
| + } |
| + }) |
| +} |
| + |
| +// [Astra R1 11] The caller's health window is the rootfs bound for a rootfs |
| +// row and the binary bound for a binary row, through the real planned-wake |
| +// and overlap wiring. The web first answers at 900 ms; the binary bound is |
| +// 300 ms, the rootfs bound 2 s. healthBy is 30 s out and the verdict floor |
| +// 100 ms, so the floor never decides. |
| +func TestRootfsHealthWindowIsTheRootfsBound(t *testing.T) { |
| + shrinkHealthBounds(t, 300*time.Millisecond, 2*time.Second, 100*time.Millisecond) |
| + const answersAt = 900 * time.Millisecond |
| + t.Run("rootfs planned wake", func(t *testing.T) { |
| + lb, ctl := newRootfsLaunch(t, &fakeSwapBackend{fakeBackend: &fakeBackend{}}) |
| + next, good := stagedRootfsIdentity(t, lb.eb, "v2"), stagedRootfsIdentity(t, lb.eb, "v3") |
| + ctl.setServe(serveVersions(t, lb.eb, lb.rt, answersAt, "v2", "v3")) |
| + a, rt, evs := rootfsPlanned(t, lb, WakePlan{Boot: next, Mode: ModeOverlap, FirstWake: true, Fallback: &good}, registry.RuntimeRootfsV1) |
| + done, err := a.beginRequest(context.Background(), rt) |
| + if err != nil { |
| + t.Fatalf("the planned wake: %v", err) |
| + } |
| + done() |
| + if g, _ := a.genStateForTest(rt.Host); g.Build != "v2" { |
| + t.Fatalf("serving %+v, want B", g) |
| + } |
| + evs.wait(t, GenHealthy) |
| + if k := evs.kinds(); len(k) != 1 { |
| + t.Fatalf("events %v, want B healthy alone", k) |
| + } |
| + }) |
| + t.Run("rootfs overlap", func(t *testing.T) { |
| + f, lb, ctl, at := rootfsSwapFixture(t, ModeOverlap) |
| + ctl.setServe(serveVersions(t, lb.eb, lb.rt, answersAt, "v2")) |
| + if res := f.a.Swap(context.Background(), at, f.newest); res.Outcome != SwapDone { |
| + t.Fatalf("Swap = %+v, want done", res) |
| + } |
| + }) |
| + t.Run("binary overlap", func(t *testing.T) { |
| + f := newSwapFixture(t) |
| + f.a.genHealth = nil |
| + f.b.genServe = func(g Gen) func() { |
| + serveAfter(t, g.Socket, answersAt) |
| + return func() {} |
| + } |
| + if res := f.a.Swap(context.Background(), f.attempt(ModeOverlap), f.newest); res.Outcome != SwapFailed || !strings.Contains(res.Reason, "health") { |
| + t.Fatalf("Swap = %+v, want failed on health", res) |
| + } |
| + }) |
| + t.Run("binary planned wake", func(t *testing.T) { |
| + fb := &fakeSwapBackend{fakeBackend: &fakeBackend{}, genSocketDir: t.TempDir()} |
| + fb.genServe = func(g Gen) func() { |
| + serveAfter(t, g.Socket, answersAt) |
| + return func() {} |
| + } |
| + next := stagedScript(t, t.TempDir(), "v2", "#!/bin/sh\nexec sleep 30\n") |
| + a, rt, evs := rootfsPlanned(t, fb, WakePlan{Boot: next, Mode: ModeOverlap, FirstWake: true}, "") |
| + if _, err := a.beginRequest(context.Background(), rt); err == nil { |
| + t.Fatal("a binary B that answers after its bound was served") |
| + } |
| + if ev := evs.wait(t, GenFailed); ev.Failed.Version != "v2" { |
| + t.Fatalf("failure event = %+v", ev) |
| + } |
| + }) |
| +} |
| + |
| +// verdictFloorGate's window is the bound it is given, so a rootfs B's |
| +// first wake is judged on the rootfs bound, never refused for time a |
| +// binary bound would not have. |
| +func TestVerdictFloorGateTakesTheBound(t *testing.T) { |
| + t.Parallel() |
| + for _, bound := range []time.Duration{spawnHealthTimeout, rootfsHealthTimeout} { |
| + var left time.Duration |
| + if err := verdictFloorGate(func() error { return nil }, time.Now().Add(time.Hour), bound, &left)(); err != nil { |
| + t.Fatalf("bound %v: %v", bound, err) |
| + } |
| + if left > bound || left < bound-time.Second { |
| + t.Fatalf("bound %v: window %v", bound, left) |
| + } |
| + } |
| + if healthBoundFor(registry.Route{Runtime: registry.RuntimeRootfsV1}) != rootfsHealthTimeout || |
| + healthBoundFor(registry.Route{}) != spawnHealthTimeout || healthBoundFor(registry.Route{Runtime: "rootfs-v9"}) != spawnHealthTimeout { |
| + t.Fatal("healthBoundFor: want the rootfs bound for rootfs-v1 alone") |
| + } |
| + if rootfsHealthTimeout != 120*time.Second { |
| + t.Fatalf("rootfsHealthTimeout = %v, want 120 s", rootfsHealthTimeout) |
| + } |
| +} |
| + |
| +// [pf 1.7] Root's pre-start /cmd answers come back typed, through the real |
| +// client over a unix socket: 403 is ErrStagedImage; 400, 409, 501, 503 and |
| +// 507 are ErrStartRefused; 502 is neither (it may follow a start). |
| +func TestCmdRootfsRefusalsAreTyped(t *testing.T) { |
| + t.Parallel() |
| + dir, err := os.MkdirTemp("", "cr") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { os.RemoveAll(dir) }) |
| + sock := filepath.Join(dir, "c.sock") |
| + ln, err := net.Listen("unix", sock) |
| + if err != nil { |
| + t.Skipf("cannot bind here (%v)", err) |
| + } |
| + var mu sync.Mutex |
| + status := 0 |
| + srv := &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + mu.Lock() |
| + s := status |
| + mu.Unlock() |
| + if r.URL.Path != "/cmd" { |
| + http.NotFound(w, r) |
| + return |
| + } |
| + if s == http.StatusOK { |
| + json.NewEncoder(w).Encode(instancectl.StartResponse{InvocationID: "inv-c"}) //nolint:errcheck |
| + return |
| + } |
| + http.Error(w, "refused", s) |
| + })} |
| + go srv.Serve(ln) //nolint:errcheck |
| + t.Cleanup(func() { srv.Close() }) |
| + c := instancectl.NewClient(sock) |
| + img := instancectl.StagedImage{Path: "/opt/carlos/versions/acme/shop/v2/rootfs.img", Version: "v2", Digest: strings.Repeat("a", 64), Process: "release"} |
| + for _, c2 := range []struct { |
| + status int |
| + staged, refused bool |
| + }{ |
| + {http.StatusOK, false, false}, |
| + {http.StatusForbidden, true, false}, |
| + {http.StatusBadRequest, false, true}, |
| + {http.StatusConflict, false, true}, |
| + {http.StatusNotImplemented, false, true}, |
| + {http.StatusServiceUnavailable, false, true}, |
| + {http.StatusInsufficientStorage, false, true}, |
| + {http.StatusBadGateway, false, false}, |
| + } { |
| + mu.Lock() |
| + status = c2.status |
| + mu.Unlock() |
| + inv, err := c.CmdRootfs(context.Background(), rootfsTestHost, 1<<20, img) |
| + if c2.status == http.StatusOK { |
| + if err != nil || inv != "inv-c" { |
| + t.Fatalf("200: %q, %v", inv, err) |
| + } |
| + continue |
| + } |
| + if err == nil { |
| + t.Fatalf("%d: no error", c2.status) |
| + } |
| + if got := errors.Is(err, instancectl.ErrStagedImage); got != c2.staged { |
| + t.Errorf("%d: ErrStagedImage %v, want %v: %v", c2.status, got, c2.staged, err) |
| + } |
| + if got := errors.Is(err, instancectl.ErrStartRefused); got != c2.refused { |
| + t.Errorf("%d: ErrStartRefused %v, want %v: %v", c2.status, got, c2.refused, err) |
| + } |
| + // The callers mark both with refuseRootfs (Tasks 10 and 12): |
| + // verdictOf reads the marked error as a refusal. |
| + if c2.staged || c2.refused { |
| + if v := verdictOf(refuseRootfs(err)); v != gateNoOwnership { |
| + t.Errorf("%d: verdictOf(marked) = %d, want gateNoOwnership", c2.status, v) |
| + } |
| + } |
| + } |
| +} |
| + |
| +// [pf 2.2] KillHost and EmptyHost read carlos-exec@<host>, where an absent |
| +// cgroup counts as proven and a rootfs process never lives: a rootfs row, |
| +// or any row with a runtime, is refused before the cgroup is opened, on the |
| +// exec backend and through the split one. |
| +func TestKillHostAndEmptyHostRefuseRootfs(t *testing.T) { |
| + t.Parallel() |
| + for _, runtime := range []string{registry.RuntimeRootfsV1, "rootfs-v9"} { |
| + eb := testExecBackend(t) |
| + opened := 0 |
| + eb.openHostCleanup = func(string) (hostCleanup, error) { |
| + opened++ |
| + return nil, errors.New("the host cgroup is absent") // read as proven if asked |
| + } |
| + rt := registry.Route{Host: rootfsTestHost, Kind: "instance", Runtime: runtime} |
| + sb := &SplitBackend{Exec: eb, Unit: &UnitBackend{}} |
| + for name, verb := range map[string]func() error{ |
| + "ExecBackend.KillHost": func() error { return eb.KillHost(context.Background(), rt) }, |
| + "ExecBackend.EmptyHost": func() error { return eb.EmptyHost(context.Background(), rt) }, |
| + "SplitBackend.KillHost": func() error { return sb.KillHost(context.Background(), rt) }, |
| + "SplitBackend.EmptyHost": func() error { return sb.EmptyHost(context.Background(), rt) }, |
| + } { |
| + if err := verb(); !errors.Is(err, errRootfsKillHost) { |
| + t.Errorf("%s, runtime %q: %v, want errRootfsKillHost", name, runtime, err) |
| + } |
| + } |
| + if opened != 0 { |
| + t.Errorf("runtime %q: the host cgroup was opened %d times", runtime, opened) |
| + } |
| + } |
| +} |
| + |
| +// [pf 1.14] A planted <host>-sidecar.env names no sidecar for a rootfs row |
| +// (or any row with a runtime), and StartSidecar still refuses one, on any |
| +// layout, before root is asked or anything execs. |
| +func TestHasSidecarFalseForRootfs(t *testing.T) { |
| + t.Parallel() |
| + eb, ctl, rt := newStagedRootfsRig(t) |
| + dir := eb.pathsFor(rt).dataDir |
| + if err := os.MkdirAll(dir, 0o700); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.WriteFile(filepath.Join(dir, rt.Host+"-sidecar.env"), []byte("CARLOS_BASE=x\n"), 0o600); err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, runtime := range []string{registry.RuntimeRootfsV1, "rootfs-v9"} { |
| + r := rt |
| + r.Runtime = runtime |
| + if eb.HasSidecar(r) { |
| + t.Errorf("runtime %q: HasSidecar answered true for a planted sidecar file", runtime) |
| + } |
| + if p, err := eb.StartSidecar(context.Background(), r); !errors.Is(err, errRootfsRefused) || p != nil { |
| + t.Errorf("runtime %q: StartSidecar = %v, %v; want a refusal", runtime, p, err) |
| + } |
| + } |
| + // A flat rootfs row: the binary co-spawn would exec the live link. |
| + flat := rt |
| + flat.DBPath, flat.Socket = provision.DefaultDBPath(eb.DataDir, flat.Host), filepath.Join(eb.RunDirOverride, flat.Host+".sock") |
| + if l, _ := eb.Layout(flat); l != LayoutFlat { |
| + t.Fatalf("fixture layout %v, want flat", l) |
| + } |
| + if p, err := eb.StartSidecar(context.Background(), flat); !errors.Is(err, errRootfsRefused) || p != nil { |
| + t.Errorf("flat rootfs row: StartSidecar = %v, %v; want a refusal", p, err) |
| + } |
| + if calls := ctl.callList(); len(calls) != 0 { |
| + t.Fatalf("root was asked %v", calls) |
| + } |
| +} |
| + |
| +// Task 10 finding F2: a flat rootfs row reached StartStaged's binary arm |
| +// and exec'd its image as a program. The runtime is asked first: a rootfs |
| +// row on any layout but nested, and a row with a runtime this binary does |
| +// not know on any layout, is refused before anything execs or root is |
| +// asked. |
| +func TestStartStagedNeverExecsARuntimeRowOffItsPath(t *testing.T) { |
| + t.Parallel() |
| + nested, nctl, nrt := newStagedRootfsRig(t) |
| + flat := testExecBackend(t) |
| + flat.RunDirOverride = t.TempDir() |
| + host := "flat.example.com" |
| + stageLive(t, flat, host, "v1") |
| + frt := registry.Route{Host: host, Kind: "instance", DBPath: filepath.Join(flat.DataDir, host+".db"), Socket: filepath.Join(flat.RunDirOverride, host+".sock")} |
| + foreign := frt |
| + foreign.Socket = filepath.Join(t.TempDir(), "elsewhere.sock") |
| + mark := filepath.Join(t.TempDir(), "ran") |
| + id := markerScript(t, t.TempDir(), "v2", mark) |
| + for _, c := range []struct { |
| + name string |
| + b *ExecBackend |
| + rt registry.Route |
| + runtime string |
| + want error |
| + }{ |
| + {"flat rootfs", flat, frt, registry.RuntimeRootfsV1, errRootfsRefused}, |
| + {"foreign rootfs", flat, foreign, registry.RuntimeRootfsV1, errRootfsRefused}, |
| + {"flat unknown runtime", flat, frt, "rootfs-v9", errRuntimeUnknown}, |
| + {"foreign unknown runtime", flat, foreign, "rootfs-v9", errRuntimeUnknown}, |
| + {"nested unknown runtime", nested, nrt, "rootfs-v9", errRuntimeUnknown}, |
| + } { |
| + rt := c.rt |
| + rt.Runtime = c.runtime |
| + _, p, err := c.b.StartStaged(context.Background(), rt, Gen{N: 5, Build: "v2", StagedPath: id.StagedPath, Digest: id.Digest}) |
| + if !errors.Is(err, c.want) || !errors.Is(err, errRootfsRefused) || p != nil { |
| + t.Errorf("%s: StartStaged = %v, %v; want a refusal (%v)", c.name, p, err, c.want) |
| + } |
| + if v := verdictOf(err); v != gateNoOwnership { |
| + t.Errorf("%s: verdictOf = %d, want gateNoOwnership", c.name, v) |
| + } |
| + } |
| + time.Sleep(200 * time.Millisecond) |
| + if _, err := os.Stat(mark); err == nil { |
| + t.Fatal("a runtime row's staged path was exec'd as a binary") |
| + } |
| + if calls := nctl.callList(); len(calls) != 0 { |
| + t.Fatalf("root was asked %v", calls) |
| + } |
| +} |
| + |
| +// The two binary launch funnels, a nested unit's (nestedGate) and a flat |
| +// child's (spawnInCgroupGated), refuse a row with any runtime before |
| +// anything is built or root is asked: rootfs-v1 as before, and a runtime |
| +// this agent does not know (Plan 3 N1, Task 2's carry). |
| +func TestBinaryLaunchFunnelsRefuseEveryRuntime(t *testing.T) { |
| + t.Parallel() |
| + b, ctl, rt := newStagedRootfsRig(t) |
| + for _, c := range []struct { |
| + runtime string |
| + want error |
| + }{{registry.RuntimeRootfsV1, errRootfsBinaryLaunch}, {"rootfs-v9", errRuntimeUnknown}} { |
| + r := rt |
| + r.Runtime = c.runtime |
| + if err := b.nestedGate(r); !errors.Is(err, c.want) || !errors.Is(err, errRootfsRefused) { |
| + t.Errorf("nestedGate, runtime %q: %v, want a refusal (%v)", c.runtime, err, c.want) |
| + } |
| + built := false |
| + if _, _, err := b.spawnInCgroupGated(r, cgroup.Limit{}, func() *exec.Cmd { built = true; return exec.Command("true") }, nil); !errors.Is(err, errRootfsRefused) || built { |
| + t.Errorf("spawnInCgroupGated, runtime %q: %v (built %v), want a refusal before the build", c.runtime, err, built) |
| + } |
| + } |
| + if calls := ctl.callList(); len(calls) != 0 { |
| + t.Fatalf("root was asked %v", calls) |
| + } |
| +} |
diff --git a/internal/activator/rootfs_test.go b/internal/activator/rootfs_test.go| index 2004d3fda..c34926724 100644 |
| --- a/internal/activator/rootfs_test.go |
| +++ b/internal/activator/rootfs_test.go |
| @@ -1063,8 +1063,9 @@ func TestRootfsRowsNeverTakeABinaryLaunch(t *testing.T) { |
| // Fix round 1, item 2: a rootfs row's swap or hold on a box that refuses it |
| // is a refusal of the start, never a failure of the build. The swap's |
| -// StartStaged reaches the binary nested spawn, which refuses a rootfs row |
| -// before root is asked; both modes are abandoned with nothing executed. |
| +// StartStaged takes its rootfs arm (Plan 3 N1), whose rootfsGates refuse |
| +// on a box with no rootfs probe before root is asked; both modes are |
| +// abandoned with nothing executed. |
| func TestARootfsSwapOrHoldOnARefusingBoxIsARefusal(t *testing.T) { |
| t.Parallel() |
| for _, mode := range []SwapMode{ModeOverlap, ModeHold} { |
diff --git a/internal/activator/swap.go b/internal/activator/swap.go| index 21668c578..f28fdab61 100644 |
| --- a/internal/activator/swap.go |
| +++ b/internal/activator/swap.go |
| @@ -354,15 +354,17 @@ func (a *Service) attemptCtx(ctx context.Context) (context.Context, context.Canc |
| } |
| // genHealthy is the successor's health check, through its own handle: |
| -// GET / below 500 within spawnHealthTimeout (spec §7.5 step 4). |
| -func (a *Service) genHealthy(ctx context.Context, g Gen) error { |
| +// GET / below 500 within healthBoundFor(rt) (spec §7.5 step 4): 30 s for a |
| +// binary row, the rootfs web process's 120 s for a rootfs row. StartStaged |
| +// waits for nothing, so this is the generation's one health wait. |
| +func (a *Service) genHealthy(ctx context.Context, rt registry.Route, g Gen) error { |
| if a.genHealth != nil { |
| return a.genHealth(ctx, g) |
| } |
| if g.Socket == "" { |
| return errors.New("generation has no socket") |
| } |
| - return waitHealthyCtx(ctx, g.Socket, spawnHealthTimeout) |
| + return waitHealthyCtx(ctx, g.Socket, healthBoundFor(rt)) |
| } |
| // spawnSuccessor is §7.5 step 3 in both modes. Claims off, it is the |
| @@ -579,7 +581,7 @@ func (a *Service) swapOverlap(ctx context.Context, gb SwapBackend, at Attempt, n |
| } |
| // Step 4: the health check, through the generation's own handle. |
| - if err := a.genHealthy(mctx, m.gen); err != nil { |
| + if err := a.genHealthy(mctx, rt, m.gen); err != nil { |
| a.dropMember(e, m) |
| if res, over := stepResult(mctx, err); over { |
| return res |
diff --git a/internal/activator/swaphold.go b/internal/activator/swaphold.go| index 279606566..a14466e0d 100644 |
| --- a/internal/activator/swaphold.go |
| +++ b/internal/activator/swaphold.go |
| @@ -430,8 +430,8 @@ func (a *Service) startPinned(ctx context.Context, rt registry.Route, pin Gen) ( |
| // |
| // healthBy, when set, caps the health check (zero-downtime spec §8.2): a |
| // first wake's B is checked until the earlier of healthBy (the waiters' |
| -// deadline minus 25 s) and spawnHealthTimeout, which genHealthy applies |
| -// itself. StartStaged returns the process at once, so this is the only |
| +// deadline minus 25 s) and the row's health bound (healthBoundFor: 30 s, |
| +// or 120 s for a rootfs row), which genHealthy applies itself. StartStaged returns the process at once, so this is the only |
| // health bound, and a B that misses it comes back with its process for the |
| // caller to record and kill. Zero leaves the check as it was. |
| func (a *Service) startPinnedAt(ctx context.Context, rt registry.Route, pin Gen, gate func() error, healthBy time.Time) (Proc, string, Gen, error) { |
| @@ -478,7 +478,7 @@ func (a *Service) startPinnedAt(ctx context.Context, rt registry.Route, pin Gen, |
| hctx, cancel = context.WithDeadline(ctx, healthBy) |
| defer cancel() |
| } |
| - if err := a.genHealthy(hctx, g); err != nil { |
| + if err := a.genHealthy(hctx, rt, g); err != nil { |
| return proc, "", g, err |
| } |
| log.Printf("activator: %s: generation %d of %s: record %s, start %s, healthy %s later", rt.Host, g.N, g.Build, |
diff --git a/internal/activator/wakeplan.go b/internal/activator/wakeplan.go| index 0774ac1d1..0419da74e 100644 |
| --- a/internal/activator/wakeplan.go |
| +++ b/internal/activator/wakeplan.go |
| @@ -379,7 +379,7 @@ func (a *Service) spawnPlanned(ctx context.Context, e *actEntry, rt registry.Rou |
| var left time.Duration |
| if canFall { |
| healthBy = e.wakeStart.Add(firstWakeBudget - firstWakeKillReserve) |
| - gate = verdictFloorGate(gate, healthBy, &left) |
| + gate = verdictFloorGate(gate, healthBy, healthBoundFor(rt), &left) |
| } |
| // Every exec here that runs the good build in another build's place is |
| // recorded with the planner's selection (ruling R-PLANNED-RECOVERY): a |
| @@ -470,18 +470,19 @@ func (a *Service) spawnPlanned(ctx context.Context, e *actEntry, rt registry.Rou |
| var errNoTimeForVerdict = errors.New("too little of the first wake's health window is left for a verdict") |
| // verdictFloorGate is gate with the first-wake floor asked after it, at |
| -// the exec: B's window is the earlier of exec + spawnHealthTimeout and |
| -// healthBy, less now. Below firstWakeMinVerdict it refuses with |
| +// the exec: B's window is the earlier of exec + bound and healthBy, less |
| +// now. bound is the row's health bound (healthBoundFor), the one |
| +// genHealthy will apply. Below firstWakeMinVerdict it refuses with |
| // errNoTimeForVerdict and stores the window in *left for the log. The |
| // ownership, lease and plan checks go first, so their refusal (abandonment) |
| // is never mistaken for a lack of time. |
| -func verdictFloorGate(gate func() error, healthBy time.Time, left *time.Duration) func() error { |
| +func verdictFloorGate(gate func() error, healthBy time.Time, bound time.Duration, left *time.Duration) func() error { |
| return func() error { |
| if err := gate(); err != nil { |
| return err |
| } |
| now := time.Now() |
| - end := now.Add(spawnHealthTimeout) |
| + end := now.Add(bound) |
| if healthBy.Before(end) { |
| end = healthBy |
| } |
diff --git a/internal/instancectl/client.go b/internal/instancectl/client.go| index cc88ada4d..6c37e9962 100644 |
| --- a/internal/instancectl/client.go |
| +++ b/internal/instancectl/client.go |
| @@ -480,7 +480,11 @@ func (c *Client) StopRootfs(ctx context.Context, host string, role Role, gen uin |
| // CmdRootfs starts host's rootfs cmd unit (run spec §5.4 for a rootfs |
| // row): img.Process names a declared process (release, or a worker for a |
| // one-off run), or img.Argv is an operator's command. It returns the new |
| -// invocation. |
| +// invocation. Root's answers given only before systemctl start are typed |
| +// as startRootfsQuery types /start's (rootfs apps Plan 3 N1): a 403 is |
| +// ErrStagedImage, and 400, 409, 501, 503 and 507 are ErrStartRefused. |
| +// Anything else, a 502 and a transport failure included, may follow a |
| +// start and stays plain. |
| func (c *Client) CmdRootfs(ctx context.Context, host string, memoryMax int64, img StagedImage) (string, error) { |
| q := hostRole(host, "") |
| q.Set("memory_max", strconv.FormatInt(memoryMax, 10)) |
| @@ -497,8 +501,11 @@ func (c *Client) CmdRootfs(ctx context.Context, host string, memoryMax int64, im |
| var out StartResponse |
| status, _, err := c.doBody(ctx, DeadlineControl, http.MethodPost, "/cmd", q, body, &out) |
| if err != nil { |
| - if status == http.StatusForbidden { |
| + switch { |
| + case status == http.StatusForbidden: |
| return "", fmt.Errorf("%w: %v", ErrStagedImage, err) |
| + case startRefused(status): |
| + return "", fmt.Errorf("%w: %v", ErrStartRefused, err) |
| } |
| return "", err |
| } |