Commit 2241267e authored by Willem Thiart's avatar Willem Thiart
Browse files

Fix: negative random election timeout handicaps

Fast nodes can deadlock the cluster if the random timeout is always
positive. We get around this by allowing the random timeout to be
negative (ie. a handicap in the election race).
parent 2b469cbf
......@@ -139,9 +139,11 @@ void raft_become_candidate(raft_server_t* me_)
me->current_leader = NULL;
raft_set_state(me_, RAFT_STATE_CANDIDATE);
/* we need a random factor here to prevent simultaneous candidates */
/* TODO: this should probably be lower */
me->timeout_elapsed = rand() % me->election_timeout;
/* We need a random factor here to prevent simultaneous candidates.
* If the randomness is always positive it's possible that a fast node
* would deadlock the cluster by always gaining a headstart. To prevent
* this, we allow a negative randomness as a potential handicap. */
me->timeout_elapsed = me->election_timeout - 2 * (rand() % me->election_timeout);
for (i = 0; i < me->num_nodes; i++)
if (me->node != me->nodes[i] && raft_node_is_voting(me->nodes[i]))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment