-
Notifications
You must be signed in to change notification settings - Fork 399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
raft: become_pre_candidate resets votes #83
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,8 +32,10 @@ use std::ops::DerefMut; | |
use std::panic::{self, AssertUnwindSafe}; | ||
|
||
use protobuf::{self, RepeatedField}; | ||
use raft::eraftpb::{ConfChange, ConfChangeType, ConfState, Entry, EntryType, HardState, Message, | ||
MessageType, Snapshot}; | ||
use raft::eraftpb::{ | ||
ConfChange, ConfChangeType, ConfState, Entry, EntryType, HardState, Message, MessageType, | ||
Snapshot, | ||
}; | ||
use rand; | ||
|
||
use raft::storage::MemStorage; | ||
|
@@ -4132,3 +4134,86 @@ fn test_election_tick_range() { | |
assert_eq!(randomized_timeout, cfg.election_tick); | ||
} | ||
} | ||
|
||
// TestPreVoteWithSplitVote verifies that after split vote, cluster can complete | ||
// election in next round. | ||
#[test] | ||
fn test_prevote_with_split_vote() { | ||
let bootstrap = |id| { | ||
let mut cfg = new_test_config(id, vec![1, 2, 3], 10, 1); | ||
cfg.pre_vote = true; | ||
let mut raft = Raft::new(&cfg, new_storage()); | ||
raft.become_follower(1, INVALID_ID); | ||
Interface::new(raft) | ||
}; | ||
let (peer1, peer2, peer3) = (bootstrap(1), bootstrap(2), bootstrap(3)); | ||
|
||
let mut network = Network::new(vec![Some(peer1), Some(peer2), Some(peer3)]); | ||
network.send(vec![new_message(1, 1, MessageType::MsgHup, 0)]); | ||
|
||
// simulate leader down. followers start split vote. | ||
network.isolate(1); | ||
network.send(vec![ | ||
new_message(2, 2, MessageType::MsgHup, 0), | ||
new_message(3, 3, MessageType::MsgHup, 0), | ||
]); | ||
|
||
// check whether the term values are expected | ||
assert_eq!( | ||
network.peers[&2].term, 3, | ||
"peer 2 term: {:?}, want {:?}", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The default error message is quite informative. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. I will change it here, just changed it in #84. |
||
network.peers[&2].term, 3, | ||
); | ||
assert_eq!( | ||
network.peers[&3].term, 3, | ||
"peer 3 term: {:?}, want {:?}", | ||
network.peers[&3].term, 3, | ||
); | ||
|
||
// check state | ||
assert_eq!( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why Candidate here? not PreCandidate? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
network.peers[&2].state, | ||
StateRole::Candidate, | ||
"peer 2 state: {:?}, want {:?}", | ||
network.peers[&2].state, | ||
StateRole::Candidate, | ||
); | ||
assert_eq!( | ||
network.peers[&3].state, | ||
StateRole::Candidate, | ||
"peer 3 state: {:?}, want {:?}", | ||
network.peers[&3].state, | ||
StateRole::Candidate, | ||
); | ||
|
||
// node 2 election timeout first | ||
network.send(vec![new_message(2, 2, MessageType::MsgHup, 0)]); | ||
|
||
// check whether the term values are expected | ||
assert_eq!( | ||
network.peers[&2].term, 4, | ||
"peer 2 term: {:?}, want {:?}", | ||
network.peers[&2].term, 4, | ||
); | ||
assert_eq!( | ||
network.peers[&3].term, 4, | ||
"peer 3 term: {:?}, want {:?}", | ||
network.peers[&3].term, 4, | ||
); | ||
|
||
// check state | ||
assert_eq!( | ||
network.peers[&2].state, | ||
StateRole::Leader, | ||
"peer 2 state: {:?}, want {:?}", | ||
network.peers[&2].state, | ||
StateRole::Leader, | ||
); | ||
assert_eq!( | ||
network.peers[&3].state, | ||
StateRole::Follower, | ||
"peer 3 state: {:?}, want {:?}", | ||
network.peers[&3].state, | ||
StateRole::Follower, | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use
new_test_raft_with_prevote
?