2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <Common/setThreadName.h>
|
|
|
|
#include <Common/MemoryTracker.h>
|
2017-09-09 23:17:38 +00:00
|
|
|
#include <Common/randomSeed.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
2015-09-29 19:19:54 +00:00
|
|
|
#include <common/logger_useful.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Storages/MergeTree/BackgroundProcessingPool.h>
|
2018-05-29 18:14:31 +00:00
|
|
|
#include <Common/CurrentThread.h>
|
2018-03-26 14:12:07 +00:00
|
|
|
#include <Interpreters/DNSCacheUpdater.h>
|
2015-09-24 04:13:06 +00:00
|
|
|
|
2018-06-19 20:30:35 +00:00
|
|
|
#include <ext/scope_guard.h>
|
2017-09-09 23:17:38 +00:00
|
|
|
#include <pcg_random.hpp>
|
2016-05-23 00:40:28 +00:00
|
|
|
#include <random>
|
2015-09-24 04:13:06 +00:00
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
|
2015-09-24 04:13:06 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2020-06-02 15:18:36 +00:00
|
|
|
void BackgroundProcessingPoolTaskInfo::signalReadyToRun()
|
2015-09-24 04:13:06 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
Poco::Timestamp current_time;
|
|
|
|
{
|
2018-08-22 03:58:19 +00:00
|
|
|
std::unique_lock lock(pool.tasks_mutex);
|
2016-09-01 02:26:45 +00:00
|
|
|
|
2020-06-02 14:37:10 +00:00
|
|
|
/// This check ensures that the iterator is valid. Must be performed under the same mutex as invalidation.
|
2018-11-27 14:59:10 +00:00
|
|
|
if (removed)
|
|
|
|
return;
|
2016-09-01 02:26:45 +00:00
|
|
|
|
2020-06-02 14:37:10 +00:00
|
|
|
/// If this task did nothing the previous time and still should sleep, then reschedule to cancel the sleep.
|
|
|
|
const auto & scheduled_time = iterator->first;
|
|
|
|
if (scheduled_time > current_time)
|
|
|
|
pool.rescheduleTask(iterator, current_time);
|
2016-10-14 02:51:03 +00:00
|
|
|
|
2020-06-02 14:37:10 +00:00
|
|
|
/// Note that if all threads are currently busy doing their work, this call will not wakeup any thread.
|
|
|
|
pool.wake_event.notify_one();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2015-09-24 04:13:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-11 06:53:30 +00:00
|
|
|
BackgroundProcessingPool::BackgroundProcessingPool(int size_,
|
2019-12-05 08:05:07 +00:00
|
|
|
const PoolSettings & pool_settings,
|
2019-11-11 06:53:30 +00:00
|
|
|
const char * log_name,
|
|
|
|
const char * thread_name_)
|
2019-11-14 11:10:17 +00:00
|
|
|
: size(size_)
|
|
|
|
, thread_name(thread_name_)
|
2019-12-05 08:05:07 +00:00
|
|
|
, settings(pool_settings)
|
2015-09-24 04:13:06 +00:00
|
|
|
{
|
2020-06-02 14:37:10 +00:00
|
|
|
logger = &Logger::get(log_name);
|
|
|
|
LOG_INFO(logger, "Create " << log_name << " with " << size << " threads");
|
2015-09-24 04:13:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
threads.resize(size);
|
|
|
|
for (auto & thread : threads)
|
2020-06-02 14:37:10 +00:00
|
|
|
thread = ThreadFromGlobalPool([this] { workLoopFunc(); });
|
2015-09-24 04:13:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-07 03:52:50 +00:00
|
|
|
BackgroundProcessingPool::TaskHandle BackgroundProcessingPool::createTask(const Task & task)
|
2015-09-24 04:13:06 +00:00
|
|
|
{
|
2020-03-07 03:52:50 +00:00
|
|
|
return std::make_shared<TaskInfo>(*this, task);
|
|
|
|
}
|
2016-10-14 02:51:03 +00:00
|
|
|
|
2020-06-02 14:37:10 +00:00
|
|
|
void BackgroundProcessingPool::startTask(const TaskHandle & task, bool allow_execute_in_parallel)
|
2020-03-07 03:52:50 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
Poco::Timestamp current_time;
|
2015-09-24 04:13:06 +00:00
|
|
|
|
2020-06-02 14:37:10 +00:00
|
|
|
task->allow_execute_in_parallel = allow_execute_in_parallel;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-08-22 03:58:19 +00:00
|
|
|
std::unique_lock lock(tasks_mutex);
|
2020-03-07 03:52:50 +00:00
|
|
|
task->iterator = tasks.emplace(current_time, task);
|
2020-06-02 14:37:10 +00:00
|
|
|
|
|
|
|
wake_event.notify_all();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2015-09-24 04:13:06 +00:00
|
|
|
|
2020-03-07 03:52:50 +00:00
|
|
|
}
|
2015-09-24 04:13:06 +00:00
|
|
|
|
2020-03-07 03:52:50 +00:00
|
|
|
BackgroundProcessingPool::TaskHandle BackgroundProcessingPool::addTask(const Task & task)
|
|
|
|
{
|
|
|
|
TaskHandle res = createTask(task);
|
|
|
|
startTask(res);
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2015-09-24 04:13:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundProcessingPool::removeTask(const TaskHandle & task)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (task->removed.exchange(true))
|
|
|
|
return;
|
|
|
|
|
2017-07-28 17:34:02 +00:00
|
|
|
/// Wait for all executions of this task.
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-08-22 03:58:19 +00:00
|
|
|
std::unique_lock wlock(task->rwlock);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-08-22 03:58:19 +00:00
|
|
|
std::unique_lock lock(tasks_mutex);
|
2017-04-01 07:20:54 +00:00
|
|
|
tasks.erase(task->iterator);
|
2018-11-27 14:59:10 +00:00
|
|
|
/// Note that the task may be still accessible through TaskHandle (shared_ptr).
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2015-09-24 04:13:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BackgroundProcessingPool::~BackgroundProcessingPool()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
try
|
|
|
|
{
|
2020-06-02 14:37:10 +00:00
|
|
|
{
|
|
|
|
std::lock_guard lock(tasks_mutex);
|
|
|
|
shutdown = true;
|
|
|
|
wake_event.notify_all();
|
|
|
|
}
|
|
|
|
|
2019-01-14 19:22:09 +00:00
|
|
|
for (auto & thread : threads)
|
2017-04-01 07:20:54 +00:00
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
|
|
|
}
|
2015-09-24 04:13:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-02 14:37:10 +00:00
|
|
|
void BackgroundProcessingPool::workLoopFunc()
|
2015-09-24 04:13:06 +00:00
|
|
|
{
|
2019-11-14 11:10:17 +00:00
|
|
|
setThreadName(thread_name);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-08-22 03:58:19 +00:00
|
|
|
{
|
|
|
|
std::lock_guard lock(tasks_mutex);
|
|
|
|
|
|
|
|
if (thread_group)
|
|
|
|
{
|
|
|
|
/// Put all threads to one thread pool
|
|
|
|
CurrentThread::attachTo(thread_group);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CurrentThread::initializeQuery();
|
|
|
|
thread_group = CurrentThread::getGroup();
|
|
|
|
}
|
|
|
|
}
|
2018-06-19 20:30:35 +00:00
|
|
|
|
2018-08-22 03:58:19 +00:00
|
|
|
SCOPE_EXIT({ CurrentThread::detachQueryIfNotDetached(); });
|
2020-06-02 14:37:10 +00:00
|
|
|
if (const auto memory_tracker = CurrentThread::getMemoryTracker())
|
2019-12-12 22:08:54 +00:00
|
|
|
memory_tracker->setMetric(settings.memory_metric);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-09-09 23:17:38 +00:00
|
|
|
pcg64 rng(randomSeed());
|
2019-12-05 08:05:07 +00:00
|
|
|
std::this_thread::sleep_for(std::chrono::duration<double>(std::uniform_real_distribution<double>(0, settings.thread_sleep_seconds_random_part)(rng)));
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-06-02 14:37:10 +00:00
|
|
|
Poco::Timestamp scheduled_task_start_time;
|
|
|
|
|
|
|
|
while (true)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-12-26 17:03:29 +00:00
|
|
|
TaskResult task_result = TaskResult::ERROR;
|
2017-04-01 07:20:54 +00:00
|
|
|
TaskHandle task;
|
|
|
|
|
|
|
|
{
|
2020-06-02 14:37:10 +00:00
|
|
|
std::unique_lock lock(tasks_mutex);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-06-02 14:37:10 +00:00
|
|
|
while (!task && !shutdown)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2020-06-02 14:37:10 +00:00
|
|
|
for (const auto & [time, handle] : tasks)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2020-06-02 14:37:10 +00:00
|
|
|
if (!handle->removed
|
|
|
|
&& (handle->allow_execute_in_parallel || handle->concurrent_executors == 0))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2020-06-02 14:37:10 +00:00
|
|
|
task = handle;
|
|
|
|
scheduled_task_start_time = time;
|
|
|
|
++task->concurrent_executors;
|
|
|
|
break;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 14:37:10 +00:00
|
|
|
if (task)
|
|
|
|
{
|
|
|
|
Poco::Timestamp current_time;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-06-02 14:37:10 +00:00
|
|
|
if (scheduled_task_start_time <= current_time)
|
|
|
|
continue;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-06-02 14:37:10 +00:00
|
|
|
wake_event.wait_for(lock,
|
|
|
|
std::chrono::microseconds(scheduled_task_start_time - current_time
|
|
|
|
+ std::uniform_int_distribution<uint64_t>(0, settings.thread_sleep_seconds_random_part * 1000000)(rng)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wake_event.wait_for(lock,
|
|
|
|
std::chrono::duration<double>(settings.thread_sleep_seconds
|
|
|
|
+ std::uniform_real_distribution<double>(0, settings.thread_sleep_seconds_random_part)(rng)));
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 14:37:10 +00:00
|
|
|
if (shutdown)
|
|
|
|
break;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-06-02 14:37:10 +00:00
|
|
|
std::shared_lock rlock(task->rwlock);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-06-02 14:37:10 +00:00
|
|
|
if (task->removed)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
CurrentMetrics::Increment metric_increment{settings.tasks_metric};
|
|
|
|
task_result = task->task_function();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-08-22 03:58:19 +00:00
|
|
|
std::unique_lock lock(tasks_mutex);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-06-02 14:37:10 +00:00
|
|
|
if (shutdown)
|
|
|
|
break;
|
|
|
|
|
|
|
|
--task->concurrent_executors;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (task->removed)
|
|
|
|
continue;
|
|
|
|
|
2018-12-26 17:03:29 +00:00
|
|
|
if (task_result == TaskResult::SUCCESS)
|
2018-07-08 02:57:41 +00:00
|
|
|
task->count_no_work_done = 0;
|
|
|
|
else
|
|
|
|
++task->count_no_work_done;
|
|
|
|
|
|
|
|
/// If task has done work, it could be executed again immediately.
|
|
|
|
/// If not, add delay before next run.
|
|
|
|
|
|
|
|
Poco::Timestamp next_time_to_execute; /// current time
|
2018-12-26 17:03:29 +00:00
|
|
|
if (task_result == TaskResult::ERROR)
|
2018-07-08 02:57:41 +00:00
|
|
|
next_time_to_execute += 1000000 * (std::min(
|
2019-12-05 08:05:07 +00:00
|
|
|
settings.task_sleep_seconds_when_no_work_max,
|
|
|
|
settings.task_sleep_seconds_when_no_work_min * std::pow(settings.task_sleep_seconds_when_no_work_multiplier, task->count_no_work_done))
|
|
|
|
+ std::uniform_real_distribution<double>(0, settings.task_sleep_seconds_when_no_work_random_part)(rng));
|
2018-12-26 17:03:29 +00:00
|
|
|
else if (task_result == TaskResult::NOTHING_TO_DO)
|
2019-12-05 08:05:07 +00:00
|
|
|
next_time_to_execute += 1000000 * settings.thread_sleep_seconds_if_nothing_to_do;
|
2018-07-08 02:57:41 +00:00
|
|
|
|
2020-06-02 14:37:10 +00:00
|
|
|
rescheduleTask(task->iterator, next_time_to_execute);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-24 04:13:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|