1
0
Fork 0

bug fixes agancy backported from devel

This commit is contained in:
Kaveh Vahedipour 2016-06-30 13:50:12 +02:00
parent 11a295b99e
commit 07bba4a9a9
3 changed files with 29 additions and 23 deletions

View File

@ -244,7 +244,7 @@ bool Agent::recvAppendEntriesRPC(term_t term,
return false; return false;
} }
if (!_constituent.vote(term, leaderId, prevIndex, prevTerm)) { if (!_constituent.vote(term, leaderId, prevIndex, prevTerm, true)) {
return false; return false;
} }
@ -440,7 +440,7 @@ void Agent::run() {
while (!this->isStopping() && size() > 1) { while (!this->isStopping() && size() > 1) {
if (leading()) { // Only if leading if (leading()) { // Only if leading
_appendCV.wait(25000); _appendCV.wait(20000);
} else { } else {
_appendCV.wait(); // Else wait for our moment in the sun _appendCV.wait(); // Else wait for our moment in the sun
} }

View File

@ -106,7 +106,8 @@ bool Constituent::waitForSync() const {
/// Random sleep times in election process /// Random sleep times in election process
duration_t Constituent::sleepFor(double min_t, double max_t) { duration_t Constituent::sleepFor(double min_t, double max_t) {
int32_t left = static_cast<int32_t>(1000.0 * min_t), right = static_cast<int32_t>(1000.0 * max_t); int32_t left = static_cast<int32_t>(1000.0 * min_t),
right = static_cast<int32_t>(1000.0 * max_t);
return duration_t( return duration_t(
static_cast<long>(RandomGenerator::interval(left, right))); static_cast<long>(RandomGenerator::interval(left, right)));
} }
@ -310,7 +311,8 @@ void Constituent::notifyAll() {
/// @brief Vote /// @brief Vote
bool Constituent::vote(term_t term, arangodb::consensus::id_t id, 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; term_t t = 0;
arangodb::consensus::id_t lid = 0; arangodb::consensus::id_t lid = 0;
@ -322,9 +324,13 @@ bool Constituent::vote(term_t term, arangodb::consensus::id_t id,
lid = _leaderID; lid = _leaderID;
cast = _cast; cast = _cast;
_cast = true; _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); MUTEX_LOCKER(guard, _castLock);
_votedFor = id; // The guy I voted for I assume leader. _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); CONDITION_LOCKER(guard, _cv);
_cv.signal(); _cv.signal();
} }
return true; return true;
} }
@ -487,23 +494,26 @@ void Constituent::run() {
} }
} }
} }
// Always start off as follower // Always start off as follower
while (!this->isStopping() && size() > 1) { while (!this->isStopping() && size() > 1) {
long t = 0;
if (_role == FOLLOWER) { if (_role == FOLLOWER) {
bool cast = false; bool cast = false;
{ {
MUTEX_LOCKER(guard, _castLock); MUTEX_LOCKER(guard, _castLock);
_cast = false; // New round set not cast vote _cast = false; // New round set not cast vote
} }
int32_t left = static_cast<int32_t>(1000000.0 * config().minPing), int32_t left = static_cast<int32_t>(1000000.0 * config().minPing),
right = static_cast<int32_t>(1000000.0 * config().maxPing); right = static_cast<int32_t>(1000000.0 * config().maxPing);
t = static_cast<long>(RandomGenerator::interval(left, right)); long rand_wait = static_cast<long>(RandomGenerator::interval(left, right));
{
CONDITION_LOCKER(guardv, _cv);
_cv.wait(rand_wait);
}
{ {
MUTEX_LOCKER(guard, _castLock); MUTEX_LOCKER(guard, _castLock);
@ -515,19 +525,14 @@ void Constituent::run() {
} }
} else if (_role == CANDIDATE) { } else if (_role == CANDIDATE) {
callElection(); // Run for office callElection(); // Run for office
continue;
} else { } else {
int32_t left = 100000.0 * config().minPing;
t = 100000.0 * config().minPing; long rand_wait = static_cast<long>(left);
{
} CONDITION_LOCKER(guardv, _cv);
_cv.wait(rand_wait);
{ }
CONDITION_LOCKER(guardv, _cv);
_cv.wait(t);
} }
} }

View File

@ -72,7 +72,8 @@ class Constituent : public arangodb::Thread {
bool running() const; bool running() const;
/// @brief Called by REST handler /// @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 /// @brief My daily business
void run() override final; void run() override final;