@@ -87,7 +87,8 @@ func bootstrap(cfg config.ServerConfig) (b *bootstrappedServer, err error) {
87
87
if err = fileutil .IsDirWriteable (cfg .WALDir ()); err != nil {
88
88
return nil , fmt .Errorf ("cannot write to WAL directory: %w" , err )
89
89
}
90
- bwal = bootstrapWALFromSnapshot (cfg , backend .snapshot )
90
+ cfg .Logger .Info ("Bootstrapping WAL from snapshot" )
91
+ bwal = bootstrapWALFromSnapshot (cfg , backend .snapshot , backend .ci )
91
92
}
92
93
93
94
cfg .Logger .Info ("bootstrapping cluster" )
@@ -556,7 +557,7 @@ func (b *bootstrappedRaft) newRaftNode(ss *snap.Snapshotter, wal *wal.WAL, cl *m
556
557
)
557
558
}
558
559
559
- func bootstrapWALFromSnapshot (cfg config.ServerConfig , snapshot * raftpb.Snapshot ) * bootstrappedWAL {
560
+ func bootstrapWALFromSnapshot (cfg config.ServerConfig , snapshot * raftpb.Snapshot , ci cindex. ConsistentIndexer ) * bootstrappedWAL {
560
561
wal , st , ents , snap , meta := openWALFromSnapshot (cfg , snapshot )
561
562
bwal := & bootstrappedWAL {
562
563
lg : cfg .Logger ,
@@ -569,6 +570,19 @@ func bootstrapWALFromSnapshot(cfg config.ServerConfig, snapshot *raftpb.Snapshot
569
570
}
570
571
571
572
if cfg .ForceNewCluster {
573
+ consistentIndex := ci .ConsistentIndex ()
574
+ oldCommitIndex := bwal .st .Commit
575
+ // If only `HardState.Commit` increases, HardState won't be persisted
576
+ // to disk, even though the committed entries might have already been
577
+ // applied. This can result in consistent_index > CommitIndex.
578
+ //
579
+ // When restarting etcd with `--force-new-cluster`, all uncommitted
580
+ // entries are dropped. To avoid losing entries that were actually
581
+ // committed, we reset Commit to max(HardState.Commit, consistent_index).
582
+ //
583
+ // See: https://github.com/etcd-io/raft/pull/300 for more details.
584
+ bwal .st .Commit = max (oldCommitIndex , consistentIndex )
585
+
572
586
// discard the previously uncommitted entries
573
587
bwal .ents = bwal .CommitedEntries ()
574
588
entries := bwal .NewConfigChangeEntries ()
@@ -578,6 +592,7 @@ func bootstrapWALFromSnapshot(cfg config.ServerConfig, snapshot *raftpb.Snapshot
578
592
"forcing restart member" ,
579
593
zap .String ("cluster-id" , meta .clusterID .String ()),
580
594
zap .String ("local-member-id" , meta .nodeID .String ()),
595
+ zap .Uint64 ("wal-commit-index" , oldCommitIndex ),
581
596
zap .Uint64 ("commit-index" , bwal .st .Commit ),
582
597
)
583
598
} else {
0 commit comments