raft: keep leader term consistent with log entries (#299)

* raft: keep leader term consistent with log entries

Signed-off-by: unconsolable <chenzhipeng2012@gmail.com>

* Set leader's term to be greater than/equal to follower

Co-authored-by: NingLin-P <linning@pingcap.com>

Co-authored-by: NingLin-P <linning@pingcap.com>
This commit is contained in:
unconsolable 2021-11-02 16:40:18 +08:00 committed by GitHub
parent 08ded5e967
commit 556518011f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -617,30 +617,31 @@ func TestFollowerCheckMessageType_MsgAppend2AB(t *testing.T) {
func TestFollowerAppendEntries2AB(t *testing.T) {
tests := []struct {
index, term uint64
lterm uint64
ents []*pb.Entry
wents []*pb.Entry
wunstable []*pb.Entry
}{
{
2, 2,
2, 2, 3,
[]*pb.Entry{{Term: 3, Index: 3}},
[]*pb.Entry{{Term: 1, Index: 1}, {Term: 2, Index: 2}, {Term: 3, Index: 3}},
[]*pb.Entry{{Term: 3, Index: 3}},
},
{
1, 1,
1, 1, 4,
[]*pb.Entry{{Term: 3, Index: 2}, {Term: 4, Index: 3}},
[]*pb.Entry{{Term: 1, Index: 1}, {Term: 3, Index: 2}, {Term: 4, Index: 3}},
[]*pb.Entry{{Term: 3, Index: 2}, {Term: 4, Index: 3}},
},
{
0, 0,
0, 0, 2,
[]*pb.Entry{{Term: 1, Index: 1}},
[]*pb.Entry{{Term: 1, Index: 1}, {Term: 2, Index: 2}},
[]*pb.Entry{},
},
{
0, 0,
0, 0, 3,
[]*pb.Entry{{Term: 3, Index: 1}},
[]*pb.Entry{{Term: 3, Index: 1}},
[]*pb.Entry{{Term: 3, Index: 1}},
@ -652,7 +653,7 @@ func TestFollowerAppendEntries2AB(t *testing.T) {
r := newTestRaft(1, []uint64{1, 2, 3}, 10, 1, storage)
r.becomeFollower(2, 2)
r.Step(pb.Message{From: 2, To: 1, MsgType: pb.MessageType_MsgAppend, Term: 2, LogTerm: tt.term, Index: tt.index, Entries: tt.ents})
r.Step(pb.Message{From: 2, To: 1, MsgType: pb.MessageType_MsgAppend, Term: tt.lterm, LogTerm: tt.term, Index: tt.index, Entries: tt.ents})
wents := make([]pb.Entry, 0, len(tt.wents))
for _, ent := range tt.wents {