diff --git a/internal/control/reconciler/spread_test.go b/internal/control/reconciler/spread_test.go new file mode 100644 index 0000000..e7f61c1 --- /dev/null +++ b/internal/control/reconciler/spread_test.go @@ -0,0 +1,68 @@ +package reconciler + +import ( + "context" + "testing" + "time" + + "gitea.mynodes.xyz/ilker/llmgw/internal/control/catalog" + "gitea.mynodes.xyz/ilker/llmgw/internal/control/inventory" +) + +func cpuNode(id string) inventory.Node { + return inventory.Node{ID: id, MgmtAddr: "10.0.0." + id, State: inventory.Ready} +} + +// TestCPUReplicasSpreadOverSuccessiveTicks checks the placement that has no +// allocation to stop it. Two CPU replicas on one machine compete for the same +// cores, and unlike GPUs nothing in the inventory prevents it. +// +// Spreading is a preference rather than a rule, so this only holds when there +// is somewhere else to go: with one node registered, all three land on it and +// that is correct. Nothing rebalances them afterwards — v1 has no autoscaler, +// and a replica that moved on its own would interrupt the traffic it is serving. +func TestCPUReplicasSpreadOverSuccessiveTicks(t *testing.T) { + m := catalog.Model{ + Name: "qwen", Source: "/m.gguf", + Scaling: catalog.Scaling{Min: 3, Max: 3}, + Profiles: []catalog.Profile{{Name: "cpu", Engine: "llamacpp"}}, + } + h := newHarness(t, m, cpuNode("1"), cpuNode("2"), cpuNode("3")) + ctx := context.Background() + for range 6 { + h.r.Reconcile(ctx) + h.markReady() + h.advance(time.Second) + } + + byNode := map[string]int{} + for _, rep := range h.r.Replicas() { + for _, s := range rep.Shards { + byNode[s.NodeID]++ + } + } + if len(byNode) != 3 { + t.Errorf("three replicas landed on %d nodes: %v", len(byNode), byNode) + } +} + +func TestCPUReplicasStackWhenThereIsNowhereElse(t *testing.T) { + // The other half of "preference, not rule": one node available means one + // node used, rather than a model left unplaced to preserve a preference. + m := catalog.Model{ + Name: "qwen", Source: "/m.gguf", + Scaling: catalog.Scaling{Min: 3, Max: 3}, + Profiles: []catalog.Profile{{Name: "cpu", Engine: "llamacpp"}}, + } + h := newHarness(t, m, cpuNode("1")) + ctx := context.Background() + for range 6 { + h.r.Reconcile(ctx) + h.markReady() + h.advance(time.Second) + } + + if n := len(h.r.Replicas()); n != 3 { + t.Errorf("%d replicas placed on the only node available, want 3", n) + } +}