2020-05-04 20:15:38 +00:00
|
|
|
#include "StoragePolicy.h"
|
2019-11-27 09:39:44 +00:00
|
|
|
#include "DiskFactory.h"
|
|
|
|
#include "DiskLocal.h"
|
2019-11-28 19:11:49 +00:00
|
|
|
|
|
|
|
#include <Interpreters/Context.h>
|
2019-09-24 00:45:40 +00:00
|
|
|
#include <Common/escapeForFileName.h>
|
2019-10-08 18:42:22 +00:00
|
|
|
#include <Common/quoteString.h>
|
2014-03-13 12:48:07 +00:00
|
|
|
|
2019-05-24 19:03:07 +00:00
|
|
|
#include <set>
|
2020-01-09 14:50:34 +00:00
|
|
|
|
2019-04-05 19:45:59 +00:00
|
|
|
#include <Poco/File.h>
|
2019-04-05 12:30:26 +00:00
|
|
|
|
2019-09-24 00:45:40 +00:00
|
|
|
|
2014-03-13 12:48:07 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
2020-02-25 12:19:47 +00:00
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-03-05 08:21:53 +00:00
|
|
|
extern const int BAD_ARGUMENTS;
|
2020-02-25 12:19:47 +00:00
|
|
|
extern const int EXCESSIVE_ELEMENT_IN_CONFIG;
|
|
|
|
extern const int UNKNOWN_DISK;
|
|
|
|
extern const int UNKNOWN_POLICY;
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
|
|
|
|
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,
|
2020-01-09 14:50:34 +00:00
|
|
|
DiskSelectorPtr disks)
|
2019-09-04 16:00:20 +00:00
|
|
|
: 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
|
|
|
|
2019-04-04 17:19:11 +00:00
|
|
|
Poco::Util::AbstractConfiguration::Keys keys;
|
2019-07-16 18:20:47 +00:00
|
|
|
config.keys(volumes_prefix, keys);
|
2019-04-04 17:19:11 +00:00
|
|
|
|
2019-05-22 19:20:10 +00:00
|
|
|
for (const auto & attr_name : keys)
|
2019-04-04 17:19:11 +00:00
|
|
|
{
|
2019-07-25 11:42:48 +00:00
|
|
|
if (!std::all_of(attr_name.begin(), attr_name.end(), isWordCharASCII))
|
2019-11-28 19:11:49 +00:00
|
|
|
throw Exception(
|
|
|
|
"Volume name can contain only alphanumeric and '_' (" + attr_name + ")", ErrorCodes::EXCESSIVE_ELEMENT_IN_CONFIG);
|
2020-05-04 20:15:38 +00:00
|
|
|
volumes.push_back(std::make_shared<VolumeJBOD>(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-04-04 17:19:11 +00:00
|
|
|
}
|
2019-07-16 18:20:47 +00:00
|
|
|
|
2019-04-21 20:23:02 +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);
|
2019-05-24 19:03:07 +00:00
|
|
|
|
|
|
|
/// Check that disks are unique in Policy
|
|
|
|
std::set<String> disk_names;
|
|
|
|
for (const auto & volume : volumes)
|
|
|
|
{
|
2020-05-09 21:24:15 +00:00
|
|
|
for (const auto & disk : volume->getDisks())
|
2019-05-24 19:03:07 +00:00
|
|
|
{
|
|
|
|
if (disk_names.find(disk->getName()) != disk_names.end())
|
2019-11-28 19:11:49 +00:00
|
|
|
throw Exception(
|
|
|
|
"Duplicate disk '" + disk->getName() + "' in storage policy '" + name + "'", ErrorCodes::EXCESSIVE_ELEMENT_IN_CONFIG);
|
2019-05-24 19:03:07 +00:00
|
|
|
|
|
|
|
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)
|
2019-11-28 19:11:49 +00:00
|
|
|
throw Exception("Disk move factor have to be in [0., 1.] interval, but set to " + toString(move_factor), ErrorCodes::LOGICAL_ERROR);
|
2019-04-04 17:19:11 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 13:34:17 +00:00
|
|
|
|
2020-05-04 20:15:38 +00:00
|
|
|
StoragePolicy::StoragePolicy(String name_, VolumesJBOD volumes_, double move_factor_)
|
2019-11-28 19:11:49 +00:00
|
|
|
: 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)
|
2019-11-28 19:11:49 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-27 18:58:59 +00:00
|
|
|
bool StoragePolicy::isDefaultPolicy() const
|
|
|
|
{
|
|
|
|
/// Guessing if this policy is default, not 100% correct though.
|
|
|
|
|
|
|
|
if (getName() != "default")
|
|
|
|
return false;
|
|
|
|
|
2019-12-27 19:44:54 +00:00
|
|
|
if (volumes.size() != 1)
|
2019-12-27 18:58:59 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (volumes[0]->getName() != "default")
|
|
|
|
return false;
|
|
|
|
|
2020-05-09 21:24:15 +00:00
|
|
|
const auto & disks = volumes[0]->getDisks();
|
2019-12-27 19:44:54 +00:00
|
|
|
if (disks.size() != 1)
|
2019-12-27 18:58:59 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (disks[0]->getName() != "default")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-23 13:34:17 +00:00
|
|
|
Disks StoragePolicy::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)
|
2020-05-09 21:24:15 +00:00
|
|
|
for (const auto & disk : volume->getDisks())
|
2019-04-21 18:38:44 +00:00
|
|
|
res.push_back(disk);
|
2019-04-04 17:19:11 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-07-23 13:34:17 +00:00
|
|
|
|
2019-05-24 19:03:07 +00:00
|
|
|
DiskPtr StoragePolicy::getAnyDisk() const
|
2019-05-13 20:58:22 +00:00
|
|
|
{
|
2019-05-24 19:03:07 +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())
|
2020-02-25 12:19:47 +00:00
|
|
|
throw Exception("StoragePolicy has no volumes. It's a bug.", ErrorCodes::LOGICAL_ERROR);
|
2019-09-04 16:00:20 +00:00
|
|
|
|
2020-05-09 21:24:15 +00:00
|
|
|
if (volumes[0]->getDisks().empty())
|
2020-02-25 12:19:47 +00:00
|
|
|
throw Exception("Volume '" + volumes[0]->getName() + "' has no disks. It's a bug.", ErrorCodes::LOGICAL_ERROR);
|
2019-09-04 16:00:20 +00:00
|
|
|
|
2020-05-09 21:24:15 +00:00
|
|
|
return volumes[0]->getDisks()[0];
|
2019-05-13 20:58:22 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 13:34:17 +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)
|
2020-05-09 21:24:15 +00:00
|
|
|
for (auto && disk : volume->getDisks())
|
2019-06-07 19:16:42 +00:00
|
|
|
if (disk->getName() == disk_name)
|
|
|
|
return disk;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-07-23 13:34:17 +00:00
|
|
|
|
2019-05-24 19:03:07 +00:00
|
|
|
UInt64 StoragePolicy::getMaxUnreservedFreeSpace() const
|
2019-04-05 17:37:27 +00:00
|
|
|
{
|
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-07-23 13:34:17 +00:00
|
|
|
res = std::max(res, volume->getMaxUnreservedFreeSpace());
|
2019-04-04 17:19:11 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-07-23 13:34:17 +00:00
|
|
|
|
2020-03-08 22:38:12 +00:00
|
|
|
ReservationPtr StoragePolicy::reserve(UInt64 bytes, 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];
|
2020-03-08 22:38:12 +00:00
|
|
|
auto reservation = volume->reserve(bytes);
|
2019-04-05 17:37:27 +00:00
|
|
|
if (reservation)
|
2019-04-04 17:19:11 +00:00
|
|
|
return reservation;
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-07-23 13:34:17 +00:00
|
|
|
|
2020-03-08 22:38:12 +00:00
|
|
|
ReservationPtr StoragePolicy::reserve(UInt64 bytes) const
|
2019-05-22 19:20:10 +00:00
|
|
|
{
|
2020-03-08 22:38:12 +00:00
|
|
|
return reserve(bytes, 0);
|
2019-05-22 19:20:10 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 13:34:17 +00:00
|
|
|
|
2019-09-06 15:09:20 +00:00
|
|
|
ReservationPtr StoragePolicy::makeEmptyReservationOnLargestDisk() const
|
2019-05-12 14:57:23 +00:00
|
|
|
{
|
|
|
|
UInt64 max_space = 0;
|
|
|
|
DiskPtr max_disk;
|
|
|
|
for (const auto & volume : volumes)
|
|
|
|
{
|
2020-05-09 21:24:15 +00:00
|
|
|
for (const auto & disk : volume->getDisks())
|
2019-05-12 14:57:23 +00:00
|
|
|
{
|
|
|
|
auto avail_space = disk->getAvailableSpace();
|
|
|
|
if (avail_space > max_space)
|
|
|
|
{
|
|
|
|
max_space = avail_space;
|
|
|
|
max_disk = disk;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-23 13:34:17 +00:00
|
|
|
return max_disk->reserve(0);
|
2019-05-12 14:57:23 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 13:34:17 +00:00
|
|
|
|
2020-01-09 14:50:34 +00:00
|
|
|
void StoragePolicy::checkCompatibleWith(const StoragePolicyPtr & new_storage_policy) const
|
|
|
|
{
|
|
|
|
std::unordered_set<String> new_volume_names;
|
|
|
|
for (const auto & volume : new_storage_policy->getVolumes())
|
|
|
|
new_volume_names.insert(volume->getName());
|
|
|
|
|
|
|
|
for (const auto & volume : getVolumes())
|
|
|
|
{
|
|
|
|
if (new_volume_names.count(volume->getName()) == 0)
|
|
|
|
throw Exception("New storage policy shall contain volumes of old one", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
|
|
std::unordered_set<String> new_disk_names;
|
2020-05-09 21:24:15 +00:00
|
|
|
for (const auto & disk : new_storage_policy->getVolumeByName(volume->getName())->getDisks())
|
2020-01-09 14:50:34 +00:00
|
|
|
new_disk_names.insert(disk->getName());
|
|
|
|
|
2020-05-09 21:24:15 +00:00
|
|
|
for (const auto & disk : volume->getDisks())
|
2020-01-09 14:50:34 +00:00
|
|
|
if (new_disk_names.count(disk->getName()) == 0)
|
|
|
|
throw Exception("New storage policy shall contain disks of old one", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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];
|
2020-05-09 21:24:15 +00:00
|
|
|
for (const auto & disk : volume->getDisks())
|
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,
|
2020-01-09 14:50:34 +00:00
|
|
|
DiskSelectorPtr 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-05-11 18:00:43 +00:00
|
|
|
if (!std::all_of(name.begin(), name.end(), isWordCharASCII))
|
2019-11-28 19:11:49 +00:00
|
|
|
throw Exception(
|
|
|
|
"StoragePolicy name can contain only alphanumeric and '_' (" + name + ")", ErrorCodes::EXCESSIVE_ELEMENT_IN_CONFIG);
|
2019-08-14 15:20:52 +00:00
|
|
|
|
2019-05-24 19:03:07 +00:00
|
|
|
policies.emplace(name, std::make_shared<StoragePolicy>(name, config, config_prefix + "." + name, disks));
|
2020-05-30 21:57:37 +00:00
|
|
|
LOG_INFO(&Poco::Logger::get("StoragePolicySelector"), "Storage policy {} loaded", backQuote(name));
|
2019-04-04 17:19:11 +00:00
|
|
|
}
|
|
|
|
|
2019-05-24 19:03:07 +00:00
|
|
|
constexpr auto default_storage_policy_name = "default";
|
2019-07-16 18:20:47 +00:00
|
|
|
constexpr auto default_volume_name = "default";
|
2019-04-21 18:38:44 +00:00
|
|
|
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
|
2019-05-24 19:03:07 +00:00
|
|
|
if (policies.find(default_storage_policy_name) == policies.end())
|
2019-08-14 15:20:52 +00:00
|
|
|
{
|
2020-05-04 20:15:38 +00:00
|
|
|
auto default_volume = std::make_shared<VolumeJBOD>(default_volume_name, std::vector<DiskPtr>{disks->get(default_disk_name)}, 0);
|
2019-09-09 13:50:19 +00:00
|
|
|
|
2020-05-04 20:15:38 +00:00
|
|
|
auto default_policy = std::make_shared<StoragePolicy>(default_storage_policy_name, VolumesJBOD{default_volume}, 0.0);
|
2019-08-14 15:20:52 +00:00
|
|
|
policies.emplace(default_storage_policy_name, default_policy);
|
|
|
|
}
|
2019-04-04 17:19:11 +00:00
|
|
|
}
|
|
|
|
|
2020-01-09 14:50:34 +00:00
|
|
|
|
|
|
|
StoragePolicySelectorPtr StoragePolicySelector::updateFromConfig(const Poco::Util::AbstractConfiguration & config, const String & config_prefix, DiskSelectorPtr disks) const
|
|
|
|
{
|
|
|
|
Poco::Util::AbstractConfiguration::Keys keys;
|
|
|
|
config.keys(config_prefix, keys);
|
|
|
|
|
|
|
|
std::shared_ptr<StoragePolicySelector> result = std::make_shared<StoragePolicySelector>(config, config_prefix, disks);
|
|
|
|
|
2020-03-05 21:03:59 +00:00
|
|
|
constexpr auto default_storage_policy_name = "default";
|
|
|
|
|
2020-01-09 14:50:34 +00:00
|
|
|
for (const auto & [name, policy] : policies)
|
|
|
|
{
|
2020-03-05 21:03:59 +00:00
|
|
|
if (name != default_storage_policy_name && result->policies.count(name) == 0)
|
2020-01-09 14:50:34 +00:00
|
|
|
throw Exception("Storage policy " + backQuote(name) + " is missing in new configuration", ErrorCodes::BAD_ARGUMENTS);
|
|
|
|
|
|
|
|
policy->checkCompatibleWith(result->policies[name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
StoragePolicyPtr StoragePolicySelector::get(const String & name) const
|
2019-04-05 17:37:27 +00:00
|
|
|
{
|
2019-05-24 19:03:07 +00:00
|
|
|
auto it = policies.find(name);
|
|
|
|
if (it == policies.end())
|
|
|
|
throw Exception("Unknown StoragePolicy " + name, ErrorCodes::UNKNOWN_POLICY);
|
2020-01-09 14:50:34 +00:00
|
|
|
|
2019-04-04 17:19:11 +00:00
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2014-03-13 12:48:07 +00:00
|
|
|
}
|