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
|
|
|
|
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";
|
2019-04-21 20:23:02 +00:00
|
|
|
bool has_default_disk = false;
|
2019-04-05 12:30:26 +00:00
|
|
|
for (const auto & disk_name : keys)
|
2019-04-04 17:19:11 +00:00
|
|
|
{
|
2019-05-11 18:00:43 +00:00
|
|
|
if (!std::all_of(disk_name.begin(), disk_name.end(), isWordCharASCII))
|
2019-04-21 18:38:44 +00:00
|
|
|
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-21 20:23:02 +00:00
|
|
|
has_default_disk = true;
|
2019-04-05 17:37:27 +00:00
|
|
|
if (!path.empty())
|
2019-05-11 18:00:43 +00:00
|
|
|
throw Exception("\"default\" disk path should be provided in <path> not it <storage_configuration>", 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-05-15 16:49:55 +00:00
|
|
|
if (path.back() != '/')
|
|
|
|
throw Exception("Disk path must end with /. 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 20:23:02 +00:00
|
|
|
if (!has_default_disk)
|
|
|
|
disks.emplace(default_disk_name, std::make_shared<const Disk>(default_disk_name, default_path, 0));
|
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 20:23:02 +00:00
|
|
|
if (disks.empty())
|
2019-04-21 18:38:44 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-05-13 20:58:22 +00:00
|
|
|
Logger * logger = &Logger::get("StorageConfiguration");
|
|
|
|
|
2019-04-21 20:23:02 +00:00
|
|
|
if (has_max_bytes)
|
|
|
|
{
|
2019-04-21 18:38:44 +00:00
|
|
|
max_data_part_size = config.getUInt64(config_prefix + ".max_data_part_size_bytes");
|
2019-04-21 20:23:02 +00:00
|
|
|
}
|
|
|
|
else if (has_max_ratio)
|
|
|
|
{
|
2019-05-13 20:58:22 +00:00
|
|
|
auto ratio = config.getDouble(config_prefix + ".max_data_part_size_ratio");
|
|
|
|
if (ratio < 0)
|
|
|
|
throw Exception("'max_data_part_size_ratio' have to be not less then 0",
|
2019-04-21 18:38:44 +00:00
|
|
|
ErrorCodes::EXCESSIVE_ELEMENT_IN_CONFIG);
|
|
|
|
UInt64 sum_size = 0;
|
2019-05-13 20:58:22 +00:00
|
|
|
std::vector<UInt64> sizes;
|
2019-04-21 18:38:44 +00:00
|
|
|
for (const auto & disk : disks)
|
2019-05-13 20:58:22 +00:00
|
|
|
{
|
|
|
|
sizes.push_back(disk->getTotalSpace());
|
|
|
|
sum_size += sizes.back();
|
|
|
|
}
|
|
|
|
max_data_part_size = static_cast<decltype(max_data_part_size)>(sum_size * ratio / disks.size());
|
|
|
|
for (size_t i = 0; i != disks.size(); ++i)
|
|
|
|
if (sizes[i] < max_data_part_size)
|
|
|
|
LOG_WARNING(logger, "Disk " << disks[i]->getName() << " on volume " << config_prefix <<
|
|
|
|
" have not enough space (" << sizes[i] <<
|
|
|
|
") for containing part the size of max_data_part_size (" <<
|
|
|
|
max_data_part_size << ")");
|
2019-04-21 20:23:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-04-21 18:38:44 +00:00
|
|
|
max_data_part_size = std::numeric_limits<UInt64>::max();
|
2019-04-04 17:19:11 +00:00
|
|
|
}
|
2019-05-21 14:06:33 +00:00
|
|
|
constexpr UInt64 SIZE_8MB = 8ull << 20u;
|
|
|
|
if (max_data_part_size < SIZE_8MB)
|
2019-05-15 16:49:55 +00:00
|
|
|
LOG_WARNING(logger, "Volume max_data_part_size is too low (" << max_data_part_size << " < " << SIZE_8MB << ")");
|
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 20:23:02 +00:00
|
|
|
if (volumes.empty())
|
2019-04-21 18:38:44 +00:00
|
|
|
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-05-13 20:58:22 +00:00
|
|
|
DiskPtr Schema::getAnyDisk() const
|
|
|
|
{
|
|
|
|
/// Schema must contain at least one Volume
|
|
|
|
/// Volume must contain at least one Disk
|
|
|
|
return volumes[0].disks[0];
|
|
|
|
}
|
|
|
|
|
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-05-12 14:57:23 +00:00
|
|
|
DiskSpaceMonitor::ReservationPtr Schema::reserveOnMaxDiskWithoutReservation() const
|
|
|
|
{
|
|
|
|
UInt64 max_space = 0;
|
|
|
|
DiskPtr max_disk;
|
|
|
|
for (const auto & volume : volumes)
|
|
|
|
{
|
|
|
|
for (const auto &disk : volume.disks)
|
|
|
|
{
|
|
|
|
auto avail_space = disk->getAvailableSpace();
|
|
|
|
if (avail_space > max_space)
|
|
|
|
{
|
|
|
|
max_space = avail_space;
|
|
|
|
max_disk = disk;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return DiskSpaceMonitor::tryToReserve(max_disk, 0);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2019-05-13 20:58:22 +00:00
|
|
|
Logger * logger = &Logger::get("SchemaSelector");
|
|
|
|
|
2019-04-04 17:19:11 +00:00
|
|
|
for (const auto & name : keys)
|
|
|
|
{
|
2019-05-11 18:00:43 +00:00
|
|
|
if (!std::all_of(name.begin(), name.end(), isWordCharASCII))
|
2019-04-21 18:38:44 +00:00
|
|
|
throw Exception("Schema name can contain only alphanumeric and '_' (" + name + ")", ErrorCodes::EXCESSIVE_ELEMENT_IN_CONFIG);
|
2019-05-11 18:00:43 +00:00
|
|
|
schemas.emplace(name, Schema{config, config_prefix + "." + name, disks});
|
2019-05-21 11:09:57 +00:00
|
|
|
LOG_INFO(logger, "Storage schema " << name << " loaded");
|
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-05-11 18:00:43 +00:00
|
|
|
if (schemas.find(default_schema_name) == schemas.end())
|
|
|
|
schemas.emplace(default_schema_name, Schema(Schema::Volumes{{std::vector<DiskPtr>{disks[default_disk_name]},
|
2019-04-21 18:38:44 +00:00
|
|
|
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-05-11 18:00:43 +00:00
|
|
|
auto it = schemas.find(name);
|
|
|
|
if (it == schemas.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
|
|
|
}
|