ClickHouse/src/Storages/MergeTree/IExecutableTask.h

71 lines
1.9 KiB
C++
Raw Normal View History

2021-08-30 19:37:03 +00:00
#pragma once
#include <memory>
#include <functional>
2021-08-30 19:37:03 +00:00
#include <common/shared_ptr_helper.h>
2021-08-30 19:37:03 +00:00
#include <Interpreters/StorageID.h>
namespace DB
{
2021-09-08 00:21:21 +00:00
/**
* Generic interface for background operations. Simply this is self-made coroutine.
* The main method is executeStep, which will return true
* if the task wants to execute another 'step' in near future and false otherwise.
*
* Each storage assigns some operations such as merges, mutations, fetches, etc.
* We need to ask a storage or some another entity to try to assign another operation when current operation is completed.
*
* Each task corresponds to a storage, that's why there is a method getStorageID.
* This is needed to correctly shutdown a storage, e.g. we need to wait for all background operations to complete.
*/
2021-09-06 22:07:41 +00:00
class IExecutableTask
2021-08-30 19:37:03 +00:00
{
public:
2021-09-08 00:21:21 +00:00
virtual bool executeStep() = 0;
2021-08-30 19:37:03 +00:00
virtual void onCompleted() = 0;
virtual StorageID getStorageID() = 0;
2021-09-06 22:07:41 +00:00
virtual ~IExecutableTask() = default;
2021-08-30 19:37:03 +00:00
};
2021-09-06 22:07:41 +00:00
using ExecutableTaskPtr = std::shared_ptr<IExecutableTask>;
2021-08-30 19:37:03 +00:00
2021-09-08 00:21:21 +00:00
/**
* Some background operations won't represent a coroutines (don't want to be executed step-by-step). For this we have this wrapper.
*/
class ExecutableLambdaAdapter : public shared_ptr_helper<ExecutableLambdaAdapter>, public IExecutableTask
{
public:
2021-09-08 00:21:21 +00:00
template <typename Job, typename Callback>
explicit ExecutableLambdaAdapter(
Job && job_to_execute_,
Callback && job_result_callback_,
StorageID id_)
: job_to_execute(job_to_execute_)
, job_result_callback(job_result_callback_)
, id(id_) {}
2021-09-08 00:21:21 +00:00
bool executeStep() override
{
2021-09-08 00:21:21 +00:00
res = job_to_execute();
job_to_execute = {};
return false;
}
2021-09-08 00:21:21 +00:00
void onCompleted() override { job_result_callback(!res); }
StorageID getStorageID() override { return id; }
private:
bool res = false;
2021-09-08 00:21:21 +00:00
std::function<bool()> job_to_execute;
std::function<void(bool)> job_result_callback;
StorageID id;
};
2021-08-30 19:37:03 +00:00
}