G9D: pin lifecycle read-only and reconcile behavior

This commit is contained in:
pingqiu committed 2026-05-02 21:00:06 -07:00
1 parent 70a75606af
commit b9df8b6e04
3 files changed
+173

No files matched your search

@@ -0,0 +1,24 @@
package master
import "github.com/seaweedfs/seaweed-block/core/lifecycle"
// LifecycleSnapshot is a read-only product registration view. It is
// deliberately not authority-shaped.
type LifecycleSnapshot struct {
Volumes []lifecycle.VolumeRecord
Nodes []lifecycle.NodeRegistration
Placements []lifecycle.PlacementIntent
}
// LifecycleSnapshot returns copies of lifecycle facts when configured.
func (h *Host) LifecycleSnapshot() (LifecycleSnapshot, bool) {
stores := h.Lifecycle()
if stores == nil {
return LifecycleSnapshot{}, false
}
return LifecycleSnapshot{
Volumes: stores.Volumes.ListVolumes(),
Nodes: stores.Nodes.ListNodes(),
Placements: stores.Placements.ListPlacements(),
}, true
}
@@ -2,6 +2,10 @@ package master
import (
"context"
"go/ast"
"go/parser"
"go/token"
"path/filepath"
"testing"
"time"
@@ -64,6 +68,23 @@ func TestMasterLifecycleStore_OpensRegistrationStoresWithoutAuthority(t *testing
if _, ok := h.Publisher().VolumeAuthorityLine("vol-a"); ok {
t.Fatal("lifecycle registration must not mint authority")
}
snapshot, ok := h.LifecycleSnapshot()
if !ok {
t.Fatal("missing lifecycle snapshot")
}
if len(snapshot.Volumes) != 1 || len(snapshot.Nodes) != 2 || len(snapshot.Placements) != 1 {
t.Fatalf("snapshot counts got volumes=%d nodes=%d placements=%d",
len(snapshot.Volumes), len(snapshot.Nodes), len(snapshot.Placements))
}
}
func TestMasterLifecycleSnapshot_IsNotAuthorityShaped(t *testing.T) {
typ := mustParseStruct(t, "lifecycle_snapshot.go", "LifecycleSnapshot")
for _, forbidden := range []string{"Epoch", "EndpointVersion", "Assignment", "Ready", "Healthy", "Primary"} {
if _, ok := typ.Fields[forbidden]; ok {
t.Fatalf("LifecycleSnapshot must not carry %s", forbidden)
}
}
}
func newTestMaster(t *testing.T, lifecycleDir string) *Host {
@@ -80,6 +101,41 @@ func newTestMaster(t *testing.T, lifecycleDir string) *Host {
return h
}
type parsedStruct struct {
Fields map[string]struct{}
}
func mustParseStruct(t *testing.T, fileName, structName string) parsedStruct {
t.Helper()
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, filepath.Clean(fileName), nil, 0)
if err != nil {
t.Fatalf("parse %s: %v", fileName, err)
}
var out parsedStruct
ast.Inspect(file, func(n ast.Node) bool {
typeSpec, ok := n.(*ast.TypeSpec)
if !ok || typeSpec.Name.Name != structName {
return true
}
st, ok := typeSpec.Type.(*ast.StructType)
if !ok {
t.Fatalf("%s is not a struct", structName)
}
out.Fields = make(map[string]struct{})
for _, field := range st.Fields.List {
for _, name := range field.Names {
out.Fields[name.Name] = struct{}{}
}
}
return false
})
if out.Fields == nil {
t.Fatalf("struct %s not found in %s", structName, fileName)
}
return out
}
func closeTestMaster(t *testing.T, h *Host) {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
@@ -2,6 +2,7 @@ package lifecycle
import (
"errors"
"reflect"
"testing"
)
@@ -112,6 +113,98 @@ func TestReconcilePlacement_ResultIsNotAuthorityShaped(t *testing.T) {
}
}
func TestReconcilePlacement_IdempotentSameInputsProduceSameIntent(t *testing.T) {
volumes := []VolumeRecord{{Spec: VolumeSpec{
VolumeID: "vol-a",
SizeBytes: 1 << 20,
ReplicationFactor: 2,
}}}
nodes := []NodeRegistration{
nodeWithPool("node-a", "pool-a", 1<<30),
nodeWithPool("node-b", "pool-b", 1<<30),
}
store, err := OpenPlacementIntentStore(t.TempDir())
if err != nil {
t.Fatalf("open: %v", err)
}
first := ReconcilePlacement(volumes, nodes, store)
second := ReconcilePlacement(volumes, nodes, store)
if len(first) != 1 || len(second) != 1 || !first[0].Applied || !second[0].Applied {
t.Fatalf("unexpected results first=%+v second=%+v", first, second)
}
if !reflect.DeepEqual(first[0].Intent, second[0].Intent) {
t.Fatalf("same inputs changed intent:\nfirst %+v\nsecond %+v", first[0].Intent, second[0].Intent)
}
got, ok := store.GetPlacement("vol-a")
if !ok {
t.Fatal("missing persisted intent")
}
if !reflect.DeepEqual(got, first[0].Intent) {
t.Fatalf("persisted intent drifted:\ngot %+v\nwant %+v", got, first[0].Intent)
}
}
func TestReconcilePlacement_DesiredRFChangeUpdatesExistingIntent(t *testing.T) {
store, err := OpenPlacementIntentStore(t.TempDir())
if err != nil {
t.Fatalf("open: %v", err)
}
nodes := []NodeRegistration{
nodeWithPool("node-a", "pool-a", 1<<30),
nodeWithPool("node-b", "pool-b", 1<<30),
nodeWithPool("node-c", "pool-c", 1<<30),
}
first := ReconcilePlacement([]VolumeRecord{{Spec: VolumeSpec{
VolumeID: "vol-a",
SizeBytes: 1 << 20,
ReplicationFactor: 2,
}}}, nodes, store)
if len(first) != 1 || !first[0].Applied {
t.Fatalf("first reconcile failed: %+v", first)
}
second := ReconcilePlacement([]VolumeRecord{{Spec: VolumeSpec{
VolumeID: "vol-a",
SizeBytes: 1 << 20,
ReplicationFactor: 3,
}}}, nodes, store)
if len(second) != 1 || !second[0].Applied {
t.Fatalf("second reconcile failed: %+v", second)
}
got, ok := store.GetPlacement("vol-a")
if !ok {
t.Fatal("missing placement")
}
if got.DesiredRF != 3 || len(got.Slots) != 3 {
t.Fatalf("RF change did not update intent: %+v", got)
}
}
func TestReconcilePlacement_ExistingIntentSameInputsOverwritesSameValue(t *testing.T) {
store, err := OpenPlacementIntentStore(t.TempDir())
if err != nil {
t.Fatalf("open: %v", err)
}
volumes := []VolumeRecord{{Spec: VolumeSpec{
VolumeID: "vol-a",
SizeBytes: 1 << 20,
ReplicationFactor: 1,
}}}
nodes := []NodeRegistration{nodeWithPool("node-a", "pool-a", 1<<30)}
first := ReconcilePlacement(volumes, nodes, store)
if len(first) != 1 || !first[0].Applied {
t.Fatalf("first reconcile failed: %+v", first)
}
before, _ := store.GetPlacement("vol-a")
again := ReconcilePlacement(volumes, nodes, store)
if len(again) != 1 || !again[0].Applied {
t.Fatalf("second reconcile failed: %+v", again)
}
after, _ := store.GetPlacement("vol-a")
if !reflect.DeepEqual(before, after) {
t.Fatalf("existing intent changed under same inputs:\nbefore %+v\nafter %+v", before, after)
}
}
func nodeWithPool(serverID, poolID string, freeBytes uint64) NodeRegistration {
return NodeRegistration{
ServerID: serverID,