2017-04-01 09:19:00 +00:00
|
|
|
#include <DataStreams/RemoteBlockOutputStream.h>
|
2017-01-31 18:39:01 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Client/Connection.h>
|
2017-01-31 18:39:01 +00:00
|
|
|
#include <common/logger_useful.h>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/NetException.h>
|
2018-06-18 16:30:26 +00:00
|
|
|
#include <Common/CurrentThread.h>
|
|
|
|
#include <Interpreters/InternalTextLogsQueue.h>
|
2019-03-07 23:11:41 +00:00
|
|
|
#include <IO/ConnectionTimeouts.h>
|
2017-01-31 18:39:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int UNEXPECTED_PACKET_FROM_SERVER;
|
2017-01-31 18:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-07 23:11:41 +00:00
|
|
|
RemoteBlockOutputStream::RemoteBlockOutputStream(Connection & connection_,
|
|
|
|
const ConnectionTimeouts & timeouts,
|
|
|
|
const String & query_,
|
2020-03-24 22:26:24 +00:00
|
|
|
const Settings * settings_,
|
|
|
|
const ClientInfo * client_info_)
|
|
|
|
: connection(connection_), query(query_), settings(settings_), client_info(client_info_)
|
2017-01-31 18:39:01 +00:00
|
|
|
{
|
2018-02-19 00:45:32 +00:00
|
|
|
/** Send query and receive "header", that describe table structure.
|
|
|
|
* Header is needed to know, what structure is required for blocks to be passed to 'write' method.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
2020-03-24 22:26:24 +00:00
|
|
|
connection.sendQuery(timeouts, query, "", QueryProcessingStage::Complete, settings, client_info);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-06-08 19:50:15 +00:00
|
|
|
while (true)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-11-15 16:34:43 +00:00
|
|
|
Packet packet = connection.receivePacket();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-06-08 19:50:15 +00:00
|
|
|
if (Protocol::Server::Data == packet.type)
|
|
|
|
{
|
|
|
|
header = packet.block;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (Protocol::Server::Exception == packet.type)
|
|
|
|
{
|
|
|
|
packet.exception->rethrow();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (Protocol::Server::Log == packet.type)
|
|
|
|
{
|
2018-06-18 16:30:26 +00:00
|
|
|
/// Pass logs from remote server to client
|
|
|
|
if (auto log_queue = CurrentThread::getInternalTextLogsQueue())
|
|
|
|
log_queue->pushBlock(std::move(packet.block));
|
2018-06-08 19:50:15 +00:00
|
|
|
}
|
2019-07-17 18:30:17 +00:00
|
|
|
else if (Protocol::Server::TableColumns == packet.type)
|
|
|
|
{
|
|
|
|
/// Server could attach ColumnsDescription in front of stream for column defaults. There's no need to pass it through cause
|
|
|
|
/// client's already got this information for remote table. Ignore.
|
|
|
|
}
|
2018-06-08 19:50:15 +00:00
|
|
|
else
|
|
|
|
throw NetException("Unexpected packet from server (expected Data or Exception, got "
|
|
|
|
+ String(Protocol::Server::toString(packet.type)) + ")", ErrorCodes::UNEXPECTED_PACKET_FROM_SERVER);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2017-01-31 18:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RemoteBlockOutputStream::write(const Block & block)
|
|
|
|
{
|
2018-10-26 15:26:07 +00:00
|
|
|
if (header)
|
|
|
|
assertBlocksHaveEqualStructure(block, header, "RemoteBlockOutputStream");
|
2018-04-04 12:11:38 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
connection.sendData(block);
|
|
|
|
}
|
2018-06-03 22:11:50 +00:00
|
|
|
catch (const NetException &)
|
2018-04-04 12:11:38 +00:00
|
|
|
{
|
|
|
|
/// Try to get more detailed exception from server
|
2018-06-13 19:01:07 +00:00
|
|
|
auto packet_type = connection.checkPacket();
|
|
|
|
if (packet_type && *packet_type == Protocol::Server::Exception)
|
2018-04-04 12:11:38 +00:00
|
|
|
{
|
2019-11-15 16:34:43 +00:00
|
|
|
Packet packet = connection.receivePacket();
|
2018-06-13 19:01:07 +00:00
|
|
|
packet.exception->rethrow();
|
2018-04-04 12:11:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
2017-01-31 18:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RemoteBlockOutputStream::writePrepared(ReadBuffer & input, size_t size)
|
|
|
|
{
|
2018-02-19 00:45:32 +00:00
|
|
|
/// We cannot use 'header'. Input must contain block with proper structure.
|
2017-04-01 07:20:54 +00:00
|
|
|
connection.sendPreparedData(input, size);
|
2017-01-31 18:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RemoteBlockOutputStream::writeSuffix()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Empty block means end of data.
|
|
|
|
connection.sendData(Block());
|
|
|
|
|
2018-06-08 19:50:15 +00:00
|
|
|
/// Wait for EndOfStream or Exception packet, skip Log packets.
|
|
|
|
while (true)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-11-15 16:34:43 +00:00
|
|
|
Packet packet = connection.receivePacket();
|
2018-06-08 19:50:15 +00:00
|
|
|
|
|
|
|
if (Protocol::Server::EndOfStream == packet.type)
|
|
|
|
break;
|
|
|
|
else if (Protocol::Server::Exception == packet.type)
|
|
|
|
packet.exception->rethrow();
|
|
|
|
else if (Protocol::Server::Log == packet.type)
|
|
|
|
{
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw NetException("Unexpected packet from server (expected EndOfStream or Exception, got "
|
|
|
|
+ String(Protocol::Server::toString(packet.type)) + ")", ErrorCodes::UNEXPECTED_PACKET_FROM_SERVER);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2018-02-14 15:11:39 +00:00
|
|
|
|
|
|
|
finished = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
RemoteBlockOutputStream::~RemoteBlockOutputStream()
|
|
|
|
{
|
2018-02-21 23:13:38 +00:00
|
|
|
/// If interrupted in the middle of the loop of communication with the server, then interrupt the connection,
|
|
|
|
/// to not leave the connection in unsynchronized state.
|
|
|
|
if (!finished)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
connection.disconnect();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
|
|
|
}
|
|
|
|
}
|
2017-01-31 18:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|