From 233617bea2c25ca90903a91c547d84d8b3c6eff9 Mon Sep 17 00:00:00 2001 From: Soheil Hassas Yeganeh Date: Thu, 23 Oct 2014 14:56:17 -0400 Subject: [PATCH] raft: Make MsgAppRes ack only the last index in MsgApp As explained in #1366, the leader will fail to transmit the missed logs if the leader receives a hearbeat response from a follower that is not yet matched in the leader. In other words, there are append responses that do not explicitly reject an append but implied a gap. This commit is based on @xiangli-cmu's idea. We should only acknowledge upto the index of logs in the append message. This way responses to heartbeats would never interfer with the log synchronization because their log index is always 0. Fixes #1366 --- raft/raft.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/raft/raft.go b/raft/raft.go index 09f91b25f..99586c850 100644 --- a/raft/raft.go +++ b/raft/raft.go @@ -380,7 +380,11 @@ func (r *raft) Step(m pb.Message) error { func (r *raft) handleAppendEntries(m pb.Message) { if r.raftLog.maybeAppend(m.Index, m.LogTerm, m.Commit, m.Entries...) { - r.send(pb.Message{To: m.From, Type: pb.MsgAppResp, Index: r.raftLog.lastIndex()}) + mlastIndex := m.Index + if len(m.Entries) != 0 { + mlastIndex = m.Entries[len(m.Entries)-1].Index + } + r.send(pb.Message{To: m.From, Type: pb.MsgAppResp, Index: mlastIndex}) } else { r.send(pb.Message{To: m.From, Type: pb.MsgAppResp, Index: m.Index, Reject: true}) } @@ -428,6 +432,9 @@ func stepLeader(r *raft, m pb.Message) { r.appendEntry(e) r.bcastAppend() case pb.MsgAppResp: + if m.Index == 0 { + return + } if m.Reject { if r.prs[m.From].maybeDecrTo(m.Index) { r.sendAppend(m.From)