Unverified Commit 99e3d4a6 authored by Shaya Potter's avatar Shaya Potter Committed by GitHub
Browse files

Improve entry "vote/append couting" in the case of a leader demotion (#13)

Before, we only did "auto commit" of entries if the number of voters == 1.  As we can have a "non voting leader", this is wrong, as the voter can be another node, not the leader, therefore we add a condition to ensure that the leader is a voter too, so that we know that the leader is the single voter.
parent 004c63e5
......@@ -177,7 +177,7 @@ int raft_node_is_voting_committed(raft_node_t* me_)
raft_node_id_t raft_node_get_id(raft_node_t* me_)
{
raft_node_private_t* me = (raft_node_private_t*)me_;
return me->id;
return me != NULL ? me->id : -1;
}
void raft_node_set_addition_committed(raft_node_t* me_, int committed)
......
......@@ -392,8 +392,8 @@ int raft_recv_appendentries_response(raft_server_t* me_,
raft_entry_t* ety = raft_get_entry_from_idx(me_, point);
if (raft_get_commit_idx(me_) < point && ety->term == me->current_term)
{
int i, votes = 1;
for (i = 0; i < me->num_nodes; i++)
int votes = raft_node_is_voting(me->node) ? 1 : 0;
for (int i = 0; i < me->num_nodes; i++)
{
raft_node_t* node = me->nodes[i];
if (me->node != node &&
......@@ -803,9 +803,10 @@ int raft_recv_entry(raft_server_t* me_,
raft_send_appendentries(me_, node);
}
/* if we're the only node, we can consider the entry committed */
if (1 == raft_get_num_voting_nodes(me_))
/* 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)) {
raft_set_commit_idx(me_, raft_get_current_idx(me_));
}
r->id = ety->id;
r->idx = raft_get_current_idx(me_);
......
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