-
Notifications
You must be signed in to change notification settings - Fork 394
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
Learner needs to respond vote requests. #58
Merged
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4636cf6
Learner needs to respond vote requests.
hicqu d2c8ff5
improve test case.
hicqu 9c8fc4f
Merge branch 'master' into learner-vote-response
hicqu b35556d
address comments.
hicqu 17981d2
Merge branch 'learner-vote-response' of github.com:hicqu/raft-rs into…
hicqu b14dc5f
address comments.
hicqu 9b99942
address comments.
hicqu 08fc147
Merge branch 'master' into learner-vote-response
hicqu 06f898f
don't need to any tick more with MsgHup.
hicqu faa583c
update protobuf dependency version.
hicqu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,19 +25,19 @@ | |
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::cmp; | ||
use std::collections::HashMap; | ||
use std::ops::Deref; | ||
use std::ops::DerefMut; | ||
use std::cmp; | ||
use std::panic::{self, AssertUnwindSafe}; | ||
|
||
use protobuf::{self, RepeatedField}; | ||
use raft::eraftpb::{ConfChange, ConfChangeType, ConfState, Entry, EntryType, HardState, Message, | ||
MessageType, Snapshot}; | ||
use rand; | ||
|
||
use raft::*; | ||
use raft::storage::MemStorage; | ||
use raft::*; | ||
|
||
pub fn ltoa(raft_log: &RaftLog<MemStorage>) -> String { | ||
let mut s = format!("committed: {}\n", raft_log.committed); | ||
|
@@ -897,34 +897,22 @@ fn test_vote_from_any_state_for_type(vt: MessageType) { | |
StateRole::Follower | ||
); | ||
assert_eq!( | ||
r.term, | ||
new_term, | ||
r.term, new_term, | ||
"{:?},{:?}, term {}, want {}", | ||
vt, | ||
state, | ||
r.term, | ||
new_term | ||
vt, state, r.term, new_term | ||
); | ||
assert_eq!(r.vote, 2, "{:?},{:?}, vote {}, want 2", vt, state, r.vote); | ||
} else { | ||
// In a pre-vote, nothing changes. | ||
assert_eq!( | ||
r.state, | ||
state, | ||
r.state, state, | ||
"{:?},{:?}, state {:?}, want {:?}", | ||
vt, | ||
state, | ||
r.state, | ||
state | ||
vt, state, r.state, state | ||
); | ||
assert_eq!( | ||
r.term, | ||
orig_term, | ||
r.term, orig_term, | ||
"{:?},{:?}, term {}, want {}", | ||
vt, | ||
state, | ||
r.term, | ||
orig_term | ||
vt, state, r.term, orig_term | ||
); | ||
// If state == Follower or PreCandidate, r hasn't voted yet. | ||
// In Candidate or Leader, it's voted for itself. | ||
|
@@ -2030,11 +2018,9 @@ fn test_candidate_reset_term(message_type: MessageType) { | |
|
||
// follower c term is reset with leader's | ||
assert_eq!( | ||
nt.peers[&3].term, | ||
nt.peers[&1].term, | ||
nt.peers[&3].term, nt.peers[&1].term, | ||
"follower term expected same term as leader's {}, got {}", | ||
nt.peers[&1].term, | ||
nt.peers[&3].term, | ||
nt.peers[&1].term, nt.peers[&3].term, | ||
) | ||
} | ||
|
||
|
@@ -3798,21 +3784,6 @@ fn test_learner_promotion() { | |
assert_eq!(network.peers[&2].state, StateRole::Leader); | ||
} | ||
|
||
// TestLearnerCannotVote checks that a learner can't vote even it receives a valid Vote request. | ||
#[test] | ||
fn test_learner_cannot_vote() { | ||
let mut n2 = new_test_learner_raft(2, vec![1], vec![2], 10, 1, new_storage()); | ||
n2.become_follower(1, INVALID_ID); | ||
|
||
let mut msg_vote = new_message(1, 2, MessageType::MsgRequestVote, 0); | ||
msg_vote.set_term(2); | ||
msg_vote.set_log_term(11); | ||
msg_vote.set_index(11); | ||
n2.step(msg_vote).unwrap(); | ||
|
||
assert_eq!(n2.msgs.len(), 0); | ||
} | ||
|
||
// TestLearnerLogReplication tests that a learner can receive entries from the leader. | ||
#[test] | ||
fn test_learner_log_replication() { | ||
|
@@ -3978,3 +3949,37 @@ fn test_remove_learner() { | |
assert!(n1.prs().nodes().is_empty()); | ||
assert!(n1.prs().learner_nodes().is_empty()); | ||
} | ||
|
||
#[test] | ||
fn test_learner_respond_vote() { | ||
let mut n1 = new_test_learner_raft(1, vec![1, 2], vec![3], 10, 1, new_storage()); | ||
n1.become_follower(1, INVALID_ID); | ||
n1.reset_randomized_election_timeout(); | ||
|
||
let mut n3 = new_test_learner_raft(3, vec![1, 2], vec![3], 10, 1, new_storage()); | ||
n3.become_follower(1, INVALID_ID); | ||
n3.reset_randomized_election_timeout(); | ||
|
||
let timeout = n1.get_election_timeout(); | ||
|
||
let do_campaign = |nw: &mut Network| { | ||
for _ in 0..timeout << 1 { | ||
nw.peers.get_mut(&1).unwrap().tick(); | ||
nw.peers.get_mut(&3).unwrap().tick(); | ||
} | ||
let msg = new_message(1, 1, MessageType::MsgHup, 0); | ||
nw.send(vec![msg]); | ||
}; | ||
|
||
let mut network = Network::new(vec![Some(n1), None, Some(n3)]); | ||
network.isolate(2); | ||
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.
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. After I remove this, store 1 will be elected as leader. |
||
|
||
// Can't elect new leader because 1 won't send MsgRequestVote to 3. | ||
do_campaign(&mut network); | ||
assert_eq!(network.peers[&1].state, StateRole::Candidate); | ||
|
||
// After promote 3 to voter, election should success. | ||
network.peers.get_mut(&1).unwrap().add_node(3); | ||
do_campaign(&mut network); | ||
assert_eq!(network.peers[&1].state, StateRole::Leader); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What's the point of ticking node 3?
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.
To show all
Vote
messages come from store 1 even if 3 will alsostep(MsgHup)
.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.
I don't get it. Where are all those messages showed? How does it help with the test case?
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.
Removed.
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.
No, they are still here.