aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Storages/MergeTree/ReplicatedMergeTreeAttachThread.h
blob: 222b30b519b10b53d330c4010d2be229a44641c3 (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
#pragma once

#include <thread>
#include <Core/BackgroundSchedulePool.h>
#include <Common/ZooKeeper/ZooKeeper.h>

namespace DB
{

class StorageReplicatedMergeTree;

// Attach table to the existing data.
// Initialize the table by creating all the necessary nodes and do the required checks.
// Initialization is repeated if an operation fails because of a ZK request or connection loss.
class ReplicatedMergeTreeAttachThread
{
public:
    explicit ReplicatedMergeTreeAttachThread(StorageReplicatedMergeTree & storage_);

    ~ReplicatedMergeTreeAttachThread();

    void start() { task->activateAndSchedule(); }

    void shutdown();

    void waitFirstTry() { first_try_done.wait(false); }

    void setSkipSanityChecks(bool skip_sanity_checks_);

    static void checkHasReplicaMetadataInZooKeeper(const zkutil::ZooKeeperPtr & zookeeper, const String & replica_path);

private:
    StorageReplicatedMergeTree & storage;
    BackgroundSchedulePool::TaskHolder task;

    std::string log_name;
    Poco::Logger * log;

    std::atomic<bool> first_try_done{false};

    std::atomic<bool> shutdown_called{false};

    UInt64 retry_period;

    bool skip_sanity_checks{false};

    void run();
    void runImpl();

    void finalizeInitialization();
};

}