1#pragma once
2
3#include <Core/Types.h>
4#include <Common/ZooKeeper/Types.h>
5#include <Common/ZooKeeper/ZooKeeper.h>
6#include <common/logger_useful.h>
7#include <Core/BackgroundSchedulePool.h>
8#include <thread>
9
10#include <map>
11#include <unordered_map>
12#include <pcg_random.hpp>
13
14
15namespace DB
16{
17
18class StorageReplicatedMergeTree;
19
20
21/** Removes obsolete data from a table of type ReplicatedMergeTree.
22 */
23class ReplicatedMergeTreeCleanupThread
24{
25public:
26 ReplicatedMergeTreeCleanupThread(StorageReplicatedMergeTree & storage_);
27
28 void start() { task->activateAndSchedule(); }
29
30 void wakeup() { task->schedule(); }
31
32 void stop() { task->deactivate(); }
33
34private:
35 StorageReplicatedMergeTree & storage;
36 String log_name;
37 Logger * log;
38 BackgroundSchedulePool::TaskHolder task;
39 pcg64 rng;
40
41 void run();
42 void iterate();
43
44 /// Remove old records from ZooKeeper.
45 void clearOldLogs();
46
47 /// The replica is marked as "lost" if it is inactive and its log pointer
48 /// is far behind and we are not going to keep logs for it.
49 /// Lost replicas will use different strategy for repair.
50 void markLostReplicas(const std::unordered_map<String, UInt32> & host_versions_lost_replicas,
51 const std::unordered_map<String, String> & log_pointers_candidate_lost_replicas,
52 size_t replicas_count, const zkutil::ZooKeeperPtr & zookeeper);
53
54 /// Remove old block hashes from ZooKeeper. This is done by the leader replica.
55 void clearOldBlocks();
56
57 /// Remove old mutations that are done from ZooKeeper. This is done by the leader replica.
58 void clearOldMutations();
59
60 using NodeCTimeCache = std::map<String, Int64>;
61 NodeCTimeCache cached_block_stats;
62
63 struct NodeWithStat;
64 /// Returns list of blocks (with their stat) sorted by ctime in descending order.
65 void getBlocksSortedByTime(zkutil::ZooKeeper & zookeeper, std::vector<NodeWithStat> & timed_blocks);
66
67 /// TODO Removing old quorum/failed_parts
68};
69
70
71}
72