daemon: extract StatsService sibling; shrink VMService's surface
Closes commit 3 of the god-service decomposition. VMService still
owned 45+ methods after the startVMLocked extraction and RPC table
landed in commits 1 and 2. Stats / ports / health / vsock-ping sit
in a corner of that surface that doesn't share any state with
lifecycle orchestration — nothing about "what's this VM's CPU
doing" belongs in the same service as Create/Start/Stop/Delete/Set.
New StatsService owns:
- GetVMStats / getVMStatsLocked / collectStats (stats collection)
- HealthVM / PingVM (vsock-agent health probe)
- PortsVM + buildVMPorts + probeWebListener + probeHTTPScheme +
dedupeVMPorts (listening-port enumeration)
- pollStats (background ticker refresh)
- stopStaleVMs (auto-stop sweep past config.AutoStopStaleAfter)
The three VMService touch-points stats genuinely needs — vmAlive,
vmHandles, the per-VM lock helpers, plus cleanupRuntime for the
stale-sweep tear-down — come in as function-typed closures, not a
*VMService pointer. StatsService has no back-reference to its
sibling. Mirrors the dependency-struct pattern WorkspaceService
already uses.
Wiring: d.stats is populated in wireServices AFTER d.vm (closures
must see a non-nil d.vm at call time). Dispatch table's four
entries (vm.stats / vm.health / vm.ping / vm.ports) now resolve
through d.stats. Background loop's pollStats / stopStaleVMs
tickers do the same. Dispatch surface from the RPC client's
perspective is byte-identical.
After this commit:
- vm_stats.go and ports.go are deleted; their content (plus the
stats-specific fields) lives in stats_service.go.
- VMService loses 12 methods. It's still the biggest service
(~30 methods, all lifecycle-supporting: handle cache, disk
provisioning, preflight, create-ops registry, lock helpers,
the lifecycle verbs themselves) but it's finally one coherent
concern instead of five.
Tests:
- TestWireServicesInstantiatesStatsService — pins that the
wiring order puts d.stats non-nil + its five closures all
populated. Prevents a silent background-loop regression.
- All existing tests that called d.vm.HealthVM / d.vm.PingVM /
d.vm.PortsVM / d.vm.collectStats were re-pointed at d.stats.
Smoke: all 21 scenarios green, including vm ports (exercises the
new PortsVM entry end-to-end) and the long-running workspace
scenarios (exercise the background stats poller implicitly).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
366e1560c9
commit
86a56fedb3
7 changed files with 480 additions and 337 deletions
|
|
@ -374,7 +374,7 @@ func TestHealthVMReturnsHealthyForRunningGuest(t *testing.T) {
|
|||
d := &Daemon{store: db, runner: runner}
|
||||
wireServices(d)
|
||||
d.vm.setVMHandlesInMemory(vm.ID, model.VMHandles{PID: handlePID})
|
||||
result, err := d.vm.HealthVM(ctx, vm.Name)
|
||||
result, err := d.stats.HealthVM(ctx, vm.Name)
|
||||
if err != nil {
|
||||
t.Fatalf("HealthVM: %v", err)
|
||||
}
|
||||
|
|
@ -438,7 +438,7 @@ func TestPingVMAliasReturnsAliveForHealthyVM(t *testing.T) {
|
|||
d := &Daemon{store: db, runner: runner}
|
||||
wireServices(d)
|
||||
d.vm.setVMHandlesInMemory(vm.ID, model.VMHandles{PID: fake.Process.Pid})
|
||||
result, err := d.vm.PingVM(ctx, vm.Name)
|
||||
result, err := d.stats.PingVM(ctx, vm.Name)
|
||||
if err != nil {
|
||||
t.Fatalf("PingVM: %v", err)
|
||||
}
|
||||
|
|
@ -538,7 +538,7 @@ func TestHealthVMReturnsFalseForStoppedVM(t *testing.T) {
|
|||
|
||||
d := &Daemon{store: db}
|
||||
wireServices(d)
|
||||
result, err := d.vm.HealthVM(ctx, vm.Name)
|
||||
result, err := d.stats.HealthVM(ctx, vm.Name)
|
||||
if err != nil {
|
||||
t.Fatalf("HealthVM: %v", err)
|
||||
}
|
||||
|
|
@ -639,7 +639,7 @@ func TestPortsVMReturnsEnrichedPortsAndWebSchemes(t *testing.T) {
|
|||
wireServices(d)
|
||||
d.vm.setVMHandlesInMemory(vm.ID, model.VMHandles{PID: fake.Process.Pid})
|
||||
|
||||
result, err := d.vm.PortsVM(ctx, vm.Name)
|
||||
result, err := d.stats.PortsVM(ctx, vm.Name)
|
||||
if err != nil {
|
||||
t.Fatalf("PortsVM: %v", err)
|
||||
}
|
||||
|
|
@ -687,7 +687,7 @@ func TestPortsVMReturnsErrorForStoppedVM(t *testing.T) {
|
|||
|
||||
d := &Daemon{store: db}
|
||||
wireServices(d)
|
||||
_, err := d.vm.PortsVM(ctx, vm.Name)
|
||||
_, err := d.stats.PortsVM(ctx, vm.Name)
|
||||
if err == nil || !strings.Contains(err.Error(), "is not running") {
|
||||
t.Fatalf("PortsVM error = %v, want not running", err)
|
||||
}
|
||||
|
|
@ -1407,7 +1407,7 @@ func TestCollectStatsIgnoresMalformedMetricsFile(t *testing.T) {
|
|||
|
||||
d := &Daemon{}
|
||||
wireServices(d)
|
||||
stats, err := d.vm.collectStats(context.Background(), model.VMRecord{
|
||||
stats, err := d.stats.collectStats(context.Background(), model.VMRecord{
|
||||
Runtime: model.VMRuntime{
|
||||
SystemOverlay: overlay,
|
||||
WorkDiskPath: workDisk,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue