2020-03-07 17:37:38 +00:00
|
|
|
#pragma once
|
2019-11-04 19:17:27 +00:00
|
|
|
|
|
|
|
#include <Access/IAccessEntity.h>
|
2021-11-18 18:07:35 +00:00
|
|
|
#include <Access/Common/QuotaDefs.h>
|
2020-05-30 20:10:45 +00:00
|
|
|
#include <Access/RolesOrUsersSet.h>
|
2019-11-04 19:17:27 +00:00
|
|
|
#include <chrono>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2020-05-02 16:05:01 +00:00
|
|
|
|
2019-11-04 19:17:27 +00:00
|
|
|
/** Quota for resources consumption for specific interval.
|
|
|
|
* Used to limit resource usage by user.
|
|
|
|
* Quota is applied "softly" - could be slightly exceed, because it is checked usually only on each block of processed data.
|
|
|
|
* Accumulated values are not persisted and are lost on server restart.
|
|
|
|
* Quota is local to server,
|
|
|
|
* but for distributed queries, accumulated values for read rows and bytes
|
|
|
|
* are collected from all participating servers and accumulated locally.
|
|
|
|
*/
|
|
|
|
struct Quota : public IAccessEntity
|
|
|
|
{
|
|
|
|
/// Amount of resources available to consume for each duration.
|
|
|
|
struct Limits
|
|
|
|
{
|
2021-11-18 18:07:35 +00:00
|
|
|
std::optional<QuotaValue> max[static_cast<size_t>(QuotaType::MAX)];
|
2019-11-04 19:17:27 +00:00
|
|
|
std::chrono::seconds duration = std::chrono::seconds::zero();
|
|
|
|
|
|
|
|
/// Intervals can be randomized (to avoid DoS if intervals for many users end at one time).
|
|
|
|
bool randomize_interval = false;
|
|
|
|
|
|
|
|
friend bool operator ==(const Limits & lhs, const Limits & rhs);
|
|
|
|
friend bool operator !=(const Limits & lhs, const Limits & rhs) { return !(lhs == rhs); }
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<Limits> all_limits;
|
|
|
|
|
|
|
|
/// Key to share quota consumption.
|
|
|
|
/// Users with the same key share the same amount of resource.
|
2021-11-18 18:43:02 +00:00
|
|
|
QuotaKeyType key_type = QuotaKeyType::NONE;
|
2019-11-04 19:17:27 +00:00
|
|
|
|
|
|
|
/// Which roles or users should use this quota.
|
2020-05-30 20:10:45 +00:00
|
|
|
RolesOrUsersSet to_roles;
|
2019-11-04 19:17:27 +00:00
|
|
|
|
|
|
|
bool equal(const IAccessEntity & other) const override;
|
|
|
|
std::shared_ptr<IAccessEntity> clone() const override { return cloneImpl<Quota>(); }
|
2021-11-18 20:54:18 +00:00
|
|
|
static constexpr const auto TYPE = AccessEntityType::QUOTA;
|
|
|
|
AccessEntityType getType() const override { return TYPE; }
|
2019-11-04 19:17:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
using QuotaPtr = std::shared_ptr<const Quota>;
|
|
|
|
}
|