ClickHouse/src/Storages/MergeTree/MergeTreePartsMover.h

74 lines
2.4 KiB
C++
Raw Normal View History

2019-08-19 14:40:12 +00:00
#pragma once
#include <functional>
2019-09-02 11:35:53 +00:00
#include <optional>
2019-11-27 09:39:44 +00:00
#include <vector>
#include <Disks/StoragePolicy.h>
2019-10-10 16:30:30 +00:00
#include <Storages/MergeTree/IMergeTreeDataPart.h>
2019-09-02 11:35:53 +00:00
#include <Common/ActionBlocker.h>
2019-08-19 14:40:12 +00:00
namespace DB
{
2019-09-10 11:21:59 +00:00
/// Active part from storage and destination reservation where
/// it have to be moved.
2019-09-03 11:32:25 +00:00
struct MergeTreeMoveEntry
2019-08-19 14:40:12 +00:00
{
2019-12-19 13:10:57 +00:00
std::shared_ptr<const IMergeTreeDataPart> part;
2019-11-27 09:39:44 +00:00
ReservationPtr reserved_space;
2019-08-19 14:40:12 +00:00
2019-12-19 13:10:57 +00:00
MergeTreeMoveEntry(const std::shared_ptr<const IMergeTreeDataPart> & part_, ReservationPtr reservation_)
2019-09-05 13:12:29 +00:00
: part(part_), reserved_space(std::move(reservation_))
2019-08-19 14:40:12 +00:00
{
}
};
2019-09-03 11:32:25 +00:00
using MergeTreeMovingParts = std::vector<MergeTreeMoveEntry>;
2019-08-19 14:40:12 +00:00
2019-09-10 11:21:59 +00:00
/** Can select parts for background moves, clone them to appropriate disks into
* /detached directory and replace them into active parts set
*/
2019-09-05 13:12:29 +00:00
class MergeTreePartsMover
2019-08-19 14:40:12 +00:00
{
2019-09-05 13:12:29 +00:00
private:
2019-09-10 11:21:59 +00:00
/// Callback tells that part is not participating in background process
2019-10-10 16:30:30 +00:00
using AllowedMovingPredicate = std::function<bool(const std::shared_ptr<const IMergeTreeDataPart> &, String * reason)>;
2019-09-05 13:12:29 +00:00
2019-08-19 14:40:12 +00:00
public:
2019-09-05 13:12:29 +00:00
MergeTreePartsMover(MergeTreeData * data_)
2019-08-19 14:40:12 +00:00
: data(data_)
, log(&Poco::Logger::get("MergeTreePartsMover"))
{
}
2019-09-10 11:21:59 +00:00
/// Select parts for background moves according to storage_policy configuration.
/// Returns true if at least one part was selected for move.
2019-09-05 15:53:23 +00:00
bool selectPartsForMove(
2019-08-19 14:40:12 +00:00
MergeTreeMovingParts & parts_to_move,
2019-09-06 15:09:20 +00:00
const AllowedMovingPredicate & can_move,
const std::lock_guard<std::mutex> & moving_parts_lock);
2019-08-19 14:40:12 +00:00
2020-01-11 09:50:41 +00:00
/// Copies part to selected reservation in detached folder. Throws exception if part already exists.
2019-10-10 16:30:30 +00:00
std::shared_ptr<const IMergeTreeDataPart> clonePart(const MergeTreeMoveEntry & moving_part) const;
2019-08-19 14:40:12 +00:00
2019-09-10 11:21:59 +00:00
/// Replaces cloned part from detached directory into active data parts set.
/// Replacing part changes state to DeleteOnDestroy and will be removed from disk after destructor of
2019-10-10 16:30:30 +00:00
///IMergeTreeDataPart called. If replacing part doesn't exists or not active (commited) than
2019-09-10 11:21:59 +00:00
/// cloned part will be removed and loge message will be reported. It may happen in case of concurrent
/// merge or mutation.
2019-10-10 16:30:30 +00:00
void swapClonedPart(const std::shared_ptr<const IMergeTreeDataPart> & cloned_parts) const;
2019-08-19 14:40:12 +00:00
2019-09-02 11:35:53 +00:00
public:
2019-09-10 12:23:18 +00:00
/// Can stop background moves and moves from queries
2019-09-02 11:35:53 +00:00
ActionBlocker moves_blocker;
2019-08-19 14:40:12 +00:00
private:
2019-09-05 13:12:29 +00:00
MergeTreeData * data;
2020-05-30 21:57:37 +00:00
Poco::Logger * log;
2019-08-19 14:40:12 +00:00
};
2019-09-03 11:32:25 +00:00
2019-08-19 14:40:12 +00:00
}