From eea052d9754ae86dd7ebabc9ea538216435bb6ab Mon Sep 17 00:00:00 2001 From: pingqiu Date: Wed, 25 Nov 2026 18:16:22 -0800 Subject: [PATCH] test: a replica mid-recovery is never counted While a recovery owns a replica's store its epoch table is invalid and its end may sit above a gap. Such a replica is never a fencing round's target, also with the highest end, and never completes a sync_all or sync_quorum write. Co-Authored-By: Claude Opus 5.5 --- .../core/host/master/fence_gate_test.go | 3 + .../component/mid_recovery_count_test.go | 93 +++++++++++++++++++ seaweed-block/docs/design/invariants.md | 2 +- 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 seaweed-block/core/replication/component/mid_recovery_count_test.go diff --git a/seaweed-block/core/host/master/fence_gate_test.go b/seaweed-block/core/host/master/fence_gate_test.go index 7765e5b63c..ff1f0c0af2 100644 --- a/seaweed-block/core/host/master/fence_gate_test.go +++ b/seaweed-block/core/host/master/fence_gate_test.go @@ -103,6 +103,9 @@ func TestPromotionGate_DecidesOnTheFencingRound(t *testing.T) { {"r3 on an invalid table: not counted", fencedAs(PromotionCandidateEvidence{ReplicaID: "r2", Ready: true, DurableLSN: 10, Floor: 2, TableValid: true}, PromotionCandidateEvidence{ReplicaID: "r3", DurableLSN: 8, Floor: 2, TableInvalid: true}), map[string]uint64{}}, + {"r3 mid-catch-up (invalid table) holds the highest fenced end, even ready: never the target", + fencedAs(PromotionCandidateEvidence{ReplicaID: "r2", Ready: true, DurableLSN: 10, Floor: 2, TableValid: true}, PromotionCandidateEvidence{ReplicaID: "r3", Ready: true, DurableLSN: 12, Floor: 2, TableInvalid: true}), + map[string]uint64{}}, {"r3 fenced but its evidence unreadable (table absent): not counted", fencedAs(PromotionCandidateEvidence{ReplicaID: "r2", Ready: true, DurableLSN: 10, Floor: 2, TableValid: true}, PromotionCandidateEvidence{ReplicaID: "r3", Floor: 2}), map[string]uint64{}}, diff --git a/seaweed-block/core/replication/component/mid_recovery_count_test.go b/seaweed-block/core/replication/component/mid_recovery_count_test.go new file mode 100644 index 0000000000..4b32d1f085 --- /dev/null +++ b/seaweed-block/core/replication/component/mid_recovery_count_test.go @@ -0,0 +1,93 @@ +package component + +import ( + "net" + "path/filepath" + "testing" + "time" + + "github.com/seaweedfs/seaweed-block/core/replication" + "github.com/seaweedfs/seaweed-block/core/transport" +) + +// withEpochTables gives each replica listener its epoch table, as the +// volume does before its role. +func withEpochTables(t *testing.T, c *Cluster) []*transport.EpochTable { + t.Helper() + tables := make([]*transport.EpochTable, c.ReplicaCount()) + for i := range tables { + tbl, err := transport.LoadEpochTable(filepath.Join(t.TempDir(), "v1.epoch-table"), "v1") + if err != nil { + t.Fatal(err) + } + c.Replica(i).Listener.SetEpochTable(tbl) + tables[i] = tbl + } + return tables +} + +// startRecoveryOn opens a catch-up lane on replica i from its end and +// leaves it open: the replica is mid-catch-up, its epoch table invalid. +func startRecoveryOn(t *testing.T, c *Cluster, i int, tbl *transport.EpochTable) { + t.Helper() + end, err := c.Replica(i).Store.Sync() + if err != nil { + t.Fatal(err) + } + conn, err := net.Dial("tcp", c.Replica(i).Addr) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = conn.Close() }) + lineage := transport.RecoveryLineage{SessionID: 1 << 40, Epoch: 1, EndpointVersion: 1, TargetLSN: end + 10} + if err := transport.WriteMsg(conn, transport.MsgRecoveryLaneStart, transport.EncodeRecoveryLaneStart(lineage, end)); err != nil { + t.Fatal(err) + } + deadline := time.Now().Add(5 * time.Second) + for !tbl.Invalid() { + if time.Now().After(deadline) { + t.Fatal("fixture: the recovery lane never took the replica's store") + } + time.Sleep(5 * time.Millisecond) + } +} + +// A replica mid-catch-up (its epoch table invalid until the recovery's +// final barrier) never completes a write: under sync_all (RF2) and under +// sync_quorum (RF3, the other replica down), the flush fails while it is +// the only replica that could count. Its reported end may be above a gap +// (a per-block catch-up ships no contiguous LSNs), so it must not count. +func TestReplicaMidCatchUpNeverCompletesAWrite(t *testing.T) { + for _, store := range []struct { + name string + factory SubstrateFactory + }{{"walstore", Walstore}, {"smartwal", Smartwal}} { + for _, mode := range []struct { + name string + replicas int + mode replication.DurabilityMode + }{{"sync_all RF2", 1, replication.DurabilitySyncAll}, {"sync_quorum RF3", 2, replication.DurabilitySyncQuorum}} { + t.Run(store.name+"/"+mode.name, func(t *testing.T) { + c := NewCluster(t, store.factory).WithReplicas(mode.replicas).WithApplyGate().WithLiveShip().Start() + tables := withEpochTables(t, c) + rv := c.Primary().RepVol + rv.SetDurabilityMode(replication.DurabilitySyncAll) + if err := attestSync(t, c, 1, 0x11); err != nil { + t.Fatalf("fixture: a flush before the recovery: %v", err) + } + rv.SetDurabilityMode(mode.mode) + if mode.replicas == 2 { + c.Replica(1).Listener.StopHard() // the only other replica is gone + } + // Control: replica-0 alone completes the flush before its recovery. + if err := attestSync(t, c, 2, 0x22); err != nil { + t.Fatalf("control: a flush with replica-0 healthy: %v", err) + } + startRecoveryOn(t, c, 0, tables[0]) + if err := attestSync(t, c, 3, 0x33); err == nil { + t.Fatal("a flush completed while the only replica that could count was mid-catch-up") + } + }) + } + } +} diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index 0e5023a82c..d0eeb20f3a 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -728,7 +728,7 @@ Notes: | `INV-REPL-NO-PER-LBA-DATA-REGRESSION` | On a replica, an entry writes data only if its LSN is above both P and its LBA's applied LSN, one value shared by every session and lane; a successful write sets that applied LSN to the entry's LSN. | M3 replica apply / session release | `core/replication/apply_gate.go` `applyRecovery`, `applyLive`; `cmd/blockvolume/replication_ingress.go` `replicationIngress` (one `NewReplicaApplyGate` per replica) | `TestReplicationIngressSharesApplyVersionsAcrossSessions` (`cmd/blockvolume/replication_ingress_apply_test.go`); `TestApplyGate_OverlappingSessionsNeverRegressBytes` (`core/replication/apply_gate_cross_session_test.go`); `TestApplyGate_AlreadyAdmittedWireSessionNeverRegressesBytes` (`core/replication/apply_gate_wire_overlap_test.go`); `TestApplyGate_PrunedDurableVersionsRemainStale` (`core/replication/apply_gate_floor_test.go`); `TestApplyGate_LiveAndRecovery_DistinctCoverageSharedVersion` (`core/replication/apply_gate_test.go`) | `ReplicaApplyOrder` NoRegression; `ReplicaPrefixFloor` NoRegression/OnlySuffix | ACTIVE | | `INV-REPL-NO-PER-LBA-STALE-RECOVERY` | A stale recovery-lane entry, LSN at or below max(applied LSN, P), writes no data and still marks its LBA covered for its own session. | M3 replica apply / session release | `core/replication/apply_gate.go` `applyRecovery` | `TestApplyGate_RecoveryLane_StaleSkip_DoesNotRegressData`, `TestApplyGate_RecoveryLane_StaleSkip_AdvancesRecoveryCovered` (`core/replication/apply_gate_test.go`) | — | ACTIVE | | `INV-REPL-NO-PER-LBA-STALE-LIVE` | A stale live-lane entry writes no data, returns an error, and marks no recovery coverage. | M3 replica apply / session release | `core/replication/apply_gate.go` `applyLive`; `core/transport/replica.go` `handleConn` (closes the connection, `INV-REPL-APPLY-FAIL-001`) | `TestApplyGate_LiveLane_StaleEntry_ReturnsError`, `TestApplyGate_LiveLane_StaleEntry_DoesNotAdvanceRecoveryCovered` (`core/replication/apply_gate_test.go`) | — | ACTIVE | -| `INV-REPL-LIVE-NEXT-LSN-001` | Inside an epoch, a live-lane entry above the store's next LSN (an LSN skipped) is not applied: the replica closes the connection and its barrier reports the end below the gap; entries replayed from that end apply once, in order. | M3 replica apply | `core/transport/replica.go` `handleConn` (with and without the apply gate; no live entry lands while a recovery owns the store) | `TestReplicaAppliesALiveEntryOnlyAtItsNextLSN`, `TestReplicaAppliesNoLiveEntryWhileARecoveryOwnsTheStore` (`core/replication/replica_next_lsn_test.go`; walstore and smartwal, the volume's listener construction) | — | ACTIVE | +| `INV-REPL-LIVE-NEXT-LSN-001` | Inside an epoch, a live-lane entry above the store's next LSN (an LSN skipped) is not applied: the replica closes the connection and its barrier reports the end below the gap; entries replayed from that end apply once, in order. | M3 replica apply | `core/transport/replica.go` `handleConn` (with and without the apply gate; no live entry lands while a recovery owns the store) | `TestReplicaAppliesALiveEntryOnlyAtItsNextLSN`, `TestReplicaAppliesNoLiveEntryWhileARecoveryOwnsTheStore` (`core/replication/replica_next_lsn_test.go`; walstore and smartwal, the volume's listener construction); guarded during a recovery (a per-block catch-up's end may sit above a gap): such a replica is never a fencing round's target and never completes a write, `TestPromotionGate_DecidesOnTheFencingRound` (row: invalid table, highest end), `TestReplicaMidCatchUpNeverCompletesAWrite` (`core/replication/component/mid_recovery_count_test.go`) | — | ACTIVE | | `INV-REPL-NO-PER-LBA-RETAIN` | Closing a session or merging a substrate seed below the recorded value never lowers an LBA's applied LSN. | M3 replica apply / session release | `core/replication/apply_gate.go` `CloseSession`, `initSessionLocked` | `TestApplyGate_CloseAndLowerSeedNeverEraseReplicaVersion` (`core/replication/apply_gate_cross_session_test.go`) | — | ACTIVE | | `INV-REPL-NO-PER-LBA-FAILED-WRITE` | A failed physical write leaves its LBA's applied LSN and its session's coverage unchanged. | M3 replica apply / session release | `core/replication/apply_gate.go` `applyRecovery`, `applyLive` | `TestApplyGate_FailedWriteDoesNotPublishReplicaVersion` (`core/replication/apply_gate_cross_session_test.go`) | — | ACTIVE | | `INV-REPL-NO-PER-LBA-CERTIFIED-PREFIX` | P advances only through an explicit recovery-prefix certificate admitted for the active, prefix-eligible lineage; synced R, live traffic and session changes never advance it. | M3 replica apply / session release | `core/replication/apply_gate.go` `CertifyRecoveryPrefix`; `core/transport/replica.go` `certifyRecoveryPrefix`, `markPrefixEligibility` | `TestApplyGate_CertifiedRecoveryClosesBoundVersionHistory`, `TestApplyGate_CertifiedPrefixFreezesAcrossSessions`, `TestApplyGate_SyncedLiveMaximumDoesNotCoverRecoveryHole` (`core/replication/apply_gate_floor_test.go`); `TestApplyGate_HealthyRecoveryPublishesWirePrefix`, `TestApplyGate_WirePrefixCannotIntroduceOrReviveSession` (`core/replication/apply_gate_prefix_wire_test.go`) | `ReplicaPrefixFloor` CertifiedCoverage | ACTIVE |