2023-05-03 16:56:45 +00:00
|
|
|
#include <IO/SharedThreadPools.h>
|
|
|
|
#include <Common/CurrentMetrics.h>
|
|
|
|
#include <Common/ThreadPool.h>
|
|
|
|
#include <Core/Field.h>
|
|
|
|
|
|
|
|
namespace CurrentMetrics
|
|
|
|
{
|
|
|
|
extern const Metric IOThreads;
|
|
|
|
extern const Metric IOThreadsActive;
|
2023-11-18 18:07:59 +00:00
|
|
|
extern const Metric IOThreadsScheduled;
|
2023-05-03 16:56:45 +00:00
|
|
|
extern const Metric BackupsIOThreads;
|
|
|
|
extern const Metric BackupsIOThreadsActive;
|
2023-11-18 18:07:59 +00:00
|
|
|
extern const Metric BackupsIOThreadsScheduled;
|
2023-06-06 12:42:56 +00:00
|
|
|
extern const Metric MergeTreePartsLoaderThreads;
|
|
|
|
extern const Metric MergeTreePartsLoaderThreadsActive;
|
2023-11-18 18:07:59 +00:00
|
|
|
extern const Metric MergeTreePartsLoaderThreadsScheduled;
|
2023-06-06 12:42:56 +00:00
|
|
|
extern const Metric MergeTreePartsCleanerThreads;
|
|
|
|
extern const Metric MergeTreePartsCleanerThreadsActive;
|
2023-11-18 18:07:59 +00:00
|
|
|
extern const Metric MergeTreePartsCleanerThreadsScheduled;
|
2023-06-06 12:42:56 +00:00
|
|
|
extern const Metric MergeTreeOutdatedPartsLoaderThreads;
|
|
|
|
extern const Metric MergeTreeOutdatedPartsLoaderThreadsActive;
|
2023-11-18 18:07:59 +00:00
|
|
|
extern const Metric MergeTreeOutdatedPartsLoaderThreadsScheduled;
|
2024-05-08 13:04:16 +00:00
|
|
|
extern const Metric MergeTreeUnexpectedPartsLoaderThreads;
|
|
|
|
extern const Metric MergeTreeUnexpectedPartsLoaderThreadsActive;
|
|
|
|
extern const Metric MergeTreeUnexpectedPartsLoaderThreadsScheduled;
|
2024-07-26 17:08:07 +00:00
|
|
|
extern const Metric DatabaseCatalogThreads;
|
|
|
|
extern const Metric DatabaseCatalogThreadsActive;
|
|
|
|
extern const Metric DatabaseCatalogThreadsScheduled;
|
2024-01-31 18:58:28 +00:00
|
|
|
extern const Metric DatabaseReplicatedCreateTablesThreads;
|
|
|
|
extern const Metric DatabaseReplicatedCreateTablesThreadsActive;
|
|
|
|
extern const Metric DatabaseReplicatedCreateTablesThreadsScheduled;
|
2023-05-03 16:56:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-06 12:42:56 +00:00
|
|
|
StaticThreadPool::StaticThreadPool(
|
|
|
|
const String & name_,
|
|
|
|
CurrentMetrics::Metric threads_metric_,
|
2023-11-18 18:07:59 +00:00
|
|
|
CurrentMetrics::Metric threads_active_metric_,
|
|
|
|
CurrentMetrics::Metric threads_scheduled_metric_)
|
2023-06-06 12:42:56 +00:00
|
|
|
: name(name_)
|
|
|
|
, threads_metric(threads_metric_)
|
|
|
|
, threads_active_metric(threads_active_metric_)
|
2023-11-18 18:07:59 +00:00
|
|
|
, threads_scheduled_metric(threads_scheduled_metric_)
|
2023-06-06 12:42:56 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void StaticThreadPool::initialize(size_t max_threads, size_t max_free_threads, size_t queue_size)
|
2023-05-03 16:56:45 +00:00
|
|
|
{
|
|
|
|
if (instance)
|
2023-06-06 12:42:56 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "The {} is initialized twice", name);
|
2023-05-03 16:56:45 +00:00
|
|
|
|
2023-06-06 12:42:56 +00:00
|
|
|
/// By default enabling "turbo mode" won't affect the number of threads anyhow
|
|
|
|
max_threads_turbo = max_threads;
|
|
|
|
max_threads_normal = max_threads;
|
2023-05-03 16:56:45 +00:00
|
|
|
instance = std::make_unique<ThreadPool>(
|
2023-06-06 12:42:56 +00:00
|
|
|
threads_metric,
|
|
|
|
threads_active_metric,
|
2023-11-18 18:07:59 +00:00
|
|
|
threads_scheduled_metric,
|
2023-05-03 16:56:45 +00:00
|
|
|
max_threads,
|
|
|
|
max_free_threads,
|
|
|
|
queue_size,
|
|
|
|
/* shutdown_on_exception= */ false);
|
|
|
|
}
|
|
|
|
|
2023-06-06 12:42:56 +00:00
|
|
|
void StaticThreadPool::reloadConfiguration(size_t max_threads, size_t max_free_threads, size_t queue_size)
|
|
|
|
{
|
|
|
|
if (!instance)
|
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "The {} is not initialized", name);
|
|
|
|
|
2023-12-23 01:04:35 +00:00
|
|
|
std::lock_guard lock(mutex);
|
|
|
|
|
2023-06-06 12:42:56 +00:00
|
|
|
instance->setMaxThreads(turbo_mode_enabled > 0 ? max_threads_turbo : max_threads);
|
|
|
|
instance->setMaxFreeThreads(max_free_threads);
|
|
|
|
instance->setQueueSize(queue_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ThreadPool & StaticThreadPool::get()
|
2023-05-03 16:56:45 +00:00
|
|
|
{
|
|
|
|
if (!instance)
|
2023-06-06 12:42:56 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "The {} is not initialized", name);
|
2023-05-03 16:56:45 +00:00
|
|
|
|
|
|
|
return *instance;
|
|
|
|
}
|
|
|
|
|
2023-06-06 12:42:56 +00:00
|
|
|
void StaticThreadPool::enableTurboMode()
|
|
|
|
{
|
|
|
|
if (!instance)
|
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "The {} is not initialized", name);
|
|
|
|
|
|
|
|
std::lock_guard lock(mutex);
|
2023-05-03 16:56:45 +00:00
|
|
|
|
2023-06-06 12:42:56 +00:00
|
|
|
++turbo_mode_enabled;
|
|
|
|
if (turbo_mode_enabled == 1)
|
|
|
|
instance->setMaxThreads(max_threads_turbo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StaticThreadPool::disableTurboMode()
|
2023-05-03 16:56:45 +00:00
|
|
|
{
|
2023-06-06 12:42:56 +00:00
|
|
|
if (!instance)
|
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "The {} is not initialized", name);
|
2023-05-03 16:56:45 +00:00
|
|
|
|
2023-06-06 12:42:56 +00:00
|
|
|
std::lock_guard lock(mutex);
|
|
|
|
|
|
|
|
--turbo_mode_enabled;
|
|
|
|
if (turbo_mode_enabled == 0)
|
|
|
|
instance->setMaxThreads(max_threads_normal);
|
2023-05-03 16:56:45 +00:00
|
|
|
}
|
|
|
|
|
2023-06-06 12:42:56 +00:00
|
|
|
void StaticThreadPool::setMaxTurboThreads(size_t max_threads_turbo_)
|
2023-05-03 16:56:45 +00:00
|
|
|
{
|
|
|
|
if (!instance)
|
2023-06-06 12:42:56 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "The {} is not initialized", name);
|
2023-05-03 16:56:45 +00:00
|
|
|
|
2023-06-06 12:42:56 +00:00
|
|
|
std::lock_guard lock(mutex);
|
|
|
|
|
|
|
|
max_threads_turbo = max_threads_turbo_;
|
|
|
|
if (turbo_mode_enabled > 0)
|
|
|
|
instance->setMaxThreads(max_threads_turbo);
|
2023-05-03 16:56:45 +00:00
|
|
|
}
|
|
|
|
|
2023-06-06 12:42:56 +00:00
|
|
|
StaticThreadPool & getIOThreadPool()
|
|
|
|
{
|
2023-11-18 18:07:59 +00:00
|
|
|
static StaticThreadPool instance("IOThreadPool", CurrentMetrics::IOThreads, CurrentMetrics::IOThreadsActive, CurrentMetrics::IOThreadsScheduled);
|
2023-06-06 12:42:56 +00:00
|
|
|
return instance;
|
|
|
|
}
|
2023-05-03 16:56:45 +00:00
|
|
|
|
2023-06-06 12:42:56 +00:00
|
|
|
StaticThreadPool & getBackupsIOThreadPool()
|
2023-05-03 16:56:45 +00:00
|
|
|
{
|
2023-11-18 18:07:59 +00:00
|
|
|
static StaticThreadPool instance("BackupsIOThreadPool", CurrentMetrics::BackupsIOThreads, CurrentMetrics::BackupsIOThreadsActive, CurrentMetrics::BackupsIOThreadsScheduled);
|
2023-06-06 12:42:56 +00:00
|
|
|
return instance;
|
|
|
|
}
|
2023-05-03 16:56:45 +00:00
|
|
|
|
2023-06-06 12:42:56 +00:00
|
|
|
StaticThreadPool & getActivePartsLoadingThreadPool()
|
|
|
|
{
|
2023-11-18 19:19:24 +00:00
|
|
|
static StaticThreadPool instance("MergeTreePartsLoaderThreadPool", CurrentMetrics::MergeTreePartsLoaderThreads, CurrentMetrics::MergeTreePartsLoaderThreadsActive, CurrentMetrics::MergeTreePartsLoaderThreadsScheduled);
|
2023-06-06 12:42:56 +00:00
|
|
|
return instance;
|
2023-05-03 16:56:45 +00:00
|
|
|
}
|
|
|
|
|
2023-06-06 12:42:56 +00:00
|
|
|
StaticThreadPool & getPartsCleaningThreadPool()
|
2023-05-03 16:56:45 +00:00
|
|
|
{
|
2023-11-18 18:07:59 +00:00
|
|
|
static StaticThreadPool instance("MergeTreePartsCleanerThreadPool", CurrentMetrics::MergeTreePartsCleanerThreads, CurrentMetrics::MergeTreePartsCleanerThreadsActive, CurrentMetrics::MergeTreePartsCleanerThreadsScheduled);
|
2023-06-06 12:42:56 +00:00
|
|
|
return instance;
|
|
|
|
}
|
2023-05-03 16:56:45 +00:00
|
|
|
|
2023-06-06 12:42:56 +00:00
|
|
|
StaticThreadPool & getOutdatedPartsLoadingThreadPool()
|
|
|
|
{
|
2023-11-18 18:07:59 +00:00
|
|
|
static StaticThreadPool instance("MergeTreeOutdatedPartsLoaderThreadPool", CurrentMetrics::MergeTreeOutdatedPartsLoaderThreads, CurrentMetrics::MergeTreeOutdatedPartsLoaderThreadsActive, CurrentMetrics::MergeTreeOutdatedPartsLoaderThreadsScheduled);
|
2023-06-06 12:42:56 +00:00
|
|
|
return instance;
|
2023-05-03 16:56:45 +00:00
|
|
|
}
|
|
|
|
|
2024-05-07 21:54:49 +00:00
|
|
|
StaticThreadPool & getUnexpectedPartsLoadingThreadPool()
|
|
|
|
{
|
|
|
|
static StaticThreadPool instance("MergeTreeUnexpectedPartsLoaderThreadPool", CurrentMetrics::MergeTreeUnexpectedPartsLoaderThreads, CurrentMetrics::MergeTreeUnexpectedPartsLoaderThreadsActive, CurrentMetrics::MergeTreeUnexpectedPartsLoaderThreadsScheduled);
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2024-01-31 18:58:28 +00:00
|
|
|
StaticThreadPool & getDatabaseReplicatedCreateTablesThreadPool()
|
|
|
|
{
|
|
|
|
static StaticThreadPool instance("CreateTablesThreadPool", CurrentMetrics::DatabaseReplicatedCreateTablesThreads, CurrentMetrics::DatabaseReplicatedCreateTablesThreadsActive, CurrentMetrics::DatabaseReplicatedCreateTablesThreadsScheduled);
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2024-07-26 17:08:07 +00:00
|
|
|
/// ThreadPool used for dropping tables.
|
|
|
|
StaticThreadPool & getDatabaseCatalogDropTablesThreadPool()
|
|
|
|
{
|
|
|
|
static StaticThreadPool instance("DropTablesThreadPool", CurrentMetrics::DatabaseCatalogThreads, CurrentMetrics::DatabaseCatalogThreadsActive, CurrentMetrics::DatabaseCatalogThreadsScheduled);
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2023-05-03 16:56:45 +00:00
|
|
|
}
|