2012-10-22 19:55:19 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/PoolBase.h>
|
|
|
|
#include <Client/Connection.h>
|
2017-12-27 17:58:52 +00:00
|
|
|
#include <IO/ConnectionTimeouts.h>
|
2020-12-10 22:05:02 +00:00
|
|
|
#include <Core/Settings.h>
|
2022-06-14 22:35:55 +00:00
|
|
|
#include <base/defines.h>
|
2012-10-22 19:55:19 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-03-09 00:56:38 +00:00
|
|
|
/** Interface for connection pools.
|
2012-10-22 19:55:19 +00:00
|
|
|
*
|
2017-03-09 00:56:38 +00:00
|
|
|
* Usage (using the usual `ConnectionPool` example)
|
2012-11-02 20:13:41 +00:00
|
|
|
* ConnectionPool pool(...);
|
2012-10-22 19:55:19 +00:00
|
|
|
*
|
2012-11-02 20:13:41 +00:00
|
|
|
* void thread()
|
|
|
|
* {
|
2020-05-17 05:45:20 +00:00
|
|
|
* auto connection = pool.get();
|
|
|
|
* connection->sendQuery(...);
|
2012-11-02 20:13:41 +00:00
|
|
|
* }
|
2012-10-22 19:55:19 +00:00
|
|
|
*/
|
2017-04-17 16:16:04 +00:00
|
|
|
|
2012-11-02 20:13:41 +00:00
|
|
|
class IConnectionPool : private boost::noncopyable
|
2012-10-22 19:55:19 +00:00
|
|
|
{
|
|
|
|
public:
|
2016-05-28 10:15:36 +00:00
|
|
|
using Entry = PoolBase<Connection>::Entry;
|
2015-10-12 14:53:16 +00:00
|
|
|
|
2021-03-19 21:49:18 +00:00
|
|
|
virtual ~IConnectionPool() = default;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-06-14 19:02:25 +00:00
|
|
|
/// Selects the connection to work.
|
|
|
|
/// If force_connected is false, the client must manually ensure that returned connection is good.
|
2022-03-11 18:16:49 +00:00
|
|
|
virtual Entry get(const ConnectionTimeouts & timeouts, /// NOLINT
|
2019-03-01 23:00:01 +00:00
|
|
|
const Settings * settings = nullptr,
|
|
|
|
bool force_connected = true) = 0;
|
2020-06-27 06:52:10 +00:00
|
|
|
|
|
|
|
virtual Int64 getPriority() const { return 1; }
|
2012-11-02 20:13:41 +00:00
|
|
|
};
|
|
|
|
|
2016-05-28 10:15:36 +00:00
|
|
|
using ConnectionPoolPtr = std::shared_ptr<IConnectionPool>;
|
2017-04-19 17:40:55 +00:00
|
|
|
using ConnectionPoolPtrs = std::vector<ConnectionPoolPtr>;
|
2012-11-02 20:13:41 +00:00
|
|
|
|
2017-03-09 00:56:38 +00:00
|
|
|
/** A common connection pool, without fault tolerance.
|
2012-11-02 20:13:41 +00:00
|
|
|
*/
|
2017-04-19 17:40:55 +00:00
|
|
|
class ConnectionPool : public IConnectionPool, private PoolBase<Connection>
|
2012-11-02 20:13:41 +00:00
|
|
|
{
|
|
|
|
public:
|
2016-05-28 10:15:36 +00:00
|
|
|
using Entry = IConnectionPool::Entry;
|
|
|
|
using Base = PoolBase<Connection>;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2012-10-22 19:55:19 +00:00
|
|
|
ConnectionPool(unsigned max_connections_,
|
2018-06-19 18:09:09 +00:00
|
|
|
const String & host_,
|
|
|
|
UInt16 port_,
|
2015-05-28 21:41:28 +00:00
|
|
|
const String & default_database_,
|
2018-06-19 18:09:09 +00:00
|
|
|
const String & user_,
|
|
|
|
const String & password_,
|
2022-08-03 19:44:08 +00:00
|
|
|
const String & quota_key_,
|
2020-09-14 21:55:43 +00:00
|
|
|
const String & cluster_,
|
|
|
|
const String & cluster_secret_,
|
2021-03-29 00:39:10 +00:00
|
|
|
const String & client_name_,
|
|
|
|
Protocol::Compression compression_,
|
|
|
|
Protocol::Secure secure_,
|
2020-06-27 06:52:10 +00:00
|
|
|
Int64 priority_ = 1)
|
2018-06-19 18:09:09 +00:00
|
|
|
: Base(max_connections_,
|
2020-05-30 21:57:37 +00:00
|
|
|
&Poco::Logger::get("ConnectionPool (" + host_ + ":" + toString(port_) + ")")),
|
2018-06-19 18:09:09 +00:00
|
|
|
host(host_),
|
|
|
|
port(port_),
|
|
|
|
default_database(default_database_),
|
|
|
|
user(user_),
|
|
|
|
password(password_),
|
2022-08-03 19:44:08 +00:00
|
|
|
quota_key(quota_key_),
|
2020-09-14 21:55:43 +00:00
|
|
|
cluster(cluster_),
|
|
|
|
cluster_secret(cluster_secret_),
|
2018-06-19 18:09:09 +00:00
|
|
|
client_name(client_name_),
|
|
|
|
compression(compression_),
|
2020-06-27 06:52:10 +00:00
|
|
|
secure(secure_),
|
|
|
|
priority(priority_)
|
2012-10-22 19:55:19 +00:00
|
|
|
{
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-03-11 18:16:49 +00:00
|
|
|
Entry get(const ConnectionTimeouts & timeouts, /// NOLINT
|
2019-03-01 23:00:01 +00:00
|
|
|
const Settings * settings = nullptr,
|
|
|
|
bool force_connected = true) override
|
2017-04-19 17:40:55 +00:00
|
|
|
{
|
2017-06-14 19:02:25 +00:00
|
|
|
Entry entry;
|
2017-04-19 17:40:55 +00:00
|
|
|
if (settings)
|
2019-08-28 15:27:26 +00:00
|
|
|
entry = Base::get(settings->connection_pool_max_wait_ms.totalMilliseconds());
|
2017-04-19 17:40:55 +00:00
|
|
|
else
|
2017-06-14 19:02:25 +00:00
|
|
|
entry = Base::get(-1);
|
|
|
|
|
|
|
|
if (force_connected)
|
2019-03-01 23:00:01 +00:00
|
|
|
entry->forceConnected(timeouts);
|
2017-06-14 19:02:25 +00:00
|
|
|
|
|
|
|
return entry;
|
2017-04-19 17:40:55 +00:00
|
|
|
}
|
|
|
|
|
2013-12-10 17:07:09 +00:00
|
|
|
const std::string & getHost() const
|
|
|
|
{
|
|
|
|
return host;
|
|
|
|
}
|
2019-08-21 08:53:41 +00:00
|
|
|
std::string getDescription() const
|
|
|
|
{
|
|
|
|
return host + ":" + toString(port);
|
|
|
|
}
|
2012-10-22 19:55:19 +00:00
|
|
|
|
2020-06-27 06:52:10 +00:00
|
|
|
Int64 getPriority() const override
|
|
|
|
{
|
|
|
|
return priority;
|
|
|
|
}
|
|
|
|
|
2014-06-03 14:32:04 +00:00
|
|
|
protected:
|
2017-03-09 04:26:17 +00:00
|
|
|
/** Creates a new object to put in the pool. */
|
2014-06-03 14:32:04 +00:00
|
|
|
ConnectionPtr allocObject() override
|
|
|
|
{
|
2016-05-28 10:15:36 +00:00
|
|
|
return std::make_shared<Connection>(
|
2018-03-29 20:21:01 +00:00
|
|
|
host, port,
|
2022-08-03 19:44:08 +00:00
|
|
|
default_database, user, password, quota_key,
|
2020-09-14 21:55:43 +00:00
|
|
|
cluster, cluster_secret,
|
2018-03-29 01:41:06 +00:00
|
|
|
client_name, compression, secure);
|
2014-06-03 14:32:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2012-10-22 19:55:19 +00:00
|
|
|
String host;
|
|
|
|
UInt16 port;
|
|
|
|
String default_database;
|
2013-08-10 09:04:45 +00:00
|
|
|
String user;
|
|
|
|
String password;
|
2022-08-03 19:44:08 +00:00
|
|
|
String quota_key;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-09-14 21:55:43 +00:00
|
|
|
/// For inter-server authorization
|
|
|
|
String cluster;
|
|
|
|
String cluster_secret;
|
|
|
|
|
2012-10-22 19:55:19 +00:00
|
|
|
String client_name;
|
2020-06-27 06:52:10 +00:00
|
|
|
Protocol::Compression compression; /// Whether to compress data when interacting with the server.
|
|
|
|
Protocol::Secure secure; /// Whether to encrypt data when interacting with the server.
|
|
|
|
Int64 priority; /// priority from <remote_servers>
|
2012-10-22 19:55:19 +00:00
|
|
|
};
|
|
|
|
|
2021-07-01 10:18:29 +00:00
|
|
|
/**
|
|
|
|
* Connection pool factory. Responsible for creating new connection pools and reuse existing ones.
|
|
|
|
*/
|
|
|
|
class ConnectionPoolFactory final : private boost::noncopyable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
struct Key
|
|
|
|
{
|
|
|
|
unsigned max_connections;
|
|
|
|
String host;
|
|
|
|
UInt16 port;
|
|
|
|
String default_database;
|
|
|
|
String user;
|
|
|
|
String password;
|
2022-08-03 19:44:08 +00:00
|
|
|
String quota_key;
|
2021-07-01 10:18:29 +00:00
|
|
|
String cluster;
|
|
|
|
String cluster_secret;
|
|
|
|
String client_name;
|
|
|
|
Protocol::Compression compression;
|
|
|
|
Protocol::Secure secure;
|
|
|
|
Int64 priority;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct KeyHash
|
|
|
|
{
|
|
|
|
size_t operator()(const ConnectionPoolFactory::Key & k) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
static ConnectionPoolFactory & instance();
|
|
|
|
|
|
|
|
ConnectionPoolPtr
|
|
|
|
get(unsigned max_connections,
|
|
|
|
String host,
|
|
|
|
UInt16 port,
|
|
|
|
String default_database,
|
|
|
|
String user,
|
|
|
|
String password,
|
2022-08-03 19:44:08 +00:00
|
|
|
String quota_key,
|
2021-07-01 10:18:29 +00:00
|
|
|
String cluster,
|
|
|
|
String cluster_secret,
|
|
|
|
String client_name,
|
|
|
|
Protocol::Compression compression,
|
|
|
|
Protocol::Secure secure,
|
|
|
|
Int64 priority);
|
|
|
|
private:
|
|
|
|
mutable std::mutex mutex;
|
|
|
|
using ConnectionPoolWeakPtr = std::weak_ptr<IConnectionPool>;
|
2022-06-14 22:35:55 +00:00
|
|
|
std::unordered_map<Key, ConnectionPoolWeakPtr, KeyHash> pools TSA_GUARDED_BY(mutex);
|
2021-07-01 10:18:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
inline bool operator==(const ConnectionPoolFactory::Key & lhs, const ConnectionPoolFactory::Key & rhs)
|
|
|
|
{
|
|
|
|
return lhs.max_connections == rhs.max_connections && lhs.host == rhs.host && lhs.port == rhs.port
|
|
|
|
&& lhs.default_database == rhs.default_database && lhs.user == rhs.user && lhs.password == rhs.password
|
2023-05-16 19:14:54 +00:00
|
|
|
&& lhs.quota_key == rhs.quota_key
|
2021-07-01 10:18:29 +00:00
|
|
|
&& lhs.cluster == rhs.cluster && lhs.cluster_secret == rhs.cluster_secret && lhs.client_name == rhs.client_name
|
|
|
|
&& lhs.compression == rhs.compression && lhs.secure == rhs.secure && lhs.priority == rhs.priority;
|
|
|
|
}
|
|
|
|
|
2012-10-22 19:55:19 +00:00
|
|
|
}
|