Merge pull request #193 from seaweedfs/fix/replica-invalidate-terminal-state

fix(replication): Invalidate preserves the NeedsRebuild terminal state (#182)
This commit is contained in:
Ping Qiu authored and GitHub committed 2026-11-16 22:15:18 -08:00
commit 28dde15335
4 files changed
+96 -4

No files matched your search

@@ -0,0 +1,18 @@
package replication
import "testing"
func TestReplicaPeer_InvalidatePreservesNeedsRebuildTerminalState(t *testing.T) {
peer, _, _ := setupPeerWithRealReplica(t)
peer.SetState(ReplicaDegraded)
peer.SetState(ReplicaNeedsRebuild)
if got := peer.State(); got != ReplicaNeedsRebuild {
t.Fatalf("setup state=%s", got)
}
peer.Invalidate("authority peer-down observation")
invalidated := peer.State()
peer.SetState(ReplicaHealthy)
if invalidated != ReplicaNeedsRebuild || peer.State() != ReplicaNeedsRebuild {
t.Fatalf("terminal state escaped: after Invalidate=%s after SetState(Healthy)=%s; want NeedsRebuild throughout", invalidated, peer.State())
}
}
@@ -0,0 +1,60 @@
package replication
import (
"bytes"
"log"
"strings"
"testing"
)
// #182: Invalidate still moves every non-terminal state to Degraded. A new
// peer starts Healthy; Unknown is only set by Close, after which Invalidate
// returns early, so it is not a reachable starting state here.
func TestReplicaPeer_InvalidateNonTerminalStatesBecomeDegraded(t *testing.T) {
for _, tc := range []struct {
name string
setup []ReplicaState
want ReplicaState // state before Invalidate
}{
{"healthy", []ReplicaState{ReplicaHealthy}, ReplicaHealthy},
{"degraded", []ReplicaState{ReplicaHealthy, ReplicaDegraded}, ReplicaDegraded},
{"catching_up", []ReplicaState{ReplicaHealthy, ReplicaCatchingUp}, ReplicaCatchingUp},
} {
t.Run(tc.name, func(t *testing.T) {
peer, _, _ := setupPeerWithRealReplica(t)
for _, s := range tc.setup {
peer.SetState(s)
}
if got := peer.State(); got != tc.want {
t.Fatalf("setup state=%s want %s", got, tc.want)
}
peer.Invalidate("test invalidation")
if got := peer.State(); got != ReplicaDegraded {
t.Fatalf("after Invalidate from %s: state=%s want degraded", tc.want, got)
}
})
}
}
// #182: on a NeedsRebuild peer, Invalidate keeps the state but still logs
// the reason, its only other effect.
func TestReplicaPeer_InvalidateOnNeedsRebuildStillLogsReason(t *testing.T) {
peer, _, _ := setupPeerWithRealReplica(t)
peer.SetState(ReplicaHealthy)
peer.SetState(ReplicaDegraded)
peer.SetState(ReplicaNeedsRebuild)
if got := peer.State(); got != ReplicaNeedsRebuild {
t.Fatalf("setup state=%s", got)
}
var buf bytes.Buffer
prev := log.Writer()
log.SetOutput(&buf)
peer.Invalidate("reason-182-marker")
log.SetOutput(prev)
if got := peer.State(); got != ReplicaNeedsRebuild {
t.Fatalf("state=%s want needs_rebuild kept", got)
}
if !strings.Contains(buf.String(), "reason-182-marker") {
t.Fatalf("invalidation reason not logged; log=%q", buf.String())
}
}
+14
View File
@@ -804,6 +804,9 @@ func replicaStateTransitionAllowed(prev, next ReplicaState) bool {
// transitions (CatchingUp, NeedsRebuild) flow through SetState.
// Calling Invalidate on an already-Degraded peer is a no-op (beyond
// the log line — still useful because the reason string may be new).
// Calling it on a NeedsRebuild peer keeps NeedsRebuild: Invalidate obeys
// replicaStateTransitionAllowed like SetState, and NeedsRebuild is
// terminal.
//
// Called by: ShipEntry on transport error; ReplicationVolume on
// explicit peer-down from authority updates.
@@ -816,6 +819,17 @@ func (p *ReplicaPeer) Invalidate(reason string) {
return
}
prev := p.state
// Invalidate goes through the same transition gate as SetState, so
// it can never move a peer out of a terminal state (NeedsRebuild).
// Every other state may move to Degraded, so their behaviour is
// unchanged. The log line is Invalidate's only other effect and is
// kept either way: the reason may be new.
if !replicaStateTransitionAllowed(prev, ReplicaDegraded) {
p.mu.Unlock()
log.Printf("replication: peer %s invalidated, state kept (prev=%s is terminal, reason=%s)",
p.target.ReplicaID, prev, reason)
return
}
p.state = ReplicaDegraded
p.mu.Unlock()
log.Printf("replication: peer %s invalidated (prev=%s, reason=%s)",
+4 -4
View File
@@ -130,9 +130,9 @@ stateDiagram-v2
| `Degraded` | `CatchingUp` or `NeedsRebuild` | the engine decides catch-up when the gap is in the primary's WAL, rebuild when it is not | `core/engine/apply.go` `decide` | `INV-REPL-CATCHUP-REBUILD-001` |
| `CatchingUp` | `Healthy`, `Degraded` or `NeedsRebuild` | done-acknowledgement; failure; WAL recycled or retries exhausted | `core/replication/peer.go` `ReplicaPeer.SetState` | `INV-REPL-PEER-TRANSITIONS-001` |
| any but `NeedsRebuild` | `Healthy` | recovered | `core/replication/peer.go` `replicaStateTransitionAllowed` | `INV-REPL-PEER-TRANSITIONS-001` |
| `NeedsRebuild` | — | no successor through `SetState`; the rebuild runs as a separate dual-lane session | `core/replication/peer.go` `replicaStateTransitionAllowed` | `INV-REPL-PEER-TRANSITIONS-001` |
| `NeedsRebuild` | — | no successor through `SetState` or `Invalidate`; the rebuild runs as a separate dual-lane session | `core/replication/peer.go` `replicaStateTransitionAllowed` | `INV-REPL-PEER-TRANSITIONS-001` |
`ReplicaPeer.Invalidate`, called on a ship, barrier or fence error, writes `Degraded` directly without consulting the table, so the "no successor" rule for `NeedsRebuild` holds for `SetState` only. Confirmed by a red test; open as [#182](https://github.com/seaweedfs/seaweed-block/issues/182). A replicated `Sync` is judged per durability mode (`core/replication/durability.go` `EvaluateBarrierAcks`; `INV-REPL-DURABILITY-MODE-001`).
`ReplicaPeer.Invalidate`, called on a ship, barrier or fence error, goes through the same gate as `SetState` (`replicaStateTransitionAllowed`): `Healthy`, `Degraded` and `CatchingUp` move to `Degraded`; from `NeedsRebuild` the state is kept and the reason is still logged ([#182](https://github.com/seaweedfs/seaweed-block/issues/182)). A replicated `Sync` is judged per durability mode (`core/replication/durability.go` `EvaluateBarrierAcks`; `INV-REPL-DURABILITY-MODE-001`).
### M4 Workspace HEAD holder and takeover
@@ -307,7 +307,7 @@ Notes:
| `INV-REPL-APPLY-FAIL-001` | A failed `ApplyEntry` on a replica closes the connection, so no barrier acknowledges past the gap. | M3 Healthy → Degraded | `core/transport/replica.go` `handleConn` | `TestReplicaApplyFailureStopsBarrierAtTheGap` (`core/transport/replica_apply_error_test.go`); `TestReplicaApplyFailure_PrimaryDegradesRecoversAndReplaysFailedLSN` (`core/replication/component/replica_apply_failure_recovery_test.go`) | — | ACTIVE |
| `INV-REPL-INGRESS-SLOTS-001` | Replication listeners are open only while the assignment has replica slots. | M3 role → listener lifecycle | `cmd/blockvolume/replication_ingress.go` `roleLocked` | `TestBinaryReplicationIngressFollowsAssignedSlots` (`cmd/blockvolume/replication_ingress_binary_test.go`) | — | ACTIVE |
| `INV-REPL-CATCHUP-REBUILD-001` | The engine chooses catch-up when the gap is still in the primary's WAL and rebuild when it is not. | M3 Degraded → CatchingUp \| NeedsRebuild | `core/engine/apply.go` `decide` | `TestV3_Decision_CatchUp_GapWithinWAL`, `TestV3_Decision_Rebuild_GapBeyondWAL` (`core/engine/apply_test.go`) | — | ACTIVE |
| `INV-REPL-PEER-TRANSITIONS-001` | `ReplicaPeer.SetState` applies only the transitions in the peer table; `NeedsRebuild` has no successor through it. | M3 peer state | `core/replication/peer.go` `ReplicaPeer.SetState`, `replicaStateTransitionAllowed` | `TestReplicaStateTransition_AllowedSet`, `TestReplicaStateTransition_RejectedSet`, `TestReplicaStateTransition_NeedsRebuildIsTerminalInT4c` (`core/replication/peer_state_machine_test.go`) | — | ACTIVE |
| `INV-REPL-PEER-TRANSITIONS-001` | `ReplicaPeer.SetState` and `ReplicaPeer.Invalidate` apply only the transitions in the peer table; `NeedsRebuild` has no successor through either. | M3 peer state | `core/replication/peer.go` `ReplicaPeer.SetState`, `ReplicaPeer.Invalidate`, `replicaStateTransitionAllowed` | `TestReplicaStateTransition_AllowedSet`, `TestReplicaStateTransition_RejectedSet`, `TestReplicaStateTransition_NeedsRebuildIsTerminalInT4c` (`core/replication/peer_state_machine_test.go`); `TestReplicaPeer_InvalidatePreservesNeedsRebuildTerminalState` (`core/replication/invalidate_terminal_regression_test.go`); `TestReplicaPeer_InvalidateNonTerminalStatesBecomeDegraded`, `TestReplicaPeer_InvalidateOnNeedsRebuildStillLogsReason` (`core/replication/invalidate_transition_test.go`) | — | ACTIVE |
| `INV-REPL-DURABILITY-MODE-001` | Under `sync_all` a replicated `Sync` fails if any replica barrier fails; under `sync_quorum` it fails unless a majority of nodes, the primary included, are durable. | M3 barrier evaluation | `core/replication/durability.go` `EvaluateBarrierAcks` | `TestDurabilityCoordinator_Quorum_RF3_OneReplicaFailed`, `TestDurabilityCoordinator_Quorum_RF3_BothReplicasFailed`, `TestDurabilityCoordinator_SyncAll_AnyFailure_Fails` (`core/replication/durability_test.go`) | — | ACTIVE |
| `INV-REPL-PROMOTION-READY-001` | A replica reports `promotion_ready` only if it is a member, reachable, caught up, fenced at the current epoch, and has delivered its post-rebuild durable acknowledgement. | M3 Supporting → promotion candidate | `core/engine/promotion_ready.go` `BuildPromotionReadyFact` | `TestBuildPromotionReadyFact_RefusesPendingFence`, `TestBuildPromotionReadyFact_RefusesPostCloseAckGap`, `TestBuildPromotionReadyFact_RefusesNotCaughtUp` (`core/engine/promotion_ready_test.go`) | `WALReplica` `I3`; `ReplicationFailover` `AckedSurvive` | ACTIVE |
| `INV-ROLE-STALE-REPLAY-001` | A role call below the host's current desired epoch is refused and cannot clear a newer role-admission failure. | M3 role admission | `core/host/volume/host.go` `fenceSelfWithPeers`, `latchRoleAdmission` | `TestG1_StaleReplayDoesNotClearANewerAdmissionFailure`, `TestG1_StaleSupportingReplayDoesNotClearTheLatch` (`core/host/volume/g1_admission_latch_test.go`) | — | ACTIVE |
@@ -317,7 +317,7 @@ Notes:
Notes:
- `INV-REPL-INGRESS-SLOTS-001`: Binary test: builds and runs the real `blockvolume`; build tag `subprocess`, not on Windows.
- `INV-REPL-PEER-TRANSITIONS-001`: `ReplicaPeer.Invalidate` writes `Degraded` directly and does not consult the table (see Part A, M3). Confirmed by a red test; open as [#182](https://github.com/seaweedfs/seaweed-block/issues/182).
- `INV-REPL-PEER-TRANSITIONS-001`: `ReplicaPeer.Invalidate` uses the same gate as `SetState`; from `NeedsRebuild` it keeps the state and still logs the reason (see Part A, M3; [#182](https://github.com/seaweedfs/seaweed-block/issues/182)).
### B.4 Workspace: HEAD holder, lease and takeover