2019-11-04 19:17:27 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
#include <Access/EnabledQuota.h>
|
2020-01-29 15:51:12 +00:00
|
|
|
#include <ext/scope_guard.h>
|
2019-11-04 19:17:27 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
2020-03-07 17:37:38 +00:00
|
|
|
#include <map>
|
2019-11-04 19:17:27 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
class AccessControlManager;
|
|
|
|
|
|
|
|
|
|
|
|
/// Stores information how much amount of resources have been consumed and how much are left.
|
2020-03-07 17:37:38 +00:00
|
|
|
class QuotaCache
|
2019-11-04 19:17:27 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-03-07 17:37:38 +00:00
|
|
|
QuotaCache(const AccessControlManager & access_control_manager_);
|
|
|
|
~QuotaCache();
|
2019-11-04 19:17:27 +00:00
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
std::shared_ptr<const EnabledQuota> getEnabledQuota(const UUID & user_id, const String & user_name, const std::vector<UUID> & enabled_roles, const Poco::Net::IPAddress & address, const String & client_key);
|
2019-11-04 19:17:27 +00:00
|
|
|
std::vector<QuotaUsageInfo> getUsageInfo() const;
|
|
|
|
|
|
|
|
private:
|
2020-03-07 17:37:38 +00:00
|
|
|
using Interval = EnabledQuota::Interval;
|
|
|
|
using Intervals = EnabledQuota::Intervals;
|
2019-11-04 19:17:27 +00:00
|
|
|
|
|
|
|
struct QuotaInfo
|
|
|
|
{
|
|
|
|
QuotaInfo(const QuotaPtr & quota_, const UUID & quota_id_) { setQuota(quota_, quota_id_); }
|
|
|
|
void setQuota(const QuotaPtr & quota_, const UUID & quota_id_);
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
String calculateKey(const EnabledQuota & enabled_quota) const;
|
2020-02-12 23:59:49 +00:00
|
|
|
boost::shared_ptr<const Intervals> getOrBuildIntervals(const String & key);
|
|
|
|
boost::shared_ptr<const Intervals> rebuildIntervals(const String & key);
|
2019-11-04 19:17:27 +00:00
|
|
|
void rebuildAllIntervals();
|
|
|
|
|
|
|
|
QuotaPtr quota;
|
|
|
|
UUID quota_id;
|
2020-03-07 17:37:38 +00:00
|
|
|
const ExtendedRoleSet * roles = nullptr;
|
2020-02-12 23:59:49 +00:00
|
|
|
std::unordered_map<String /* quota key */, boost::shared_ptr<const Intervals>> key_to_intervals;
|
2019-11-04 19:17:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void ensureAllQuotasRead();
|
|
|
|
void quotaAddedOrChanged(const UUID & quota_id, const std::shared_ptr<const Quota> & new_quota);
|
|
|
|
void quotaRemoved(const UUID & quota_id);
|
2020-03-07 17:37:38 +00:00
|
|
|
void chooseQuotaToConsume();
|
|
|
|
void chooseQuotaToConsumeFor(EnabledQuota & enabled_quota);
|
2019-11-04 19:17:27 +00:00
|
|
|
|
|
|
|
const AccessControlManager & access_control_manager;
|
|
|
|
mutable std::mutex mutex;
|
|
|
|
std::unordered_map<UUID /* quota id */, QuotaInfo> all_quotas;
|
|
|
|
bool all_quotas_read = false;
|
2020-01-29 15:51:12 +00:00
|
|
|
ext::scope_guard subscription;
|
2020-03-07 17:37:38 +00:00
|
|
|
std::map<EnabledQuota::Params, std::weak_ptr<EnabledQuota>> enabled_quotas;
|
2019-11-04 19:17:27 +00:00
|
|
|
};
|
|
|
|
}
|