2014-07-06 21:59:20 +00:00
|
|
|
#include <iomanip>
|
|
|
|
|
2012-05-16 18:03:00 +00:00
|
|
|
#include <Poco/Net/NetException.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Core/Defines.h>
|
|
|
|
#include <IO/CompressedReadBuffer.h>
|
|
|
|
#include <IO/CompressedWriteBuffer.h>
|
|
|
|
#include <IO/ReadBufferFromPocoSocket.h>
|
|
|
|
#include <IO/WriteBufferFromPocoSocket.h>
|
|
|
|
#include <IO/ReadHelpers.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <IO/copyData.h>
|
|
|
|
#include <DataStreams/NativeBlockInputStream.h>
|
|
|
|
#include <DataStreams/NativeBlockOutputStream.h>
|
|
|
|
#include <Client/Connection.h>
|
2018-03-28 14:07:28 +00:00
|
|
|
#include <Client/TimeoutSetter.h>
|
2017-10-30 18:10:50 +00:00
|
|
|
#include <Common/ClickHouseRevision.h>
|
|
|
|
#include <Common/Exception.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/NetException.h>
|
|
|
|
#include <Common/CurrentMetrics.h>
|
2018-04-19 13:56:14 +00:00
|
|
|
#include <Common/DNSResolver.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/ClientInfo.h>
|
2016-10-24 21:40:39 +00:00
|
|
|
|
2017-10-30 18:10:50 +00:00
|
|
|
#include <Common/config.h>
|
|
|
|
#if Poco_NetSSL_FOUND
|
|
|
|
#include <Poco/Net/SecureStreamSocket.h>
|
|
|
|
#endif
|
|
|
|
|
2012-05-16 18:03:00 +00:00
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
namespace CurrentMetrics
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const Metric SendExternalTables;
|
2016-10-24 04:06:27 +00:00
|
|
|
}
|
|
|
|
|
2012-05-16 18:03:00 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int NETWORK_ERROR;
|
|
|
|
extern const int SOCKET_TIMEOUT;
|
|
|
|
extern const int SERVER_REVISION_IS_TOO_OLD;
|
|
|
|
extern const int UNEXPECTED_PACKET_FROM_SERVER;
|
|
|
|
extern const int UNKNOWN_PACKET_FROM_SERVER;
|
2017-10-30 18:10:50 +00:00
|
|
|
extern const int SUPPORT_IS_DISABLED;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-16 18:03:00 +00:00
|
|
|
void Connection::connect()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if (connected)
|
|
|
|
disconnect();
|
|
|
|
|
2018-03-29 01:41:06 +00:00
|
|
|
LOG_TRACE(log_wrapper.get(), "Connecting. Database: " << (default_database.empty() ? "(not specified)" : default_database) << ". User: " << user
|
|
|
|
<< (static_cast<bool>(secure) ? ". Secure" : "") << (static_cast<bool>(compression) ? "" : ". Uncompressed") );
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-03-29 01:41:06 +00:00
|
|
|
if (static_cast<bool>(secure))
|
2017-10-30 18:10:50 +00:00
|
|
|
{
|
|
|
|
#if Poco_NetSSL_FOUND
|
|
|
|
socket = std::make_unique<Poco::Net::SecureStreamSocket>();
|
|
|
|
#else
|
2018-03-29 01:41:06 +00:00
|
|
|
throw Exception{"tcp_secure protocol is disabled because poco library was built without NetSSL support.", ErrorCodes::SUPPORT_IS_DISABLED};
|
2017-10-30 18:10:50 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
socket = std::make_unique<Poco::Net::StreamSocket>();
|
|
|
|
}
|
2018-03-29 20:21:01 +00:00
|
|
|
|
2018-04-19 13:56:14 +00:00
|
|
|
current_resolved_address = DNSResolver::instance().resolveAddress(host, port);
|
2018-03-29 20:21:01 +00:00
|
|
|
|
|
|
|
socket->connect(current_resolved_address, timeouts.connection_timeout);
|
2017-12-27 17:58:52 +00:00
|
|
|
socket->setReceiveTimeout(timeouts.receive_timeout);
|
|
|
|
socket->setSendTimeout(timeouts.send_timeout);
|
2017-09-28 19:43:31 +00:00
|
|
|
socket->setNoDelay(true);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-09-28 19:43:31 +00:00
|
|
|
in = std::make_shared<ReadBufferFromPocoSocket>(*socket);
|
|
|
|
out = std::make_shared<WriteBufferFromPocoSocket>(*socket);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
connected = true;
|
|
|
|
|
|
|
|
sendHello();
|
|
|
|
receiveHello();
|
|
|
|
|
|
|
|
LOG_TRACE(log_wrapper.get(), "Connected to " << server_name
|
|
|
|
<< " server version " << server_version_major
|
|
|
|
<< "." << server_version_minor
|
|
|
|
<< "." << server_revision
|
|
|
|
<< ".");
|
|
|
|
}
|
|
|
|
catch (Poco::Net::NetException & e)
|
|
|
|
{
|
|
|
|
disconnect();
|
|
|
|
|
|
|
|
/// Add server address to exception. Also Exception will remember stack trace. It's a pity that more precise exception type is lost.
|
|
|
|
throw NetException(e.displayText(), "(" + getDescription() + ")", ErrorCodes::NETWORK_ERROR);
|
|
|
|
}
|
|
|
|
catch (Poco::TimeoutException & e)
|
|
|
|
{
|
|
|
|
disconnect();
|
|
|
|
|
|
|
|
/// Add server address to exception. Also Exception will remember stack trace. It's a pity that more precise exception type is lost.
|
|
|
|
throw NetException(e.displayText(), "(" + getDescription() + ")", ErrorCodes::SOCKET_TIMEOUT);
|
|
|
|
}
|
2012-05-16 18:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-18 23:38:16 +00:00
|
|
|
void Connection::disconnect()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
//LOG_TRACE(log_wrapper.get(), "Disconnecting");
|
2014-07-06 21:59:20 +00:00
|
|
|
|
2017-10-04 15:18:05 +00:00
|
|
|
in = nullptr;
|
|
|
|
out = nullptr; // can write to socket
|
2017-09-29 15:01:06 +00:00
|
|
|
if (socket)
|
|
|
|
socket->close();
|
2017-09-28 19:43:31 +00:00
|
|
|
socket = nullptr;
|
2017-04-01 07:20:54 +00:00
|
|
|
connected = false;
|
2012-10-18 23:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-16 18:03:00 +00:00
|
|
|
void Connection::sendHello()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
//LOG_TRACE(log_wrapper.get(), "Sending hello");
|
2013-08-10 09:04:45 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
writeVarUInt(Protocol::Client::Hello, *out);
|
|
|
|
writeStringBinary((DBMS_NAME " ") + client_name, *out);
|
|
|
|
writeVarUInt(DBMS_VERSION_MAJOR, *out);
|
|
|
|
writeVarUInt(DBMS_VERSION_MINOR, *out);
|
|
|
|
writeVarUInt(ClickHouseRevision::get(), *out);
|
|
|
|
writeStringBinary(default_database, *out);
|
|
|
|
writeStringBinary(user, *out);
|
|
|
|
writeStringBinary(password, *out);
|
2014-07-06 21:59:20 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
out->next();
|
2012-05-16 18:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Connection::receiveHello()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
//LOG_TRACE(log_wrapper.get(), "Receiving hello");
|
|
|
|
|
|
|
|
/// Receive hello packet.
|
|
|
|
UInt64 packet_type = 0;
|
|
|
|
|
|
|
|
readVarUInt(packet_type, *in);
|
|
|
|
if (packet_type == Protocol::Server::Hello)
|
|
|
|
{
|
|
|
|
readStringBinary(server_name, *in);
|
|
|
|
readVarUInt(server_version_major, *in);
|
|
|
|
readVarUInt(server_version_minor, *in);
|
|
|
|
readVarUInt(server_revision, *in);
|
|
|
|
if (server_revision >= DBMS_MIN_REVISION_WITH_SERVER_TIMEZONE)
|
|
|
|
{
|
|
|
|
readStringBinary(server_timezone, *in);
|
|
|
|
}
|
2018-02-26 06:49:17 +00:00
|
|
|
if (server_revision >= DBMS_MIN_REVISION_WITH_SERVER_DISPLAY_NAME)
|
2018-02-24 12:06:53 +00:00
|
|
|
{
|
2018-02-26 06:49:17 +00:00
|
|
|
readStringBinary(server_display_name, *in);
|
2018-02-24 12:06:53 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else if (packet_type == Protocol::Server::Exception)
|
|
|
|
receiveException()->rethrow();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// Close connection, to not stay in unsynchronised state.
|
|
|
|
disconnect();
|
2017-04-17 16:02:48 +00:00
|
|
|
throwUnexpectedPacket(packet_type, "Hello or Exception");
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2012-05-16 18:03:00 +00:00
|
|
|
}
|
|
|
|
|
2014-04-18 13:59:39 +00:00
|
|
|
void Connection::setDefaultDatabase(const String & database)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
default_database = database;
|
2014-04-18 13:59:39 +00:00
|
|
|
}
|
|
|
|
|
2016-06-08 14:39:49 +00:00
|
|
|
const String & Connection::getDefaultDatabase() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return default_database;
|
2016-06-08 14:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const String & Connection::getDescription() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return description;
|
2016-06-08 14:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const String & Connection::getHost() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return host;
|
2016-06-08 14:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UInt16 Connection::getPort() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return port;
|
2016-06-08 14:39:49 +00:00
|
|
|
}
|
2014-04-18 13:59:39 +00:00
|
|
|
|
2012-05-16 18:20:45 +00:00
|
|
|
void Connection::getServerVersion(String & name, UInt64 & version_major, UInt64 & version_minor, UInt64 & revision)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!connected)
|
|
|
|
connect();
|
2014-07-06 21:59:20 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
name = server_name;
|
|
|
|
version_major = server_version_major;
|
|
|
|
version_minor = server_version_minor;
|
|
|
|
revision = server_revision;
|
2012-05-16 18:20:45 +00:00
|
|
|
}
|
|
|
|
|
2016-11-17 00:12:08 +00:00
|
|
|
const String & Connection::getServerTimezone()
|
2016-11-13 19:34:31 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!connected)
|
|
|
|
connect();
|
2016-11-17 00:12:08 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return server_timezone;
|
2016-11-13 19:34:31 +00:00
|
|
|
}
|
2012-05-16 18:20:45 +00:00
|
|
|
|
2018-02-26 06:49:17 +00:00
|
|
|
const String & Connection::getServerDisplayName()
|
2018-02-24 12:06:53 +00:00
|
|
|
{
|
|
|
|
if (!connected)
|
|
|
|
connect();
|
|
|
|
|
2018-02-26 06:49:17 +00:00
|
|
|
return server_display_name;
|
2018-02-24 12:06:53 +00:00
|
|
|
}
|
|
|
|
|
2012-05-16 18:03:00 +00:00
|
|
|
void Connection::forceConnected()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!connected)
|
|
|
|
{
|
|
|
|
connect();
|
|
|
|
}
|
|
|
|
else if (!ping())
|
|
|
|
{
|
|
|
|
LOG_TRACE(log_wrapper.get(), "Connection was closed, will reconnect.");
|
|
|
|
connect();
|
|
|
|
}
|
2012-05-16 18:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Connection::ping()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
// LOG_TRACE(log_wrapper.get(), "Ping");
|
|
|
|
|
2018-04-04 12:11:38 +00:00
|
|
|
TimeoutSetter timeout_setter(*socket, sync_request_timeout, true);
|
2017-04-01 07:20:54 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
UInt64 pong = 0;
|
|
|
|
writeVarUInt(Protocol::Client::Ping, *out);
|
|
|
|
out->next();
|
|
|
|
|
|
|
|
if (in->eof())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
readVarUInt(pong, *in);
|
|
|
|
|
|
|
|
/// Could receive late packets with progress. TODO: Maybe possible to fix.
|
|
|
|
while (pong == Protocol::Server::Progress)
|
|
|
|
{
|
|
|
|
receiveProgress();
|
|
|
|
|
|
|
|
if (in->eof())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
readVarUInt(pong, *in);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pong != Protocol::Server::Pong)
|
2017-04-17 16:02:48 +00:00
|
|
|
throwUnexpectedPacket(pong, "Pong");
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
catch (const Poco::Exception & e)
|
|
|
|
{
|
|
|
|
LOG_TRACE(log_wrapper.get(), e.displayText());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2012-05-16 18:03:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 16:02:48 +00:00
|
|
|
TablesStatusResponse Connection::getTablesStatus(const TablesStatusRequest & request)
|
|
|
|
{
|
|
|
|
if (!connected)
|
|
|
|
connect();
|
|
|
|
|
2018-04-04 12:11:38 +00:00
|
|
|
TimeoutSetter timeout_setter(*socket, sync_request_timeout, true);
|
2017-04-17 16:02:48 +00:00
|
|
|
|
|
|
|
writeVarUInt(Protocol::Client::TablesStatusRequest, *out);
|
|
|
|
request.write(*out, server_revision);
|
|
|
|
out->next();
|
|
|
|
|
|
|
|
UInt64 response_type = 0;
|
|
|
|
readVarUInt(response_type, *in);
|
|
|
|
|
|
|
|
if (response_type == Protocol::Server::Exception)
|
|
|
|
receiveException()->rethrow();
|
|
|
|
else if (response_type != Protocol::Server::TablesStatusResponse)
|
|
|
|
throwUnexpectedPacket(response_type, "TablesStatusResponse");
|
|
|
|
|
|
|
|
TablesStatusResponse response;
|
|
|
|
response.read(*in, server_revision);
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2012-05-16 18:03:00 +00:00
|
|
|
|
2016-10-24 21:40:39 +00:00
|
|
|
void Connection::sendQuery(
|
2017-04-01 07:20:54 +00:00
|
|
|
const String & query,
|
|
|
|
const String & query_id_,
|
|
|
|
UInt64 stage,
|
|
|
|
const Settings * settings,
|
|
|
|
const ClientInfo * client_info,
|
|
|
|
bool with_pending_data)
|
|
|
|
{
|
2017-04-17 16:02:48 +00:00
|
|
|
if (!connected)
|
|
|
|
connect();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-10-12 23:56:28 +00:00
|
|
|
compression_settings = settings ? CompressionSettings(*settings) : CompressionSettings(CompressionMethod::LZ4);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
query_id = query_id_;
|
|
|
|
|
|
|
|
//LOG_TRACE(log_wrapper.get(), "Sending query");
|
|
|
|
|
|
|
|
writeVarUInt(Protocol::Client::Query, *out);
|
|
|
|
writeStringBinary(query_id, *out);
|
|
|
|
|
|
|
|
/// Client info.
|
|
|
|
if (server_revision >= DBMS_MIN_REVISION_WITH_CLIENT_INFO)
|
|
|
|
{
|
|
|
|
ClientInfo client_info_to_send;
|
|
|
|
|
|
|
|
if (!client_info)
|
|
|
|
{
|
|
|
|
/// No client info passed - means this query initiated by me.
|
|
|
|
client_info_to_send.query_kind = ClientInfo::QueryKind::INITIAL_QUERY;
|
|
|
|
client_info_to_send.fillOSUserHostNameAndVersionInfo();
|
|
|
|
client_info_to_send.client_name = (DBMS_NAME " ") + client_name;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// This query is initiated by another query.
|
|
|
|
client_info_to_send = *client_info;
|
|
|
|
client_info_to_send.query_kind = ClientInfo::QueryKind::SECONDARY_QUERY;
|
|
|
|
}
|
|
|
|
|
|
|
|
client_info_to_send.write(*out, server_revision);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Per query settings.
|
|
|
|
if (settings)
|
|
|
|
settings->serialize(*out);
|
|
|
|
else
|
|
|
|
writeStringBinary("", *out);
|
|
|
|
|
|
|
|
writeVarUInt(stage, *out);
|
2017-10-03 14:52:08 +00:00
|
|
|
writeVarUInt(static_cast<bool>(compression), *out);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
writeStringBinary(query, *out);
|
|
|
|
|
|
|
|
maybe_compressed_in.reset();
|
|
|
|
maybe_compressed_out.reset();
|
|
|
|
block_in.reset();
|
|
|
|
block_out.reset();
|
|
|
|
|
2017-08-16 20:27:35 +00:00
|
|
|
/// Send empty block which means end of data.
|
|
|
|
if (!with_pending_data)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
sendData(Block());
|
|
|
|
out->next();
|
|
|
|
}
|
2012-05-16 18:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Connection::sendCancel()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
//LOG_TRACE(log_wrapper.get(), "Sending cancel");
|
2014-04-06 06:43:16 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
writeVarUInt(Protocol::Client::Cancel, *out);
|
|
|
|
out->next();
|
2012-05-16 18:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-06 14:02:20 +00:00
|
|
|
void Connection::sendData(const Block & block, const String & name)
|
2012-05-16 18:03:00 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
//LOG_TRACE(log_wrapper.get(), "Sending data");
|
2014-07-06 21:59:20 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!block_out)
|
|
|
|
{
|
|
|
|
if (compression == Protocol::Compression::Enable)
|
2017-10-12 23:56:28 +00:00
|
|
|
maybe_compressed_out = std::make_shared<CompressedWriteBuffer>(*out, compression_settings);
|
2017-04-01 07:20:54 +00:00
|
|
|
else
|
|
|
|
maybe_compressed_out = out;
|
2012-05-16 18:03:00 +00:00
|
|
|
|
2018-02-19 00:45:32 +00:00
|
|
|
block_out = std::make_shared<NativeBlockOutputStream>(*maybe_compressed_out, server_revision, block.cloneEmpty());
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2012-05-16 18:03:00 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
writeVarUInt(Protocol::Client::Data, *out);
|
2017-08-16 20:27:35 +00:00
|
|
|
writeStringBinary(name, *out);
|
2014-03-04 15:31:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t prev_bytes = out->count();
|
2015-02-10 20:48:17 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
block_out->write(block);
|
|
|
|
maybe_compressed_out->next();
|
|
|
|
out->next();
|
2015-02-10 20:48:17 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (throttler)
|
|
|
|
throttler->add(out->count() - prev_bytes);
|
2012-05-16 18:03:00 +00:00
|
|
|
}
|
|
|
|
|
2014-04-06 06:43:16 +00:00
|
|
|
|
2014-08-21 12:58:07 +00:00
|
|
|
void Connection::sendPreparedData(ReadBuffer & input, size_t size, const String & name)
|
2014-07-11 00:29:59 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/// NOTE 'Throttler' is not used in this method (could use, but it's not important right now).
|
2015-02-10 20:48:17 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
writeVarUInt(Protocol::Client::Data, *out);
|
2017-08-16 20:27:35 +00:00
|
|
|
writeStringBinary(name, *out);
|
2014-07-11 00:29:59 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (0 == size)
|
|
|
|
copyData(input, *out);
|
|
|
|
else
|
|
|
|
copyData(input, *out, size);
|
|
|
|
out->next();
|
2014-07-11 00:29:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-14 15:42:30 +00:00
|
|
|
void Connection::sendExternalTablesData(ExternalTablesData & data)
|
2014-03-04 15:31:56 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (data.empty())
|
|
|
|
{
|
|
|
|
/// Send empty block, which means end of data transfer.
|
|
|
|
sendData(Block());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Stopwatch watch;
|
|
|
|
size_t out_bytes = out ? out->count() : 0;
|
|
|
|
size_t maybe_compressed_out_bytes = maybe_compressed_out ? maybe_compressed_out->count() : 0;
|
|
|
|
size_t rows = 0;
|
|
|
|
|
|
|
|
CurrentMetrics::Increment metric_increment{CurrentMetrics::SendExternalTables};
|
|
|
|
|
|
|
|
for (auto & elem : data)
|
|
|
|
{
|
|
|
|
elem.first->readPrefix();
|
|
|
|
while (Block block = elem.first->read())
|
|
|
|
{
|
|
|
|
rows += block.rows();
|
|
|
|
sendData(block, elem.second);
|
|
|
|
}
|
|
|
|
elem.first->readSuffix();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Send empty block, which means end of data transfer.
|
|
|
|
sendData(Block());
|
|
|
|
|
|
|
|
out_bytes = out->count() - out_bytes;
|
|
|
|
maybe_compressed_out_bytes = maybe_compressed_out->count() - maybe_compressed_out_bytes;
|
|
|
|
double elapsed = watch.elapsedSeconds();
|
|
|
|
|
|
|
|
std::stringstream msg;
|
|
|
|
msg << std::fixed << std::setprecision(3);
|
|
|
|
msg << "Sent data for " << data.size() << " external tables, total " << rows << " rows in " << elapsed << " sec., "
|
|
|
|
<< static_cast<size_t>(rows / watch.elapsedSeconds()) << " rows/sec., "
|
|
|
|
<< maybe_compressed_out_bytes / 1048576.0 << " MiB (" << maybe_compressed_out_bytes / 1048576.0 / watch.elapsedSeconds() << " MiB/sec.)";
|
|
|
|
|
|
|
|
if (compression == Protocol::Compression::Enable)
|
|
|
|
msg << ", compressed " << static_cast<double>(maybe_compressed_out_bytes) / out_bytes << " times to "
|
|
|
|
<< out_bytes / 1048576.0 << " MiB (" << out_bytes / 1048576.0 / watch.elapsedSeconds() << " MiB/sec.)";
|
|
|
|
else
|
|
|
|
msg << ", no compression.";
|
|
|
|
|
|
|
|
LOG_DEBUG(log_wrapper.get(), msg.rdbuf());
|
2014-03-04 15:31:56 +00:00
|
|
|
}
|
|
|
|
|
2018-03-29 20:21:01 +00:00
|
|
|
Poco::Net::SocketAddress Connection::getResolvedAddress() const
|
|
|
|
{
|
|
|
|
if (connected)
|
|
|
|
return current_resolved_address;
|
|
|
|
|
2018-04-19 13:56:14 +00:00
|
|
|
return DNSResolver::instance().resolveAddress(host, port);
|
2018-03-29 20:21:01 +00:00
|
|
|
}
|
|
|
|
|
2014-03-04 15:31:56 +00:00
|
|
|
|
2012-05-16 18:03:00 +00:00
|
|
|
bool Connection::poll(size_t timeout_microseconds)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return static_cast<ReadBufferFromPocoSocket &>(*in).poll(timeout_microseconds);
|
2012-05-16 18:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-30 14:06:51 +00:00
|
|
|
bool Connection::hasReadBufferPendingData() const
|
2015-01-29 12:13:21 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return static_cast<const ReadBufferFromPocoSocket &>(*in).hasPendingData();
|
2015-01-29 12:13:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-16 18:03:00 +00:00
|
|
|
Connection::Packet Connection::receivePacket()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
//LOG_TRACE(log_wrapper.get(), "Receiving packet");
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Packet res;
|
|
|
|
readVarUInt(res.type, *in);
|
|
|
|
|
|
|
|
switch (res.type)
|
|
|
|
{
|
|
|
|
case Protocol::Server::Data:
|
|
|
|
res.block = receiveData();
|
|
|
|
return res;
|
|
|
|
|
|
|
|
case Protocol::Server::Exception:
|
|
|
|
res.exception = receiveException();
|
|
|
|
return res;
|
|
|
|
|
|
|
|
case Protocol::Server::Progress:
|
|
|
|
res.progress = receiveProgress();
|
|
|
|
return res;
|
|
|
|
|
|
|
|
case Protocol::Server::ProfileInfo:
|
|
|
|
res.profile_info = receiveProfileInfo();
|
|
|
|
return res;
|
|
|
|
|
|
|
|
case Protocol::Server::Totals:
|
|
|
|
/// Block with total values is passed in same form as ordinary block. The only difference is packed id.
|
|
|
|
res.block = receiveData();
|
|
|
|
return res;
|
|
|
|
|
|
|
|
case Protocol::Server::Extremes:
|
|
|
|
/// Same as above.
|
|
|
|
res.block = receiveData();
|
|
|
|
return res;
|
|
|
|
|
|
|
|
case Protocol::Server::EndOfStream:
|
|
|
|
return res;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/// In unknown state, disconnect - to not leave unsynchronised connection.
|
|
|
|
disconnect();
|
|
|
|
throw Exception("Unknown packet "
|
|
|
|
+ toString(res.type)
|
|
|
|
+ " from server " + getDescription(), ErrorCodes::UNKNOWN_PACKET_FROM_SERVER);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception & e)
|
|
|
|
{
|
|
|
|
/// Add server address to exception message, if need.
|
|
|
|
if (e.code() != ErrorCodes::UNKNOWN_PACKET_FROM_SERVER)
|
|
|
|
e.addMessage("while receiving packet from " + getDescription());
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
2012-05-16 18:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Block Connection::receiveData()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
//LOG_TRACE(log_wrapper.get(), "Receiving data");
|
2014-07-06 21:59:20 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
initBlockInput();
|
2013-09-05 20:22:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
String external_table_name;
|
2017-08-16 20:27:35 +00:00
|
|
|
readStringBinary(external_table_name, *in);
|
2014-03-04 15:31:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t prev_bytes = in->count();
|
2015-02-10 20:48:17 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Read one block from network.
|
|
|
|
Block res = block_in->read();
|
2015-02-10 20:48:17 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (throttler)
|
|
|
|
throttler->add(in->count() - prev_bytes);
|
2015-02-10 20:48:17 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2013-09-05 20:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Connection::initBlockInput()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!block_in)
|
|
|
|
{
|
|
|
|
if (compression == Protocol::Compression::Enable)
|
|
|
|
maybe_compressed_in = std::make_shared<CompressedReadBuffer>(*in);
|
|
|
|
else
|
|
|
|
maybe_compressed_in = in;
|
2012-05-21 06:49:05 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
block_in = std::make_shared<NativeBlockInputStream>(*maybe_compressed_in, server_revision);
|
|
|
|
}
|
2012-05-16 18:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-29 00:33:56 +00:00
|
|
|
void Connection::setDescription()
|
2012-10-12 18:19:44 +00:00
|
|
|
{
|
2018-03-29 20:21:01 +00:00
|
|
|
auto resolved_address = getResolvedAddress();
|
2017-04-01 07:20:54 +00:00
|
|
|
description = host + ":" + toString(resolved_address.port());
|
|
|
|
auto ip_address = resolved_address.host().toString();
|
2015-05-29 00:33:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (host != ip_address)
|
|
|
|
description += ", " + ip_address;
|
2012-10-12 18:19:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-28 14:14:18 +00:00
|
|
|
std::unique_ptr<Exception> Connection::receiveException()
|
2012-05-16 18:03:00 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
//LOG_TRACE(log_wrapper.get(), "Receiving exception");
|
2014-07-06 21:59:20 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Exception e;
|
|
|
|
readException(e, *in, "Received from " + getDescription());
|
|
|
|
return std::unique_ptr<Exception>{ e.clone() };
|
2012-05-16 18:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Progress Connection::receiveProgress()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
//LOG_TRACE(log_wrapper.get(), "Receiving progress");
|
2014-07-06 21:59:20 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Progress progress;
|
|
|
|
progress.read(*in, server_revision);
|
|
|
|
return progress;
|
2012-05-16 18:03:00 +00:00
|
|
|
}
|
|
|
|
|
2013-05-22 14:57:43 +00:00
|
|
|
|
|
|
|
BlockStreamProfileInfo Connection::receiveProfileInfo()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
BlockStreamProfileInfo profile_info;
|
|
|
|
profile_info.read(*in);
|
|
|
|
return profile_info;
|
2013-05-22 14:57:43 +00:00
|
|
|
}
|
|
|
|
|
2015-10-12 14:53:16 +00:00
|
|
|
void Connection::fillBlockExtraInfo(BlockExtraInfo & info) const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
info.is_valid = true;
|
|
|
|
info.host = host;
|
2018-03-29 20:21:01 +00:00
|
|
|
info.resolved_address = getResolvedAddress().toString();
|
2017-04-01 07:20:54 +00:00
|
|
|
info.port = port;
|
|
|
|
info.user = user;
|
2015-10-12 14:53:16 +00:00
|
|
|
}
|
2013-09-05 20:22:43 +00:00
|
|
|
|
2017-04-17 16:02:48 +00:00
|
|
|
void Connection::throwUnexpectedPacket(UInt64 packet_type, const char * expected) const
|
|
|
|
{
|
|
|
|
throw NetException(
|
|
|
|
"Unexpected packet from server " + getDescription() + " (expected " + expected
|
|
|
|
+ ", got " + String(Protocol::Server::toString(packet_type)) + ")",
|
|
|
|
ErrorCodes::UNEXPECTED_PACKET_FROM_SERVER);
|
|
|
|
}
|
|
|
|
|
2012-05-16 18:03:00 +00:00
|
|
|
}
|