Files
ilkerandClaude Opus 5 421efc1d42 hpcod: make oversubscription a switch, and turn it on for hv01
There was already a cpu_overcommit multiplier, but a multiplier is not a
policy. Nothing said whether a site was meant to be strict, a ratio left in
the file kept inflating capacity indefinitely, and the resulting number was
reported on its own - "56 cores" being either a machine or a decision, with
no way to tell which.

Now: oversubscribe is the switch, off by default, and the ratios say how
much. Separating them means turning the policy off does not mean editing
the numbers back to 1.0 and losing what they were tuned to. Every
combination that has more than one meaning is refused at startup - a ratio
above 1 with the switch off, the switch on with nothing to multiply - so a
config that reads as strict cannot quietly be anything else.

Memory joins CPU, because it had no ratio at all and enabling only CPU
would have moved the refusal from one resource to the other. They are not
equally safe and the comments say so: oversubscribed cores make jobs slow,
oversubscribed memory makes them die.

The inventory now carries the hardware alongside the schedulable figures,
and both the CLI and the plan output say when a node is oversubscribed and
by how much. A held device is finally printed too - discovery learned who
owns each card in the previous commit, and nothing displayed it.

Enabled on hv01 at cpu 6.0, mem 1.3, chosen by measurement. 28 cores with
153 vCPU already committed by 36 running guests, load average 2.3: the box
is committed, not busy, so 6.0 sits just above what is already promised.
Guests claim 618 GiB of 503 and ballooning absorbs it, with 118 GiB
genuinely available, so 1.3 promises about 36 GiB more than today and stays
inside what the host can actually produce.

Measured both ways, because a switch tested in one direction is untested:

  off  28 cores (0 free), 504 GiB (0 free)    request rejected
  on   168 cores (15 free), 655 GiB (37 free) PLACED, compute1 gets
                                              gpu 0000:01:00.0 (Quadro K620)

The live config was the contradictory case - ratio 2.0, no switch - and
startup refused it by name before it was fixed.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-20 23:14:30 +02:00

148 lines
4.7 KiB
Go

package proxmox
import (
"strings"
"testing"
"github.com/ilkermanap/hpcod/internal/provider"
)
func TestOversubscriptionPolicy(t *testing.T) {
cases := []struct {
name string
settings map[string]string
wantCPU float64
wantMem float64
wantOn bool
wantErr string
}{
{
name: "absent means strict",
settings: map[string]string{},
wantCPU: 1, wantMem: 1, wantOn: false,
},
{
name: "on, cpu only",
settings: map[string]string{"oversubscribe": "true", "cpu_overcommit": "6"},
wantCPU: 6, wantMem: 1, wantOn: true,
},
{
name: "on, both",
settings: map[string]string{"oversubscribe": "true", "cpu_overcommit": "6", "mem_overcommit": "1.3"},
wantCPU: 6, wantMem: 1.3, wantOn: true,
},
{
name: "explicitly off with ratios at 1 is fine",
settings: map[string]string{"oversubscribe": "false", "cpu_overcommit": "1.0"},
wantCPU: 1, wantMem: 1, wantOn: false,
},
// The case this validation exists for: the policy reads as off but
// the site would still be inflated.
{
name: "off but a ratio is left behind",
settings: map[string]string{"oversubscribe": "false", "cpu_overcommit": "2.0"},
wantErr: "would still inflate",
},
{
name: "ratio with no switch at all",
settings: map[string]string{"cpu_overcommit": "2.0"},
wantErr: "would still inflate",
},
{
name: "on but nothing to multiply",
settings: map[string]string{"oversubscribe": "true"},
wantErr: "would do nothing",
},
{
name: "a ratio below 1 is not undersubscription",
settings: map[string]string{"oversubscribe": "true", "cpu_overcommit": "0.5"},
wantErr: "at least 1.0",
},
{
name: "not a number",
settings: map[string]string{"oversubscribe": "true", "cpu_overcommit": "lots"},
wantErr: "must be a number",
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
cpu, mem, on, err := oversubscription(provider.SiteConfig{ID: "hv01", Settings: c.settings})
if c.wantErr != "" {
if err == nil {
t.Fatalf("got cpu=%g mem=%g on=%v, want an error mentioning %q", cpu, mem, on, c.wantErr)
}
if !strings.Contains(err.Error(), c.wantErr) {
t.Errorf("error %q does not mention %q", err, c.wantErr)
}
if !strings.Contains(err.Error(), "hv01") {
t.Errorf("error %q does not name the site", err)
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cpu != c.wantCPU || mem != c.wantMem || on != c.wantOn {
t.Errorf("got cpu=%g mem=%g on=%v, want cpu=%g mem=%g on=%v",
cpu, mem, on, c.wantCPU, c.wantMem, c.wantOn)
}
})
}
}
// The ratios have to reach the reported capacity, and the hardware has to
// survive alongside them - otherwise the policy is invisible to whoever
// reads the inventory.
func TestDiscoverReportsBothSchedulableAndPhysical(t *testing.T) {
routes := map[string]string{
"/api2/json/nodes": `{"data":[{"node":"hv01","status":"online","maxcpu":28,"maxmem":540000000000}]}`,
"/api2/json/cluster/status": `{"data":[]}`,
"/api2/json/cluster/resources": `{"data":[]}`,
"/api2/json/nodes/hv01/status": `{"data":{"cpuinfo":{"model":"Xeon","cpus":28}}}`,
"/api2/json/nodes/hv01/storage": `{"data":[]}`,
}
p := &Provider{
id: "hv01", cli: testServer(t, routes), skipPCI: true,
oversubscribe: true, cpuOvercommit: 6, memOvercommit: 1.3,
}
site, err := p.Discover(t.Context())
if err != nil {
t.Fatal(err)
}
n := site.Nodes[0]
if n.PhysicalCores != 28 {
t.Errorf("physical cores = %d, want 28", n.PhysicalCores)
}
if n.Cores != 168 {
t.Errorf("schedulable cores = %d, want 168 (28 x 6)", n.Cores)
}
if n.MemBytes <= n.PhysicalMemBytes {
t.Errorf("schedulable memory %d should exceed physical %d at ratio 1.3", n.MemBytes, n.PhysicalMemBytes)
}
if !n.Oversubscribed() {
t.Error("node does not report itself oversubscribed")
}
}
func TestStrictNodeIsNotReportedOversubscribed(t *testing.T) {
routes := map[string]string{
"/api2/json/nodes": `{"data":[{"node":"hv01","status":"online","maxcpu":28,"maxmem":540000000000}]}`,
"/api2/json/cluster/status": `{"data":[]}`,
"/api2/json/cluster/resources": `{"data":[]}`,
"/api2/json/nodes/hv01/status": `{"data":{"cpuinfo":{"model":"Xeon","cpus":28}}}`,
"/api2/json/nodes/hv01/storage": `{"data":[]}`,
}
p := &Provider{id: "hv01", cli: testServer(t, routes), skipPCI: true, cpuOvercommit: 1, memOvercommit: 1}
site, err := p.Discover(t.Context())
if err != nil {
t.Fatal(err)
}
if n := site.Nodes[0]; n.Oversubscribed() || n.Cores != n.PhysicalCores {
t.Errorf("strict node reports cores=%d physical=%d oversubscribed=%v", n.Cores, n.PhysicalCores, n.Oversubscribed())
}
}