Unverified Commit 04cdff03 authored by Ozan Tezcan's avatar Ozan Tezcan Committed by GitHub
Browse files

Fix: single node cluster noop entry commit (#31)

parent 34da95b5
......@@ -175,10 +175,16 @@ int raft_become_leader(raft_server_t* me_)
raft_entry_t *noop = raft_entry_new(0);
noop->term = raft_get_current_term(me_);
noop->type = RAFT_LOGTYPE_NO_OP;
int e = raft_append_entry(me_, noop);
raft_entry_release(noop);
if (0 != e)
return e;
raft_entry_release(noop);
// Commit noop immediately if this is a single node cluster
if (raft_is_single_node_voting_cluster(me_)) {
raft_set_commit_idx(me_, raft_get_current_idx(me_));
}
}
raft_set_state(me_, RAFT_STATE_LEADER);
......
......@@ -3815,6 +3815,29 @@ void TestRaft_read_action_callback(
CuAssertIntEquals(tc, 0, ra.last_cb_safe);
}
void single_node_commits_noop_cb(void* arg, int can_read)
{
(void) can_read;
*((char**) arg) = "success";
}
void TestRaft_single_node_commits_noop(CuTest * tc)
{
static char* str = "test";
void *r = raft_new();
raft_add_node(r, NULL, 1, 1);
raft_set_current_term(r, 2);
raft_set_commit_idx(r, 0);
raft_periodic(r, 500);
raft_queue_read_request(r, single_node_commits_noop_cb, &str);
raft_periodic(r, 500);
CuAssertIntEquals(tc, 1, raft_get_commit_idx(r));
CuAssertStrEquals(tc, "success", str);
}
int main(void)
{
CuString *output = CuStringNew();
......@@ -3938,6 +3961,7 @@ int main(void)
SUITE_ADD_TEST(suite, TestRaft_leader_recv_requestvote_responds_without_granting);
SUITE_ADD_TEST(suite, TestRaft_leader_recv_appendentries_response_set_has_sufficient_logs_after_voting_committed);
SUITE_ADD_TEST(suite, TestRaft_read_action_callback);
SUITE_ADD_TEST(suite, TestRaft_single_node_commits_noop);
CuSuiteRun(suite);
CuSuiteDetails(suite, output);
......
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