2012-11-02 20:13:41 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
|
|
#include <DB/Client/ConnectionPool.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/** Пул соединений с отказоустойчивостью.
|
|
|
|
|
* Инициализируется несколькими другими IConnectionPool-ами.
|
|
|
|
|
* При получении соединения, пытается создать или выбрать живое соединение из какого-нибудь пула,
|
|
|
|
|
* перебирая их в некотором порядке, используя не более указанного количества попыток.
|
|
|
|
|
* Предпочитаются пулы с меньшим количеством ошибок;
|
|
|
|
|
* пулы с одинаковым количеством ошибок пробуются в случайном порядке.
|
|
|
|
|
*
|
|
|
|
|
* Замечание: если один из вложенных пулов заблокируется из-за переполнения, то этот пул тоже заблокируется.
|
|
|
|
|
*/
|
|
|
|
|
class ConnectionPoolWithFailover : public IConnectionPool
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
typedef detail::ConnectionPoolEntry Entry;
|
|
|
|
|
|
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)
|
2013-12-11 11:51:01 +00:00
|
|
|
|
: nested_pools(nested_pools_.begin(), nested_pools_.end(), decrease_error_period_), max_tries(max_tries_),
|
|
|
|
|
log(&Logger::get("ConnectionPoolWithFailover")), default_load_balancing(load_balancing)
|
2012-11-02 20:13:41 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Выделяет соединение для работы. */
|
2013-12-11 11:51:01 +00:00
|
|
|
|
Entry get(Settings * settings)
|
2012-11-02 20:13:41 +00:00
|
|
|
|
{
|
2014-02-17 23:56:45 +00:00
|
|
|
|
LoadBalancing load_balancing = default_load_balancing;
|
2013-12-11 11:51:01 +00:00
|
|
|
|
if (settings)
|
|
|
|
|
load_balancing = settings->load_balancing;
|
|
|
|
|
|
2014-04-06 23:15:27 +00:00
|
|
|
|
/// Обновление случайных чисел, а также счётчиков ошибок.
|
2013-12-11 11:51:01 +00:00
|
|
|
|
nested_pools.update(load_balancing);
|
2014-04-06 23:15:27 +00:00
|
|
|
|
|
|
|
|
|
typedef std::vector<PoolWithErrorCount*> PoolPtrs;
|
|
|
|
|
|
|
|
|
|
size_t pools_size = nested_pools.size();
|
|
|
|
|
PoolPtrs pool_ptrs(pools_size);
|
|
|
|
|
for (size_t i = 0; i < pools_size; ++i)
|
|
|
|
|
pool_ptrs[i] = &nested_pools[i];
|
|
|
|
|
|
|
|
|
|
std::sort(pool_ptrs.begin(), pool_ptrs.end(),
|
|
|
|
|
[=](const PoolPtrs::value_type & lhs, const PoolPtrs::value_type & rhs)
|
|
|
|
|
{
|
|
|
|
|
return PoolWithErrorCount::compare(*lhs, *rhs, load_balancing);
|
|
|
|
|
});
|
2012-11-02 20:13:41 +00:00
|
|
|
|
|
|
|
|
|
std::stringstream fail_messages;
|
|
|
|
|
|
|
|
|
|
for (size_t try_no = 0; try_no < max_tries; ++try_no)
|
|
|
|
|
{
|
2014-04-06 23:15:27 +00:00
|
|
|
|
for (size_t i = 0; i < pools_size; ++i)
|
2012-11-02 20:13:41 +00:00
|
|
|
|
{
|
2012-11-06 18:20:00 +00:00
|
|
|
|
std::stringstream fail_message;
|
2013-10-23 19:35:43 +00:00
|
|
|
|
|
2012-11-02 20:13:41 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2014-04-06 23:15:27 +00:00
|
|
|
|
Entry res = pool_ptrs[i]->pool->get(settings);
|
2012-11-02 20:13:41 +00:00
|
|
|
|
res->forceConnected();
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2013-10-26 03:20:51 +00:00
|
|
|
|
catch (const Exception & e)
|
2012-11-02 20:13:41 +00:00
|
|
|
|
{
|
2013-10-23 19:35:43 +00:00
|
|
|
|
if (e.code() != ErrorCodes::NETWORK_ERROR && e.code() != ErrorCodes::SOCKET_TIMEOUT)
|
|
|
|
|
throw;
|
|
|
|
|
|
2013-10-26 03:20:51 +00:00
|
|
|
|
fail_message << "Code: " << e.code() << ", e.displayText() = " << e.displayText() << ", e.what() = " << e.what();
|
2012-11-06 18:20:00 +00:00
|
|
|
|
}
|
2012-11-02 20:13:41 +00:00
|
|
|
|
|
2012-11-06 18:20:00 +00:00
|
|
|
|
LOG_WARNING(log, "Connection failed at try №"
|
|
|
|
|
<< (try_no + 1) << ", reason: " << fail_message.str());
|
2012-11-02 20:13:41 +00:00
|
|
|
|
|
2012-11-06 18:20:00 +00:00
|
|
|
|
fail_messages << fail_message.str() << std::endl;
|
|
|
|
|
|
2014-04-06 23:15:27 +00:00
|
|
|
|
__sync_fetch_and_add(&pool_ptrs[i]->random_error_count, 1);
|
|
|
|
|
__sync_fetch_and_add(&pool_ptrs[i]->nearest_hostname_error_count, 1);
|
2012-11-02 20:13:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-26 03:20:51 +00:00
|
|
|
|
throw Exception("All connection tries failed. Log: \n\n" + fail_messages.str() + "\n",
|
2012-11-02 20:13:41 +00:00
|
|
|
|
ErrorCodes::ALL_CONNECTION_TRIES_FAILED);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-06 23:15:27 +00:00
|
|
|
|
|
2012-11-02 20:13:41 +00:00
|
|
|
|
private:
|
|
|
|
|
struct PoolWithErrorCount
|
|
|
|
|
{
|
|
|
|
|
ConnectionPoolPtr pool;
|
2013-12-11 11:51:01 +00:00
|
|
|
|
|
2014-04-06 23:15:27 +00:00
|
|
|
|
UInt64 random_error_count = 0;
|
|
|
|
|
UInt32 random = 0;
|
2012-11-06 18:20:00 +00:00
|
|
|
|
drand48_data rand_state;
|
2013-12-11 11:51:01 +00:00
|
|
|
|
|
|
|
|
|
/// берётся имя локального сервера (Poco::Net::DNS::hostName) и имя хоста из конфига; строки обрезаются до минимальной длины;
|
|
|
|
|
/// затем считается количество отличающихся позиций
|
|
|
|
|
/// Пример example01-01-1 и example01-02-2 отличаются в двух позициях.
|
2014-04-06 23:15:27 +00:00
|
|
|
|
size_t hostname_difference = 0;
|
|
|
|
|
UInt64 nearest_hostname_error_count = 0;
|
2012-11-02 20:13:41 +00:00
|
|
|
|
|
2014-04-06 23:15:27 +00:00
|
|
|
|
PoolWithErrorCount(const ConnectionPoolPtr & pool_) : pool(pool_)
|
2012-11-06 18:20:00 +00:00
|
|
|
|
{
|
|
|
|
|
/// Инициализация плохая, но это не важно.
|
2014-04-06 23:15:27 +00:00
|
|
|
|
srand48_r(reinterpret_cast<intptr_t>(this), &rand_state);
|
2013-12-10 17:07:09 +00:00
|
|
|
|
|
|
|
|
|
std::string local_hostname = Poco::Net::DNS::hostName();
|
|
|
|
|
|
|
|
|
|
ConnectionPool & connection_pool = dynamic_cast<ConnectionPool &>(*pool);
|
|
|
|
|
const std::string & host = connection_pool.getHost();
|
|
|
|
|
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;
|
2012-11-06 18:20:00 +00:00
|
|
|
|
}
|
2014-04-06 23:15:27 +00:00
|
|
|
|
|
2012-11-06 18:20:00 +00:00
|
|
|
|
void randomize()
|
|
|
|
|
{
|
|
|
|
|
long int rand_res;
|
|
|
|
|
lrand48_r(&rand_state, &rand_res);
|
|
|
|
|
random = rand_res;
|
|
|
|
|
}
|
2012-11-02 20:13:41 +00:00
|
|
|
|
|
2014-02-17 23:56:45 +00:00
|
|
|
|
static bool compare(const PoolWithErrorCount & lhs, const PoolWithErrorCount & rhs, LoadBalancing load_balancing_mode)
|
2012-11-02 20:13:41 +00:00
|
|
|
|
{
|
2013-12-10 17:07:09 +00:00
|
|
|
|
if (load_balancing_mode == LoadBalancing::RANDOM)
|
|
|
|
|
{
|
2014-04-06 23:15:27 +00:00
|
|
|
|
return std::tie(lhs.random_error_count, lhs.random)
|
|
|
|
|
< std::tie(rhs.random_error_count, rhs.random);
|
2013-12-10 17:07:09 +00:00
|
|
|
|
}
|
|
|
|
|
else if (load_balancing_mode == LoadBalancing::NEAREST_HOSTNAME)
|
|
|
|
|
{
|
2014-04-06 23:15:27 +00:00
|
|
|
|
return std::tie(lhs.nearest_hostname_error_count, lhs.hostname_difference)
|
|
|
|
|
< std::tie(rhs.nearest_hostname_error_count, rhs.hostname_difference);
|
2013-12-10 17:07:09 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2014-04-06 23:15:27 +00:00
|
|
|
|
throw Exception("Unknown load_balancing_mode: " + toString(static_cast<int>(load_balancing_mode)), ErrorCodes::LOGICAL_ERROR);
|
2013-12-10 17:07:09 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-04-06 23:15:27 +00:00
|
|
|
|
|
2013-12-10 17:07:09 +00:00
|
|
|
|
class PoolsWithErrorCount : public std::vector<PoolWithErrorCount>
|
|
|
|
|
{
|
|
|
|
|
public:
|
2014-04-06 23:15:27 +00:00
|
|
|
|
PoolsWithErrorCount(DB::ConnectionPools::iterator begin_, DB::ConnectionPools::iterator end_,
|
|
|
|
|
time_t decrease_error_period_)
|
|
|
|
|
: std::vector<PoolWithErrorCount>(begin_, end_),
|
|
|
|
|
decrease_error_period(decrease_error_period_)
|
2013-12-10 17:07:09 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-17 23:56:45 +00:00
|
|
|
|
void update(LoadBalancing load_balancing_mode)
|
2013-12-10 17:07:09 +00:00
|
|
|
|
{
|
2014-04-06 23:15:27 +00:00
|
|
|
|
Poco::ScopedLock<Poco::FastMutex> lock(mutex);
|
|
|
|
|
|
|
|
|
|
switch (load_balancing_mode)
|
2013-12-10 17:07:09 +00:00
|
|
|
|
{
|
2014-04-06 23:15:27 +00:00
|
|
|
|
case LoadBalancing::RANDOM:
|
|
|
|
|
{
|
|
|
|
|
for (PoolsWithErrorCount::iterator it = begin(); it != end(); ++it)
|
|
|
|
|
it->randomize();
|
|
|
|
|
/// NOTE Почему бы не делить счётчики ошибок в случае LoadBalancing::RANDOM тоже?
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case LoadBalancing::NEAREST_HOSTNAME:
|
2013-12-10 17:07:09 +00:00
|
|
|
|
{
|
2014-04-06 23:15:27 +00:00
|
|
|
|
/// Для режима NEAREST_HOSTNAME каждые N секунд уменьшаем количество ошибок в 2 раза
|
|
|
|
|
time_t current_time = time(0);
|
|
|
|
|
|
|
|
|
|
if (last_decrease_time)
|
|
|
|
|
{
|
|
|
|
|
time_t delta = current_time - last_decrease_time;
|
|
|
|
|
|
|
|
|
|
if (delta < 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/// Каждые decrease_error_period секунд, делим количество ошибок на два.
|
|
|
|
|
size_t shift_amount = delta / decrease_error_period;
|
|
|
|
|
|
|
|
|
|
if (shift_amount > sizeof(UInt64))
|
|
|
|
|
{
|
|
|
|
|
last_decrease_time = current_time;
|
|
|
|
|
for (PoolsWithErrorCount::iterator it = begin(); it != end(); ++it)
|
|
|
|
|
it->nearest_hostname_error_count = 0;
|
|
|
|
|
}
|
|
|
|
|
else if (shift_amount)
|
|
|
|
|
{
|
|
|
|
|
last_decrease_time = current_time;
|
|
|
|
|
for (PoolsWithErrorCount::iterator it = begin(); it != end(); ++it)
|
|
|
|
|
it->nearest_hostname_error_count >>= shift_amount;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
last_decrease_time = current_time;
|
|
|
|
|
|
|
|
|
|
break;
|
2013-12-10 17:07:09 +00:00
|
|
|
|
}
|
2014-04-06 23:15:27 +00:00
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
throw Exception("Unknown load_balancing_mode: " + toString(static_cast<int>(load_balancing_mode)), ErrorCodes::LOGICAL_ERROR);
|
2013-12-10 17:07:09 +00:00
|
|
|
|
}
|
2012-11-02 20:13:41 +00:00
|
|
|
|
}
|
2013-12-10 17:07:09 +00:00
|
|
|
|
|
|
|
|
|
private:
|
2014-04-06 23:15:27 +00:00
|
|
|
|
/// Время, когда последний раз уменьшался счётчик ошибок для LoadBalancing::NEAREST_HOSTNAME
|
|
|
|
|
time_t last_decrease_time = 0;
|
2013-12-10 17:07:09 +00:00
|
|
|
|
time_t decrease_error_period;
|
2014-04-06 23:15:27 +00:00
|
|
|
|
Poco::FastMutex mutex;
|
2012-11-02 20:13:41 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
PoolsWithErrorCount nested_pools;
|
|
|
|
|
size_t max_tries;
|
|
|
|
|
Logger * log;
|
2014-02-17 23:56:45 +00:00
|
|
|
|
LoadBalancing default_load_balancing;
|
2012-11-02 20:13:41 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|