Skip to content

Commit

Permalink
Merge pull request #97 from yossigo/fix/ae-failure-handling
Browse files Browse the repository at this point in the history
AE error response not properly handled.
  • Loading branch information
willemt committed Oct 6, 2018
2 parents 0e75563 + f8236e0 commit 4ddc273
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/raft_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,7 @@ int raft_recv_appendentries_response(raft_server_t* me_,
raft_index_t next_idx = raft_node_get_next_idx(node);
assert(0 < next_idx);
/* Stale response -- ignore */
assert(match_idx <= next_idx - 1);
if (match_idx == next_idx - 1)
if (r->current_idx < match_idx)
return 0;
if (r->current_idx < next_idx - 1)
raft_node_set_next_idx(node, min(r->current_idx + 1, raft_get_current_idx(me_)));
Expand Down
80 changes: 80 additions & 0 deletions tests/test_snapshotting.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ static int __raft_send_appendentries_capture(raft_server_t* raft,
return 0;
}

static int __raft_send_snapshot_increment(raft_server_t* raft,
void* udata,
raft_node_t* node)
{
int *counter = udata;

(*counter)++;
return 0;
}

/* static raft_cbs_t generic_funcs = { */
/* .persist_term = __raft_persist_term, */
/* .persist_vote = __raft_persist_vote, */
Expand Down Expand Up @@ -735,3 +745,73 @@ void TestRaft_cancel_snapshot_restores_state(CuTest* tc)
CuAssertIntEquals(tc, 2, raft_get_snapshot_last_idx(r));
}

void TestRaft_leader_sends_snapshot_if_log_was_compacted(CuTest* tc)
{
raft_cbs_t funcs = {
.send_snapshot = __raft_send_snapshot_increment,
.send_appendentries = __raft_send_appendentries
};

int send_snapshot_count = 0;

void *r = raft_new();
raft_set_callbacks(r, &funcs, &send_snapshot_count);

raft_node_t* node;
raft_add_node(r, NULL, 1, 1);
node = raft_add_node(r, NULL, 2, 0);
raft_add_node(r, NULL, 3, 0);

raft_set_state(r, RAFT_STATE_LEADER);
raft_set_current_term(r, 1);

/* entry 1 */
char *str = "aaa";
raft_entry_t ety = {};
ety.term = 1;
ety.id = 1;
ety.data.buf = str;
ety.data.len = 3;
raft_append_entry(r, &ety);

/* entry 2 */
ety.term = 1;
ety.id = 2;
ety.data.buf = str;
ety.data.len = 3;
raft_append_entry(r, &ety);

/* entry 3 */
ety.term = 1;
ety.id = 3;
ety.data.buf = str;
ety.data.len = 3;
raft_append_entry(r, &ety);
CuAssertIntEquals(tc, 3, raft_get_current_idx(r));

/* compact entry 1 & 2 */
raft_set_commit_idx(r, 2);
CuAssertIntEquals(tc, 0, raft_begin_snapshot(r, 0));
CuAssertIntEquals(tc, 0, raft_end_snapshot(r));
CuAssertIntEquals(tc, 1, raft_get_log_count(r));

/* at this point a snapshot should have been sent; we'll continue
* assuming the node was not available to get it.
*/
CuAssertIntEquals(tc, 2, send_snapshot_count);
send_snapshot_count = 0;

/* node wants an entry that was compacted */
raft_node_set_match_idx(node, 1);
raft_node_set_next_idx(node, 2);

msg_appendentries_response_t aer;
aer.term = 1;
aer.success = 0;
aer.current_idx = 1;
aer.first_idx = 4;

raft_recv_appendentries_response(r, node, &aer);
CuAssertIntEquals(tc, 1, send_snapshot_count);
}

0 comments on commit 4ddc273

Please sign in to comment.