From 52114f78ebd6ca0c2cfcd892ac0a2c32d098f472 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 3 May 2026 11:10:17 -0700 Subject: [PATCH] G15d: materialize launcher replica identities --- seaweed-block/core/host/master/host.go | 11 +++ .../core/host/master/launcher_plan.go | 84 +++++++++++++++++++ .../core/host/master/launcher_plan_test.go | 83 ++++++++++++++++++ .../core/lifecycle/workload_materialize.go | 34 ++++++++ .../core/lifecycle/workload_plan_test.go | 53 ++++++++++++ 5 files changed, 265 insertions(+) create mode 100644 seaweed-block/core/host/master/launcher_plan.go create mode 100644 seaweed-block/core/host/master/launcher_plan_test.go create mode 100644 seaweed-block/core/lifecycle/workload_materialize.go diff --git a/seaweed-block/core/host/master/host.go b/seaweed-block/core/host/master/host.go index 3d35080155..2067b73b08 100644 --- a/seaweed-block/core/host/master/host.go +++ b/seaweed-block/core/host/master/host.go @@ -263,6 +263,17 @@ func (h *Host) replicaSlotsFor(volumeID string) []string { } return out } + if h.lifecycle != nil && h.lifecycle.Placements != nil { + if placement, ok := h.lifecycle.Placements.GetPlacement(volumeID); ok { + out := make([]string, 0, len(placement.Slots)) + for _, slot := range placement.Slots { + if slot.ReplicaID != "" { + out = append(out, slot.ReplicaID) + } + } + return out + } + } return nil } diff --git a/seaweed-block/core/host/master/launcher_plan.go b/seaweed-block/core/host/master/launcher_plan.go new file mode 100644 index 0000000000..9f59955540 --- /dev/null +++ b/seaweed-block/core/host/master/launcher_plan.go @@ -0,0 +1,84 @@ +package master + +import "github.com/seaweedfs/seaweed-block/core/lifecycle" + +// LifecycleWorkloadPlanTickResult summarizes the launcher planning seam. Plans +// are workload intents only; they do not mint authority. +type LifecycleWorkloadPlanTickResult struct { + PlannedVolumes int + MaterializedPlacements int + Plans []lifecycle.BlockVolumeWorkloadPlan + SkippedMissingVolume int + SkippedMissingInventory int +} + +// RunLifecycleWorkloadPlanTick converts desired lifecycle state into +// blockvolume workload plans. If a plan assigns concrete replica IDs for +// blank-pool slots, it writes those identities back as existing-replica +// placement intent so the launched blockvolume daemons can later verify via +// heartbeat. +func (h *Host) RunLifecycleWorkloadPlanTick(cfg lifecycle.WorkloadPlanConfig) (LifecycleWorkloadPlanTickResult, error) { + stores := h.Lifecycle() + if stores == nil { + return LifecycleWorkloadPlanTickResult{}, nil + } + volumes := stores.Volumes.ListVolumes() + volumeByID := make(map[string]lifecycle.VolumeRecord, len(volumes)) + for _, volume := range volumes { + volumeByID[volume.Spec.VolumeID] = volume + } + nodes := stores.Nodes.ListNodes() + var result LifecycleWorkloadPlanTickResult + for _, placement := range stores.Placements.ListPlacements() { + volume, ok := volumeByID[placement.VolumeID] + if !ok { + result.SkippedMissingVolume++ + continue + } + plan, err := lifecycle.PlanBlockVolumeWorkloads(volume, placement, nodes, cfg) + if err != nil { + result.SkippedMissingInventory++ + continue + } + result.Plans = append(result.Plans, plan) + result.PlannedVolumes++ + if placementHasBlankPool(placement) { + materialized, err := lifecycle.MaterializePlacementFromWorkloadPlan(placement, plan) + if err != nil { + return result, err + } + if _, err := stores.Placements.ApplyPlan(placementPlanFromIntent(materialized)); err != nil { + return result, err + } + result.MaterializedPlacements++ + } + } + return result, nil +} + +func placementHasBlankPool(intent lifecycle.PlacementIntent) bool { + for _, slot := range intent.Slots { + if slot.Source == lifecycle.PlacementSourceBlankPool { + return true + } + } + return false +} + +func placementPlanFromIntent(intent lifecycle.PlacementIntent) lifecycle.PlacementPlan { + plan := lifecycle.PlacementPlan{ + VolumeID: intent.VolumeID, + DesiredRF: intent.DesiredRF, + Candidates: make([]lifecycle.PlacementCandidate, 0, len(intent.Slots)), + } + for _, slot := range intent.Slots { + plan.Candidates = append(plan.Candidates, lifecycle.PlacementCandidate{ + VolumeID: intent.VolumeID, + ServerID: slot.ServerID, + PoolID: slot.PoolID, + ReplicaID: slot.ReplicaID, + Source: slot.Source, + }) + } + return plan +} diff --git a/seaweed-block/core/host/master/launcher_plan_test.go b/seaweed-block/core/host/master/launcher_plan_test.go new file mode 100644 index 0000000000..9b012f870f --- /dev/null +++ b/seaweed-block/core/host/master/launcher_plan_test.go @@ -0,0 +1,83 @@ +package master + +import ( + "testing" + + "github.com/seaweedfs/seaweed-block/core/lifecycle" +) + +func TestG15d_WorkloadPlanTickMaterializesBlankPoolPlacement(t *testing.T) { + h := newTestMaster(t, t.TempDir()) + defer closeTestMaster(t, h) + stores := h.Lifecycle() + if _, err := stores.Volumes.CreateVolume(lifecycle.VolumeSpec{ + VolumeID: "pvc-a", + SizeBytes: 1 << 20, + ReplicationFactor: 1, + }); err != nil { + t.Fatalf("create volume: %v", err) + } + if _, err := stores.Nodes.RegisterNode(lifecycle.NodeRegistration{ + ServerID: "m02", + DataAddr: "10.0.0.2:9201", + CtrlAddr: "10.0.0.2:9101", + Pools: []lifecycle.StoragePool{{ + PoolID: "default", + TotalBytes: 1 << 30, + FreeBytes: 1 << 30, + BlockSize: 4096, + }}, + }); err != nil { + t.Fatalf("register node: %v", err) + } + if _, err := h.RunLifecycleProductTick(); err != nil { + t.Fatalf("product tick: %v", err) + } + before, ok := stores.Placements.GetPlacement("pvc-a") + if !ok { + t.Fatal("placement not reconciled") + } + if before.Slots[0].Source != lifecycle.PlacementSourceBlankPool || before.Slots[0].ReplicaID != "" { + t.Fatalf("before=%+v want blank pool without replica id", before.Slots[0]) + } + + result, err := h.RunLifecycleWorkloadPlanTick(lifecycle.WorkloadPlanConfig{ISCSIPortBase: 3260}) + if err != nil { + t.Fatalf("workload plan tick: %v", err) + } + if result.PlannedVolumes != 1 || result.MaterializedPlacements != 1 || len(result.Plans) != 1 { + t.Fatalf("result=%+v want one planned+materialized volume", result) + } + if got := result.Plans[0].Replicas[0].ReplicaID; got != "r1" { + t.Fatalf("planned replica=%q want r1", got) + } + after, ok := stores.Placements.GetPlacement("pvc-a") + if !ok { + t.Fatal("placement disappeared") + } + if after.Slots[0].Source != lifecycle.PlacementSourceExistingReplica || after.Slots[0].ReplicaID != "r1" { + t.Fatalf("after=%+v want materialized existing replica r1", after.Slots[0]) + } + if _, ok := h.Publisher().VolumeAuthorityLine("pvc-a"); ok { + t.Fatal("workload planning/materialization must not mint authority") + } +} + +func TestG15d_ReplicaSlotsForFallsBackToLifecyclePlacement(t *testing.T) { + h := newTestMaster(t, t.TempDir()) + defer closeTestMaster(t, h) + if _, err := h.Lifecycle().Placements.ApplyPlan(lifecycle.PlacementPlan{ + VolumeID: "pvc-a", + DesiredRF: 2, + Candidates: []lifecycle.PlacementCandidate{ + {VolumeID: "pvc-a", ServerID: "node-a", ReplicaID: "r1", Source: lifecycle.PlacementSourceExistingReplica}, + {VolumeID: "pvc-a", ServerID: "node-b", ReplicaID: "r2", Source: lifecycle.PlacementSourceExistingReplica}, + }, + }); err != nil { + t.Fatalf("apply placement: %v", err) + } + got := h.replicaSlotsFor("pvc-a") + if len(got) != 2 || got[0] != "r1" || got[1] != "r2" { + t.Fatalf("slots=%v want r1,r2 from lifecycle placement", got) + } +} diff --git a/seaweed-block/core/lifecycle/workload_materialize.go b/seaweed-block/core/lifecycle/workload_materialize.go new file mode 100644 index 0000000000..689aec17f1 --- /dev/null +++ b/seaweed-block/core/lifecycle/workload_materialize.go @@ -0,0 +1,34 @@ +package lifecycle + +import "fmt" + +// MaterializePlacementFromWorkloadPlan records concrete replica IDs chosen by +// the launcher back into placement intent. It converts blank-pool slots into +// existing-replica slots only after a workload plan has produced per-replica +// identities. +// +// This remains non-authority: it does not choose primary, mark readiness, or +// mint epoch/endpoint-version. +func MaterializePlacementFromWorkloadPlan(placement PlacementIntent, plan BlockVolumeWorkloadPlan) (PlacementIntent, error) { + if placement.VolumeID != plan.VolumeID { + return PlacementIntent{}, fmt.Errorf("%w: workload plan volume %q != placement volume %q", ErrInvalidVolumeSpec, plan.VolumeID, placement.VolumeID) + } + if len(placement.Slots) != len(plan.Replicas) { + return PlacementIntent{}, fmt.Errorf("%w: workload replica count %d != placement slot count %d", ErrInvalidVolumeSpec, len(plan.Replicas), len(placement.Slots)) + } + out := placement + out.Slots = append([]PlacementSlotIntent(nil), placement.Slots...) + for i := range out.Slots { + slot := &out.Slots[i] + replica := plan.Replicas[i] + if slot.ServerID != replica.ServerID { + return PlacementIntent{}, fmt.Errorf("%w: workload replica[%d] server %q != placement server %q", ErrInvalidVolumeSpec, i, replica.ServerID, slot.ServerID) + } + if replica.ReplicaID == "" { + return PlacementIntent{}, fmt.Errorf("%w: workload replica[%d] missing replica id", ErrInvalidVolumeSpec, i) + } + slot.ReplicaID = replica.ReplicaID + slot.Source = PlacementSourceExistingReplica + } + return out, nil +} diff --git a/seaweed-block/core/lifecycle/workload_plan_test.go b/seaweed-block/core/lifecycle/workload_plan_test.go index c1bde8ed0f..d380a2c2dc 100644 --- a/seaweed-block/core/lifecycle/workload_plan_test.go +++ b/seaweed-block/core/lifecycle/workload_plan_test.go @@ -87,6 +87,59 @@ func TestG15d_WorkloadPlan_IsIdempotentForSameInputs(t *testing.T) { } } +func TestG15d_MaterializePlacementFromWorkloadPlan_BlankPoolBecomesReplicaSlots(t *testing.T) { + placement := PlacementIntent{ + VolumeID: "pvc-a", + DesiredRF: 2, + Slots: []PlacementSlotIntent{ + {ServerID: "node-a", PoolID: "pool-a", Source: PlacementSourceBlankPool}, + {ServerID: "node-b", PoolID: "pool-b", Source: PlacementSourceBlankPool}, + }, + } + plan := BlockVolumeWorkloadPlan{ + VolumeID: "pvc-a", + Replicas: []BlockVolumeReplicaWorkload{ + {ServerID: "node-a", ReplicaID: "r1"}, + {ServerID: "node-b", ReplicaID: "r2"}, + }, + } + got, err := MaterializePlacementFromWorkloadPlan(placement, plan) + if err != nil { + t.Fatalf("MaterializePlacementFromWorkloadPlan: %v", err) + } + if got.Slots[0].ReplicaID != "r1" || got.Slots[1].ReplicaID != "r2" { + t.Fatalf("replica ids=%q/%q want r1/r2", got.Slots[0].ReplicaID, got.Slots[1].ReplicaID) + } + for _, slot := range got.Slots { + if slot.Source != PlacementSourceExistingReplica { + t.Fatalf("slot=%+v want existing_replica after materialization", slot) + } + } + if placement.Slots[0].ReplicaID != "" || placement.Slots[0].Source != PlacementSourceBlankPool { + t.Fatalf("input mutated: %+v", placement.Slots[0]) + } +} + +func TestG15d_MaterializePlacementFromWorkloadPlan_FailsOnMismatchedServer(t *testing.T) { + _, err := MaterializePlacementFromWorkloadPlan( + PlacementIntent{ + VolumeID: "pvc-a", + DesiredRF: 1, + Slots: []PlacementSlotIntent{{ServerID: "node-a", Source: PlacementSourceBlankPool}}, + }, + BlockVolumeWorkloadPlan{ + VolumeID: "pvc-a", + Replicas: []BlockVolumeReplicaWorkload{{ + ServerID: "node-b", + ReplicaID: "r1", + }}, + }, + ) + if err == nil { + t.Fatal("expected mismatch error") + } +} + func TestG15d_WorkloadPlan_IsNotAuthorityShaped(t *testing.T) { for _, name := range []string{"BlockVolumeWorkloadPlan", "BlockVolumeReplicaWorkload"} { typ := mustParseStruct(t, "workload_plan.go", name)