2020-03-07 17:37:38 +00:00
|
|
|
#include <Access/EnabledQuota.h>
|
2020-05-08 12:50:45 +00:00
|
|
|
#include <Access/QuotaUsage.h>
|
2019-11-04 19:17:27 +00:00
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <Common/quoteString.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/chrono_io.h>
|
|
|
|
#include <base/range.h>
|
2020-02-12 23:59:49 +00:00
|
|
|
#include <boost/smart_ptr/make_shared.hpp>
|
2019-11-04 19:17:27 +00:00
|
|
|
#include <boost/range/algorithm/fill.hpp>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int QUOTA_EXPIRED;
|
|
|
|
}
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
struct EnabledQuota::Impl
|
2019-11-04 19:17:27 +00:00
|
|
|
{
|
|
|
|
[[noreturn]] static void throwQuotaExceed(
|
|
|
|
const String & user_name,
|
|
|
|
const String & quota_name,
|
|
|
|
ResourceType resource_type,
|
|
|
|
ResourceAmount used,
|
|
|
|
ResourceAmount max,
|
|
|
|
std::chrono::seconds duration,
|
|
|
|
std::chrono::system_clock::time_point end_of_interval)
|
|
|
|
{
|
2020-05-08 12:50:45 +00:00
|
|
|
const auto & type_info = Quota::ResourceTypeInfo::get(resource_type);
|
2019-11-04 19:17:27 +00:00
|
|
|
throw Exception(
|
2021-06-15 19:55:21 +00:00
|
|
|
"Quota for user " + backQuote(user_name) + " for " + to_string(duration) + " has been exceeded: "
|
2020-05-08 12:50:45 +00:00
|
|
|
+ type_info.outputWithAmount(used) + "/" + type_info.amountToString(max) + ". "
|
2021-06-15 19:55:21 +00:00
|
|
|
+ "Interval will end at " + to_string(end_of_interval) + ". " + "Name of quota template: " + backQuote(quota_name),
|
2019-11-04 19:17:27 +00:00
|
|
|
ErrorCodes::QUOTA_EXPIRED);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-04 19:14:44 +00:00
|
|
|
/// Returns the end of the current interval. If the passed `current_time` is greater than that end,
|
|
|
|
/// the function automatically recalculates the interval's end by adding the interval's duration
|
|
|
|
/// one or more times until the interval's end is greater than `current_time`.
|
|
|
|
/// If that recalculation occurs the function also resets amounts of resources used and sets the variable
|
|
|
|
/// `counters_were_reset`.
|
2019-11-04 19:17:27 +00:00
|
|
|
static std::chrono::system_clock::time_point getEndOfInterval(
|
2021-02-04 19:14:44 +00:00
|
|
|
const Interval & interval, std::chrono::system_clock::time_point current_time, bool & counters_were_reset)
|
2019-11-04 19:17:27 +00:00
|
|
|
{
|
|
|
|
auto & end_of_interval = interval.end_of_interval;
|
|
|
|
auto end_loaded = end_of_interval.load();
|
|
|
|
auto end = std::chrono::system_clock::time_point{end_loaded};
|
|
|
|
if (current_time < end)
|
|
|
|
{
|
2021-02-04 19:14:44 +00:00
|
|
|
counters_were_reset = false;
|
2019-11-04 19:17:27 +00:00
|
|
|
return end;
|
|
|
|
}
|
|
|
|
|
2021-11-12 10:22:55 +00:00
|
|
|
bool need_reset_counters = false;
|
2019-11-04 19:17:27 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2021-02-04 19:14:44 +00:00
|
|
|
/// Calculate the end of the next interval:
|
|
|
|
/// | X |
|
|
|
|
/// end current_time next_end = end + duration * n
|
|
|
|
/// where n is an integer number, n >= 1.
|
|
|
|
const auto duration = interval.duration;
|
|
|
|
UInt64 n = static_cast<UInt64>((current_time - end + duration) / duration);
|
|
|
|
end = end + duration * n;
|
2019-11-04 19:17:27 +00:00
|
|
|
if (end_of_interval.compare_exchange_strong(end_loaded, end.time_since_epoch()))
|
2021-11-12 10:22:55 +00:00
|
|
|
{
|
2021-12-06 07:32:12 +00:00
|
|
|
need_reset_counters = true;
|
2019-11-04 19:17:27 +00:00
|
|
|
break;
|
2021-11-12 10:22:55 +00:00
|
|
|
}
|
2019-11-04 19:17:27 +00:00
|
|
|
end = std::chrono::system_clock::time_point{end_loaded};
|
|
|
|
}
|
|
|
|
while (current_time >= end);
|
|
|
|
|
2021-02-04 19:14:44 +00:00
|
|
|
if (need_reset_counters)
|
|
|
|
{
|
|
|
|
boost::range::fill(interval.used, 0);
|
|
|
|
counters_were_reset = true;
|
|
|
|
}
|
2019-11-04 19:17:27 +00:00
|
|
|
return end;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void used(
|
|
|
|
const String & user_name,
|
|
|
|
const Intervals & intervals,
|
|
|
|
ResourceType resource_type,
|
|
|
|
ResourceAmount amount,
|
|
|
|
std::chrono::system_clock::time_point current_time,
|
|
|
|
bool check_exceeded)
|
|
|
|
{
|
|
|
|
for (const auto & interval : intervals.intervals)
|
|
|
|
{
|
2021-12-10 09:47:05 +00:00
|
|
|
if (!interval.end_of_interval.load().count())
|
|
|
|
{
|
|
|
|
/// We need to calculate end of the interval if it hasn't been calculated before.
|
|
|
|
bool dummy;
|
|
|
|
getEndOfInterval(interval, current_time, dummy);
|
|
|
|
}
|
|
|
|
|
2019-11-04 19:17:27 +00:00
|
|
|
ResourceAmount used = (interval.used[resource_type] += amount);
|
|
|
|
ResourceAmount max = interval.max[resource_type];
|
2021-12-10 09:47:05 +00:00
|
|
|
|
2020-05-08 12:50:45 +00:00
|
|
|
if (!max)
|
2019-11-04 19:17:27 +00:00
|
|
|
continue;
|
2021-12-10 09:47:05 +00:00
|
|
|
|
2019-11-04 19:17:27 +00:00
|
|
|
if (used > max)
|
|
|
|
{
|
|
|
|
bool counters_were_reset = false;
|
2021-02-04 19:14:44 +00:00
|
|
|
auto end_of_interval = getEndOfInterval(interval, current_time, counters_were_reset);
|
2019-11-04 19:17:27 +00:00
|
|
|
if (counters_were_reset)
|
|
|
|
{
|
|
|
|
used = (interval.used[resource_type] += amount);
|
|
|
|
if ((used > max) && check_exceeded)
|
|
|
|
throwQuotaExceed(user_name, intervals.quota_name, resource_type, used, max, interval.duration, end_of_interval);
|
|
|
|
}
|
|
|
|
else if (check_exceeded)
|
|
|
|
throwQuotaExceed(user_name, intervals.quota_name, resource_type, used, max, interval.duration, end_of_interval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void checkExceeded(
|
|
|
|
const String & user_name,
|
|
|
|
const Intervals & intervals,
|
|
|
|
ResourceType resource_type,
|
|
|
|
std::chrono::system_clock::time_point current_time)
|
|
|
|
{
|
|
|
|
for (const auto & interval : intervals.intervals)
|
|
|
|
{
|
2021-12-10 09:47:05 +00:00
|
|
|
if (!interval.end_of_interval.load().count())
|
|
|
|
{
|
|
|
|
/// We need to calculate end of the interval if it hasn't been calculated before.
|
|
|
|
bool dummy;
|
|
|
|
getEndOfInterval(interval, current_time, dummy);
|
|
|
|
}
|
|
|
|
|
2019-11-04 19:17:27 +00:00
|
|
|
ResourceAmount used = interval.used[resource_type];
|
|
|
|
ResourceAmount max = interval.max[resource_type];
|
2020-05-08 12:50:45 +00:00
|
|
|
if (!max)
|
2019-11-04 19:17:27 +00:00
|
|
|
continue;
|
2021-12-10 09:47:05 +00:00
|
|
|
|
2019-11-04 19:17:27 +00:00
|
|
|
if (used > max)
|
|
|
|
{
|
2021-02-04 19:14:44 +00:00
|
|
|
bool counters_were_reset = false;
|
|
|
|
std::chrono::system_clock::time_point end_of_interval = getEndOfInterval(interval, current_time, counters_were_reset);
|
|
|
|
if (!counters_were_reset)
|
2019-11-04 19:17:27 +00:00
|
|
|
throwQuotaExceed(user_name, intervals.quota_name, resource_type, used, max, interval.duration, end_of_interval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void checkExceeded(
|
|
|
|
const String & user_name,
|
|
|
|
const Intervals & intervals,
|
|
|
|
std::chrono::system_clock::time_point current_time)
|
|
|
|
{
|
2021-06-15 19:55:21 +00:00
|
|
|
for (auto resource_type : collections::range(Quota::MAX_RESOURCE_TYPE))
|
2019-11-04 19:17:27 +00:00
|
|
|
checkExceeded(user_name, intervals, resource_type, current_time);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-05-08 12:50:45 +00:00
|
|
|
EnabledQuota::Interval::Interval()
|
|
|
|
{
|
2021-06-15 19:55:21 +00:00
|
|
|
for (auto resource_type : collections::range(MAX_RESOURCE_TYPE))
|
2020-05-08 12:50:45 +00:00
|
|
|
{
|
|
|
|
used[resource_type].store(0);
|
|
|
|
max[resource_type] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
EnabledQuota::Interval & EnabledQuota::Interval::operator =(const Interval & src)
|
2019-11-04 19:17:27 +00:00
|
|
|
{
|
2020-03-18 02:02:24 +00:00
|
|
|
if (this == &src)
|
|
|
|
return *this;
|
|
|
|
|
2019-11-04 19:17:27 +00:00
|
|
|
randomize_interval = src.randomize_interval;
|
|
|
|
duration = src.duration;
|
|
|
|
end_of_interval.store(src.end_of_interval.load());
|
2021-06-15 19:55:21 +00:00
|
|
|
for (auto resource_type : collections::range(MAX_RESOURCE_TYPE))
|
2019-11-04 19:17:27 +00:00
|
|
|
{
|
|
|
|
max[resource_type] = src.max[resource_type];
|
|
|
|
used[resource_type].store(src.used[resource_type].load());
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-08 12:50:45 +00:00
|
|
|
std::optional<QuotaUsage> EnabledQuota::Intervals::getUsage(std::chrono::system_clock::time_point current_time) const
|
2019-11-04 19:17:27 +00:00
|
|
|
{
|
2020-05-08 12:50:45 +00:00
|
|
|
if (!quota_id)
|
|
|
|
return {};
|
|
|
|
QuotaUsage usage;
|
|
|
|
usage.quota_id = *quota_id;
|
|
|
|
usage.quota_name = quota_name;
|
|
|
|
usage.quota_key = quota_key;
|
|
|
|
usage.intervals.reserve(intervals.size());
|
2019-11-04 19:17:27 +00:00
|
|
|
for (const auto & in : intervals)
|
|
|
|
{
|
2020-05-08 12:50:45 +00:00
|
|
|
usage.intervals.push_back({});
|
|
|
|
auto & out = usage.intervals.back();
|
2019-11-04 19:17:27 +00:00
|
|
|
out.duration = in.duration;
|
|
|
|
out.randomize_interval = in.randomize_interval;
|
2021-02-04 19:14:44 +00:00
|
|
|
bool counters_were_reset = false;
|
|
|
|
out.end_of_interval = Impl::getEndOfInterval(in, current_time, counters_were_reset);
|
2021-06-15 19:55:21 +00:00
|
|
|
for (auto resource_type : collections::range(MAX_RESOURCE_TYPE))
|
2019-11-04 19:17:27 +00:00
|
|
|
{
|
2020-05-08 12:50:45 +00:00
|
|
|
if (in.max[resource_type])
|
|
|
|
out.max[resource_type] = in.max[resource_type];
|
2019-11-04 19:17:27 +00:00
|
|
|
out.used[resource_type] = in.used[resource_type];
|
|
|
|
}
|
|
|
|
}
|
2020-05-08 12:50:45 +00:00
|
|
|
return usage;
|
2019-11-04 19:17:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
EnabledQuota::EnabledQuota(const Params & params_) : params(params_)
|
2019-11-04 19:17:27 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
EnabledQuota::~EnabledQuota() = default;
|
2019-11-04 19:17:27 +00:00
|
|
|
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
void EnabledQuota::used(ResourceType resource_type, ResourceAmount amount, bool check_exceeded) const
|
2019-11-04 19:17:27 +00:00
|
|
|
{
|
|
|
|
used({resource_type, amount}, check_exceeded);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
void EnabledQuota::used(const std::pair<ResourceType, ResourceAmount> & resource, bool check_exceeded) const
|
2019-11-04 19:17:27 +00:00
|
|
|
{
|
2020-02-12 23:59:49 +00:00
|
|
|
auto loaded = intervals.load();
|
2019-11-04 19:17:27 +00:00
|
|
|
auto current_time = std::chrono::system_clock::now();
|
2020-03-07 17:37:38 +00:00
|
|
|
Impl::used(getUserName(), *loaded, resource.first, resource.second, current_time, check_exceeded);
|
2019-11-04 19:17:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
void EnabledQuota::used(const std::pair<ResourceType, ResourceAmount> & resource1, const std::pair<ResourceType, ResourceAmount> & resource2, bool check_exceeded) const
|
2019-11-04 19:17:27 +00:00
|
|
|
{
|
2020-02-12 23:59:49 +00:00
|
|
|
auto loaded = intervals.load();
|
2019-11-04 19:17:27 +00:00
|
|
|
auto current_time = std::chrono::system_clock::now();
|
2020-03-07 17:37:38 +00:00
|
|
|
Impl::used(getUserName(), *loaded, resource1.first, resource1.second, current_time, check_exceeded);
|
|
|
|
Impl::used(getUserName(), *loaded, resource2.first, resource2.second, current_time, check_exceeded);
|
2019-11-04 19:17:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
void EnabledQuota::used(const std::pair<ResourceType, ResourceAmount> & resource1, const std::pair<ResourceType, ResourceAmount> & resource2, const std::pair<ResourceType, ResourceAmount> & resource3, bool check_exceeded) const
|
2019-11-04 19:17:27 +00:00
|
|
|
{
|
2020-02-12 23:59:49 +00:00
|
|
|
auto loaded = intervals.load();
|
2019-11-04 19:17:27 +00:00
|
|
|
auto current_time = std::chrono::system_clock::now();
|
2020-03-07 17:37:38 +00:00
|
|
|
Impl::used(getUserName(), *loaded, resource1.first, resource1.second, current_time, check_exceeded);
|
|
|
|
Impl::used(getUserName(), *loaded, resource2.first, resource2.second, current_time, check_exceeded);
|
|
|
|
Impl::used(getUserName(), *loaded, resource3.first, resource3.second, current_time, check_exceeded);
|
2019-11-04 19:17:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
void EnabledQuota::used(const std::vector<std::pair<ResourceType, ResourceAmount>> & resources, bool check_exceeded) const
|
2019-11-04 19:17:27 +00:00
|
|
|
{
|
2020-02-12 23:59:49 +00:00
|
|
|
auto loaded = intervals.load();
|
2019-11-04 19:17:27 +00:00
|
|
|
auto current_time = std::chrono::system_clock::now();
|
|
|
|
for (const auto & resource : resources)
|
2020-03-07 17:37:38 +00:00
|
|
|
Impl::used(getUserName(), *loaded, resource.first, resource.second, current_time, check_exceeded);
|
2019-11-04 19:17:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
void EnabledQuota::checkExceeded() const
|
2019-11-04 19:17:27 +00:00
|
|
|
{
|
2020-02-12 23:59:49 +00:00
|
|
|
auto loaded = intervals.load();
|
2020-03-07 17:37:38 +00:00
|
|
|
Impl::checkExceeded(getUserName(), *loaded, std::chrono::system_clock::now());
|
2019-11-04 19:17:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
void EnabledQuota::checkExceeded(ResourceType resource_type) const
|
2019-11-04 19:17:27 +00:00
|
|
|
{
|
2020-02-12 23:59:49 +00:00
|
|
|
auto loaded = intervals.load();
|
2020-03-07 17:37:38 +00:00
|
|
|
Impl::checkExceeded(getUserName(), *loaded, resource_type, std::chrono::system_clock::now());
|
2019-11-04 19:17:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-08 12:50:45 +00:00
|
|
|
std::optional<QuotaUsage> EnabledQuota::getUsage() const
|
2019-11-04 19:17:27 +00:00
|
|
|
{
|
2020-02-12 23:59:49 +00:00
|
|
|
auto loaded = intervals.load();
|
2020-05-08 12:50:45 +00:00
|
|
|
return loaded->getUsage(std::chrono::system_clock::now());
|
2019-11-04 19:17:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
std::shared_ptr<const EnabledQuota> EnabledQuota::getUnlimitedQuota()
|
2019-11-04 19:17:27 +00:00
|
|
|
{
|
2020-03-07 17:37:38 +00:00
|
|
|
static const std::shared_ptr<const EnabledQuota> res = []
|
|
|
|
{
|
|
|
|
auto unlimited_quota = std::shared_ptr<EnabledQuota>(new EnabledQuota);
|
|
|
|
unlimited_quota->intervals = boost::make_shared<Intervals>();
|
|
|
|
return unlimited_quota;
|
|
|
|
}();
|
|
|
|
return res;
|
2019-11-04 19:17:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|