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

Fix to returning stale leader ID issue #116

Merged
merged 1 commit into from
May 14, 2020
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
8 changes: 7 additions & 1 deletion include/libnuraft/raft_server.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,13 @@ public:
* @return Leader ID
* -1 if there is no live leader.
*/
int32 get_leader() const { return leader_; }
int32 get_leader() const {
// We should handle the case when `role_` is already
// updated, but `leader_` value is stale.
if ( leader_ == id_ &&
role_ != srv_role::leader ) return -1;
return leader_;
}

/**
* Check if this server is leader.
Expand Down
7 changes: 6 additions & 1 deletion src/handle_vote.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ void raft_server::request_prevote() {
}

hb_alive_ = false;
leader_ = -1;
pre_vote_.reset(state_->get_term());
// Count for myself.
pre_vote_.dead_++;
Expand Down Expand Up @@ -147,7 +148,11 @@ void raft_server::initiate_vote(bool ignore_priority) {
ctx_->state_mgr_->save_state(*state_);
request_vote(ignore_priority);
}
hb_alive_ = false;

if (role_ != srv_role::leader) {
hb_alive_ = false;
leader_ = -1;
}
}

void raft_server::request_vote(bool ignore_priority) {
Expand Down