From 07bba4a9a975c740e7962caa4cb72388b10fea19 Mon Sep 17 00:00:00 2001 From: Kaveh Vahedipour Date: Thu, 30 Jun 2016 13:50:12 +0200 Subject: [PATCH] bug fixes agancy backported from devel --- arangod/Agency/Agent.cpp | 4 +-- arangod/Agency/Constituent.cpp | 45 +++++++++++++++++++--------------- arangod/Agency/Constituent.h | 3 ++- 3 files changed, 29 insertions(+), 23 deletions(-) diff --git a/arangod/Agency/Agent.cpp b/arangod/Agency/Agent.cpp index 8f6e45e510..5425031356 100644 --- a/arangod/Agency/Agent.cpp +++ b/arangod/Agency/Agent.cpp @@ -244,7 +244,7 @@ bool Agent::recvAppendEntriesRPC(term_t term, return false; } - if (!_constituent.vote(term, leaderId, prevIndex, prevTerm)) { + if (!_constituent.vote(term, leaderId, prevIndex, prevTerm, true)) { return false; } @@ -440,7 +440,7 @@ void Agent::run() { while (!this->isStopping() && size() > 1) { if (leading()) { // Only if leading - _appendCV.wait(25000); + _appendCV.wait(20000); } else { _appendCV.wait(); // Else wait for our moment in the sun } diff --git a/arangod/Agency/Constituent.cpp b/arangod/Agency/Constituent.cpp index 6b9f132ab2..db9174ba16 100644 --- a/arangod/Agency/Constituent.cpp +++ b/arangod/Agency/Constituent.cpp @@ -106,7 +106,8 @@ bool Constituent::waitForSync() const { /// Random sleep times in election process duration_t Constituent::sleepFor(double min_t, double max_t) { - int32_t left = static_cast(1000.0 * min_t), right = static_cast(1000.0 * max_t); + int32_t left = static_cast(1000.0 * min_t), + right = static_cast(1000.0 * max_t); return duration_t( static_cast(RandomGenerator::interval(left, right))); } @@ -310,7 +311,8 @@ void Constituent::notifyAll() { /// @brief Vote bool Constituent::vote(term_t term, arangodb::consensus::id_t id, - index_t prevLogIndex, term_t prevLogTerm) { + index_t prevLogIndex, term_t prevLogTerm, + bool appendEntries) { term_t t = 0; arangodb::consensus::id_t lid = 0; @@ -322,9 +324,13 @@ bool Constituent::vote(term_t term, arangodb::consensus::id_t id, lid = _leaderID; cast = _cast; _cast = true; + if (appendEntries && t <= term) { + _leaderID = id; + return true; + } } - if (term > t || (t == term && lid == id && !cast)) { + if (term > t || (t == term && lid == id)) { { MUTEX_LOCKER(guard, _castLock); _votedFor = id; // The guy I voted for I assume leader. @@ -338,6 +344,7 @@ bool Constituent::vote(term_t term, arangodb::consensus::id_t id, CONDITION_LOCKER(guard, _cv); _cv.signal(); } + return true; } @@ -487,23 +494,26 @@ void Constituent::run() { } } } - + // Always start off as follower while (!this->isStopping() && size() > 1) { - - long t = 0; if (_role == FOLLOWER) { bool cast = false; - + { MUTEX_LOCKER(guard, _castLock); _cast = false; // New round set not cast vote } - + int32_t left = static_cast(1000000.0 * config().minPing), right = static_cast(1000000.0 * config().maxPing); - t = static_cast(RandomGenerator::interval(left, right)); + long rand_wait = static_cast(RandomGenerator::interval(left, right)); + + { + CONDITION_LOCKER(guardv, _cv); + _cv.wait(rand_wait); + } { MUTEX_LOCKER(guard, _castLock); @@ -515,19 +525,14 @@ void Constituent::run() { } } else if (_role == CANDIDATE) { - callElection(); // Run for office - continue; - } else { - - t = 100000.0 * config().minPing; - - } - - { - CONDITION_LOCKER(guardv, _cv); - _cv.wait(t); + int32_t left = 100000.0 * config().minPing; + long rand_wait = static_cast(left); + { + CONDITION_LOCKER(guardv, _cv); + _cv.wait(rand_wait); + } } } diff --git a/arangod/Agency/Constituent.h b/arangod/Agency/Constituent.h index 1740b0a79b..6e8e762583 100644 --- a/arangod/Agency/Constituent.h +++ b/arangod/Agency/Constituent.h @@ -72,7 +72,8 @@ class Constituent : public arangodb::Thread { bool running() const; /// @brief Called by REST handler - bool vote(term_t, arangodb::consensus::id_t, index_t, term_t); + bool vote(term_t, arangodb::consensus::id_t, index_t, term_t, + bool appendEntries = false); /// @brief My daily business void run() override final;