diff --git a/seaweed-block/core/lifecycle/workload_plan.go b/seaweed-block/core/lifecycle/workload_plan.go new file mode 100644 index 0000000000..ef45077e2d --- /dev/null +++ b/seaweed-block/core/lifecycle/workload_plan.go @@ -0,0 +1,90 @@ +package lifecycle + +import ( + "fmt" + "sort" +) + +// BlockVolumeWorkloadPlan is launcher input. It describes which blockvolume +// daemons should exist for one desired volume. It is deliberately not +// authority-shaped: no epoch, endpoint_version, primary, ready, or healthy. +type BlockVolumeWorkloadPlan struct { + VolumeID string + SizeBytes uint64 + Replicas []BlockVolumeReplicaWorkload +} + +type BlockVolumeReplicaWorkload struct { + ServerID string + PoolID string + ReplicaID string + Source string + DataAddr string + CtrlAddr string + ISCSIListenPort int + ISCSIQualifiedName string +} + +// PlanBlockVolumeWorkloads converts desired lifecycle state plus placement +// intent into blockvolume daemon workload intent. It does not verify +// observation, choose primary, or publish authority. +func PlanBlockVolumeWorkloads(volume VolumeRecord, placement PlacementIntent, nodes []NodeRegistration, cfg WorkloadPlanConfig) (BlockVolumeWorkloadPlan, error) { + if err := validateSpec(volume.Spec); err != nil { + return BlockVolumeWorkloadPlan{}, err + } + if placement.VolumeID != volume.Spec.VolumeID { + return BlockVolumeWorkloadPlan{}, fmt.Errorf("%w: placement volume %q != desired volume %q", ErrInvalidVolumeSpec, placement.VolumeID, volume.Spec.VolumeID) + } + if err := validatePlacementIntent(placement); err != nil { + return BlockVolumeWorkloadPlan{}, err + } + if cfg.ISCSIPortBase == 0 { + cfg.ISCSIPortBase = 3260 + } + if cfg.IQNPrefix == "" { + cfg.IQNPrefix = "iqn.2026-05.io.seaweedfs" + } + nodeByID := make(map[string]NodeRegistration, len(nodes)) + for _, node := range nodes { + nodeByID[node.ServerID] = node + } + + out := BlockVolumeWorkloadPlan{ + VolumeID: volume.Spec.VolumeID, + SizeBytes: volume.Spec.SizeBytes, + Replicas: make([]BlockVolumeReplicaWorkload, 0, len(placement.Slots)), + } + for i, slot := range placement.Slots { + node, ok := nodeByID[slot.ServerID] + if !ok { + return BlockVolumeWorkloadPlan{}, fmt.Errorf("%w: placement server %q has no node inventory", ErrInvalidNodeRegistration, slot.ServerID) + } + replicaID := slot.ReplicaID + if replicaID == "" { + replicaID = fmt.Sprintf("r%d", i+1) + } + dataAddr, ctrlAddr := nodePlacementAddrs(node) + out.Replicas = append(out.Replicas, BlockVolumeReplicaWorkload{ + ServerID: slot.ServerID, + PoolID: slot.PoolID, + ReplicaID: replicaID, + Source: slot.Source, + DataAddr: dataAddr, + CtrlAddr: ctrlAddr, + ISCSIListenPort: cfg.ISCSIPortBase + i, + ISCSIQualifiedName: fmt.Sprintf("%s:%s", cfg.IQNPrefix, volume.Spec.VolumeID), + }) + } + return out, nil +} + +type WorkloadPlanConfig struct { + ISCSIPortBase int + IQNPrefix string +} + +func SortWorkloadPlans(plans []BlockVolumeWorkloadPlan) { + sort.Slice(plans, func(i, j int) bool { + return plans[i].VolumeID < plans[j].VolumeID + }) +} diff --git a/seaweed-block/core/lifecycle/workload_plan_test.go b/seaweed-block/core/lifecycle/workload_plan_test.go new file mode 100644 index 0000000000..c1bde8ed0f --- /dev/null +++ b/seaweed-block/core/lifecycle/workload_plan_test.go @@ -0,0 +1,107 @@ +package lifecycle + +import "testing" + +func TestG15d_WorkloadPlan_BlankPoolRF2CreatesReplicaWorkloads(t *testing.T) { + volume := VolumeRecord{Spec: VolumeSpec{ + VolumeID: "pvc-a", + SizeBytes: 1 << 20, + ReplicationFactor: 2, + }} + 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, err := PlanBlockVolumeWorkloads(volume, placement, []NodeRegistration{ + nodeForWorkload("node-a", "10.0.0.1:9201", "10.0.0.1:9101"), + nodeForWorkload("node-b", "10.0.0.2:9201", "10.0.0.2:9101"), + }, WorkloadPlanConfig{ISCSIPortBase: 3260, IQNPrefix: "iqn.test"}) + if err != nil { + t.Fatalf("PlanBlockVolumeWorkloads: %v", err) + } + if plan.VolumeID != "pvc-a" || plan.SizeBytes != 1<<20 || len(plan.Replicas) != 2 { + t.Fatalf("plan=%+v", plan) + } + if plan.Replicas[0].ReplicaID != "r1" || plan.Replicas[1].ReplicaID != "r2" { + t.Fatalf("replica ids=%q/%q want r1/r2", plan.Replicas[0].ReplicaID, plan.Replicas[1].ReplicaID) + } + if plan.Replicas[0].ISCSIListenPort != 3260 || plan.Replicas[1].ISCSIListenPort != 3261 { + t.Fatalf("iscsi ports=%d/%d", plan.Replicas[0].ISCSIListenPort, plan.Replicas[1].ISCSIListenPort) + } + if plan.Replicas[0].ISCSIQualifiedName != "iqn.test:pvc-a" { + t.Fatalf("iqn=%q", plan.Replicas[0].ISCSIQualifiedName) + } +} + +func TestG15d_WorkloadPlan_ExistingReplicaKeepsReplicaID(t *testing.T) { + volume := VolumeRecord{Spec: VolumeSpec{VolumeID: "pvc-a", SizeBytes: 1 << 20, ReplicationFactor: 1}} + placement := PlacementIntent{ + VolumeID: "pvc-a", + DesiredRF: 1, + Slots: []PlacementSlotIntent{{ + ServerID: "node-a", + ReplicaID: "r9", + Source: PlacementSourceExistingReplica, + }}, + } + plan, err := PlanBlockVolumeWorkloads(volume, placement, []NodeRegistration{ + nodeForWorkload("node-a", "10.0.0.1:9201", "10.0.0.1:9101"), + }, WorkloadPlanConfig{}) + if err != nil { + t.Fatalf("PlanBlockVolumeWorkloads: %v", err) + } + if got := plan.Replicas[0].ReplicaID; got != "r9" { + t.Fatalf("replica id=%q want r9", got) + } + if got := plan.Replicas[0].ISCSIListenPort; got != 3260 { + t.Fatalf("default iscsi port=%d want 3260", got) + } +} + +func TestG15d_WorkloadPlan_IsIdempotentForSameInputs(t *testing.T) { + volume := VolumeRecord{Spec: VolumeSpec{VolumeID: "pvc-a", SizeBytes: 1 << 20, ReplicationFactor: 1}} + placement := PlacementIntent{ + VolumeID: "pvc-a", + DesiredRF: 1, + Slots: []PlacementSlotIntent{{ + ServerID: "node-a", + PoolID: "pool-a", + Source: PlacementSourceBlankPool, + }}, + } + nodes := []NodeRegistration{nodeForWorkload("node-a", "10.0.0.1:9201", "10.0.0.1:9101")} + first, err := PlanBlockVolumeWorkloads(volume, placement, nodes, WorkloadPlanConfig{}) + if err != nil { + t.Fatalf("first plan: %v", err) + } + second, err := PlanBlockVolumeWorkloads(volume, placement, nodes, WorkloadPlanConfig{}) + if err != nil { + t.Fatalf("second plan: %v", err) + } + if first.Replicas[0] != second.Replicas[0] { + t.Fatalf("non-idempotent first=%+v second=%+v", first, second) + } +} + +func TestG15d_WorkloadPlan_IsNotAuthorityShaped(t *testing.T) { + for _, name := range []string{"BlockVolumeWorkloadPlan", "BlockVolumeReplicaWorkload"} { + typ := mustParseStruct(t, "workload_plan.go", name) + for _, forbidden := range []string{"Epoch", "EndpointVersion", "Assignment", "Ready", "Healthy", "Primary"} { + if _, ok := typ.Fields[forbidden]; ok { + t.Fatalf("%s must not carry %s", name, forbidden) + } + } + } +} + +func nodeForWorkload(serverID, dataAddr, ctrlAddr string) NodeRegistration { + return NodeRegistration{ + ServerID: serverID, + DataAddr: dataAddr, + CtrlAddr: ctrlAddr, + } +}