From dc8449aef0da3e80903143b9a228307d86ba0955 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Mon, 16 Nov 2026 21:19:50 -0800 Subject: [PATCH] fix(replication): Invalidate obeys the peer state transition gate Invalidate stored Degraded directly, so a NeedsRebuild peer could be invalidated to Degraded and then set Healthy, escaping the terminal state. Invalidate now checks replicaStateTransitionAllowed like SetState: from NeedsRebuild the state is kept and the reason is still logged; every other state still moves to Degraded. Fixes #182. Co-Authored-By: Claude Opus 5.5 --- .../replication/invalidate_transition_test.go | 62 +++++++++++++++++++ seaweed-block/core/replication/peer.go | 14 +++++ 2 files changed, 76 insertions(+) create mode 100644 seaweed-block/core/replication/invalidate_transition_test.go diff --git a/seaweed-block/core/replication/invalidate_transition_test.go b/seaweed-block/core/replication/invalidate_transition_test.go new file mode 100644 index 0000000000..56d6b4802e --- /dev/null +++ b/seaweed-block/core/replication/invalidate_transition_test.go @@ -0,0 +1,62 @@ +package replication + +import ( + "bytes" + "log" + "strings" + "testing" +) + +// #182: Invalidate still moves every non-terminal state to Degraded. +func TestReplicaPeer_InvalidateNonTerminalStatesBecomeDegraded(t *testing.T) { + for _, tc := range []struct { + name string + setup []ReplicaState + want ReplicaState // state before Invalidate + }{ + {"unknown", nil, ReplicaUnknown}, + {"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) + if tc.setup == nil && peer.State() != ReplicaUnknown { + t.Skipf("new peer starts in %s, not Unknown", peer.State()) + } + 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()) + } +} diff --git a/seaweed-block/core/replication/peer.go b/seaweed-block/core/replication/peer.go index ffe01ab003..6280721502 100644 --- a/seaweed-block/core/replication/peer.go +++ b/seaweed-block/core/replication/peer.go @@ -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)",