2019-11-27 09:39:44 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-05-04 20:15:38 +00:00
|
|
|
#include <Disks/DiskSelector.h>
|
2019-11-28 19:11:49 +00:00
|
|
|
#include <Disks/IDisk.h>
|
2020-05-04 20:15:38 +00:00
|
|
|
#include <Disks/IVolume.h>
|
|
|
|
#include <Disks/VolumeJBOD.h>
|
2020-05-28 05:38:55 +00:00
|
|
|
#include <Disks/VolumeRAID1.h>
|
2020-05-09 21:24:15 +00:00
|
|
|
#include <Disks/SingleDiskVolume.h>
|
2019-11-28 19:11:49 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
2019-11-27 09:39:44 +00:00
|
|
|
#include <Common/CurrentMetrics.h>
|
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <Common/formatReadable.h>
|
2019-11-28 19:11:49 +00:00
|
|
|
#include <common/logger_useful.h>
|
2019-11-27 09:39:44 +00:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
2020-10-20 15:10:24 +00:00
|
|
|
#include <unordered_map>
|
2019-11-27 09:39:44 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
#include <Poco/Util/AbstractConfiguration.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2020-01-09 14:50:34 +00:00
|
|
|
class StoragePolicy;
|
|
|
|
using StoragePolicyPtr = std::shared_ptr<const StoragePolicy>;
|
2019-11-27 09:39:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Contains all information about volumes configuration for Storage.
|
|
|
|
* Can determine appropriate Volume and Disk for each reservation.
|
|
|
|
*/
|
2019-12-03 13:37:40 +00:00
|
|
|
class StoragePolicy
|
2019-11-27 09:39:44 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-01-09 14:50:34 +00:00
|
|
|
StoragePolicy(String name_, const Poco::Util::AbstractConfiguration & config, const String & config_prefix, DiskSelectorPtr disks);
|
2019-11-27 09:39:44 +00:00
|
|
|
|
2020-05-28 05:38:55 +00:00
|
|
|
StoragePolicy(String name_, Volumes volumes_, double move_factor_);
|
2019-11-27 09:39:44 +00:00
|
|
|
|
2020-10-20 15:10:24 +00:00
|
|
|
StoragePolicy(
|
|
|
|
const StoragePolicy & storage_policy,
|
|
|
|
const Poco::Util::AbstractConfiguration & config,
|
|
|
|
const String & config_prefix,
|
|
|
|
DiskSelectorPtr disks
|
|
|
|
);
|
|
|
|
|
2019-12-27 18:58:59 +00:00
|
|
|
bool isDefaultPolicy() const;
|
|
|
|
|
2019-11-27 09:39:44 +00:00
|
|
|
/// Returns disks ordered by volumes priority
|
|
|
|
Disks getDisks() const;
|
|
|
|
|
|
|
|
/// Returns any disk
|
|
|
|
/// Used when it's not important, for example for
|
|
|
|
/// mutations files
|
|
|
|
DiskPtr getAnyDisk() const;
|
|
|
|
|
|
|
|
DiskPtr getDiskByName(const String & disk_name) const;
|
|
|
|
|
|
|
|
/// Get free space from most free disk
|
|
|
|
UInt64 getMaxUnreservedFreeSpace() const;
|
|
|
|
|
2019-12-03 13:37:40 +00:00
|
|
|
const String & getName() const { return name; }
|
2019-11-27 09:39:44 +00:00
|
|
|
|
|
|
|
/// Returns valid reservation or null
|
2019-12-03 13:37:40 +00:00
|
|
|
ReservationPtr reserve(UInt64 bytes) const;
|
2019-11-27 09:39:44 +00:00
|
|
|
|
|
|
|
/// Reserve space on any volume with index > min_volume_index
|
|
|
|
ReservationPtr reserve(UInt64 bytes, size_t min_volume_index) const;
|
|
|
|
|
|
|
|
/// Find volume index, which contains disk
|
|
|
|
size_t getVolumeIndexByDisk(const DiskPtr & disk_ptr) const;
|
|
|
|
|
|
|
|
/// Reserves 0 bytes on disk with max available space
|
|
|
|
/// Do not use this function when it is possible to predict size.
|
|
|
|
ReservationPtr makeEmptyReservationOnLargestDisk() const;
|
|
|
|
|
2020-05-28 05:38:55 +00:00
|
|
|
const Volumes & getVolumes() const { return volumes; }
|
2019-11-27 09:39:44 +00:00
|
|
|
|
|
|
|
/// Returns number [0., 1.] -- fraction of free space on disk
|
|
|
|
/// which should be kept with help of background moves
|
|
|
|
double getMoveFactor() const { return move_factor; }
|
|
|
|
|
2020-10-20 15:10:24 +00:00
|
|
|
/// Get volume by index.
|
|
|
|
VolumePtr getVolume(size_t index) const;
|
2019-11-27 09:39:44 +00:00
|
|
|
|
2020-10-20 15:10:24 +00:00
|
|
|
VolumePtr getVolumeByName(const String & volume_name) const;
|
2019-11-27 09:39:44 +00:00
|
|
|
|
2020-01-09 14:50:34 +00:00
|
|
|
/// Checks if storage policy can be replaced by another one.
|
|
|
|
void checkCompatibleWith(const StoragePolicyPtr & new_storage_policy) const;
|
|
|
|
|
2020-10-22 12:41:01 +00:00
|
|
|
/// Check if we have any volume with stopped merges
|
|
|
|
bool hasAnyVolumeWithDisabledMerges() const;
|
|
|
|
|
2019-11-27 09:39:44 +00:00
|
|
|
private:
|
2020-05-28 05:38:55 +00:00
|
|
|
Volumes volumes;
|
2019-11-27 09:39:44 +00:00
|
|
|
const String name;
|
2020-10-20 15:10:24 +00:00
|
|
|
std::unordered_map<String, size_t> volume_index_by_volume_name;
|
|
|
|
std::unordered_map<String, size_t> volume_index_by_disk_name;
|
2019-11-27 09:39:44 +00:00
|
|
|
|
|
|
|
/// move_factor from interval [0., 1.]
|
|
|
|
/// We move something if disk from this policy
|
|
|
|
/// filled more than total_size * move_factor
|
|
|
|
double move_factor = 0.1; /// by default move factor is 10%
|
2020-10-20 15:10:24 +00:00
|
|
|
|
|
|
|
void buildVolumeIndices();
|
2019-11-27 09:39:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-01-09 14:50:34 +00:00
|
|
|
class StoragePolicySelector;
|
|
|
|
using StoragePolicySelectorPtr = std::shared_ptr<const StoragePolicySelector>;
|
2020-05-22 10:33:57 +00:00
|
|
|
using StoragePoliciesMap = std::map<String, StoragePolicyPtr>;
|
2019-11-27 09:39:44 +00:00
|
|
|
|
|
|
|
/// Parse .xml configuration and store information about policies
|
|
|
|
/// Mostly used for introspection.
|
|
|
|
class StoragePolicySelector
|
|
|
|
{
|
|
|
|
public:
|
2020-01-09 14:50:34 +00:00
|
|
|
StoragePolicySelector(const Poco::Util::AbstractConfiguration & config, const String & config_prefix, DiskSelectorPtr disks);
|
|
|
|
|
|
|
|
StoragePolicySelectorPtr updateFromConfig(const Poco::Util::AbstractConfiguration & config, const String & config_prefix, DiskSelectorPtr disks) const;
|
2019-11-27 09:39:44 +00:00
|
|
|
|
|
|
|
/// Policy by name
|
2020-01-09 14:50:34 +00:00
|
|
|
StoragePolicyPtr get(const String & name) const;
|
2019-11-27 09:39:44 +00:00
|
|
|
|
|
|
|
/// All policies
|
2020-05-22 10:33:57 +00:00
|
|
|
const StoragePoliciesMap & getPoliciesMap() const { return policies; }
|
2019-11-27 09:39:44 +00:00
|
|
|
|
|
|
|
private:
|
2020-05-22 10:33:57 +00:00
|
|
|
StoragePoliciesMap policies;
|
2019-11-27 09:39:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|