Skip to content
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

Correct learner checking in handle_transfer_leader #165

Merged
merged 1 commit into from
Jan 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/raft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1203,13 +1203,13 @@ impl<T: Storage> Raft<T> {
}
}

fn handle_transfer_leader(&mut self, m: &Message, pr: &mut Progress) {
if self.is_learner {
debug!("{} is learner. Ignored transferring leadership", self.tag);
fn handle_transfer_leader(&mut self, m: &Message, prs: &mut ProgressSet) {
let from = m.get_from();
if prs.learner_ids().contains(&from) {
debug!("{} is learner. Ignored transferring leadership", from);
return;
}

let lead_transferee = m.get_from();
let lead_transferee = from;
let last_lead_transferee = self.lead_transferee;
if last_lead_transferee.is_some() {
if last_lead_transferee.unwrap() == lead_transferee {
Expand Down Expand Up @@ -1244,6 +1244,7 @@ impl<T: Storage> Raft<T> {
// so reset r.electionElapsed.
self.election_elapsed = 0;
self.lead_transferee = Some(lead_transferee);
let pr = prs.get_mut(from).unwrap();
if pr.matched == self.raft_log.last_index() {
self.send_timeout_now(lead_transferee);
info!(
Expand Down Expand Up @@ -1323,8 +1324,7 @@ impl<T: Storage> Raft<T> {
);
}
MessageType::MsgTransferLeader => {
let pr = prs.get_mut(m.get_from()).unwrap();
self.handle_transfer_leader(m, pr);
self.handle_transfer_leader(m, &mut prs);
}
_ => {}
}
Expand Down
17 changes: 17 additions & 0 deletions tests/integration_cases/test_raft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3274,6 +3274,23 @@ fn test_leader_transfer_to_non_existing_node() {
check_leader_transfer_state(&nt.peers[&1], StateRole::Leader, 1);
}

#[test]
fn test_leader_transfer_to_learner() {
setup_for_test();
let mut leader_config = new_test_config(1, vec![1], 10, 1);
leader_config.learners = vec![2];
let leader = new_test_raft_with_config(&leader_config, new_storage());
let mut learner_config = new_test_config(2, vec![1], 10, 1);
learner_config.learners = vec![2];
let learner = new_test_raft_with_config(&learner_config, new_storage());
let mut nt = Network::new(vec![Some(leader), Some(learner)]);
nt.send(vec![new_message(1, 1, MessageType::MsgHup, 0)]);

// Transfer leadership to learner node, there will be noop.
nt.send(vec![new_message(2, 1, MessageType::MsgTransferLeader, 0)]);
check_leader_transfer_state(&nt.peers[&1], StateRole::Leader, 1);
}

#[test]
fn test_leader_transfer_timeout() {
setup_for_test();
Expand Down