diff --git a/seaweed-block/core/lifecycle/reconciler.go b/seaweed-block/core/lifecycle/reconciler.go new file mode 100644 index 0000000000..639aefe39d --- /dev/null +++ b/seaweed-block/core/lifecycle/reconciler.go @@ -0,0 +1,36 @@ +package lifecycle + +// ReconcileResult reports one desired volume reconciliation attempt. +type ReconcileResult struct { + VolumeID string + Plan PlacementPlan + Intent PlacementIntent + Applied bool + Err error +} + +// ReconcilePlacement plans every desired volume against the latest node +// inventory and persists placement intent only when enough candidates exist. +// This is still controller input: no authority is minted here. +func ReconcilePlacement(volumes []VolumeRecord, nodes []NodeRegistration, placements *PlacementIntentStore) []ReconcileResult { + volumes = append([]VolumeRecord(nil), volumes...) + nodes = append([]NodeRegistration(nil), nodes...) + results := make([]ReconcileResult, 0, len(volumes)) + for _, volume := range volumes { + plan := PlanPlacement(volume, nodes) + result := ReconcileResult{ + VolumeID: volume.Spec.VolumeID, + Plan: plan, + } + intent, err := placements.ApplyPlan(plan) + if err != nil { + result.Err = err + results = append(results, result) + continue + } + result.Intent = intent + result.Applied = true + results = append(results, result) + } + return results +} diff --git a/seaweed-block/core/lifecycle/reconciler_test.go b/seaweed-block/core/lifecycle/reconciler_test.go new file mode 100644 index 0000000000..16c012460b --- /dev/null +++ b/seaweed-block/core/lifecycle/reconciler_test.go @@ -0,0 +1,126 @@ +package lifecycle + +import ( + "errors" + "testing" +) + +func TestReconcilePlacement_HappyPathWritesPlacementIntent(t *testing.T) { + volumes := []VolumeRecord{{Spec: VolumeSpec{ + VolumeID: "vol-a", + SizeBytes: 1 << 20, + ReplicationFactor: 2, + }}} + nodes := []NodeRegistration{ + nodeWithPool("node-a", "pool-a", 1<<30), + nodeWithPool("node-b", "pool-b", 1<<30), + } + store, err := OpenPlacementIntentStore(t.TempDir()) + if err != nil { + t.Fatalf("open: %v", err) + } + results := ReconcilePlacement(volumes, nodes, store) + if len(results) != 1 { + t.Fatalf("result count=%d want 1", len(results)) + } + if !results[0].Applied || results[0].Err != nil { + t.Fatalf("result not applied: %+v", results[0]) + } + intent, ok := store.GetPlacement("vol-a") + if !ok { + t.Fatal("placement intent not written") + } + if intent.DesiredRF != 2 || len(intent.Slots) != 2 { + t.Fatalf("intent=%+v want RF=2 slots=2", intent) + } +} + +func TestReconcilePlacement_InsufficientInventoryReportsButDoesNotPersist(t *testing.T) { + volumes := []VolumeRecord{{Spec: VolumeSpec{ + VolumeID: "vol-a", + SizeBytes: 1 << 20, + ReplicationFactor: 2, + }}} + nodes := []NodeRegistration{nodeWithPool("node-a", "pool-a", 1<<30)} + store, err := OpenPlacementIntentStore(t.TempDir()) + if err != nil { + t.Fatalf("open: %v", err) + } + results := ReconcilePlacement(volumes, nodes, store) + if len(results) != 1 { + t.Fatalf("result count=%d want 1", len(results)) + } + if results[0].Applied { + t.Fatalf("insufficient plan must not apply: %+v", results[0]) + } + if !errors.Is(results[0].Err, ErrInsufficientPlacementCandidates) { + t.Fatalf("err=%v want ErrInsufficientPlacementCandidates", results[0].Err) + } + if _, ok := store.GetPlacement("vol-a"); ok { + t.Fatal("insufficient reconcile must not persist placement") + } +} + +func TestReconcilePlacement_ExistingReplicaConflictIsReportOnly(t *testing.T) { + volumes := []VolumeRecord{{Spec: VolumeSpec{ + VolumeID: "vol-a", + SizeBytes: 1 << 20, + ReplicationFactor: 1, + }}} + nodes := []NodeRegistration{{ + ServerID: "node-a", + Addr: "127.0.0.1:9101", + Pools: []StoragePool{{ + PoolID: "pool-a", + TotalBytes: 1 << 30, + FreeBytes: 1 << 30, + BlockSize: 4096, + }}, + Replicas: []ReplicaInventory{{ + VolumeID: "vol-a", + ReplicaID: "r2", + StoreUUID: "store-1", + SizeBytes: 2 << 20, + State: "existing", + }}, + }} + store, err := OpenPlacementIntentStore(t.TempDir()) + if err != nil { + t.Fatalf("open: %v", err) + } + results := ReconcilePlacement(volumes, nodes, store) + if len(results) != 1 { + t.Fatalf("result count=%d want 1", len(results)) + } + if results[0].Applied { + t.Fatalf("conflict must not apply: %+v", results[0]) + } + if len(results[0].Plan.Conflicts) != 1 { + t.Fatalf("conflict count=%d want 1: %+v", len(results[0].Plan.Conflicts), results[0].Plan) + } + if _, ok := store.GetPlacement("vol-a"); ok { + t.Fatal("conflicting existing replica must not persist placement") + } +} + +func TestReconcilePlacement_ResultIsNotAuthorityShaped(t *testing.T) { + typ := mustParseStruct(t, "reconciler.go", "ReconcileResult") + for _, forbidden := range []string{"Epoch", "EndpointVersion", "Assignment", "Ready", "Healthy", "Primary"} { + if _, ok := typ.Fields[forbidden]; ok { + t.Fatalf("ReconcileResult must not carry %s", forbidden) + } + } +} + +func nodeWithPool(serverID, poolID string, freeBytes uint64) NodeRegistration { + return NodeRegistration{ + ServerID: serverID, + Addr: "127.0.0.1:9100", + Pools: []StoragePool{{ + PoolID: poolID, + TotalBytes: 1 << 30, + FreeBytes: freeBytes, + BlockSize: 4096, + }}, + } +}