Unverified Commit 40100cfa authored by Ozan Tezcan's avatar Ozan Tezcan Committed by GitHub
Browse files

Throttle operations early not to block server (#108)

Throttle operations early not to block server

Introduced throttling in #106. If execution of operations take as much as request-timeout, it means this server will not generate responses on time for the existing requests. This is causing extra elections. I've changed deadline as 'now + request_timeout / 2', giving server some room for other operations and enough time to send responses.
parent 846127fe
...@@ -2234,8 +2234,11 @@ int raft_pending_operations(raft_server_t *me) ...@@ -2234,8 +2234,11 @@ int raft_pending_operations(raft_server_t *me)
int raft_exec_operations(raft_server_t *me) int raft_exec_operations(raft_server_t *me)
{ {
/* Operations should take less than `request-timeout`. If we block here more
* than request-timeout, it means this server won't generate responses on
* time for the existing requests. */
me->exec_deadline = raft_time_millis(me) + (me->request_timeout / 2);
me->pending_operations = 0; me->pending_operations = 0;
me->exec_deadline = raft_time_millis(me) + me->request_timeout;
int e = raft_apply_all(me); int e = raft_apply_all(me);
if (e != 0) { if (e != 0) {
......
...@@ -4776,10 +4776,12 @@ void TestRaft_recv_appendentries_does_not_change_next_idx(CuTest *tc) ...@@ -4776,10 +4776,12 @@ void TestRaft_recv_appendentries_does_not_change_next_idx(CuTest *tc)
CuAssertIntEquals(tc, 21, raft_node_get_next_msgid(node)); CuAssertIntEquals(tc, 21, raft_node_get_next_msgid(node));
} }
#define OPERATION_DURATION 10000 /* milliseconds */
raft_time_t timestamp(raft_server_t *raft, void *user_data) raft_time_t timestamp(raft_server_t *raft, void *user_data)
{ {
raft_time_t *time = user_data; raft_time_t *time = user_data;
*time += 10000; *time += OPERATION_DURATION;
return *time; return *time;
} }
...@@ -4800,10 +4802,22 @@ void TestRaft_apply_entry_timeout(CuTest *tc) ...@@ -4800,10 +4802,22 @@ void TestRaft_apply_entry_timeout(CuTest *tc)
__RAFT_APPEND_ENTRIES_SEQ_ID(r, 21, 0, 1, ""); __RAFT_APPEND_ENTRIES_SEQ_ID(r, 21, 0, 1, "");
raft_set_commit_idx(r, 21); raft_set_commit_idx(r, 21);
/* Each execution iteration will apply 5 entries as we throttle when we
* reach 'RAFT_CONFIG_REQUEST_TIMEOUT / 2'.
* So, each iteration is '100 / 2 = 50 milliseconds'.
* Each operation is 10 milliseconds. */
raft_exec_operations(r);
CuAssertIntEquals(tc, 1, raft_pending_operations(r));
CuAssertIntEquals(tc, 5, raft_get_last_applied_idx(r));
raft_exec_operations(r); raft_exec_operations(r);
CuAssertIntEquals(tc, 1, raft_pending_operations(r)); CuAssertIntEquals(tc, 1, raft_pending_operations(r));
CuAssertIntEquals(tc, 10, raft_get_last_applied_idx(r)); CuAssertIntEquals(tc, 10, raft_get_last_applied_idx(r));
raft_exec_operations(r);
CuAssertIntEquals(tc, 1, raft_pending_operations(r));
CuAssertIntEquals(tc, 15, raft_get_last_applied_idx(r));
raft_exec_operations(r); raft_exec_operations(r);
CuAssertIntEquals(tc, 1, raft_pending_operations(r)); CuAssertIntEquals(tc, 1, raft_pending_operations(r));
CuAssertIntEquals(tc, 20, raft_get_last_applied_idx(r)); CuAssertIntEquals(tc, 20, raft_get_last_applied_idx(r));
...@@ -4845,10 +4859,22 @@ void TestRaft_apply_read_request_timeout(CuTest *tc) ...@@ -4845,10 +4859,22 @@ void TestRaft_apply_read_request_timeout(CuTest *tc)
raft_recv_read_request(r, read_request, &remaining); raft_recv_read_request(r, read_request, &remaining);
} }
/* Each execution iteration will execute 5 operations as we throttle when we
* reach 'RAFT_CONFIG_REQUEST_TIMEOUT / 2'.
* So, each iteration is '100 / 2 = 50 milliseconds'.
* Each operation is 10 milliseconds. */
raft_exec_operations(r);
CuAssertIntEquals(tc, 1, raft_pending_operations(r));
CuAssertIntEquals(tc, 15, remaining);
raft_exec_operations(r); raft_exec_operations(r);
CuAssertIntEquals(tc, 1, raft_pending_operations(r)); CuAssertIntEquals(tc, 1, raft_pending_operations(r));
CuAssertIntEquals(tc, 10, remaining); CuAssertIntEquals(tc, 10, remaining);
raft_exec_operations(r);
CuAssertIntEquals(tc, 1, raft_pending_operations(r));
CuAssertIntEquals(tc, 5, remaining);
raft_exec_operations(r); raft_exec_operations(r);
CuAssertIntEquals(tc, 0, raft_pending_operations(r)); CuAssertIntEquals(tc, 0, raft_pending_operations(r));
CuAssertIntEquals(tc, 0, remaining); CuAssertIntEquals(tc, 0, remaining);
......
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