ClickHouse/src/Common/OvercommitTracker.cpp

105 lines
3.2 KiB
C++
Raw Normal View History

2021-10-22 12:56:09 +00:00
#include "OvercommitTracker.h"
#include <base/logger_useful.h>
2021-10-22 15:15:33 +00:00
#include <chrono>
2021-10-22 12:56:09 +00:00
#include <Interpreters/ProcessList.h>
2021-10-22 15:15:33 +00:00
using namespace std::chrono_literals;
OvercommitTracker::OvercommitTracker()
: max_wait_time(0us)
, picked_tracker(nullptr)
, cancelation_state(QueryCancelationState::NONE)
{}
void OvercommitTracker::setMaxWaitTime(UInt64 wait_time)
{
max_wait_time = wait_time * 1us;
}
2021-10-22 12:56:09 +00:00
bool OvercommitTracker::needToStopQuery(MemoryTracker * tracker)
{
std::unique_lock<std::mutex> lk(overcommit_m);
pickQueryToExclude();
assert(cancelation_state == QueryCancelationState::RUNNING);
if (picked_tracker == nullptr)
{
cancelation_state = QueryCancelationState::NONE;
2021-10-22 12:56:09 +00:00
return true;
}
if (picked_tracker == tracker)
return true;
2021-10-22 15:15:33 +00:00
return cv.wait_for(lk, max_wait_time, [this]()
2021-10-22 12:56:09 +00:00
{
return cancelation_state == QueryCancelationState::NONE;
});
}
void OvercommitTracker::unsubscribe(MemoryTracker * tracker)
{
std::unique_lock<std::mutex> lk(overcommit_m);
if (picked_tracker == tracker)
{
LOG_DEBUG(&Poco::Logger::get("OvercommitTracker"), "Picked query stopped");
picked_tracker = nullptr;
cancelation_state = QueryCancelationState::NONE;
cv.notify_all();
}
}
2021-10-22 12:56:09 +00:00
UserOvercommitTracker::UserOvercommitTracker(DB::ProcessListForUser * user_process_list_)
: user_process_list(user_process_list_)
{}
void UserOvercommitTracker::pickQueryToExcludeImpl()
{
MemoryTracker * current_tracker = nullptr;
OvercommitRatio current_ratio{0, 0};
// At this moment query list must be read only
auto & queries = user_process_list->queries;
LOG_DEBUG(&Poco::Logger::get("OvercommitTracker"),
"Trying to choose query to stop from {} queries", queries.size());
for (auto const & query : queries)
2021-10-22 12:56:09 +00:00
{
if (query.second->isKilled())
continue;
2021-10-22 12:56:09 +00:00
auto * memory_tracker = query.second->getMemoryTracker();
auto ratio = memory_tracker->getOvercommitRatio();
LOG_DEBUG(&Poco::Logger::get("OvercommitTracker"),
"Query has ratio {}/{}", ratio.committed, ratio.soft_limit);
if (ratio.soft_limit != 0 && current_ratio < ratio)
2021-10-22 12:56:09 +00:00
{
current_tracker = memory_tracker;
current_ratio = ratio;
}
}
LOG_DEBUG(&Poco::Logger::get("OvercommitTracker"),
"Selected to stop query with overcommit ratio {}/{}",
current_ratio.committed, current_ratio.soft_limit);
2021-10-22 12:56:09 +00:00
picked_tracker = current_tracker;
}
void GlobalOvercommitTracker::pickQueryToExcludeImpl()
{
MemoryTracker * current_tracker = nullptr;
OvercommitRatio current_ratio{0, 0};
process_list->processEachQueryStatus([&](DB::QueryStatus const & query)
{
auto * memory_tracker = query.getMemoryTracker();
Int64 user_soft_limit = 0;
if (auto const * user_process_list = query.getUserProcessList())
user_soft_limit = user_process_list->user_memory_tracker.getSoftLimit();
auto ratio = memory_tracker->getOvercommitRatio(user_soft_limit);
2021-10-22 12:56:09 +00:00
if (current_ratio < ratio)
{
current_tracker = memory_tracker;
current_ratio = ratio;
}
});
picked_tracker = current_tracker;
}