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
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
using Poco::SharedPtr;
|
|
|
|
|
|
|
|
|
|
|
2012-11-02 20:13:41 +00:00
|
|
|
|
/** Интерфейс для пулов соединений.
|
2012-10-22 19:55:19 +00:00
|
|
|
|
*
|
2012-11-02 20:13:41 +00:00
|
|
|
|
* Использование (на примере обычного ConnectionPool):
|
|
|
|
|
* ConnectionPool pool(...);
|
2012-10-22 19:55:19 +00:00
|
|
|
|
*
|
2012-11-02 20:13:41 +00:00
|
|
|
|
* void thread()
|
|
|
|
|
* {
|
2016-02-03 21:37:52 +00:00
|
|
|
|
* auto connection = pool.get();
|
2012-11-02 20:13:41 +00:00
|
|
|
|
* 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:
|
2014-06-03 14:32:04 +00:00
|
|
|
|
typedef PoolBase<Connection>::Entry Entry;
|
2015-10-12 14:53:16 +00:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
virtual ~IConnectionPool() {}
|
|
|
|
|
|
2015-10-12 15:05:30 +00:00
|
|
|
|
/** Выделяет соединение для работы. */
|
2015-10-12 14:53:16 +00:00
|
|
|
|
Entry get(const Settings * settings = nullptr)
|
|
|
|
|
{
|
|
|
|
|
return doGet(settings);
|
|
|
|
|
}
|
2014-12-30 14:11:02 +00:00
|
|
|
|
|
2015-01-29 14:22:15 +00:00
|
|
|
|
/** Выделяет до указанного количества соединений для работы.
|
2015-01-16 10:48:53 +00:00
|
|
|
|
* Соединения предоставляют доступ к разным репликам одного шарда.
|
2015-10-12 14:53:16 +00:00
|
|
|
|
* Если флаг get_all установлен, все соединения достаются.
|
2015-01-16 10:48:53 +00:00
|
|
|
|
* Выкидывает исключение, если не удалось выделить ни одного соединения.
|
|
|
|
|
*/
|
2015-10-12 14:53:16 +00:00
|
|
|
|
std::vector<Entry> getMany(const Settings * settings = nullptr, bool get_all = false)
|
2014-12-30 14:11:02 +00:00
|
|
|
|
{
|
2015-10-12 14:53:16 +00:00
|
|
|
|
return doGetMany(settings, get_all);
|
2014-12-30 14:11:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-12 14:53:16 +00:00
|
|
|
|
protected:
|
|
|
|
|
virtual Entry doGet(const Settings * settings) = 0;
|
|
|
|
|
|
|
|
|
|
virtual std::vector<Entry> doGetMany(const Settings * settings, bool get_all)
|
|
|
|
|
{
|
|
|
|
|
return std::vector<Entry>{ get(settings) };
|
|
|
|
|
}
|
2012-11-02 20:13:41 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef SharedPtr<IConnectionPool> ConnectionPoolPtr;
|
|
|
|
|
typedef std::vector<ConnectionPoolPtr> ConnectionPools;
|
2015-11-06 17:44:01 +00:00
|
|
|
|
typedef SharedPtr<ConnectionPools> ConnectionPoolsPtr;
|
2012-10-22 19:55:19 +00:00
|
|
|
|
|
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:
|
2014-06-03 14:32:04 +00:00
|
|
|
|
typedef IConnectionPool::Entry Entry;
|
|
|
|
|
typedef PoolBase<Connection> Base;
|
|
|
|
|
|
2012-10-22 19:55:19 +00:00
|
|
|
|
ConnectionPool(unsigned max_connections_,
|
2015-05-28 21:41:28 +00:00
|
|
|
|
const String & host_, UInt16 port_,
|
|
|
|
|
const String & default_database_,
|
2013-08-10 09:04:45 +00:00
|
|
|
|
const String & user_, const String & password_,
|
2012-10-22 19:55:19 +00:00
|
|
|
|
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))
|
2015-05-28 21:41:28 +00:00
|
|
|
|
: Base(max_connections_, &Logger::get("ConnectionPool (" + host_ + ":" + toString(port_) + ")")),
|
2014-06-03 14:32:04 +00:00
|
|
|
|
host(host_), port(port_), default_database(default_database_),
|
2015-05-28 21:41:28 +00:00
|
|
|
|
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_),
|
2015-05-28 03:49:28 +00:00
|
|
|
|
client_name(client_name_), compression(compression_),
|
2014-06-03 14:32:04 +00:00
|
|
|
|
connect_timeout(connect_timeout_), receive_timeout(receive_timeout_), send_timeout(send_timeout_)
|
2012-10-22 19:55:19 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-10 17:07:09 +00:00
|
|
|
|
const std::string & getHost() const
|
|
|
|
|
{
|
|
|
|
|
return host;
|
|
|
|
|
}
|
2012-10-22 19:55:19 +00:00
|
|
|
|
|
2014-06-03 14:32:04 +00:00
|
|
|
|
protected:
|
|
|
|
|
/** Создает новый объект для помещения в пул. */
|
|
|
|
|
ConnectionPtr allocObject() override
|
|
|
|
|
{
|
|
|
|
|
return new Connection(
|
2015-05-29 00:33:56 +00:00
|
|
|
|
host, port, resolved_address,
|
|
|
|
|
default_database, user, password,
|
2015-05-28 03:49:28 +00:00
|
|
|
|
client_name, compression,
|
2014-06-03 14:32:04 +00:00
|
|
|
|
connect_timeout, receive_timeout, send_timeout);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-12 14:53:16 +00:00
|
|
|
|
private:
|
|
|
|
|
Entry doGet(const Settings * settings) override
|
|
|
|
|
{
|
|
|
|
|
if (settings)
|
|
|
|
|
return Base::get(settings->queue_max_wait_ms.totalMilliseconds());
|
|
|
|
|
else
|
|
|
|
|
return Base::get(-1);
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
2012-10-22 19:55:19 +00:00
|
|
|
|
|
2015-05-29 00:33:56 +00:00
|
|
|
|
/** Адрес может быть заранее отрезолвен и передан в конструктор. Тогда поля host и port имеют смысл только для логгирования.
|
|
|
|
|
* Иначе адрес резолвится в конструкторе. То есть, DNS балансировка не поддерживается.
|
|
|
|
|
*/
|
2015-05-28 21:41:28 +00:00
|
|
|
|
Poco::Net::SocketAddress resolved_address;
|
|
|
|
|
|
2012-10-22 19:55:19 +00:00
|
|
|
|
String client_name;
|
|
|
|
|
Protocol::Compression::Enum compression; /// Сжимать ли данные при взаимодействии с сервером.
|
|
|
|
|
|
|
|
|
|
Poco::Timespan connect_timeout;
|
|
|
|
|
Poco::Timespan receive_timeout;
|
|
|
|
|
Poco::Timespan send_timeout;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|