Skip to content

Commit

Permalink
raft: do not attach term to MsgReadIndex
Browse files Browse the repository at this point in the history
Fix #6744.

MsgReadIndex, as MsgProp, is to be forwarded to leader.
So we should treat it as local message.
  • Loading branch information
gyuho committed Oct 28, 2016
1 parent c6cd63d commit b869090
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
38 changes: 38 additions & 0 deletions raft/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,44 @@ func TestNodeReadIndex(t *testing.T) {
}
}

// TestNodeReadIndexToOldLeader ensures that raftpb.MsgReadIndex to old leader
// gets forwarded to the new leader and 'send' method does not attach its term.
func TestNodeReadIndexToOldLeader(t *testing.T) {
r1 := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage())
r2 := newTestRaft(2, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage())
r3 := newTestRaft(3, []uint64{1, 2, 3}, 10, 1, NewMemoryStorage())
nt := newNetwork(r1, r2, r3)

// elect r1 as leader
nt.send(raftpb.Message{From: 1, To: 1, Type: raftpb.MsgHup})

// elect r3 as leader, r1 is now follower
nt.send(raftpb.Message{From: 3, To: 3, Type: raftpb.MsgHup})

// to old leader r1
r2.Step(raftpb.Message{
From: 2,
To: 1,
Type: raftpb.MsgReadIndex,
Entries: []raftpb.Entry{{Data: []byte("testdata")}},
})

// term should not be attached
m := r2.msgs[0]
if m.Term != 0 {
t.Fatalf("m.Term expected 0, got %d", m.Term)
}

// old leader r1 forwards to new leader r3
m.To = 3
r1.Step(m)

m = r1.msgs[0]
if m.Term != 0 {
t.Fatalf("m.Term expected 0, got %d", m.Term)
}
}

// TestNodeProposeConfig ensures that node.ProposeConfChange sends the given configuration proposal
// to the underlying raft.
func TestNodeProposeConfig(t *testing.T) {
Expand Down
5 changes: 4 additions & 1 deletion raft/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,10 @@ func (r *raft) send(m pb.Message) {
// do not attach term to MsgProp
// proposals are a way to forward to the leader and
// should be treated as local message.
if m.Type != pb.MsgProp {
//
// do not attach term to MsgReadIndex
// for the same reason
if m.Type != pb.MsgProp && m.Type != pb.MsgReadIndex {
m.Term = r.Term
}
}
Expand Down

0 comments on commit b869090

Please sign in to comment.