Slightly better thread pool

This commit is contained in:
Nikolay Degterinsky 2022-12-14 12:23:08 +00:00
parent fef57b4e3b
commit b2f3b236d8

View File

@ -153,45 +153,36 @@ Pipe StorageSystemReplicas::read(
MutableColumns res_columns = storage_snapshot->metadata->getSampleBlock().cloneEmptyColumns();
size_t tables_size = col_database->size();
size_t thread_pool_size = std::min(tables_size, static_cast<size_t>(getNumberOfPhysicalCPUCores()));
std::vector<StorageReplicatedMergeTree::Status> statuses(tables_size);
size_t thread_pool_size = std::min(tables_size, static_cast<size_t>(getNumberOfPhysicalCPUCores()));
auto settings = context->getSettingsRef();
if (settings.max_threads != 0)
thread_pool_size = std::min(thread_pool_size, static_cast<size_t>(settings.max_threads));
ThreadPool thread_pool(thread_pool_size);
std::atomic<bool> error_flag = false;
Exception exception;
std::vector<StorageReplicatedMergeTree::Status> statuses(tables_size);
for (size_t i = 0; i < tables_size; ++i)
{
thread_pool.scheduleOrThrowOnError([&, i=i]
try
{
try
thread_pool.scheduleOrThrowOnError([&, i=i]
{
dynamic_cast<StorageReplicatedMergeTree &>(
*replicated_tables
[(*col_database)[i].safeGet<const String &>()]
[(*col_table)[i].safeGet<const String &>()]).getStatus(statuses[i], with_zk_fields);
}
catch (...)
{
tryLogCurrentException("system.replicas", "Failed to fetch system.replicas data");
/// We capture one of the exceptions to be thrown later
if (!error_flag.exchange(true))
exception = Exception(getCurrentExceptionCode(), getCurrentExceptionMessage(false));
}
});
});
}
catch (...)
{
thread_pool.wait();
throw;
}
}
thread_pool.wait();
if (error_flag)
throw exception;
for (const auto & status: statuses)
{
size_t col_num = 3;