2014-02-22 18:53:42 +00:00
|
|
|
#pragma once
|
|
|
|
|
2013-12-07 16:51:29 +00:00
|
|
|
#include <map>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/Settings.h>
|
|
|
|
#include <Client/ConnectionPool.h>
|
|
|
|
#include <Client/ConnectionPoolWithFailover.h>
|
2013-12-07 16:51:29 +00:00
|
|
|
#include <Poco/Net/SocketAddress.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2015-05-28 21:41:28 +00:00
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/// Cluster contains connection pools to each node
|
|
|
|
/// With the local nodes, the connection is not established, but the request is executed directly.
|
|
|
|
/// Therefore we store only the number of local nodes
|
|
|
|
/// In the config, the cluster includes nodes <node> or <shard>
|
2015-10-20 14:59:29 +00:00
|
|
|
class Cluster
|
2013-12-07 16:51:29 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
Cluster(Poco::Util::AbstractConfiguration & config, const Settings & settings, const String & cluster_name);
|
2013-12-07 16:51:29 +00:00
|
|
|
|
2017-12-01 17:13:14 +00:00
|
|
|
/// Construct a cluster by the names of shards and replicas.
|
|
|
|
/// Local are treated as well as remote ones if treat_local_as_shared is true.
|
2017-09-07 17:55:02 +00:00
|
|
|
/// 'clickhouse_port' - port that this server instance listen for queries.
|
|
|
|
/// This parameter is needed only to check that some address is local (points to ourself).
|
2017-04-01 07:20:54 +00:00
|
|
|
Cluster(const Settings & settings, const std::vector<std::vector<String>> & names,
|
2017-12-01 17:13:14 +00:00
|
|
|
const String & username, const String & password,
|
2018-01-26 14:39:31 +00:00
|
|
|
UInt16 clickhouse_port, bool treat_local_as_shared);
|
2014-02-07 15:11:57 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Cluster(const Cluster &) = delete;
|
|
|
|
Cluster & operator=(const Cluster &) = delete;
|
2013-12-07 16:51:29 +00:00
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/// is used to set a limit on the size of the timeout
|
2017-04-01 07:20:54 +00:00
|
|
|
static Poco::Timespan saturate(const Poco::Timespan & v, const Poco::Timespan & limit);
|
2015-04-30 12:43:16 +00:00
|
|
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
struct Address
|
|
|
|
{
|
|
|
|
/** In configuration file,
|
|
|
|
* addresses are located either in <node> elements:
|
|
|
|
* <node>
|
|
|
|
* <host>example01-01-1</host>
|
|
|
|
* <port>9000</port>
|
|
|
|
* <!-- <user>, <password>, <default_database> if needed -->
|
|
|
|
* </node>
|
|
|
|
* ...
|
|
|
|
* or in <shard> and inside in <replica> elements:
|
|
|
|
* <shard>
|
|
|
|
* <replica>
|
|
|
|
* <host>example01-01-1</host>
|
|
|
|
* <port>9000</port>
|
|
|
|
* <!-- <user>, <password>, <default_database> if needed -->
|
|
|
|
* </replica>
|
|
|
|
* </shard>
|
|
|
|
*/
|
|
|
|
Poco::Net::SocketAddress resolved_address;
|
|
|
|
String host_name;
|
|
|
|
UInt16 port;
|
|
|
|
String user;
|
|
|
|
String password;
|
|
|
|
String default_database; /// this database is selected when no database is specified for Distributed table
|
|
|
|
UInt32 replica_num;
|
2017-08-11 15:02:07 +00:00
|
|
|
bool is_local;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-26 19:31:32 +00:00
|
|
|
Address() = default;
|
2017-04-01 07:20:54 +00:00
|
|
|
Address(Poco::Util::AbstractConfiguration & config, const String & config_prefix);
|
2017-09-07 14:38:35 +00:00
|
|
|
Address(const String & host_port_, const String & user_, const String & password_, UInt16 clickhouse_port);
|
2017-04-13 16:12:56 +00:00
|
|
|
|
2017-07-28 16:14:49 +00:00
|
|
|
/// Returns 'escaped_host_name:port'
|
2017-04-13 16:12:56 +00:00
|
|
|
String toString() const;
|
2017-05-30 11:49:17 +00:00
|
|
|
|
2017-07-28 16:14:49 +00:00
|
|
|
/// Returns 'host_name:port'
|
|
|
|
String readableString() const;
|
|
|
|
|
2017-05-30 11:49:17 +00:00
|
|
|
static String toString(const String & host_name, UInt16 port);
|
|
|
|
|
2017-07-26 19:31:32 +00:00
|
|
|
static void fromString(const String & host_port_string, String & host_name, UInt16 & port);
|
|
|
|
|
2017-05-30 11:49:17 +00:00
|
|
|
/// Retrurns escaped user:password@resolved_host_address:resolved_host_port#default_database
|
|
|
|
String toStringFull() const;
|
2017-04-01 07:20:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
using Addresses = std::vector<Address>;
|
|
|
|
using AddressesWithFailover = std::vector<Addresses>;
|
|
|
|
|
|
|
|
struct ShardInfo
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool isLocal() const { return !local_addresses.empty(); }
|
2017-04-19 17:40:55 +00:00
|
|
|
bool hasRemoteConnections() const { return pool != nullptr; }
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t getLocalNodeCount() const { return local_addresses.size(); }
|
2017-05-30 11:49:17 +00:00
|
|
|
bool hasInternalReplication() const { return has_internal_replication; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
public:
|
2017-08-11 15:02:07 +00:00
|
|
|
/// Name of directory for asynchronous write to StorageDistributed if has_internal_replication
|
|
|
|
std::string dir_name_for_internal_replication;
|
2017-04-25 15:21:03 +00:00
|
|
|
/// Number of the shard, the indexation begins with 1
|
|
|
|
UInt32 shard_num;
|
2017-08-08 00:06:21 +00:00
|
|
|
UInt32 weight;
|
2017-04-01 07:20:54 +00:00
|
|
|
Addresses local_addresses;
|
2017-10-13 19:13:41 +00:00
|
|
|
/// nullptr if there are no remote addresses
|
2017-04-19 17:40:55 +00:00
|
|
|
ConnectionPoolWithFailoverPtr pool;
|
2018-02-14 15:11:39 +00:00
|
|
|
/// Connection pool for each replica, contains nullptr for local replicas
|
|
|
|
ConnectionPoolPtrs per_replica_pools;
|
2017-05-30 11:49:17 +00:00
|
|
|
bool has_internal_replication;
|
2017-04-01 07:20:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
using ShardsInfo = std::vector<ShardInfo>;
|
|
|
|
|
|
|
|
String getHashOfAddresses() const { return hash_of_addresses; }
|
|
|
|
const ShardsInfo & getShardsInfo() const { return shards_info; }
|
2017-08-11 15:02:07 +00:00
|
|
|
const AddressesWithFailover & getShardsAddresses() const { return addresses_with_failover; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
const ShardInfo & getAnyShardInfo() const
|
|
|
|
{
|
|
|
|
if (shards_info.empty())
|
|
|
|
throw Exception("Cluster is empty", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
return shards_info.front();
|
|
|
|
}
|
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/// The number of remote shards.
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t getRemoteShardCount() const { return remote_shard_count; }
|
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/// The number of clickhouse nodes located locally
|
|
|
|
/// we access the local nodes directly.
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t getLocalShardCount() const { return local_shard_count; }
|
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/// The number of all shards.
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t getShardCount() const { return shards_info.size(); }
|
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/// Get a subcluster consisting of one shard - index by count (from 0) of the shard of this cluster.
|
2017-04-01 07:20:54 +00:00
|
|
|
std::unique_ptr<Cluster> getClusterWithSingleShard(size_t index) const;
|
2016-05-13 03:22:16 +00:00
|
|
|
|
|
|
|
private:
|
2017-09-01 17:21:03 +00:00
|
|
|
using SlotToShard = std::vector<UInt64>;
|
2017-04-01 07:20:54 +00:00
|
|
|
SlotToShard slot_to_shard;
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2015-10-20 14:59:29 +00:00
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
const SlotToShard & getSlotToShard() const { return slot_to_shard; }
|
2015-04-30 12:43:16 +00:00
|
|
|
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
void initMisc();
|
2015-10-20 14:59:29 +00:00
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/// For getClusterWithSingleShard implementation.
|
2017-04-01 07:20:54 +00:00
|
|
|
Cluster(const Cluster & from, size_t index);
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
String hash_of_addresses;
|
2017-06-02 21:37:28 +00:00
|
|
|
/// Description of the cluster shards.
|
2017-04-01 07:20:54 +00:00
|
|
|
ShardsInfo shards_info;
|
2017-06-02 21:37:28 +00:00
|
|
|
/// Any remote shard.
|
2017-04-01 07:20:54 +00:00
|
|
|
ShardInfo * any_remote_shard_info = nullptr;
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/// Non-empty is either addresses or addresses_with_failover.
|
|
|
|
/// The size and order of the elements in the corresponding array corresponds to shards_info.
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2017-06-02 21:37:28 +00:00
|
|
|
/// An array of shards. For each shard, an array of replica addresses (servers that are considered identical).
|
2017-04-01 07:20:54 +00:00
|
|
|
AddressesWithFailover addresses_with_failover;
|
2013-12-07 16:51:29 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t remote_shard_count = 0;
|
|
|
|
size_t local_shard_count = 0;
|
2013-12-07 16:51:29 +00:00
|
|
|
};
|
|
|
|
|
2016-10-10 08:44:52 +00:00
|
|
|
using ClusterPtr = std::shared_ptr<Cluster>;
|
|
|
|
|
2016-03-04 02:40:48 +00:00
|
|
|
|
2016-03-01 17:47:53 +00:00
|
|
|
class Clusters
|
2013-12-07 16:51:29 +00:00
|
|
|
{
|
2016-03-01 17:47:53 +00:00
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
Clusters(Poco::Util::AbstractConfiguration & config, const Settings & settings, const String & config_name = "remote_servers");
|
2016-03-01 17:47:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Clusters(const Clusters &) = delete;
|
|
|
|
Clusters & operator=(const Clusters &) = delete;
|
2016-03-01 17:47:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
ClusterPtr getCluster(const std::string & cluster_name) const;
|
2017-11-03 19:53:10 +00:00
|
|
|
void setCluster(const String & cluster_name, const ClusterPtr & cluster);
|
2016-10-10 08:44:52 +00:00
|
|
|
|
2017-10-13 19:13:41 +00:00
|
|
|
void updateClusters(Poco::Util::AbstractConfiguration & config, const Settings & settings, const String & config_name);
|
2016-03-01 17:47:53 +00:00
|
|
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
using Impl = std::map<String, ClusterPtr>;
|
2016-10-10 08:44:52 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Impl getContainer() const;
|
2016-10-10 08:44:52 +00:00
|
|
|
|
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
Impl impl;
|
|
|
|
mutable std::mutex mutex;
|
2013-12-07 16:51:29 +00:00
|
|
|
};
|
2014-02-22 21:50:27 +00:00
|
|
|
|
2016-10-10 08:44:52 +00:00
|
|
|
using ClustersPtr = std::shared_ptr<Clusters>;
|
|
|
|
|
2013-12-07 16:51:29 +00:00
|
|
|
}
|