Fix Raft election

Summary:
Based on the failure manifested in
https://apollo.memgraph.io/runs/512803/ it seems like machines give each other
votes for the same term.

Looking at the code, `voted_for_` variable wasn't assigned on the election start
and the election starter could grant his vote to someone else but would still
count his vote to himself.

Reviewers: ipaljak, mferencevic

Reviewed By: mferencevic

Subscribers: pullbot, vkasljevic

Differential Revision: https://phabricator.memgraph.io/D1941
This commit is contained in:
Matija Santl 2019-04-02 12:53:22 +02:00
parent 2a63e2f12d
commit 1124b8d371

View File

@ -570,7 +570,7 @@ void RaftServer::Transition(const Mode &new_mode) {
// RequestVote RPCs in parallel to each of the other servers in the
// cluster."
SetCurrentTerm(current_term_ + 1);
disk_storage_.Put(kVotedForKey, std::to_string(server_id_));
SetVotedFor(server_id_);
granted_votes_ = 1;
vote_requested_.assign(coordination_->WorkerCount(), false);
@ -895,6 +895,8 @@ void RaftServer::PeerThreadMain(uint16_t peer_id) {
}
});
vote_requested_[peer_id] = true;
lock.unlock(); // Release lock while waiting for response
auto reply = peer_future.get();
lock.lock();
@ -911,8 +913,6 @@ void RaftServer::PeerThreadMain(uint16_t peer_id) {
continue;
}
vote_requested_[peer_id] = true;
if (reply.vote_granted) {
VLOG(40) << "Server " << server_id_ << ": Got vote from "
<< peer_id;