Repository navigation
fix logservice stale replica truncation - #24982
mergify[bot] merged 9 commits into
Conversation
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
f053f08 to
c3b3af8
Compare
XuPeng-SH
left a comment
There was a problem hiding this comment.
🎯 Multi-Angle Review Summary (3.0-dev Branch)
Comprehensive review completed across 6 dimensions: logic correctness, concurrency safety, error handling, test coverage, unhappy paths, and dependency upgrade.
✅ Overall Assessment: APPROVE
This is the 3.0-dev branch version of PR #24983 (main branch) with identical logic.
Strengths:
- 🔒 Concurrency bug FIXED: snapshotManager now thread-safe (sync.RWMutex)
- 🔄 Transactional cleanup: Metadata only removed if snapshot cleanup succeeds
- 📝 Excellent test coverage: 259 new test lines (1.7:1 test:code ratio)
- 🛡️ Conservative logic: Double-check pattern prevents false deletions
- 🧪 Fault injection testing:
TestTruncationKeepsStaleReplicaMetadataOnSnapshotCleanupFailure - 📦 Critical dependency upgrade: Dragonboat snapshot import metadata fix
🔴 Critical Issues FIXED
Issue #1: snapshotManager Data Race [FIXED]
Root cause: snapshotManager.snapshots map accessed by multiple goroutines without synchronization.
Concurrent paths:
truncationWorker→Count(),NewestIndex(),EvalImportSnapshot()startReplica→Add()processHAKeeperTruncation→Remove()
Impact: "concurrent map read/write" panic
Fix applied:
type snapshotManager struct {
mu sync.RWMutex // ✅ ADDED
cfg *Config
snapshots map[nodeID]*snapshotRecord
}Lock strategy:
- Read locks (RLock):
Count,NewestIndex,EvalImportSnapshot - Write locks (Lock):
Add,Remove,RemoveReplica,DropNewest,Init - Professional pattern:
Add/addLockedto avoid nested locks
✅ Verified correct — standard RWMutex usage, complete map coverage.
Issue #2: Stale Replica Metadata Causes Fake Epoch [FIXED]
Problem: Old replica metadata + exported snapshots linger after replica superseded:
- Stale membership reported to HAKeeper with fake large epoch
- Disk space waste
- Truncation processing stale metadata
Fix: New cleanupStaleReplica logic in processTruncateLog:
activeReplicas := l.startedReplicaIDs() // T1: Get active replicas
for _, shard := range l.getShards() {
replicas := activeReplicas[shard.ShardID]
if _, ok := replicas[shard.ReplicaID]; !ok { // Not active
if len(replicas) > 0 || l.isSkippedZombie(...) {
// Double-check before cleanup (TOCTOU protection)
replicas = l.startedReplicaIDs()
if _, ok := replicas[shard.ReplicaID]; ok {
continue // ✅ Now active, skip
}
l.cleanupStaleReplica(...) // ✅ Safe
}
}
}✅ Conservative logic — double-check pattern prevents false deletions.
🧪 Test Coverage Analysis
3 new test cases (259 lines):
| Test | Unhappy Path |
|---|---|
TestTruncationSkipsStaleReplicaMetadata |
No active replicas → skip cleanup; Skipped zombie → cleanup |
TestTruncationRealignsSnapshotIndexAfterStaleReplicaCleanup |
After stale cleanup, realign index to active replica's newest snapshot |
TestTruncationKeepsStaleReplicaMetadataOnSnapshotCleanupFailure |
FS failure → metadata preserved (transactional) ⭐ |
Fault injection test:
// Inject FS failure
failingSnapshotCfg.FS = vfs.Wrap(originalSnapshotCfg.FS, vfs.OnIndex(0, vfs.OpWrite))
s.store.snapshotMgr.cfg = &failingSnapshotCfg
assert.NoError(t, s.store.processTruncateLog(ctx))
assert.Equal(t, int64(staleReplica), s.store.getReplicaID(shardID)) // ✅ Still there
assert.Equal(t, 1, s.store.snapshotMgr.Count(shardID, staleReplica)) // ✅ Preserved
// Fix FS, retry
s.store.snapshotMgr.cfg = originalSnapshotCfg
assert.NoError(t, s.store.processTruncateLog(ctx))
assert.Equal(t, int64(-1), s.store.getReplicaID(shardID)) // ✅ Now removed✅ Outstanding — fault injection verifies transactional cleanup.
🔍 Unhappy Path Deep Dive
Edge case #1: FS failure during snapshot cleanup → TESTED ✅
- Metadata preserved, cleanup retried next iteration
- Test:
TestTruncationKeepsStaleReplicaMetadataOnSnapshotCleanupFailure
Edge case #2: Replica becomes active during cleanup → HANDLED ✅
- Double-check pattern catches this:
replicas = l.startedReplicaIDs()re-snapshot - If now active, skip cleanup
Edge case #3: Multiple stale replicas for same shard → HANDLED ✅
processedmap deduplicates: only process each shardID once
Edge case #4: Snapshot index realignment → TESTED ✅
- After stale cleanup, index reset to newest active replica snapshot
- Test:
TestTruncationRealignsSnapshotIndexAfterStaleReplicaCleanup
Edge case #5: Concurrent snapshotManager access → FIXED ✅
- All methods protected by RWMutex
📦 Dragonboat Dependency Upgrade
Upgrade: v4.0.0-20251214113216-2ddf81ef2a85 → v4.0.0-20260612094223-103d119addce
Upstream fix: matrixorigin/dragonboat@103d119addce
Issue fixed: Imported snapshots no longer synthesize Membership.ConfigChangeId from snapshot index
Impact: Prevents old imported membership from being treated as newer (fake epoch bug)
✅ Critical fix — required for correct HAKeeper operation.
📊 Code Quality
- Transactional cleanup: Snapshot cleanup failure → metadata preserved
- Conservative logic: Double-check prevents false deletions
- Fault injection: FS failure tested
- Complete lock coverage: All snapshotManager methods protected
- No breaking changes: Backward compatible
🎯 Verdict
APPROVE — This is the 3.0-dev backport of #24983 with identical logic.
Why APPROVED:
- Concurrency bug fixed (snapshotManager RWMutex)
- Comprehensive test coverage (259 lines, 1.7:1 ratio)
- Fault injection verifies transactional cleanup
- Conservative double-check logic prevents false deletions
- Critical Dragonboat dependency upgrade
- All unhappy paths tested
Comparison to #24983:
- Target branch:
3.0-dev(vsmain) - Code logic: Identical
- Test coverage: Identical
- Dragonboat version: Same
No blocking issues — ready to merge.
Reviewed dimensions: logic, concurrency, errors, tests, unhappy paths, dependency
Test coverage: 259 lines | Test:Code ratio: 1.7:1 | Fault injection: 1 test
Merge Queue Status
This pull request spent 15 seconds in the queue, including 2 seconds running CI. Required conditions to merge
|
What type of PR is this?
Which issue(s) this PR fixes:
issue #24966
What this PR does / why we need it:
This PR updates the MatrixOne
3.0-devbranch to use the Dragonboat snapshot import metadata fix merged in:Main changes:
github.com/lni/dragonboat/v4replacement togithub.com/matrixorigin/dragonboat/v4 v4.0.0-20260612094223-103d119addce.go.sumentries for the updated Dragonboat module.Why this is needed:
Membership.ConfigChangeIdfrom the snapshot index.ConfigChangeIdas the LogService shard epoch.Validation:
git diff --checkGOTELEMETRY=off go mod download github.com/lni/dragonboat/v4GOTELEMETRY=off go list -m -json github.com/lni/dragonboat/v4GOTELEMETRY=off GOCACHE=/private/tmp/mo-dragonboat-3dev-gocache go test github.com/lni/dragonboat/v4/tools github.com/lni/dragonboat/v4/internal/rsm github.com/lni/dragonboat/v4/internal/raft -run '^$' -count=0Fixes logservice stale replica recovery with the previous MO-side stale truncation metadata cleanup plus the Dragonboat snapshot import metadata fix from #24967. This keeps truncation from processing stale local replica metadata/exported snapshots, and updates Dragonboat so imported snapshots no longer synthesize membership ConfigChangeId from snapshot index.