2012-11-02 20:13:41 +00:00
|
|
|
#pragma once
|
|
|
|
|
2016-10-26 22:27:38 +00:00
|
|
|
#include <random>
|
|
|
|
|
2012-11-02 20:13:41 +00:00
|
|
|
#include <Poco/Net/NetException.h>
|
2013-12-10 17:07:09 +00:00
|
|
|
#include <Poco/Net/DNS.h>
|
2012-11-02 20:13:41 +00:00
|
|
|
|
2015-10-05 01:26:43 +00:00
|
|
|
#include <DB/Common/PoolWithFailoverBase.h>
|
2014-06-03 14:32:04 +00:00
|
|
|
|
2015-05-29 00:33:56 +00:00
|
|
|
#include <DB/Common/getFQDNOrHostName.h>
|
2012-11-02 20:13:41 +00:00
|
|
|
#include <DB/Client/ConnectionPool.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
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.
|
2012-11-02 20:13:41 +00:00
|
|
|
*
|
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.
|
2012-11-02 20:13:41 +00:00
|
|
|
*/
|
2015-06-28 06:38:18 +00:00
|
|
|
class ConnectionPoolWithFailover : public PoolWithFailoverBase<IConnectionPool>, public IConnectionPool
|
2012-11-02 20:13:41 +00:00
|
|
|
{
|
|
|
|
public:
|
2016-05-28 10:35:44 +00:00
|
|
|
using Entry = IConnectionPool::Entry;
|
|
|
|
using Base = PoolWithFailoverBase<IConnectionPool>;
|
2012-11-02 20:13:41 +00:00
|
|
|
|
2013-12-11 11:51:01 +00:00
|
|
|
ConnectionPoolWithFailover(ConnectionPools & nested_pools_,
|
2014-02-17 23:56:45 +00:00
|
|
|
LoadBalancing load_balancing,
|
|
|
|
size_t max_tries_ = DBMS_CONNECTION_POOL_WITH_FAILOVER_DEFAULT_MAX_TRIES,
|
2014-04-06 23:15:27 +00:00
|
|
|
time_t decrease_error_period_ = DBMS_CONNECTION_POOL_WITH_FAILOVER_DEFAULT_DECREASE_ERROR_PERIOD)
|
2014-06-03 14:32:04 +00:00
|
|
|
: Base(nested_pools_, max_tries_, decrease_error_period_,
|
|
|
|
&Logger::get("ConnectionPoolWithFailover")), default_load_balancing(load_balancing)
|
2012-11-02 20:13:41 +00:00
|
|
|
{
|
2015-05-29 00:33:56 +00:00
|
|
|
const std::string & local_hostname = getFQDNOrHostName();
|
2012-11-02 20:13:41 +00:00
|
|
|
|
2014-06-03 14:32:04 +00:00
|
|
|
hostname_differences.resize(nested_pools.size());
|
|
|
|
for (size_t i = 0; i < nested_pools.size(); ++i)
|
2012-11-06 18:20:00 +00:00
|
|
|
{
|
2014-06-03 14:32:04 +00:00
|
|
|
ConnectionPool & connection_pool = dynamic_cast<ConnectionPool &>(*nested_pools[i].pool);
|
2013-12-10 17:07:09 +00:00
|
|
|
const std::string & host = connection_pool.getHost();
|
2014-06-03 14:32:04 +00:00
|
|
|
|
|
|
|
size_t hostname_difference = 0;
|
2013-12-10 17:07:09 +00:00
|
|
|
for (size_t i = 0; i < std::min(local_hostname.length(), host.length()); ++i)
|
|
|
|
if (local_hostname[i] != host[i])
|
|
|
|
++hostname_difference;
|
2014-04-06 23:15:27 +00:00
|
|
|
|
2014-06-03 14:32:04 +00:00
|
|
|
hostname_differences[i] = hostname_difference;
|
2012-11-06 18:20:00 +00:00
|
|
|
}
|
2014-06-03 14:32:04 +00:00
|
|
|
}
|
2012-11-02 20:13:41 +00:00
|
|
|
|
2014-06-03 14:32:04 +00:00
|
|
|
protected:
|
2015-06-28 06:38:18 +00:00
|
|
|
bool tryGet(ConnectionPoolPtr pool, const Settings * settings, Entry & out_entry, std::stringstream & fail_message) override
|
2013-12-10 17:07:09 +00:00
|
|
|
{
|
2014-06-03 14:32:04 +00:00
|
|
|
try
|
2013-12-10 17:07:09 +00:00
|
|
|
{
|
2014-06-03 14:32:04 +00:00
|
|
|
out_entry = pool->get(settings);
|
|
|
|
out_entry->forceConnected();
|
|
|
|
return true;
|
2013-12-10 17:07:09 +00:00
|
|
|
}
|
2014-06-03 14:32:04 +00:00
|
|
|
catch (const Exception & e)
|
2013-12-10 17:07:09 +00:00
|
|
|
{
|
2014-06-03 14:32:04 +00:00
|
|
|
if (e.code() != ErrorCodes::NETWORK_ERROR && e.code() != ErrorCodes::SOCKET_TIMEOUT)
|
|
|
|
throw;
|
2013-12-10 17:07:09 +00:00
|
|
|
|
2014-06-03 14:32:04 +00:00
|
|
|
fail_message << "Code: " << e.code() << ", e.displayText() = " << e.displayText() << ", e.what() = " << e.what();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-11-02 20:13:41 +00:00
|
|
|
|
2014-06-03 14:32:04 +00:00
|
|
|
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
|
|
|
}
|
2014-12-30 14:11:02 +00:00
|
|
|
|
2015-06-28 06:38:18 +00:00
|
|
|
void applyLoadBalancing(const Settings * settings)
|
2014-12-30 14:11:02 +00:00
|
|
|
{
|
|
|
|
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)
|
2015-03-26 14:53:35 +00:00
|
|
|
nested_pools[i].state.priority = hostname_differences[i];
|
2014-12-30 14:11:02 +00:00
|
|
|
else if (load_balancing == LoadBalancing::RANDOM)
|
2015-03-26 14:53:35 +00:00
|
|
|
nested_pools[i].state.priority = 0;
|
2014-12-30 14:11:02 +00:00
|
|
|
else if (load_balancing == LoadBalancing::IN_ORDER)
|
2015-03-26 14:53:35 +00:00
|
|
|
nested_pools[i].state.priority = i;
|
2014-12-30 14:11:02 +00:00
|
|
|
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;
|
2012-11-02 20:13:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|