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
|
|
|
|
2021-09-08 12:42:03 +00:00
|
|
|
/// Settings for background tasks scheduling. Each background assignee has one
|
2020-10-16 10:12:31 +00:00
|
|
|
/// BackgroundSchedulingPoolTask and depending on execution result may put this
|
|
|
|
/// task to sleep according to settings. Look at scheduleTask function for details.
|
2021-09-08 12:42:03 +00:00
|
|
|
struct BackgroundTaskSchedulingSettings
|
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
|
|
|
|
2021-09-02 21:31:32 +00:00
|
|
|
class MergeTreeData;
|
2021-02-11 11:46:18 +00:00
|
|
|
|
2021-09-08 00:21:21 +00:00
|
|
|
class BackgroundJobsAssignee : public 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-09-08 12:42:03 +00:00
|
|
|
BackgroundTaskSchedulingSettings 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.
|
2021-11-09 12:26:51 +00:00
|
|
|
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-09-08 12:42:03 +00:00
|
|
|
/// In case of ReplicatedMergeTree the first assignee will be responsible for
|
|
|
|
/// polling the replication queue and schedule operations according to the LogEntry type
|
|
|
|
/// e.g. merges, mutations and fetches. The same will be for Plain MergeTree except there is no
|
|
|
|
/// replication queue, so we will just scan parts and decide what to do.
|
|
|
|
/// Moving operations are the same for all types of MergeTree and also have their own timetable.
|
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
|
|
|
|
2022-06-01 19:09:53 +00:00
|
|
|
bool scheduleMergeMutateTask(ExecutableTaskPtr merge_task);
|
2021-08-30 19:37:03 +00:00
|
|
|
void scheduleFetchTask(ExecutableTaskPtr fetch_task);
|
|
|
|
void scheduleMoveTask(ExecutableTaskPtr move_task);
|
2021-11-09 12:26:51 +00:00
|
|
|
void scheduleCommonTask(ExecutableTaskPtr common_task, bool need_trigger);
|
2021-06-21 13:36:21 +00:00
|
|
|
|
2020-10-16 10:12:31 +00:00
|
|
|
/// Just call finish
|
2021-09-22 11:52:47 +00:00
|
|
|
~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,
|
2021-04-10 23:33:54 +00:00
|
|
|
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
|
2021-09-08 00:21:21 +00:00
|
|
|
void threadFunc();
|
2020-10-14 14:56:42 +00:00
|
|
|
};
|
|
|
|
|
2021-08-30 19:37:03 +00:00
|
|
|
|
2020-10-13 14:25:42 +00:00
|
|
|
}
|