Skip to content

Commit 1707a75

Browse files
authored
Merge pull request #36 from ClickHouse-Extras/fix_handle_commit_batch
Fix bug which lead to memory leak
2 parents c2043aa + 7c6c696 commit 1707a75

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/handle_commit.cxx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,14 +315,27 @@ void raft_server::commit_app_log(ulong idx_to_commit,
315315
std::list< ptr<commit_ret_elem> > async_elems;
316316
if (need_to_handle_commit_elem) {
317317
std::unique_lock<std::mutex> cre_lock(commit_ret_elems_lock_);
318-
bool match_found = false;
318+
/// Sometimes user can batch requests to RAFT: for example send 30
319+
/// append entries requests in a single batch. For such request batch
320+
/// user will receive a single response: all was successful or all
321+
/// failed. Obviously we don't need to add info about responses
322+
/// (commit_ret_elems) for 29 requests from batch and need to do it only
323+
/// for 30-th request. precommit_index is exact value which identify ID
324+
/// of the last request from the latest batch. So if we commiting this
325+
/// last request and for some reason it was not added into
326+
/// commit_ret_elems in the handle_cli_req method (logical race
327+
/// condition) we have to add it here. Otherwise we don't need to add
328+
/// anything into commit_ret_elems_, because nobody will wait for the
329+
/// responses of the intermediate requests from requests batch.
330+
bool need_to_wait_commit_ret = sm_idx == pc_idx;
331+
319332
auto entry = commit_ret_elems_.find(sm_idx);
320333
if (entry != commit_ret_elems_.end()) {
321334
ptr<commit_ret_elem> elem = entry->second;
322335
if (elem->idx_ == sm_idx) {
323336
elem->result_code_ = cmd_result_code::OK;
324337
elem->ret_value_ = ret_value;
325-
match_found = true;
338+
need_to_wait_commit_ret = false;
326339
p_dv("notify cb %ld %p", sm_idx, &elem->awaiter_);
327340

328341
switch (ctx_->get_params()->return_method_) {
@@ -341,7 +354,7 @@ void raft_server::commit_app_log(ulong idx_to_commit,
341354
}
342355
}
343356

344-
if (!match_found && !initial_commit_exec) {
357+
if (need_to_wait_commit_ret && !initial_commit_exec) {
345358
// If not found, commit thread is invoked earlier than user thread.
346359
// Create one here.
347360
ptr<commit_ret_elem> elem = cs_new<commit_ret_elem>();

0 commit comments

Comments
 (0)