ClickHouse/dbms/src/Client/Connection.cpp

274 lines
5.7 KiB
C++
Raw Normal View History

2012-05-16 18:03:00 +00:00
#include <Poco/Net/NetException.h>
#include <Yandex/Revision.h>
#include <DB/Core/Defines.h>
#include <DB/IO/CompressedReadBuffer.h>
#include <DB/IO/CompressedWriteBuffer.h>
#include <DB/IO/ReadHelpers.h>
#include <DB/IO/WriteHelpers.h>
#include <DB/DataStreams/NativeBlockInputStream.h>
#include <DB/DataStreams/NativeBlockOutputStream.h>
#include <DB/Client/Connection.h>
namespace DB
{
void Connection::connect()
{
2012-10-15 19:38:33 +00:00
try
{
socket.connect(Poco::Net::SocketAddress(host, port), connect_timeout);
socket.setReceiveTimeout(receive_timeout);
socket.setSendTimeout(send_timeout);
2012-07-26 19:42:20 +00:00
2012-10-15 19:38:33 +00:00
connected = true;
2012-05-16 18:03:00 +00:00
2012-10-15 19:38:33 +00:00
sendHello();
receiveHello();
}
catch (Poco::Net::NetException & e)
{
/// Добавляем в сообщение адрес сервера. Жаль, что более точный тип исключения теряется.
throw Poco::Net::NetException(e.displayText(), "(" + getServerAddress() + ")", e.code());
}
2012-05-16 18:03:00 +00:00
}
void Connection::sendHello()
{
2012-05-21 06:49:05 +00:00
writeVarUInt(Protocol::Client::Hello, *out);
2012-05-23 19:51:30 +00:00
writeStringBinary((DBMS_NAME " ") + client_name, *out);
2012-05-21 06:49:05 +00:00
writeVarUInt(DBMS_VERSION_MAJOR, *out);
writeVarUInt(DBMS_VERSION_MINOR, *out);
writeVarUInt(Revision::get(), *out);
2012-05-30 06:46:57 +00:00
writeStringBinary(default_database, *out);
2012-05-16 18:03:00 +00:00
2012-05-21 06:49:05 +00:00
out->next();
2012-05-16 18:03:00 +00:00
}
void Connection::receiveHello()
{
/// Получить hello пакет.
UInt64 packet_type = 0;
2012-05-21 06:49:05 +00:00
readVarUInt(packet_type, *in);
2012-05-30 06:46:57 +00:00
if (packet_type == Protocol::Server::Hello)
{
readStringBinary(server_name, *in);
readVarUInt(server_version_major, *in);
readVarUInt(server_version_minor, *in);
readVarUInt(server_revision, *in);
}
else if (packet_type == Protocol::Server::Exception)
receiveException()->rethrow();
else
{
/// Закроем соединение, чтобы не было рассинхронизации.
socket.close();
throw Exception("Unexpected packet from server " + getServerAddress() + " (expected Hello or Exception, got "
2012-06-19 22:46:02 +00:00
+ String(Protocol::Server::toString(Protocol::Server::Enum(packet_type))) + ")", ErrorCodes::UNEXPECTED_PACKET_FROM_SERVER);
}
2012-05-16 18:03:00 +00:00
}
2012-05-16 18:20:45 +00:00
void Connection::getServerVersion(String & name, UInt64 & version_major, UInt64 & version_minor, UInt64 & revision)
{
2012-05-21 20:38:34 +00:00
if (!connected)
connect();
2012-05-16 18:20:45 +00:00
name = server_name;
version_major = server_version_major;
version_minor = server_version_minor;
revision = server_revision;
}
2012-05-16 18:03:00 +00:00
void Connection::forceConnected()
{
2012-05-21 20:38:34 +00:00
if (!connected)
connect();
else
2012-05-16 18:03:00 +00:00
{
2012-05-21 20:38:34 +00:00
try
{
if (!ping())
{
socket.close();
connect();
}
}
catch (const Poco::Net::NetException & e)
2012-05-16 18:03:00 +00:00
{
connect();
}
}
}
bool Connection::ping()
{
UInt64 pong = 0;
2012-05-21 06:49:05 +00:00
writeVarUInt(Protocol::Client::Ping, *out);
out->next();
2012-05-16 18:03:00 +00:00
2012-05-21 06:49:05 +00:00
if (in->eof())
2012-05-16 18:03:00 +00:00
return false;
2012-05-21 06:49:05 +00:00
readVarUInt(pong, *in);
2012-05-16 18:03:00 +00:00
2012-06-24 23:17:06 +00:00
/// Можем получить запоздалые пакеты прогресса. TODO: может быть, это можно исправить.
2012-06-01 10:45:29 +00:00
while (pong == Protocol::Server::Progress)
{
receiveProgress();
2012-06-24 23:17:06 +00:00
if (in->eof())
return false;
2012-06-01 10:45:29 +00:00
readVarUInt(pong, *in);
}
2012-05-16 18:03:00 +00:00
if (pong != Protocol::Server::Pong)
{
/// Закроем соединение, чтобы не было рассинхронизации.
socket.close();
throw Exception("Unexpected packet from server " + getServerAddress() + " (expected Pong, got "
2012-05-23 19:51:30 +00:00
+ String(Protocol::Server::toString(Protocol::Server::Enum(pong))) + ")",
ErrorCodes::UNEXPECTED_PACKET_FROM_SERVER);
}
2012-05-16 18:03:00 +00:00
return true;
}
void Connection::sendQuery(const String & query, UInt64 query_id_, UInt64 stage)
{
forceConnected();
query_id = query_id_;
2012-05-23 19:51:30 +00:00
2012-05-21 06:49:05 +00:00
writeVarUInt(Protocol::Client::Query, *out);
writeIntBinary(query_id, *out);
writeVarUInt(stage, *out);
writeVarUInt(compression, *out);
2012-05-16 18:03:00 +00:00
2012-05-21 06:49:05 +00:00
writeStringBinary(query, *out);
2012-05-16 18:03:00 +00:00
2012-05-21 06:49:05 +00:00
out->next();
2012-05-16 18:03:00 +00:00
maybe_compressed_in = NULL;
maybe_compressed_out = NULL;
block_in = NULL;
block_out = NULL;
}
void Connection::sendCancel()
{
2012-05-21 06:49:05 +00:00
writeVarUInt(Protocol::Client::Cancel, *out);
out->next();
2012-05-16 18:03:00 +00:00
}
2012-05-21 20:38:34 +00:00
void Connection::sendData(const Block & block)
2012-05-16 18:03:00 +00:00
{
if (!block_out)
{
2012-05-21 06:49:05 +00:00
if (compression == Protocol::Compression::Enable)
maybe_compressed_out = new CompressedWriteBuffer(*out);
else
maybe_compressed_out = out;
2012-05-16 18:03:00 +00:00
block_out = new NativeBlockOutputStream(*maybe_compressed_out);
}
2012-05-21 06:49:05 +00:00
writeVarUInt(Protocol::Client::Data, *out);
2012-05-16 18:03:00 +00:00
block_out->write(block);
2012-05-28 19:57:44 +00:00
maybe_compressed_out->next();
2012-05-21 06:49:05 +00:00
out->next();
2012-05-16 18:03:00 +00:00
}
bool Connection::poll(size_t timeout_microseconds)
{
2012-05-21 06:49:05 +00:00
return in->poll(timeout_microseconds);
2012-05-16 18:03:00 +00:00
}
Connection::Packet Connection::receivePacket()
{
Packet res;
2012-05-21 06:49:05 +00:00
readVarUInt(res.type, *in);
2012-05-16 18:03:00 +00:00
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::EndOfStream:
return res;
default:
/// Закроем соединение, чтобы не было рассинхронизации.
socket.close();
throw Exception("Unknown packet from server" + getServerAddress(), ErrorCodes::UNKNOWN_PACKET_FROM_SERVER);
2012-05-16 18:03:00 +00:00
}
}
Block Connection::receiveData()
{
if (!block_in)
{
2012-05-21 06:49:05 +00:00
if (compression == Protocol::Compression::Enable)
maybe_compressed_in = new CompressedReadBuffer(*in);
else
maybe_compressed_in = in;
2012-05-16 18:03:00 +00:00
block_in = new NativeBlockInputStream(*maybe_compressed_in, data_type_factory);
}
/// Прочитать из сети один блок
return block_in->read();
}
String Connection::getServerAddress() const
{
return Poco::Net::SocketAddress(host, port).toString();
}
2012-05-16 18:03:00 +00:00
SharedPtr<Exception> Connection::receiveException()
{
Exception e;
readException(e, *in, "Received from " + getServerAddress());
2012-05-16 18:03:00 +00:00
return e.clone();
}
Progress Connection::receiveProgress()
{
Progress progress;
2012-05-21 06:49:05 +00:00
progress.read(*in);
2012-05-16 18:03:00 +00:00
return progress;
}
}