diff --git a/src/raft/RaftDispatcher.cc b/src/raft/RaftDispatcher.cc index df8f2f312f4e050a86749da617ee1bdee2728951..796a423a96b151a0f5a09a8124b5085c11df7bcd 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 12132157eccf18ccd5d336fb6f925044e4b466c4..ca9fdf319ddc22583fb72cb4a79f1f9ccda0567b 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