diff --git a/arangod/Agency/Agent.cpp b/arangod/Agency/Agent.cpp index c69d9cad1d..4ce261ecc8 100644 --- a/arangod/Agency/Agent.cpp +++ b/arangod/Agency/Agent.cpp @@ -312,7 +312,7 @@ void Agent::sendAppendEntriesRPC() { for (auto const& followerId : _config.active()) { - if (followerId != myid) { + if (followerId != myid && leading()) { term_t t(0); @@ -370,6 +370,11 @@ void Agent::sendAppendEntriesRPC() { << highest << " to follower " << followerId; } + // Really leading? + if (challengeLeadership()) { + _constituent.candidate(); + } + // Send request auto headerFields = std::make_unique>(); @@ -705,17 +710,12 @@ void Agent::run() { // Leader working only if (leading()) { - // Really leading? - if (challengeLeadership()) { - _constituent.candidate(); - } + // Append entries to followers + sendAppendEntriesRPC(); // Don't panic _appendCV.wait(1000); - // Append entries to followers - sendAppendEntriesRPC(); - // Detect faulty agent and replace // if possible and only if not already activating if (duration(system_clock::now() - tp).count() > 10.0) { @@ -862,6 +862,11 @@ void Agent::beginShutdown() { } } + +bool Agent::prepareLead() { + return true; +} + /// Becoming leader bool Agent::lead() { // Key value stores diff --git a/arangod/Agency/Agent.h b/arangod/Agency/Agent.h index 2c05bec520..c4c6c4c963 100644 --- a/arangod/Agency/Agent.h +++ b/arangod/Agency/Agent.h @@ -83,6 +83,9 @@ class Agent : public arangodb::Thread { /// @brief Pick up leadership tasks bool lead(); + /// @brief Prepare leadership + bool prepareLead(); + /// @brief Load persistent state bool load(); diff --git a/arangod/Agency/Constituent.cpp b/arangod/Agency/Constituent.cpp index 1543a07874..77ab43d5dc 100644 --- a/arangod/Agency/Constituent.cpp +++ b/arangod/Agency/Constituent.cpp @@ -210,8 +210,13 @@ void Constituent::lead(term_t term, LOG_TOPIC(DEBUG, Logger::AGENCY) << ss.str(); } - // we need to rebuild spear_head and read_db; + // we need to rebuild spear_head and read_db + _agent->prepareLead(); + + + // we need to start work as leader _agent->lead(); + } /// Become candidate @@ -564,7 +569,7 @@ void Constituent::run() { // in the beginning, pure random if (_lastHeartbeatSeen > 0.0) { double now = TRI_microtime(); - randWait -= static_cast(M * (now - _lastHeartbeatSeen)); + randWait -= static_cast(M * (_lastHeartbeatSeen-now)); } } diff --git a/arangod/Agency/Inception.cpp b/arangod/Agency/Inception.cpp index 911a054c8e..0241969b84 100644 --- a/arangod/Agency/Inception.cpp +++ b/arangod/Agency/Inception.cpp @@ -447,7 +447,7 @@ bool Inception::estimateRAFTInterval() { double precision = 1.0e-2; mn = precision * - std::ceil((1. / precision)*(1. + precision * (maxmean + 3.*maxstdev))); + std::ceil((1. / precision)*(.35 + precision * (maxmean + 3.*maxstdev))); if (config.waitForSync()) { mn *= 4.; } diff --git a/arangod/Agency/Supervision.cpp b/arangod/Agency/Supervision.cpp index 6f10af52d4..6a57a11f73 100644 --- a/arangod/Agency/Supervision.cpp +++ b/arangod/Agency/Supervision.cpp @@ -60,14 +60,10 @@ Supervision::Supervision() Supervision::~Supervision() { shutdown(); }; void Supervision::wakeUp() { - { - MUTEX_LOCKER(locker, _lock); - updateSnapshot(); - upgradeAgency(); - } - - CONDITION_LOCKER(guard, _cv); - _cv.signal(); + + updateSnapshot(); + upgradeAgency(); + } static std::string const syncPrefix = "/Sync/ServerStates/"; @@ -441,6 +437,12 @@ void Supervision::run() { } while (!this->isStopping()) { + + // Get bunch of job IDs from agency for future jobs + if (_jobId == 0 || _jobId == _jobIdMax) { + getUniqueIds(); // cannot fail but only hang + } + { MUTEX_LOCKER(locker, _lock); @@ -542,11 +544,6 @@ void Supervision::handleShutdown() { // Guarded by caller bool Supervision::handleJobs() { - // Get bunch of job IDs from agency for future jobs - if (_jobId == 0 || _jobId == _jobIdMax) { - getUniqueIds(); // cannot fail but only hang - } - // Do supervision shrinkCluster(); @@ -882,10 +879,9 @@ void Supervision::getUniqueIds() { // is initialized by some other server... while (!this->isStopping()) { try { - latestId = std::stoul(_agent->readDB() - .get(_agencyPrefix + "/Sync/LatestID") - .slice() - .toJson()); + MUTEX_LOCKER(locker, _lock); + latestId = std::stoul( + _agent->readDB().get(_agencyPrefix + "/Sync/LatestID").slice().toJson()); } catch (...) { std::this_thread::sleep_for(std::chrono::seconds(1)); continue; diff --git a/scripts/startStandAloneAgency.sh b/scripts/startStandAloneAgency.sh index 74e5150670..c94381870a 100755 --- a/scripts/startStandAloneAgency.sh +++ b/scripts/startStandAloneAgency.sh @@ -163,7 +163,7 @@ mkdir -p agency PIDS="" aaid=(`seq 0 $(( $POOLSZ - 1 ))`) -shuffle +#shuffle count=1