activator: the size column is the tier source for every spawn and the claim snapshot
SizedLimit(rt, fallback) returns the size column's row when rt.Size names one, else the build's tier; an unknown name logs once per host and value. tier(rt) and GenTier(rt, build) take it, so run, sidecar, seed, cmd, parent, generation and the claim snapshot all carry the effective size. The bound unit's snapshot falls back to TierDefault as before. EffectiveSize(gb, rt, build) is the package function of ruling C1, tested through an *ExecBackend and a *SplitBackend (#329).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
8 files changed,
+289
−7
internal/activator/backend_exec.go+4 −3internal/activator/backend_exec_test.go+113 −0internal/activator/claim_snapshot_test.go+42 −0internal/activator/genbackend.go+4 −3internal/activator/genbackend_test.go+32 −0internal/activator/size.go+47 −0internal/host/claimunit_inputs.go+1 −1internal/host/claimunit_inputs_test.go+46 −0
diff --git a/internal/activator/backend_exec.go b/internal/activator/backend_exec.go| index d6c81c739..f14eb9d98 100644 |
| --- a/internal/activator/backend_exec.go |
| +++ b/internal/activator/backend_exec.go |
| @@ -414,12 +414,13 @@ func (b *ExecBackend) teeStd(cmd *exec.Cmd, rt registry.Route) { |
| var _ Backend = (*ExecBackend)(nil) |
| -// tier is Tier with the nil default applied. |
| +// tier is the limit rt's live build runs at: the size column when it names |
| +// a row (#329), else Tier with the nil default applied (the build's tier). |
| func (b *ExecBackend) tier(rt registry.Route) cgroup.Limit { |
| if b.Tier == nil { |
| - return cgroup.TierDefault |
| + return SizedLimit(rt, cgroup.TierDefault) |
| } |
| - return b.Tier(rt) |
| + return SizedLimit(rt, b.Tier(rt)) |
| } |
| // spawnInCgroup is the ONE seam all three child shapes — seed, instance, |
diff --git a/internal/activator/backend_exec_test.go b/internal/activator/backend_exec_test.go| index 5c8de257f..6bf730c5d 100644 |
| --- a/internal/activator/backend_exec_test.go |
| +++ b/internal/activator/backend_exec_test.go |
| @@ -4,6 +4,7 @@ import ( |
| "context" |
| "database/sql" |
| "errors" |
| + "log" |
| "os" |
| "os/exec" |
| "path/filepath" |
| @@ -38,6 +39,118 @@ func TestExecBackendTierSeam(t *testing.T) { |
| } |
| } |
| +// The size column (#329) is the tier source: a named size wins over the |
| +// build's tier from the Tier seam, an empty column is the build's tier as |
| +// before, and a name this box does not know falls back to the build's tier. |
| +func TestTierSeamPrefersTheSizeColumn(t *testing.T) { |
| + t.Parallel() |
| + b := &ExecBackend{Tier: func(registry.Route) cgroup.Limit { return cgroup.TierChildren }} |
| + for _, c := range []struct { |
| + size string |
| + want int64 |
| + }{ |
| + {"", cgroup.TierChildren.MemoryMax}, |
| + {"small", 256 << 20}, |
| + {"xlarge", 2 << 30}, |
| + {"xxlarge", cgroup.TierChildren.MemoryMax}, |
| + } { |
| + rt := registry.Route{Host: "tierseam-size.example.com", Size: c.size} |
| + got := b.tier(rt) |
| + if got.MemoryMax != c.want { |
| + t.Fatalf("size %q: tier MemoryMax %d, want %d", c.size, got.MemoryMax, c.want) |
| + } |
| + if _, ok := cgroup.SizeOf(got); !ok { |
| + t.Fatalf("size %q: tier %+v is not a row of the size table", c.size, got) |
| + } |
| + } |
| + // With no Tier seam the fallback is the default tier. |
| + if got := (&ExecBackend{}).tier(registry.Route{Host: "tierseam-size.example.com", Size: "large"}); got != cgroup.TierChildren { |
| + t.Fatalf("nil Tier, size large: %+v, want large", got) |
| + } |
| +} |
| + |
| +// An unknown size name logs once per host and value, not on every spawn. |
| +// Not parallel: it captures the package log output. |
| +func TestSizedLimitLogsAnUnknownSizeOnce(t *testing.T) { |
| + var buf strings.Builder |
| + var mu sync.Mutex |
| + log.SetOutput(writerFunc(func(p []byte) (int, error) { |
| + mu.Lock() |
| + defer mu.Unlock() |
| + return buf.Write(p) |
| + })) |
| + t.Cleanup(func() { log.SetOutput(os.Stderr) }) |
| + rt := registry.Route{Host: "sizedlimit-once.example.com", Size: "huge"} |
| + for range 3 { |
| + if got := SizedLimit(rt, cgroup.TierDefault); got != cgroup.TierDefault { |
| + t.Fatalf("unknown size: %+v, want the fallback", got) |
| + } |
| + } |
| + other := rt |
| + other.Size = "giant" |
| + SizedLimit(other, cgroup.TierDefault) |
| + mu.Lock() |
| + out := buf.String() |
| + mu.Unlock() |
| + line := `activator: sizedlimit-once.example.com: size "huge" is not one this box knows; using the build's tier` |
| + if n := strings.Count(out, line); n != 1 { |
| + t.Fatalf("log has %d copies of %q, want 1:\n%s", n, line, out) |
| + } |
| + if !strings.Contains(out, `size "giant" is not one`) { |
| + t.Fatalf("a second unknown value was not logged:\n%s", out) |
| + } |
| +} |
| + |
| +type writerFunc func([]byte) (int, error) |
| + |
| +func (f writerFunc) Write(p []byte) (int, error) { return f(p) } |
| + |
| +// EffectiveSize is the size row the box spawns rt at (ruling C1): through |
| +// an *ExecBackend, and through a *SplitBackend, production's backend, which |
| +// delegates an exec row and answers TierDefault for a unit-backed row. |
| +func TestEffectiveSizeThroughExecAndSplitBackends(t *testing.T) { |
| + t.Parallel() |
| + eb := &ExecBackend{ |
| + Tier: func(registry.Route) cgroup.Limit { return cgroup.TierDefault }, |
| + TierOf: func(registry.Route, string) cgroup.Limit { return cgroup.TierChildren }, |
| + } |
| + split := &SplitBackend{Exec: eb} |
| + for _, c := range []struct { |
| + name string |
| + gb interface { |
| + GenTier(registry.Route, string) cgroup.Limit |
| + } |
| + size string |
| + build string |
| + want string |
| + }{ |
| + {"exec, size column", eb, "small", "v1", "small"}, |
| + {"exec, build's tier", eb, "", "v1", "large"}, |
| + {"exec, live tier", eb, "", "", "standard"}, |
| + {"exec, unknown size", eb, "xxlarge", "v1", "large"}, |
| + {"split, size column", split, "xlarge", "v1", "xlarge"}, |
| + {"split, build's tier", split, "", "v1", "large"}, |
| + } { |
| + rt := registry.Route{Host: "effsize.example.com", Size: c.size} |
| + if got := EffectiveSize(c.gb, rt, c.build); got.Name != c.want { |
| + t.Fatalf("%s: EffectiveSize = %q, want %q", c.name, got.Name, c.want) |
| + } |
| + } |
| + unit := registry.Route{Host: "effsize-unit.example.com", Backing: registry.BackingUnit} |
| + if got := EffectiveSize(split, unit, "v1"); got.Name != "standard" { |
| + t.Fatalf("unit row: EffectiveSize = %q, want standard", got.Name) |
| + } |
| + // A tier that is no row of the table reads as standard. |
| + odd := genTierFunc(func(registry.Route, string) cgroup.Limit { return cgroup.Limit{MemoryMax: 7} }) |
| + if got := EffectiveSize(odd, registry.Route{Host: "effsize.example.com"}, ""); got.Name != "standard" { |
| + t.Fatalf("non-size tier: EffectiveSize = %q, want standard", got.Name) |
| + } |
| +} |
| + |
| +type genTierFunc func(registry.Route, string) cgroup.Limit |
| + |
| +func (f genTierFunc) GenTier(rt registry.Route, build string) cgroup.Limit { return f(rt, build) } |
| + |
| // TestSpawnInCgroupAppliesTheTier: the seam every spawn site uses really |
| // writes the route's tier into the host cgroup — a regression that swapped |
| // a site back to a bare Cgroups.Start(…, cgroup.Limit{}, …) compiles fine |
diff --git a/internal/activator/claim_snapshot_test.go b/internal/activator/claim_snapshot_test.go| new file mode 100644 |
| index 000000000..779e002ef |
| --- /dev/null |
| +++ b/internal/activator/claim_snapshot_test.go |
| @@ -0,0 +1,42 @@ |
| +package activator |
| + |
| +import ( |
| + "os" |
| + "path/filepath" |
| + "strings" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/cgroup" |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +// The claim snapshot's Limit is the effective size (#329): the size column |
| +// when it names a row, the Tier seam's (the build's) tier when it is empty. |
| +func TestClaimSnapshotCarriesTheEffectiveSize(t *testing.T) { |
| + t.Parallel() |
| + const host = "snapshot-size.example.com" |
| + data, opt := t.TempDir(), t.TempDir() |
| + b := &ExecBackend{DataDir: data, OptDir: opt, Tier: func(registry.Route) cgroup.Limit { return cgroup.TierChildren }} |
| + stageLiveUnderAccount(t, opt, host, "bab", "v1", "jam") |
| + if err := os.WriteFile(filepath.Join(data, host+".env"), []byte("CARLOS_ADMIN_TOKEN="+strings.Repeat("ab", 32)+"\n"), 0o600); err != nil { |
| + t.Fatal(err) |
| + } |
| + rt := registry.Route{Host: host, Socket: filepath.Join(t.TempDir(), "app.sock"), DBPath: filepath.Join(data, host+".db")} |
| + small, _ := cgroup.SizeByName("small") |
| + for _, c := range []struct { |
| + size string |
| + want cgroup.Limit |
| + }{ |
| + {"small", small.Limit}, |
| + {"", cgroup.TierChildren}, |
| + } { |
| + rt.Size = c.size |
| + snap, err := b.SnapshotClaimLaunch(t.Context(), rt) |
| + if err != nil { |
| + t.Fatalf("size %q: %v", c.size, err) |
| + } |
| + if snap.Limit != c.want || (c.size == "small" && snap.Limit.MemoryMax != 256<<20) { |
| + t.Fatalf("size %q: snapshot Limit %+v, want %+v", c.size, snap.Limit, c.want) |
| + } |
| + } |
| +} |
diff --git a/internal/activator/genbackend.go b/internal/activator/genbackend.go| index 606601454..5f0a5f2cd 100644 |
| --- a/internal/activator/genbackend.go |
| +++ b/internal/activator/genbackend.go |
| @@ -276,11 +276,12 @@ func (b *ExecBackend) StartStaged(ctx context.Context, rt registry.Route, g Gen) |
| return g, nil, errSwapLayout |
| } |
| -// GenTier is build's own tier: TierOf when it is wired (the agent reads |
| -// the build's staged manifest), the live tier otherwise. |
| +// GenTier is the limit a generation of build runs at: the size column when |
| +// it names a row (#329), else build's own tier, TierOf when it is wired |
| +// (the agent reads the build's staged manifest), the live tier otherwise. |
| func (b *ExecBackend) GenTier(rt registry.Route, build string) cgroup.Limit { |
| if b.TierOf != nil && build != "" { |
| - return b.TierOf(rt, build) |
| + return SizedLimit(rt, b.TierOf(rt, build)) |
| } |
| return b.tier(rt) |
| } |
diff --git a/internal/activator/genbackend_test.go b/internal/activator/genbackend_test.go| index 1847e5a97..5dac8fc54 100644 |
| --- a/internal/activator/genbackend_test.go |
| +++ b/internal/activator/genbackend_test.go |
| @@ -236,6 +236,38 @@ func TestExecBackendStartStagedClearsALeftoverCanonicalLink(t *testing.T) { |
| } |
| } |
| +// The size column (#329) overrides a generation's build tier; an empty |
| +// column leaves the build's own tier (TierOf), as before. With no TierOf |
| +// the column still wins over the live tier. |
| +func TestGenTierPrefersTheSizeColumnOverTheBuild(t *testing.T) { |
| + t.Parallel() |
| + b := &ExecBackend{ |
| + Tier: func(registry.Route) cgroup.Limit { return cgroup.TierChildren }, |
| + TierOf: func(registry.Route, string) cgroup.Limit { return cgroup.TierDefault }, |
| + } |
| + rt := registry.Route{Host: "gentier-size.example.com"} |
| + if got := b.GenTier(rt, "v2"); got != cgroup.TierDefault { |
| + t.Fatalf("empty size: GenTier(v2) = %+v, want the build's TierDefault", got) |
| + } |
| + rt.Size = "large" |
| + if got := b.GenTier(rt, "v2"); got != cgroup.TierChildren { |
| + t.Fatalf("size large: GenTier(v2) = %+v, want TierChildren", got) |
| + } |
| + rt.Size = "small" |
| + if got := b.GenTier(rt, ""); got.MemoryMax != 256<<20 { |
| + t.Fatalf("size small, no build: GenTier = %+v, want 256 MiB", got) |
| + } |
| + rt.Size = "xxlarge" |
| + if got := b.GenTier(rt, "v2"); got != cgroup.TierDefault { |
| + t.Fatalf("unknown size: GenTier(v2) = %+v, want the build's TierDefault", got) |
| + } |
| + // SplitBackend delegates an exec row unchanged. |
| + rt.Size = "xlarge" |
| + if got := (&SplitBackend{Exec: b}).GenTier(rt, "v2"); got.MemoryMax != 2<<30 { |
| + t.Fatalf("split, size xlarge: GenTier = %+v, want 2 GiB", got) |
| + } |
| +} |
| + |
| // Each artifact's own tier (spec §7.3): a staged child gets g.Tier, not the |
| // live link's tier, and the overlap parent is the incumbent's tier plus the |
| // successor's. Here the live link (b.Tier) says 512 MiB throughout, while |
diff --git a/internal/activator/size.go b/internal/activator/size.go| new file mode 100644 |
| index 000000000..3f116520c |
| --- /dev/null |
| +++ b/internal/activator/size.go |
| @@ -0,0 +1,47 @@ |
| +package activator |
| + |
| +import ( |
| + "log" |
| + "sync" |
| + |
| + "github.com/carlosframework/platform/internal/cgroup" |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +// unknownSizes holds each host and size value SizedLimit has already |
| +// logged, so a name this box does not know is said once, not on every |
| +// spawn. |
| +var unknownSizes sync.Map |
| + |
| +// SizedLimit is the limit rt runs at (#329, spec §5): the size column's row |
| +// when rt.Size names one, else fallback, the build's tier. An empty column |
| +// is fallback exactly, so a box with no policy runs as it did before. A |
| +// non-empty name this box does not know (a newer console's size) logs once |
| +// per host and value and falls back too. |
| +func SizedLimit(rt registry.Route, fallback cgroup.Limit) cgroup.Limit { |
| + if rt.Size == "" { |
| + return fallback |
| + } |
| + if s, ok := cgroup.SizeByName(rt.Size); ok { |
| + return s.Limit |
| + } |
| + if _, seen := unknownSizes.LoadOrStore(rt.Host+"\x00"+rt.Size, struct{}{}); !seen { |
| + log.Printf("activator: %s: size %q is not one this box knows; using the build's tier", rt.Host, rt.Size) |
| + } |
| + return fallback |
| +} |
| + |
| +// EffectiveSize is the size row rt's build runs at on this box: |
| +// SizeOf(gb.GenTier(rt, build)), standard when the tier is no row. gb is |
| +// the backend the activator already spawns through: production's |
| +// *SplitBackend (which answers TierDefault for a unit-backed row) or an |
| +// *ExecBackend. |
| +func EffectiveSize(gb interface { |
| + GenTier(registry.Route, string) cgroup.Limit |
| +}, rt registry.Route, build string) cgroup.Size { |
| + if s, ok := cgroup.SizeOf(gb.GenTier(rt, build)); ok { |
| + return s |
| + } |
| + s, _ := cgroup.SizeByName("standard") |
| + return s |
| +} |
diff --git a/internal/host/claimunit_inputs.go b/internal/host/claimunit_inputs.go| index 1f2c6fcac..071d79dc1 100644 |
| --- a/internal/host/claimunit_inputs.go |
| +++ b/internal/host/claimunit_inputs.go |
| @@ -71,7 +71,7 @@ func claimBoundSnapshot(ctx context.Context, backend *activator.ExecBackend, rt |
| if id.Version == "" || !claimHexDigest(id.Digest) { |
| return out, hostbox.ErrChanged |
| } |
| - out = ClaimServingLaunch{Unit: unit, Identity: hostwrite.Identity{Config: unit.Program, Release: id.Digest}, Executable: release.LiveLink(backend.OptDir, rt.Host), ExecutableSHA256: id.Digest, Args: unit.Args, Env: unit.Env, Version: id.Version, Staged: id, Limit: cgroup.TierDefault, Roles: []string{"main"}} |
| + out = ClaimServingLaunch{Unit: unit, Identity: hostwrite.Identity{Config: unit.Program, Release: id.Digest}, Executable: release.LiveLink(backend.OptDir, rt.Host), ExecutableSHA256: id.Digest, Args: unit.Args, Env: unit.Env, Version: id.Version, Staged: id, Limit: activator.SizedLimit(rt, cgroup.TierDefault), Roles: []string{"main"}} |
| return out, nil |
| } |
diff --git a/internal/host/claimunit_inputs_test.go b/internal/host/claimunit_inputs_test.go| index a2c05eaa3..269903dda 100644 |
| --- a/internal/host/claimunit_inputs_test.go |
| +++ b/internal/host/claimunit_inputs_test.go |
| @@ -7,10 +7,13 @@ import ( |
| "encoding/json" |
| "github.com/carlosframework/platform/internal/provision" |
| "os" |
| + "path/filepath" |
| "strings" |
| "testing" |
| "time" |
| + "github.com/carlosframework/platform/internal/activator" |
| + "github.com/carlosframework/platform/internal/cgroup" |
| "github.com/carlosframework/platform/internal/hostbox" |
| "github.com/carlosframework/platform/internal/hostguard" |
| "github.com/carlosframework/platform/internal/registry" |
| @@ -107,3 +110,46 @@ func TestPlan14BoundColdSelection(t *testing.T) { |
| t.Fatal("bound selection minted ordinary environment", err) |
| } |
| } |
| + |
| +// The bound unit's snapshot carries the size column (#329) when it names a |
| +// row; an empty column keeps TierDefault, byte-identical to main, not the |
| +// build's tier. |
| +func TestClaimBoundSnapshotCarriesTheSizeColumn(t *testing.T) { |
| + t.Parallel() |
| + const host = "bound-size.example.com" |
| + opt := t.TempDir() |
| + dir := filepath.Join(opt, "versions", "v1", "bab") |
| + if err := os.MkdirAll(dir, 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.WriteFile(filepath.Join(dir, "jam"), []byte("#!/bin/sh\n"), 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.MkdirAll(filepath.Join(opt, "live"), 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.Symlink(filepath.Join(dir, "jam"), filepath.Join(opt, "live", host)); err != nil { |
| + t.Fatal(err) |
| + } |
| + unit := &hostbox.UnitLaunchInputs{Unit: "console.service", Addr: "127.0.0.1:9000", DBPath: filepath.Join(provision.InstanceDir("/data", host), "app.db"), Program: strings.Repeat("ab", 32)} |
| + read := func(context.Context, registry.Route) (*hostbox.UnitLaunchInputs, error) { return unit, nil } |
| + backend := &activator.ExecBackend{OptDir: opt, Tier: func(registry.Route) cgroup.Limit { return cgroup.TierChildren }} |
| + xlarge, _ := cgroup.SizeByName("xlarge") |
| + for _, c := range []struct { |
| + size string |
| + want cgroup.Limit |
| + }{ |
| + {"xlarge", xlarge.Limit}, |
| + {"", cgroup.TierDefault}, |
| + {"xxlarge", cgroup.TierDefault}, |
| + } { |
| + rt := registry.Route{Host: host, Unit: "console.service", Size: c.size} |
| + out, err := claimBoundSnapshot(t.Context(), backend, rt, read) |
| + if err != nil { |
| + t.Fatalf("size %q: %v", c.size, err) |
| + } |
| + if out.Limit != c.want { |
| + t.Fatalf("size %q: bound Limit %+v, want %+v", c.size, out.Limit, c.want) |
| + } |
| + } |
| +} |