Fix ConfState divergence on crash during Raft ConfChange recovery #21190
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix Raft ConfState Divergence After Crash During ConfChange Application
Summary
This PR fixes a critical crash-recovery bug in etcd where Raft’s in-memory
ConfStatecan diverge from the backend-persistedConfStateif the process crashes while applying a membership change.The issue occurs because
ConfStatepersistence is not atomic withApplyConfChange(). On restart, etcd trusted the backendConfState, which may be stale, leading to incorrect cluster membership after recovery.This PR makes the WAL the single source of truth for rebuilding
ConfStateduring bootstrap, eliminating this inconsistency.Problem Description
When applying a
ConfChange, etcd performs the following steps:ConfStateviaApplyConfChange()ConfStateas dirty usingSetConfState()If etcd crashes after step (1) but before the backend transaction commits, the system enters an inconsistent state:
ConfStateremains staleConfStateThis violates the invariant that Raft state and persisted cluster membership metadata must be consistent after recovery.
Why This Is Critical
Root Cause
ConfStateis treated as authoritative during bootstrapConfChangeentriesApplyConfChange()and backend commit leaves persisted state staleFix Overview
Rebuild
ConfStatefrom WAL during bootstrap and reconcile backend state if necessary.Key changes
ConfStateby replaying committedConfChangeentries from WALConfStatewith the backendConfStateConfStateto the backend before starting RaftThis guarantees crash-safe and deterministic recovery without changing Raft semantics.
Steps to Reproduce (Before Fix)
ConfChangeRemoveNodeApplyConfChange()but before backend commitVerification (After Fix)
ConfStateis rebuilt from WAL on startupConfStateis corrected automaticallyTests Added
ConfStatereconstructionConfChangeusing gofailConfStatematch after recoveryImpact
Notes for Reviewers