From 677d9ebe057b47f0885c8ea00c2efca842344e99 Mon Sep 17 00:00:00 2001
From: Georgios Bitzes <georgios.bitzes@cern.ch>
Date: Tue, 24 Mar 2020 12:08:16 +0100
Subject: [PATCH] raft: pre-format string description of vote request

---
 src/raft/RaftDispatcher.cc | 24 +++++++++++++-----------
 src/raft/RaftDispatcher.hh |  2 +-
 2 files changed, 14 insertions(+), 12 deletions(-)

diff --git a/src/raft/RaftDispatcher.cc b/src/raft/RaftDispatcher.cc
index df8f2f31..796a423a 100644
--- a/src/raft/RaftDispatcher.cc
+++ b/src/raft/RaftDispatcher.cc
@@ -537,10 +537,12 @@ void RaftDispatcher::warnIfLagging(LogIndex leaderCommitIndex) {
   }
 }
 
-RaftVoteResponse RaftDispatcher::requestVote(RaftVoteRequest &req) {
+RaftVoteResponse RaftDispatcher::requestVote(const RaftVoteRequest &req) {
+  std::string reqDescr = req.describe(false);
+
   std::scoped_lock lock(raftCommand);
   if(req.candidate == state.getMyself()) {
-    qdb_throw("received vote request from myself: " << req.describe(false));
+    qdb_throw("received vote request from myself: " << reqDescr);
   }
 
   if(!contains(state.getNodes(), req.candidate)) {
@@ -569,7 +571,7 @@ RaftVoteResponse RaftDispatcher::requestVote(RaftVoteRequest &req) {
 
   if(req.lastIndex <= journal.getCommitIndex()) {
     if(req.lastIndex < journal.getLogStart()) {
-      qdb_event("Vetoing " << req.describe(false) << " because its lastIndex (" << req.lastIndex << ") is before my log start (" << journal.getLogStart() << ") - way too far behind me.");
+      qdb_event("Vetoing " << reqDescr << " because its lastIndex (" << req.lastIndex << ") is before my log start (" << journal.getLogStart() << ") - way too far behind me.");
       return {snapshot->term, RaftVote::VETO};
     }
 
@@ -585,25 +587,25 @@ RaftVoteResponse RaftDispatcher::requestVote(RaftVoteRequest &req) {
     // If the node were to ascend, it'll try and remove my req.lastIndex entry
     // as inconsistent, which I consider committed already... Veto!
     if(req.lastTerm != myLastIndexTerm) {
-      qdb_event("Vetoing " << req.describe(false) << " because its ascension would overwrite my committed entry with index " << req.lastIndex);
+      qdb_event("Vetoing " << reqDescr << " because its ascension would overwrite my committed entry with index " << req.lastIndex);
       return {snapshot->term, RaftVote::VETO};
     }
 
     if(req.lastIndex+1 <= journal.getCommitIndex()) {
       // If the node were to ascend, it would add a leadership marker, and try
       // to remove my committed req.lastIndex+1 entry as conflicting. Veto!
-      qdb_event("Vetoing " << req.describe(false) << " because its ascension would overwrite my committed entry with index " << req.lastIndex+1 << " through the addition of a leadership marker.");
+      qdb_event("Vetoing " << reqDescr << " because its ascension would overwrite my committed entry with index " << req.lastIndex+1 << " through the addition of a leadership marker.");
       return {snapshot->term, RaftVote::VETO};
     }
   }
 
   if(snapshot->term != req.term) {
-    qdb_event("Rejecting " << req.describe(false) << " because of a term mismatch: " << snapshot->term << " vs " << req.term);
+    qdb_event("Rejecting " << reqDescr << " because of a term mismatch: " << snapshot->term << " vs " << req.term);
     return {snapshot->term, RaftVote::REFUSED};
   }
 
   if(!snapshot->votedFor.empty() && snapshot->votedFor != req.candidate) {
-    qdb_event("Rejecting " << req.describe(false) << " since I've voted already in this term (" << snapshot->term << ") for " << snapshot->votedFor.toString());
+    qdb_event("Rejecting " << reqDescr << " since I've voted already in this term (" << snapshot->term << ") for " << snapshot->votedFor.toString());
     return {snapshot->term, RaftVote::REFUSED};
   }
 
@@ -615,12 +617,12 @@ RaftVoteResponse RaftDispatcher::requestVote(RaftVoteRequest &req) {
   }
 
   if(req.lastTerm < myLastTerm) {
-    qdb_event("Rejecting " << req.describe(false) << " since my journal is more up-to-date, based on last term: " << myLastIndex << "," << myLastTerm << " vs " << req.lastIndex << "," << req.lastTerm);
+    qdb_event("Rejecting " << reqDescr << " since my journal is more up-to-date, based on last term: " << myLastIndex << "," << myLastTerm << " vs " << req.lastIndex << "," << req.lastTerm);
     return {snapshot->term, RaftVote::REFUSED};
   }
 
   if(req.lastTerm == myLastTerm && req.lastIndex < myLastIndex) {
-    qdb_event("Rejecting " << req.describe(false) << " since my journal is more up-to-date, based on last index: " << myLastIndex << "," << myLastTerm << " vs " << req.lastIndex << "," << req.lastTerm);
+    qdb_event("Rejecting " << reqDescr << " since my journal is more up-to-date, based on last index: " << myLastIndex << "," << myLastTerm << " vs " << req.lastIndex << "," << req.lastTerm);
     return {snapshot->term, RaftVote::REFUSED};
   }
 
@@ -637,12 +639,12 @@ RaftVoteResponse RaftDispatcher::requestVote(RaftVoteRequest &req) {
   // Therefore, register the heartbeat twice just to be sure.
   heartbeatTracker.heartbeat(std::chrono::steady_clock::now());
   if(!state.grantVote(req.term, req.candidate)) {
-    qdb_warn("RaftState rejected " << req.describe(false) << " - probably benign race condition?");
+    qdb_warn("RaftState rejected " << reqDescr << " - probably benign race condition?");
     return {snapshot->term, RaftVote::REFUSED};
   }
   heartbeatTracker.heartbeat(std::chrono::steady_clock::now());
 
-  qdb_event("Granted " << req.describe(false));
+  qdb_event("Granted " << reqDescr);
   return {snapshot->term, RaftVote::GRANTED};
 }
 
diff --git a/src/raft/RaftDispatcher.hh b/src/raft/RaftDispatcher.hh
index 12132157..ca9fdf31 100644
--- a/src/raft/RaftDispatcher.hh
+++ b/src/raft/RaftDispatcher.hh
@@ -60,7 +60,7 @@ public:
 
   RaftHeartbeatResponse heartbeat(const RaftHeartbeatRequest &req);
   RaftAppendEntriesResponse appendEntries(RaftAppendEntriesRequest &&req);
-  RaftVoteResponse requestVote(RaftVoteRequest &req);
+  RaftVoteResponse requestVote(const RaftVoteRequest &req);
 
   //----------------------------------------------------------------------------
   // Return health information
-- 
GitLab