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 "Logger/Logger.h"
#include "Basics/ConditionLocker.h"
#include "Basics/Mutex.h"
#include "Basics/MutexLocker.h"
#include "Aql/Query.h"
#include "Aql/QueryRegistry.h"
@ -417,11 +419,20 @@ void Constituent::run() {
// Always start off as follower
while (!this->isStopping() && size() > 1) {
if (_role == FOLLOWER) {
_cast = false; // New round set not cast vote
std::this_thread::sleep_for( // Sleep for random time
sleepFor(config().min_ping, config().max_ping));
if (!_cast) {
candidate(); // Next round, we are running
{
MUTEX_LOCKER(guard, _castLock);
_cast = false; // New round set not cast vote
}
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 {
callElection(); // Run for office

View File

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