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
|
|
|
|
2019-08-06 04:36:38 +00:00
|
|
|
#include <chrono>
|
|
|
|
#include <vector>
|
|
|
|
|
2012-11-02 20:13:41 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
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-19 17:40:55 +00:00
|
|
|
|
|
|
|
/// Specifies how many connections to return from ConnectionPoolWithFailover::getMany() method.
|
|
|
|
enum class PoolMode
|
|
|
|
{
|
|
|
|
/// Return exactly one connection.
|
|
|
|
GET_ONE = 0,
|
|
|
|
/// Return a number of connections, this number being determined by max_parallel_replicas setting.
|
|
|
|
GET_MANY,
|
|
|
|
/// Return a connection from each nested pool.
|
|
|
|
GET_ALL
|
|
|
|
};
|
|
|
|
|
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(
|
2017-04-19 17:40:55 +00:00
|
|
|
ConnectionPoolPtrs nested_pools_,
|
2017-04-17 16:16:04 +00:00
|
|
|
LoadBalancing load_balancing,
|
2019-08-06 04:36:38 +00:00
|
|
|
time_t decrease_error_period_ = DBMS_CONNECTION_POOL_WITH_FAILOVER_DEFAULT_DECREASE_ERROR_PERIOD,
|
|
|
|
size_t max_error_cap = DBMS_CONNECTION_POOL_WITH_FAILOVER_MAX_ERROR_COUNT);
|
2017-04-17 16:16:04 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
using Entry = IConnectionPool::Entry;
|
|
|
|
|
|
|
|
/** Allocates connection to work. */
|
2019-03-01 23:03:41 +00:00
|
|
|
Entry get(const ConnectionTimeouts & timeouts,
|
2020-03-18 03:27:32 +00:00
|
|
|
const Settings * settings,
|
|
|
|
bool force_connected) 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.
|
|
|
|
*/
|
2019-03-01 23:03:41 +00:00
|
|
|
std::vector<Entry> getMany(const ConnectionTimeouts & timeouts,
|
|
|
|
const Settings * settings, PoolMode pool_mode);
|
2017-04-17 16:16:04 +00:00
|
|
|
|
2018-07-25 12:31:47 +00:00
|
|
|
/// The same as getMany(), but return std::vector<TryResult>.
|
2019-03-01 23:03:41 +00:00
|
|
|
std::vector<TryResult> getManyForTableFunction(const ConnectionTimeouts & timeouts,
|
|
|
|
const Settings * settings, PoolMode pool_mode);
|
2018-07-24 13:10:34 +00:00
|
|
|
|
2017-07-28 19:34:25 +00:00
|
|
|
using Base = PoolWithFailoverBase<IConnectionPool>;
|
|
|
|
using TryResult = Base::TryResult;
|
|
|
|
|
2017-04-19 18:29:50 +00:00
|
|
|
/// The same as getMany(), but check that replication delay for table_to_check is acceptable.
|
|
|
|
/// Delay threshold is taken from settings.
|
2017-07-28 19:34:25 +00:00
|
|
|
std::vector<TryResult> getManyChecked(
|
2019-03-01 23:03:41 +00:00
|
|
|
const ConnectionTimeouts & timeouts,
|
|
|
|
const Settings * settings,
|
|
|
|
PoolMode pool_mode,
|
|
|
|
const QualifiedTableName & table_to_check);
|
2017-04-19 17:40:55 +00:00
|
|
|
|
2019-08-06 04:36:38 +00:00
|
|
|
struct NestedPoolStatus
|
|
|
|
{
|
|
|
|
const IConnectionPool * pool;
|
|
|
|
size_t error_count;
|
|
|
|
std::chrono::seconds estimated_recovery_time;
|
|
|
|
};
|
|
|
|
|
|
|
|
using Status = std::vector<NestedPoolStatus>;
|
|
|
|
Status getStatus() const;
|
|
|
|
|
2017-04-19 17:40:55 +00:00
|
|
|
private:
|
2017-04-19 18:29:50 +00:00
|
|
|
/// Get the values of relevant settings and call Base::getMany()
|
2017-07-28 19:34:25 +00:00
|
|
|
std::vector<TryResult> getManyImpl(
|
2017-04-17 16:16:04 +00:00
|
|
|
const Settings * settings,
|
|
|
|
PoolMode pool_mode,
|
2017-04-19 18:29:50 +00:00
|
|
|
const TryGetEntryFunc & try_get_entry);
|
2017-04-17 16:16:04 +00:00
|
|
|
|
2017-04-19 18:29:50 +00:00
|
|
|
/// Try to get a connection from the pool and check that it is good.
|
|
|
|
/// If table_to_check is not null and the check is enabled in settings, check that replication delay
|
|
|
|
/// for this table is not too large.
|
|
|
|
TryResult tryGetEntry(
|
2017-04-17 16:16:04 +00:00
|
|
|
IConnectionPool & pool,
|
2019-03-01 23:03:41 +00:00
|
|
|
const ConnectionTimeouts & timeouts,
|
2017-04-17 16:16:04 +00:00
|
|
|
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.
|
2020-06-13 17:23:51 +00:00
|
|
|
size_t last_used = 0; /// Last used for round_robin policy.
|
2017-04-01 07:20:54 +00:00
|
|
|
LoadBalancing default_load_balancing;
|
2012-11-02 20:13:41 +00:00
|
|
|
};
|
|
|
|
|
2017-04-19 17:40:55 +00:00
|
|
|
using ConnectionPoolWithFailoverPtr = std::shared_ptr<ConnectionPoolWithFailover>;
|
|
|
|
using ConnectionPoolWithFailoverPtrs = std::vector<ConnectionPoolWithFailoverPtr>;
|
2012-11-02 20:13:41 +00:00
|
|
|
|
|
|
|
}
|