aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Storages/MergeTree/ReplicatedMergeTreePartCheckThread.h
blob: fc76cbad4ed4b257dcaeb64e15e211c88ed5fa2e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#pragma once

#include <set>
#include <map>
#include <list>
#include <mutex>
#include <thread>
#include <atomic>
#include <boost/noncopyable.hpp>
#include <Poco/Event.h>
#include <base/types.h>
#include <Core/BackgroundSchedulePool.h>
#include <Storages/CheckResults.h>
#include <Storages/MergeTree/IMergeTreeDataPart.h>

namespace DB
{

class StorageReplicatedMergeTree;

struct ReplicatedCheckResult
{
    enum Action
    {
        None,

        Cancelled,
        DoNothing,
        RecheckLater,

        DetachUnexpected,
        TryFetchMissing,
    };

    CheckResult status;
    Action action = None;

    bool exists_in_zookeeper;
    MergeTreeDataPartPtr part;
    time_t recheck_after = 0;
};

/** Checks the integrity of the parts requested for validation.
  *
  * Identifies the extra parts and removes them from the working set.
  * Find the missing parts and add them for download from replicas.
  * Checks the integrity of the data and, in the event of a violation,
  *  removes a part from the working set and adds it for download from replicas.
  */
class ReplicatedMergeTreePartCheckThread
{
public:
    explicit ReplicatedMergeTreePartCheckThread(StorageReplicatedMergeTree & storage_);
    ~ReplicatedMergeTreePartCheckThread();

    /// Processing of the queue to be checked is done in the background thread, which you must first start.
    void start();
    void stop();

    /// Add a part (for which there are suspicions that it is missing, damaged or not needed) in the queue for check.
    /// delay_to_check_seconds - check no sooner than the specified number of seconds.
    void enqueuePart(const String & name, time_t delay_to_check_seconds = 0);

    /// Get the number of parts in the queue for check.
    size_t size() const;

    /// Check part by name
    CheckResult checkPartAndFix(const String & part_name, std::optional<time_t> * recheck_after = nullptr);

    ReplicatedCheckResult checkPartImpl(const String & part_name);

    std::unique_lock<std::mutex> pausePartsCheck();

    /// Can be called only while holding a lock returned from pausePartsCheck()
    void cancelRemovedPartsCheck(const MergeTreePartInfo & drop_range_info);

private:
    void run();

    bool onPartIsLostForever(const String & part_name);

    std::pair<bool, MergeTreeDataPartPtr> findLocalPart(const String & part_name);

    /// Search for missing part on other replicas or covering part on all replicas (including our replica).
    /// Returns false if the part is lost forever.
    bool searchForMissingPartOnOtherReplicas(const String & part_name) const;

    StorageReplicatedMergeTree & storage;
    String log_name;
    Poco::Logger * log;

    using StringSet = std::set<String>;
    using PartToCheck = std::pair<String, time_t>;    /// The name of the part and the minimum time to check (or zero, if not important).
    using PartsToCheckQueue = std::list<PartToCheck>;

    /** Parts for which you want to check one of two:
      *  - If we have the part, check, its data with its checksums, and them with ZooKeeper.
      *  - If we do not have a part, check to see if it (or the part covering it) exists anywhere on another replicas.
      */

    mutable std::mutex parts_mutex;
    StringSet parts_set;
    PartsToCheckQueue parts_queue;

    std::mutex start_stop_mutex;
    std::atomic<bool> need_stop { false };
    BackgroundSchedulePool::TaskHolder task;
};

}