2012-10-22 19:55:19 +00:00
|
|
|
#pragma once
|
|
|
|
|
2015-10-05 01:26:43 +00:00
|
|
|
#include <DB/Common/PoolBase.h>
|
2012-10-22 19:55:19 +00:00
|
|
|
|
|
|
|
#include <DB/Client/Connection.h>
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
*
|
2017-04-01 07:20:54 +00:00
|
|
|
* void thread()
|
|
|
|
* {
|
|
|
|
* auto connection = pool.get();
|
|
|
|
* connection->sendQuery("SELECT 'Hello, world!' AS world");
|
|
|
|
* }
|
2012-10-22 19:55:19 +00:00
|
|
|
*/
|
2012-11-02 20:13:41 +00:00
|
|
|
class IConnectionPool : private boost::noncopyable
|
2012-10-22 19:55:19 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
using Entry = PoolBase<Connection>::Entry;
|
2015-10-12 14:53:16 +00:00
|
|
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
virtual ~IConnectionPool() {}
|
|
|
|
|
|
|
|
/** Selects the connection to work. */
|
|
|
|
Entry get(const Settings * settings = nullptr)
|
|
|
|
{
|
|
|
|
return doGet(settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Allocates up to the specified number of connections to work.
|
|
|
|
* Connections provide access to different replicas of one shard.
|
|
|
|
* If `get_all` flag is set, all connections are taken.
|
|
|
|
* Throws an exception if no connections can be selected.
|
|
|
|
*/
|
|
|
|
std::vector<Entry> getMany(const Settings * settings = nullptr,
|
|
|
|
PoolMode pool_mode = PoolMode::GET_MANY)
|
|
|
|
{
|
|
|
|
return doGetMany(settings, pool_mode);
|
|
|
|
}
|
2014-12-30 14:11:02 +00:00
|
|
|
|
2015-10-12 14:53:16 +00:00
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
virtual Entry doGet(const Settings * settings) = 0;
|
2015-10-12 14:53:16 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
virtual std::vector<Entry> doGetMany(const Settings * settings, PoolMode pool_mode)
|
|
|
|
{
|
|
|
|
return std::vector<Entry>{ get(settings) };
|
|
|
|
}
|
2012-11-02 20:13:41 +00:00
|
|
|
};
|
|
|
|
|
2016-05-28 10:15:36 +00:00
|
|
|
using ConnectionPoolPtr = std::shared_ptr<IConnectionPool>;
|
|
|
|
using ConnectionPools = std::vector<ConnectionPoolPtr>;
|
|
|
|
using ConnectionPoolsPtr = std::shared_ptr<ConnectionPools>;
|
2012-10-22 19:55:19 +00:00
|
|
|
|
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
|
|
|
*/
|
2014-06-03 14:32:04 +00:00
|
|
|
class ConnectionPool : public PoolBase<Connection>, public IConnectionPool
|
2012-11-02 20:13:41 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
using Entry = IConnectionPool::Entry;
|
|
|
|
using Base = PoolBase<Connection>;
|
|
|
|
|
|
|
|
ConnectionPool(unsigned max_connections_,
|
|
|
|
const String & host_, UInt16 port_,
|
|
|
|
const String & default_database_,
|
|
|
|
const String & user_, const String & password_,
|
|
|
|
const String & client_name_ = "client",
|
|
|
|
Protocol::Compression::Enum compression_ = Protocol::Compression::Enable,
|
|
|
|
Poco::Timespan connect_timeout_ = Poco::Timespan(DBMS_DEFAULT_CONNECT_TIMEOUT_SEC, 0),
|
|
|
|
Poco::Timespan receive_timeout_ = Poco::Timespan(DBMS_DEFAULT_RECEIVE_TIMEOUT_SEC, 0),
|
|
|
|
Poco::Timespan send_timeout_ = Poco::Timespan(DBMS_DEFAULT_SEND_TIMEOUT_SEC, 0))
|
|
|
|
: Base(max_connections_, &Logger::get("ConnectionPool (" + host_ + ":" + toString(port_) + ")")),
|
|
|
|
host(host_), port(port_), default_database(default_database_),
|
|
|
|
user(user_), password(password_), resolved_address(host_, port_),
|
|
|
|
client_name(client_name_), compression(compression_),
|
|
|
|
connect_timeout(connect_timeout_), receive_timeout(receive_timeout_), send_timeout(send_timeout_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ConnectionPool(unsigned max_connections_,
|
|
|
|
const String & host_, UInt16 port_, const Poco::Net::SocketAddress & resolved_address_,
|
|
|
|
const String & default_database_,
|
|
|
|
const String & user_, const String & password_,
|
|
|
|
const String & client_name_ = "client",
|
|
|
|
Protocol::Compression::Enum compression_ = Protocol::Compression::Enable,
|
|
|
|
Poco::Timespan connect_timeout_ = Poco::Timespan(DBMS_DEFAULT_CONNECT_TIMEOUT_SEC, 0),
|
|
|
|
Poco::Timespan receive_timeout_ = Poco::Timespan(DBMS_DEFAULT_RECEIVE_TIMEOUT_SEC, 0),
|
|
|
|
Poco::Timespan send_timeout_ = Poco::Timespan(DBMS_DEFAULT_SEND_TIMEOUT_SEC, 0))
|
|
|
|
: Base(max_connections_, &Logger::get("ConnectionPool (" + host_ + ":" + toString(port_) + ")")),
|
|
|
|
host(host_), port(port_), default_database(default_database_),
|
|
|
|
user(user_), password(password_), resolved_address(resolved_address_),
|
|
|
|
client_name(client_name_), compression(compression_),
|
|
|
|
connect_timeout(connect_timeout_), receive_timeout(receive_timeout_), send_timeout(send_timeout_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string & getHost() const
|
|
|
|
{
|
|
|
|
return host;
|
|
|
|
}
|
2012-10-22 19:55:19 +00:00
|
|
|
|
2014-06-03 14:32:04 +00:00
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
/** Creates a new object to put in the pool. */
|
|
|
|
ConnectionPtr allocObject() override
|
|
|
|
{
|
|
|
|
return std::make_shared<Connection>(
|
|
|
|
host, port, resolved_address,
|
|
|
|
default_database, user, password,
|
|
|
|
client_name, compression,
|
|
|
|
connect_timeout, receive_timeout, send_timeout);
|
|
|
|
}
|
2014-06-03 14:32:04 +00:00
|
|
|
|
2015-10-12 14:53:16 +00:00
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
Entry doGet(const Settings * settings) override
|
|
|
|
{
|
|
|
|
if (settings)
|
|
|
|
return Base::get(settings->queue_max_wait_ms.totalMilliseconds());
|
|
|
|
else
|
|
|
|
return Base::get(-1);
|
|
|
|
}
|
2015-10-12 14:53:16 +00:00
|
|
|
|
2014-06-03 14:32:04 +00:00
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
String host;
|
|
|
|
UInt16 port;
|
|
|
|
String default_database;
|
|
|
|
String user;
|
|
|
|
String password;
|
|
|
|
|
|
|
|
/** The address can be resolved in advance and passed to the constructor. Then `host` and `port` fields are meaningful only for logging.
|
|
|
|
* Otherwise, address is resolved in constructor. That is, DNS balancing is not supported.
|
|
|
|
*/
|
|
|
|
Poco::Net::SocketAddress resolved_address;
|
|
|
|
|
|
|
|
String client_name;
|
|
|
|
Protocol::Compression::Enum compression; /// Whether to compress data when interacting with the server.
|
|
|
|
|
|
|
|
Poco::Timespan connect_timeout;
|
|
|
|
Poco::Timespan receive_timeout;
|
|
|
|
Poco::Timespan send_timeout;
|
2012-10-22 19:55:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|