2012-11-02 20:13:41 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/PoolWithFailoverBase.h>
|
|
|
|
#include <Client/ConnectionPool.h>
|
2012-11-02 20:13:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int NETWORK_ERROR;
|
|
|
|
extern const int SOCKET_TIMEOUT;
|
|
|
|
extern const int LOGICAL_ERROR;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2017-04-17 16:16:04 +00:00
|
|
|
class ConnectionPoolWithFailover : public IConnectionPool, private PoolWithFailoverBase<IConnectionPool>
|
2012-11-02 20:13:41 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-04-17 16:16:04 +00:00
|
|
|
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);
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
using Entry = IConnectionPool::Entry;
|
|
|
|
|
2017-04-17 16:16:04 +00:00
|
|
|
std::vector<Entry> getManyChecked(
|
|
|
|
const Settings * settings, PoolMode pool_mode, const QualifiedTableName & table_to_check);
|
2012-11-02 20:13:41 +00:00
|
|
|
|
2014-06-03 14:32:04 +00:00
|
|
|
private:
|
2017-04-17 16:16:04 +00:00
|
|
|
using Base = PoolWithFailoverBase<IConnectionPool>;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/** Allocates connection to work. */
|
2017-04-17 16:16:04 +00:00
|
|
|
Entry doGet(const Settings * settings) override; /// From IConnectionPool
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/** Allocates up to the specified number of connections to work.
|
|
|
|
* Connections provide access to different replicas of one shard.
|
|
|
|
*/
|
2017-04-17 16:16:04 +00:00
|
|
|
std::vector<Entry> doGetMany(const Settings * settings, PoolMode pool_mode) override; /// From IConnectionPool
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<Entry> getManyImpl(
|
|
|
|
const Settings * settings,
|
|
|
|
PoolMode pool_mode,
|
|
|
|
const Base::TryGetEntryFunc & try_get_entry);
|
|
|
|
|
|
|
|
Base::TryResult tryGetEntry(
|
|
|
|
IConnectionPool & pool,
|
|
|
|
std::string & fail_message,
|
|
|
|
const Settings * settings,
|
|
|
|
const QualifiedTableName * table_to_check = nullptr);
|
2015-10-12 14:53:16 +00:00
|
|
|
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
std::vector<size_t> hostname_differences; /// Distances from name of this host to the names of hosts of pools.
|
|
|
|
LoadBalancing default_load_balancing;
|
2012-11-02 20:13:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|