2020-05-04 20:15:38 +00:00
|
|
|
#include "IVolume.h"
|
|
|
|
|
|
|
|
#include <Common/StringUtils/StringUtils.h>
|
|
|
|
#include <Common/quoteString.h>
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-08-21 15:44:29 +00:00
|
|
|
extern const int EXCESSIVE_ELEMENT_IN_CONFIG;
|
2020-05-24 16:32:58 +00:00
|
|
|
extern const int INCONSISTENT_RESERVATIONS;
|
|
|
|
extern const int NO_RESERVATIONS_PROVIDED;
|
2020-06-23 09:47:36 +00:00
|
|
|
extern const int UNKNOWN_VOLUME_TYPE;
|
2020-05-28 05:38:55 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 15:59:09 +00:00
|
|
|
String volumeTypeToString(VolumeType type)
|
2020-05-28 05:40:04 +00:00
|
|
|
{
|
2020-07-27 15:59:09 +00:00
|
|
|
switch (type)
|
2020-05-28 05:40:04 +00:00
|
|
|
{
|
2020-05-28 05:38:55 +00:00
|
|
|
case VolumeType::JBOD:
|
|
|
|
return "JBOD";
|
|
|
|
case VolumeType::RAID1:
|
|
|
|
return "RAID1";
|
|
|
|
case VolumeType::SINGLE_DISK:
|
|
|
|
return "SINGLE_DISK";
|
|
|
|
case VolumeType::UNKNOWN:
|
|
|
|
return "UNKNOWN";
|
|
|
|
}
|
2020-06-23 09:47:36 +00:00
|
|
|
throw Exception("Unknown volume type, please add it to DB::volumeTypeToString", ErrorCodes::UNKNOWN_VOLUME_TYPE);
|
2020-05-04 20:15:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IVolume::IVolume(
|
2020-07-30 10:04:49 +00:00
|
|
|
String name_,
|
|
|
|
const Poco::Util::AbstractConfiguration & config,
|
|
|
|
const String & config_prefix,
|
|
|
|
DiskSelectorPtr disk_selector)
|
2020-05-04 20:15:38 +00:00
|
|
|
: name(std::move(name_))
|
|
|
|
{
|
|
|
|
Poco::Util::AbstractConfiguration::Keys keys;
|
|
|
|
config.keys(config_prefix, keys);
|
|
|
|
|
|
|
|
for (const auto & disk : keys)
|
|
|
|
{
|
|
|
|
if (startsWith(disk, "disk"))
|
|
|
|
{
|
|
|
|
auto disk_name = config.getString(config_prefix + "." + disk);
|
|
|
|
disks.push_back(disk_selector->get(disk_name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (disks.empty())
|
2020-08-21 15:44:29 +00:00
|
|
|
throw Exception("Volume must contain at least one disk.", ErrorCodes::EXCESSIVE_ELEMENT_IN_CONFIG);
|
2020-05-04 20:15:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UInt64 IVolume::getMaxUnreservedFreeSpace() const
|
|
|
|
{
|
|
|
|
UInt64 res = 0;
|
|
|
|
for (const auto & disk : disks)
|
|
|
|
res = std::max(res, disk->getUnreservedSpace());
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-07-27 15:59:09 +00:00
|
|
|
MultiDiskReservation::MultiDiskReservation(Reservations & reservations_, UInt64 size_)
|
2020-05-24 16:32:58 +00:00
|
|
|
: reservations(std::move(reservations_))
|
|
|
|
, size(size_)
|
|
|
|
{
|
2020-05-25 08:42:59 +00:00
|
|
|
if (reservations.empty())
|
2020-05-24 16:32:58 +00:00
|
|
|
{
|
|
|
|
throw Exception("At least one reservation must be provided to MultiDiskReservation", ErrorCodes::NO_RESERVATIONS_PROVIDED);
|
|
|
|
}
|
|
|
|
|
2020-07-27 15:59:09 +00:00
|
|
|
for (auto & reservation : reservations)
|
2020-05-24 16:32:58 +00:00
|
|
|
{
|
2020-07-27 15:59:09 +00:00
|
|
|
if (reservation->getSize() != size_)
|
2020-05-24 16:32:58 +00:00
|
|
|
{
|
|
|
|
throw Exception("Reservations must have same size", ErrorCodes::INCONSISTENT_RESERVATIONS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Disks MultiDiskReservation::getDisks() const
|
|
|
|
{
|
|
|
|
Disks res;
|
|
|
|
res.reserve(reservations.size());
|
2020-07-22 22:05:45 +00:00
|
|
|
for (const auto & reservation : reservations)
|
2020-05-24 16:32:58 +00:00
|
|
|
{
|
2020-07-22 22:05:45 +00:00
|
|
|
res.push_back(reservation->getDisk());
|
2020-05-24 16:32:58 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiDiskReservation::update(UInt64 new_size)
|
|
|
|
{
|
2020-07-27 15:59:09 +00:00
|
|
|
for (auto & reservation : reservations)
|
2020-05-24 16:32:58 +00:00
|
|
|
{
|
2020-07-27 15:59:09 +00:00
|
|
|
reservation->update(new_size);
|
2020-05-24 16:32:58 +00:00
|
|
|
}
|
|
|
|
size = new_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-04 20:15:38 +00:00
|
|
|
}
|