Project2AA's test case "TestLeaderElectionInOneRoundRPC2AA" and "TestLeaderElection2AA" may have problems
bobbai00 opened this issue · 2 comments
Dear TiDB developers,
Hi! Thanks for writing such a marvelous course project. The framework code are really amazing. I'm currently working on Part A of project2. But after coding and debugging, I pass all the other test cases but still cannot pass the test case TestLeaderElectionInOneRoundRPC2AA
and TestLeaderElection2AA
. So I check the test file tinykv/raft/raft_paper_test.go
. The code of TestLeaderElectionInOneRoundRPC2AA
is below:
func TestLeaderElectionInOneRoundRPC2AA(t *testing.T) {
// .... some code, I'll leave it out for now
for i, tt := range tests {
r := newTestRaft(1, idsBySize(tt.size), 10, 1, NewMemoryStorage())
r.Step(pb.Message{From: 1, To: 1, MsgType: pb.MessageType_MsgHup})
for id, vote := range tt.votes {
r.Step(pb.Message{From: id, To: 1, Term: r.Term, MsgType: pb.MessageType_MsgRequestVoteResponse, Reject: !vote})
}
if r.State != tt.state {
t.Errorf("#%d: state = %s, want %s", i, r.State, tt.state)
}
if g := r.Term; g != 1 {
// My code fails at here, which logged:
// #0: term = 2, want 1
// #1: term = 2, want 1
// #2: term = 2, want 1
// ...
// and so on
t.Errorf("#%d: term = %d, want %d", i, g, 1)
}
}
}
According to my understanding of raft algorithm, after follower/candidate starts a new election(by running r.Step(pb.Message{MsgType: pb.MessageType_MsgHup})
), its term needs to add 1. Because the initial term is 1, so after executing r.Step(pb.Message{From: 1, To: 1, MsgType: pb.MessageType_MsgHup})
, the term should be 2 not 1. But this test case requires the current term to be 1. The same problem occurs in the TestLeaderElection2AA
case. So are there some problems with this test case, or my understanding about the election is wrong? Thanks for your patience and kind help!
the initial term start from 0 not 1
the initial term start from 0 not 1
Got it. I just saw the hint😂Thanks!