ClickHouse/src/Storages/MergeTree/MergeTreeBackgroundExecutor.cpp

209 lines
5.5 KiB
C++
Raw Normal View History

2021-09-06 12:01:16 +00:00
#include <Storages/MergeTree/MergeTreeBackgroundExecutor.h>
2021-08-30 19:37:03 +00:00
2021-09-07 12:45:39 +00:00
#include <algorithm>
#include <Common/setThreadName.h>
#include <Common/Exception.h>
2021-09-06 12:01:16 +00:00
#include <Storages/MergeTree/BackgroundJobsAssignee.h>
2021-08-30 19:37:03 +00:00
2021-08-30 19:37:03 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int ABORTED;
}
template <class Queue>
void MergeTreeBackgroundExecutor<Queue>::wait()
2021-09-03 22:15:20 +00:00
{
{
std::lock_guard lock(mutex);
shutdown = true;
has_tasks.notify_all();
}
2021-09-07 12:45:39 +00:00
pool.wait();
2021-09-03 22:15:20 +00:00
}
template <class Queue>
bool MergeTreeBackgroundExecutor<Queue>::trySchedule(ExecutableTaskPtr task)
2021-09-03 22:15:20 +00:00
{
std::lock_guard lock(mutex);
if (shutdown)
return false;
auto & value = CurrentMetrics::values[metric];
if (value.load() >= static_cast<int64_t>(max_tasks_count))
return false;
pending.push(std::make_shared<TaskRuntimeData>(std::move(task), metric));
2021-09-03 22:15:20 +00:00
has_tasks.notify_one();
return true;
}
template <class Queue>
void MergeTreeBackgroundExecutor<Queue>::removeTasksCorrespondingToStorage(StorageID id)
2021-08-31 11:02:39 +00:00
{
2021-09-08 00:21:21 +00:00
std::vector<TaskRuntimeDataPtr> tasks_to_wait;
2021-08-31 11:02:39 +00:00
{
2021-08-31 23:20:23 +00:00
std::lock_guard lock(mutex);
2021-09-02 18:49:37 +00:00
/// Erase storage related tasks from pending and select active tasks to wait for
pending.remove(id);
2021-09-03 22:15:20 +00:00
/// Copy items to wait for their completion
std::copy_if(active.begin(), active.end(), std::back_inserter(tasks_to_wait),
[&] (auto item) -> bool { return item->task->getStorageID() == id; });
2021-09-07 12:45:39 +00:00
for (auto & item : tasks_to_wait)
item->is_currently_deleting = true;
2021-08-31 11:02:39 +00:00
}
/// Wait for each task to be executed
2021-09-03 13:02:49 +00:00
for (auto & item : tasks_to_wait)
{
2021-09-03 13:02:49 +00:00
item->is_done.wait();
item.reset();
}
2021-08-31 11:02:39 +00:00
}
2021-08-30 19:37:03 +00:00
template <class Queue>
void MergeTreeBackgroundExecutor<Queue>::routine(TaskRuntimeDataPtr item)
2021-09-03 12:27:49 +00:00
{
2021-09-08 12:42:03 +00:00
DENY_ALLOCATIONS_IN_SCOPE;
/// All operations with queues are considered no to do any allocations
2021-09-06 22:07:41 +00:00
auto erase_from_active = [this, item]
2021-09-03 12:27:49 +00:00
{
2021-09-03 22:15:20 +00:00
active.erase(std::remove(active.begin(), active.end(), item), active.end());
2021-09-03 12:27:49 +00:00
};
2021-09-08 12:42:03 +00:00
bool need_execute_again = false;
2021-09-03 12:27:49 +00:00
try
{
2021-09-08 12:42:03 +00:00
ALLOW_ALLOCATIONS_IN_SCOPE;
need_execute_again = item->task->executeStep();
}
catch (const Exception & e)
{
if (e.code() == ErrorCodes::ABORTED) /// Cancelled merging parts is not an error - log as info.
LOG_INFO(log, fmt::runtime(getCurrentExceptionMessage(false)));
else
tryLogCurrentException(__PRETTY_FUNCTION__);
}
2021-09-08 12:42:03 +00:00
catch (...)
{
tryLogCurrentException(__PRETTY_FUNCTION__);
}
2021-09-03 12:27:49 +00:00
2021-09-08 12:42:03 +00:00
if (need_execute_again)
{
std::lock_guard guard(mutex);
2021-09-03 12:27:49 +00:00
2021-09-08 12:42:03 +00:00
if (item->is_currently_deleting)
2021-09-08 00:21:21 +00:00
{
erase_from_active();
/// This is significant to order the destructors.
item->task.reset();
2021-09-22 11:52:47 +00:00
item->is_done.set();
item = nullptr;
2021-09-08 12:42:03 +00:00
return;
2021-09-08 00:21:21 +00:00
}
/// After the `guard` destruction `item` has to be in moved from state
/// Not to own the object it points to.
/// Otherwise the destruction of the task won't be ordered with the destruction of the
/// storage.
pending.push(std::move(item));
2021-09-08 12:42:03 +00:00
erase_from_active();
has_tasks.notify_one();
item = nullptr;
2021-09-08 12:42:03 +00:00
return;
}
{
std::lock_guard guard(mutex);
erase_from_active();
has_tasks.notify_one();
try
{
ALLOW_ALLOCATIONS_IN_SCOPE;
/// In a situation of a lack of memory this method can throw an exception,
/// because it may interact somehow with BackgroundSchedulePool, which may allocate memory
/// But it is rather safe, because we have try...catch block here, and another one in ThreadPool.
item->task->onCompleted();
}
catch (const Exception & e)
{
if (e.code() == ErrorCodes::ABORTED) /// Cancelled merging parts is not an error - log as info.
LOG_INFO(log, fmt::runtime(getCurrentExceptionMessage(false)));
else
tryLogCurrentException(__PRETTY_FUNCTION__);
}
catch (...)
{
tryLogCurrentException(__PRETTY_FUNCTION__);
}
/// We have to call reset() under a lock, otherwise a race is possible.
/// Imagine, that task is finally completed (last execution returned false),
/// we removed the task from both queues, but still have pointer.
/// The thread that shutdowns storage will scan queues in order to find some tasks to wait for, but will find nothing.
/// So, the destructor of a task and the destructor of a storage will be executed concurrently.
2021-09-06 22:07:41 +00:00
item->task.reset();
2021-09-22 11:52:47 +00:00
item->is_done.set();
item = nullptr;
2021-09-03 12:27:49 +00:00
}
}
template <class Queue>
void MergeTreeBackgroundExecutor<Queue>::threadFunction()
2021-08-30 19:37:03 +00:00
{
2021-09-07 13:13:23 +00:00
setThreadName(name.c_str());
2021-09-08 12:42:03 +00:00
DENY_ALLOCATIONS_IN_SCOPE;
2021-08-30 19:37:03 +00:00
while (true)
{
2021-09-08 12:42:03 +00:00
try
2021-09-07 13:13:23 +00:00
{
2021-09-08 12:42:03 +00:00
TaskRuntimeDataPtr item;
{
std::unique_lock lock(mutex);
has_tasks.wait(lock, [this](){ return !pending.empty() || shutdown; });
2021-08-30 19:37:03 +00:00
2021-09-08 12:42:03 +00:00
if (shutdown)
break;
2021-08-30 19:37:03 +00:00
item = std::move(pending.pop());
2021-09-08 12:42:03 +00:00
active.push_back(item);
}
2021-08-31 23:20:23 +00:00
2021-09-22 11:52:47 +00:00
routine(std::move(item));
2021-09-08 12:42:03 +00:00
}
catch (...)
{
tryLogCurrentException(__PRETTY_FUNCTION__);
}
2021-08-30 19:37:03 +00:00
}
}
template class MergeTreeBackgroundExecutor<MergeMutateRuntimeQueue>;
template class MergeTreeBackgroundExecutor<OrdinaryRuntimeQueue>;
2021-08-30 19:37:03 +00:00
}