ClickHouse/src/Storages/MergeTree/MergeMutateExecutor.cpp

126 lines
2.8 KiB
C++
Raw Normal View History

2021-08-30 19:37:03 +00:00
#include <Storages/MergeTree/MergeMutateExecutor.h>
#include <Storages/MergeTree/BackgroundJobsExecutor.h>
namespace DB
{
2021-08-31 12:09:35 +00:00
2021-08-31 11:02:39 +00:00
void MergeTreeBackgroundExecutor::removeTasksCorrespondingToStorage(StorageID id)
{
std::lock_guard remove_lock(remove_mutex);
2021-08-31 23:20:23 +00:00
std::vector<ItemPtr> tasks_to_wait;
2021-08-31 11:02:39 +00:00
{
2021-08-31 23:20:23 +00:00
std::lock_guard lock(mutex);
/// Mark this StorageID as deleting
currently_deleting.emplace(id);
std::erase_if(pending, [&] (auto item) -> bool { return item->task->getStorageID() == id; });
/// Find pending to wait
for (auto & item : active)
if (item->task->getStorageID() == id)
tasks_to_wait.emplace_back(item);
2021-08-31 11:02:39 +00:00
}
2021-08-31 23:20:23 +00:00
for (auto & item : tasks_to_wait)
2021-08-31 11:02:39 +00:00
{
2021-08-31 23:20:23 +00:00
assert(item->future.valid());
item->future.wait();
}
2021-08-31 11:02:39 +00:00
2021-08-31 23:20:23 +00:00
{
std::lock_guard lock(mutex);
currently_deleting.erase(id);
2021-08-31 11:02:39 +00:00
}
}
2021-08-30 19:37:03 +00:00
void MergeTreeBackgroundExecutor::schedulerThreadFunction()
{
while (true)
{
2021-08-31 23:20:23 +00:00
std::unique_lock lock(mutex);
2021-08-30 19:37:03 +00:00
2021-08-31 23:20:23 +00:00
has_tasks.wait(lock, [this](){ return !pending.empty() || shutdown_suspend; });
2021-08-30 19:37:03 +00:00
2021-08-31 23:20:23 +00:00
if (shutdown_suspend)
break;
2021-08-30 19:37:03 +00:00
2021-08-31 23:20:23 +00:00
auto item = std::move(pending.front());
pending.pop_front();
2021-08-30 19:37:03 +00:00
2021-08-31 23:20:23 +00:00
active.emplace(item);
/// This is needed to increase / decrease the number of threads at runtime
updatePoolConfiguration();
2021-08-31 11:02:39 +00:00
2021-08-31 18:07:24 +00:00
bool res = pool.trySchedule([this, item] ()
2021-08-31 11:02:39 +00:00
{
2021-08-31 23:20:23 +00:00
auto check_if_deleting = [&] () -> bool
2021-08-31 11:02:39 +00:00
{
2021-08-31 23:20:23 +00:00
active.erase(item);
for (auto & id : currently_deleting)
2021-08-31 11:02:39 +00:00
{
2021-08-31 23:20:23 +00:00
if (item->task->getStorageID() == id)
{
item->promise.set_value();
return true;
}
2021-08-31 11:02:39 +00:00
}
2021-08-31 23:20:23 +00:00
return false;
2021-08-31 11:02:39 +00:00
};
2021-08-31 23:20:23 +00:00
SCOPE_EXIT({
std::lock_guard guard(mutex);
check_if_deleting();
});
2021-08-31 11:02:39 +00:00
2021-08-30 19:37:03 +00:00
try
{
2021-08-31 23:20:23 +00:00
if (item->task->execute())
2021-08-30 19:37:03 +00:00
{
std::lock_guard guard(mutex);
2021-08-31 23:20:23 +00:00
if (check_if_deleting())
return;
pending.emplace_back(item);
2021-08-30 19:37:03 +00:00
has_tasks.notify_one();
return;
}
2021-08-31 18:07:24 +00:00
item->task->onCompleted();
2021-08-30 19:37:03 +00:00
std::lock_guard guard(mutex);
has_tasks.notify_one();
}
catch(...)
{
2021-08-31 18:07:24 +00:00
item->task->onCompleted();
2021-08-30 19:37:03 +00:00
std::lock_guard guard(mutex);
has_tasks.notify_one();
tryLogCurrentException(__PRETTY_FUNCTION__);
}
2021-08-31 23:20:23 +00:00
2021-08-30 19:37:03 +00:00
});
if (!res)
{
2021-08-31 23:20:23 +00:00
active.erase(item);
pending.emplace_back(item);
2021-08-30 19:37:03 +00:00
}
2021-08-31 23:20:23 +00:00
2021-08-30 19:37:03 +00:00
}
}
}