Skip to content

Commit 78e0e23

Browse files
committed
Initialize Commit Index = max(consistent_index, CommitIndex) on bootstrap if --force-new-cluster set
Signed-off-by: Benjamin Wang <[email protected]>
1 parent 9d53b14 commit 78e0e23

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

server/etcdserver/bootstrap.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ func bootstrap(cfg config.ServerConfig) (b *bootstrappedServer, err error) {
8787
if err = fileutil.IsDirWriteable(cfg.WALDir()); err != nil {
8888
return nil, fmt.Errorf("cannot write to WAL directory: %w", err)
8989
}
90-
bwal = bootstrapWALFromSnapshot(cfg, backend.snapshot)
90+
cfg.Logger.Info("Bootstrapping WAL from snapshot")
91+
bwal = bootstrapWALFromSnapshot(cfg, backend.snapshot, backend.ci)
9192
}
9293

9394
cfg.Logger.Info("bootstrapping cluster")
@@ -556,7 +557,7 @@ func (b *bootstrappedRaft) newRaftNode(ss *snap.Snapshotter, wal *wal.WAL, cl *m
556557
)
557558
}
558559

559-
func bootstrapWALFromSnapshot(cfg config.ServerConfig, snapshot *raftpb.Snapshot) *bootstrappedWAL {
560+
func bootstrapWALFromSnapshot(cfg config.ServerConfig, snapshot *raftpb.Snapshot, ci cindex.ConsistentIndexer) *bootstrappedWAL {
560561
wal, st, ents, snap, meta := openWALFromSnapshot(cfg, snapshot)
561562
bwal := &bootstrappedWAL{
562563
lg: cfg.Logger,
@@ -569,6 +570,19 @@ func bootstrapWALFromSnapshot(cfg config.ServerConfig, snapshot *raftpb.Snapshot
569570
}
570571

571572
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+
572586
// discard the previously uncommitted entries
573587
bwal.ents = bwal.CommitedEntries()
574588
entries := bwal.NewConfigChangeEntries()
@@ -578,6 +592,7 @@ func bootstrapWALFromSnapshot(cfg config.ServerConfig, snapshot *raftpb.Snapshot
578592
"forcing restart member",
579593
zap.String("cluster-id", meta.clusterID.String()),
580594
zap.String("local-member-id", meta.nodeID.String()),
595+
zap.Uint64("wal-commit-index", oldCommitIndex),
581596
zap.Uint64("commit-index", bwal.st.Commit),
582597
)
583598
} else {

0 commit comments

Comments
 (0)