1
0
Fork 0

replace sleep_for with wait

This commit is contained in:
Kaveh Vahedipour 2016-04-18 10:46:26 +02:00
parent a0f05f584f
commit 6ae8aa7c0d
2 changed files with 17 additions and 5 deletions

View File

@ -25,6 +25,8 @@
#include "Cluster/ClusterComm.h" #include "Cluster/ClusterComm.h"
#include "Logger/Logger.h" #include "Logger/Logger.h"
#include "Basics/ConditionLocker.h" #include "Basics/ConditionLocker.h"
#include "Basics/Mutex.h"
#include "Basics/MutexLocker.h"
#include "Aql/Query.h" #include "Aql/Query.h"
#include "Aql/QueryRegistry.h" #include "Aql/QueryRegistry.h"
@ -417,11 +419,20 @@ void Constituent::run() {
// Always start off as follower // Always start off as follower
while (!this->isStopping() && size() > 1) { while (!this->isStopping() && size() > 1) {
if (_role == FOLLOWER) { if (_role == FOLLOWER) {
_cast = false; // New round set not cast vote {
std::this_thread::sleep_for( // Sleep for random time MUTEX_LOCKER(guard, _castLock);
sleepFor(config().min_ping, config().max_ping)); _cast = false; // New round set not cast vote
if (!_cast) { }
candidate(); // Next round, we are running
dist_t dis(config().min_ping, config().max_ping);
long rand_wait = static_cast<long>(dis(_gen)*1000000.0);
bool timedout = _cv.wait(rand_wait);
{
MUTEX_LOCKER(guard, _castLock);
if (!_cast) {
candidate(); // Next round, we are running
}
} }
} else { } else {
callElection(); // Run for office callElection(); // Run for office

View File

@ -158,6 +158,7 @@ private:
id_t _voted_for; id_t _voted_for;
arangodb::basics::ConditionVariable _cv; // agency callbacks arangodb::basics::ConditionVariable _cv; // agency callbacks
arangodb::Mutex _castLock;
}; };