ClickHouse/src/Common/OvercommitTracker.h

135 lines
3.9 KiB
C++
Raw Normal View History

2021-10-22 12:56:09 +00:00
#pragma once
2022-04-27 15:05:45 +00:00
#include <Common/logger_useful.h>
2021-10-22 12:56:09 +00:00
#include <base/types.h>
#include <boost/core/noncopyable.hpp>
2022-01-18 12:21:59 +00:00
#include <Poco/Logger.h>
2021-10-22 12:56:09 +00:00
#include <cassert>
2021-10-22 15:15:33 +00:00
#include <chrono>
2021-10-22 12:56:09 +00:00
#include <condition_variable>
#include <mutex>
#include <unordered_map>
2022-01-18 12:21:59 +00:00
// This struct is used for the comparison of query memory usage.
2021-10-22 12:56:09 +00:00
struct OvercommitRatio
{
2021-11-09 13:40:23 +00:00
OvercommitRatio(Int64 committed_, Int64 soft_limit_)
: committed(committed_)
2021-10-22 12:56:09 +00:00
, soft_limit(soft_limit_)
{}
2022-01-18 12:21:59 +00:00
friend bool operator<(OvercommitRatio const & lhs, OvercommitRatio const & rhs) noexcept
2021-10-22 12:56:09 +00:00
{
2021-10-26 13:55:57 +00:00
// (a / b < c / d) <=> (a * d < c * b)
return (lhs.committed * rhs.soft_limit) < (rhs.committed * lhs.soft_limit)
|| (lhs.soft_limit == 0 && rhs.soft_limit > 0)
|| (lhs.committed == 0 && rhs.committed == 0 && lhs.soft_limit > rhs.soft_limit);
2021-10-22 12:56:09 +00:00
}
2022-01-18 12:21:59 +00:00
// actual query memory usage
2021-11-09 13:40:23 +00:00
Int64 committed;
2022-01-18 12:21:59 +00:00
// guaranteed amount of memory query can use
2021-10-22 12:56:09 +00:00
Int64 soft_limit;
};
class MemoryTracker;
2022-01-18 12:21:59 +00:00
// Usually it's hard to set some reasonable hard memory limit
// (especially, the default value). This class introduces new
// mechanisim for the limiting of memory usage.
// Soft limit represents guaranteed amount of memory query/user
// may use. It's allowed to exceed this limit. But if hard limit
// is reached, query with the biggest overcommit ratio
// is killed to free memory.
2021-10-22 12:56:09 +00:00
struct OvercommitTracker : boost::noncopyable
{
2021-10-22 15:15:33 +00:00
void setMaxWaitTime(UInt64 wait_time);
2021-10-22 12:56:09 +00:00
bool needToStopQuery(MemoryTracker * tracker);
2021-10-22 12:56:09 +00:00
void unsubscribe(MemoryTracker * tracker);
2021-10-22 12:56:09 +00:00
virtual ~OvercommitTracker() = default;
protected:
2022-02-21 15:45:17 +00:00
explicit OvercommitTracker(std::mutex & global_mutex_);
2021-10-22 12:56:09 +00:00
virtual void pickQueryToExcludeImpl() = 0;
2022-02-21 15:45:17 +00:00
// This mutex is used to disallow concurrent access
// to picked_tracker and cancelation_state variables.
mutable std::mutex overcommit_m;
mutable std::condition_variable cv;
2021-10-22 12:56:09 +00:00
2021-10-22 15:15:33 +00:00
std::chrono::microseconds max_wait_time;
enum class QueryCancelationState
{
NONE,
RUNNING,
};
2021-11-09 13:40:23 +00:00
// Specifies memory tracker of the chosen to stop query.
// If soft limit is not set, all the queries which reach hard limit must stop.
// This case is represented as picked tracker pointer is set to nullptr and
// overcommit tracker is in RUNNING state.
2021-10-22 12:56:09 +00:00
MemoryTracker * picked_tracker;
QueryCancelationState cancelation_state;
2021-10-22 12:56:09 +00:00
2022-01-18 12:21:59 +00:00
virtual Poco::Logger * getLogger() = 0;
2021-10-22 12:56:09 +00:00
private:
void pickQueryToExclude()
{
if (cancelation_state != QueryCancelationState::RUNNING)
2021-10-22 12:56:09 +00:00
{
pickQueryToExcludeImpl();
cancelation_state = QueryCancelationState::RUNNING;
2021-10-22 12:56:09 +00:00
}
}
2022-02-21 15:45:17 +00:00
// Global mutex which is used in ProcessList to synchronize
// insertion and deletion of queries.
// OvercommitTracker::pickQueryToExcludeImpl() implementations
// require this mutex to be locked, because they read list (or sublist)
// of queries.
2022-02-16 20:02:14 +00:00
std::mutex & global_mutex;
2021-10-22 12:56:09 +00:00
};
namespace DB
{
class ProcessList;
struct ProcessListForUser;
}
struct UserOvercommitTracker : OvercommitTracker
{
2022-02-16 20:02:14 +00:00
explicit UserOvercommitTracker(DB::ProcessList * process_list, DB::ProcessListForUser * user_process_list_);
2021-10-22 12:56:09 +00:00
~UserOvercommitTracker() override = default;
protected:
void pickQueryToExcludeImpl() override final;
2021-10-22 12:56:09 +00:00
2022-01-18 12:21:59 +00:00
Poco::Logger * getLogger() override final { return logger; }
2021-10-22 12:56:09 +00:00
private:
DB::ProcessListForUser * user_process_list;
2021-12-06 22:06:00 +00:00
Poco::Logger * logger = &Poco::Logger::get("UserOvercommitTracker");
2021-10-22 12:56:09 +00:00
};
struct GlobalOvercommitTracker : OvercommitTracker
{
2022-02-16 20:02:14 +00:00
explicit GlobalOvercommitTracker(DB::ProcessList * process_list_);
2021-10-22 12:56:09 +00:00
~GlobalOvercommitTracker() override = default;
protected:
void pickQueryToExcludeImpl() override final;
2021-10-22 12:56:09 +00:00
2022-01-18 12:21:59 +00:00
Poco::Logger * getLogger() override final { return logger; }
2021-10-22 12:56:09 +00:00
private:
DB::ProcessList * process_list;
2021-12-06 22:06:00 +00:00
Poco::Logger * logger = &Poco::Logger::get("GlobalOvercommitTracker");
2021-10-22 12:56:09 +00:00
};