-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BugFix] Fix transaction stream load lock leak #53564
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
#include <deque> | ||
#include <future> | ||
#include <sstream> | ||
#include <utility> | ||
|
||
// use string iequal | ||
#include <event2/buffer.h> | ||
|
@@ -144,22 +145,14 @@ void TransactionStreamLoadAction::_send_error_reply(HttpRequest* req, const Stat | |
} | ||
|
||
void TransactionStreamLoadAction::handle(HttpRequest* req) { | ||
StreamLoadContext* ctx = nullptr; | ||
const auto& label = req->header(HTTP_LABEL_KEY); | ||
if (!req->header(HTTP_CHANNEL_ID).empty()) { | ||
int channel_id = std::stoi(req->header(HTTP_CHANNEL_ID)); | ||
ctx = _exec_env->stream_context_mgr()->get_channel_context(label, channel_id); | ||
} else { | ||
ctx = _exec_env->stream_context_mgr()->get(label); | ||
if (config::enable_stream_load_verbose_log) { | ||
LOG(INFO) << "transaction streaming load request, handle: " << req->debug_string(); | ||
} | ||
|
||
StreamLoadContext* ctx = (StreamLoadContext*)req->handler_ctx(); | ||
if (ctx == nullptr) { | ||
return; | ||
} | ||
DeferOp defer([&] { | ||
if (ctx->unref()) { | ||
delete ctx; | ||
} | ||
}); | ||
ctx->last_active_ts = MonotonicNanos(); | ||
|
||
if (!ctx->status.ok()) { | ||
|
@@ -179,14 +172,12 @@ void TransactionStreamLoadAction::handle(HttpRequest* req) { | |
} | ||
|
||
auto resp = _exec_env->transaction_mgr()->_build_reply(TXN_LOAD, ctx); | ||
ctx->lock.unlock(); | ||
|
||
_send_reply(req, resp); | ||
} | ||
|
||
int TransactionStreamLoadAction::on_header(HttpRequest* req) { | ||
if (config::enable_stream_load_verbose_log) { | ||
LOG(INFO) << "transaction streaming load request: " << req->debug_string(); | ||
LOG(INFO) << "transaction streaming load request, header: " << req->debug_string(); | ||
} | ||
|
||
const auto& label = req->header(HTTP_LABEL_KEY); | ||
|
@@ -230,6 +221,9 @@ int TransactionStreamLoadAction::on_header(HttpRequest* req) { | |
_send_error_reply(req, Status::TransactionInProcessing("Transaction in processing, please retry later")); | ||
return -1; | ||
} | ||
// referenced by the http request | ||
ctx->ref(); | ||
req->set_handler_ctx(ctx); | ||
ctx->last_active_ts = MonotonicNanos(); | ||
ctx->received_data_cost_nanos = 0; | ||
ctx->receive_bytes = 0; | ||
|
@@ -243,7 +237,6 @@ int TransactionStreamLoadAction::on_header(HttpRequest* req) { | |
(void)_exec_env->transaction_mgr()->_rollback_transaction(ctx); | ||
} | ||
auto resp = _exec_env->transaction_mgr()->_build_reply(TXN_LOAD, ctx); | ||
ctx->lock.unlock(); | ||
_send_reply(req, resp); | ||
return -1; | ||
} | ||
|
@@ -501,22 +494,10 @@ Status TransactionStreamLoadAction::_exec_plan_fragment(HttpRequest* http_req, S | |
} | ||
|
||
void TransactionStreamLoadAction::on_chunk_data(HttpRequest* req) { | ||
StreamLoadContext* ctx = nullptr; | ||
const string& label = req->header(HTTP_LABEL_KEY); | ||
if (!req->header(HTTP_CHANNEL_ID).empty()) { | ||
int channel_id = std::stoi(req->header(HTTP_CHANNEL_ID)); | ||
ctx = _exec_env->stream_context_mgr()->get_channel_context(label, channel_id); | ||
} else { | ||
ctx = _exec_env->stream_context_mgr()->get(label); | ||
} | ||
StreamLoadContext* ctx = (StreamLoadContext*)req->handler_ctx(); | ||
if (ctx == nullptr) { | ||
return; | ||
} | ||
DeferOp defer([&] { | ||
if (ctx->unref()) { | ||
delete ctx; | ||
} | ||
}); | ||
|
||
SCOPED_THREAD_LOCAL_MEM_TRACKER_SETTER(ctx->instance_mem_tracker.get()); | ||
|
||
|
@@ -584,6 +565,23 @@ void TransactionStreamLoadAction::on_chunk_data(HttpRequest* req) { | |
ctx->last_active_ts = MonotonicNanos(); | ||
ctx->received_data_cost_nanos += ctx->last_active_ts - start_read_data_time; | ||
ctx->total_received_data_cost_nanos += ctx->last_active_ts - start_read_data_time; | ||
VLOG(1) << "Receive http chunk, " << ctx->brief() << ", total expected bytes: " << ctx->body_bytes | ||
<< ", total received bytes: " << ctx->total_receive_bytes; | ||
} | ||
|
||
void TransactionStreamLoadAction::free_handler_ctx(void* param) { | ||
StreamLoadContext* ctx = (StreamLoadContext*)param; | ||
if (ctx == nullptr) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shall check nullptr against There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. static_cast can be applied to nullptr so it's safe to check after the cast |
||
return; | ||
} | ||
DCHECK(!ctx->lock.try_lock()); | ||
ctx->lock.unlock(); | ||
if (config::enable_stream_load_verbose_log) { | ||
LOG(INFO) << "free handler context, " << ctx->brief(); | ||
} | ||
if (ctx->unref()) { | ||
delete ctx; | ||
} | ||
} | ||
|
||
} // namespace starrocks | ||
banmoy marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
static_cast?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed