aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Backups/WithRetries.cpp
blob: 0893c65d8fd1a8cc9ff3122e32bc118650ac0504 (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
#include <mutex>
#include <Backups/WithRetries.h>

namespace DB
{


WithRetries::WithRetries(Poco::Logger * log_, zkutil::GetZooKeeper get_zookeeper_, const KeeperSettings & settings_, RenewerCallback callback_)
    : log(log_)
    , get_zookeeper(get_zookeeper_)
    , settings(settings_)
    , callback(callback_)
    , global_zookeeper_retries_info(
        log->name(),
        log,
        settings.keeper_max_retries,
        settings.keeper_retry_initial_backoff_ms,
        settings.keeper_retry_max_backoff_ms)
{}

WithRetries::RetriesControlHolder::RetriesControlHolder(const WithRetries * parent, const String & name)
    : info(parent->global_zookeeper_retries_info)
    , retries_ctl(name, info, nullptr)
    , faulty_zookeeper(parent->getFaultyZooKeeper())
{}

WithRetries::RetriesControlHolder WithRetries::createRetriesControlHolder(const String & name)
{
    return RetriesControlHolder(this, name);
}

void WithRetries::renewZooKeeper(FaultyKeeper my_faulty_zookeeper) const
{
    std::lock_guard lock(zookeeper_mutex);

    if (!zookeeper || zookeeper->expired())
    {
        zookeeper = get_zookeeper();
        my_faulty_zookeeper->setKeeper(zookeeper);

        callback(my_faulty_zookeeper);
    }
}

WithRetries::FaultyKeeper WithRetries::getFaultyZooKeeper() const
{
    /// We need to create new instance of ZooKeeperWithFaultInjection each time a copy a pointer to ZooKeeper client there
    /// The reason is that ZooKeeperWithFaultInjection may reset the underlying pointer and there could be a race condition
    /// when the same object is used from multiple threads.
    auto faulty_zookeeper = ZooKeeperWithFaultInjection::createInstance(
        settings.keeper_fault_injection_probability,
        settings.keeper_fault_injection_seed,
        zookeeper,
        log->name(),
        log);

    return faulty_zookeeper;
}


}