2021-10-22 12:56:09 +00:00
|
|
|
#include "OvercommitTracker.h"
|
|
|
|
|
2021-10-22 15:15:33 +00:00
|
|
|
#include <chrono>
|
2021-12-06 22:16:09 +00:00
|
|
|
#include <mutex>
|
2022-05-23 14:35:09 +00:00
|
|
|
#include <Common/ProfileEvents.h>
|
2021-10-22 12:56:09 +00:00
|
|
|
#include <Interpreters/ProcessList.h>
|
|
|
|
|
2022-05-23 14:35:09 +00:00
|
|
|
namespace ProfileEvents
|
|
|
|
{
|
|
|
|
extern const Event MemoryOvercommitWaitTimeMicroseconds;
|
|
|
|
}
|
|
|
|
|
2021-10-22 15:15:33 +00:00
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
2022-02-16 22:24:09 +00:00
|
|
|
constexpr std::chrono::microseconds ZERO_MICROSEC = 0us;
|
|
|
|
|
2022-02-16 20:02:14 +00:00
|
|
|
OvercommitTracker::OvercommitTracker(std::mutex & global_mutex_)
|
2022-05-27 16:30:29 +00:00
|
|
|
: picked_tracker(nullptr)
|
2022-05-02 22:45:13 +00:00
|
|
|
, cancellation_state(QueryCancellationState::NONE)
|
2022-02-16 20:02:14 +00:00
|
|
|
, global_mutex(global_mutex_)
|
2022-05-02 22:45:13 +00:00
|
|
|
, freed_memory(0)
|
|
|
|
, required_memory(0)
|
|
|
|
, allow_release(true)
|
2021-10-22 15:15:33 +00:00
|
|
|
{}
|
|
|
|
|
2022-06-20 17:35:24 +00:00
|
|
|
#define LOG_DEBUG_SAFE(...) \
|
2022-06-20 23:10:40 +00:00
|
|
|
do { \
|
2022-06-20 17:35:24 +00:00
|
|
|
OvercommitTrackerBlockerInThread blocker; \
|
|
|
|
LOG_DEBUG(__VA_ARGS__); \
|
2022-06-20 23:10:40 +00:00
|
|
|
} while (false)
|
2022-06-20 17:35:24 +00:00
|
|
|
|
2022-05-23 14:35:09 +00:00
|
|
|
OvercommitResult OvercommitTracker::needToStopQuery(MemoryTracker * tracker, Int64 amount)
|
2021-10-22 12:56:09 +00:00
|
|
|
{
|
2022-06-20 17:35:24 +00:00
|
|
|
if (OvercommitTrackerBlockerInThread::isBlocked())
|
|
|
|
return OvercommitResult::NONE;
|
2022-03-24 16:47:48 +00:00
|
|
|
// NOTE: Do not change the order of locks
|
2022-02-21 15:45:17 +00:00
|
|
|
//
|
|
|
|
// global_mutex must be acquired before overcommit_m, because
|
2022-05-02 22:45:13 +00:00
|
|
|
// method OvercommitTracker::onQueryStop(MemoryTracker *) is
|
2022-02-21 15:45:17 +00:00
|
|
|
// always called with already acquired global_mutex in
|
|
|
|
// ProcessListEntry::~ProcessListEntry().
|
2022-02-16 20:02:14 +00:00
|
|
|
std::unique_lock<std::mutex> global_lock(global_mutex);
|
2021-10-22 12:56:09 +00:00
|
|
|
std::unique_lock<std::mutex> lk(overcommit_m);
|
|
|
|
|
2022-05-27 16:30:29 +00:00
|
|
|
auto max_wait_time = tracker->getOvercommitWaitingTime();
|
|
|
|
|
2022-02-16 22:24:09 +00:00
|
|
|
if (max_wait_time == ZERO_MICROSEC)
|
2022-05-23 14:35:09 +00:00
|
|
|
return OvercommitResult::DISABLED;
|
2022-02-16 22:24:09 +00:00
|
|
|
|
2021-10-22 12:56:09 +00:00
|
|
|
pickQueryToExclude();
|
2022-05-02 22:45:13 +00:00
|
|
|
assert(cancellation_state != QueryCancellationState::NONE);
|
2022-02-16 20:02:14 +00:00
|
|
|
global_lock.unlock();
|
2021-12-06 18:34:52 +00:00
|
|
|
|
2022-01-18 12:21:59 +00:00
|
|
|
// If no query was chosen we need to stop current query.
|
|
|
|
// This may happen if no soft limit is set.
|
2021-12-06 18:34:52 +00:00
|
|
|
if (picked_tracker == nullptr)
|
2021-10-26 13:21:58 +00:00
|
|
|
{
|
2022-05-02 23:02:43 +00:00
|
|
|
// Here state can not be RUNNING, because it requires
|
|
|
|
// picked_tracker to be not null pointer.
|
2022-05-02 22:45:13 +00:00
|
|
|
assert(cancellation_state == QueryCancellationState::SELECTED);
|
|
|
|
cancellation_state = QueryCancellationState::NONE;
|
2022-05-23 14:35:09 +00:00
|
|
|
return OvercommitResult::DISABLED;
|
2021-10-26 13:21:58 +00:00
|
|
|
}
|
2021-12-06 18:34:52 +00:00
|
|
|
if (picked_tracker == tracker)
|
2022-05-02 22:45:13 +00:00
|
|
|
{
|
2022-05-02 23:02:43 +00:00
|
|
|
// Query of the provided as an argument memory tracker was chosen.
|
|
|
|
// It may happen even when current state is RUNNING, because
|
|
|
|
// ThreadStatus::~ThreadStatus may call MemoryTracker::alloc.
|
2022-05-02 22:45:13 +00:00
|
|
|
cancellation_state = QueryCancellationState::RUNNING;
|
2022-05-23 14:35:09 +00:00
|
|
|
return OvercommitResult::SELECTED;
|
2022-05-02 22:45:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
allow_release = true;
|
|
|
|
|
|
|
|
required_memory += amount;
|
|
|
|
required_per_thread[tracker] = amount;
|
2022-05-23 14:35:09 +00:00
|
|
|
auto wait_start_time = std::chrono::system_clock::now();
|
2022-05-02 22:45:13 +00:00
|
|
|
bool timeout = !cv.wait_for(lk, max_wait_time, [this, tracker]()
|
2021-10-22 12:56:09 +00:00
|
|
|
{
|
2022-05-02 22:45:13 +00:00
|
|
|
return required_per_thread[tracker] == 0 || cancellation_state == QueryCancellationState::NONE;
|
2021-10-22 12:56:09 +00:00
|
|
|
});
|
2022-05-23 14:35:09 +00:00
|
|
|
auto wait_end_time = std::chrono::system_clock::now();
|
|
|
|
ProfileEvents::increment(ProfileEvents::MemoryOvercommitWaitTimeMicroseconds, (wait_end_time - wait_start_time) / 1us);
|
2022-06-20 23:10:40 +00:00
|
|
|
LOG_DEBUG_SAFE(getLogger(), "Memory was{} freed within timeout", (timeout ? " not" : ""));
|
2022-05-02 22:45:13 +00:00
|
|
|
|
|
|
|
required_memory -= amount;
|
|
|
|
Int64 still_need = required_per_thread[tracker]; // If enough memory is freed it will be 0
|
|
|
|
required_per_thread.erase(tracker);
|
|
|
|
|
|
|
|
// If threads where not released since last call of this method,
|
|
|
|
// we can release them now.
|
|
|
|
if (allow_release && required_memory <= freed_memory && still_need != 0)
|
|
|
|
releaseThreads();
|
|
|
|
|
|
|
|
// All required amount of memory is free now and selected query to stop doesn't know about it.
|
|
|
|
// As we don't need to free memory, we can continue execution of the selected query.
|
|
|
|
if (required_memory == 0 && cancellation_state == QueryCancellationState::SELECTED)
|
|
|
|
reset();
|
2022-05-23 14:35:09 +00:00
|
|
|
if (timeout)
|
|
|
|
return OvercommitResult::TIMEOUTED;
|
|
|
|
if (still_need != 0)
|
|
|
|
return OvercommitResult::NOT_ENOUGH_FREED;
|
|
|
|
else
|
|
|
|
return OvercommitResult::MEMORY_FREED;
|
2022-05-02 22:45:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OvercommitTracker::tryContinueQueryExecutionAfterFree(Int64 amount)
|
|
|
|
{
|
2022-06-20 17:35:24 +00:00
|
|
|
if (OvercommitTrackerBlockerInThread::isBlocked())
|
|
|
|
return;
|
|
|
|
|
2022-05-02 22:45:13 +00:00
|
|
|
std::lock_guard guard(overcommit_m);
|
|
|
|
if (cancellation_state != QueryCancellationState::NONE)
|
|
|
|
{
|
|
|
|
freed_memory += amount;
|
|
|
|
if (freed_memory >= required_memory)
|
|
|
|
releaseThreads();
|
|
|
|
}
|
2022-03-26 18:25:26 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 22:45:13 +00:00
|
|
|
void OvercommitTracker::onQueryStop(MemoryTracker * tracker)
|
2021-10-26 13:21:58 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lk(overcommit_m);
|
2021-12-06 18:34:52 +00:00
|
|
|
if (picked_tracker == tracker)
|
2021-10-26 13:21:58 +00:00
|
|
|
{
|
2022-06-20 23:10:40 +00:00
|
|
|
LOG_DEBUG_SAFE(getLogger(), "Picked query stopped");
|
2021-12-06 18:34:52 +00:00
|
|
|
|
2022-05-02 22:45:13 +00:00
|
|
|
reset();
|
2021-12-06 18:34:52 +00:00
|
|
|
cv.notify_all();
|
2021-10-26 13:21:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-02 22:45:13 +00:00
|
|
|
void OvercommitTracker::releaseThreads()
|
|
|
|
{
|
|
|
|
for (auto & required : required_per_thread)
|
|
|
|
required.second = 0;
|
|
|
|
freed_memory = 0;
|
|
|
|
allow_release = false; // To avoid repeating call of this method in OvercommitTracker::needToStopQuery
|
|
|
|
cv.notify_all();
|
|
|
|
}
|
|
|
|
|
2022-02-16 20:02:14 +00:00
|
|
|
UserOvercommitTracker::UserOvercommitTracker(DB::ProcessList * process_list, DB::ProcessListForUser * user_process_list_)
|
|
|
|
: OvercommitTracker(process_list->mutex)
|
|
|
|
, user_process_list(user_process_list_)
|
2021-10-22 12:56:09 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
void UserOvercommitTracker::pickQueryToExcludeImpl()
|
|
|
|
{
|
2022-01-18 12:21:59 +00:00
|
|
|
MemoryTracker * query_tracker = nullptr;
|
2021-10-22 12:56:09 +00:00
|
|
|
OvercommitRatio current_ratio{0, 0};
|
2022-01-18 12:21:59 +00:00
|
|
|
// At this moment query list must be read only.
|
2022-02-21 15:45:17 +00:00
|
|
|
// This is guaranteed by locking global_mutex in OvercommitTracker::needToStopQuery.
|
2021-12-06 18:34:52 +00:00
|
|
|
auto & queries = user_process_list->queries;
|
2022-06-20 23:10:40 +00:00
|
|
|
LOG_DEBUG_SAFE(logger, "Trying to choose query to stop from {} queries", queries.size());
|
2021-12-06 18:34:52 +00:00
|
|
|
for (auto const & query : queries)
|
2021-10-22 12:56:09 +00:00
|
|
|
{
|
2021-12-06 18:34:52 +00:00
|
|
|
if (query.second->isKilled())
|
|
|
|
continue;
|
2021-12-14 13:39:53 +00:00
|
|
|
|
2021-10-22 12:56:09 +00:00
|
|
|
auto * memory_tracker = query.second->getMemoryTracker();
|
2021-12-14 13:39:53 +00:00
|
|
|
if (!memory_tracker)
|
|
|
|
continue;
|
|
|
|
|
2021-10-22 12:56:09 +00:00
|
|
|
auto ratio = memory_tracker->getOvercommitRatio();
|
2022-06-20 23:10:40 +00:00
|
|
|
LOG_DEBUG_SAFE(logger, "Query has ratio {}/{}", ratio.committed, ratio.soft_limit);
|
2021-12-06 18:34:52 +00:00
|
|
|
if (ratio.soft_limit != 0 && current_ratio < ratio)
|
2021-10-22 12:56:09 +00:00
|
|
|
{
|
2022-01-18 12:21:59 +00:00
|
|
|
query_tracker = memory_tracker;
|
2021-10-22 12:56:09 +00:00
|
|
|
current_ratio = ratio;
|
|
|
|
}
|
|
|
|
}
|
2022-06-20 17:35:24 +00:00
|
|
|
LOG_DEBUG_SAFE(logger, "Selected to stop query with overcommit ratio {}/{}",
|
2022-06-20 23:10:40 +00:00
|
|
|
current_ratio.committed, current_ratio.soft_limit);
|
2022-01-18 12:21:59 +00:00
|
|
|
picked_tracker = query_tracker;
|
2021-10-22 12:56:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 20:02:14 +00:00
|
|
|
GlobalOvercommitTracker::GlobalOvercommitTracker(DB::ProcessList * process_list_)
|
|
|
|
: OvercommitTracker(process_list_->mutex)
|
|
|
|
, process_list(process_list_)
|
|
|
|
{}
|
|
|
|
|
2021-10-22 12:56:09 +00:00
|
|
|
void GlobalOvercommitTracker::pickQueryToExcludeImpl()
|
|
|
|
{
|
2022-01-18 12:21:59 +00:00
|
|
|
MemoryTracker * query_tracker = nullptr;
|
2021-10-22 12:56:09 +00:00
|
|
|
OvercommitRatio current_ratio{0, 0};
|
2022-02-15 15:04:13 +00:00
|
|
|
// At this moment query list must be read only.
|
2022-02-21 15:45:17 +00:00
|
|
|
// This is guaranteed by locking global_mutex in OvercommitTracker::needToStopQuery.
|
2022-06-20 23:10:40 +00:00
|
|
|
LOG_DEBUG_SAFE(logger, "Trying to choose query to stop from {} queries", process_list->size());
|
2022-02-21 15:45:17 +00:00
|
|
|
for (auto const & query : process_list->processes)
|
2021-10-22 12:56:09 +00:00
|
|
|
{
|
2021-12-06 22:06:00 +00:00
|
|
|
if (query.isKilled())
|
2022-05-04 00:41:15 +00:00
|
|
|
continue;
|
2021-12-06 22:06:00 +00:00
|
|
|
|
2021-10-26 12:32:17 +00:00
|
|
|
Int64 user_soft_limit = 0;
|
|
|
|
if (auto const * user_process_list = query.getUserProcessList())
|
|
|
|
user_soft_limit = user_process_list->user_memory_tracker.getSoftLimit();
|
2021-12-06 22:06:00 +00:00
|
|
|
if (user_soft_limit == 0)
|
2022-05-04 00:41:15 +00:00
|
|
|
continue;
|
2021-10-26 12:32:17 +00:00
|
|
|
|
2021-12-06 22:06:00 +00:00
|
|
|
auto * memory_tracker = query.getMemoryTracker();
|
2021-12-14 13:39:53 +00:00
|
|
|
if (!memory_tracker)
|
2022-05-04 00:41:15 +00:00
|
|
|
continue;
|
2021-10-26 12:32:17 +00:00
|
|
|
auto ratio = memory_tracker->getOvercommitRatio(user_soft_limit);
|
2022-06-20 23:10:40 +00:00
|
|
|
LOG_DEBUG_SAFE(logger, "Query has ratio {}/{}", ratio.committed, ratio.soft_limit);
|
2021-10-22 12:56:09 +00:00
|
|
|
if (current_ratio < ratio)
|
|
|
|
{
|
2022-01-18 12:21:59 +00:00
|
|
|
query_tracker = memory_tracker;
|
2021-10-22 12:56:09 +00:00
|
|
|
current_ratio = ratio;
|
|
|
|
}
|
2022-02-21 15:45:17 +00:00
|
|
|
}
|
2022-06-20 17:35:24 +00:00
|
|
|
LOG_DEBUG_SAFE(logger, "Selected to stop query with overcommit ratio {}/{}",
|
2022-06-20 23:10:40 +00:00
|
|
|
current_ratio.committed, current_ratio.soft_limit);
|
2022-01-18 12:21:59 +00:00
|
|
|
picked_tracker = query_tracker;
|
2021-10-22 12:56:09 +00:00
|
|
|
}
|
2022-05-17 18:07:52 +00:00
|
|
|
|
|
|
|
thread_local size_t OvercommitTrackerBlockerInThread::counter = 0;
|