Skip to content

Commit

Permalink
etcdserver: renaming db happens before snapshot persists to wal and s…
Browse files Browse the repository at this point in the history
…nap files

In the case that follower recieves a snapshot from leader
and crashes before renaming xxx.snap.db to db but after
snapshot has persisted to .wal and .snap, restarting
follower results loading old db, new .wal, and new .snap.
This will causes a index mismatch between snap metadata index
and consistent index from db.

This pr forces an ordering where saving/renaming db must
happen before snapshot is persisted to wal and snap file.
this ensures that db file can never be newer than  wal and snap file.
hence, it guarantees the invariant snapshot.Metadata.Index <= db.ConsistentIndex()
in NewServer() when checking validity of db and snap file.

FIXES etcd-io#7628
  • Loading branch information
fanminshi committed May 4, 2017
1 parent 505bf8c commit d31e1cb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
17 changes: 11 additions & 6 deletions etcdserver/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ type RaftTimer interface {
// to raft storage concurrently; the application must read
// raftDone before assuming the raft messages are stable.
type apply struct {
entries []raftpb.Entry
snapshot raftpb.Snapshot
raftDone <-chan struct{} // rx {} after raft has persisted messages
entries []raftpb.Entry
snapshot raftpb.Snapshot
raftDone <-chan struct{} // rx {} after raft has persisted messages
replaceDBDone chan struct{} // snapshot db from leader has replaced the current db
}

type raftNode struct {
Expand Down Expand Up @@ -191,10 +192,12 @@ func (r *raftNode) start(rh *raftReadyHandler) {
}

raftDone := make(chan struct{}, 1)
replaceDBDone := make(chan struct{}, 1)
ap := apply{
entries: rd.CommittedEntries,
snapshot: rd.Snapshot,
raftDone: raftDone,
entries: rd.CommittedEntries,
snapshot: rd.Snapshot,
raftDone: raftDone,
replaceDBDone: replaceDBDone,
}

updateCommittedIndex(&ap, rh)
Expand Down Expand Up @@ -223,6 +226,8 @@ func (r *raftNode) start(rh *raftReadyHandler) {
// gofail: var raftAfterSave struct{}

if !raft.IsEmptySnap(rd.Snapshot) {
// waits etcd server to finish renaming snap db to db.
<-replaceDBDone
// gofail: var raftBeforeSaveSnap struct{}
if err := r.storage.SaveSnap(rd.Snapshot); err != nil {
plog.Fatalf("raft save snapshot error: %v", err)
Expand Down
2 changes: 2 additions & 0 deletions etcdserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,8 @@ func (s *EtcdServer) applySnapshot(ep *etcdProgress, apply *apply) {
if err := os.Rename(snapfn, fn); err != nil {
plog.Panicf("rename snapshot file error: %v", err)
}
// notifies raftNode that db has been replaced.
apply.replaceDBDone <- struct{}{}

newbe := newBackend(fn, s.Cfg.QuotaBackendBytes)

Expand Down

0 comments on commit d31e1cb

Please sign in to comment.