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 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-16 21:19:50 -08:00
1 parent 76c41c6c56
commit dc8449aef0
2 files changed
+76

No files matched your search

@@ -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())
}
}
+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)",