-
Notifications
You must be signed in to change notification settings - Fork 409
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
FLASH-562: Fix race condition of batch command handling #277
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
#include <Flash/BatchCommandsHandler.h> | ||
#include <Flash/CoprocessorHandler.h> | ||
#include <common/ThreadPool.h> | ||
|
||
namespace DB | ||
{ | ||
|
@@ -10,38 +9,40 @@ BatchCommandsHandler::BatchCommandsHandler(BatchCommandsContext & batch_commands | |
: batch_commands_context(batch_commands_context_), request(request_), response(response_), log(&Logger::get("BatchCommandsHandler")) | ||
{} | ||
|
||
ThreadPool::Job BatchCommandsHandler::handleCommandJob( | ||
const tikvpb::BatchCommandsRequest::Request & req, tikvpb::BatchCommandsResponse::Response & resp, grpc::Status & ret) const | ||
{ | ||
return [&]() { | ||
if (!req.has_coprocessor()) | ||
{ | ||
ret = grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, ""); | ||
return; | ||
} | ||
|
||
const auto & cop_req = req.coprocessor(); | ||
auto cop_resp = resp.mutable_coprocessor(); | ||
|
||
auto [context, status] = batch_commands_context.db_context_creation_func(&batch_commands_context.grpc_server_context); | ||
if (!status.ok()) | ||
{ | ||
ret = status; | ||
return; | ||
} | ||
|
||
CoprocessorContext cop_context(context, cop_req.context(), batch_commands_context.grpc_server_context); | ||
CoprocessorHandler cop_handler(cop_context, &cop_req, cop_resp); | ||
|
||
ret = cop_handler.execute(); | ||
}; | ||
} | ||
|
||
grpc::Status BatchCommandsHandler::execute() | ||
{ | ||
if (request.requests_size() == 0) | ||
return grpc::Status::OK; | ||
|
||
// TODO: Fill transport_layer_load into BatchCommandsResponse. | ||
|
||
auto command_handler_func | ||
= [](BatchCommandsContext::DBContextCreationFunc db_context_creation_func, grpc::ServerContext * grpc_server_context, | ||
const tikvpb::BatchCommandsRequest::Request & req, tikvpb::BatchCommandsResponse::Response & resp, grpc::Status & ret) { | ||
if (!req.has_coprocessor()) | ||
{ | ||
ret = grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, ""); | ||
return; | ||
} | ||
|
||
const auto & cop_req = req.coprocessor(); | ||
auto cop_resp = resp.mutable_coprocessor(); | ||
|
||
auto [context, status] = db_context_creation_func(grpc_server_context); | ||
if (!status.ok()) | ||
{ | ||
ret = status; | ||
return; | ||
} | ||
|
||
CoprocessorContext cop_context(context, cop_req.context(), *grpc_server_context); | ||
CoprocessorHandler cop_handler(cop_context, &cop_req, cop_resp); | ||
|
||
ret = cop_handler.execute(); | ||
}; | ||
|
||
/// Shortcut for only one request by not going to thread pool. | ||
if (request.requests_size() == 1) | ||
{ | ||
|
@@ -51,7 +52,7 @@ grpc::Status BatchCommandsHandler::execute() | |
auto resp = response.add_responses(); | ||
response.add_request_ids(request.request_ids(0)); | ||
auto ret = grpc::Status::OK; | ||
command_handler_func(batch_commands_context.db_context_creation_func, &batch_commands_context.grpc_server_context, req, *resp, ret); | ||
handleCommandJob(req, *resp, ret)(); | ||
return ret; | ||
} | ||
|
||
|
@@ -65,18 +66,16 @@ grpc::Status BatchCommandsHandler::execute() | |
|
||
ThreadPool thread_pool(max_threads); | ||
|
||
std::vector<grpc::Status> rets; | ||
std::vector<grpc::Status> rets(request.requests_size()); | ||
size_t i = 0; | ||
|
||
for (const auto & req : request.requests()) | ||
{ | ||
auto resp = response.add_responses(); | ||
response.add_request_ids(request.request_ids(i++)); | ||
rets.emplace_back(grpc::Status::OK); | ||
thread_pool.schedule([&]() { | ||
command_handler_func( | ||
batch_commands_context.db_context_creation_func, &batch_commands_context.grpc_server_context, req, *resp, rets.back()); | ||
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. The pitfall here is that, lambda will capture The same rule applies to Fix by evaluating |
||
}); | ||
|
||
thread_pool.schedule(handleCommandJob(req, *resp, rets.back())); | ||
} | ||
|
||
thread_pool.wait(); | ||
|
@@ -85,7 +84,10 @@ grpc::Status BatchCommandsHandler::execute() | |
for (const auto & ret : rets) | ||
{ | ||
if (!ret.ok()) | ||
{ | ||
response.Clear(); | ||
return ret; | ||
} | ||
} | ||
|
||
return grpc::Status::OK; | ||
|
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.
This pre-allocation is a must, otherwise the following
rets.back()
reference will become dangling after buffer re-allocation when inserting more elements.