1#pragma once
2
3#include <functional>
4#include <optional>
5#include <vector>
6#include <Disks/DiskSpaceMonitor.h>
7#include <Storages/MergeTree/MergeTreeDataPart.h>
8#include <Common/ActionBlocker.h>
9
10namespace DB
11{
12
13
14/// Active part from storage and destination reservation where
15/// it have to be moved.
16struct MergeTreeMoveEntry
17{
18 std::shared_ptr<const MergeTreeDataPart> part;
19 ReservationPtr reserved_space;
20
21 MergeTreeMoveEntry(const std::shared_ptr<const MergeTreeDataPart> & part_, ReservationPtr reservation_)
22 : part(part_), reserved_space(std::move(reservation_))
23 {
24 }
25};
26
27using MergeTreeMovingParts = std::vector<MergeTreeMoveEntry>;
28
29/** Can select parts for background moves, clone them to appropriate disks into
30 * /detached directory and replace them into active parts set
31 */
32class MergeTreePartsMover
33{
34private:
35 /// Callback tells that part is not participating in background process
36 using AllowedMovingPredicate = std::function<bool(const std::shared_ptr<const MergeTreeDataPart> &, String * reason)>;
37
38public:
39 MergeTreePartsMover(MergeTreeData * data_)
40 : data(data_)
41 , log(&Poco::Logger::get("MergeTreePartsMover"))
42 {
43 }
44
45 /// Select parts for background moves according to storage_policy configuration.
46 /// Returns true if at least one part was selected for move.
47 bool selectPartsForMove(
48 MergeTreeMovingParts & parts_to_move,
49 const AllowedMovingPredicate & can_move,
50 const std::lock_guard<std::mutex> & moving_parts_lock);
51
52 /// Copies part to selected reservation in detached folder. Throws exception if part alredy exists.
53 std::shared_ptr<const MergeTreeDataPart> clonePart(const MergeTreeMoveEntry & moving_part) const;
54
55 /// Replaces cloned part from detached directory into active data parts set.
56 /// Replacing part changes state to DeleteOnDestroy and will be removed from disk after destructor of
57 /// MergeTreeDataPart called. If replacing part doesn't exists or not active (commited) than
58 /// cloned part will be removed and loge message will be reported. It may happen in case of concurrent
59 /// merge or mutation.
60 void swapClonedPart(const std::shared_ptr<const MergeTreeDataPart> & cloned_parts) const;
61
62public:
63 /// Can stop background moves and moves from queries
64 ActionBlocker moves_blocker;
65
66private:
67
68 MergeTreeData * data;
69 Logger * log;
70};
71
72
73}
74