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>
|
2022-12-14 16:56:19 +00:00
|
|
|
#include <Common/CurrentMetrics.h>
|
2021-10-22 12:56:09 +00:00
|
|
|
#include <Interpreters/ProcessList.h>
|
|
|
|
|
2022-12-14 16:56:19 +00:00
|
|
|
namespace CurrentMetrics
|
|
|
|
{
|
|
|
|
extern const Metric ThreadsInOvercommitTracker;
|
|
|
|
}
|
2022-10-17 02:21:08 +00:00
|
|
|
|
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-08-29 18:24:35 +00:00
|
|
|
OvercommitTracker::OvercommitTracker(DB::ProcessList * process_list_)
|
2022-05-27 16:30:29 +00:00
|
|
|
: picked_tracker(nullptr)
|
2022-08-29 18:24:35 +00:00
|
|
|
, process_list(process_list_)
|
2022-05-02 22:45:13 +00:00
|
|
|
, cancellation_state(QueryCancellationState::NONE)
|
|
|
|
, freed_memory(0)
|
|
|
|
, required_memory(0)
|
2022-06-21 10:15:33 +00:00
|
|
|
, next_id(0)
|
|
|
|
, id_to_release(0)
|
2022-05-02 22:45:13 +00:00
|
|
|
, allow_release(true)
|
2021-10-22 15:15:33 +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-21 12:26:13 +00:00
|
|
|
DENY_ALLOCATIONS_IN_SCOPE;
|
|
|
|
|
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
|
|
|
//
|
2022-08-29 18:24:35 +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-08-29 18:24:35 +00:00
|
|
|
// always called with already acquired global mutex in
|
2022-02-21 15:45:17 +00:00
|
|
|
// ProcessListEntry::~ProcessListEntry().
|
2022-08-29 18:24:35 +00:00
|
|
|
auto global_lock = process_list->unsafeLock();
|
2021-10-22 12:56:09 +00:00
|
|
|
std::unique_lock<std::mutex> lk(overcommit_m);
|
|
|
|
|
2022-06-21 10:15:33 +00:00
|
|
|
size_t id = next_id++;
|
|
|
|
|
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;
|
2022-05-23 14:35:09 +00:00
|
|
|
auto wait_start_time = std::chrono::system_clock::now();
|
2022-12-14 16:56:19 +00:00
|
|
|
bool timeout;
|
2021-10-22 12:56:09 +00:00
|
|
|
{
|
2022-12-14 16:56:19 +00:00
|
|
|
CurrentMetrics::Increment metric_increment(CurrentMetrics::ThreadsInOvercommitTracker);
|
|
|
|
timeout = !cv.wait_for(lk, max_wait_time, [this, id]()
|
|
|
|
{
|
|
|
|
return id < id_to_release || cancellation_state == QueryCancellationState::NONE;
|
|
|
|
});
|
|
|
|
}
|
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-05-02 22:45:13 +00:00
|
|
|
|
|
|
|
required_memory -= amount;
|
2022-06-21 10:15:33 +00:00
|
|
|
bool still_need = !(id < id_to_release); // True if thread wasn't released
|
2022-05-02 22:45:13 +00:00
|
|
|
|
|
|
|
// If threads where not released since last call of this method,
|
|
|
|
// we can release them now.
|
2022-06-21 10:15:33 +00:00
|
|
|
if (allow_release && required_memory <= freed_memory && still_need)
|
2022-05-02 22:45:13 +00:00
|
|
|
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;
|
2022-06-21 10:15:33 +00:00
|
|
|
if (still_need)
|
2022-05-23 14:35:09 +00:00
|
|
|
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-21 12:26:13 +00:00
|
|
|
DENY_ALLOCATIONS_IN_SCOPE;
|
|
|
|
|
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
|
|
|
{
|
2022-06-21 12:26:13 +00:00
|
|
|
DENY_ALLOCATIONS_IN_SCOPE;
|
|
|
|
|
2022-06-28 19:19:06 +00:00
|
|
|
std::lock_guard lk(overcommit_m);
|
2021-12-06 18:34:52 +00:00
|
|
|
if (picked_tracker == tracker)
|
2021-10-26 13:21:58 +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()
|
|
|
|
{
|
2022-06-21 10:15:33 +00:00
|
|
|
id_to_release = next_id;
|
2022-05-02 22:45:13 +00:00
|
|
|
freed_memory = 0;
|
|
|
|
allow_release = false; // To avoid repeating call of this method in OvercommitTracker::needToStopQuery
|
|
|
|
cv.notify_all();
|
|
|
|
}
|
|
|
|
|
2022-08-29 18:24:35 +00:00
|
|
|
UserOvercommitTracker::UserOvercommitTracker(DB::ProcessList * process_list_, DB::ProcessListForUser * user_process_list_)
|
|
|
|
: OvercommitTracker(process_list_)
|
2022-02-16 20:02:14 +00:00
|
|
|
, 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;
|
|
|
|
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();
|
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-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_)
|
2022-08-29 18:24:35 +00:00
|
|
|
: OvercommitTracker(process_list_)
|
2022-10-17 02:21:08 +00:00
|
|
|
{
|
|
|
|
}
|
2022-02-16 20:02:14 +00:00
|
|
|
|
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.
|
|
|
|
for (auto const & query : process_list->processes)
|
2021-10-22 12:56:09 +00:00
|
|
|
{
|
2022-10-17 02:21:08 +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;
|
2022-10-17 02:21:08 +00:00
|
|
|
if (auto const * user_process_list = query->getUserProcessList())
|
2021-10-26 12:32:17 +00:00
|
|
|
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
|
|
|
|
2022-10-17 02:21:08 +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);
|
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-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;
|