ClickHouse/dbms/src/Disks/DiskSpaceMonitor.cpp

372 lines
12 KiB
C++
Raw Normal View History

2019-11-27 09:39:44 +00:00
#include "DiskSpaceMonitor.h"
#include "DiskFactory.h"
#include "DiskLocal.h"
#include <Interpreters/Context.h>
2019-09-24 00:45:40 +00:00
#include <Common/escapeForFileName.h>
#include <Common/quoteString.h>
2014-03-13 12:48:07 +00:00
#include <set>
#include <Poco/File.h>
2019-09-24 00:45:40 +00:00
2014-03-13 12:48:07 +00:00
namespace DB
{
2019-11-27 09:39:44 +00:00
DiskSelector::DiskSelector(const Poco::Util::AbstractConfiguration & config, const String & config_prefix, const Context & context)
2019-04-05 17:37:27 +00:00
{
Poco::Util::AbstractConfiguration::Keys keys;
config.keys(config_prefix, keys);
2019-11-27 09:39:44 +00:00
auto & factory = DiskFactory::instance();
constexpr auto default_disk_name = "default";
bool has_default_disk = false;
for (const auto & disk_name : keys)
{
2019-05-11 18:00:43 +00:00
if (!std::all_of(disk_name.begin(), disk_name.end(), isWordCharASCII))
throw Exception("Disk name can contain only alphanumeric and '_' (" + disk_name + ")", ErrorCodes::EXCESSIVE_ELEMENT_IN_CONFIG);
2019-04-05 19:58:59 +00:00
if (disk_name == default_disk_name)
has_default_disk = true;
2019-11-27 09:39:44 +00:00
auto disk_config_prefix = config_prefix + "." + disk_name;
disks.emplace(disk_name, factory.create(disk_name, config, disk_config_prefix, context));
}
if (!has_default_disk)
2019-12-03 13:37:40 +00:00
disks.emplace(default_disk_name, std::make_shared<DiskLocal>(default_disk_name, context.getPath(), 0));
}
const DiskPtr & DiskSelector::operator[](const String & name) const
2019-04-05 17:37:27 +00:00
{
auto it = disks.find(name);
2019-04-05 17:37:27 +00:00
if (it == disks.end())
throw Exception("Unknown disk " + name, ErrorCodes::UNKNOWN_DISK);
return it->second;
}
2019-08-15 17:02:04 +00:00
Volume::Volume(
String name_,
const Poco::Util::AbstractConfiguration & config,
2019-11-27 09:39:44 +00:00
const String & config_prefix,
2019-08-15 17:02:04 +00:00
const DiskSelector & disk_selector)
2019-07-16 18:20:47 +00:00
: name(std::move(name_))
2019-04-05 17:37:27 +00:00
{
Poco::Util::AbstractConfiguration::Keys keys;
config.keys(config_prefix, keys);
Logger * logger = &Logger::get("StorageConfiguration");
for (const auto & disk : keys)
{
if (startsWith(disk, "disk"))
2019-04-05 17:37:27 +00:00
{
auto disk_name = config.getString(config_prefix + "." + disk);
disks.push_back(disk_selector[disk_name]);
2019-04-05 17:37:27 +00:00
}
}
if (disks.empty())
2019-09-04 16:00:20 +00:00
throw Exception("Volume must contain at least one disk.", ErrorCodes::EXCESSIVE_ELEMENT_IN_CONFIG);
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)
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)
{
2019-09-09 13:50:19 +00:00
max_data_part_size = config.getUInt64(config_prefix + ".max_data_part_size_bytes", 0);
}
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.", ErrorCodes::EXCESSIVE_ELEMENT_IN_CONFIG);
UInt64 sum_size = 0;
2019-05-13 20:58:22 +00:00
std::vector<UInt64> sizes;
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());
2019-08-15 17:02:04 +00:00
for (size_t i = 0; i < disks.size(); ++i)
2019-05-13 20:58:22 +00:00
if (sizes[i] < max_data_part_size)
LOG_WARNING(
logger,
"Disk " << backQuote(disks[i]->getName()) << " on volume " << backQuote(config_prefix) << " have not enough space ("
<< formatReadableSizeWithBinarySuffix(sizes[i]) << ") for containing part the size of max_data_part_size ("
<< formatReadableSizeWithBinarySuffix(max_data_part_size) << ")");
}
constexpr UInt64 MIN_PART_SIZE = 8u * 1024u * 1024u;
if (max_data_part_size != 0 && max_data_part_size < MIN_PART_SIZE)
LOG_WARNING(
logger,
"Volume " << backQuote(name) << " max_data_part_size is too low (" << formatReadableSizeWithBinarySuffix(max_data_part_size)
<< " < " << formatReadableSizeWithBinarySuffix(MIN_PART_SIZE) << ")");
}
2019-12-03 13:37:40 +00:00
ReservationPtr Volume::reserve(UInt64 expected_size)
2019-04-05 17:37:27 +00:00
{
/// This volume can not store files which size greater than max_data_part_size
2019-09-09 13:50:19 +00:00
if (max_data_part_size != 0 && expected_size > max_data_part_size)
return {};
2019-04-05 17:37:27 +00:00
size_t start_from = last_used.fetch_add(1u, std::memory_order_relaxed);
2019-05-21 17:57:17 +00:00
size_t disks_num = disks.size();
2019-08-15 17:02:04 +00:00
for (size_t i = 0; i < disks_num; ++i)
2019-04-05 17:37:27 +00:00
{
2019-05-21 17:57:17 +00:00
size_t index = (start_from + i) % disks_num;
auto reservation = disks[index]->reserve(expected_size);
2019-05-22 19:20:10 +00:00
2019-08-15 17:02:04 +00:00
if (reservation)
2019-05-22 19:20:10 +00:00
return reservation;
}
return {};
}
UInt64 Volume::getMaxUnreservedFreeSpace() const
2019-04-05 17:37:27 +00:00
{
UInt64 res = 0;
2019-04-05 17:37:27 +00:00
for (const auto & disk : disks)
res = std::max(res, disk->getUnreservedSpace());
return res;
}
2019-09-04 16:00:20 +00:00
StoragePolicy::StoragePolicy(
String name_,
const Poco::Util::AbstractConfiguration & config,
2019-11-27 09:39:44 +00:00
const String & config_prefix,
2019-09-04 16:00:20 +00:00
const DiskSelector & disks)
: name(std::move(name_))
2019-04-05 17:37:27 +00:00
{
2019-07-16 18:20:47 +00:00
String volumes_prefix = config_prefix + ".volumes";
if (!config.has(volumes_prefix))
2019-09-10 11:21:59 +00:00
throw Exception("StoragePolicy must contain at least one volume (.volumes)", ErrorCodes::EXCESSIVE_ELEMENT_IN_CONFIG);
2019-07-16 18:20:47 +00:00
Poco::Util::AbstractConfiguration::Keys keys;
2019-07-16 18:20:47 +00:00
config.keys(volumes_prefix, keys);
2019-05-22 19:20:10 +00:00
for (const auto & attr_name : keys)
{
2019-07-25 11:42:48 +00:00
if (!std::all_of(attr_name.begin(), attr_name.end(), isWordCharASCII))
throw Exception(
"Volume name can contain only alphanumeric and '_' (" + attr_name + ")", ErrorCodes::EXCESSIVE_ELEMENT_IN_CONFIG);
volumes.push_back(std::make_shared<Volume>(attr_name, config, volumes_prefix + "." + attr_name, disks));
2019-07-25 11:42:48 +00:00
if (volumes_names.find(attr_name) != volumes_names.end())
throw Exception("Volumes names must be unique (" + attr_name + " duplicated)", ErrorCodes::UNKNOWN_POLICY);
2019-07-16 18:20:47 +00:00
volumes_names[attr_name] = volumes.size() - 1;
}
2019-07-16 18:20:47 +00:00
if (volumes.empty())
2019-09-10 11:21:59 +00:00
throw Exception("StoragePolicy must contain at least one volume.", ErrorCodes::EXCESSIVE_ELEMENT_IN_CONFIG);
/// Check that disks are unique in Policy
std::set<String> disk_names;
for (const auto & volume : volumes)
{
for (const auto & disk : volume->disks)
{
if (disk_names.find(disk->getName()) != disk_names.end())
throw Exception(
"Duplicate disk '" + disk->getName() + "' in storage policy '" + name + "'", ErrorCodes::EXCESSIVE_ELEMENT_IN_CONFIG);
disk_names.insert(disk->getName());
}
}
2019-08-01 10:29:14 +00:00
move_factor = config.getDouble(config_prefix + ".move_factor", 0.1);
2019-08-16 09:20:44 +00:00
if (move_factor > 1)
throw Exception("Disk move factor have to be in [0., 1.] interval, but set to " + toString(move_factor), ErrorCodes::LOGICAL_ERROR);
}
2019-08-14 15:20:52 +00:00
StoragePolicy::StoragePolicy(String name_, Volumes volumes_, double move_factor_)
: volumes(std::move(volumes_)), name(std::move(name_)), move_factor(move_factor_)
2019-08-14 15:20:52 +00:00
{
if (volumes.empty())
2019-09-04 16:00:20 +00:00
throw Exception("StoragePolicy must contain at least one Volume.", ErrorCodes::UNKNOWN_POLICY);
2019-08-14 15:20:52 +00:00
2019-08-16 09:20:44 +00:00
if (move_factor > 1)
throw Exception("Disk move factor have to be in [0., 1.] interval, but set to " + toString(move_factor), ErrorCodes::LOGICAL_ERROR);
2019-08-16 09:20:44 +00:00
2019-08-15 17:02:04 +00:00
for (size_t i = 0; i < volumes.size(); ++i)
2019-08-14 15:20:52 +00:00
{
if (volumes_names.find(volumes[i]->getName()) != volumes_names.end())
2019-09-04 16:00:20 +00:00
throw Exception("Volumes names must be unique (" + volumes[i]->getName() + " duplicated).", ErrorCodes::UNKNOWN_POLICY);
2019-08-14 15:20:52 +00:00
volumes_names[volumes[i]->getName()] = i;
}
}
bool StoragePolicy::isDefaultPolicy() const
{
/// Guessing if this policy is default, not 100% correct though.
if (getName() != "default")
return false;
const auto & volumes = getVolumes();
if (volumes.size != 1)
return false;
if (volumes[0]->getName() != "default")
return false;
const auto & disks = volumes[0]->disks;
if (disks.size != 1)
return false;
if (disks[0]->getName() != "default")
return false;
return true;
}
Disks StoragePolicy::getDisks() const
2019-04-05 17:37:27 +00:00
{
Disks res;
2019-04-05 17:37:27 +00:00
for (const auto & volume : volumes)
for (const auto & disk : volume->disks)
res.push_back(disk);
return res;
}
DiskPtr StoragePolicy::getAnyDisk() const
2019-05-13 20:58:22 +00:00
{
/// StoragePolicy must contain at least one Volume
2019-05-13 20:58:22 +00:00
/// Volume must contain at least one Disk
2019-05-22 19:20:10 +00:00
if (volumes.empty())
2019-09-10 11:21:59 +00:00
throw Exception("StoragePolicy has no volumes. It's a bug.", ErrorCodes::NOT_ENOUGH_SPACE);
2019-09-04 16:00:20 +00:00
if (volumes[0]->disks.empty())
2019-09-10 11:21:59 +00:00
throw Exception("Volume '" + volumes[0]->getName() + "' has no disks. It's a bug.", ErrorCodes::NOT_ENOUGH_SPACE);
2019-09-04 16:00:20 +00:00
return volumes[0]->disks[0];
2019-05-13 20:58:22 +00:00
}
2019-06-09 12:31:03 +00:00
DiskPtr StoragePolicy::getDiskByName(const String & disk_name) const
{
2019-06-07 19:16:42 +00:00
for (auto && volume : volumes)
for (auto && disk : volume->disks)
2019-06-07 19:16:42 +00:00
if (disk->getName() == disk_name)
return disk;
return {};
}
UInt64 StoragePolicy::getMaxUnreservedFreeSpace() const
2019-04-05 17:37:27 +00:00
{
UInt64 res = 0;
2019-04-05 17:37:27 +00:00
for (const auto & volume : volumes)
res = std::max(res, volume->getMaxUnreservedFreeSpace());
return res;
}
ReservationPtr StoragePolicy::reserve(UInt64 expected_size, size_t min_volume_index) const
2019-04-05 17:37:27 +00:00
{
2019-07-16 11:07:04 +00:00
for (size_t i = min_volume_index; i < volumes.size(); ++i)
{
2019-06-19 17:56:41 +00:00
const auto & volume = volumes[i];
auto reservation = volume->reserve(expected_size);
2019-04-05 17:37:27 +00:00
if (reservation)
return reservation;
}
return {};
}
ReservationPtr StoragePolicy::reserve(UInt64 expected_size) const
2019-05-22 19:20:10 +00:00
{
return reserve(expected_size, 0);
2019-05-22 19:20:10 +00:00
}
2019-09-06 15:09:20 +00:00
ReservationPtr StoragePolicy::makeEmptyReservationOnLargestDisk() const
{
UInt64 max_space = 0;
DiskPtr max_disk;
for (const auto & volume : volumes)
{
2019-08-15 17:02:04 +00:00
for (const auto & disk : volume->disks)
{
auto avail_space = disk->getAvailableSpace();
if (avail_space > max_space)
{
max_space = avail_space;
max_disk = disk;
}
}
}
return max_disk->reserve(0);
}
2019-09-10 11:21:59 +00:00
size_t StoragePolicy::getVolumeIndexByDisk(const DiskPtr & disk_ptr) const
2019-08-14 15:20:52 +00:00
{
2019-08-15 17:02:04 +00:00
for (size_t i = 0; i < volumes.size(); ++i)
2019-08-14 15:20:52 +00:00
{
const auto & volume = volumes[i];
2019-09-04 16:00:20 +00:00
for (const auto & disk : volume->disks)
2019-08-14 15:20:52 +00:00
if (disk->getName() == disk_ptr->getName())
return i;
}
2019-09-04 16:00:20 +00:00
throw Exception("No disk " + disk_ptr->getName() + " in policy " + name, ErrorCodes::UNKNOWN_DISK);
2019-08-14 15:20:52 +00:00
}
StoragePolicySelector::StoragePolicySelector(
const Poco::Util::AbstractConfiguration & config,
const String & config_prefix,
const DiskSelector & disks)
2019-04-05 19:58:59 +00:00
{
Poco::Util::AbstractConfiguration::Keys keys;
config.keys(config_prefix, keys);
for (const auto & name : keys)
{
2019-05-11 18:00:43 +00:00
if (!std::all_of(name.begin(), name.end(), isWordCharASCII))
throw Exception(
"StoragePolicy name can contain only alphanumeric and '_' (" + name + ")", ErrorCodes::EXCESSIVE_ELEMENT_IN_CONFIG);
2019-08-14 15:20:52 +00:00
policies.emplace(name, std::make_shared<StoragePolicy>(name, config, config_prefix + "." + name, disks));
2019-09-24 00:45:40 +00:00
LOG_INFO(&Logger::get("StoragePolicySelector"), "Storage policy " << backQuote(name) << " loaded");
}
constexpr auto default_storage_policy_name = "default";
2019-07-16 18:20:47 +00:00
constexpr auto default_volume_name = "default";
constexpr auto default_disk_name = "default";
2019-09-04 16:00:20 +00:00
2019-09-10 11:21:59 +00:00
/// Add default policy if it's not specified explicetly
if (policies.find(default_storage_policy_name) == policies.end())
2019-08-14 15:20:52 +00:00
{
auto default_volume = std::make_shared<Volume>(default_volume_name, std::vector<DiskPtr>{disks[default_disk_name]}, 0);
2019-09-09 13:50:19 +00:00
2019-08-14 15:20:52 +00:00
auto default_policy = std::make_shared<StoragePolicy>(default_storage_policy_name, Volumes{default_volume}, 0.0);
policies.emplace(default_storage_policy_name, default_policy);
}
}
2019-09-04 16:00:20 +00:00
const StoragePolicyPtr & StoragePolicySelector::operator[](const String & name) const
2019-04-05 17:37:27 +00:00
{
auto it = policies.find(name);
if (it == policies.end())
throw Exception("Unknown StoragePolicy " + name, ErrorCodes::UNKNOWN_POLICY);
return it->second;
}
2014-03-13 12:48:07 +00:00
}