ClickHouse/dbms/include/DB/Client/ConnectionPoolWithFailover.h

126 lines
3.8 KiB
C++
Raw Normal View History

#pragma once
#include <random>
#include <Poco/Net/NetException.h>
#include <Poco/Net/DNS.h>
2015-10-05 01:26:43 +00:00
#include <DB/Common/PoolWithFailoverBase.h>
#include <DB/Common/getFQDNOrHostName.h>
#include <DB/Client/ConnectionPool.h>
namespace DB
{
namespace ErrorCodes
{
extern const int NETWORK_ERROR;
extern const int SOCKET_TIMEOUT;
extern const int LOGICAL_ERROR;
}
2017-03-09 00:56:38 +00:00
/** Connection pool with fault tolerance.
* Initialized by several other IConnectionPools.
* When a connection is received, it tries to create or select a live connection from a pool,
* fetch them in some order, using no more than the specified number of attempts.
* Pools with fewer errors are preferred;
* pools with the same number of errors are tried in random order.
*
2017-03-09 00:56:38 +00:00
* Note: if one of the nested pools is blocked due to overflow, then this pool will also be blocked.
*/
class ConnectionPoolWithFailover : public PoolWithFailoverBase<IConnectionPool>, public IConnectionPool
{
public:
using Entry = IConnectionPool::Entry;
using Base = PoolWithFailoverBase<IConnectionPool>;
ConnectionPoolWithFailover(ConnectionPools & nested_pools_,
LoadBalancing load_balancing,
size_t max_tries_ = DBMS_CONNECTION_POOL_WITH_FAILOVER_DEFAULT_MAX_TRIES,
time_t decrease_error_period_ = DBMS_CONNECTION_POOL_WITH_FAILOVER_DEFAULT_DECREASE_ERROR_PERIOD)
: Base(nested_pools_, max_tries_, decrease_error_period_,
&Logger::get("ConnectionPoolWithFailover")), default_load_balancing(load_balancing)
{
const std::string & local_hostname = getFQDNOrHostName();
hostname_differences.resize(nested_pools.size());
for (size_t i = 0; i < nested_pools.size(); ++i)
{
ConnectionPool & connection_pool = dynamic_cast<ConnectionPool &>(*nested_pools[i].pool);
const std::string & host = connection_pool.getHost();
size_t hostname_difference = 0;
for (size_t i = 0; i < std::min(local_hostname.length(), host.length()); ++i)
if (local_hostname[i] != host[i])
++hostname_difference;
hostname_differences[i] = hostname_difference;
}
}
protected:
bool tryGet(ConnectionPoolPtr pool, const Settings * settings, Entry & out_entry, std::stringstream & fail_message) override
{
try
{
out_entry = pool->get(settings);
out_entry->forceConnected();
return true;
}
catch (const Exception & e)
{
if (e.code() != ErrorCodes::NETWORK_ERROR && e.code() != ErrorCodes::SOCKET_TIMEOUT)
throw;
fail_message << "Code: " << e.code() << ", e.displayText() = " << e.displayText() << ", e.what() = " << e.what();
return false;
}
}
private:
2017-03-09 04:26:17 +00:00
/** Allocates connection to work. */
2015-10-12 14:53:16 +00:00
Entry doGet(const Settings * settings) override
{
applyLoadBalancing(settings);
return Base::get(settings);
}
2017-03-09 04:26:17 +00:00
/** Allocates up to the specified number of connections to work.
2017-03-09 00:56:38 +00:00
* Connections provide access to different replicas of one shard.
2015-10-12 14:53:16 +00:00
*/
2016-03-01 17:47:53 +00:00
std::vector<Entry> doGetMany(const Settings * settings, PoolMode pool_mode) override
2015-10-12 14:53:16 +00:00
{
applyLoadBalancing(settings);
2016-03-01 17:47:53 +00:00
return Base::getMany(settings, pool_mode);
2015-10-12 14:53:16 +00:00
}
void applyLoadBalancing(const Settings * settings)
{
LoadBalancing load_balancing = default_load_balancing;
if (settings)
load_balancing = settings->load_balancing;
for (size_t i = 0; i < nested_pools.size(); ++i)
{
if (load_balancing == LoadBalancing::NEAREST_HOSTNAME)
nested_pools[i].state.priority = hostname_differences[i];
else if (load_balancing == LoadBalancing::RANDOM)
nested_pools[i].state.priority = 0;
else if (load_balancing == LoadBalancing::IN_ORDER)
nested_pools[i].state.priority = i;
else
throw Exception("Unknown load_balancing_mode: " + toString(static_cast<int>(load_balancing)), ErrorCodes::LOGICAL_ERROR);
}
}
2015-10-12 14:53:16 +00:00
private:
2017-03-09 00:56:38 +00:00
std::vector<size_t> hostname_differences; /// Distances from name of this host to the names of hosts of pools.
2015-10-12 14:53:16 +00:00
LoadBalancing default_load_balancing;
};
}