ClickHouse/src/Storages/MergeTree/BackgroundJobsExecutor.h

118 lines
2.5 KiB
C++
Raw Normal View History

2020-10-13 14:25:42 +00:00
#pragma once
#include <Storages/MergeTree/MergeTreeData.h>
#include <Common/ThreadPool.h>
#include <Core/BackgroundSchedulePool.h>
2020-10-14 12:32:35 +00:00
#include <pcg_random.hpp>
2020-10-13 14:25:42 +00:00
2020-10-14 12:44:10 +00:00
namespace CurrentMetrics
{
extern const Metric BackgroundPoolTask;
2020-10-14 14:56:42 +00:00
extern const Metric BackgroundMovePoolTask;
2020-10-14 12:44:10 +00:00
}
2020-10-13 14:25:42 +00:00
namespace DB
{
2020-10-14 12:44:10 +00:00
2020-10-14 14:56:42 +00:00
struct TaskSleepSettings
2020-10-14 12:44:10 +00:00
{
double thread_sleep_seconds = 10;
double thread_sleep_seconds_random_part = 1.0;
double thread_sleep_seconds_if_nothing_to_do = 0.1;
/// For exponential backoff.
double task_sleep_seconds_when_no_work_min = 10;
double task_sleep_seconds_when_no_work_max = 600;
double task_sleep_seconds_when_no_work_multiplier = 1.1;
2020-10-14 14:56:42 +00:00
2020-10-14 12:44:10 +00:00
double task_sleep_seconds_when_no_work_random_part = 1.0;
2020-10-14 14:56:42 +00:00
};
2020-10-14 12:44:10 +00:00
2020-10-14 14:56:42 +00:00
enum PoolType
{
MERGE_MUTATE,
FETCH,
MOVE,
LOW_PRIORITY,
};
2020-10-14 12:44:10 +00:00
2020-10-14 14:56:42 +00:00
struct PoolConfig
{
PoolType pool_type;
size_t max_pool_size;
CurrentMetrics::Metric tasks_metric;
2020-10-14 12:44:10 +00:00
};
2020-10-13 14:25:42 +00:00
2020-10-14 14:56:42 +00:00
struct JobAndPool
2020-10-13 14:25:42 +00:00
{
2020-10-14 14:56:42 +00:00
ThreadPool::Job job;
PoolType pool_type;
};
class IBackgroundJobExecutor
{
2020-10-14 07:22:48 +00:00
Context & global_context;
2020-10-14 14:56:42 +00:00
private:
String task_name;
TaskSleepSettings sleep_settings;
2020-10-14 12:32:35 +00:00
pcg64 rng;
2020-10-13 14:25:42 +00:00
2020-10-14 14:56:42 +00:00
std::atomic<size_t> errors_count{0};
std::unordered_map<PoolType, ThreadPool> pools;
std::unordered_map<PoolType, PoolConfig> pools_configs;
2020-10-13 14:25:42 +00:00
2020-10-14 14:56:42 +00:00
BackgroundSchedulePool::TaskHolder scheduling_task;
2020-10-15 10:41:36 +00:00
std::mutex scheduling_task_mutex;
2020-10-13 14:25:42 +00:00
public:
void start();
2020-10-15 07:43:50 +00:00
void triggerTask();
2020-10-13 14:25:42 +00:00
void finish();
2020-10-14 14:56:42 +00:00
virtual ~IBackgroundJobExecutor();
protected:
2020-10-15 10:22:02 +00:00
IBackgroundJobExecutor(
Context & global_context_,
const TaskSleepSettings & sleep_settings_,
const std::vector<PoolConfig> & pools_configs_);
virtual String getBackgroundJobName() const = 0;
2020-10-14 14:56:42 +00:00
virtual std::optional<JobAndPool> getBackgroundJob() = 0;
2020-10-15 10:22:02 +00:00
2020-10-14 14:56:42 +00:00
private:
void jobExecutingTask();
void scheduleTask(bool nothing_to_do);
2020-10-13 14:25:42 +00:00
};
2020-10-14 14:56:42 +00:00
class BackgroundJobsExecutor final : public IBackgroundJobExecutor
{
2020-10-15 10:22:02 +00:00
private:
MergeTreeData & data;
2020-10-14 14:56:42 +00:00
public:
BackgroundJobsExecutor(
MergeTreeData & data_,
Context & global_context_);
protected:
2020-10-15 10:22:02 +00:00
String getBackgroundJobName() const override;
2020-10-14 14:56:42 +00:00
std::optional<JobAndPool> getBackgroundJob() override;
};
class BackgroundMovesExecutor final : public IBackgroundJobExecutor
{
2020-10-15 10:22:02 +00:00
private:
MergeTreeData & data;
2020-10-14 14:56:42 +00:00
public:
BackgroundMovesExecutor(
MergeTreeData & data_,
Context & global_context_);
protected:
2020-10-15 10:22:02 +00:00
String getBackgroundJobName() const override;
2020-10-14 14:56:42 +00:00
std::optional<JobAndPool> getBackgroundJob() override;
};
2020-10-13 14:25:42 +00:00
}