diff --git a/seaweed-block/cmd/blockmaster/cluster_spec.go b/seaweed-block/cmd/blockmaster/cluster_spec.go new file mode 100644 index 0000000000..8e4a4c2c4e --- /dev/null +++ b/seaweed-block/cmd/blockmaster/cluster_spec.go @@ -0,0 +1,84 @@ +package main + +import ( + "fmt" + "os" + + "github.com/seaweedfs/seaweed-block/core/lifecycle" + "gopkg.in/yaml.v3" +) + +type clusterSpecFile struct { + Volumes []clusterSpecVolume `yaml:"volumes"` +} + +type clusterSpecVolume struct { + ID string `yaml:"id"` + SizeBytes uint64 `yaml:"size_bytes"` + ReplicationFactor int `yaml:"replication_factor"` + Placements []clusterSpecPlacementSlot `yaml:"placements"` +} + +type clusterSpecPlacementSlot struct { + ServerID string `yaml:"server_id"` + ReplicaID string `yaml:"replica_id"` + Source string `yaml:"source"` +} + +type clusterSpecImport struct { + Topology topologyFile + Placements []lifecycle.PlacementIntent +} + +func loadClusterSpec(path string) (clusterSpecImport, error) { + raw, err := os.ReadFile(path) + if err != nil { + return clusterSpecImport{}, fmt.Errorf("cluster spec: read %q: %w", path, err) + } + var spec clusterSpecFile + if err := yaml.Unmarshal(raw, &spec); err != nil { + return clusterSpecImport{}, fmt.Errorf("cluster spec: parse %q: %w", path, err) + } + return clusterSpecToImports(spec) +} + +func clusterSpecToImports(spec clusterSpecFile) (clusterSpecImport, error) { + var out clusterSpecImport + for i, vol := range spec.Volumes { + if vol.ID == "" { + return clusterSpecImport{}, fmt.Errorf("cluster spec: volume[%d] missing id", i) + } + if vol.ReplicationFactor <= 0 { + return clusterSpecImport{}, fmt.Errorf("cluster spec: volume %q replication_factor must be > 0", vol.ID) + } + if len(vol.Placements) != vol.ReplicationFactor { + return clusterSpecImport{}, fmt.Errorf("cluster spec: volume %q placements %d != replication_factor %d", vol.ID, len(vol.Placements), vol.ReplicationFactor) + } + topVol := topologyVolume{VolumeID: vol.ID} + intent := lifecycle.PlacementIntent{ + VolumeID: vol.ID, + DesiredRF: vol.ReplicationFactor, + } + for j, slot := range vol.Placements { + if slot.ServerID == "" || slot.ReplicaID == "" { + return clusterSpecImport{}, fmt.Errorf("cluster spec: volume %q placement[%d] missing server_id or replica_id", vol.ID, j) + } + source := slot.Source + if source == "" { + source = lifecycle.PlacementSourceExistingReplica + } + topVol.Slots = append(topVol.Slots, topologySlot{ + ServerID: slot.ServerID, + ReplicaID: slot.ReplicaID, + }) + intent.Slots = append(intent.Slots, lifecycle.PlacementSlotIntent{ + ServerID: slot.ServerID, + ReplicaID: slot.ReplicaID, + Source: source, + }) + } + out.Topology.Volumes = append(out.Topology.Volumes, topVol) + out.Placements = append(out.Placements, intent) + } + return out, nil +} diff --git a/seaweed-block/cmd/blockmaster/cluster_spec_test.go b/seaweed-block/cmd/blockmaster/cluster_spec_test.go new file mode 100644 index 0000000000..b39318b63b --- /dev/null +++ b/seaweed-block/cmd/blockmaster/cluster_spec_test.go @@ -0,0 +1,47 @@ +package main + +import ( + "os" + "path/filepath" + "testing" +) + +func TestClusterSpec_ImportsDesiredVolumeNodeAndPlacement(t *testing.T) { + path := writeClusterSpecForTest(t) + imports, err := loadClusterSpec(path) + if err != nil { + t.Fatalf("load cluster spec: %v", err) + } + if len(imports.Topology.Volumes) != 1 || imports.Topology.Volumes[0].VolumeID != "v1" { + t.Fatalf("topology=%+v want one v1 volume", imports.Topology) + } + if len(imports.Topology.Volumes[0].Slots) != 1 || imports.Topology.Volumes[0].Slots[0].ReplicaID != "r2" { + t.Fatalf("topology slots=%+v want r2", imports.Topology.Volumes[0].Slots) + } + if len(imports.Placements) != 1 || imports.Placements[0].VolumeID != "v1" || imports.Placements[0].DesiredRF != 1 { + t.Fatalf("placements=%+v want v1 rf=1", imports.Placements) + } + slot := imports.Placements[0].Slots[0] + if slot.ServerID != "s2" || slot.ReplicaID != "r2" || slot.Source != "existing_replica" { + t.Fatalf("placement slot=%+v want s2/r2 existing_replica", slot) + } +} + +func writeClusterSpecForTest(t *testing.T) string { + t.Helper() + path := filepath.Join(t.TempDir(), "m01.yaml") + raw := []byte(` +volumes: + - id: v1 + size_bytes: 1048576 + replication_factor: 1 + placements: + - server_id: s2 + replica_id: r2 + source: existing_replica +`) + if err := os.WriteFile(path, raw, 0o644); err != nil { + t.Fatalf("write cluster spec: %v", err) + } + return path +} diff --git a/seaweed-block/cmd/blockmaster/main.go b/seaweed-block/cmd/blockmaster/main.go index 0d362c7522..8c8bb06df2 100644 --- a/seaweed-block/cmd/blockmaster/main.go +++ b/seaweed-block/cmd/blockmaster/main.go @@ -23,6 +23,7 @@ type flags struct { authorityStore string lifecycleStore string lifecyclePlacementSeed string + clusterSpec string listen string topology string expectedSlotsPerVol int @@ -42,6 +43,7 @@ func parseFlags(args []string) (flags, error) { fs.StringVar(&f.authorityStore, "authority-store", "", "durable authority store directory (required)") fs.StringVar(&f.lifecycleStore, "lifecycle-store", "", "optional G9D product lifecycle registration store directory (desired volumes, node inventory, placement intents); read-only with respect to assignment publication") fs.StringVar(&f.lifecyclePlacementSeed, "lifecycle-placement-seed", "", "optional G9G seed JSON file containing placement intents to import into --lifecycle-store before the product loop starts") + fs.StringVar(&f.clusterSpec, "cluster-spec", "", "optional G9G-3 cluster spec YAML facade; imports accepted topology and lifecycle placement intent") fs.StringVar(&f.listen, "listen", "127.0.0.1:0", "gRPC listen address (e.g. 127.0.0.1:9180)") fs.StringVar(&f.topology, "topology", "", "path to accepted-topology YAML (required for assignment to mint)") fs.IntVar(&f.expectedSlotsPerVol, "expected-slots-per-volume", 3, "RF/expected slot count per volume; the controller rejects observation snapshots whose slot count differs (default 3, set to 2 for 2-node smoke clusters)") @@ -75,7 +77,7 @@ type readyLine struct { } func run(f flags) int { - topo, err := loadTopology(f.topology) + topo, clusterImport, err := loadProductInputs(f) if err != nil { fmt.Fprintln(os.Stderr, "blockmaster:", err) return 2 @@ -100,6 +102,13 @@ func run(f flags) int { fmt.Fprintln(os.Stderr, "blockmaster:", err) return 1 } + if len(clusterImport.Placements) > 0 { + if err := importLifecyclePlacementIntents(h, clusterImport.Placements); err != nil { + fmt.Fprintln(os.Stderr, "blockmaster:", err) + _ = h.Close(context.Background()) + return 1 + } + } if f.lifecyclePlacementSeed != "" { if err := importLifecyclePlacementSeed(h, f.lifecyclePlacementSeed); err != nil { fmt.Fprintln(os.Stderr, "blockmaster:", err) @@ -134,6 +143,25 @@ func run(f flags) int { return 0 } +func loadProductInputs(f flags) (authority.AcceptedTopology, clusterSpecImport, error) { + if f.clusterSpec == "" { + topo, err := loadTopology(f.topology) + return topo, clusterSpecImport{}, err + } + if f.topology != "" { + return authority.AcceptedTopology{}, clusterSpecImport{}, fmt.Errorf("--cluster-spec and --topology are mutually exclusive in this slice") + } + imports, err := loadClusterSpec(f.clusterSpec) + if err != nil { + return authority.AcceptedTopology{}, clusterSpecImport{}, err + } + topo, err := toAcceptedTopology(&imports.Topology) + if err != nil { + return authority.AcceptedTopology{}, clusterSpecImport{}, err + } + return topo, imports, nil +} + func importLifecyclePlacementSeed(h *master.Host, path string) error { stores := h.Lifecycle() if stores == nil { @@ -147,6 +175,14 @@ func importLifecyclePlacementSeed(h *master.Host, path string) error { if err := json.Unmarshal(raw, &intents); err != nil { return fmt.Errorf("parse lifecycle placement seed %q: %w", path, err) } + return importLifecyclePlacementIntents(h, intents) +} + +func importLifecyclePlacementIntents(h *master.Host, intents []lifecycle.PlacementIntent) error { + stores := h.Lifecycle() + if stores == nil { + return fmt.Errorf("lifecycle placement import requires --lifecycle-store") + } for _, intent := range intents { plan := lifecycle.PlacementPlan{ VolumeID: intent.VolumeID, diff --git a/seaweed-block/cmd/blockmaster/main_test.go b/seaweed-block/cmd/blockmaster/main_test.go index 1df4baf789..98f67db6e1 100644 --- a/seaweed-block/cmd/blockmaster/main_test.go +++ b/seaweed-block/cmd/blockmaster/main_test.go @@ -28,3 +28,17 @@ func TestParseFlags_LifecyclePlacementSeedOptional(t *testing.T) { t.Fatalf("lifecyclePlacementSeed=%q want seed.json", f.lifecyclePlacementSeed) } } + +func TestParseFlags_ClusterSpecOptional(t *testing.T) { + f, err := parseFlags([]string{ + "--authority-store", "authority-dir", + "--lifecycle-store", "lifecycle-dir", + "--cluster-spec", "m01.yaml", + }) + if err != nil { + t.Fatalf("parseFlags: %v", err) + } + if f.clusterSpec != "m01.yaml" { + t.Fatalf("clusterSpec=%q want m01.yaml", f.clusterSpec) + } +} diff --git a/seaweed-block/cmd/blockvolume/g9g_l2_product_loop_test.go b/seaweed-block/cmd/blockvolume/g9g_l2_product_loop_test.go index 5eab66afeb..0bfb6c6be6 100644 --- a/seaweed-block/cmd/blockvolume/g9g_l2_product_loop_test.go +++ b/seaweed-block/cmd/blockvolume/g9g_l2_product_loop_test.go @@ -7,8 +7,6 @@ import ( "path/filepath" "testing" "time" - - "github.com/seaweedfs/seaweed-block/core/lifecycle" ) func TestG9G_L2ProductLoopPublishesAssignmentToBlockvolume(t *testing.T) { @@ -18,9 +16,9 @@ func TestG9G_L2ProductLoopPublishesAssignmentToBlockvolume(t *testing.T) { bins := buildG54Binaries(t) art := t.TempDir() lifecycleDir := filepath.Join(art, "lifecycle") - seedPath := writeG9GPlacementSeed(t, art) + specPath := writeG9GClusterSpec(t, art) - _, masterAddr := startG9GMaster(t, bins, art, lifecycleDir, seedPath) + _, masterAddr := startG9GMaster(t, bins, art, lifecycleDir, specPath) r2Data, r2Status := pickAddr(t), pickAddr(t) r2Ctrl := pickAddr(t) @@ -47,32 +45,29 @@ func TestG9G_L2ProductLoopPublishesAssignmentToBlockvolume(t *testing.T) { } } -func writeG9GPlacementSeed(t *testing.T, art string) string { +func writeG9GClusterSpec(t *testing.T, art string) string { t.Helper() - seedPath := filepath.Join(art, "g9g-placement-seed.json") - raw, err := json.MarshalIndent([]lifecycle.PlacementIntent{{ - VolumeID: "v1", - DesiredRF: 1, - Slots: []lifecycle.PlacementSlotIntent{{ - ServerID: "s2", - ReplicaID: "r2", - Source: lifecycle.PlacementSourceExistingReplica, - }}, - }}, "", " ") - if err != nil { - t.Fatalf("marshal seed: %v", err) + specPath := filepath.Join(art, "m01.yaml") + raw := []byte(` +volumes: + - id: v1 + size_bytes: 1048576 + replication_factor: 1 + placements: + - server_id: s2 + replica_id: r2 + source: existing_replica +`) + if err := os.WriteFile(specPath, raw, 0o644); err != nil { + t.Fatalf("write cluster spec: %v", err) } - if err := os.WriteFile(seedPath, raw, 0o644); err != nil { - t.Fatalf("write seed: %v", err) - } - return seedPath + return specPath } -func startG9GMaster(t *testing.T, bins l2bins, art, lifecycleDir, seedPath string) (*proc, string) { +func startG9GMaster(t *testing.T, bins l2bins, art, lifecycleDir, specPath string) (*proc, string) { t.Helper() storeDir := filepath.Join(art, "master-store") _ = os.MkdirAll(storeDir, 0o755) - topo := write2SlotTopology(t, art) logPath := filepath.Join(art, "master-g9g.log") lf, err := os.Create(logPath) if err != nil { @@ -81,10 +76,9 @@ func startG9GMaster(t *testing.T, bins l2bins, art, lifecycleDir, seedPath strin cmd := exec.Command(bins.master, "--authority-store", storeDir, "--lifecycle-store", lifecycleDir, - "--lifecycle-placement-seed", seedPath, + "--cluster-spec", specPath, "--lifecycle-product-loop-interval", "100ms", "--listen", "127.0.0.1:0", - "--topology", topo, "--expected-slots-per-volume", "2", "--freshness-window", "800ms", "--pending-grace", "100ms",