Unverified Commit 34da95b5 authored by Shaya Potter's avatar Shaya Potter Committed by GitHub
Browse files

abstract out logic of "we're the only voting node" (#27)

create a self documenting function name instead of throwing in a bunch of conditions
parent 99e3d4a6
......@@ -154,6 +154,8 @@ int raft_node_has_vote_for_me(raft_node_t* me_);
void raft_node_set_has_sufficient_logs(raft_node_t* me_);
int raft_is_single_node_voting_cluster(raft_server_t *me_);
int raft_votes_is_majority(const int nnodes, const int nvotes);
void raft_handle_append_cfg_change(raft_server_t* me_, raft_entry_t* ety, const raft_index_t idx);
......
......@@ -251,7 +251,8 @@ int raft_periodic(raft_server_t* me_, int msec_since_last_period)
me->timeout_elapsed += msec_since_last_period;
/* Only one voting node means it's safe for us to become the leader */
if ((1 == raft_get_num_voting_nodes(me_)) && raft_node_is_voting(raft_get_my_node(me_)) && !raft_is_leader(me_)) {
if (raft_is_single_node_voting_cluster(me_) && !raft_is_leader(me_)) {
// need to update term on new leadership
int e = raft_set_current_term(me_, raft_get_current_term(me_) + 1);
if (e != 0) {
return e;
......@@ -261,7 +262,6 @@ int raft_periodic(raft_server_t* me_, int msec_since_last_period)
if (e != 0) {
return e;
}
// need to update term on new leadership
}
if (me->state == RAFT_STATE_LEADER)
......@@ -803,8 +803,8 @@ int raft_recv_entry(raft_server_t* me_,
raft_send_appendentries(me_, node);
}
/* if we are the only voter (1 voter and we are that voter), commit now, as no appendentries_response will occur */
if (1 == raft_get_num_voting_nodes(me_) && raft_node_is_voting(me->node)) {
/* if we are the only voter, commit now, as no appendentries_response will occur */
if (raft_is_single_node_voting_cluster(me_)) {
raft_set_commit_idx(me_, raft_get_current_idx(me_));
}
......
......@@ -274,3 +274,10 @@ void raft_set_snapshot_metadata(raft_server_t *me_, raft_term_t term, raft_index
me->snapshot_last_term = term;
me->snapshot_last_idx = idx;
}
int raft_is_single_node_voting_cluster(raft_server_t *me_)
{
raft_server_private_t* me = (raft_server_private_t*)me_;
return (1 == raft_get_num_voting_nodes(me_) && raft_node_is_voting(me->node));
}
\ No newline at end of file
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