From 1124b8d3710e98a51b5ed6bca28cc20a1135b831 Mon Sep 17 00:00:00 2001
From: Matija Santl <matija.santl@memgraph.com>
Date: Tue, 2 Apr 2019 12:53:22 +0200
Subject: [PATCH] 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
---
 src/raft/raft_server.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/raft/raft_server.cpp b/src/raft/raft_server.cpp
index 4b54bc10f..b9a4a50ef 100644
--- a/src/raft/raft_server.cpp
+++ b/src/raft/raft_server.cpp
@@ -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;