2019-11-04 19:18:17 +00:00
|
|
|
#include <Storages/System/StorageSystemQuotas.h>
|
2021-11-02 11:06:20 +00:00
|
|
|
#include <Access/AccessControl.h>
|
2021-10-31 08:51:20 +00:00
|
|
|
#include <Access/Common/AccessFlags.h>
|
|
|
|
#include <Access/Quota.h>
|
2022-06-15 18:25:13 +00:00
|
|
|
#include <Backups/BackupEntriesCollector.h>
|
|
|
|
#include <Backups/RestorerFromBackup.h>
|
2021-10-31 08:51:20 +00:00
|
|
|
#include <Columns/ColumnArray.h>
|
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
2019-11-04 19:18:17 +00:00
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <DataTypes/DataTypeEnum.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <DataTypes/DataTypeUUID.h>
|
|
|
|
#include <DataTypes/DataTypeArray.h>
|
|
|
|
#include <Interpreters/Context.h>
|
2021-10-31 08:51:20 +00:00
|
|
|
#include <Parsers/Access/ASTRolesOrUsersSet.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/range.h>
|
2019-11-04 19:18:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
DataTypeEnum8::Values getKeyTypeEnumValues()
|
|
|
|
{
|
|
|
|
DataTypeEnum8::Values enum_values;
|
2021-11-18 18:43:02 +00:00
|
|
|
for (auto key_type : collections::range(QuotaKeyType::MAX))
|
2020-06-05 17:57:33 +00:00
|
|
|
{
|
2021-11-18 18:43:02 +00:00
|
|
|
const auto & type_info = QuotaKeyTypeInfo::get(key_type);
|
|
|
|
if ((key_type != QuotaKeyType::NONE) && type_info.base_types.empty())
|
2020-06-05 17:57:33 +00:00
|
|
|
enum_values.push_back({type_info.name, static_cast<Int8>(key_type)});
|
|
|
|
}
|
2019-11-04 19:18:17 +00:00
|
|
|
return enum_values;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NamesAndTypesList StorageSystemQuotas::getNamesAndTypes()
|
|
|
|
{
|
|
|
|
NamesAndTypesList names_and_types{
|
|
|
|
{"name", std::make_shared<DataTypeString>()},
|
|
|
|
{"id", std::make_shared<DataTypeUUID>()},
|
2020-06-05 17:57:33 +00:00
|
|
|
{"storage", std::make_shared<DataTypeString>()},
|
|
|
|
{"keys", std::make_shared<DataTypeArray>(std::make_shared<DataTypeEnum8>(getKeyTypeEnumValues()))},
|
2020-05-08 12:50:45 +00:00
|
|
|
{"durations", std::make_shared<DataTypeArray>(std::make_shared<DataTypeUInt32>())},
|
|
|
|
{"apply_to_all", std::make_shared<DataTypeUInt8>()},
|
|
|
|
{"apply_to_list", std::make_shared<DataTypeArray>(std::make_shared<DataTypeString>())},
|
|
|
|
{"apply_to_except", std::make_shared<DataTypeArray>(std::make_shared<DataTypeString>())}
|
|
|
|
};
|
2019-11-04 19:18:17 +00:00
|
|
|
return names_and_types;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
void StorageSystemQuotas::fillData(MutableColumns & res_columns, ContextPtr context, const SelectQueryInfo &) const
|
2019-11-04 19:18:17 +00:00
|
|
|
{
|
2022-07-11 16:21:44 +00:00
|
|
|
/// If "select_from_system_db_requires_grant" is enabled the access rights were already checked in InterpreterSelectQuery.
|
2021-11-02 11:06:20 +00:00
|
|
|
const auto & access_control = context->getAccessControl();
|
2022-07-11 16:21:44 +00:00
|
|
|
if (!access_control.doesSelectFromSystemDatabaseRequireGrant())
|
|
|
|
context->checkAccess(AccessType::SHOW_QUOTAS);
|
|
|
|
|
2020-05-08 12:50:45 +00:00
|
|
|
std::vector<UUID> ids = access_control.findAll<Quota>();
|
|
|
|
|
|
|
|
size_t column_index = 0;
|
|
|
|
auto & column_name = assert_cast<ColumnString &>(*res_columns[column_index++]);
|
2021-05-03 22:59:38 +00:00
|
|
|
auto & column_id = assert_cast<ColumnUUID &>(*res_columns[column_index++]).getData();
|
2020-05-08 12:50:45 +00:00
|
|
|
auto & column_storage = assert_cast<ColumnString &>(*res_columns[column_index++]);
|
2020-06-05 17:57:33 +00:00
|
|
|
auto & column_key_types = assert_cast<ColumnInt8 &>(assert_cast<ColumnArray &>(*res_columns[column_index]).getData()).getData();
|
|
|
|
auto & column_key_types_offsets = assert_cast<ColumnArray &>(*res_columns[column_index++]).getOffsets();
|
2020-05-08 12:50:45 +00:00
|
|
|
auto & column_durations = assert_cast<ColumnUInt32 &>(assert_cast<ColumnArray &>(*res_columns[column_index]).getData()).getData();
|
|
|
|
auto & column_durations_offsets = assert_cast<ColumnArray &>(*res_columns[column_index++]).getOffsets();
|
|
|
|
auto & column_apply_to_all = assert_cast<ColumnUInt8 &>(*res_columns[column_index++]).getData();
|
|
|
|
auto & column_apply_to_list = assert_cast<ColumnString &>(assert_cast<ColumnArray &>(*res_columns[column_index]).getData());
|
|
|
|
auto & column_apply_to_list_offsets = assert_cast<ColumnArray &>(*res_columns[column_index++]).getOffsets();
|
|
|
|
auto & column_apply_to_except = assert_cast<ColumnString &>(assert_cast<ColumnArray &>(*res_columns[column_index]).getData());
|
|
|
|
auto & column_apply_to_except_offsets = assert_cast<ColumnArray &>(*res_columns[column_index++]).getOffsets();
|
2020-04-05 16:28:52 +00:00
|
|
|
|
2020-05-08 12:50:45 +00:00
|
|
|
auto add_row = [&](const String & name,
|
|
|
|
const UUID & id,
|
|
|
|
const String & storage_name,
|
|
|
|
const std::vector<Quota::Limits> & all_limits,
|
2021-11-18 18:43:02 +00:00
|
|
|
QuotaKeyType key_type,
|
2020-05-30 20:10:45 +00:00
|
|
|
const RolesOrUsersSet & apply_to)
|
2019-11-04 19:18:17 +00:00
|
|
|
{
|
2020-05-08 12:50:45 +00:00
|
|
|
column_name.insertData(name.data(), name.length());
|
2021-04-25 09:30:43 +00:00
|
|
|
column_id.push_back(id.toUnderType());
|
2020-05-08 12:50:45 +00:00
|
|
|
column_storage.insertData(storage_name.data(), storage_name.length());
|
2020-06-05 17:57:33 +00:00
|
|
|
|
2021-11-18 18:43:02 +00:00
|
|
|
if (key_type != QuotaKeyType::NONE)
|
2020-06-05 17:57:33 +00:00
|
|
|
{
|
2021-11-18 18:43:02 +00:00
|
|
|
const auto & type_info = QuotaKeyTypeInfo::get(key_type);
|
2020-06-05 17:57:33 +00:00
|
|
|
for (auto base_type : type_info.base_types)
|
|
|
|
column_key_types.push_back(static_cast<Int8>(base_type));
|
|
|
|
if (type_info.base_types.empty())
|
|
|
|
column_key_types.push_back(static_cast<Int8>(key_type));
|
|
|
|
}
|
|
|
|
column_key_types_offsets.push_back(column_key_types.size());
|
2020-05-08 12:50:45 +00:00
|
|
|
|
|
|
|
for (const auto & limits : all_limits)
|
2022-10-07 10:46:45 +00:00
|
|
|
{
|
|
|
|
column_durations.push_back(
|
|
|
|
static_cast<UInt32>(std::chrono::duration_cast<std::chrono::seconds>(limits.duration).count()));
|
|
|
|
}
|
2020-05-08 12:50:45 +00:00
|
|
|
column_durations_offsets.push_back(column_durations.size());
|
|
|
|
|
|
|
|
auto apply_to_ast = apply_to.toASTWithNames(access_control);
|
|
|
|
column_apply_to_all.push_back(apply_to_ast->all);
|
|
|
|
|
|
|
|
for (const auto & role_name : apply_to_ast->names)
|
|
|
|
column_apply_to_list.insertData(role_name.data(), role_name.length());
|
|
|
|
column_apply_to_list_offsets.push_back(column_apply_to_list.size());
|
|
|
|
|
|
|
|
for (const auto & role_name : apply_to_ast->except_names)
|
|
|
|
column_apply_to_except.insertData(role_name.data(), role_name.length());
|
|
|
|
column_apply_to_except_offsets.push_back(column_apply_to_except.size());
|
|
|
|
};
|
2019-11-04 19:18:17 +00:00
|
|
|
|
|
|
|
for (const auto & id : access_control.findAll<Quota>())
|
|
|
|
{
|
|
|
|
auto quota = access_control.tryRead<Quota>(id);
|
|
|
|
if (!quota)
|
|
|
|
continue;
|
2020-08-05 19:54:06 +00:00
|
|
|
auto storage = access_control.findStorage(id);
|
2020-05-08 12:50:45 +00:00
|
|
|
if (!storage)
|
|
|
|
continue;
|
2019-11-04 19:18:17 +00:00
|
|
|
|
2020-05-08 12:50:45 +00:00
|
|
|
add_row(quota->getName(), id, storage->getStorageName(), quota->all_limits, quota->key_type, quota->to_roles);
|
2019-11-04 19:18:17 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-15 18:25:13 +00:00
|
|
|
|
|
|
|
void StorageSystemQuotas::backupData(
|
2022-06-29 12:42:23 +00:00
|
|
|
BackupEntriesCollector & backup_entries_collector, const String & data_path_in_backup, const std::optional<ASTs> & /* partitions */)
|
2022-06-15 18:25:13 +00:00
|
|
|
{
|
|
|
|
const auto & access_control = backup_entries_collector.getContext()->getAccessControl();
|
2022-06-29 20:44:05 +00:00
|
|
|
access_control.backup(backup_entries_collector, data_path_in_backup, AccessEntityType::QUOTA);
|
2022-06-15 18:25:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StorageSystemQuotas::restoreDataFromBackup(
|
2022-06-29 20:44:05 +00:00
|
|
|
RestorerFromBackup & restorer, const String & /* data_path_in_backup */, const std::optional<ASTs> & /* partitions */)
|
2022-06-15 18:25:13 +00:00
|
|
|
{
|
|
|
|
auto & access_control = restorer.getContext()->getAccessControl();
|
2022-06-29 20:44:05 +00:00
|
|
|
access_control.restoreFromBackup(restorer);
|
2022-06-15 18:25:13 +00:00
|
|
|
}
|
|
|
|
|
2019-11-04 19:18:17 +00:00
|
|
|
}
|