Skip to content

Commit

Permalink
limit the queued task number and queued duration of coprocess task. (#…
Browse files Browse the repository at this point in the history
…6394) (#7739)

ref #6438, close #7747
  • Loading branch information
ti-chi-bot authored Jul 5, 2023
1 parent 6e18968 commit 72da328
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion contrib/client-c
24 changes: 24 additions & 0 deletions dbms/src/Flash/FlashService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,31 @@ grpc::Status FlashService::Coprocessor(

context->setMockStorage(mock_storage);

const auto & settings = context->getSettingsRef();
auto handle_limit = settings.cop_pool_handle_limit != 0 ? settings.cop_pool_handle_limit.get() : 10 * cop_pool->size();
auto max_queued_duration_seconds = std::min(settings.cop_pool_max_queued_seconds, 20);

if (handle_limit > 0)
{
// We use this atomic variable metrics from the prometheus-cpp library to mark the number of queued queries.
// TODO: Use grpc asynchronous server and a more fully-featured thread pool.
if (auto current = GET_METRIC(tiflash_coprocessor_handling_request_count, type_cop).Value(); current > handle_limit)
{
response->mutable_region_error()->mutable_server_is_busy()->set_reason(fmt::format("tiflash cop pool queued too much, current = {}, limit = {}", current, handle_limit));
return grpc::Status::OK;
}
}


grpc::Status ret = executeInThreadPool(*cop_pool, [&] {
if (max_queued_duration_seconds > 0)
{
if (auto current = watch.elapsedSeconds(); current > max_queued_duration_seconds)
{
response->mutable_region_error()->mutable_server_is_busy()->set_reason(fmt::format("this task queued in tiflash cop pool too long, current = {}, limit = {}", current, max_queued_duration_seconds));
return grpc::Status::OK;
}
}
auto [db_context, status] = createDBContext(grpc_context);
if (!status.ok())
{
Expand Down
2 changes: 2 additions & 0 deletions dbms/src/Interpreters/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ struct Settings
M(SettingUInt64, min_insert_block_size_bytes, (DEFAULT_INSERT_BLOCK_SIZE * 256), "Squash blocks passed to INSERT query to specified size in bytes, if blocks are not big enough.") \
M(SettingMaxThreads, max_threads, 0, "The maximum number of threads to execute the request. By default, it is determined automatically.") \
M(SettingUInt64, cop_pool_size, 0, "The number of threads to handle cop requests. By default, it is determined automatically.") \
M(SettingInt64, cop_pool_handle_limit, 0, "The maximum number of requests can be handled by cop pool, include executing and queuing tasks. More cop requests will get error \"TiFlash Server is Busy\". -1 means unlimited, 0 means determined automatically (10 times of cop-pool-size).") \
M(SettingInt64, cop_pool_max_queued_seconds, 15, "The maximum queuing duration of coprocessor task, unit is second. When task starts to run, it checks whether queued more than this config, if so, it will directly return error \"TiFlash Server is Busy\". <=0 means unlimited, default is 15. The upper limit of this config is 20.") \
M(SettingUInt64, batch_cop_pool_size, 0, "The number of threads to handle batch cop requests. By default, it is determined automatically.") \
M(SettingUInt64, max_read_buffer_size, DBMS_DEFAULT_BUFFER_SIZE, "The maximum size of the buffer to read from the filesystem.") \
M(SettingUInt64, max_distributed_connections, DEFAULT_MAX_DISTRIBUTED_CONNECTIONS, "The maximum number of connections for distributed processing of one query (should be greater than max_threads).") \
Expand Down

0 comments on commit 72da328

Please sign in to comment.