ClickHouse/src/Storages/MergeTree/BackgroundJobsAssignee.h

85 lines
2.2 KiB
C++
Raw Normal View History

2020-10-13 14:25:42 +00:00
#pragma once
2021-09-06 12:01:16 +00:00
#include <Storages/MergeTree/MergeTreeBackgroundExecutor.h>
2020-10-13 14:25:42 +00:00
#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
2020-10-13 14:25:42 +00:00
namespace DB
{
2020-10-14 12:44:10 +00:00
2020-10-16 10:12:31 +00:00
/// Settings for background tasks scheduling. Each background executor has one
/// BackgroundSchedulingPoolTask and depending on execution result may put this
/// task to sleep according to settings. Look at scheduleTask function for details.
2021-08-30 19:37:03 +00:00
struct ExecutableTaskSchedulingSettings
2020-10-14 12:44:10 +00:00
{
double thread_sleep_seconds_random_part = 1.0;
double thread_sleep_seconds_if_nothing_to_do = 0.1;
2020-10-16 10:12:31 +00:00
double task_sleep_seconds_when_no_work_max = 600;
2020-10-20 13:12:25 +00:00
/// For exponential backoff.
2020-10-16 10:12:31 +00:00
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-20 13:12:25 +00:00
2020-11-12 23:27:18 +00:00
/// Deprecated settings, don't affect background execution
2020-10-20 13:12:25 +00:00
double thread_sleep_seconds = 10;
double task_sleep_seconds_when_no_work_min = 10;
2020-10-14 14:56:42 +00:00
};
2020-10-14 12:44:10 +00:00
class MergeTreeData;
2021-09-06 12:01:16 +00:00
class BackgroundJobsAssignee : protected WithContext
2020-10-13 14:25:42 +00:00
{
2020-10-14 14:56:42 +00:00
private:
2021-08-30 19:37:03 +00:00
MergeTreeData & data;
2020-10-16 10:12:31 +00:00
/// Settings for execution control of background scheduling task
2021-08-30 19:37:03 +00:00
ExecutableTaskSchedulingSettings sleep_settings;
2020-10-16 10:12:31 +00:00
/// Useful for random backoff timeouts generation
2020-10-14 12:32:35 +00:00
pcg64 rng;
2020-10-13 14:25:42 +00:00
2020-10-16 10:12:31 +00:00
/// How many times execution of background job failed or we have
/// no new jobs.
2020-10-15 13:57:50 +00:00
std::atomic<size_t> no_work_done_count{0};
2020-10-14 14:56:42 +00:00
2020-10-16 10:12:31 +00:00
/// Scheduling task which assign jobs in background pool
2021-08-30 19:37:03 +00:00
BackgroundSchedulePool::TaskHolder holder;
2020-10-16 10:12:31 +00:00
/// Mutex for thread safety
2021-08-30 19:37:03 +00:00
std::mutex holder_mutex;
2020-10-13 14:25:42 +00:00
public:
2021-08-30 19:37:03 +00:00
enum class Type
{
DataProcessing,
Moving
};
Type type{Type::DataProcessing};
2020-10-16 10:12:31 +00:00
2020-10-13 14:25:42 +00:00
void start();
2021-08-30 19:37:03 +00:00
void trigger();
void postpone();
2020-10-13 14:25:42 +00:00
void finish();
2020-10-14 14:56:42 +00:00
2021-08-30 19:37:03 +00:00
void scheduleMergeMutateTask(ExecutableTaskPtr merge_task);
void scheduleFetchTask(ExecutableTaskPtr fetch_task);
void scheduleMoveTask(ExecutableTaskPtr move_task);
2021-06-21 13:36:21 +00:00
2020-10-16 10:12:31 +00:00
/// Just call finish
2021-09-06 12:01:16 +00:00
virtual ~BackgroundJobsAssignee();
2020-10-15 10:22:02 +00:00
2021-09-06 12:01:16 +00:00
BackgroundJobsAssignee(
2020-10-14 14:56:42 +00:00
MergeTreeData & data_,
2021-08-30 19:37:03 +00:00
Type type,
ContextPtr global_context_);
2020-10-14 14:56:42 +00:00
2020-10-15 10:22:02 +00:00
private:
2021-08-30 19:37:03 +00:00
static String toString(Type type);
2020-10-14 14:56:42 +00:00
2021-08-30 19:37:03 +00:00
/// Function that executes in background scheduling pool
void main();
2020-10-14 14:56:42 +00:00
};
2021-08-30 19:37:03 +00:00
2020-10-13 14:25:42 +00:00
}