void raft_become_candidate(raft_server_t* me_) { raft_server_private_t* me = (raft_server_private_t*)me_; int i; __log(me_, "becoming candidate"); memset(me->votes_for_me, 0, sizeof(int) * me->num_nodes); me->current_term += 1; raft_vote(me_, me->nodeid); raft_set_state(me_, RAFT_STATE_CANDIDATE); /* we need a random factor here to prevent simultaneous candidates */ me->timeout_elapsed = rand() % 500; for (i = 0; i < me->num_nodes; i++) if (me->nodeid != i) raft_send_requestvote(me_, i); }
void raft_become_candidate(raft_server_t* me_) { raft_server_private_t* me = (raft_server_private_t*)me_; int i; __log(me_, NULL, "becoming candidate"); raft_set_current_term(me_, raft_get_current_term(me_) + 1); for (i = 0; i < me->num_nodes; i++) raft_node_vote_for_me(me->nodes[i], 0); raft_vote(me_, me->node); 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; for (i = 0; i < me->num_nodes; i++) if (me->node != me->nodes[i] && raft_node_is_voting(me->nodes[i])) raft_send_requestvote(me_, me->nodes[i]); }