Files
ilkerandClaude Opus 5 d67af5d6c3 hpcod: projects, and what a cluster cost
"How much did the protein run use" is a question about a piece of
work, not about c003-15. A tenant asks for clusters over months;
individually each one is an address, some hardware and a lease, and
nothing joined them up.

A project groups them, and is optional throughout. Somebody who wants
one cluster for an afternoon should not have to invent a project to
get it, and requiring one would mean every tenant carrying a
"default" that means nothing. The form does not even ask when there
are none: a field whose only option is "none" is a question with one
answer.

The cost side needed no new columns. The cluster record already said
when it was created and destroyed, and cluster_vm said how many cores
and how much memory it held; what was missing was anything that read
them. cluster_usage is that, and it is a view rather than a table
because a derived number stored is a number that can disagree with
what it was derived from. It works retroactively - every cluster this
estate has ever built now has a line.

Core-hours are reserved, not executed, and the view says so. That is
the honest unit for an on-demand cluster: the hardware was yours and
nobody else could have it, whether or not a job ran. What was
actually executed is a Slurm accounting question and there is no
slurmdbd.

The project is attached inside the allocating transaction, for the
same reason the placement and the applications are, and it is checked
there to be the tenant's own and still open - a cluster filed against
somebody else's project leaks what they are working on. Closing keeps
history and stops it being offered. There is no delete: the reference
from cluster is RESTRICT, because a project removed from under its
clusters leaves their cost attributed to nothing, and afterwards is
when the question gets asked.

A release now writes manifest.json beside home.tgz: the nodes, the
wall time, the core-hours. The database knows all of it and outlives
the cluster, but the tarball can be moved off this host and the
description travels with it. Not fatal if it fails - the data is
already safe by then, and holding hardware because a note could not
be written is the wrong trade.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-22 13:05:35 +02:00

304 lines
9.9 KiB
Go

