aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Common/ZooKeeper/ZooKeeperArgs.cpp
blob: 5d01294e9b080cc2c877d3c650698d786581232d (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <Common/ZooKeeper/ZooKeeperArgs.h>
#include <Common/ZooKeeper/KeeperException.h>
#include <base/find_symbols.h>
#include <base/getFQDNOrHostName.h>
#include <Poco/Util/AbstractConfiguration.h>
#include <Common/isLocalAddress.h>
#include <Common/StringUtils/StringUtils.h>
#include <Poco/String.h>

namespace DB
{
namespace ErrorCodes
{
    extern const int BAD_ARGUMENTS;
}
}

namespace zkutil
{

ZooKeeperArgs::ZooKeeperArgs(const Poco::Util::AbstractConfiguration & config, const String & config_name)
{
    if (config_name == "keeper_server")
        initFromKeeperServerSection(config);
    else
        initFromKeeperSection(config, config_name);

    if (!chroot.empty())
    {
        if (chroot.front() != '/')
            throw KeeperException(
                Coordination::Error::ZBADARGUMENTS,
                "Root path in config file should start with '/', but got {}", chroot);
        if (chroot.back() == '/')
            chroot.pop_back();
    }

    if (session_timeout_ms < 0 || operation_timeout_ms < 0 || connection_timeout_ms < 0)
        throw KeeperException::fromMessage(Coordination::Error::ZBADARGUMENTS, "Timeout cannot be negative");

    /// init get_priority_load_balancing
    get_priority_load_balancing.hostname_differences.resize(hosts.size());
    const String & local_hostname = getFQDNOrHostName();
    for (size_t i = 0; i < hosts.size(); ++i)
    {
        const String & node_host = hosts[i].substr(0, hosts[i].find_last_of(':'));
        get_priority_load_balancing.hostname_differences[i] = DB::getHostNameDifference(local_hostname, node_host);
    }
}

ZooKeeperArgs::ZooKeeperArgs(const String & hosts_string)
{
    splitInto<','>(hosts, hosts_string);
}

void ZooKeeperArgs::initFromKeeperServerSection(const Poco::Util::AbstractConfiguration & config)
{
    static constexpr std::string_view config_name = "keeper_server";

    if (auto key = std::string{config_name} + ".tcp_port_secure";
        config.has(key))
    {
        auto tcp_port_secure = config.getString(key);

        if (tcp_port_secure.empty())
            throw KeeperException::fromMessage(Coordination::Error::ZBADARGUMENTS, "Empty tcp_port_secure in config file");
    }

    bool secure{false};
    std::string tcp_port;
    if (auto tcp_port_secure_key = std::string{config_name} + ".tcp_port_secure";
        config.has(tcp_port_secure_key))
    {
        secure = true;
        tcp_port = config.getString(tcp_port_secure_key);
    }
    else if (auto tcp_port_key = std::string{config_name} + ".tcp_port";
        config.has(tcp_port_key))
    {
        tcp_port = config.getString(tcp_port_key);
    }

    if (tcp_port.empty())
        throw KeeperException::fromMessage(Coordination::Error::ZBADARGUMENTS, "No tcp_port or tcp_port_secure in config file");

    if (auto coordination_key = std::string{config_name} + ".coordination_settings";
        config.has(coordination_key))
    {
        if (auto operation_timeout_key = coordination_key + ".operation_timeout_ms";
            config.has(operation_timeout_key))
            operation_timeout_ms = config.getInt(operation_timeout_key);

        if (auto session_timeout_key = coordination_key + ".session_timeout_ms";
            config.has(session_timeout_key))
            session_timeout_ms = config.getInt(session_timeout_key);
    }

    Poco::Util::AbstractConfiguration::Keys keys;
    std::string raft_configuration_key = std::string{config_name} + ".raft_configuration";
    config.keys(raft_configuration_key, keys);
    for (const auto & key : keys)
    {
        if (startsWith(key, "server"))
            hosts.push_back(
                (secure ? "secure://" : "") + config.getString(raft_configuration_key + "." + key + ".hostname") + ":" + tcp_port);
    }

    static constexpr std::array load_balancing_keys
    {
        ".zookeeper_load_balancing",
        ".keeper_load_balancing"
    };

    for (const auto * load_balancing_key : load_balancing_keys)
    {
        if (auto load_balancing_config = std::string{config_name} + load_balancing_key;
            config.has(load_balancing_config))
        {
            String load_balancing_str = config.getString(load_balancing_config);
            /// Use magic_enum to avoid dependency from dbms (`SettingFieldLoadBalancingTraits::fromString(...)`)
            auto load_balancing = magic_enum::enum_cast<DB::LoadBalancing>(Poco::toUpper(load_balancing_str));
            if (!load_balancing)
                throw DB::Exception(DB::ErrorCodes::BAD_ARGUMENTS, "Unknown load balancing: {}", load_balancing_str);
            get_priority_load_balancing.load_balancing = *load_balancing;
            break;
        }
    }

}

void ZooKeeperArgs::initFromKeeperSection(const Poco::Util::AbstractConfiguration & config, const std::string & config_name)
{
    Poco::Util::AbstractConfiguration::Keys keys;
    config.keys(config_name, keys);

    for (const auto & key : keys)
    {
        if (key.starts_with("node"))
        {
            hosts.push_back(
                (config.getBool(config_name + "." + key + ".secure", false) ? "secure://" : "")
                + config.getString(config_name + "." + key + ".host") + ":" + config.getString(config_name + "." + key + ".port", "2181"));
        }
        else if (key == "session_timeout_ms")
        {
            session_timeout_ms = config.getInt(config_name + "." + key);
        }
        else if (key == "operation_timeout_ms")
        {
            operation_timeout_ms = config.getInt(config_name + "." + key);
        }
        else if (key == "connection_timeout_ms")
        {
            connection_timeout_ms = config.getInt(config_name + "." + key);
        }
        else if (key == "enable_fault_injections_during_startup")
        {
            enable_fault_injections_during_startup = config.getBool(config_name + "." + key);
        }
        else if (key == "send_fault_probability")
        {
            send_fault_probability = config.getDouble(config_name + "." + key);
        }
        else if (key == "recv_fault_probability")
        {
            recv_fault_probability = config.getDouble(config_name + "." + key);
        }
        else if (key == "send_sleep_probability")
        {
            send_sleep_probability = config.getDouble(config_name + "." + key);
        }
        else if (key == "recv_sleep_probability")
        {
            recv_sleep_probability = config.getDouble(config_name + "." + key);
        }
        else if (key == "send_sleep_ms")
        {
            send_sleep_ms = config.getUInt64(config_name + "." + key);
        }
        else if (key == "recv_sleep_ms")
        {
            recv_sleep_ms = config.getUInt64(config_name + "." + key);
        }
        else if (key == "identity")
        {
            identity = config.getString(config_name + "." + key);
            if (!identity.empty())
                auth_scheme = "digest";
        }
        else if (key == "root")
        {
            chroot = config.getString(config_name + "." + key);
        }
        else if (key == "implementation")
        {
            implementation = config.getString(config_name + "." + key);
        }
        else if (key == "zookeeper_load_balancing" || key == "keeper_load_balancing")
        {
            String load_balancing_str = config.getString(config_name + "." + key);
            /// Use magic_enum to avoid dependency from dbms (`SettingFieldLoadBalancingTraits::fromString(...)`)
            auto load_balancing = magic_enum::enum_cast<DB::LoadBalancing>(Poco::toUpper(load_balancing_str));
            if (!load_balancing)
                throw DB::Exception(DB::ErrorCodes::BAD_ARGUMENTS, "Unknown load balancing: {}", load_balancing_str);
            get_priority_load_balancing.load_balancing = *load_balancing;
        }
        else if (key == "fallback_session_lifetime")
        {
            fallback_session_lifetime = SessionLifetimeConfiguration
            {
                .min_sec = config.getUInt(config_name + "." + key + ".min"),
                .max_sec = config.getUInt(config_name + "." + key + ".max"),
            };
        }
        else
            throw KeeperException(Coordination::Error::ZBADARGUMENTS, "Unknown key {} in config file", key);
    }
}

}