ClickHouse/dbms/Access/EnabledQuota.h

100 lines
3.7 KiB
C++
Raw Normal View History

2019-11-04 19:17:27 +00:00
#pragma once
#include <Access/Quota.h>
#include <Core/UUID.h>
#include <Poco/Net/IPAddress.h>
#include <boost/noncopyable.hpp>
#include <boost/smart_ptr/atomic_shared_ptr.hpp>
2019-11-04 19:17:27 +00:00
#include <atomic>
#include <chrono>
#include <memory>
namespace DB
{
struct QuotaUsageInfo;
/// Instances of `EnabledQuota` are used to track resource consumption.
class EnabledQuota : public boost::noncopyable
2019-11-04 19:17:27 +00:00
{
public:
struct Params
{
UUID user_id;
String user_name;
std::vector<UUID> enabled_roles;
Poco::Net::IPAddress client_address;
String client_key;
auto toTuple() const { return std::tie(user_id, enabled_roles, user_name, client_address, client_key); }
friend bool operator ==(const Params & lhs, const Params & rhs) { return lhs.toTuple() == rhs.toTuple(); }
friend bool operator !=(const Params & lhs, const Params & rhs) { return !(lhs == rhs); }
friend bool operator <(const Params & lhs, const Params & rhs) { return lhs.toTuple() < rhs.toTuple(); }
friend bool operator >(const Params & lhs, const Params & rhs) { return rhs < lhs; }
friend bool operator <=(const Params & lhs, const Params & rhs) { return !(rhs < lhs); }
friend bool operator >=(const Params & lhs, const Params & rhs) { return !(lhs < rhs); }
};
2019-11-04 19:17:27 +00:00
using ResourceType = Quota::ResourceType;
using ResourceAmount = Quota::ResourceAmount;
~EnabledQuota();
2019-11-04 19:17:27 +00:00
/// Tracks resource consumption. If the quota exceeded and `check_exceeded == true`, throws an exception.
void used(ResourceType resource_type, ResourceAmount amount, bool check_exceeded = true) const;
void used(const std::pair<ResourceType, ResourceAmount> & resource, bool check_exceeded = true) const;
void used(const std::pair<ResourceType, ResourceAmount> & resource1, const std::pair<ResourceType, ResourceAmount> & resource2, bool check_exceeded = true) const;
void used(const std::pair<ResourceType, ResourceAmount> & resource1, const std::pair<ResourceType, ResourceAmount> & resource2, const std::pair<ResourceType, ResourceAmount> & resource3, bool check_exceeded = true) const;
void used(const std::vector<std::pair<ResourceType, ResourceAmount>> & resources, bool check_exceeded = true) const;
2019-11-04 19:17:27 +00:00
/// Checks if the quota exceeded. If so, throws an exception.
void checkExceeded() const;
void checkExceeded(ResourceType resource_type) const;
2019-11-04 19:17:27 +00:00
/// Returns the information about quota consumption.
2019-11-04 19:17:27 +00:00
QuotaUsageInfo getUsageInfo() const;
/// Returns an instance of EnabledQuota which is never exceeded.
static std::shared_ptr<const EnabledQuota> getUnlimitedQuota();
2019-11-04 19:17:27 +00:00
private:
friend class QuotaCache;
EnabledQuota(const Params & params_);
EnabledQuota() {}
2019-11-04 19:17:27 +00:00
const String & getUserName() const { return params.user_name; }
2019-11-04 19:17:27 +00:00
static constexpr size_t MAX_RESOURCE_TYPE = Quota::MAX_RESOURCE_TYPE;
struct Interval
{
mutable std::atomic<ResourceAmount> used[MAX_RESOURCE_TYPE];
ResourceAmount max[MAX_RESOURCE_TYPE];
std::chrono::seconds duration;
bool randomize_interval;
mutable std::atomic<std::chrono::system_clock::duration> end_of_interval;
Interval() {}
Interval(const Interval & src) { *this = src; }
Interval & operator =(const Interval & src);
};
struct Intervals
{
std::vector<Interval> intervals;
UUID quota_id;
String quota_name;
String quota_key;
QuotaUsageInfo getUsageInfo(std::chrono::system_clock::time_point current_time) const;
};
struct Impl;
const Params params;
boost::atomic_shared_ptr<const Intervals> intervals; /// atomically changed by QuotaUsageManager
2019-11-04 19:17:27 +00:00
};
}