carlos / platform Public

instancectl, host: every tier validator takes the size table

parseRequest's /start, /seed and /cmd memory_max, claimTier, claimLaunchTier
and claimIsolatedPaths now accept exactly the four sizes' MemoryMax via
cgroup.SizeOf (#329). A sum of two sizes is still refused there; /parent
takes one size or two through ValidParentLimit.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev a16f2770e8b7afbf4ddf78600d6d4d6bea225f43 parent d2949af
9 files changed, +83 −11
  • internal/host/claimlaunch_root.go +2 −1
  • internal/host/claimlaunch_root_test.go +14 −0
  • internal/host/claimlaunch_transport_test.go +8 −0
  • internal/host/claimroleassembly.go +2 −2
  • internal/instancectl/claimunit.go +3 −4
  • internal/instancectl/claimunit_test.go +13 −0
  • internal/instancectl/server.go +2 −4
  • internal/instancectl/server_gen_test.go +16 −0
  • internal/instancectl/server_test.go +23 −0
diff --git a/internal/host/claimlaunch_root.go b/internal/host/claimlaunch_root.go
index f64f98684..7284f4a9d 100644
--- a/internal/host/claimlaunch_root.go
+++ b/internal/host/claimlaunch_root.go
@@ -161,7 +161,8 @@ func claimLaunchAuthority(c *hostclaim.Claim, t hostguard.FenceTarget, box strin
}
func claimLaunchTier(n int64) bool {
- return n == cgroup.TierDefault.MemoryMax || n == cgroup.TierChildren.MemoryMax
+ _, ok := cgroup.SizeOf(cgroup.Limit{MemoryMax: n})
+ return ok
}
// launchRecord is the prelaunch record of exactly t, written for the live
diff --git a/internal/host/claimlaunch_root_test.go b/internal/host/claimlaunch_root_test.go
index 136b12c1d..60d24ded7 100644
--- a/internal/host/claimlaunch_root_test.go
+++ b/internal/host/claimlaunch_root_test.go
@@ -192,6 +192,20 @@ func (f *launchFixture) noReservation(t *testing.T, name string) {
}
}
+// TestClaimLaunchTierTakesEverySize: a claim launch's MemoryMax is exactly
+// one size's; a sum of two sizes is not a size (#329).
+func TestClaimLaunchTierTakesEverySize(t *testing.T) {
+ t.Parallel()
+ for _, n := range []int64{268435456, 536870912, 1073741824, 2147483648} {
+ if !claimLaunchTier(n) {
+ t.Errorf("claimLaunchTier(%d) = false, want true", n)
+ }
+ }
+ if claimLaunchTier(805306368) {
+ t.Error("claimLaunchTier(805306368) took a sum of two sizes")
+ }
+}
+
func TestClaimLaunchRootStartsExactlyTheReservedRole(t *testing.T) {
f := newLaunchFixture(t, servingOp())
resp, err := f.root.Serve(t.Context(), f.peer, f.req)
diff --git a/internal/host/claimlaunch_transport_test.go b/internal/host/claimlaunch_transport_test.go
index 80547849c..2c0f93004 100644
--- a/internal/host/claimlaunch_transport_test.go
+++ b/internal/host/claimlaunch_transport_test.go
@@ -209,11 +209,19 @@ func TestClaimRoleIsolatedPathsBindTheNestedInstance(t *testing.T) {
if got.LaunchDir != "/data/instances/"+iidOf(host) || got.RunDir != "/run/carlos/instances/"+iidOf(host) || !strings.HasSuffix(got.LogSocket, ".run.sock") || got.MemoryMax != cgroup.TierDefault.MemoryMax {
t.Fatalf("paths=%+v", got)
}
+ for _, name := range []string{"small", "xlarge"} {
+ size, _ := cgroup.SizeByName(name)
+ got, err := claimIsolatedPaths(roots, host, ClaimLaunchInputs{DBPath: "/data/instances/" + iidOf(host) + "/shop.db", Limit: size.Limit})
+ if err != nil || got.MemoryMax != size.Limit.MemoryMax {
+ t.Fatalf("%s: MemoryMax=%d, %v; want %d", name, got.MemoryMax, err, size.Limit.MemoryMax)
+ }
+ }
for name, in := range map[string]ClaimLaunchInputs{
"flat database": {DBPath: "/data/shop.example.db", Limit: cgroup.TierDefault},
"other instance": {DBPath: "/data/instances/other/shop.db", Limit: cgroup.TierDefault},
"nested deeper": {DBPath: "/data/instances/" + iidOf(host) + "/x/shop.db", Limit: cgroup.TierDefault},
"not a tier": {DBPath: "/data/instances/" + iidOf(host) + "/shop.db", Limit: cgroup.Limit{MemoryMax: 7}},
+ "a sum": {DBPath: "/data/instances/" + iidOf(host) + "/shop.db", Limit: cgroup.Limit{MemoryMax: 805306368}},
"relative": {DBPath: "instances/shop.db", Limit: cgroup.TierDefault},
"unescaped parent": {DBPath: "/data/instances/" + iidOf(host) + "/../x/shop.db", Limit: cgroup.TierDefault},
} {
diff --git a/internal/host/claimroleassembly.go b/internal/host/claimroleassembly.go
index 9308f8eba..3b716c31f 100644
--- a/internal/host/claimroleassembly.go
+++ b/internal/host/claimroleassembly.go
@@ -119,8 +119,8 @@ type claimIsolatedLaunch struct {
// the host's instance directory, and a platform tier.
func claimIsolatedPaths(roots map[string]string, host string, in ClaimLaunchInputs) (claimIsolatedLaunch, error) {
data := roots["data"]
- if !filepath.IsAbs(data) || !provision.ValidHostShape(host) || !filepath.IsAbs(in.DBPath) || filepath.Clean(in.DBPath) != in.DBPath ||
- (in.Limit.MemoryMax != cgroup.TierDefault.MemoryMax && in.Limit.MemoryMax != cgroup.TierChildren.MemoryMax) {
+ _, sized := cgroup.SizeOf(cgroup.Limit{MemoryMax: in.Limit.MemoryMax})
+ if !filepath.IsAbs(data) || !provision.ValidHostShape(host) || !filepath.IsAbs(in.DBPath) || filepath.Clean(in.DBPath) != in.DBPath || !sized {
return claimIsolatedLaunch{}, hostbox.ErrRoleLaunchShape
}
dir := provision.InstanceDir(data, host)
diff --git a/internal/instancectl/claimunit.go b/internal/instancectl/claimunit.go
index 34fc81ee5..166c70a5a 100644
--- a/internal/instancectl/claimunit.go
+++ b/internal/instancectl/claimunit.go
@@ -69,11 +69,10 @@ func ClaimUnitControlGroup(host string) string {
}
func claimTier(n int64) (cgroup.Limit, bool) {
- switch n {
- case cgroup.TierDefault.MemoryMax, cgroup.TierChildren.MemoryMax:
- return cgroup.Limit{MemoryMax: n}, true
+ if _, ok := cgroup.SizeOf(cgroup.Limit{MemoryMax: n}); !ok {
+ return cgroup.Limit{}, false
}
- return cgroup.Limit{}, false
+ return cgroup.Limit{MemoryMax: n}, true
}
func (u ClaimUnits) budget() time.Duration {
diff --git a/internal/instancectl/claimunit_test.go b/internal/instancectl/claimunit_test.go
index ac2278b82..981fbce15 100644
--- a/internal/instancectl/claimunit_test.go
+++ b/internal/instancectl/claimunit_test.go
@@ -116,6 +116,19 @@ func populate(t *testing.T, events string, on bool) {
}
}
+// TestClaimTierTakesEverySize: a claim unit's memory_max is exactly one
+// size's MemoryMax; a sum of two sizes is not a size (#329).
+func TestClaimTierTakesEverySize(t *testing.T) {
+ for _, n := range []int64{268435456, 536870912, 1073741824, 2147483648} {
+ if got, ok := claimTier(n); !ok || got != (cgroup.Limit{MemoryMax: n}) {
+ t.Errorf("claimTier(%d) = %+v, %v; want the limit, true", n, got, ok)
+ }
+ }
+ if _, ok := claimTier(805306368); ok {
+ t.Error("claimTier(805306368) took a sum of two sizes")
+ }
+}
+
func TestClaimUnitStartsMainsIsolatedRunUnitOnThePinnedBuild(t *testing.T) {
u, ctl, in, _ := claimUnitFixture(t)
got, err := u.Start(context.Background(), in)
diff --git a/internal/instancectl/server.go b/internal/instancectl/server.go
index b3cc63aa7..c21ec28bf 100644
--- a/internal/instancectl/server.go
+++ b/internal/instancectl/server.go
@@ -425,12 +425,10 @@ func parseRequest(r *http.Request, v verbShape) (request, error) {
if err != nil {
return request{}, fmt.Errorf("memory_max %q is not a number", q.Get("memory_max"))
}
- switch n {
- case cgroup.TierDefault.MemoryMax, cgroup.TierChildren.MemoryMax:
- req.tier = cgroup.Limit{MemoryMax: n}
- default:
+ if _, ok := cgroup.SizeOf(cgroup.Limit{MemoryMax: n}); !ok {
return request{}, fmt.Errorf("memory_max %d is not a platform tier", n)
}
+ req.tier = cgroup.Limit{MemoryMax: n}
}
if q.Has("gen") {
raw := q.Get("gen")
diff --git a/internal/instancectl/server_gen_test.go b/internal/instancectl/server_gen_test.go
index 577b5baf1..c53146719 100644
--- a/internal/instancectl/server_gen_test.go
+++ b/internal/instancectl/server_gen_test.go
@@ -293,6 +293,21 @@ func TestParentSetsOnlyTheSliceMemory(t *testing.T) {
}
}
+// TestParentTakesASumWithSmall: a parent limit may be the sum of any two
+// sizes, small included (small plus standard is 768 MiB).
+func TestParentTakesASumWithSmall(t *testing.T) {
+ ctl := genCtl()
+ _, c := newServer(t, ctl)
+ resp := post(t, c, "/parent?host="+genHost+"&memory_max=805306368")
+ if resp.StatusCode != 204 {
+ t.Fatalf("status %d, want 204", resp.StatusCode)
+ }
+ want := `set-property --runtime carlos-instances-bill\x2d2.example.com.slice MemoryMax=805306368 MemoryHigh=724775724 MemorySwapMax=805306368`
+ if ctl.joined() != want {
+ t.Errorf("calls =\n%s\nwant\n%s", ctl.joined(), want)
+ }
+}
+
func TestSweepStopsGenerationUnitsAndRemovesTheirFiles(t *testing.T) {
ctl := genCtl()
ctl.answers["list-units"] = genUnit + " loaded active running carlos exec-backed instance generation\n"
@@ -366,6 +381,7 @@ func TestGenAndParentInputsAreValidated(t *testing.T) {
"/parent?host=" + genHost,
"/parent?host=" + genHost + "&memory_max=-536870912",
"/parent?host=" + genHost + "&memory_max=0",
+ "/parent?host=" + genHost + "&memory_max=402653184", // 384 MiB: no size and no sum of two
} {
if resp := post(t, c, path); resp.StatusCode != 400 {
t.Errorf("%s: status %d, want 400", path, resp.StatusCode)
diff --git a/internal/instancectl/server_test.go b/internal/instancectl/server_test.go
index c2e12c2b3..de755b011 100644
--- a/internal/instancectl/server_test.go
+++ b/internal/instancectl/server_test.go
@@ -168,6 +168,28 @@ func TestStartSetsTheTierThenStartsThenReadsTheInvocation(t *testing.T) {
}
}
+// TestStartTakesEverySize: /start accepts each size's MemoryMax and writes
+// that size's whole row onto the host slice (#329, spec §5.1).
+func TestStartTakesEverySize(t *testing.T) {
+ for _, tc := range []struct{ max, line string }{
+ {"268435456", "set-property --runtime carlos-instances-bill\\x2d2.example.com.slice MemoryMax=268435456 MemoryHigh=241591905 MemorySwapMax=268435456 CPUWeight=50 CPUQuota=50% IOWeight=50"},
+ {"536870912", "set-property --runtime carlos-instances-bill\\x2d2.example.com.slice MemoryMax=536870912 MemoryHigh=483183819 MemorySwapMax=536870912 CPUWeight=100 CPUQuota=100% IOWeight=100"},
+ {"1073741824", "set-property --runtime carlos-instances-bill\\x2d2.example.com.slice MemoryMax=1073741824 MemoryHigh=966367638 MemorySwapMax=1073741824 CPUWeight=100 CPUQuota=200% IOWeight=100"},
+ {"2147483648", "set-property --runtime carlos-instances-bill\\x2d2.example.com.slice MemoryMax=2147483648 MemoryHigh=1932735276 MemorySwapMax=2147483648 CPUWeight=200 CPUQuota=400% IOWeight=200"},
+ } {
+ ctl := &fakeCtl{answers: map[string]string{"show -p InvocationID --value": "abc123\n"}}
+ _, c := newServer(t, ctl)
+ resp := post(t, c, "/start?host=bill-2.example.com&role=run&memory_max="+tc.max)
+ if resp.StatusCode != 200 {
+ t.Errorf("memory_max=%s: status %d, want 200", tc.max, resp.StatusCode)
+ continue
+ }
+ if first, _, _ := strings.Cut(ctl.joined(), "\n"); first != tc.line {
+ t.Errorf("memory_max=%s: first systemctl line\n%s\nwant\n%s", tc.max, first, tc.line)
+ }
+ }
+}
+
func TestStartRefusesBadInputsWithoutCallingSystemctl(t *testing.T) {
ctl := &fakeCtl{}
_, c := newServer(t, ctl)
@@ -175,6 +197,7 @@ func TestStartRefusesBadInputsWithoutCallingSystemctl(t *testing.T) {
"host=Bad_Host&role=run&memory_max=536870912",
"host=h.example.com&role=exec&memory_max=536870912",
"host=h.example.com&role=run&memory_max=1",
+ "host=h.example.com&role=run&memory_max=805306368", // 768 MiB: a sum of two sizes, not a size
"host=h.example.com&role=seed&memory_max=536870912", // seed is not a /start role
"role=run&memory_max=536870912",
} {