2017-12-29 22:32:04 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Poco/Notification.h>
|
|
|
|
#include <Poco/NotificationQueue.h>
|
|
|
|
#include <Poco/Timestamp.h>
|
|
|
|
#include <thread>
|
|
|
|
#include <atomic>
|
|
|
|
#include <mutex>
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
#include <functional>
|
|
|
|
#include <boost/noncopyable.hpp>
|
2018-03-22 12:34:42 +00:00
|
|
|
#include <Common/ZooKeeper/Types.h>
|
2018-06-19 20:30:35 +00:00
|
|
|
#include <Common/CurrentThread.h>
|
2019-01-14 19:22:09 +00:00
|
|
|
#include <Common/ThreadPool.h>
|
|
|
|
|
2017-12-29 22:32:04 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
class TaskNotification;
|
2019-04-06 01:09:15 +00:00
|
|
|
class BackgroundSchedulePoolTaskInfo;
|
|
|
|
class BackgroundSchedulePoolTaskHolder;
|
2017-12-29 22:32:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
/** Executes functions scheduled at a specific point in time.
|
|
|
|
* Basically all tasks are added in a queue and precessed by worker threads.
|
|
|
|
*
|
|
|
|
* The most important difference between this and BackgroundProcessingPool
|
|
|
|
* is that we have the guarantee that the same function is not executed from many workers in the same time.
|
|
|
|
*
|
|
|
|
* The usage scenario: instead starting a separate thread for each task,
|
|
|
|
* register a task in BackgroundSchedulePool and when you need to run the task,
|
|
|
|
* call schedule or scheduleAfter(duration) method.
|
|
|
|
*/
|
|
|
|
class BackgroundSchedulePool
|
|
|
|
{
|
|
|
|
public:
|
2019-04-06 01:09:15 +00:00
|
|
|
friend class BackgroundSchedulePoolTaskInfo;
|
|
|
|
|
|
|
|
using TaskInfo = BackgroundSchedulePoolTaskInfo;
|
2018-05-31 13:05:05 +00:00
|
|
|
using TaskInfoPtr = std::shared_ptr<TaskInfo>;
|
|
|
|
using TaskFunc = std::function<void()>;
|
2019-04-06 01:09:15 +00:00
|
|
|
using TaskHolder = BackgroundSchedulePoolTaskHolder;
|
2018-05-31 13:05:05 +00:00
|
|
|
using DelayedTasks = std::multimap<Poco::Timestamp, TaskInfoPtr>;
|
2017-12-29 22:32:04 +00:00
|
|
|
|
2018-05-31 13:05:05 +00:00
|
|
|
TaskHolder createTask(const std::string & log_name, const TaskFunc & function);
|
2017-12-29 22:32:04 +00:00
|
|
|
|
|
|
|
size_t getNumberOfThreads() const { return size; }
|
|
|
|
|
2019-08-03 11:02:40 +00:00
|
|
|
BackgroundSchedulePool(size_t size_);
|
2018-05-31 13:05:05 +00:00
|
|
|
~BackgroundSchedulePool();
|
|
|
|
|
2017-12-29 22:32:04 +00:00
|
|
|
private:
|
2019-01-14 19:22:09 +00:00
|
|
|
using Threads = std::vector<ThreadFromGlobalPool>;
|
2017-12-29 22:32:04 +00:00
|
|
|
|
|
|
|
void threadFunction();
|
|
|
|
void delayExecutionThreadFunction();
|
|
|
|
|
|
|
|
/// Schedule task for execution after specified delay from now.
|
2018-05-31 13:05:05 +00:00
|
|
|
void scheduleDelayedTask(const TaskInfoPtr & task_info, size_t ms, std::lock_guard<std::mutex> & task_schedule_mutex_lock);
|
2017-12-29 22:32:04 +00:00
|
|
|
|
|
|
|
/// Remove task, that was scheduled with delay, from schedule.
|
2018-05-31 13:05:05 +00:00
|
|
|
void cancelDelayedTask(const TaskInfoPtr & task_info, std::lock_guard<std::mutex> & task_schedule_mutex_lock);
|
2017-12-29 22:32:04 +00:00
|
|
|
|
|
|
|
/// Number for worker threads.
|
|
|
|
const size_t size;
|
|
|
|
std::atomic<bool> shutdown {false};
|
|
|
|
Threads threads;
|
|
|
|
Poco::NotificationQueue queue;
|
|
|
|
|
|
|
|
/// Delayed notifications.
|
|
|
|
|
2018-03-22 10:31:05 +00:00
|
|
|
std::condition_variable wakeup_cond;
|
2018-05-31 13:05:05 +00:00
|
|
|
std::mutex delayed_tasks_mutex;
|
2017-12-29 22:32:04 +00:00
|
|
|
/// Thread waiting for next delayed task.
|
2019-01-14 19:22:09 +00:00
|
|
|
ThreadFromGlobalPool delayed_thread;
|
2017-12-29 22:32:04 +00:00
|
|
|
/// Tasks ordered by scheduled time.
|
2018-05-31 13:05:05 +00:00
|
|
|
DelayedTasks delayed_tasks;
|
2018-06-19 20:30:35 +00:00
|
|
|
|
|
|
|
/// Thread group used for profiling purposes
|
|
|
|
ThreadGroupStatusPtr thread_group;
|
2018-08-22 04:04:39 +00:00
|
|
|
|
|
|
|
void attachToThreadGroup();
|
2017-12-29 22:32:04 +00:00
|
|
|
};
|
|
|
|
|
2019-04-06 01:09:15 +00:00
|
|
|
|
|
|
|
class BackgroundSchedulePoolTaskInfo : public std::enable_shared_from_this<BackgroundSchedulePoolTaskInfo>, private boost::noncopyable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BackgroundSchedulePoolTaskInfo(BackgroundSchedulePool & pool_, const std::string & log_name_, const BackgroundSchedulePool::TaskFunc & function_);
|
|
|
|
|
|
|
|
/// Schedule for execution as soon as possible (if not already scheduled).
|
|
|
|
/// If the task was already scheduled with delay, the delay will be ignored.
|
|
|
|
bool schedule();
|
|
|
|
|
|
|
|
/// Schedule for execution after specified delay.
|
|
|
|
bool scheduleAfter(size_t ms);
|
|
|
|
|
|
|
|
/// Further attempts to schedule become no-op. Will wait till the end of the current execution of the task.
|
|
|
|
void deactivate();
|
|
|
|
|
|
|
|
void activate();
|
|
|
|
|
|
|
|
/// Atomically activate task and schedule it for execution.
|
|
|
|
bool activateAndSchedule();
|
|
|
|
|
|
|
|
/// get Coordination::WatchCallback needed for notifications from ZooKeeper watches.
|
|
|
|
Coordination::WatchCallback getWatchCallback();
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class TaskNotification;
|
|
|
|
friend class BackgroundSchedulePool;
|
|
|
|
|
|
|
|
void execute();
|
|
|
|
|
|
|
|
void scheduleImpl(std::lock_guard<std::mutex> & schedule_mutex_lock);
|
|
|
|
|
|
|
|
BackgroundSchedulePool & pool;
|
|
|
|
std::string log_name;
|
|
|
|
BackgroundSchedulePool::TaskFunc function;
|
|
|
|
|
|
|
|
std::mutex exec_mutex;
|
|
|
|
std::mutex schedule_mutex;
|
|
|
|
|
|
|
|
/// Invariants:
|
|
|
|
/// * If deactivated is true then scheduled, delayed and executing are all false.
|
|
|
|
/// * scheduled and delayed cannot be true at the same time.
|
|
|
|
bool deactivated = false;
|
|
|
|
bool scheduled = false;
|
|
|
|
bool delayed = false;
|
|
|
|
bool executing = false;
|
|
|
|
|
|
|
|
/// If the task is scheduled with delay, points to element of delayed_tasks.
|
|
|
|
BackgroundSchedulePool::DelayedTasks::iterator iterator;
|
|
|
|
};
|
|
|
|
|
|
|
|
using BackgroundSchedulePoolTaskInfoPtr = std::shared_ptr<BackgroundSchedulePoolTaskInfo>;
|
|
|
|
|
|
|
|
|
|
|
|
class BackgroundSchedulePoolTaskHolder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BackgroundSchedulePoolTaskHolder() = default;
|
|
|
|
explicit BackgroundSchedulePoolTaskHolder(const BackgroundSchedulePoolTaskInfoPtr & task_info_) : task_info(task_info_) {}
|
|
|
|
BackgroundSchedulePoolTaskHolder(const BackgroundSchedulePoolTaskHolder & other) = delete;
|
|
|
|
BackgroundSchedulePoolTaskHolder(BackgroundSchedulePoolTaskHolder && other) noexcept = default;
|
|
|
|
BackgroundSchedulePoolTaskHolder & operator=(const BackgroundSchedulePoolTaskHolder & other) noexcept = delete;
|
|
|
|
BackgroundSchedulePoolTaskHolder & operator=(BackgroundSchedulePoolTaskHolder && other) noexcept = default;
|
|
|
|
|
|
|
|
~BackgroundSchedulePoolTaskHolder()
|
|
|
|
{
|
|
|
|
if (task_info)
|
|
|
|
task_info->deactivate();
|
|
|
|
}
|
|
|
|
|
|
|
|
BackgroundSchedulePoolTaskInfo * operator->() { return task_info.get(); }
|
|
|
|
const BackgroundSchedulePoolTaskInfo * operator->() const { return task_info.get(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
BackgroundSchedulePoolTaskInfoPtr task_info;
|
|
|
|
};
|
|
|
|
|
2017-12-29 22:32:04 +00:00
|
|
|
}
|