| new file mode 100644 |
| index 000000000..58b1045fc |
| --- /dev/null |
| +++ b/internal/activator/size_pending_test.go |
| @@ -0,0 +1,227 @@ |
| +package activator |
| + |
| +import ( |
| + "context" |
| + "os" |
| + "os/exec" |
| + "path/filepath" |
| + "strconv" |
| + "sync" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/cgroup" |
| + "github.com/carlosframework/platform/internal/instancectl" |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +// cgroupSidecarBackend is the swap fixture's backend whose legacy sidecar |
| +// starts also go through the real flat spawn seam (ExecBackend. |
| +// spawnInCgroupAt with no child: the host cgroup, sized from the route the |
| +// activator hands it) onto a real cgroup.Manager over a temp-dir fixture, |
| +// so applyLimit's memory.max for the host is what the test reads. The |
| +// sidecar process itself is the fixture's fakeProc, so the test can crash |
| +// it. |
| +type cgroupSidecarBackend struct { |
| + *fakeSwapBackend |
| + eb *ExecBackend |
| + root string |
| + t *testing.T |
| +} |
| + |
| +func newCgroupSidecarBackend(t *testing.T, fb *fakeSwapBackend) *cgroupSidecarBackend { |
| + t.Helper() |
| + root := t.TempDir() |
| + for _, f := range []string{"cgroup.procs", "cgroup.subtree_control"} { |
| + if err := os.WriteFile(filepath.Join(root, f), nil, 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + m, err := cgroup.SetupAt(root, t.Logf) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return &cgroupSidecarBackend{fakeSwapBackend: fb, root: root, t: t, |
| + eb: &ExecBackend{Cgroups: m, Tier: func(registry.Route) cgroup.Limit { return cgroup.TierDefault }}} |
| +} |
| + |
| +func (b *cgroupSidecarBackend) spawn(rt registry.Route) { |
| + cmd, settle, err := b.eb.spawnInCgroupAt(rt, "", false, func() *exec.Cmd { return exec.Command("/bin/true") }) |
| + if err != nil { |
| + b.t.Errorf("flat sidecar spawn: %v", err) |
| + return |
| + } |
| + _ = cmd.Wait() |
| + settle() |
| +} |
| + |
| +func (b *cgroupSidecarBackend) StartSidecar(ctx context.Context, rt registry.Route) (Proc, error) { |
| + b.spawn(rt) |
| + return b.fakeSwapBackend.StartSidecar(ctx, rt) |
| +} |
| + |
| +func (b *cgroupSidecarBackend) StartSidecarGen(ctx context.Context, rt registry.Route, g Gen) (Proc, error) { |
| + b.spawn(rt) |
| + return b.fakeSwapBackend.StartSidecarGen(ctx, rt, g) |
| +} |
| + |
| +// memoryMax is the host cgroup's memory.max as applyLimit last wrote it. |
| +func (b *cgroupSidecarBackend) memoryMax(host string) string { |
| + raw, err := os.ReadFile(filepath.Join(b.root, "carlos-exec@"+host, "memory.max")) |
| + if err != nil { |
| + return err.Error() |
| + } |
| + return string(raw) |
| +} |
| + |
| +// Final review finding 1 (ruling 12): a flat host with generations off has |
| +// no live size (the pass is refused), and a size change must not reach the |
| +// running instance's shared cgroup through a sidecar restart either. Awake |
| +// at one size, the column moves to another and the pass runs; the sidecar |
| +// crashes and the supervisor restarts it: the host cgroup's memory.max |
| +// stays what the instance woke at, for a shrink and for a growth. |
| +func TestASidecarRestartKeepsTheRunningHostsMemory(t *testing.T) { |
| + t.Parallel() |
| + for _, tc := range []struct { |
| + name string |
| + from, to string |
| + want int64 |
| + }{ |
| + {"shrink", "large", "small", gib}, |
| + {"growth", "small", "large", int64(1 << 28)}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + t.Parallel() |
| + f := newSizeFixture(t, tc.from, tc.want) |
| + f.a.gens = false |
| + f.b.sidecarStaged = true |
| + f.b.setSizeHook(func(cgroup.Size) (instancectl.SizeReadBack, error) { |
| + return instancectl.SizeReadBack{}, errSizeNotLive // a flat row has no live size |
| + }) |
| + cb := newCgroupSidecarBackend(t, f.b) |
| + f.a.Bounce(f.host) |
| + waitFor(t, "the stop", func() bool { return f.a.entryCount() == 0 }) |
| + f.a.mu.Lock() |
| + f.a.backend = cb |
| + f.a.mu.Unlock() |
| + rt := testRoute(f.host) |
| + rt.Size = tc.from |
| + f.routes.put(rt) |
| + arrangeAwake(t, f.a, rt) |
| + want := "" |
| + if s, ok := cgroup.SizeByName(tc.from); ok { |
| + want = strconv.FormatInt(s.Limit.MemoryMax, 10) |
| + } |
| + if got := cb.memoryMax(f.host); got != want || f.b.sidecarStartCount() != 1 { |
| + t.Fatalf("after the wake: memory.max %q, %d sidecar starts; want %s, 1", got, f.b.sidecarStartCount(), want) |
| + } |
| + |
| + f.operatorSets(tc.to) |
| + f.sweep(t) |
| + if got := f.sizeEvents(); len(got) != 1 { |
| + t.Fatalf("size calls %v, want the one refused call", got) |
| + } |
| + if st := f.state(t); st.Want.Name != tc.to { |
| + t.Fatalf("SizeState = %+v, want %s pending", st, tc.to) |
| + } |
| + |
| + f.b.lastSidecar().kill() |
| + f.clk.advance(3 * time.Second) |
| + f.a.sweepOnce() |
| + waitFor(t, "the supervised restart", func() bool { return f.a.sidecarSettled(f.host) && f.b.sidecarStartCount() == 2 }) |
| + if got := cb.memoryMax(f.host); got != want { |
| + t.Fatalf("after the sidecar restart: host memory.max %q, want %s as the instance woke (no live memory change)", got, want) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// sizingPlanBackend is the planned-wake fixture's backend with the live |
| +// size seam: every call is applied, and its tier reads the size column. |
| +type sizingPlanBackend struct { |
| + *planGenBackend |
| + mu sync.Mutex |
| + calls int |
| +} |
| + |
| +func (b *sizingPlanBackend) ResizeLive(_ context.Context, _ registry.Route, size cgroup.Size) (instancectl.SizeReadBack, error) { |
| + b.mu.Lock() |
| + b.calls++ |
| + b.mu.Unlock() |
| + return sizeApplied(size, half), nil |
| +} |
| + |
| +// GenTier reads the size column, as ExecBackend's does. |
| +func (b *sizingPlanBackend) GenTier(rt registry.Route, _ string) cgroup.Limit { |
| + return SizedLimit(rt, cgroup.TierDefault) |
| +} |
| + |
| +func (b *sizingPlanBackend) count() int { |
| + b.mu.Lock() |
| + defer b.mu.Unlock() |
| + return b.calls |
| +} |
| + |
| +// Final review finding 2: a planned wake reports its outcome (afterWake) |
| +// off a.mu with copies of the entry's route, after the entry is awake. A |
| +// size change and its sweep landing then must not write the route under |
| +// it. The wake is held inside OnGen, after afterWake has copied the route |
| +// into its event and before it takes a.mu again, while the column moves |
| +// and a size pass runs to its call. The test learns nothing from the wake |
| +// goroutine past its last unlock (no channel from OnGen): it sweeps until |
| +// the pass has made its call, after seeing the entry awake under a.mu. So a write of e.route by the pass is |
| +// unordered with afterWake's read, and -race reports it; the route check |
| +// at the end catches it without -race. |
| +func TestASizePassDuringPlannedWakeCompletionLeavesTheRouteAlone(t *testing.T) { |
| + t.Parallel() |
| + dir := t.TempDir() |
| + next := stagedID(t, dir, "v2") |
| + a, pb, _ := planActivator(t, WakePlan{Boot: next, Mode: ModeOverlap}) |
| + b := &sizingPlanBackend{planGenBackend: pb} |
| + a.backend = b |
| + host := "one.example.com" |
| + rt := testRoute(host) |
| + routes := &liveRoutes{rows: map[string]registry.Route{host: rt}} |
| + a.Routes = routes |
| + release := make(chan struct{}) |
| + var once sync.Once |
| + a.OnGen = func(ev GenEvent) { |
| + if ev.Kind == GenHealthy { |
| + once.Do(func() { <-release }) |
| + } |
| + } |
| + woke := make(chan error, 1) |
| + go func() { |
| + done, err := a.beginRequest(context.Background(), rt) |
| + if err == nil { |
| + done() |
| + } |
| + woke <- err |
| + }() |
| + // Awake is published under a.mu before afterWake runs: reading it |
| + // orders the test after that unlock only. |
| + waitFor(t, "the entry awake", func() bool { _, ok := a.SizeState(host); return ok }) |
| + routes.setSize(host, "xlarge") |
| + for deadline := time.Now().Add(10 * time.Second); b.count() == 0; time.Sleep(5 * time.Millisecond) { |
| + if time.Now().After(deadline) { |
| + t.Fatal("no size pass made its call while the wake completed") |
| + } |
| + a.NoteSize(host) |
| + a.sweepOnce() |
| + } |
| + waitFor(t, "the size pass to finish", func() bool { return a.sizeIdleForTest(host) }) |
| + close(release) |
| + if err := <-woke; err != nil { |
| + t.Fatalf("the wake: %v", err) |
| + } |
| + st, ok := a.SizeState(host) |
| + if !ok || st.Want.Name != "xlarge" { |
| + t.Fatalf("SizeState = %+v, %v; want xlarge pending", st, ok) |
| + } |
| + a.mu.Lock() |
| + got := a.entries[host].route.Size |
| + a.mu.Unlock() |
| + if got != "" { |
| + t.Fatalf("the awake entry's route size = %q, want the wake's \"\" unchanged", got) |
| + } |
| +} |