diff --git a/src/Common/MemoryTracker.cpp b/src/Common/MemoryTracker.cpp index 61aa470e2d4..4c0e6fa7503 100644 --- a/src/Common/MemoryTracker.cpp +++ b/src/Common/MemoryTracker.cpp @@ -365,6 +365,12 @@ OvercommitRatio MemoryTracker::getOvercommitRatio() } +OvercommitRatio MemoryTracker::getOvercommitRatio(Int64 limit) +{ + return { amount.load(std::memory_order_relaxed), limit }; +} + + void MemoryTracker::resetCounters() { amount.store(0, std::memory_order_relaxed); diff --git a/src/Common/MemoryTracker.h b/src/Common/MemoryTracker.h index e055a347617..7602a15e2a4 100644 --- a/src/Common/MemoryTracker.h +++ b/src/Common/MemoryTracker.h @@ -114,6 +114,11 @@ public: void setSoftLimit(Int64 value); void setHardLimit(Int64 value); + Int64 getSoftLimit() const + { + return soft_limit.load(std::memory_order_relaxed); + } + /** Set limit if it was not set. * Otherwise, set limit to new value, if new value is greater than previous limit. */ @@ -164,6 +169,7 @@ public: } OvercommitRatio getOvercommitRatio(); + OvercommitRatio getOvercommitRatio(Int64 limit); void setOvercommitTracker(OvercommitTracker * tracker) noexcept { diff --git a/src/Common/OvercommitTracker.cpp b/src/Common/OvercommitTracker.cpp index ae7720b878c..56b7f40ef6f 100644 --- a/src/Common/OvercommitTracker.cpp +++ b/src/Common/OvercommitTracker.cpp @@ -39,7 +39,7 @@ void UserOvercommitTracker::pickQueryToExcludeImpl() { MemoryTracker * current_tracker = nullptr; OvercommitRatio current_ratio{0, 0}; - //TODO: ensure this container is not being modified + // At this moment query list must be read only for (auto const & query : user_process_list->queries) { auto * memory_tracker = query.second->getMemoryTracker(); @@ -61,7 +61,11 @@ void GlobalOvercommitTracker::pickQueryToExcludeImpl() process_list->processEachQueryStatus([&](DB::QueryStatus const & query) { auto * memory_tracker = query.getMemoryTracker(); - auto ratio = memory_tracker->getOvercommitRatio(); + 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); if (current_ratio < ratio) { current_tracker = memory_tracker; diff --git a/src/Interpreters/ProcessList.h b/src/Interpreters/ProcessList.h index e5a2e4f280d..328467ee83b 100644 --- a/src/Interpreters/ProcessList.h +++ b/src/Interpreters/ProcessList.h @@ -80,6 +80,7 @@ protected: friend class ThreadStatus; friend class CurrentThread; friend class ProcessListEntry; + friend struct ::GlobalOvercommitTracker; String query; ClientInfo client_info;