Browse Source

Merge pull request #989 from coreos/fix_ignore_term

raft: fix ignore term
Xiang Li 11 years ago
parent
commit
5ddfe18cda
2 changed files with 17 additions and 0 deletions
  1. 1 0
      raft/raft.go
  2. 16 0
      raft/raft_test.go

+ 1 - 0
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

+ 16 - 0
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),