2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/Cluster.h>
|
2017-08-07 17:01:04 +00:00
|
|
|
#include <Interpreters/DNSCache.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/escapeForFileName.h>
|
|
|
|
#include <Common/isLocalAddress.h>
|
|
|
|
#include <Common/SimpleCache.h>
|
|
|
|
#include <Common/StringUtils.h>
|
|
|
|
#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>
|
2016-03-01 17:47:53 +00:00
|
|
|
#include <openssl/sha.h>
|
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.
|
2017-08-08 00:06:21 +00:00
|
|
|
static constexpr UInt32 default_weight = 1;
|
2015-10-20 14:59:29 +00:00
|
|
|
|
|
|
|
inline bool isLocal(const Cluster::Address & address)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/// If there is replica, for which:
|
|
|
|
/// - 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).
|
|
|
|
|
|
|
|
return address.default_database.empty() && isLocalAddress(address.resolved_address);
|
2015-10-20 14:59:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Poco::Net::SocketAddress resolveSocketAddress(const String & host, UInt16 port)
|
2015-10-16 16:10:10 +00:00
|
|
|
{
|
2017-08-07 17:01:04 +00:00
|
|
|
return Poco::Net::SocketAddress(DNSCache::instance().resolveHost(host), port);
|
2015-10-16 16:10:10 +00:00
|
|
|
}
|
|
|
|
|
2015-10-20 14:59:29 +00:00
|
|
|
Poco::Net::SocketAddress resolveSocketAddress(const String & host_and_port)
|
2015-10-16 16:10:10 +00:00
|
|
|
{
|
2017-08-07 17:01:04 +00:00
|
|
|
return DNSCache::instance().resolveHostAndPort(host_and_port);
|
2015-10-16 16:10:10 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-10-10 08:44:52 +00:00
|
|
|
Cluster::Address::Address(Poco::Util::AbstractConfiguration & config, const String & config_prefix)
|
2013-12-07 16:51:29 +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"));
|
2017-04-01 07:20:54 +00:00
|
|
|
resolved_address = resolveSocketAddress(host_name, port);
|
|
|
|
user = config.getString(config_prefix + ".user", "default");
|
|
|
|
password = config.getString(config_prefix + ".password", "");
|
|
|
|
default_database = config.getString(config_prefix + ".default_database", "");
|
2017-08-11 15:02:07 +00:00
|
|
|
is_local = isLocal(*this);
|
2014-08-12 13:46:46 +00:00
|
|
|
}
|
2014-02-22 21:50:27 +00:00
|
|
|
|
2015-10-16 16:10:10 +00:00
|
|
|
|
2014-02-22 21:50:27 +00:00
|
|
|
Cluster::Address::Address(const String & host_port_, const String & user_, const String & password_)
|
2017-04-01 07:20:54 +00:00
|
|
|
: user(user_), password(password_)
|
2014-02-22 21:50:27 +00:00
|
|
|
{
|
2017-08-07 17:01:04 +00:00
|
|
|
UInt16 default_port = static_cast<UInt16>(Poco::Util::Application::instance().config().getInt("tcp_port", 0));
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/// It's like that 'host_port_' string contains port. If condition is met, it doesn't necessarily mean that port exists (example: [::]).
|
|
|
|
if ((nullptr != strchr(host_port_.c_str(), ':')) || !default_port)
|
|
|
|
{
|
|
|
|
resolved_address = resolveSocketAddress(host_port_);
|
|
|
|
host_name = host_port_.substr(0, host_port_.find(':'));
|
|
|
|
port = resolved_address.port();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
resolved_address = resolveSocketAddress(host_port_, default_port);
|
|
|
|
host_name = host_port_;
|
|
|
|
port = default_port;
|
|
|
|
}
|
2017-08-11 15:02:07 +00:00
|
|
|
is_local = isLocal(*this);
|
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
|
|
|
|
{
|
|
|
|
return host_name + ':' + DB::toString(port);
|
|
|
|
}
|
|
|
|
|
2017-07-26 19:31:32 +00:00
|
|
|
void Cluster::Address::fromString(const String & host_port_string, String & host_name, UInt16 & port)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
host_name = unescapeForFileName(host_port_string.substr(0, pos));
|
2017-07-27 18:44:55 +00:00
|
|
|
port = parse<UInt16>(host_port_string.substr(pos + 1));
|
2017-07-26 19:31:32 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 11:49:17 +00:00
|
|
|
|
|
|
|
String Cluster::Address::toStringFull() const
|
|
|
|
{
|
|
|
|
return
|
|
|
|
escapeForFileName(user) +
|
|
|
|
(password.empty() ? "" : (':' + escapeForFileName(password))) + '@' +
|
|
|
|
escapeForFileName(resolved_address.host().toString()) + ':' +
|
|
|
|
std::to_string(resolved_address.port()) +
|
|
|
|
(default_database.empty() ? "" : ('#' + escapeForFileName(default_database)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-22 20:34:21 +00:00
|
|
|
/// Implementation of Clusters class
|
2014-02-22 21:50:27 +00:00
|
|
|
|
2016-10-10 08:44:52 +00:00
|
|
|
Clusters::Clusters(Poco::Util::AbstractConfiguration & config, const Settings & settings, const String & config_name)
|
2013-12-07 16:51:29 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
Poco::Util::AbstractConfiguration::Keys config_keys;
|
|
|
|
config.keys(config_name, config_keys);
|
2013-12-07 16:51:29 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (const auto & key : config_keys)
|
|
|
|
impl.emplace(key, std::make_shared<Cluster>(config, settings, config_name + "." + key));
|
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
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::lock_guard<std::mutex> 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
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-10 08:44:52 +00:00
|
|
|
void Clusters::updateClusters(Poco::Util::AbstractConfiguration & config, const Settings & settings, const String & config_name)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
Poco::Util::AbstractConfiguration::Keys config_keys;
|
|
|
|
config.keys(config_name, config_keys);
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
|
|
|
|
for (const auto & key : config_keys)
|
|
|
|
{
|
|
|
|
auto it = impl.find(key);
|
|
|
|
auto new_cluster = std::make_shared<Cluster>(config, settings, config_name + "." + key);
|
|
|
|
|
|
|
|
if (it == impl.end())
|
|
|
|
impl.emplace(key, std::move(new_cluster));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//TODO: Check that cluster update is necessarily
|
|
|
|
it->second = std::move(new_cluster);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
/// The following line copies container of shared_ptrs to return value under lock
|
|
|
|
return impl;
|
2013-12-07 16:51:29 +00:00
|
|
|
}
|
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// Implementation of `Cluster` class
|
2013-12-07 16:51:29 +00:00
|
|
|
|
2016-10-10 08:44:52 +00:00
|
|
|
Cluster::Cluster(Poco::Util::AbstractConfiguration & config, const Settings & settings, const String & cluster_name)
|
2013-12-07 16:51:29 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
Poco::Util::AbstractConfiguration::Keys config_keys;
|
|
|
|
config.keys(cluster_name, config_keys);
|
|
|
|
|
|
|
|
if (config_keys.empty())
|
|
|
|
throw Exception("No cluster elements (shard, node) specified in config at path " + cluster_name, ErrorCodes::SHARD_HAS_NO_CONNECTIONS);
|
|
|
|
|
|
|
|
const auto & config_prefix = cluster_name + ".";
|
|
|
|
|
|
|
|
UInt32 current_shard_num = 1;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
addresses.emplace_back(config, prefix);
|
|
|
|
addresses.back().replica_num = 1;
|
|
|
|
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);
|
|
|
|
else
|
|
|
|
{
|
2017-04-19 17:40:55 +00:00
|
|
|
ConnectionPoolPtrs pools;
|
|
|
|
pools.push_back(std::make_shared<ConnectionPool>(
|
2017-04-01 07:20:54 +00:00
|
|
|
settings.distributed_connections_pool_size,
|
|
|
|
address.host_name, address.port, address.resolved_address,
|
|
|
|
address.default_database, address.user, address.password,
|
|
|
|
"server", Protocol::Compression::Enable,
|
|
|
|
saturate(settings.connect_timeout, settings.limits.max_execution_time),
|
|
|
|
saturate(settings.receive_timeout, settings.limits.max_execution_time),
|
2017-04-19 17:40:55 +00:00
|
|
|
saturate(settings.send_timeout, settings.limits.max_execution_time)));
|
|
|
|
|
|
|
|
info.pool = std::make_shared<ConnectionPoolWithFailover>(
|
|
|
|
std::move(pools), settings.load_balancing, settings.connections_with_failover_max_tries);
|
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());
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
shards_info.push_back(info);
|
2017-08-11 15:02:07 +00:00
|
|
|
addresses_with_failover.push_back(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
|
|
|
|
2017-08-11 15:02:07 +00:00
|
|
|
/// in case of internal_replication we will be appending names to dir_name_for_internal_replication
|
|
|
|
std::string dir_name_for_internal_replication;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
auto first = true;
|
|
|
|
for (const auto & replica_key : replica_keys)
|
|
|
|
{
|
|
|
|
if (startsWith(replica_key, "weight") || startsWith(replica_key, "internal_replication"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (startsWith(replica_key, "replica"))
|
|
|
|
{
|
|
|
|
replica_addresses.emplace_back(config, partial_prefix + replica_key);
|
|
|
|
replica_addresses.back().replica_num = current_replica_num;
|
|
|
|
++current_replica_num;
|
|
|
|
|
2017-08-11 15:02:07 +00:00
|
|
|
if (!replica_addresses.back().is_local)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (internal_replication)
|
|
|
|
{
|
2017-05-30 11:49:17 +00:00
|
|
|
auto dir_name = replica_addresses.back().toStringFull();
|
2017-04-01 07:20:54 +00:00
|
|
|
if (first)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if (first) first = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw Exception("Unknown element in config: " + replica_key, ErrorCodes::UNKNOWN_ELEMENT_IN_CONFIG);
|
|
|
|
}
|
|
|
|
|
|
|
|
Addresses shard_local_addresses;
|
|
|
|
|
2017-04-19 17:40:55 +00:00
|
|
|
ConnectionPoolPtrs replicas;
|
2017-04-01 07:20:54 +00:00
|
|
|
replicas.reserve(replica_addresses.size());
|
|
|
|
|
|
|
|
for (const auto & replica : replica_addresses)
|
|
|
|
{
|
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);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
replicas.emplace_back(std::make_shared<ConnectionPool>(
|
|
|
|
settings.distributed_connections_pool_size,
|
|
|
|
replica.host_name, replica.port, replica.resolved_address,
|
|
|
|
replica.default_database, replica.user, replica.password,
|
|
|
|
"server", Protocol::Compression::Enable,
|
|
|
|
saturate(settings.connect_timeout_with_failover_ms, settings.limits.max_execution_time),
|
|
|
|
saturate(settings.receive_timeout, settings.limits.max_execution_time),
|
|
|
|
saturate(settings.send_timeout, settings.limits.max_execution_time)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 17:40:55 +00:00
|
|
|
ConnectionPoolWithFailoverPtr shard_pool;
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!replicas.empty())
|
2017-04-19 17:40:55 +00:00
|
|
|
shard_pool = std::make_shared<ConnectionPoolWithFailover>(
|
|
|
|
std::move(replicas), settings.load_balancing, settings.connections_with_failover_max_tries);
|
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());
|
|
|
|
|
2017-08-11 15:02:07 +00:00
|
|
|
shards_info.push_back({std::move(dir_name_for_internal_replication), current_shard_num, weight,
|
|
|
|
shard_local_addresses, shard_pool, 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,
|
2017-04-01 07:20:54 +00:00
|
|
|
const String & username, const String & password)
|
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;
|
|
|
|
for (auto & replica : shard)
|
|
|
|
current.emplace_back(replica, username, password);
|
|
|
|
|
|
|
|
addresses_with_failover.emplace_back(current);
|
|
|
|
|
2017-04-19 17:40:55 +00:00
|
|
|
ConnectionPoolPtrs replicas;
|
2017-04-01 07:20:54 +00:00
|
|
|
replicas.reserve(current.size());
|
|
|
|
|
|
|
|
for (const auto & replica : current)
|
|
|
|
{
|
|
|
|
replicas.emplace_back(std::make_shared<ConnectionPool>(
|
|
|
|
settings.distributed_connections_pool_size,
|
|
|
|
replica.host_name, replica.port, replica.resolved_address,
|
|
|
|
replica.default_database, replica.user, replica.password,
|
|
|
|
"server", Protocol::Compression::Enable,
|
|
|
|
saturate(settings.connect_timeout_with_failover_ms, settings.limits.max_execution_time),
|
|
|
|
saturate(settings.receive_timeout, settings.limits.max_execution_time),
|
|
|
|
saturate(settings.send_timeout, settings.limits.max_execution_time)));
|
|
|
|
}
|
|
|
|
|
2017-04-19 17:40:55 +00:00
|
|
|
ConnectionPoolWithFailoverPtr shard_pool = std::make_shared<ConnectionPoolWithFailover>(
|
|
|
|
std::move(replicas), settings.load_balancing, settings.connections_with_failover_max_tries);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
slot_to_shard.insert(std::end(slot_to_shard), default_weight, shards_info.size());
|
|
|
|
shards_info.push_back({{}, current_shard_num, default_weight, {}, shard_pool});
|
|
|
|
++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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
calculateHashOfAddresses();
|
2016-03-02 13:35:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-13 03:22:16 +00:00
|
|
|
void Cluster::calculateHashOfAddresses()
|
2016-03-02 13:35:30 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::vector<std::string> elements;
|
|
|
|
|
2017-08-11 15:02:07 +00:00
|
|
|
if (!addresses_with_failover.empty())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
for (const auto & addresses : addresses_with_failover)
|
|
|
|
{
|
|
|
|
for (const auto & address : addresses)
|
|
|
|
elements.push_back(address.host_name + ":" + toString(address.port));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw Exception("Cluster: ill-formed cluster", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
|
|
std::sort(elements.begin(), elements.end());
|
|
|
|
|
|
|
|
unsigned char hash[SHA512_DIGEST_LENGTH];
|
|
|
|
|
|
|
|
SHA512_CTX ctx;
|
|
|
|
SHA512_Init(&ctx);
|
|
|
|
|
|
|
|
for (const auto & host : elements)
|
|
|
|
SHA512_Update(&ctx, reinterpret_cast<const void *>(host.c_str()), host.size() + 1);
|
|
|
|
|
|
|
|
SHA512_Final(hash, &ctx);
|
|
|
|
|
|
|
|
{
|
|
|
|
WriteBufferFromString buf(hash_of_addresses);
|
|
|
|
HexWriteBuffer hex_buf(buf);
|
|
|
|
hex_buf.write(reinterpret_cast<const char *>(hash), sizeof(hash));
|
|
|
|
}
|
2013-12-07 16:51:29 +00:00
|
|
|
}
|
|
|
|
|
2016-05-13 03:22:16 +00:00
|
|
|
|
|
|
|
std::unique_ptr<Cluster> Cluster::getClusterWithSingleShard(size_t index) const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::unique_ptr<Cluster>{ new Cluster(*this, index) };
|
2016-05-13 03:22:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Cluster::Cluster(const Cluster & from, size_t index)
|
2017-04-01 07:20:54 +00:00
|
|
|
: shards_info{from.shards_info[index]}
|
2016-05-13 03:22:16 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!from.addresses_with_failover.empty())
|
|
|
|
addresses_with_failover.emplace_back(from.addresses_with_failover[index]);
|
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
|
|
|
}
|
|
|
|
|
2013-12-07 16:51:29 +00:00
|
|
|
}
|