package store
import (
"context"
"errors"
"reflect"
"testing"
"time"
"github.com/ilkermanap/hpcod/internal/model"
)
func TestPlacementIsReadBackAsWritten(t *testing.T) {
s := testStore(t)
ctx := context.Background()
tenant := mustTenant(t, s, "acme")
p := Placement{
Site: "hv01",
VMs: []VM{
{Role: "head", NodeIndex: 0, HostNode: "hv01", Cores: 4, MemoryGB: 8},
{Role: "compute", NodeIndex: 1, HostNode: "pve2", Cores: 8, MemoryGB: 34,
Devices: []Device{{NativeID: "0000:01:00.0", Capability: "gpu"}}},
{Role: "compute", NodeIndex: 2, HostNode: "hv03", Cores: 8, MemoryGB: 34},
},
}
c, err := s.AllocateCluster(ctx, tenant, p, time.Hour, nil, nil)
if err != nil {
t.Fatalf("allocate: %v", err)
}
got, err := s.VMs(ctx, c.SerialID)
if err != nil {
t.Fatalf("read back: %v", err)
}
if !reflect.DeepEqual(got, p.VMs) {
t.Errorf("read back\n %+v\nwant\n %+v", got, p.VMs)
}
}
// A placement has to say where every VM goes. Accepting one that does not
// would store a cluster the provisioner cannot build.
func TestPlacementMustBeComplete(t *testing.T) {
s := testStore(t)
ctx := context.Background()
tenant := mustTenant(t, s, "acme")
cases := map[string]Placement{
"no site": {VMs: plainPlacement().VMs},
"no VMs": {Site: "hv01"},
"no head": {Site: "hv01", VMs: []VM{{Role: "compute", NodeIndex: 1, HostNode: "hv01", Cores: 1, MemoryGB: 1}}},
"two heads": {Site: "hv01", VMs: []VM{{Role: "head", HostNode: "hv01", Cores: 1, MemoryGB: 1}, {Role: "head", NodeIndex: 9, HostNode: "hv01", Cores: 1, MemoryGB: 1}}},
"no host node": {Site: "hv01", VMs: []VM{{Role: "head", Cores: 1, MemoryGB: 1}}},
"unknown role": {Site: "hv01", VMs: []VM{{Role: "login", HostNode: "hv01", Cores: 1, MemoryGB: 1}}},
}
for name, p := range cases {
t.Run(name, func(t *testing.T) {
if _, err := s.AllocateCluster(ctx, tenant, p, 0, nil, nil); err == nil {
t.Error("accepted an incomplete placement")
}
})
}
// None of those may have consumed an address on the way to failing.
live, err := s.Live(ctx)
if err != nil {
t.Fatalf("live: %v", err)
}
if len(live) != 0 {
t.Errorf("rejected placements left %d cluster(s) behind: %+v", len(live), live)
}
}
// The point of putting the reservation in the same transaction as the
// address: a device that turns out to be taken must not leave a cluster
// holding an address that nothing will ever release.
func TestDeviceConflictRollsBackTheAddressToo(t *testing.T) {
s := testStore(t)
ctx := context.Background()
tenant := mustTenant(t, s, "acme")
first, err := s.AllocateCluster(ctx, tenant, withGPU("hv01", "0000:01:00.0"), time.Hour, nil, nil)
if err != nil {
t.Fatalf("first: %v", err)
}
_, err = s.AllocateCluster(ctx, tenant, withGPU("hv01", "0000:01:00.0"), time.Hour, nil, nil)
if !errors.Is(err, ErrDeviceHeld) {
t.Fatalf("second allocation: got %v, want ErrDeviceHeld", err)
}
live, err := s.Live(ctx)
if err != nil {
t.Fatalf("live: %v", err)
}
if len(live) != 1 || live[0].SerialID != first.SerialID {
t.Fatalf("after the refused allocation, live = %+v; want only cluster %d", live, first.SerialID)
}
// And the next request gets the address the failed one would have taken,
// which is what says it was really rolled back and not merely hidden.
next, err := s.AllocateCluster(ctx, tenant, plainPlacement(), time.Hour, nil, nil)
if err != nil {
t.Fatalf("third: %v", err)
}
if next.ClusterID != first.ClusterID+1 {
t.Errorf("next address is %d, want %d - the failed allocation consumed one",
next.ClusterID, first.ClusterID+1)
}
}
// The same card on a different hypervisor is a different card.
func TestSameAddressOnAnotherHostIsNotAConflict(t *testing.T) {
s := testStore(t)
ctx := context.Background()
tenant := mustTenant(t, s, "acme")
if _, err := s.AllocateCluster(ctx, tenant, withGPU("hv01", "0000:01:00.0"), 0, nil, nil); err != nil {
t.Fatalf("hv01: %v", err)
}
if _, err := s.AllocateCluster(ctx, tenant, withGPU("pve2", "0000:01:00.0"), 0, nil, nil); err != nil {
t.Fatalf("pve2: %v - a PCI address is only unique within one host", err)
}
}
func TestReleasingAClusterReturnsItsHardware(t *testing.T) {
s := testStore(t)
ctx := context.Background()
tenant := mustTenant(t, s, "acme")
first, err := s.AllocateCluster(ctx, tenant, withGPU("hv01", "0000:01:00.0"), time.Hour, nil, nil)
if err != nil {
t.Fatalf("first: %v", err)
}
res, err := s.Reservations(ctx)
if err != nil {
t.Fatalf("reservations: %v", err)
}
want := []model.Reservation{{Site: "hv01", HostNode: "hv01", NativeID: "0000:01:00.0", HeldBy: first.AccountingName}}
if !reflect.DeepEqual(res, want) {
t.Fatalf("reservations = %+v, want %+v", res, want)
}
if err := s.Release(ctx, first.SerialID); err != nil {
t.Fatalf("release: %v", err)
}
res, err = s.Reservations(ctx)
if err != nil {
t.Fatalf("reservations after release: %v", err)
}
if len(res) != 0 {
t.Errorf("a destroyed cluster still holds %+v", res)
}
if _, err := s.AllocateCluster(ctx, tenant, withGPU("hv01", "0000:01:00.0"), 0, nil, nil); err != nil {
t.Errorf("the card did not come back: %v", err)
}
}
// A reservation only matters if the planner sees it. This is the join
// between the two halves: what the database has promised, folded into the
// inventory the planner reads.
func TestReservationsHideDevicesFromTheInventory(t *testing.T) {
s := testStore(t)
ctx := context.Background()
tenant := mustTenant(t, s, "acme")
c, err := s.AllocateCluster(ctx, tenant, withGPU("hv01", "0000:01:00.0"), time.Hour, nil, nil)
if err != nil {
t.Fatalf("allocate: %v", err)
}
res, err := s.Reservations(ctx)
if err != nil {
t.Fatalf("reservations: %v", err)
}
sites := []model.Site{{
ID: "hv01", Reachable: true, Healthy: true,
Nodes: []model.Node{{
Name: "hv01", Online: true, Cores: 32, MemBytes: 128 << 30,
Devices: []model.Device{
{NativeID: "0000:01:00.0", Capability: model.CapGPU, Confidence: model.ConfClass, Passthrough: true},
{NativeID: "0000:02:00.0", Capability: model.CapGPU, Confidence: model.ConfClass, Passthrough: true},
},
}},
}}
if got := sites[0].Capabilities()[model.CapGPU]; got != 2 {
t.Fatalf("fixture starts with %d schedulable GPUs, want 2", got)
}
model.ApplyReservations(sites, res)
if got := sites[0].Capabilities()[model.CapGPU]; got != 1 {
t.Errorf("after reservations, %d GPUs are schedulable; want 1", got)
}
if got := sites[0].Nodes[0].Devices[0].AllocatedTo; got != "reserved for "+c.AccountingName {
t.Errorf("device 0 allocated_to = %q, want it named after cluster %s", got, c.AccountingName)
}
if got := sites[0].Nodes[0].Devices[1].AllocatedTo; got != "" {
t.Errorf("the unreserved GPU was marked too: %q", got)
}
}
// What the hypervisor reports is the stronger fact and must not be
// overwritten by our own weaker record of intent.
func TestAnAttachedDeviceKeepsItsOwnLabel(t *testing.T) {
sites := []model.Site{{
ID: "hv01",
Nodes: []model.Node{{
Name: "hv01", Online: true,
Devices: []model.Device{{NativeID: "0000:01:00.0", AllocatedTo: "vm 9099 (someone-else)"}},
}},
}}
model.ApplyReservations(sites, []model.Reservation{
{Site: "hv01", HostNode: "hv01", NativeID: "0000:01:00.0", HeldBy: "c002-1"},
})
if got := sites[0].Nodes[0].Devices[0].AllocatedTo; got != "vm 9099 (someone-else)" {
t.Errorf("allocated_to = %q; the live attachment must win", got)
}
}
// Adoption exists so a hand-built cluster cannot be given away underneath
// itself. The address it already uses must be protected exactly as an
// allocated one is.
func TestAdoptedClusterHoldsItsAddress(t *testing.T) {
s := testStore(t)
ctx := context.Background()
tenant := mustTenant(t, s, "acme")
adopted, err := s.Adopt(ctx, tenant, 2, plainPlacement(), "ready", nil)
if err != nil {
t.Fatalf("adopt: %v", err)
}
if adopted.ClusterID != 2 || adopted.State != "ready" {
t.Fatalf("adopted = %+v", adopted)
}
if _, err := s.Adopt(ctx, tenant, 2, plainPlacement(), "ready", nil); err == nil {
t.Error("adopting the same address twice was allowed")
}
// And the allocator must step over it rather than onto it.
first, err := s.AllocateCluster(ctx, tenant, plainPlacement(), 0, nil, nil)
if err != nil {
t.Fatalf("allocate: %v", err)
}
second, err := s.AllocateCluster(ctx, tenant, plainPlacement(), 0, nil, nil)
if err != nil {
t.Fatalf("allocate: %v", err)
}
if first.ClusterID != 1 || second.ClusterID != 3 {
t.Errorf("allocator gave %d then %d; want 1 then 3, stepping over the adopted 2",
first.ClusterID, second.ClusterID)
}
}
func TestAdoptionTakesTheHardwareToo(t *testing.T) {
s := testStore(t)
ctx := context.Background()
tenant := mustTenant(t, s, "acme")
if _, err := s.Adopt(ctx, tenant, 2, withGPU("hv01", "0000:01:00.0"), "ready", nil); err != nil {
t.Fatalf("adopt: %v", err)
}
if _, err := s.AllocateCluster(ctx, tenant, withGPU("hv01", "0000:01:00.0"), 0, nil, nil); !errors.Is(err, ErrDeviceHeld) {
t.Errorf("the adopted cluster's card was offered again: %v", err)
}
}
// A field that is populated on one path and blank on another is worse than
// one that is always absent: nothing notices until something renders the
// blank.
func TestTenantNameIsSetOnEveryPath(t *testing.T) {
s := testStore(t)
ctx := context.Background()
tenant := mustTenant(t, s, "acme")
allocated, err := s.AllocateCluster(ctx, tenant, plainPlacement(), time.Hour, nil, nil)
if err != nil {
t.Fatalf("allocate: %v", err)
}
if allocated.TenantName != "acme" {
t.Errorf("AllocateCluster returned tenant name %q", allocated.TenantName)
}
adopted, err := s.Adopt(ctx, tenant, 99, plainPlacement(), "ready", nil)
if err != nil {
t.Fatalf("adopt: %v", err)
}
if adopted.TenantName != "acme" {
t.Errorf("Adopt returned tenant name %q", adopted.TenantName)
}
live, err := s.Live(ctx)
if err != nil {
t.Fatalf("live: %v", err)
}
for _, c := range live {
if c.TenantName != "acme" {
t.Errorf("Live returned %s with tenant name %q", c.AccountingName, c.TenantName)
}
}
}