From b3f4bbcd805dc62f0f1f440d53258cd0cf56cd70 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sat, 2 May 2026 14:53:15 -0700 Subject: [PATCH] test(g8): pin failover authority fence and data continuity prelude --- .../authority/g8_0_authority_failover_test.go | 111 ++++++++++++++++++ .../core/host/volume/g8a_stale_fence_test.go | 89 ++++++++++++++ .../g8b_data_continuity_component_test.go | 107 +++++++++++++++++ 3 files changed, 307 insertions(+) create mode 100644 seaweed-block/core/authority/g8_0_authority_failover_test.go create mode 100644 seaweed-block/core/host/volume/g8a_stale_fence_test.go create mode 100644 seaweed-block/core/replication/component/g8b_data_continuity_component_test.go diff --git a/seaweed-block/core/authority/g8_0_authority_failover_test.go b/seaweed-block/core/authority/g8_0_authority_failover_test.go new file mode 100644 index 0000000000..421cd8e66c --- /dev/null +++ b/seaweed-block/core/authority/g8_0_authority_failover_test.go @@ -0,0 +1,111 @@ +package authority + +import "testing" + +// G8-0 pins the authority-only failover contract. These tests do +// not touch engine/proto/frontend: the controller may only emit a +// publisher ask, and the publisher remains the sole epoch minter. + +func TestG8_0_TopologyController_FailoverRequiresReadyEligibleReachableCandidate(t *testing.T) { + reader := newFakeBasisReader() + reader.Set("v1", "r1", AuthorityBasis{ + Assigned: true, ReplicaID: "r1", Epoch: 7, EndpointVersion: 1, + DataAddr: "d1", CtrlAddr: "c1", + }) + ctrl := NewTopologyController(TopologyControllerConfig{}, reader) + + r1 := candidate("r1", "s1", "d1", "c1", 10) + r1.Reachable = false + r2 := candidate("r2", "s2", "d2", "c2", 100) + r2.ReadyForPrimary = false // high evidence is not enough. + r3 := candidate("r3", "s3", "d3", "c3", 1) + + snap := ClusterSnapshot{ + CollectedRevision: 11, + Servers: []ServerObservation{ + {ServerID: "s1", Reachable: true, Eligible: true}, + {ServerID: "s2", Reachable: true, Eligible: true}, + {ServerID: "s3", Reachable: true, Eligible: true}, + }, + Volumes: []VolumeTopologySnapshot{ + clusterVolume("v1", + AuthorityBasis{Assigned: true, ReplicaID: "r1", Epoch: 7, EndpointVersion: 1, DataAddr: "d1", CtrlAddr: "c1"}, + r1, r2, r3, + ), + }, + } + + if err := ctrl.SubmitClusterSnapshot(snap); err != nil { + t.Fatalf("SubmitClusterSnapshot: %v", err) + } + ask := nextAskOrFail(t, ctrl) + if ask.Intent != IntentReassign || ask.ReplicaID != "r3" { + t.Fatalf("failover must choose the only ready+eligible+reachable candidate r3, got %+v", ask) + } +} + +func TestG8_0_TopologyController_NoEligibleFailoverCandidate_DoesNotMint(t *testing.T) { + reader := newFakeBasisReader() + reader.Set("v1", "r1", AuthorityBasis{ + Assigned: true, ReplicaID: "r1", Epoch: 3, EndpointVersion: 1, + DataAddr: "d1", CtrlAddr: "c1", + }) + ctrl := NewTopologyController(TopologyControllerConfig{}, reader) + + r1 := candidate("r1", "s1", "d1", "c1", 10) + r1.Reachable = false + r2 := candidate("r2", "s2", "d2", "c2", 20) + r2.ReadyForPrimary = false + r3 := candidate("r3", "s3", "d3", "c3", 30) + r3.Eligible = false + + snap := ClusterSnapshot{ + CollectedRevision: 12, + Servers: []ServerObservation{ + {ServerID: "s1", Reachable: true, Eligible: true}, + {ServerID: "s2", Reachable: true, Eligible: true}, + {ServerID: "s3", Reachable: true, Eligible: true}, + }, + Volumes: []VolumeTopologySnapshot{ + clusterVolume("v1", + AuthorityBasis{Assigned: true, ReplicaID: "r1", Epoch: 3, EndpointVersion: 1, DataAddr: "d1", CtrlAddr: "c1"}, + r1, r2, r3, + ), + }, + } + + if err := ctrl.SubmitClusterSnapshot(snap); err != nil { + t.Fatalf("SubmitClusterSnapshot: %v", err) + } + expectNoNext(t, ctrl) + if desired, ok := ctrl.DesiredFor("v1"); ok { + t.Fatalf("no eligible failover candidate must not leave a desired mint, got %+v", desired) + } +} + +func TestG8_0_Publisher_ReassignAdvancesPerVolumeEpochAcrossReplicaSlots(t *testing.T) { + pub := NewPublisher(NewStaticDirective(nil)) + if err := pub.apply(AssignmentAsk{ + VolumeID: "v1", ReplicaID: "r1", DataAddr: "d1", CtrlAddr: "c1", Intent: IntentBind, + }); err != nil { + t.Fatalf("bind r1: %v", err) + } + if err := pub.apply(AssignmentAsk{ + VolumeID: "v1", ReplicaID: "r2", DataAddr: "d2", CtrlAddr: "c2", Intent: IntentReassign, + }); err != nil { + t.Fatalf("reassign r2: %v", err) + } + + line, ok := pub.VolumeAuthorityLine("v1") + if !ok { + t.Fatal("missing volume authority line after reassign") + } + if line.ReplicaID != "r2" || line.Epoch != 2 || line.EndpointVersion != 1 { + t.Fatalf("reassign must mint a new per-volume epoch on r2, got %+v", line) + } + + old, ok := pub.LastPublished("v1", "r1") + if !ok || old.Epoch != 1 { + t.Fatalf("old slot record should remain historical r1@1 for subscription catch-up, got %+v ok=%v", old, ok) + } +} diff --git a/seaweed-block/core/host/volume/g8a_stale_fence_test.go b/seaweed-block/core/host/volume/g8a_stale_fence_test.go new file mode 100644 index 0000000000..514bf2d189 --- /dev/null +++ b/seaweed-block/core/host/volume/g8a_stale_fence_test.go @@ -0,0 +1,89 @@ +package volume + +import ( + "context" + "errors" + "testing" + + "github.com/seaweedfs/seaweed-block/core/engine" + "github.com/seaweedfs/seaweed-block/core/frontend" + "github.com/seaweedfs/seaweed-block/core/frontend/durable" + control "github.com/seaweedfs/seaweed-block/core/rpc/control" + "github.com/seaweedfs/seaweed-block/core/storage/memorywal" +) + +// G8-A pins the host/frontend composition for old-primary fencing: +// when the volume-scoped assignment stream says another replica owns +// a newer authority line, the old primary's locally-healthy engine +// projection must fail closed at the durable backend gate. +func TestG8A_SupersedeFact_FencesOldPrimaryDurableBackend(t *testing.T) { + proj := &mutableProjector{p: engine.ReplicaProjection{ + Mode: engine.ModeHealthy, Epoch: 1, EndpointVersion: 1, + }} + h := &Host{cfg: Config{VolumeID: "v1", ReplicaID: "r1"}} + h.view = NewAdapterProjectionView(proj, "v1", "r1", h) + + id := frontend.Identity{VolumeID: "v1", ReplicaID: "r1", Epoch: 1, EndpointVersion: 1} + store := memorywal.NewStore(8, 4096) + t.Cleanup(func() { _ = store.Close() }) + backend := durable.NewStorageBackend(store, h.view, id) + backend.SetOperational(true, "g8-b old-primary baseline") + + ctx := context.Background() + payload := []byte("before-supersede") + if n, err := backend.Write(ctx, 0, payload); err != nil || n != len(payload) { + t.Fatalf("baseline write: n=%d err=%v", n, err) + } + + h.recordOtherLine(&control.AssignmentFact{ + VolumeId: "v1", + ReplicaId: "r2", + Epoch: 2, + EndpointVersion: 1, + DataAddr: "r2-data", + CtrlAddr: "r2-ctrl", + }) + + if p := h.view.Projection(); p.Healthy { + t.Fatalf("old primary projection must fail closed after supersede, got %+v", p) + } + if _, err := backend.Read(ctx, 0, make([]byte, len(payload))); !errors.Is(err, frontend.ErrStalePrimary) { + t.Fatalf("superseded old-primary read: got %v want ErrStalePrimary", err) + } + if _, err := backend.Write(ctx, 0, []byte("x")); !errors.Is(err, frontend.ErrStalePrimary) { + t.Fatalf("superseded old-primary write: got %v want ErrStalePrimary", err) + } + if err := backend.Sync(ctx); !errors.Is(err, frontend.ErrStalePrimary) { + t.Fatalf("superseded old-primary sync: got %v want ErrStalePrimary", err) + } +} + +func TestG8A_StaleSupersedeFact_DoesNotFenceCurrentPrimaryBackend(t *testing.T) { + proj := &mutableProjector{p: engine.ReplicaProjection{ + Mode: engine.ModeHealthy, Epoch: 5, EndpointVersion: 3, + }} + h := &Host{cfg: Config{VolumeID: "v1", ReplicaID: "r1"}} + h.view = NewAdapterProjectionView(proj, "v1", "r1", h) + + id := frontend.Identity{VolumeID: "v1", ReplicaID: "r1", Epoch: 5, EndpointVersion: 3} + store := memorywal.NewStore(8, 4096) + t.Cleanup(func() { _ = store.Close() }) + backend := durable.NewStorageBackend(store, h.view, id) + backend.SetOperational(true, "g8-b current-primary baseline") + + h.recordOtherLine(&control.AssignmentFact{ + VolumeId: "v1", + ReplicaId: "r2", + Epoch: 4, + EndpointVersion: 99, + DataAddr: "old-data", + CtrlAddr: "old-ctrl", + }) + + if p := h.view.Projection(); !p.Healthy { + t.Fatalf("older cross-replica fact must not fence current primary, got %+v", p) + } + if n, err := backend.Write(context.Background(), 0, []byte("still-current")); err != nil || n != len("still-current") { + t.Fatalf("current primary write after stale supersede: n=%d err=%v", n, err) + } +} diff --git a/seaweed-block/core/replication/component/g8b_data_continuity_component_test.go b/seaweed-block/core/replication/component/g8b_data_continuity_component_test.go new file mode 100644 index 0000000000..ded2bc6d0e --- /dev/null +++ b/seaweed-block/core/replication/component/g8b_data_continuity_component_test.go @@ -0,0 +1,107 @@ +package component_test + +import ( + "bytes" + "testing" + "time" + + "github.com/seaweedfs/seaweed-block/core/authority" + "github.com/seaweedfs/seaweed-block/core/replication/component" +) + +// G8-B component prelude pins the data-continuity prerequisite for failover: after +// acknowledged primary writes have been replicated to a candidate, +// the candidate's local durable bytes are sufficient for serving the +// same data once authority moves to it. Authority movement itself is +// covered in core/authority G8-A; stale old-primary fencing is covered +// in core/host/volume G8-B. +func TestG8B0_NewPrimaryCandidate_ReadsAcknowledgedWritesAfterConvergence(t *testing.T) { + component.RunMatrix(t, func(t *testing.T, c *component.Cluster) { + c.WithReplicas(1). + WithLiveShip(). + WithApplyGate(). + Start() + + const writes = 24 + for i := 0; i < writes; i++ { + data := make([]byte, component.DefaultBlockSize) + data[0] = byte(0x80 + i) + data[len(data)-1] = byte(0x40 ^ i) + c.PrimaryWriteViaBackend(uint32(i), data) + } + c.PrimarySync() + c.WaitForConverge(3 * time.Second) + + for lba := uint32(0); lba < writes; lba++ { + primaryData, err := c.Primary().Store.Read(lba) + if err != nil { + t.Fatalf("primary read lba=%d: %v", lba, err) + } + newPrimaryData, err := c.Replica(0).Store.Read(lba) + if err != nil { + t.Fatalf("candidate read lba=%d: %v", lba, err) + } + if !bytes.Equal(primaryData, newPrimaryData) { + t.Fatalf("candidate data mismatch at lba=%d: primary[0]=%02x candidate[0]=%02x", + lba, primaryData[0], newPrimaryData[0]) + } + } + }) +} + +func TestG8B0_FailoverCandidate_NotAcceptedBeforeByteConvergence(t *testing.T) { + component.RunMatrix(t, func(t *testing.T, c *component.Cluster) { + c.WithReplicas(1).Start() + c.PrimaryWriteN(8) + c.PrimarySync() + + if candidateMatchesPrimary(t, c, 0, 8) { + t.Fatal("empty candidate unexpectedly matches primary before recovery/convergence") + } + + result := c.CatchUpReplica(0) + if !result.Success { + t.Fatalf("catch-up: %s", result.FailReason) + } + if !candidateMatchesPrimary(t, c, 0, 8) { + t.Fatal("candidate must match primary after successful recovery/convergence") + } + }) +} + +func TestG8_FailoverCannotCloseOnAuthorityMoveOnly(t *testing.T) { + component.RunSubstrate(t, "memorywal", component.MemoryWAL, func(t *testing.T, c *component.Cluster) { + c.WithReplicas(1).Start() + c.PrimaryWriteN(8) + c.PrimarySync() + + line := authority.AuthorityBasis{ + Assigned: true, ReplicaID: "r2", Epoch: 2, EndpointVersion: 1, + DataAddr: "r2-data", CtrlAddr: "r2-ctrl", + } + if !line.Assigned || line.ReplicaID != "r2" { + t.Fatalf("authority move setup failed: line=%+v", line) + } + if candidateMatchesPrimary(t, c, 0, 8) { + t.Fatal("authority move alone must not imply G8 data-continuity close") + } + }) +} + +func candidateMatchesPrimary(t *testing.T, c *component.Cluster, replicaIdx int, n uint32) bool { + t.Helper() + for lba := uint32(0); lba < n; lba++ { + primaryData, err := c.Primary().Store.Read(lba) + if err != nil { + t.Fatalf("primary read lba=%d: %v", lba, err) + } + replicaData, err := c.Replica(replicaIdx).Store.Read(lba) + if err != nil { + return false + } + if !bytes.Equal(primaryData, replicaData) { + return false + } + } + return true +}