2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/Cluster.h>
|
2019-06-28 18:06:38 +00:00
|
|
|
#include <common/SimpleCache.h>
|
2018-04-19 13:56:14 +00:00
|
|
|
#include <Common/DNSResolver.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/escapeForFileName.h>
|
|
|
|
#include <Common/isLocalAddress.h>
|
2018-01-15 19:07:47 +00:00
|
|
|
#include <Common/StringUtils/StringUtils.h>
|
2017-12-28 04:28:05 +00:00
|
|
|
#include <Common/parseAddress.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/HexWriteBuffer.h>
|
2017-04-13 16:12:56 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
2017-07-26 19:31:32 +00:00
|
|
|
#include <IO/ReadHelpers.h>
|
2013-12-07 16:51:29 +00:00
|
|
|
#include <Poco/Util/AbstractConfiguration.h>
|
|
|
|
#include <Poco/Util/Application.h>
|
2020-01-07 10:26:16 +00:00
|
|
|
#include <ext/range.h>
|
2020-09-12 22:27:44 +00:00
|
|
|
#include <boost/range/algorithm_ext/erase.hpp>
|
2013-12-07 16:51:29 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-12 02:21:15 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int UNKNOWN_ELEMENT_IN_CONFIG;
|
|
|
|
extern const int EXCESSIVE_ELEMENT_IN_CONFIG;
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
extern const int SHARD_HAS_NO_CONNECTIONS;
|
2017-07-26 19:31:32 +00:00
|
|
|
extern const int SYNTAX_ERROR;
|
2016-01-12 02:21:15 +00:00
|
|
|
}
|
|
|
|
|
2015-10-20 14:59:29 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2016-08-22 20:34:21 +00:00
|
|
|
/// Default shard weight.
|
2020-03-08 23:48:08 +00:00
|
|
|
constexpr UInt32 default_weight = 1;
|
2015-10-20 14:59:29 +00:00
|
|
|
|
2019-07-08 01:43:41 +00:00
|
|
|
inline bool isLocalImpl(const Cluster::Address & address, const Poco::Net::SocketAddress & resolved_address, UInt16 clickhouse_port)
|
2015-10-20 14:59:29 +00:00
|
|
|
{
|
2019-07-08 01:43:41 +00:00
|
|
|
/// If there is replica, for which:
|
2017-04-01 07:20:54 +00:00
|
|
|
/// - its port is the same that the server is listening;
|
|
|
|
/// - its host is resolved to set of addresses, one of which is the same as one of addresses of network interfaces of the server machine*;
|
|
|
|
/// then we must go to this shard without any inter-process communication.
|
|
|
|
///
|
|
|
|
/// * - this criteria is somewhat approximate.
|
|
|
|
///
|
|
|
|
/// Also, replica is considered non-local, if it has default database set
|
|
|
|
/// (only reason is to avoid query rewrite).
|
|
|
|
|
2018-03-29 20:21:01 +00:00
|
|
|
return address.default_database.empty() && isLocalAddress(resolved_address, clickhouse_port);
|
2015-10-20 14:59:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-22 20:34:21 +00:00
|
|
|
/// Implementation of Cluster::Address class
|
2015-10-16 16:10:10 +00:00
|
|
|
|
2019-07-08 01:43:41 +00:00
|
|
|
std::optional<Poco::Net::SocketAddress> Cluster::Address::getResolvedAddress() const
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return DNSResolver::instance().resolveAddress(host_name, port);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
/// Failure in DNS resolution in cluster initialization is Ok.
|
|
|
|
tryLogCurrentException("Cluster");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Cluster::Address::isLocal(UInt16 clickhouse_port) const
|
2013-12-07 16:51:29 +00:00
|
|
|
{
|
2019-07-08 01:43:41 +00:00
|
|
|
if (auto resolved = getResolvedAddress())
|
|
|
|
return isLocalImpl(*this, *resolved, clickhouse_port);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-07 14:38:35 +00:00
|
|
|
|
2020-07-05 00:35:57 +00:00
|
|
|
Cluster::Address::Address(
|
2020-09-14 21:55:43 +00:00
|
|
|
const Poco::Util::AbstractConfiguration & config,
|
|
|
|
const String & config_prefix,
|
|
|
|
const String & cluster_,
|
|
|
|
const String & cluster_secret_,
|
|
|
|
UInt32 shard_index_,
|
|
|
|
UInt32 replica_index_)
|
|
|
|
: cluster(cluster_)
|
|
|
|
, cluster_secret(cluster_secret_)
|
|
|
|
, shard_index(shard_index_)
|
|
|
|
, replica_index(replica_index_)
|
2019-07-08 01:43:41 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
host_name = config.getString(config_prefix + ".host");
|
2017-08-07 17:01:04 +00:00
|
|
|
port = static_cast<UInt16>(config.getInt(config_prefix + ".port"));
|
2018-12-28 17:11:52 +00:00
|
|
|
if (config.has(config_prefix + ".user"))
|
|
|
|
user_specified = true;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
user = config.getString(config_prefix + ".user", "default");
|
|
|
|
password = config.getString(config_prefix + ".password", "");
|
|
|
|
default_database = config.getString(config_prefix + ".default_database", "");
|
2018-03-29 01:41:06 +00:00
|
|
|
secure = config.getBool(config_prefix + ".secure", false) ? Protocol::Secure::Enable : Protocol::Secure::Disable;
|
|
|
|
compression = config.getBool(config_prefix + ".compression", true) ? Protocol::Compression::Enable : Protocol::Compression::Disable;
|
2020-06-27 06:52:10 +00:00
|
|
|
priority = config.getInt(config_prefix + ".priority", 1);
|
2019-12-19 19:39:49 +00:00
|
|
|
const char * port_type = secure == Protocol::Secure::Enable ? "tcp_port_secure" : "tcp_port";
|
|
|
|
is_local = isLocal(config.getInt(port_type, 0));
|
2014-08-12 13:46:46 +00:00
|
|
|
}
|
2014-02-22 21:50:27 +00:00
|
|
|
|
2015-10-16 16:10:10 +00:00
|
|
|
|
2020-09-14 21:55:43 +00:00
|
|
|
Cluster::Address::Address(
|
|
|
|
const String & host_port_,
|
|
|
|
const String & user_,
|
|
|
|
const String & password_,
|
|
|
|
UInt16 clickhouse_port,
|
|
|
|
bool secure_,
|
|
|
|
Int64 priority_)
|
|
|
|
: user(user_)
|
|
|
|
, password(password_)
|
2014-02-22 21:50:27 +00:00
|
|
|
{
|
2017-12-28 04:28:05 +00:00
|
|
|
auto parsed_host_port = parseAddress(host_port_, clickhouse_port);
|
2018-01-11 18:55:31 +00:00
|
|
|
host_name = parsed_host_port.first;
|
|
|
|
port = parsed_host_port.second;
|
2019-01-17 17:55:44 +00:00
|
|
|
secure = secure_ ? Protocol::Secure::Enable : Protocol::Secure::Disable;
|
2020-06-27 06:52:10 +00:00
|
|
|
priority = priority_;
|
2019-07-08 01:43:41 +00:00
|
|
|
is_local = isLocal(clickhouse_port);
|
2014-02-22 21:50:27 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 11:49:17 +00:00
|
|
|
|
2017-04-13 16:12:56 +00:00
|
|
|
String Cluster::Address::toString() const
|
|
|
|
{
|
2017-05-30 11:49:17 +00:00
|
|
|
return toString(host_name, port);
|
|
|
|
}
|
|
|
|
|
|
|
|
String Cluster::Address::toString(const String & host_name, UInt16 port)
|
|
|
|
{
|
|
|
|
return escapeForFileName(host_name) + ':' + DB::toString(port);
|
2017-04-13 16:12:56 +00:00
|
|
|
}
|
|
|
|
|
2017-07-28 16:14:49 +00:00
|
|
|
String Cluster::Address::readableString() const
|
|
|
|
{
|
2018-04-19 19:25:54 +00:00
|
|
|
String res;
|
|
|
|
|
|
|
|
/// If it looks like IPv6 address add braces to avoid ambiguity in ipv6_host:port notation
|
|
|
|
if (host_name.find_first_of(':') != std::string::npos && !host_name.empty() && host_name.back() != ']')
|
|
|
|
res += '[' + host_name + ']';
|
|
|
|
else
|
|
|
|
res += host_name;
|
|
|
|
|
|
|
|
res += ':' + DB::toString(port);
|
|
|
|
return res;
|
2017-07-28 16:14:49 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 19:45:26 +00:00
|
|
|
std::pair<String, UInt16> Cluster::Address::fromString(const String & host_port_string)
|
2017-07-26 19:31:32 +00:00
|
|
|
{
|
|
|
|
auto pos = host_port_string.find_last_of(':');
|
|
|
|
if (pos == std::string::npos)
|
2017-07-27 18:44:55 +00:00
|
|
|
throw Exception("Incorrect <host>:<port> format " + host_port_string, ErrorCodes::SYNTAX_ERROR);
|
2017-07-26 19:31:32 +00:00
|
|
|
|
2019-01-21 19:45:26 +00:00
|
|
|
return {unescapeForFileName(host_port_string.substr(0, pos)), parse<UInt16>(host_port_string.substr(pos + 1))};
|
2017-07-26 19:31:32 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 11:49:17 +00:00
|
|
|
|
2020-03-13 18:49:46 +00:00
|
|
|
String Cluster::Address::toFullString(bool use_compact_format) const
|
2017-05-30 11:49:17 +00:00
|
|
|
{
|
2020-03-13 18:49:46 +00:00
|
|
|
if (use_compact_format)
|
|
|
|
{
|
2020-06-08 19:06:32 +00:00
|
|
|
if (shard_index == 0 || replica_index == 0)
|
|
|
|
// shard_num/replica_num like in system.clusters table
|
|
|
|
throw Exception("shard_num/replica_num cannot be zero", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
|
|
return "shard" + std::to_string(shard_index) + "_replica" + std::to_string(replica_index);
|
2020-03-13 18:49:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return
|
|
|
|
escapeForFileName(user)
|
|
|
|
+ (password.empty() ? "" : (':' + escapeForFileName(password))) + '@'
|
|
|
|
+ escapeForFileName(host_name) + ':' + std::to_string(port)
|
|
|
|
+ (default_database.empty() ? "" : ('#' + escapeForFileName(default_database)))
|
|
|
|
+ ((secure == Protocol::Secure::Enable) ? "+secure" : "");
|
|
|
|
}
|
2017-05-30 11:49:17 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 19:45:26 +00:00
|
|
|
Cluster::Address Cluster::Address::fromFullString(const String & full_string)
|
2018-12-02 02:17:08 +00:00
|
|
|
{
|
|
|
|
const char * address_begin = full_string.data();
|
2020-01-31 10:49:10 +00:00
|
|
|
const char * address_end = address_begin + full_string.size();
|
2018-12-02 02:17:08 +00:00
|
|
|
|
2020-01-31 10:49:10 +00:00
|
|
|
const char * user_pw_end = strchr(full_string.data(), '@');
|
|
|
|
|
2020-02-16 04:11:19 +00:00
|
|
|
/// parsing with the new [shard{shard_index}[_replica{replica_index}]] format
|
2020-02-03 13:18:00 +00:00
|
|
|
if (!user_pw_end && startsWith(full_string, "shard"))
|
2020-01-31 10:49:10 +00:00
|
|
|
{
|
|
|
|
const char * underscore = strchr(full_string.data(), '_');
|
|
|
|
|
|
|
|
Address address;
|
2020-02-16 04:12:48 +00:00
|
|
|
address.shard_index = parse<UInt32>(address_begin + strlen("shard"));
|
|
|
|
address.replica_index = underscore ? parse<UInt32>(underscore + strlen("_replica")) : 0;
|
2020-02-16 04:11:19 +00:00
|
|
|
|
2020-01-31 10:49:10 +00:00
|
|
|
return address;
|
|
|
|
}
|
2020-02-16 04:11:19 +00:00
|
|
|
else
|
2020-02-12 10:13:11 +00:00
|
|
|
{
|
2020-02-16 04:11:19 +00:00
|
|
|
/// parsing with the old user[:password]@host:port#default_database format
|
|
|
|
/// This format is appeared to be inconvenient for the following reasons:
|
|
|
|
/// - credentials are exposed in file name;
|
|
|
|
/// - the file name can be too long.
|
|
|
|
|
|
|
|
Protocol::Secure secure = Protocol::Secure::Disable;
|
|
|
|
const char * secure_tag = "+secure";
|
|
|
|
if (endsWith(full_string, secure_tag))
|
|
|
|
{
|
|
|
|
address_end -= strlen(secure_tag);
|
|
|
|
secure = Protocol::Secure::Enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * colon = strchr(full_string.data(), ':');
|
|
|
|
if (!user_pw_end || !colon)
|
|
|
|
throw Exception("Incorrect user[:password]@host:port#default_database format " + full_string, ErrorCodes::SYNTAX_ERROR);
|
|
|
|
|
|
|
|
const bool has_pw = colon < user_pw_end;
|
|
|
|
const char * host_end = has_pw ? strchr(user_pw_end + 1, ':') : colon;
|
|
|
|
if (!host_end)
|
|
|
|
throw Exception("Incorrect address '" + full_string + "', it does not contain port", ErrorCodes::SYNTAX_ERROR);
|
|
|
|
|
|
|
|
const char * has_db = strchr(full_string.data(), '#');
|
|
|
|
const char * port_end = has_db ? has_db : address_end;
|
2020-02-12 10:13:11 +00:00
|
|
|
|
2020-02-16 04:11:19 +00:00
|
|
|
Address address;
|
|
|
|
address.secure = secure;
|
|
|
|
address.port = parse<UInt16>(host_end + 1, port_end - (host_end + 1));
|
|
|
|
address.host_name = unescapeForFileName(std::string(user_pw_end + 1, host_end));
|
|
|
|
address.user = unescapeForFileName(std::string(address_begin, has_pw ? colon : user_pw_end));
|
|
|
|
address.password = has_pw ? unescapeForFileName(std::string(colon + 1, user_pw_end)) : std::string();
|
|
|
|
address.default_database = has_db ? unescapeForFileName(std::string(has_db + 1, address_end)) : std::string();
|
2020-06-27 06:52:10 +00:00
|
|
|
// address.priority ignored
|
2020-02-16 04:11:19 +00:00
|
|
|
return address;
|
|
|
|
}
|
2018-12-02 02:17:08 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 11:49:17 +00:00
|
|
|
|
2016-08-22 20:34:21 +00:00
|
|
|
/// Implementation of Clusters class
|
2014-02-22 21:50:27 +00:00
|
|
|
|
2020-09-14 21:55:43 +00:00
|
|
|
Clusters::Clusters(const Poco::Util::AbstractConfiguration & config, const Settings & settings, const String & config_prefix)
|
2013-12-07 16:51:29 +00:00
|
|
|
{
|
2020-09-14 21:55:43 +00:00
|
|
|
updateClusters(config, settings, config_prefix);
|
2016-10-10 08:44:52 +00:00
|
|
|
}
|
|
|
|
|
2016-10-14 15:06:46 +00:00
|
|
|
|
|
|
|
ClusterPtr Clusters::getCluster(const std::string & cluster_name) const
|
|
|
|
{
|
2018-01-25 12:18:27 +00:00
|
|
|
std::lock_guard lock(mutex);
|
2016-10-14 15:06:46 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
auto it = impl.find(cluster_name);
|
|
|
|
return (it != impl.end()) ? it->second : nullptr;
|
2016-10-14 15:06:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-03 19:53:10 +00:00
|
|
|
void Clusters::setCluster(const String & cluster_name, const std::shared_ptr<Cluster> & cluster)
|
|
|
|
{
|
2018-01-25 12:18:27 +00:00
|
|
|
std::lock_guard lock(mutex);
|
2017-11-03 19:53:10 +00:00
|
|
|
impl[cluster_name] = cluster;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-14 21:55:43 +00:00
|
|
|
void Clusters::updateClusters(const Poco::Util::AbstractConfiguration & config, const Settings & settings, const String & config_prefix)
|
2016-10-10 08:44:52 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
Poco::Util::AbstractConfiguration::Keys config_keys;
|
2020-09-14 21:55:43 +00:00
|
|
|
config.keys(config_prefix, config_keys);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-01-25 12:18:27 +00:00
|
|
|
std::lock_guard lock(mutex);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-08-05 03:55:41 +00:00
|
|
|
impl.clear();
|
2017-04-01 07:20:54 +00:00
|
|
|
for (const auto & key : config_keys)
|
2018-10-22 12:38:04 +00:00
|
|
|
{
|
|
|
|
if (key.find('.') != String::npos)
|
2019-06-15 12:06:22 +00:00
|
|
|
throw Exception("Cluster names with dots are not supported: '" + key + "'", ErrorCodes::SYNTAX_ERROR);
|
2018-10-22 12:38:04 +00:00
|
|
|
|
2020-09-14 21:55:43 +00:00
|
|
|
impl.emplace(key, std::make_shared<Cluster>(config, settings, config_prefix, key));
|
2018-10-22 12:38:04 +00:00
|
|
|
}
|
2016-10-10 08:44:52 +00:00
|
|
|
}
|
|
|
|
|
2016-10-14 15:06:46 +00:00
|
|
|
Clusters::Impl Clusters::getContainer() const
|
2016-10-10 08:44:52 +00:00
|
|
|
{
|
2018-01-25 12:18:27 +00:00
|
|
|
std::lock_guard lock(mutex);
|
2017-04-01 07:20:54 +00:00
|
|
|
/// The following line copies container of shared_ptrs to return value under lock
|
|
|
|
return impl;
|
2013-12-07 16:51:29 +00:00
|
|
|
}
|
|
|
|
|
2017-11-03 19:53:10 +00:00
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// Implementation of `Cluster` class
|
2013-12-07 16:51:29 +00:00
|
|
|
|
2020-09-14 21:55:43 +00:00
|
|
|
Cluster::Cluster(const Poco::Util::AbstractConfiguration & config,
|
|
|
|
const Settings & settings,
|
|
|
|
const String & config_prefix_,
|
|
|
|
const String & cluster_name)
|
2013-12-07 16:51:29 +00:00
|
|
|
{
|
2020-09-14 21:55:43 +00:00
|
|
|
auto config_prefix = config_prefix_ + "." + cluster_name;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Poco::Util::AbstractConfiguration::Keys config_keys;
|
2020-09-14 21:55:43 +00:00
|
|
|
config.keys(config_prefix, config_keys);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-09-14 21:55:43 +00:00
|
|
|
config_prefix += ".";
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-09-14 21:55:43 +00:00
|
|
|
secret = config.getString(config_prefix + "secret", "");
|
2020-09-12 22:27:44 +00:00
|
|
|
boost::range::remove_erase(config_keys, "secret");
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-09-14 21:55:43 +00:00
|
|
|
if (config_keys.empty())
|
|
|
|
throw Exception("No cluster elements (shard, node) specified in config at path " + config_prefix, ErrorCodes::SHARD_HAS_NO_CONNECTIONS);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-09-14 21:55:43 +00:00
|
|
|
UInt32 current_shard_num = 1;
|
2017-04-01 07:20:54 +00:00
|
|
|
for (const auto & key : config_keys)
|
|
|
|
{
|
|
|
|
if (startsWith(key, "node"))
|
|
|
|
{
|
2017-04-02 17:37:49 +00:00
|
|
|
/// Shard without replicas.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-08-11 15:02:07 +00:00
|
|
|
Addresses addresses;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const auto & prefix = config_prefix + key;
|
|
|
|
const auto weight = config.getInt(prefix + ".weight", default_weight);
|
|
|
|
|
2020-09-14 21:55:43 +00:00
|
|
|
addresses.emplace_back(config, prefix, cluster_name, secret, current_shard_num, 1);
|
2017-04-01 07:20:54 +00:00
|
|
|
const auto & address = addresses.back();
|
|
|
|
|
|
|
|
ShardInfo info;
|
|
|
|
info.shard_num = current_shard_num;
|
|
|
|
info.weight = weight;
|
|
|
|
|
2017-08-11 15:02:07 +00:00
|
|
|
if (address.is_local)
|
2017-04-01 07:20:54 +00:00
|
|
|
info.local_addresses.push_back(address);
|
2018-08-10 01:27:54 +00:00
|
|
|
|
|
|
|
ConnectionPoolPtr pool = std::make_shared<ConnectionPool>(
|
|
|
|
settings.distributed_connections_pool_size,
|
|
|
|
address.host_name, address.port,
|
|
|
|
address.default_database, address.user, address.password,
|
2020-09-14 21:55:43 +00:00
|
|
|
address.cluster, address.cluster_secret,
|
2020-06-27 06:52:10 +00:00
|
|
|
"server", address.compression,
|
|
|
|
address.secure, address.priority);
|
2018-08-10 01:27:54 +00:00
|
|
|
|
|
|
|
info.pool = std::make_shared<ConnectionPoolWithFailover>(
|
2019-03-01 23:14:11 +00:00
|
|
|
ConnectionPoolPtrs{pool}, settings.load_balancing);
|
2018-08-10 01:27:54 +00:00
|
|
|
info.per_replica_pools = {std::move(pool)};
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-08-08 00:06:21 +00:00
|
|
|
if (weight)
|
|
|
|
slot_to_shard.insert(std::end(slot_to_shard), weight, shards_info.size());
|
|
|
|
|
2018-02-14 15:11:39 +00:00
|
|
|
shards_info.emplace_back(std::move(info));
|
|
|
|
addresses_with_failover.emplace_back(std::move(addresses));
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else if (startsWith(key, "shard"))
|
|
|
|
{
|
2017-04-02 17:37:49 +00:00
|
|
|
/// Shard with replicas.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
Poco::Util::AbstractConfiguration::Keys replica_keys;
|
|
|
|
config.keys(config_prefix + key, replica_keys);
|
|
|
|
|
|
|
|
addresses_with_failover.emplace_back();
|
|
|
|
Addresses & replica_addresses = addresses_with_failover.back();
|
|
|
|
UInt32 current_replica_num = 1;
|
|
|
|
|
|
|
|
const auto & partial_prefix = config_prefix + key + ".";
|
2017-08-08 00:06:21 +00:00
|
|
|
const auto weight = config.getUInt(partial_prefix + ".weight", default_weight);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-05-30 11:49:17 +00:00
|
|
|
bool internal_replication = config.getBool(partial_prefix + ".internal_replication", false);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-04-19 19:25:54 +00:00
|
|
|
/// In case of internal_replication we will be appending names to dir_name_for_internal_replication
|
2017-08-11 15:02:07 +00:00
|
|
|
std::string dir_name_for_internal_replication;
|
2020-05-14 00:02:28 +00:00
|
|
|
std::string dir_name_for_internal_replication_with_local;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
for (const auto & replica_key : replica_keys)
|
|
|
|
{
|
|
|
|
if (startsWith(replica_key, "weight") || startsWith(replica_key, "internal_replication"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (startsWith(replica_key, "replica"))
|
|
|
|
{
|
2020-09-14 21:55:43 +00:00
|
|
|
replica_addresses.emplace_back(config,
|
|
|
|
partial_prefix + replica_key,
|
|
|
|
cluster_name,
|
|
|
|
secret,
|
|
|
|
current_shard_num,
|
|
|
|
current_replica_num);
|
2017-04-01 07:20:54 +00:00
|
|
|
++current_replica_num;
|
|
|
|
|
2020-05-14 00:02:28 +00:00
|
|
|
if (internal_replication)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2020-05-14 00:02:28 +00:00
|
|
|
auto dir_name = replica_addresses.back().toFullString(settings.use_compact_format_in_distributed_parts_names);
|
|
|
|
if (!replica_addresses.back().is_local)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2020-05-14 00:02:28 +00:00
|
|
|
if (dir_name_for_internal_replication.empty())
|
2017-08-11 15:02:07 +00:00
|
|
|
dir_name_for_internal_replication = dir_name;
|
2017-04-01 07:20:54 +00:00
|
|
|
else
|
2017-08-11 15:02:07 +00:00
|
|
|
dir_name_for_internal_replication += "," + dir_name;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2020-05-14 00:02:28 +00:00
|
|
|
if (dir_name_for_internal_replication_with_local.empty())
|
|
|
|
dir_name_for_internal_replication_with_local = dir_name;
|
|
|
|
else
|
|
|
|
dir_name_for_internal_replication_with_local += "," + dir_name;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw Exception("Unknown element in config: " + replica_key, ErrorCodes::UNKNOWN_ELEMENT_IN_CONFIG);
|
|
|
|
}
|
|
|
|
|
|
|
|
Addresses shard_local_addresses;
|
|
|
|
|
2018-02-14 15:11:39 +00:00
|
|
|
ConnectionPoolPtrs all_replicas_pools;
|
|
|
|
all_replicas_pools.reserve(replica_addresses.size());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
for (const auto & replica : replica_addresses)
|
|
|
|
{
|
2018-08-10 01:27:54 +00:00
|
|
|
auto replica_pool = std::make_shared<ConnectionPool>(
|
|
|
|
settings.distributed_connections_pool_size,
|
|
|
|
replica.host_name, replica.port,
|
|
|
|
replica.default_database, replica.user, replica.password,
|
2020-09-14 21:55:43 +00:00
|
|
|
replica.cluster, replica.cluster_secret,
|
2020-06-27 06:52:10 +00:00
|
|
|
"server", replica.compression,
|
|
|
|
replica.secure, replica.priority);
|
2018-08-10 01:27:54 +00:00
|
|
|
|
|
|
|
all_replicas_pools.emplace_back(replica_pool);
|
2017-08-11 15:02:07 +00:00
|
|
|
if (replica.is_local)
|
2017-04-01 07:20:54 +00:00
|
|
|
shard_local_addresses.push_back(replica);
|
|
|
|
}
|
|
|
|
|
2018-08-10 01:27:54 +00:00
|
|
|
ConnectionPoolWithFailoverPtr shard_pool = std::make_shared<ConnectionPoolWithFailover>(
|
2019-08-23 05:04:45 +00:00
|
|
|
all_replicas_pools, settings.load_balancing,
|
2019-09-05 10:35:36 +00:00
|
|
|
settings.distributed_replica_error_half_life.totalSeconds(), settings.distributed_replica_error_cap);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-08-08 00:06:21 +00:00
|
|
|
if (weight)
|
|
|
|
slot_to_shard.insert(std::end(slot_to_shard), weight, shards_info.size());
|
|
|
|
|
2020-05-14 00:02:28 +00:00
|
|
|
shards_info.push_back({
|
|
|
|
std::move(dir_name_for_internal_replication),
|
|
|
|
std::move(dir_name_for_internal_replication_with_local),
|
|
|
|
current_shard_num,
|
|
|
|
weight,
|
|
|
|
std::move(shard_local_addresses),
|
|
|
|
std::move(shard_pool),
|
|
|
|
std::move(all_replicas_pools),
|
|
|
|
internal_replication
|
|
|
|
});
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
throw Exception("Unknown element in config: " + key, ErrorCodes::UNKNOWN_ELEMENT_IN_CONFIG);
|
|
|
|
|
|
|
|
++current_shard_num;
|
|
|
|
}
|
|
|
|
|
2017-08-11 15:02:07 +00:00
|
|
|
if (addresses_with_failover.empty())
|
|
|
|
throw Exception("There must be either 'node' or 'shard' elements in config", ErrorCodes::EXCESSIVE_ELEMENT_IN_CONFIG);
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
initMisc();
|
2013-12-07 16:51:29 +00:00
|
|
|
}
|
|
|
|
|
2014-02-22 21:50:27 +00:00
|
|
|
|
2016-08-22 20:34:21 +00:00
|
|
|
Cluster::Cluster(const Settings & settings, const std::vector<std::vector<String>> & names,
|
2020-06-27 06:52:10 +00:00
|
|
|
const String & username, const String & password, UInt16 clickhouse_port, bool treat_local_as_remote,
|
|
|
|
bool secure, Int64 priority)
|
2014-02-07 15:11:57 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
UInt32 current_shard_num = 1;
|
|
|
|
|
|
|
|
for (const auto & shard : names)
|
|
|
|
{
|
|
|
|
Addresses current;
|
2020-04-22 06:01:33 +00:00
|
|
|
for (const auto & replica : shard)
|
2020-06-27 06:52:10 +00:00
|
|
|
current.emplace_back(replica, username, password, clickhouse_port, secure, priority);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
addresses_with_failover.emplace_back(current);
|
|
|
|
|
2017-12-01 17:13:14 +00:00
|
|
|
Addresses shard_local_addresses;
|
2018-02-14 15:11:39 +00:00
|
|
|
ConnectionPoolPtrs all_replicas;
|
|
|
|
all_replicas.reserve(current.size());
|
2017-12-01 17:13:14 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (const auto & replica : current)
|
|
|
|
{
|
2018-08-10 01:27:54 +00:00
|
|
|
auto replica_pool = std::make_shared<ConnectionPool>(
|
2017-12-01 17:13:14 +00:00
|
|
|
settings.distributed_connections_pool_size,
|
2018-03-29 20:21:01 +00:00
|
|
|
replica.host_name, replica.port,
|
2017-12-01 17:13:14 +00:00
|
|
|
replica.default_database, replica.user, replica.password,
|
2020-09-14 21:55:43 +00:00
|
|
|
replica.cluster, replica.cluster_secret,
|
2020-06-27 06:52:10 +00:00
|
|
|
"server", replica.compression, replica.secure, replica.priority);
|
2018-08-10 01:27:54 +00:00
|
|
|
all_replicas.emplace_back(replica_pool);
|
|
|
|
if (replica.is_local && !treat_local_as_remote)
|
|
|
|
shard_local_addresses.push_back(replica);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 17:40:55 +00:00
|
|
|
ConnectionPoolWithFailoverPtr shard_pool = std::make_shared<ConnectionPoolWithFailover>(
|
2019-08-23 05:04:45 +00:00
|
|
|
all_replicas, settings.load_balancing,
|
2019-09-05 10:35:36 +00:00
|
|
|
settings.distributed_replica_error_half_life.totalSeconds(), settings.distributed_replica_error_cap);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
slot_to_shard.insert(std::end(slot_to_shard), default_weight, shards_info.size());
|
2020-05-14 00:02:28 +00:00
|
|
|
shards_info.push_back({
|
|
|
|
{}, // dir_name_for_internal_replication
|
|
|
|
{}, // dir_name_for_internal_replication_with_local
|
|
|
|
current_shard_num,
|
|
|
|
default_weight,
|
|
|
|
std::move(shard_local_addresses),
|
|
|
|
std::move(shard_pool),
|
|
|
|
std::move(all_replicas),
|
2020-06-27 06:52:10 +00:00
|
|
|
false // has_internal_replication
|
2020-05-14 00:02:28 +00:00
|
|
|
});
|
2017-04-01 07:20:54 +00:00
|
|
|
++current_shard_num;
|
|
|
|
}
|
|
|
|
|
|
|
|
initMisc();
|
2014-02-07 15:11:57 +00:00
|
|
|
}
|
|
|
|
|
2014-02-22 21:50:27 +00:00
|
|
|
|
|
|
|
Poco::Timespan Cluster::saturate(const Poco::Timespan & v, const Poco::Timespan & limit)
|
2013-12-07 16:51:29 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (limit.totalMicroseconds() == 0)
|
|
|
|
return v;
|
|
|
|
else
|
|
|
|
return (v > limit) ? limit : v;
|
2013-12-07 16:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-20 14:59:29 +00:00
|
|
|
void Cluster::initMisc()
|
2013-12-07 16:51:29 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
for (const auto & shard_info : shards_info)
|
|
|
|
{
|
|
|
|
if (!shard_info.isLocal() && !shard_info.hasRemoteConnections())
|
|
|
|
throw Exception("Found shard without any specified connection",
|
|
|
|
ErrorCodes::SHARD_HAS_NO_CONNECTIONS);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto & shard_info : shards_info)
|
|
|
|
{
|
|
|
|
if (shard_info.isLocal())
|
|
|
|
++local_shard_count;
|
|
|
|
else
|
|
|
|
++remote_shard_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto & shard_info : shards_info)
|
|
|
|
{
|
|
|
|
if (!shard_info.isLocal())
|
|
|
|
{
|
|
|
|
any_remote_shard_info = &shard_info;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-12-07 16:51:29 +00:00
|
|
|
}
|
|
|
|
|
2020-01-07 10:26:16 +00:00
|
|
|
std::unique_ptr<Cluster> Cluster::getClusterWithReplicasAsShards(const Settings & settings) const
|
|
|
|
{
|
2020-01-10 17:44:34 +00:00
|
|
|
return std::unique_ptr<Cluster>{ new Cluster(ReplicasAsShardsTag{}, *this, settings)};
|
2020-01-07 10:26:16 +00:00
|
|
|
}
|
2016-05-13 03:22:16 +00:00
|
|
|
|
|
|
|
std::unique_ptr<Cluster> Cluster::getClusterWithSingleShard(size_t index) const
|
|
|
|
{
|
2020-01-10 17:44:34 +00:00
|
|
|
return std::unique_ptr<Cluster>{ new Cluster(SubclusterTag{}, *this, {index}) };
|
2016-05-13 03:22:16 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 04:04:05 +00:00
|
|
|
std::unique_ptr<Cluster> Cluster::getClusterWithMultipleShards(const std::vector<size_t> & indices) const
|
2016-05-13 03:22:16 +00:00
|
|
|
{
|
2020-01-10 17:44:34 +00:00
|
|
|
return std::unique_ptr<Cluster>{ new Cluster(SubclusterTag{}, *this, indices) };
|
2016-05-13 03:22:16 +00:00
|
|
|
}
|
|
|
|
|
2020-01-10 17:44:34 +00:00
|
|
|
Cluster::Cluster(Cluster::ReplicasAsShardsTag, const Cluster & from, const Settings & settings)
|
2020-01-07 10:26:16 +00:00
|
|
|
{
|
2020-01-10 17:44:34 +00:00
|
|
|
if (from.addresses_with_failover.empty())
|
|
|
|
throw Exception("Cluster is empty", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
|
|
std::set<std::pair<String, int>> unique_hosts;
|
|
|
|
for (size_t shard_index : ext::range(0, from.shards_info.size()))
|
2020-01-07 10:26:16 +00:00
|
|
|
{
|
2020-01-10 17:44:34 +00:00
|
|
|
const auto & replicas = from.addresses_with_failover[shard_index];
|
|
|
|
for (const auto & address : replicas)
|
2020-01-07 10:26:16 +00:00
|
|
|
{
|
2020-01-10 17:44:34 +00:00
|
|
|
if (!unique_hosts.emplace(address.host_name, address.port).second)
|
|
|
|
continue; /// Duplicate host, skip.
|
|
|
|
|
|
|
|
ShardInfo info;
|
|
|
|
if (address.is_local)
|
|
|
|
info.local_addresses.push_back(address);
|
|
|
|
|
|
|
|
ConnectionPoolPtr pool = std::make_shared<ConnectionPool>(
|
|
|
|
settings.distributed_connections_pool_size,
|
|
|
|
address.host_name,
|
|
|
|
address.port,
|
|
|
|
address.default_database,
|
|
|
|
address.user,
|
|
|
|
address.password,
|
2020-09-14 21:55:43 +00:00
|
|
|
address.cluster,
|
|
|
|
address.cluster_secret,
|
2020-01-10 17:44:34 +00:00
|
|
|
"server",
|
|
|
|
address.compression,
|
2020-06-27 06:52:10 +00:00
|
|
|
address.secure,
|
|
|
|
address.priority);
|
2020-01-10 17:44:34 +00:00
|
|
|
|
|
|
|
info.pool = std::make_shared<ConnectionPoolWithFailover>(ConnectionPoolPtrs{pool}, settings.load_balancing);
|
|
|
|
info.per_replica_pools = {std::move(pool)};
|
|
|
|
|
|
|
|
addresses_with_failover.emplace_back(Addresses{address});
|
|
|
|
shards_info.emplace_back(std::move(info));
|
2020-01-07 10:26:16 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-10 17:44:34 +00:00
|
|
|
|
2020-01-07 10:26:16 +00:00
|
|
|
initMisc();
|
|
|
|
}
|
|
|
|
|
2020-01-10 17:44:34 +00:00
|
|
|
|
|
|
|
Cluster::Cluster(Cluster::SubclusterTag, const Cluster & from, const std::vector<size_t> & indices)
|
2016-05-13 03:22:16 +00:00
|
|
|
{
|
2018-11-21 04:02:19 +00:00
|
|
|
for (size_t index : indices)
|
|
|
|
{
|
2018-11-21 04:06:40 +00:00
|
|
|
shards_info.emplace_back(from.shards_info.at(index));
|
2018-11-16 09:55:16 +00:00
|
|
|
|
|
|
|
if (!from.addresses_with_failover.empty())
|
2018-11-21 04:06:40 +00:00
|
|
|
addresses_with_failover.emplace_back(from.addresses_with_failover.at(index));
|
2018-11-16 09:55:16 +00:00
|
|
|
}
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
initMisc();
|
2016-05-13 03:22:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 00:02:28 +00:00
|
|
|
const std::string & Cluster::ShardInfo::pathForInsert(bool prefer_localhost_replica) const
|
|
|
|
{
|
|
|
|
if (!has_internal_replication)
|
|
|
|
throw Exception("internal_replication is not set", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
|
|
if (dir_name_for_internal_replication.empty() || dir_name_for_internal_replication_with_local.empty())
|
|
|
|
throw Exception("Directory name for async inserts is empty", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
|
|
if (prefer_localhost_replica)
|
|
|
|
return dir_name_for_internal_replication;
|
|
|
|
else
|
|
|
|
return dir_name_for_internal_replication_with_local;
|
|
|
|
}
|
|
|
|
|
2013-12-07 16:51:29 +00:00
|
|
|
}
|