2013-08-12 00:36:18 +00:00
|
|
|
#pragma once
|
|
|
|
|
2016-05-23 00:40:28 +00:00
|
|
|
#include <cstring>
|
2014-01-08 16:33:28 +00:00
|
|
|
#include <unordered_map>
|
2015-04-16 06:12:35 +00:00
|
|
|
#include <memory>
|
2016-05-23 00:40:28 +00:00
|
|
|
#include <random>
|
2013-08-12 00:36:18 +00:00
|
|
|
|
|
|
|
#include <Poco/Timespan.h>
|
|
|
|
|
|
|
|
#include <Poco/Util/Application.h>
|
|
|
|
#include <Poco/Util/AbstractConfiguration.h>
|
|
|
|
|
|
|
|
#include <Poco/Net/IPAddress.h>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Core/Types.h>
|
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
2013-08-12 00:36:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-12-25 04:00:33 +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.
|
2013-08-12 00:36:18 +00:00
|
|
|
*/
|
|
|
|
|
2016-07-31 03:53:16 +00:00
|
|
|
/// Used both for maximum allowed values and for counters of current accumulated values.
|
2017-04-01 07:20:54 +00:00
|
|
|
template <typename Counter> /// either size_t or std::atomic<size_t>
|
2013-08-12 00:36:18 +00:00
|
|
|
struct QuotaValues
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Zero values (for maximums) means no limit.
|
|
|
|
Counter queries; /// Number of queries.
|
2017-04-20 20:30:33 +00:00
|
|
|
Counter errors; /// Number of queries with exceptions.
|
2017-04-01 07:20:54 +00:00
|
|
|
Counter result_rows; /// Number of rows returned as result.
|
2017-04-20 20:30:33 +00:00
|
|
|
Counter result_bytes; /// Number of bytes returned as result.
|
|
|
|
Counter read_rows; /// Number of rows read from tables.
|
|
|
|
Counter read_bytes; /// Number of bytes read from tables.
|
2017-04-01 07:20:54 +00:00
|
|
|
Counter execution_time_usec; /// Total amount of query execution time in microseconds.
|
|
|
|
|
|
|
|
QuotaValues()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
QuotaValues(const QuotaValues & rhs)
|
|
|
|
{
|
|
|
|
tuple() = rhs.tuple();
|
|
|
|
}
|
|
|
|
|
|
|
|
QuotaValues & operator=(const QuotaValues & rhs)
|
|
|
|
{
|
|
|
|
tuple() = rhs.tuple();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
tuple() = std::make_tuple(0, 0, 0, 0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void initFromConfig(const String & config_elem, Poco::Util::AbstractConfiguration & config);
|
|
|
|
|
|
|
|
bool operator== (const QuotaValues & rhs) const
|
|
|
|
{
|
|
|
|
return tuple() == rhs.tuple();
|
|
|
|
}
|
2016-07-31 03:53:16 +00:00
|
|
|
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
auto tuple()
|
|
|
|
{
|
|
|
|
return std::forward_as_tuple(queries, errors, result_rows, result_bytes, read_rows, read_bytes, execution_time_usec);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto tuple() const
|
|
|
|
{
|
|
|
|
return std::make_tuple(queries, errors, result_rows, result_bytes, read_rows, read_bytes, execution_time_usec);
|
|
|
|
}
|
2013-08-12 00:36:18 +00:00
|
|
|
};
|
|
|
|
|
2016-07-31 03:53:16 +00:00
|
|
|
template <>
|
|
|
|
inline auto QuotaValues<std::atomic<size_t>>::tuple() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_tuple(
|
|
|
|
queries.load(std::memory_order_relaxed),
|
|
|
|
errors.load(std::memory_order_relaxed),
|
|
|
|
result_rows.load(std::memory_order_relaxed),
|
|
|
|
result_bytes.load(std::memory_order_relaxed),
|
|
|
|
read_rows.load(std::memory_order_relaxed),
|
|
|
|
read_bytes.load(std::memory_order_relaxed),
|
|
|
|
execution_time_usec.load(std::memory_order_relaxed));
|
2016-07-31 03:53:16 +00:00
|
|
|
}
|
|
|
|
|
2013-08-12 00:36:18 +00:00
|
|
|
|
2016-12-25 04:00:33 +00:00
|
|
|
/// Time, rounded down to start of interval; limits for that interval and accumulated values.
|
2013-08-12 00:36:18 +00:00
|
|
|
struct QuotaForInterval
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
time_t rounded_time = 0;
|
|
|
|
size_t duration = 0;
|
|
|
|
time_t offset = 0; /// Offset of interval for randomization (to avoid DoS if intervals for many users end at one time).
|
|
|
|
QuotaValues<size_t> max;
|
|
|
|
QuotaValues<std::atomic<size_t>> used;
|
|
|
|
|
|
|
|
QuotaForInterval() {}
|
|
|
|
QuotaForInterval(time_t duration_) : duration(duration_) {}
|
|
|
|
|
|
|
|
void initFromConfig(const String & config_elem, time_t duration_, time_t offset_, Poco::Util::AbstractConfiguration & config);
|
|
|
|
|
|
|
|
/// Increase current value.
|
|
|
|
void addQuery() noexcept;
|
|
|
|
void addError() noexcept;
|
|
|
|
|
|
|
|
/// Check if quota is already exceeded. If that, throw an exception.
|
|
|
|
void checkExceeded(time_t current_time, const String & quota_name, const String & user_name);
|
|
|
|
|
|
|
|
/// Check corresponding value. If exceeded, throw an exception. Otherwise, increase that value.
|
|
|
|
void checkAndAddResultRowsBytes(time_t current_time, const String & quota_name, const String & user_name, size_t rows, size_t bytes);
|
|
|
|
void checkAndAddReadRowsBytes(time_t current_time, const String & quota_name, const String & user_name, size_t rows, size_t bytes);
|
|
|
|
void checkAndAddExecutionTime(time_t current_time, const String & quota_name, const String & user_name, Poco::Timespan amount);
|
|
|
|
|
|
|
|
/// Get a text, describing what quota is exceeded.
|
|
|
|
String toString() const;
|
|
|
|
|
|
|
|
bool operator== (const QuotaForInterval & rhs) const
|
|
|
|
{
|
|
|
|
return
|
|
|
|
rounded_time == rhs.rounded_time &&
|
|
|
|
duration == rhs.duration &&
|
|
|
|
max == rhs.max &&
|
|
|
|
used == rhs.used;
|
|
|
|
}
|
2013-08-12 00:36:18 +00:00
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Reset counters of used resources, if interval for quota is expired.
|
|
|
|
void updateTime(time_t current_time);
|
|
|
|
void check(size_t max_amount, size_t used_amount, time_t current_time,
|
|
|
|
const String & quota_name, const String & user_name, const char * resource_name);
|
2013-08-12 00:36:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct Quota;
|
|
|
|
|
2016-07-31 03:53:16 +00:00
|
|
|
/// Length of interval -> quota: maximum allowed and currently accumulated values for that interval (example: 3600 -> values for current hour).
|
2013-08-12 00:36:18 +00:00
|
|
|
class QuotaForIntervals
|
|
|
|
{
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
/// While checking, will walk through intervals in order of decreasing size - from largest to smallest.
|
|
|
|
/// To report first about largest interval on what quota was exceeded.
|
|
|
|
using Container = std::map<size_t, QuotaForInterval>;
|
|
|
|
Container cont;
|
2013-08-12 00:36:18 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string quota_name;
|
|
|
|
std::string user_name; /// user name is set only for current counters for user, not for object that contain maximum values (limits).
|
2013-08-12 00:36:18 +00:00
|
|
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
QuotaForIntervals(const std::string & quota_name_, const std::string & user_name_)
|
|
|
|
: quota_name(quota_name_), user_name(user_name_) {}
|
2016-12-25 04:00:33 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
QuotaForIntervals(const QuotaForIntervals & other, const std::string & user_name_)
|
|
|
|
: QuotaForIntervals(other)
|
|
|
|
{
|
|
|
|
user_name = user_name_;
|
|
|
|
}
|
2016-12-25 04:00:33 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
QuotaForIntervals() = default;
|
|
|
|
QuotaForIntervals(const QuotaForIntervals & other) = default;
|
2013-08-28 20:47:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Is there at least one interval for counting quota?
|
|
|
|
bool empty() const
|
|
|
|
{
|
|
|
|
return cont.empty();
|
|
|
|
}
|
2015-04-16 06:12:35 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void initFromConfig(const String & config_elem, Poco::Util::AbstractConfiguration & config, std::mt19937 & rng);
|
2014-02-13 07:17:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Set maximum values (limits) from passed argument.
|
|
|
|
/// Remove intervals that does not exist in argument. Add intervals from argument, that we don't have.
|
|
|
|
void setMax(const QuotaForIntervals & quota);
|
2013-08-12 00:36:18 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void addQuery() noexcept;
|
|
|
|
void addError() noexcept;
|
2013-08-12 00:36:18 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void checkExceeded(time_t current_time);
|
2013-08-12 00:36:18 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void checkAndAddResultRowsBytes(time_t current_time, size_t rows, size_t bytes);
|
|
|
|
void checkAndAddReadRowsBytes(time_t current_time, size_t rows, size_t bytes);
|
|
|
|
void checkAndAddExecutionTime(time_t current_time, Poco::Timespan amount);
|
2013-08-28 20:47:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Get text, describing what part of quota has been exceeded.
|
|
|
|
String toString() const;
|
2014-02-13 07:17:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
bool operator== (const QuotaForIntervals & rhs) const
|
|
|
|
{
|
|
|
|
return cont == rhs.cont && quota_name == rhs.quota_name;
|
|
|
|
}
|
2013-08-12 00:36:18 +00:00
|
|
|
};
|
|
|
|
|
2016-05-28 10:35:44 +00:00
|
|
|
using QuotaForIntervalsPtr = std::shared_ptr<QuotaForIntervals>;
|
2014-02-13 07:17:22 +00:00
|
|
|
|
2013-08-12 00:36:18 +00:00
|
|
|
|
2016-07-31 03:53:16 +00:00
|
|
|
/// Quota key -> quotas (max and current values) for intervals. If quota doesn't have keys, then values stored at key 0.
|
2013-08-12 00:36:18 +00:00
|
|
|
struct Quota
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using Container = std::unordered_map<UInt64, QuotaForIntervalsPtr>;
|
2013-08-12 00:36:18 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
String name;
|
2013-08-12 00:36:18 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Maximum values from config.
|
|
|
|
QuotaForIntervals max;
|
|
|
|
/// Maximum and accumulated values for different keys.
|
|
|
|
/// For all keys, maximum values are the same and taken from 'max'.
|
|
|
|
Container quota_for_keys;
|
|
|
|
std::mutex mutex;
|
2013-08-12 00:36:18 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
bool is_keyed = false;
|
2017-04-20 20:30:33 +00:00
|
|
|
|
|
|
|
/// If the quota is not keyed, but the user passed some key, ignore it instead of throwing exception.
|
|
|
|
/// For transitional periods, when you want to enable quota keys
|
|
|
|
/// - first, enable passing keys from your application, then make quota keyed in ClickHouse users config.
|
|
|
|
bool ignore_key_if_not_keyed = false;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
bool keyed_by_ip = false;
|
2013-08-12 00:36:18 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void loadFromConfig(const String & config_elem, const String & name_, Poco::Util::AbstractConfiguration & config, std::mt19937 & rng);
|
|
|
|
QuotaForIntervalsPtr get(const String & quota_key, const String & user_name, const Poco::Net::IPAddress & ip);
|
2013-08-12 00:36:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Quotas
|
|
|
|
{
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Name of quota -> quota.
|
|
|
|
using Container = std::unordered_map<String, std::unique_ptr<Quota>>;
|
|
|
|
Container cont;
|
2013-08-12 00:36:18 +00:00
|
|
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
void loadFromConfig(Poco::Util::AbstractConfiguration & config);
|
|
|
|
QuotaForIntervalsPtr get(const String & name, const String & quota_key,
|
|
|
|
const String & user_name, const Poco::Net::IPAddress & ip);
|
2013-08-12 00:36:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|