2017-04-01 09:19:00 +00:00
|
|
|
#include <Storages/MergeTree/DiskSpaceMonitor.h>
|
2014-03-13 12:48:07 +00:00
|
|
|
|
2019-04-05 12:30:26 +00:00
|
|
|
#include <Common/escapeForFileName.h>
|
2019-04-05 19:45:59 +00:00
|
|
|
#include <Poco/File.h>
|
2019-04-05 12:30:26 +00:00
|
|
|
|
2019-04-21 18:38:44 +00:00
|
|
|
/// @TODO_IGR ASK Does such function already exists?
|
|
|
|
bool isAlphaNumeric(const std::string & s)
|
|
|
|
{
|
|
|
|
for (auto c : s)
|
|
|
|
if (!isalnum(c) && c != '_')
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-13 12:48:07 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2019-04-01 18:41:19 +00:00
|
|
|
std::map<String, DiskSpaceMonitor::DiskReserve> DiskSpaceMonitor::reserved;
|
2016-05-28 10:15:36 +00:00
|
|
|
std::mutex DiskSpaceMonitor::mutex;
|
2014-03-13 12:48:07 +00:00
|
|
|
|
2019-04-21 18:38:44 +00:00
|
|
|
DiskSelector::DiskSelector(const Poco::Util::AbstractConfiguration & config, const std::string & config_prefix, String default_path)
|
2019-04-05 17:37:27 +00:00
|
|
|
{
|
2019-04-04 17:19:11 +00:00
|
|
|
Poco::Util::AbstractConfiguration::Keys keys;
|
|
|
|
config.keys(config_prefix, keys);
|
|
|
|
|
2019-04-05 12:30:26 +00:00
|
|
|
constexpr auto default_disk_name = "default";
|
|
|
|
for (const auto & disk_name : keys)
|
2019-04-04 17:19:11 +00:00
|
|
|
{
|
2019-04-21 18:38:44 +00:00
|
|
|
if (!isAlphaNumeric(disk_name))
|
|
|
|
throw Exception("Disk name can contain only alphanumeric and '_' (" + disk_name + ")", ErrorCodes::EXCESSIVE_ELEMENT_IN_CONFIG);
|
|
|
|
|
|
|
|
auto disk_config_prefix = config_prefix + "." + disk_name;
|
|
|
|
UInt64 keep_free_space_bytes = config.getUInt64(disk_config_prefix + ".keep_free_space_bytes", 0);
|
2019-04-05 12:30:26 +00:00
|
|
|
String path;
|
2019-04-21 18:38:44 +00:00
|
|
|
if (config.has(disk_config_prefix + ".path"))
|
|
|
|
path = config.getString(disk_config_prefix + ".path");
|
2019-04-05 12:30:26 +00:00
|
|
|
|
2019-04-05 19:58:59 +00:00
|
|
|
if (disk_name == default_disk_name)
|
|
|
|
{
|
2019-04-05 17:37:27 +00:00
|
|
|
if (!path.empty())
|
2019-04-05 12:30:26 +00:00
|
|
|
throw Exception("It is not possible to specify default disk path", ErrorCodes::UNKNOWN_ELEMENT_IN_CONFIG);
|
2019-04-21 18:38:44 +00:00
|
|
|
disks.emplace(disk_name, std::make_shared<const Disk>(disk_name, default_path, keep_free_space_bytes));
|
2019-04-05 17:37:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (path.empty())
|
2019-04-05 12:30:26 +00:00
|
|
|
throw Exception("Disk path can not be empty. Disk " + disk_name, ErrorCodes::UNKNOWN_ELEMENT_IN_CONFIG);
|
2019-04-21 18:38:44 +00:00
|
|
|
disks.emplace(disk_name, std::make_shared<const Disk>(disk_name, path, keep_free_space_bytes));
|
2019-04-05 12:30:26 +00:00
|
|
|
}
|
2019-04-04 17:19:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-21 18:38:44 +00:00
|
|
|
const DiskPtr & DiskSelector::operator[](const String & name) const
|
2019-04-05 17:37:27 +00:00
|
|
|
{
|
2019-04-04 17:19:11 +00:00
|
|
|
auto it = disks.find(name);
|
2019-04-05 17:37:27 +00:00
|
|
|
if (it == disks.end())
|
2019-04-21 18:38:44 +00:00
|
|
|
throw Exception("Unknown disk " + name, ErrorCodes::UNKNOWN_DISK);
|
2019-04-04 17:19:11 +00:00
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2019-04-05 17:37:27 +00:00
|
|
|
bool DiskSelector::has(const String & name) const
|
|
|
|
{
|
2019-04-05 12:30:26 +00:00
|
|
|
auto it = disks.find(name);
|
|
|
|
return it != disks.end();
|
|
|
|
}
|
|
|
|
|
2019-04-21 18:38:44 +00:00
|
|
|
void DiskSelector::add(const DiskPtr & disk)
|
2019-04-05 19:45:59 +00:00
|
|
|
{
|
2019-04-21 18:38:44 +00:00
|
|
|
disks.emplace(disk->getName(), disk);
|
2019-04-05 19:45:59 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 17:37:27 +00:00
|
|
|
Schema::Volume::Volume(const Poco::Util::AbstractConfiguration & config, const std::string & config_prefix, const DiskSelector & disk_selector)
|
|
|
|
{
|
2019-04-04 17:19:11 +00:00
|
|
|
Poco::Util::AbstractConfiguration::Keys keys;
|
|
|
|
config.keys(config_prefix, keys);
|
|
|
|
|
|
|
|
for (const auto & name : keys)
|
|
|
|
{
|
2019-04-21 18:38:44 +00:00
|
|
|
if (startsWith(name, "disk"))
|
2019-04-05 17:37:27 +00:00
|
|
|
{
|
2019-04-21 18:38:44 +00:00
|
|
|
auto disk_name = config.getString(config_prefix + "." + name);
|
|
|
|
disks.push_back(disk_selector[disk_name]);
|
2019-04-05 17:37:27 +00:00
|
|
|
}
|
2019-04-04 17:19:11 +00:00
|
|
|
}
|
|
|
|
|
2019-04-21 18:38:44 +00:00
|
|
|
if (disks.empty()) {
|
|
|
|
throw Exception("Volume must contain at least one disk", ErrorCodes::EXCESSIVE_ELEMENT_IN_CONFIG);
|
|
|
|
}
|
2019-04-04 17:19:11 +00:00
|
|
|
|
2019-04-21 18:38:44 +00:00
|
|
|
auto has_max_bytes = config.has(config_prefix + ".max_data_part_size_bytes");
|
|
|
|
auto has_max_ratio = config.has(config_prefix + ".max_data_part_size_ratio");
|
|
|
|
if (has_max_bytes && has_max_ratio)
|
2019-04-05 17:37:27 +00:00
|
|
|
{
|
2019-04-21 18:38:44 +00:00
|
|
|
throw Exception("Only one of 'max_data_part_size_bytes' and 'max_data_part_size_ratio' should be specified",
|
|
|
|
ErrorCodes::EXCESSIVE_ELEMENT_IN_CONFIG);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (has_max_bytes) {
|
|
|
|
max_data_part_size = config.getUInt64(config_prefix + ".max_data_part_size_bytes");
|
|
|
|
} else if (has_max_ratio) {
|
|
|
|
auto ratio = config.getDouble(config_prefix + ".max_data_part_size_bytes");
|
|
|
|
if (ratio < 0 and ratio > 1) {
|
|
|
|
throw Exception("'max_data_part_size_bytes' have to be between 0 and 1",
|
|
|
|
ErrorCodes::EXCESSIVE_ELEMENT_IN_CONFIG);
|
2019-04-04 17:19:11 +00:00
|
|
|
}
|
2019-04-21 18:38:44 +00:00
|
|
|
UInt64 sum_size = 0;
|
|
|
|
for (const auto & disk : disks)
|
|
|
|
sum_size += disk->getTotalSpace();
|
|
|
|
max_data_part_size = static_cast<decltype(max_data_part_size)>(sum_size * ratio);
|
|
|
|
} else {
|
|
|
|
max_data_part_size = std::numeric_limits<UInt64>::max();
|
2019-04-04 17:19:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 17:37:27 +00:00
|
|
|
DiskSpaceMonitor::ReservationPtr Schema::Volume::reserve(UInt64 expected_size) const
|
|
|
|
{
|
2019-04-04 17:19:11 +00:00
|
|
|
/// This volume can not store files which size greater than max_data_part_size
|
2019-04-21 18:38:44 +00:00
|
|
|
|
2019-04-05 17:37:27 +00:00
|
|
|
if (expected_size > max_data_part_size)
|
2019-04-04 17:19:11 +00:00
|
|
|
return {};
|
2019-04-05 17:37:27 +00:00
|
|
|
|
2019-04-04 17:19:11 +00:00
|
|
|
size_t start_from = last_used.fetch_add(1u, std::memory_order_relaxed);
|
2019-04-05 17:37:27 +00:00
|
|
|
for (size_t i = 0; i != disks.size(); ++i)
|
|
|
|
{
|
2019-04-04 17:19:11 +00:00
|
|
|
size_t index = (start_from + i) % disks.size();
|
|
|
|
auto reservation = DiskSpaceMonitor::tryToReserve(disks[index], expected_size);
|
2019-04-21 18:38:44 +00:00
|
|
|
|
|
|
|
if (reservation && *reservation)
|
2019-04-04 17:19:11 +00:00
|
|
|
return reservation;
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-04-05 17:37:27 +00:00
|
|
|
UInt64 Schema::Volume::getMaxUnreservedFreeSpace() const
|
|
|
|
{
|
2019-04-04 17:19:11 +00:00
|
|
|
UInt64 res = 0;
|
2019-04-05 17:37:27 +00:00
|
|
|
for (const auto & disk : disks)
|
2019-04-04 17:19:11 +00:00
|
|
|
res = std::max(res, DiskSpaceMonitor::getUnreservedFreeSpace(disk));
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-04-05 17:37:27 +00:00
|
|
|
Schema::Schema(const Poco::Util::AbstractConfiguration & config, const std::string & config_prefix, const DiskSelector & disks)
|
|
|
|
{
|
2019-04-04 17:19:11 +00:00
|
|
|
Poco::Util::AbstractConfiguration::Keys keys;
|
|
|
|
config.keys(config_prefix, keys);
|
|
|
|
|
|
|
|
for (const auto & name : keys)
|
|
|
|
{
|
2019-04-21 18:38:44 +00:00
|
|
|
if (!startsWith(name, "volume"))
|
|
|
|
throw Exception("Unknown element in config: " + config_prefix + "." + name + ", must be 'volume'",
|
2019-04-04 17:19:11 +00:00
|
|
|
ErrorCodes::UNKNOWN_ELEMENT_IN_CONFIG);
|
|
|
|
volumes.emplace_back(config, config_prefix + "." + name, disks);
|
|
|
|
}
|
2019-04-21 18:38:44 +00:00
|
|
|
if (volumes.empty()) {
|
|
|
|
throw Exception("Schema must contain at least one Volume", ErrorCodes::EXCESSIVE_ELEMENT_IN_CONFIG);
|
|
|
|
}
|
2019-04-04 17:19:11 +00:00
|
|
|
}
|
|
|
|
|
2019-04-21 18:38:44 +00:00
|
|
|
Schema::Disks Schema::getDisks() const
|
2019-04-05 17:37:27 +00:00
|
|
|
{
|
2019-04-21 18:38:44 +00:00
|
|
|
Disks res;
|
2019-04-05 17:37:27 +00:00
|
|
|
for (const auto & volume : volumes)
|
|
|
|
for (const auto & disk : volume.disks)
|
2019-04-21 18:38:44 +00:00
|
|
|
res.push_back(disk);
|
2019-04-04 17:19:11 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-04-05 17:37:27 +00:00
|
|
|
UInt64 Schema::getMaxUnreservedFreeSpace() const
|
|
|
|
{
|
2019-04-04 17:19:11 +00:00
|
|
|
UInt64 res = 0;
|
2019-04-05 17:37:27 +00:00
|
|
|
for (const auto & volume : volumes)
|
2019-04-04 17:19:11 +00:00
|
|
|
res = std::max(res, volume.getMaxUnreservedFreeSpace());
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-04-05 17:37:27 +00:00
|
|
|
DiskSpaceMonitor::ReservationPtr Schema::reserve(UInt64 expected_size) const
|
|
|
|
{
|
2019-04-05 19:45:59 +00:00
|
|
|
for (const auto & volume : volumes)
|
2019-04-05 17:37:27 +00:00
|
|
|
{
|
2019-04-04 17:19:11 +00:00
|
|
|
auto reservation = volume.reserve(expected_size);
|
2019-04-05 17:37:27 +00:00
|
|
|
if (reservation)
|
2019-04-04 17:19:11 +00:00
|
|
|
return reservation;
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-04-21 18:38:44 +00:00
|
|
|
SchemaSelector::SchemaSelector(const Poco::Util::AbstractConfiguration & config, const String& config_prefix, const DiskSelector & disks)
|
2019-04-05 19:58:59 +00:00
|
|
|
{
|
2019-04-04 17:19:11 +00:00
|
|
|
Poco::Util::AbstractConfiguration::Keys keys;
|
|
|
|
config.keys(config_prefix, keys);
|
|
|
|
|
|
|
|
for (const auto & name : keys)
|
|
|
|
{
|
2019-04-21 18:38:44 +00:00
|
|
|
if (!isAlphaNumeric(name))
|
|
|
|
throw Exception("Schema name can contain only alphanumeric and '_' (" + name + ")", ErrorCodes::EXCESSIVE_ELEMENT_IN_CONFIG);
|
2019-04-04 17:19:11 +00:00
|
|
|
schemes.emplace(name, Schema{config, config_prefix + "." + name, disks});
|
2019-04-21 18:38:44 +00:00
|
|
|
LOG_INFO(&Logger::get("StatusFile"), "Storage schema " << name << "loaded"); ///@TODO_IGR ASK Logger?
|
2019-04-04 17:19:11 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 12:30:26 +00:00
|
|
|
constexpr auto default_schema_name = "default";
|
2019-04-21 18:38:44 +00:00
|
|
|
constexpr auto default_disk_name = "default";
|
2019-04-05 17:37:27 +00:00
|
|
|
if (schemes.find(default_schema_name) == schemes.end())
|
2019-04-21 18:38:44 +00:00
|
|
|
schemes.emplace(default_schema_name, Schema(Schema::Volumes{{std::vector<DiskPtr>{disks[default_disk_name]},
|
|
|
|
std::numeric_limits<UInt64>::max()}}));
|
2019-04-04 17:19:11 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 17:37:27 +00:00
|
|
|
const Schema & SchemaSelector::operator[](const String & name) const
|
|
|
|
{
|
2019-04-04 17:19:11 +00:00
|
|
|
auto it = schemes.find(name);
|
2019-04-05 17:37:27 +00:00
|
|
|
if (it == schemes.end())
|
2019-04-21 18:38:44 +00:00
|
|
|
throw Exception("Unknown schema " + name, ErrorCodes::UNKNOWN_SCHEMA);
|
2019-04-04 17:19:11 +00:00
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2014-03-13 12:48:07 +00:00
|
|
|
}
|