2012-05-21 20:38:34 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
2015-09-29 19:19:54 +00:00
|
|
|
|
#include <common/logger_useful.h>
|
2012-10-16 19:20:58 +00:00
|
|
|
|
|
2012-05-16 18:03:00 +00:00
|
|
|
|
#include <Poco/Net/StreamSocket.h>
|
|
|
|
|
|
2015-02-10 20:48:17 +00:00
|
|
|
|
#include <DB/Common/Throttler.h>
|
|
|
|
|
|
2012-05-16 18:03:00 +00:00
|
|
|
|
#include <DB/Core/Block.h>
|
2014-03-13 15:00:06 +00:00
|
|
|
|
#include <DB/Core/Defines.h>
|
2012-05-16 18:03:00 +00:00
|
|
|
|
#include <DB/Core/Progress.h>
|
|
|
|
|
#include <DB/Core/Protocol.h>
|
|
|
|
|
#include <DB/Core/QueryProcessingStage.h>
|
|
|
|
|
|
|
|
|
|
#include <DB/DataStreams/IBlockInputStream.h>
|
|
|
|
|
#include <DB/DataStreams/IBlockOutputStream.h>
|
2015-01-18 08:25:56 +00:00
|
|
|
|
#include <DB/DataStreams/BlockStreamProfileInfo.h>
|
2012-05-16 18:03:00 +00:00
|
|
|
|
|
2013-02-01 19:02:04 +00:00
|
|
|
|
#include <DB/Interpreters/Settings.h>
|
|
|
|
|
|
2014-10-08 19:00:25 +00:00
|
|
|
|
#include <atomic>
|
|
|
|
|
|
2012-05-16 18:03:00 +00:00
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
2016-10-24 21:40:39 +00:00
|
|
|
|
class ClientInfo;
|
|
|
|
|
|
2014-03-13 15:00:06 +00:00
|
|
|
|
/// Поток блоков читающих из таблицы и ее имя
|
2016-05-28 10:15:36 +00:00
|
|
|
|
using ExternalTableData = std::pair<BlockInputStreamPtr, std::string>;
|
2014-03-13 15:00:06 +00:00
|
|
|
|
/// Вектор пар, описывающих таблицы
|
2016-05-28 10:15:36 +00:00
|
|
|
|
using ExternalTablesData = std::vector<ExternalTableData>;
|
|
|
|
|
|
|
|
|
|
class Connection;
|
|
|
|
|
|
|
|
|
|
using ConnectionPtr = std::shared_ptr<Connection>;
|
|
|
|
|
using Connections = std::vector<ConnectionPtr>;
|
2012-05-16 18:03:00 +00:00
|
|
|
|
|
2015-02-10 20:48:17 +00:00
|
|
|
|
|
2016-07-31 03:53:16 +00:00
|
|
|
|
/** Connection with database server, to use by client.
|
|
|
|
|
* How to use - see Core/Protocol.h
|
|
|
|
|
* (Implementation of server end - see Server/TCPHandler.h)
|
2012-05-30 06:46:57 +00:00
|
|
|
|
*
|
2016-07-31 03:53:16 +00:00
|
|
|
|
* As 'default_database' empty string could be passed
|
|
|
|
|
* - in that case, server will use it's own default database.
|
2012-05-16 18:03:00 +00:00
|
|
|
|
*/
|
2012-10-22 19:03:01 +00:00
|
|
|
|
class Connection : private boost::noncopyable
|
2012-05-16 18:03:00 +00:00
|
|
|
|
{
|
2015-02-04 10:27:06 +00:00
|
|
|
|
friend class ParallelReplicas;
|
2015-11-06 17:44:01 +00:00
|
|
|
|
friend class MultiplexedConnections;
|
2015-01-14 10:06:30 +00:00
|
|
|
|
|
2012-05-16 18:03:00 +00:00
|
|
|
|
public:
|
2012-05-30 06:46:57 +00:00
|
|
|
|
Connection(const String & host_, UInt16 port_, const String & default_database_,
|
2013-08-10 09:04:45 +00:00
|
|
|
|
const String & user_, const String & password_,
|
2012-05-23 19:51:30 +00:00
|
|
|
|
const String & client_name_ = "client",
|
2012-07-26 19:42:20 +00:00
|
|
|
|
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),
|
2015-03-06 09:37:16 +00:00
|
|
|
|
Poco::Timespan send_timeout_ = Poco::Timespan(DBMS_DEFAULT_SEND_TIMEOUT_SEC, 0),
|
|
|
|
|
Poco::Timespan ping_timeout_ = Poco::Timespan(DBMS_DEFAULT_PING_TIMEOUT_SEC, 0))
|
2013-08-10 09:04:45 +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_),
|
|
|
|
|
ping_timeout(ping_timeout_),
|
|
|
|
|
log_wrapper(*this)
|
|
|
|
|
{
|
2016-07-31 03:53:16 +00:00
|
|
|
|
/// Don't connect immediately, only on first need.
|
2015-05-28 21:41:28 +00:00
|
|
|
|
|
|
|
|
|
if (user.empty())
|
|
|
|
|
user = "default";
|
2015-05-29 00:33:56 +00:00
|
|
|
|
|
|
|
|
|
setDescription();
|
2015-05-28 21:41:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Connection(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),
|
|
|
|
|
Poco::Timespan ping_timeout_ = Poco::Timespan(DBMS_DEFAULT_PING_TIMEOUT_SEC, 0))
|
|
|
|
|
:
|
|
|
|
|
host(host_), port(port_),
|
|
|
|
|
default_database(default_database_),
|
2013-08-10 09:04:45 +00:00
|
|
|
|
user(user_), password(password_),
|
2015-05-28 21:41:28 +00:00
|
|
|
|
resolved_address(resolved_address_),
|
2015-02-01 07:21:19 +00:00
|
|
|
|
client_name(client_name_),
|
2015-05-28 03:49:28 +00:00
|
|
|
|
compression(compression_),
|
2012-10-16 19:20:58 +00:00
|
|
|
|
connect_timeout(connect_timeout_), receive_timeout(receive_timeout_), send_timeout(send_timeout_),
|
2015-03-06 09:37:16 +00:00
|
|
|
|
ping_timeout(ping_timeout_),
|
2015-05-28 21:41:28 +00:00
|
|
|
|
log_wrapper(*this)
|
2012-05-16 18:03:00 +00:00
|
|
|
|
{
|
2016-07-31 03:53:16 +00:00
|
|
|
|
/// Don't connect immediately, only on first need.
|
2013-08-10 09:04:45 +00:00
|
|
|
|
|
|
|
|
|
if (user.empty())
|
|
|
|
|
user = "default";
|
2015-05-29 00:33:56 +00:00
|
|
|
|
|
|
|
|
|
setDescription();
|
2012-05-16 18:03:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual ~Connection() {};
|
|
|
|
|
|
2016-07-31 03:53:16 +00:00
|
|
|
|
/// Set throttler of network traffic. One throttler could be used for multiple connections to limit total traffic.
|
2015-02-10 20:48:17 +00:00
|
|
|
|
void setThrottler(const ThrottlerPtr & throttler_)
|
|
|
|
|
{
|
|
|
|
|
throttler = throttler_;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-16 18:03:00 +00:00
|
|
|
|
|
2016-07-31 03:53:16 +00:00
|
|
|
|
/// Packet that could be received from server.
|
2012-05-16 18:03:00 +00:00
|
|
|
|
struct Packet
|
|
|
|
|
{
|
|
|
|
|
UInt64 type;
|
|
|
|
|
|
|
|
|
|
Block block;
|
2016-05-28 14:14:18 +00:00
|
|
|
|
std::unique_ptr<Exception> exception;
|
2012-05-16 18:03:00 +00:00
|
|
|
|
Progress progress;
|
2013-05-22 14:57:43 +00:00
|
|
|
|
BlockStreamProfileInfo profile_info;
|
2012-05-16 18:03:00 +00:00
|
|
|
|
|
|
|
|
|
Packet() : type(Protocol::Server::Hello) {}
|
|
|
|
|
};
|
|
|
|
|
|
2016-07-31 03:53:16 +00:00
|
|
|
|
/// Change default database. Changes will take effect on next reconnect.
|
2014-04-18 13:59:39 +00:00
|
|
|
|
void setDefaultDatabase(const String & database);
|
|
|
|
|
|
2012-05-16 18:20:45 +00:00
|
|
|
|
void getServerVersion(String & name, UInt64 & version_major, UInt64 & version_minor, UInt64 & revision);
|
2012-05-16 18:03:00 +00:00
|
|
|
|
|
2016-11-17 00:12:08 +00:00
|
|
|
|
const String & getServerTimezone();
|
2016-11-13 19:34:31 +00:00
|
|
|
|
|
2016-07-31 03:53:16 +00:00
|
|
|
|
/// For log and exception messages.
|
2016-06-08 14:39:49 +00:00
|
|
|
|
const String & getDescription() const;
|
|
|
|
|
const String & getHost() const;
|
|
|
|
|
UInt16 getPort() const;
|
|
|
|
|
const String & getDefaultDatabase() const;
|
2015-05-28 21:41:28 +00:00
|
|
|
|
|
2016-07-31 03:53:16 +00:00
|
|
|
|
/// If last flag is true, you need to call sendExternalTablesData after.
|
2016-10-24 21:40:39 +00:00
|
|
|
|
void sendQuery(
|
|
|
|
|
const String & query,
|
|
|
|
|
const String & query_id_ = "",
|
|
|
|
|
UInt64 stage = QueryProcessingStage::Complete,
|
|
|
|
|
const Settings * settings = nullptr,
|
|
|
|
|
const ClientInfo * client_info = nullptr,
|
|
|
|
|
bool with_pending_data = false);
|
2014-07-11 00:29:59 +00:00
|
|
|
|
|
2012-05-16 18:03:00 +00:00
|
|
|
|
void sendCancel();
|
2016-07-31 03:53:16 +00:00
|
|
|
|
/// Send block of data; if name is specified, server will write it to external (temporary) table of that name.
|
2014-03-06 14:02:20 +00:00
|
|
|
|
void sendData(const Block & block, const String & name = "");
|
2016-07-31 03:53:16 +00:00
|
|
|
|
/// Send all contents of external (temporary) tables.
|
2014-03-14 15:42:30 +00:00
|
|
|
|
void sendExternalTablesData(ExternalTablesData & data);
|
2012-05-16 18:03:00 +00:00
|
|
|
|
|
2016-07-31 03:53:16 +00:00
|
|
|
|
/// Send prepared block of data (serialized and, if need, compressed), that will be read from 'input'.
|
|
|
|
|
/// You could pass size of serialized/compressed block.
|
|
|
|
|
void sendPreparedData(ReadBuffer & input, size_t size, const String & name = "");
|
2014-07-11 00:29:59 +00:00
|
|
|
|
|
2016-07-31 03:53:16 +00:00
|
|
|
|
/// Check, if has data to read.
|
2012-05-16 18:03:00 +00:00
|
|
|
|
bool poll(size_t timeout_microseconds = 0);
|
|
|
|
|
|
2016-07-31 03:53:16 +00:00
|
|
|
|
/// Check, if has data in read buffer.
|
2015-01-30 14:06:51 +00:00
|
|
|
|
bool hasReadBufferPendingData() const;
|
2015-01-29 12:13:21 +00:00
|
|
|
|
|
2016-07-31 03:53:16 +00:00
|
|
|
|
/// Receive packet from server.
|
2012-05-16 18:03:00 +00:00
|
|
|
|
Packet receivePacket();
|
2012-10-12 18:19:44 +00:00
|
|
|
|
|
2016-07-31 03:53:16 +00:00
|
|
|
|
/// If not connected yet, or if connection is broken - then connect. If cannot connect - throw an exception.
|
2012-11-02 20:13:41 +00:00
|
|
|
|
void forceConnected();
|
|
|
|
|
|
2016-07-31 03:53:16 +00:00
|
|
|
|
/** Disconnect.
|
|
|
|
|
* This may be used, if connection is left in unsynchronised state
|
|
|
|
|
* (when someone continues to wait for something) after an exception.
|
2013-01-28 20:32:21 +00:00
|
|
|
|
*/
|
|
|
|
|
void disconnect();
|
|
|
|
|
|
2015-10-12 14:53:16 +00:00
|
|
|
|
/** Заполнить информацию, которая необходима при получении блока для некоторых задач
|
|
|
|
|
* (пока только для запроса DESCRIBE TABLE с Distributed-таблицами).
|
|
|
|
|
*/
|
|
|
|
|
void fillBlockExtraInfo(BlockExtraInfo & info) const;
|
|
|
|
|
|
2016-05-28 14:14:18 +00:00
|
|
|
|
size_t outBytesCount() const { return out ? out->count() : 0; }
|
|
|
|
|
size_t inBytesCount() const { return in ? in->count() : 0; }
|
2014-11-27 19:35:31 +00:00
|
|
|
|
|
2012-05-16 18:03:00 +00:00
|
|
|
|
private:
|
|
|
|
|
String host;
|
|
|
|
|
UInt16 port;
|
2012-05-30 06:46:57 +00:00
|
|
|
|
String default_database;
|
2013-08-10 09:04:45 +00:00
|
|
|
|
String user;
|
|
|
|
|
String password;
|
2012-05-16 18:20:45 +00:00
|
|
|
|
|
2016-07-31 03:53:16 +00:00
|
|
|
|
/** Address could be resolved beforehand and passed to constructor. Then 'host' and 'port' fields are used just for logging.
|
|
|
|
|
* Otherwise address is resolved in constructor. Thus, DNS based load balancing is not supported.
|
2015-05-29 00:33:56 +00:00
|
|
|
|
*/
|
2015-05-28 21:41:28 +00:00
|
|
|
|
Poco::Net::SocketAddress resolved_address;
|
|
|
|
|
|
2015-05-29 00:33:56 +00:00
|
|
|
|
/// Для сообщений в логе и в эксепшенах.
|
|
|
|
|
String description;
|
|
|
|
|
void setDescription();
|
|
|
|
|
|
2012-05-23 19:51:30 +00:00
|
|
|
|
String client_name;
|
|
|
|
|
|
2015-02-01 07:21:19 +00:00
|
|
|
|
bool connected = false;
|
2012-05-21 20:38:34 +00:00
|
|
|
|
|
2012-05-16 18:20:45 +00:00
|
|
|
|
String server_name;
|
2015-02-01 07:21:19 +00:00
|
|
|
|
UInt64 server_version_major = 0;
|
|
|
|
|
UInt64 server_version_minor = 0;
|
|
|
|
|
UInt64 server_revision = 0;
|
2016-11-13 19:34:31 +00:00
|
|
|
|
String server_timezone;
|
2014-07-11 00:29:59 +00:00
|
|
|
|
|
2012-05-16 18:03:00 +00:00
|
|
|
|
Poco::Net::StreamSocket socket;
|
2016-05-28 14:14:18 +00:00
|
|
|
|
std::shared_ptr<ReadBuffer> in;
|
|
|
|
|
std::shared_ptr<WriteBuffer> out;
|
2012-05-16 18:03:00 +00:00
|
|
|
|
|
2014-02-12 17:31:02 +00:00
|
|
|
|
String query_id;
|
2016-07-31 03:53:16 +00:00
|
|
|
|
UInt64 compression; /// Enable data compression for communication.
|
|
|
|
|
/// What compression algorithm to use while sending data for INSERT queries and external tables.
|
2015-05-22 09:48:49 +00:00
|
|
|
|
CompressionMethod network_compression_method = CompressionMethod::LZ4;
|
2012-05-16 18:03:00 +00:00
|
|
|
|
|
2016-07-31 03:53:16 +00:00
|
|
|
|
/** If not nullptr, used to limit network traffic.
|
|
|
|
|
* Only traffic for transferring blocks is accounted. Other packets don't.
|
2015-02-10 20:48:17 +00:00
|
|
|
|
*/
|
|
|
|
|
ThrottlerPtr throttler;
|
|
|
|
|
|
2012-07-26 19:42:20 +00:00
|
|
|
|
Poco::Timespan connect_timeout;
|
|
|
|
|
Poco::Timespan receive_timeout;
|
|
|
|
|
Poco::Timespan send_timeout;
|
2015-03-06 09:37:16 +00:00
|
|
|
|
Poco::Timespan ping_timeout;
|
2012-07-26 19:42:20 +00:00
|
|
|
|
|
2016-07-31 03:53:16 +00:00
|
|
|
|
/// From where to read query execution result.
|
2016-05-28 14:14:18 +00:00
|
|
|
|
std::shared_ptr<ReadBuffer> maybe_compressed_in;
|
2012-05-16 18:03:00 +00:00
|
|
|
|
BlockInputStreamPtr block_in;
|
|
|
|
|
|
2016-07-31 03:53:16 +00:00
|
|
|
|
/// Where to write data for INSERT.
|
2016-05-28 14:14:18 +00:00
|
|
|
|
std::shared_ptr<WriteBuffer> maybe_compressed_out;
|
2012-05-16 18:03:00 +00:00
|
|
|
|
BlockOutputStreamPtr block_out;
|
|
|
|
|
|
2016-07-31 03:53:16 +00:00
|
|
|
|
/// Logger is created lazily, for avoid to run DNS request in constructor.
|
2014-10-08 19:00:25 +00:00
|
|
|
|
class LoggerWrapper
|
|
|
|
|
{
|
|
|
|
|
public:
|
2015-05-28 21:41:28 +00:00
|
|
|
|
LoggerWrapper(Connection & parent_)
|
|
|
|
|
: log(nullptr), parent(parent_)
|
2014-10-08 19:00:25 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logger * get()
|
|
|
|
|
{
|
|
|
|
|
if (!log)
|
2015-05-29 00:33:56 +00:00
|
|
|
|
log = &Logger::get("Connection (" + parent.getDescription() + ")");
|
2014-10-08 19:00:25 +00:00
|
|
|
|
|
|
|
|
|
return log;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::atomic<Logger *> log;
|
2015-05-28 21:41:28 +00:00
|
|
|
|
Connection & parent;
|
2014-10-08 19:00:25 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
LoggerWrapper log_wrapper;
|
2012-10-16 19:20:58 +00:00
|
|
|
|
|
2012-05-16 18:03:00 +00:00
|
|
|
|
void connect();
|
|
|
|
|
void sendHello();
|
|
|
|
|
void receiveHello();
|
|
|
|
|
bool ping();
|
|
|
|
|
|
|
|
|
|
Block receiveData();
|
2016-05-28 14:14:18 +00:00
|
|
|
|
std::unique_ptr<Exception> receiveException();
|
2012-05-16 18:03:00 +00:00
|
|
|
|
Progress receiveProgress();
|
2013-05-22 14:57:43 +00:00
|
|
|
|
BlockStreamProfileInfo receiveProfileInfo();
|
2013-09-05 20:22:43 +00:00
|
|
|
|
|
|
|
|
|
void initBlockInput();
|
2012-05-16 18:03:00 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|