From 8dfa490e498ab0ca17dcceda21551f514fbf7733 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Thu, 4 Sep 2014 10:58:22 -0700 Subject: [PATCH] raft: fix ignore term --- raft/raft.go | 1 + raft/raft_test.go | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/raft/raft.go b/raft/raft.go index a7ce5fcef..4058458ea 100644 --- a/raft/raft.go +++ b/raft/raft.go @@ -348,6 +348,7 @@ func (r *raft) Step(m pb.Message) error { r.becomeFollower(m.Term, lead) case m.Term < r.Term: // ignore + return nil } r.step(r, m) return nil diff --git a/raft/raft_test.go b/raft/raft_test.go index 9c7cf4a83..bcd8e075b 100644 --- a/raft/raft_test.go +++ b/raft/raft_test.go @@ -458,6 +458,22 @@ func TestCommit(t *testing.T) { } } +// ensure that the Step function ignores the message from old term and does not pass it to the +// acutal stepX function. +func TestStepIgnoreOldTermMsg(t *testing.T) { + called := false + fakeStep := func(r *raft, m pb.Message) { + called = true + } + sm := newRaft(0, []int64{0}, 0, 0) + sm.step = fakeStep + sm.Term = 2 + sm.Step(pb.Message{Type: msgApp, Term: sm.Term - 1}) + if called == true { + t.Errorf("stepFunc called = %v , want %v", called, false) + } +} + // TestHandleMsgApp ensures: // 1. Reply false if log doesn’t contain an entry at prevLogIndex whose term matches prevLogTerm. // 2. If an existing entry conflicts with a new one (same index but different terms),