diff --git a/seaweed-block/cmd/blockmaster/main.go b/seaweed-block/cmd/blockmaster/main.go index 2df8854a17..0d362c7522 100644 --- a/seaweed-block/cmd/blockmaster/main.go +++ b/seaweed-block/cmd/blockmaster/main.go @@ -16,17 +16,19 @@ import ( "github.com/seaweedfs/seaweed-block/core/authority" "github.com/seaweedfs/seaweed-block/core/host/master" + "github.com/seaweedfs/seaweed-block/core/lifecycle" ) type flags struct { - authorityStore string - lifecycleStore string - listen string - topology string - expectedSlotsPerVol int - freshnessWindow time.Duration - pendingGrace time.Duration - lifecycleLoop time.Duration + authorityStore string + lifecycleStore string + lifecyclePlacementSeed string + listen string + topology string + expectedSlotsPerVol int + freshnessWindow time.Duration + pendingGrace time.Duration + lifecycleLoop time.Duration // printReadyLine: test-only flag that emits a single // structured JSON line to stdout after the gRPC listener is // bound, so L2 subprocess tests can parse the ready event. @@ -39,6 +41,7 @@ func parseFlags(args []string) (flags, error) { fs := flag.NewFlagSet("blockmaster", flag.ContinueOnError) 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.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)") @@ -97,6 +100,13 @@ func run(f flags) int { fmt.Fprintln(os.Stderr, "blockmaster:", err) return 1 } + if f.lifecyclePlacementSeed != "" { + if err := importLifecyclePlacementSeed(h, f.lifecyclePlacementSeed); err != nil { + fmt.Fprintln(os.Stderr, "blockmaster:", err) + _ = h.Close(context.Background()) + return 1 + } + } if f.printReadyLine { _ = json.NewEncoder(os.Stdout).Encode(readyLine{ @@ -124,6 +134,41 @@ func run(f flags) int { return 0 } +func importLifecyclePlacementSeed(h *master.Host, path string) error { + stores := h.Lifecycle() + if stores == nil { + return fmt.Errorf("--lifecycle-placement-seed requires --lifecycle-store") + } + raw, err := os.ReadFile(path) + if err != nil { + return fmt.Errorf("read lifecycle placement seed %q: %w", path, err) + } + var intents []lifecycle.PlacementIntent + if err := json.Unmarshal(raw, &intents); err != nil { + return fmt.Errorf("parse lifecycle placement seed %q: %w", path, err) + } + for _, intent := range intents { + 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, + }) + } + if _, err := stores.Placements.ApplyPlan(plan); err != nil { + return fmt.Errorf("apply lifecycle placement seed %s: %w", intent.VolumeID, err) + } + } + return nil +} + func runLifecycleProductLoop(h *master.Host, interval time.Duration) { ticker := time.NewTicker(interval) defer ticker.Stop() diff --git a/seaweed-block/cmd/blockmaster/main_test.go b/seaweed-block/cmd/blockmaster/main_test.go index 7368ef132a..1df4baf789 100644 --- a/seaweed-block/cmd/blockmaster/main_test.go +++ b/seaweed-block/cmd/blockmaster/main_test.go @@ -14,3 +14,17 @@ func TestParseFlags_LifecycleStoreOptional(t *testing.T) { t.Fatalf("lifecycleStore=%q want lifecycle-dir", f.lifecycleStore) } } + +func TestParseFlags_LifecyclePlacementSeedOptional(t *testing.T) { + f, err := parseFlags([]string{ + "--authority-store", "authority-dir", + "--lifecycle-store", "lifecycle-dir", + "--lifecycle-placement-seed", "seed.json", + }) + if err != nil { + t.Fatalf("parseFlags: %v", err) + } + if f.lifecyclePlacementSeed != "seed.json" { + t.Fatalf("lifecyclePlacementSeed=%q want seed.json", f.lifecyclePlacementSeed) + } +} 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 0fbdc3802a..5eab66afeb 100644 --- a/seaweed-block/cmd/blockvolume/g9g_l2_product_loop_test.go +++ b/seaweed-block/cmd/blockvolume/g9g_l2_product_loop_test.go @@ -18,9 +18,9 @@ func TestG9G_L2ProductLoopPublishesAssignmentToBlockvolume(t *testing.T) { bins := buildG54Binaries(t) art := t.TempDir() lifecycleDir := filepath.Join(art, "lifecycle") - seedG9GExistingReplicaPlacement(t, lifecycleDir) + seedPath := writeG9GPlacementSeed(t, art) - _, masterAddr := startG9GMaster(t, bins, art, lifecycleDir) + _, masterAddr := startG9GMaster(t, bins, art, lifecycleDir, seedPath) r2Data, r2Status := pickAddr(t), pickAddr(t) r2Ctrl := pickAddr(t) @@ -47,33 +47,28 @@ func TestG9G_L2ProductLoopPublishesAssignmentToBlockvolume(t *testing.T) { } } -func seedG9GExistingReplicaPlacement(t *testing.T, lifecycleDir string) { +func writeG9GPlacementSeed(t *testing.T, art string) string { t.Helper() - placementStore, err := lifecycle.OpenPlacementIntentStore(filepath.Join(lifecycleDir, "placements")) - if err != nil { - t.Fatalf("open placement store: %v", err) - } - if _, err := placementStore.ApplyPlan(lifecycle.PlacementPlan{ + seedPath := filepath.Join(art, "g9g-placement-seed.json") + raw, err := json.MarshalIndent([]lifecycle.PlacementIntent{{ VolumeID: "v1", DesiredRF: 1, - Candidates: []lifecycle.PlacementCandidate{{ - VolumeID: "v1", + Slots: []lifecycle.PlacementSlotIntent{{ ServerID: "s2", ReplicaID: "r2", Source: lifecycle.PlacementSourceExistingReplica, }}, - }); err != nil { - t.Fatalf("apply placement: %v", err) + }}, "", " ") + if err != nil { + t.Fatalf("marshal seed: %v", err) } - if _, err := lifecycle.OpenFileStore(filepath.Join(lifecycleDir, "volumes")); err != nil { - t.Fatalf("open volume store: %v", err) - } - if _, err := lifecycle.OpenNodeInventoryStore(filepath.Join(lifecycleDir, "nodes")); err != nil { - t.Fatalf("open node store: %v", err) + if err := os.WriteFile(seedPath, raw, 0o644); err != nil { + t.Fatalf("write seed: %v", err) } + return seedPath } -func startG9GMaster(t *testing.T, bins l2bins, art, lifecycleDir string) (*proc, string) { +func startG9GMaster(t *testing.T, bins l2bins, art, lifecycleDir, seedPath string) (*proc, string) { t.Helper() storeDir := filepath.Join(art, "master-store") _ = os.MkdirAll(storeDir, 0o755) @@ -86,6 +81,7 @@ func startG9GMaster(t *testing.T, bins l2bins, art, lifecycleDir string) (*proc, cmd := exec.Command(bins.master, "--authority-store", storeDir, "--lifecycle-store", lifecycleDir, + "--lifecycle-placement-seed", seedPath, "--lifecycle-product-loop-interval", "100ms", "--listen", "127.0.0.1:0", "--topology", topo,