G9D: wire lifecycle stores into blockmaster

This commit is contained in:
pingqiu committed 2026-05-02 20:54:57 -07:00
1 parent 992e411261
commit 70a75606af
4 files changed
+179 -17

No files matched your search

+3
View File
@@ -20,6 +20,7 @@ import (
type flags struct {
authorityStore string
lifecycleStore string
listen string
topology string
expectedSlotsPerVol int
@@ -36,6 +37,7 @@ func parseFlags(args []string) (flags, error) {
var f flags
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.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)")
@@ -82,6 +84,7 @@ func run(f flags) int {
}
cfg := master.Config{
AuthorityStoreDir: f.authorityStore,
LifecycleStoreDir: f.lifecycleStore,
Listen: f.listen,
Topology: topo,
Freshness: authority.FreshnessConfig{FreshnessWindow: f.freshnessWindow, PendingGrace: f.pendingGrace},
@@ -0,0 +1,16 @@
package main
import "testing"
func TestParseFlags_LifecycleStoreOptional(t *testing.T) {
f, err := parseFlags([]string{
"--authority-store", "authority-dir",
"--lifecycle-store", "lifecycle-dir",
})
if err != nil {
t.Fatalf("parseFlags: %v", err)
}
if f.lifecycleStore != "lifecycle-dir" {
t.Fatalf("lifecycleStore=%q want lifecycle-dir", f.lifecycleStore)
}
}
+70 -17
View File
@@ -20,11 +20,13 @@ import (
"fmt"
"log"
"net"
"path/filepath"
"sync"
"sync/atomic"
"github.com/seaweedfs/seaweed-block/core/authority"
"github.com/seaweedfs/seaweed-block/core/host/bootstrap"
"github.com/seaweedfs/seaweed-block/core/lifecycle"
control "github.com/seaweedfs/seaweed-block/core/rpc/control"
"google.golang.org/grpc"
)
@@ -51,24 +53,40 @@ type Config struct {
// ControllerConfig tunes controller knobs (retry window etc).
ControllerConfig authority.TopologyControllerConfig
// LifecycleStoreDir, when non-empty, opens product lifecycle
// registration stores (desired volumes, node inventory, placement
// intents). This is read-only composition for G9D: these stores do
// NOT drive assignment publication.
LifecycleStoreDir string
// Logger is used for structured startup and error logging. If
// nil, the default log package logger is used.
Logger *log.Logger
}
// LifecycleStores groups the product registration stores hosted by
// blockmaster. The stores are controller input only; assignment
// publication remains owned by authority.Controller/Publisher.
type LifecycleStores struct {
Volumes *lifecycle.FileStore
Nodes *lifecycle.NodeInventoryStore
Placements *lifecycle.PlacementIntentStore
}
// Host is the composed master-side block product daemon. Lifecycle
// is: New -> Start -> (serve requests) -> Close.
type Host struct {
cfg Config
log *log.Logger
boot *bootstrap.DurableAuthorityBootstrap
ctrl *authority.TopologyController
obs *authority.ObservationHost
ln net.Listener
grpc *grpc.Server
topo authority.AcceptedTopology
cancel context.CancelFunc
wg sync.WaitGroup
cfg Config
log *log.Logger
boot *bootstrap.DurableAuthorityBootstrap
ctrl *authority.TopologyController
obs *authority.ObservationHost
ln net.Listener
grpc *grpc.Server
topo authority.AcceptedTopology
lifecycle *LifecycleStores
cancel context.CancelFunc
wg sync.WaitGroup
started atomic.Bool
}
@@ -150,6 +168,12 @@ func New(cfg Config) (*Host, error) {
Reader: boot.Publisher,
})
lifecycleStores, err := openLifecycleStores(cfg.LifecycleStoreDir)
if err != nil {
_ = boot.Close()
return nil, fmt.Errorf("master.New: lifecycle stores: %w", err)
}
ln, err := net.Listen("tcp", cfg.Listen)
if err != nil {
_ = boot.Close()
@@ -157,13 +181,14 @@ func New(cfg Config) (*Host, error) {
}
h := &Host{
cfg: cfg,
log: lg,
boot: boot,
ctrl: ctrl,
obs: obs,
ln: ln,
topo: cfg.Topology,
cfg: cfg,
log: lg,
boot: boot,
ctrl: ctrl,
obs: obs,
ln: ln,
topo: cfg.Topology,
lifecycle: lifecycleStores,
}
grpcSrv := grpc.NewServer()
@@ -181,6 +206,29 @@ func New(cfg Config) (*Host, error) {
return h, nil
}
func openLifecycleStores(dir string) (*LifecycleStores, error) {
if dir == "" {
return nil, nil
}
volumes, err := lifecycle.OpenFileStore(filepath.Join(dir, "volumes"))
if err != nil {
return nil, err
}
nodes, err := lifecycle.OpenNodeInventoryStore(filepath.Join(dir, "nodes"))
if err != nil {
return nil, err
}
placements, err := lifecycle.OpenPlacementIntentStore(filepath.Join(dir, "placements"))
if err != nil {
return nil, err
}
return &LifecycleStores{
Volumes: volumes,
Nodes: nodes,
Placements: placements,
}, nil
}
// Addr returns the bound listener address. Valid after New.
func (h *Host) Addr() string { return h.ln.Addr().String() }
@@ -194,6 +242,11 @@ func (h *Host) Controller() *authority.TopologyController { return h.ctrl }
// ObservationHost exposes the observation host for tests.
func (h *Host) ObservationHost() *authority.ObservationHost { return h.obs }
// Lifecycle returns product registration stores when configured. Nil means
// the daemon is running in static-topology-only mode. Reading these stores
// must not be treated as assignment authority.
func (h *Host) Lifecycle() *LifecycleStores { return h.lifecycle }
// replicaSlotsFor returns the accepted-topology replica IDs for a
// volume, or nil if the volume isn't in accepted topology. Used
// by AssignmentService to fan-in per-(vol, rid) publisher
@@ -0,0 +1,90 @@
package master
import (
"context"
"testing"
"time"
"github.com/seaweedfs/seaweed-block/core/lifecycle"
)
func TestMasterLifecycleStore_OptionalNilByDefault(t *testing.T) {
h := newTestMaster(t, "")
defer closeTestMaster(t, h)
if h.Lifecycle() != nil {
t.Fatal("Lifecycle() = non-nil without LifecycleStoreDir")
}
}
func TestMasterLifecycleStore_OpensRegistrationStoresWithoutAuthority(t *testing.T) {
h := newTestMaster(t, t.TempDir())
defer closeTestMaster(t, h)
stores := h.Lifecycle()
if stores == nil {
t.Fatal("Lifecycle() = nil with LifecycleStoreDir")
}
if _, err := stores.Volumes.CreateVolume(lifecycle.VolumeSpec{
VolumeID: "vol-a",
SizeBytes: 1 << 20,
ReplicationFactor: 2,
}); err != nil {
t.Fatalf("create desired volume: %v", err)
}
if _, err := stores.Nodes.RegisterNode(lifecycle.NodeRegistration{
ServerID: "node-a",
Addr: "127.0.0.1:9101",
Pools: []lifecycle.StoragePool{{
PoolID: "pool-a",
TotalBytes: 1 << 30,
FreeBytes: 1 << 30,
BlockSize: 4096,
}},
}); err != nil {
t.Fatalf("register node: %v", err)
}
if _, err := stores.Nodes.RegisterNode(lifecycle.NodeRegistration{
ServerID: "node-b",
Addr: "127.0.0.1:9102",
Pools: []lifecycle.StoragePool{{
PoolID: "pool-b",
TotalBytes: 1 << 30,
FreeBytes: 1 << 30,
BlockSize: 4096,
}},
}); err != nil {
t.Fatalf("register second node: %v", err)
}
results := lifecycle.ReconcilePlacement(stores.Volumes.ListVolumes(), stores.Nodes.ListNodes(), stores.Placements)
if len(results) != 1 || !results[0].Applied {
t.Fatalf("reconcile results=%+v want one applied", results)
}
if _, ok := stores.Placements.GetPlacement("vol-a"); !ok {
t.Fatal("placement intent not written")
}
if _, ok := h.Publisher().VolumeAuthorityLine("vol-a"); ok {
t.Fatal("lifecycle registration must not mint authority")
}
}
func newTestMaster(t *testing.T, lifecycleDir string) *Host {
t.Helper()
h, err := New(Config{
AuthorityStoreDir: t.TempDir(),
LifecycleStoreDir: lifecycleDir,
Listen: "127.0.0.1:0",
})
if err != nil {
t.Fatalf("master.New: %v", err)
}
h.Start()
return h
}
func closeTestMaster(t *testing.T, h *Host) {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := h.Close(ctx); err != nil {
t.Fatalf("close: %v", err)
}
}