Files
substrate/cmd/ateapi/internal/controlapi/actor_sizing_test.go
T
Chenyi Wang 71b006309f Right-size actor sandboxes to declared ActorTemplate resource limits (#679)
Actors previously ran in sandboxes sized to the whole node; there was no
way to declare how much CPU/memory a given actor should get. This adds
an explicit, immutable sizing knob on the ActorTemplate and plumbs it
into the sandbox's OCI spec for both the gVisor and micro-VM runtimes.

**API** — `ActorTemplate.spec.resources`
(`*corev1.ResourceRequirements`). The `limits` size the sandbox and are
baked into the immutable spec; the CRD and generated code are
regenerated accordingly.

**internal/sizing** (shared by both runtimes) — new `SandboxSize` value
(`FromLimits` / `VCPUs` / `ApplyToOCISpec`). `ApplyToOCISpec` writes CPU
quota+period and the memory limit onto the OCI spec, and is a no-op when
neither dimension is set, so 0 means "unconstrained". `VCPUs` rounds
milliCPU up to whole
vCPUs.

**Plumbing** — ateapi reads the template limits (`actorResourceLimits` →
`tmpl.Spec.Resources`) and supplies `CpuMilli`/`MemoryBytes` over the
actor RPCs (ateapi → atelet → ateom); ateom applies them — gVisor via
the cgroup leaf (`runsc --cpu-num-from-quota` provisions the sentry vCPU
count), micro-VM via the guest VmConfig. The two fields are carried on
the proto messages.

**Scheduling** — worker capacity is taken from the WorkerPool's
per-worker limits and advertised on the Worker; the scheduler only
places an actor on a worker whose capacity >= the actor's declared
limits. A missing worker or actor
dimension is treated as unconstrained, so placement is never blocked by
absent data.

**Docs & demos** — document the model in `api-guide.md`; the counter,
sandbox, and micro-VM demos declare actor limits, and their WorkerPool
comments now describe the real model (worker limits size the worker pod
+ advertise scheduling capacity; the sandbox itself is sized by
`ActorTemplate.spec.resources`).

- [x] Tests pass (unit tests for sizing + scheduling; e2e suite in
`internal/e2e/suites/sizing` resumes an actor and asserts, via the probe
fixture's `/resources` endpoint, that the running sandbox observes the
declared CPU/memory from the inside)
- [x] Appropriate changes to documentation are included in the PR
2026-08-14 14:32:26 -07:00

140 lines
4.1 KiB
Go

// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package controlapi
import (
"testing"
atev1alpha1 "github.com/agent-substrate/substrate/pkg/api/v1alpha1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)
// TestActorResourceLimits covers the actor-side extraction: the CPU/memory limits
// an ActorTemplate declares become the sandbox size and the scheduling floor.
func TestActorResourceLimits(t *testing.T) {
tests := []struct {
name string
res *corev1.ResourceRequirements
wantCPU int64
wantMemory int64
}{
{
name: "nil resources yields zero",
res: nil,
wantCPU: 0,
wantMemory: 0,
},
{
name: "cpu and memory limits are read",
res: &corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("2"),
corev1.ResourceMemory: resource.MustParse("4Gi"),
},
},
wantCPU: 2000,
wantMemory: 4 << 30,
},
{
name: "millicpu is preserved",
res: &corev1.ResourceRequirements{
Limits: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1500m")},
},
wantCPU: 1500,
wantMemory: 0,
},
{
name: "requests are ignored; only limits size the actor",
res: &corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("1"),
corev1.ResourceMemory: resource.MustParse("1Gi"),
},
},
wantCPU: 0,
wantMemory: 0,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
tmpl := &atev1alpha1.ActorTemplate{Spec: atev1alpha1.ActorTemplateSpec{Resources: tc.res}}
cpu, mem := actorResourceLimits(tmpl)
if cpu != tc.wantCPU || mem != tc.wantMemory {
t.Fatalf("actorResourceLimits() = (%d, %d), want (%d, %d)", cpu, mem, tc.wantCPU, tc.wantMemory)
}
})
}
}
// TestWorkerCapacity covers the worker-side extraction: capacity comes from the
// ateom container's limits, not the pod total, and other containers are ignored.
func TestWorkerCapacity(t *testing.T) {
pod := func(ctrs ...corev1.Container) *corev1.Pod {
return &corev1.Pod{Spec: corev1.PodSpec{Containers: ctrs}}
}
limited := func(name, cpu, mem string) corev1.Container {
lim := corev1.ResourceList{}
if cpu != "" {
lim[corev1.ResourceCPU] = resource.MustParse(cpu)
}
if mem != "" {
lim[corev1.ResourceMemory] = resource.MustParse(mem)
}
return corev1.Container{Name: name, Resources: corev1.ResourceRequirements{Limits: lim}}
}
tests := []struct {
name string
pod *corev1.Pod
wantCPU int64
wantMemory int64
}{
{
name: "no ateom container yields zero",
pod: pod(limited("sidecar", "1", "1Gi")),
wantCPU: 0,
wantMemory: 0,
},
{
name: "ateom container limits become capacity",
pod: pod(limited(ateomContainerName, "4", "8Gi")),
wantCPU: 4000,
wantMemory: 8 << 30,
},
{
name: "only the ateom container counts, not the pod total",
pod: pod(limited("sidecar", "16", "64Gi"), limited(ateomContainerName, "2", "2Gi")),
wantCPU: 2000,
wantMemory: 2 << 30,
},
{
name: "unset dimension reports zero",
pod: pod(limited(ateomContainerName, "2", "")),
wantCPU: 2000,
wantMemory: 0,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := workerCapacity(tc.pod)
if got.GetCpuMilli() != tc.wantCPU || got.GetMemoryBytes() != tc.wantMemory {
t.Fatalf("workerCapacity() = (%d, %d), want (%d, %d)",
got.GetCpuMilli(), got.GetMemoryBytes(), tc.wantCPU, tc.wantMemory)
}
})
}
}