Commit a2c2934b authored by Willem Thiart's avatar Willem Thiart
Browse files

Prevent duped appendentries response

parent 4c7cfd82
......@@ -179,6 +179,10 @@ int raft_recv_appendentries_response(raft_server_t* me_,
r->current_idx,
r->first_idx);
/* Stale response -- ignore */
if (r->current_idx <= raft_node_get_match_idx(node))
return 0;
if (!raft_is_leader(me_))
return -1;
......@@ -213,10 +217,6 @@ int raft_recv_appendentries_response(raft_server_t* me_,
assert(r->current_idx <= raft_get_current_idx(me_));
/* response to a repeat transmission -- ignore */
if (raft_node_get_match_idx(node) == r->current_idx)
return 0;
raft_node_set_next_idx(node, r->current_idx + 1);
raft_node_set_match_idx(node, r->current_idx);
......
......@@ -1883,6 +1883,68 @@ void TestRaft_leader_recv_appendentries_response_increase_commit_idx_when_majori
CuAssertIntEquals(tc, 2, raft_get_last_applied_idx(r));
}
void TestRaft_leader_recv_appendentries_response_duplicate_does_not_decrement_match_idx(
CuTest * tc)
{
raft_cbs_t funcs = {
.send_appendentries = sender_appendentries,
.log = NULL
};
msg_appendentries_response_t aer;
void *sender = sender_new(NULL);
void *r = raft_new();
raft_add_node(r, NULL, 1, 1);
raft_add_node(r, NULL, 2, 0);
raft_add_node(r, NULL, 3, 0);
raft_set_callbacks(r, &funcs, sender);
/* I'm the leader */
raft_set_state(r, RAFT_STATE_LEADER);
raft_set_current_term(r, 1);
raft_set_commit_idx(r, 0);
/* the last applied idx will became 1, and then 2 */
raft_set_last_applied_idx(r, 0);
/* append entries - we need two */
raft_entry_t ety;
ety.term = 1;
ety.id = 1;
ety.data.buf = "aaaa";
ety.data.len = 4;
raft_append_entry(r, &ety);
ety.id = 2;
raft_append_entry(r, &ety);
ety.id = 3;
raft_append_entry(r, &ety);
memset(&aer, 0, sizeof(msg_appendentries_response_t));
/* receive msg 1 */
aer.term = 1;
aer.success = 1;
aer.current_idx = 1;
aer.first_idx = 1;
raft_recv_appendentries_response(r, raft_get_node(r, 2), &aer);
CuAssertIntEquals(tc, 1, raft_node_get_match_idx(raft_get_node(r, 2)));
/* receive msg 2 */
aer.term = 1;
aer.success = 1;
aer.current_idx = 2;
aer.first_idx = 2;
raft_recv_appendentries_response(r, raft_get_node(r, 2), &aer);
CuAssertIntEquals(tc, 2, raft_node_get_match_idx(raft_get_node(r, 2)));
/* receive msg 1 - because of duplication ie. unreliable network */
aer.term = 1;
aer.success = 1;
aer.current_idx = 1;
aer.first_idx = 1;
raft_recv_appendentries_response(r, raft_get_node(r, 2), &aer);
CuAssertIntEquals(tc, 2, raft_node_get_match_idx(raft_get_node(r, 2)));
}
void TestRaft_leader_recv_appendentries_response_do_not_increase_commit_idx_because_of_old_terms_with_majority(
CuTest * tc)
{
......
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