2012-03-19 12:57:56 +00:00
|
|
|
#include <iomanip>
|
|
|
|
|
2013-01-13 22:13:54 +00:00
|
|
|
#include <Poco/Net/NetException.h>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/ClickHouseRevision.h>
|
2012-03-09 15:46:52 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Stopwatch.h>
|
2012-03-09 15:46:52 +00:00
|
|
|
|
2017-11-24 13:55:31 +00:00
|
|
|
#include <IO/Progress.h>
|
2012-03-09 15:46:52 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/CompressedReadBuffer.h>
|
|
|
|
#include <IO/CompressedWriteBuffer.h>
|
|
|
|
#include <IO/ReadBufferFromPocoSocket.h>
|
|
|
|
#include <IO/WriteBufferFromPocoSocket.h>
|
2017-10-12 23:56:28 +00:00
|
|
|
#include <IO/CompressionSettings.h>
|
2014-02-27 22:35:49 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/copyData.h>
|
2012-03-09 15:46:52 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataStreams/AsynchronousBlockInputStream.h>
|
|
|
|
#include <DataStreams/NativeBlockInputStream.h>
|
|
|
|
#include <DataStreams/NativeBlockOutputStream.h>
|
|
|
|
#include <Interpreters/executeQuery.h>
|
|
|
|
#include <Interpreters/Quota.h>
|
2017-04-17 16:02:48 +00:00
|
|
|
#include <Interpreters/TablesStatus.h>
|
2012-03-09 15:46:52 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Storages/StorageMemory.h>
|
2017-04-17 16:02:48 +00:00
|
|
|
#include <Storages/StorageReplicatedMergeTree.h>
|
2014-03-04 15:31:56 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/ExternalTable.h>
|
2014-04-08 13:43:20 +00:00
|
|
|
|
2012-03-09 15:46:52 +00:00
|
|
|
#include "TCPHandler.h"
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/NetException.h>
|
2018-03-28 14:07:28 +00:00
|
|
|
#include <ext/scope_guard.h>
|
2012-03-09 15:46:52 +00:00
|
|
|
|
2018-02-16 20:53:47 +00:00
|
|
|
|
2012-03-09 15:46:52 +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 CLIENT_HAS_CONNECTED_TO_WRONG_PORT;
|
|
|
|
extern const int UNKNOWN_DATABASE;
|
|
|
|
extern const int UNKNOWN_EXCEPTION;
|
|
|
|
extern const int UNKNOWN_PACKET_FROM_CLIENT;
|
|
|
|
extern const int POCO_EXCEPTION;
|
|
|
|
extern const int STD_EXCEPTION;
|
|
|
|
extern const int SOCKET_TIMEOUT;
|
|
|
|
extern const int UNEXPECTED_PACKET_FROM_CLIENT;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
2012-03-09 15:46:52 +00:00
|
|
|
|
|
|
|
void TCPHandler::runImpl()
|
|
|
|
{
|
2017-08-09 11:57:09 +00:00
|
|
|
connection_context = server.context();
|
2017-04-01 07:20:54 +00:00
|
|
|
connection_context.setSessionContext(connection_context);
|
|
|
|
|
2017-08-09 11:57:09 +00:00
|
|
|
Settings global_settings = connection_context.getSettings();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
socket().setReceiveTimeout(global_settings.receive_timeout);
|
|
|
|
socket().setSendTimeout(global_settings.send_timeout);
|
|
|
|
socket().setNoDelay(true);
|
|
|
|
|
|
|
|
in = std::make_shared<ReadBufferFromPocoSocket>(socket());
|
|
|
|
out = std::make_shared<WriteBufferFromPocoSocket>(socket());
|
|
|
|
|
|
|
|
if (in->eof())
|
|
|
|
{
|
|
|
|
LOG_WARNING(log, "Client has not sent any data.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
receiveHello();
|
|
|
|
}
|
|
|
|
catch (const Exception & e) /// Typical for an incorrect username, password, or address.
|
|
|
|
{
|
|
|
|
if (e.code() == ErrorCodes::CLIENT_HAS_CONNECTED_TO_WRONG_PORT)
|
|
|
|
{
|
|
|
|
LOG_DEBUG(log, "Client has connected to wrong port.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.code() == ErrorCodes::ATTEMPT_TO_READ_AFTER_EOF)
|
|
|
|
{
|
|
|
|
LOG_WARNING(log, "Client has gone away.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2017-04-17 16:02:48 +00:00
|
|
|
/// We try to send error information to the client.
|
2017-04-01 07:20:54 +00:00
|
|
|
sendException(e);
|
|
|
|
}
|
|
|
|
catch (...) {}
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// When connecting, the default database can be specified.
|
|
|
|
if (!default_database.empty())
|
|
|
|
{
|
|
|
|
if (!connection_context.isDatabaseExist(default_database))
|
|
|
|
{
|
|
|
|
Exception e("Database " + default_database + " doesn't exist", ErrorCodes::UNKNOWN_DATABASE);
|
|
|
|
LOG_ERROR(log, "Code: " << e.code() << ", e.displayText() = " << e.displayText()
|
|
|
|
<< ", Stack trace:\n\n" << e.getStackTrace().toString());
|
|
|
|
sendException(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
connection_context.setCurrentDatabase(default_database);
|
|
|
|
}
|
|
|
|
|
|
|
|
sendHello();
|
|
|
|
|
|
|
|
connection_context.setProgressCallback([this] (const Progress & value) { return this->updateProgress(value); });
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2017-04-17 16:02:48 +00:00
|
|
|
/// We are waiting for a packet from the client. Thus, every `POLL_INTERVAL` seconds check whether we need to shut down.
|
2017-08-09 14:33:07 +00:00
|
|
|
while (!static_cast<ReadBufferFromPocoSocket &>(*in).poll(global_settings.poll_interval * 1000000) && !server.isCancelled())
|
2017-04-01 07:20:54 +00:00
|
|
|
;
|
|
|
|
|
2017-04-17 16:02:48 +00:00
|
|
|
/// If we need to shut down, or client disconnects.
|
2017-08-09 14:33:07 +00:00
|
|
|
if (server.isCancelled() || in->eof())
|
2017-04-01 07:20:54 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
Stopwatch watch;
|
|
|
|
state.reset();
|
|
|
|
|
|
|
|
/** An exception during the execution of request (it must be sent over the network to the client).
|
2017-04-17 16:02:48 +00:00
|
|
|
* The client will be able to accept it, if it did not happen while sending another packet and the client has not disconnected yet.
|
|
|
|
*/
|
2017-04-01 07:20:54 +00:00
|
|
|
std::unique_ptr<Exception> exception;
|
2018-04-04 12:11:38 +00:00
|
|
|
bool network_error = false;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2017-04-17 16:02:48 +00:00
|
|
|
/// Restore context of request.
|
2017-04-01 07:20:54 +00:00
|
|
|
query_context = connection_context;
|
|
|
|
|
2018-03-28 14:07:28 +00:00
|
|
|
/// If a user passed query-local timeouts, reset socket to initial state at the end of the query
|
|
|
|
SCOPE_EXIT({state.timeout_setter.reset();});
|
|
|
|
|
2017-04-17 16:02:48 +00:00
|
|
|
/** If Query - process it. If Ping or Cancel - go back to the beginning.
|
|
|
|
* There may come settings for a separate query that modify `query_context`.
|
|
|
|
*/
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!receivePacket())
|
|
|
|
continue;
|
|
|
|
|
2017-04-17 16:02:48 +00:00
|
|
|
/// Get blocks of temporary tables
|
2017-08-16 20:27:35 +00:00
|
|
|
readData(global_settings);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-17 16:02:48 +00:00
|
|
|
/// Reset the input stream, as we received an empty block while receiving external table data.
|
|
|
|
/// So, the stream has been marked as cancelled and we can't read from it anymore.
|
2017-04-01 07:20:54 +00:00
|
|
|
state.block_in.reset();
|
2017-04-17 16:02:48 +00:00
|
|
|
state.maybe_compressed_in.reset(); /// For more accurate accounting by MemoryTracker.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-17 16:02:48 +00:00
|
|
|
/// Processing Query
|
2017-04-01 07:20:54 +00:00
|
|
|
state.io = executeQuery(state.query, query_context, false, state.stage);
|
|
|
|
|
|
|
|
if (state.io.out)
|
|
|
|
state.need_receive_data_for_insert = true;
|
|
|
|
|
|
|
|
after_check_cancelled.restart();
|
|
|
|
after_send_progress.restart();
|
|
|
|
|
2017-04-17 16:02:48 +00:00
|
|
|
/// Does the request require receive data from client?
|
2017-04-01 07:20:54 +00:00
|
|
|
if (state.need_receive_data_for_insert)
|
|
|
|
processInsertQuery(global_settings);
|
|
|
|
else
|
|
|
|
processOrdinaryQuery();
|
|
|
|
|
|
|
|
sendEndOfStream();
|
|
|
|
|
|
|
|
state.reset();
|
|
|
|
}
|
|
|
|
catch (const Exception & e)
|
|
|
|
{
|
|
|
|
state.io.onException();
|
|
|
|
exception.reset(e.clone());
|
|
|
|
|
|
|
|
if (e.code() == ErrorCodes::UNKNOWN_PACKET_FROM_CLIENT)
|
|
|
|
throw;
|
2018-04-04 12:11:38 +00:00
|
|
|
|
|
|
|
/// If a timeout occurred, try to inform client about it and close the session
|
|
|
|
if (e.code() == ErrorCodes::SOCKET_TIMEOUT)
|
|
|
|
network_error = true;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
catch (const Poco::Net::NetException & e)
|
|
|
|
{
|
2017-04-17 16:02:48 +00:00
|
|
|
/** We can get here if there was an error during connection to the client,
|
|
|
|
* or in connection with a remote server that was used to process the request.
|
|
|
|
* It is not possible to distinguish between these two cases.
|
|
|
|
* Although in one of them, we have to send exception to the client, but in the other - we can not.
|
|
|
|
* We will try to send exception to the client in any case - see below.
|
|
|
|
*/
|
2017-04-01 07:20:54 +00:00
|
|
|
state.io.onException();
|
|
|
|
exception = std::make_unique<Exception>(e.displayText(), ErrorCodes::POCO_EXCEPTION);
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception & e)
|
|
|
|
{
|
|
|
|
state.io.onException();
|
|
|
|
exception = std::make_unique<Exception>(e.displayText(), ErrorCodes::POCO_EXCEPTION);
|
|
|
|
}
|
|
|
|
catch (const std::exception & e)
|
|
|
|
{
|
|
|
|
state.io.onException();
|
|
|
|
exception = std::make_unique<Exception>(e.what(), ErrorCodes::STD_EXCEPTION);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
state.io.onException();
|
|
|
|
exception = std::make_unique<Exception>("Unknown exception", ErrorCodes::UNKNOWN_EXCEPTION);
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (exception)
|
|
|
|
sendException(*exception);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2017-04-17 16:02:48 +00:00
|
|
|
/** Could not send exception information to the client. */
|
2017-04-01 07:20:54 +00:00
|
|
|
network_error = true;
|
|
|
|
LOG_WARNING(log, "Client has gone away.");
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
state.reset();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2017-04-17 16:02:48 +00:00
|
|
|
/** During the processing of request, there was an exception that we caught and possibly sent to client.
|
|
|
|
* When destroying the request pipeline execution there was a second exception.
|
|
|
|
* For example, a pipeline could run in multiple threads, and an exception could occur in each of them.
|
|
|
|
* Ignore it.
|
|
|
|
*/
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
watch.stop();
|
|
|
|
|
|
|
|
LOG_INFO(log, std::fixed << std::setprecision(3)
|
|
|
|
<< "Processed in " << watch.elapsedSeconds() << " sec.");
|
|
|
|
|
|
|
|
if (network_error)
|
|
|
|
break;
|
|
|
|
}
|
2012-03-11 08:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-04 15:31:56 +00:00
|
|
|
void TCPHandler::readData(const Settings & global_settings)
|
2012-05-21 06:49:05 +00:00
|
|
|
{
|
2018-04-04 12:11:38 +00:00
|
|
|
auto receive_timeout = query_context.getSettingsRef().receive_timeout.value;
|
|
|
|
|
|
|
|
/// Poll interval should not be greater than receive_timeout
|
|
|
|
size_t default_poll_interval = global_settings.poll_interval.value * 1000000;
|
|
|
|
size_t current_poll_interval = static_cast<size_t>(receive_timeout.totalMicroseconds());
|
|
|
|
constexpr size_t min_poll_interval = 5000; // 5 ms
|
|
|
|
size_t poll_interval = std::max(min_poll_interval, std::min(default_poll_interval, current_poll_interval));
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
Stopwatch watch(CLOCK_MONOTONIC_COARSE);
|
|
|
|
|
2017-04-17 16:02:48 +00:00
|
|
|
/// We are waiting for a packet from the client. Thus, every `POLL_INTERVAL` seconds check whether we need to shut down.
|
2017-04-01 07:20:54 +00:00
|
|
|
while (1)
|
|
|
|
{
|
2018-04-04 12:11:38 +00:00
|
|
|
if (static_cast<ReadBufferFromPocoSocket &>(*in).poll(poll_interval))
|
2017-04-01 07:20:54 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-17 16:02:48 +00:00
|
|
|
/// Do we need to shut down?
|
2017-08-09 14:33:07 +00:00
|
|
|
if (server.isCancelled())
|
2017-04-01 07:20:54 +00:00
|
|
|
return;
|
|
|
|
|
2017-04-17 16:02:48 +00:00
|
|
|
/** Have we waited for data for too long?
|
|
|
|
* If we periodically poll, the receive_timeout of the socket itself does not work.
|
|
|
|
* Therefore, an additional check is added.
|
|
|
|
*/
|
2018-04-04 12:11:38 +00:00
|
|
|
double elapsed = watch.elapsedSeconds();
|
|
|
|
if (elapsed > receive_timeout.totalSeconds())
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "Timeout exceeded while receiving data from client.";
|
|
|
|
ss << " Waited for " << static_cast<size_t>(elapsed) << " seconds,";
|
|
|
|
ss << " timeout is " << receive_timeout.totalSeconds() << " seconds.";
|
|
|
|
|
|
|
|
throw Exception(ss.str(), ErrorCodes::SOCKET_TIMEOUT);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// If client disconnected.
|
|
|
|
if (in->eof())
|
|
|
|
return;
|
|
|
|
|
|
|
|
/// We accept and process data. And if they are over, then we leave.
|
|
|
|
if (!receivePacket())
|
|
|
|
break;
|
|
|
|
}
|
2014-03-04 15:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TCPHandler::processInsertQuery(const Settings & global_settings)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/** Made above the rest of the lines, so that in case of `writePrefix` function throws an exception,
|
|
|
|
* client receive exception before sending data.
|
|
|
|
*/
|
|
|
|
state.io.out->writePrefix();
|
|
|
|
|
|
|
|
/// Send block to the client - table structure.
|
2018-02-19 00:45:32 +00:00
|
|
|
Block block = state.io.out->getHeader();
|
2017-04-01 07:20:54 +00:00
|
|
|
sendData(block);
|
|
|
|
|
|
|
|
readData(global_settings);
|
|
|
|
state.io.out->writeSuffix();
|
|
|
|
state.io.onFinish();
|
2012-05-21 06:49:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TCPHandler::processOrdinaryQuery()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Pull query execution result, if exists, and send it to network.
|
|
|
|
if (state.io.in)
|
|
|
|
{
|
|
|
|
/// Send header-block, to allow client to prepare output format for data to send.
|
2018-01-07 00:35:44 +00:00
|
|
|
{
|
|
|
|
Block header = state.io.in->getHeader();
|
2018-01-08 01:14:43 +00:00
|
|
|
if (header)
|
|
|
|
sendData(header);
|
2018-01-07 00:35:44 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
AsynchronousBlockInputStream async_in(state.io.in);
|
|
|
|
async_in.readPrefix();
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
Block block;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (isQueryCancelled())
|
|
|
|
{
|
2017-09-10 02:06:16 +00:00
|
|
|
/// A packet was received requesting to stop execution of the request.
|
2018-03-05 21:09:39 +00:00
|
|
|
async_in.cancel(false);
|
2017-04-01 07:20:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (state.progress.rows && after_send_progress.elapsed() / 1000 >= query_context.getSettingsRef().interactive_delay)
|
|
|
|
{
|
|
|
|
/// Some time passed and there is a progress.
|
|
|
|
after_send_progress.restart();
|
|
|
|
sendProgress();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (async_in.poll(query_context.getSettingsRef().interactive_delay / 1000))
|
|
|
|
{
|
|
|
|
/// There is the following result block.
|
|
|
|
block = async_in.read();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 01:14:43 +00:00
|
|
|
/** If data has run out, we will send the profiling data and total values to
|
|
|
|
* the last zero block to be able to use
|
|
|
|
* this information in the suffix output of stream.
|
|
|
|
* If the request was interrupted, then `sendTotals` and other methods could not be called,
|
|
|
|
* because we have not read all the data yet,
|
|
|
|
* and there could be ongoing calculations in other threads at the same time.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
|
|
|
if (!block && !isQueryCancelled())
|
|
|
|
{
|
|
|
|
sendTotals();
|
|
|
|
sendExtremes();
|
|
|
|
sendProfileInfo();
|
|
|
|
sendProgress();
|
|
|
|
}
|
|
|
|
|
|
|
|
sendData(block);
|
|
|
|
if (!block)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
async_in.readSuffix();
|
|
|
|
}
|
|
|
|
|
|
|
|
state.io.onFinish();
|
2012-07-21 07:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-17 16:02:48 +00:00
|
|
|
void TCPHandler::processTablesStatusRequest()
|
|
|
|
{
|
|
|
|
TablesStatusRequest request;
|
|
|
|
request.read(*in, client_revision);
|
|
|
|
|
|
|
|
TablesStatusResponse response;
|
|
|
|
for (const QualifiedTableName & table_name: request.tables)
|
|
|
|
{
|
2017-04-20 16:19:30 +00:00
|
|
|
StoragePtr table = connection_context.tryGetTable(table_name.database, table_name.table);
|
|
|
|
if (!table)
|
|
|
|
continue;
|
|
|
|
|
2017-04-17 16:02:48 +00:00
|
|
|
TableStatus status;
|
|
|
|
if (auto * replicated_table = dynamic_cast<StorageReplicatedMergeTree *>(table.get()))
|
|
|
|
{
|
|
|
|
status.is_replicated = true;
|
|
|
|
status.absolute_delay = replicated_table->getAbsoluteDelay();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
status.is_replicated = false;
|
|
|
|
|
|
|
|
response.table_states_by_id.emplace(table_name, std::move(status));
|
|
|
|
}
|
|
|
|
|
|
|
|
writeVarUInt(Protocol::Server::TablesStatusResponse, *out);
|
|
|
|
response.write(*out, client_revision);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-22 14:57:43 +00:00
|
|
|
void TCPHandler::sendProfileInfo()
|
|
|
|
{
|
2018-02-23 10:49:48 +00:00
|
|
|
if (const IProfilingBlockInputStream * input = dynamic_cast<const IProfilingBlockInputStream *>(state.io.in.get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
writeVarUInt(Protocol::Server::ProfileInfo, *out);
|
|
|
|
input->getProfileInfo().write(*out);
|
|
|
|
out->next();
|
|
|
|
}
|
2013-05-22 14:57:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-05 20:22:43 +00:00
|
|
|
void TCPHandler::sendTotals()
|
|
|
|
{
|
2018-02-23 10:49:48 +00:00
|
|
|
if (IProfilingBlockInputStream * input = dynamic_cast<IProfilingBlockInputStream *>(state.io.in.get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
const Block & totals = input->getTotals();
|
|
|
|
|
|
|
|
if (totals)
|
|
|
|
{
|
2018-02-19 00:45:32 +00:00
|
|
|
initBlockOutput(totals);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
writeVarUInt(Protocol::Server::Totals, *out);
|
2017-08-16 20:27:35 +00:00
|
|
|
writeStringBinary("", *out);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
state.block_out->write(totals);
|
|
|
|
state.maybe_compressed_out->next();
|
|
|
|
out->next();
|
|
|
|
}
|
|
|
|
}
|
2013-09-05 20:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-07 02:03:13 +00:00
|
|
|
void TCPHandler::sendExtremes()
|
|
|
|
{
|
2018-02-23 10:49:48 +00:00
|
|
|
if (IProfilingBlockInputStream * input = dynamic_cast<IProfilingBlockInputStream *>(state.io.in.get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-02-23 10:49:48 +00:00
|
|
|
Block extremes = input->getExtremes();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (extremes)
|
|
|
|
{
|
2018-02-19 00:45:32 +00:00
|
|
|
initBlockOutput(extremes);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
writeVarUInt(Protocol::Server::Extremes, *out);
|
2017-08-16 20:27:35 +00:00
|
|
|
writeStringBinary("", *out);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
state.block_out->write(extremes);
|
|
|
|
state.maybe_compressed_out->next();
|
|
|
|
out->next();
|
|
|
|
}
|
|
|
|
}
|
2013-09-07 02:03:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-21 06:49:05 +00:00
|
|
|
void TCPHandler::receiveHello()
|
2012-05-16 18:03:00 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Receive `hello` packet.
|
|
|
|
UInt64 packet_type = 0;
|
|
|
|
String user = "default";
|
|
|
|
String password;
|
|
|
|
|
|
|
|
readVarUInt(packet_type, *in);
|
|
|
|
if (packet_type != Protocol::Client::Hello)
|
|
|
|
{
|
|
|
|
/** If you accidentally accessed the HTTP protocol for a port destined for an internal TCP protocol,
|
2017-04-17 16:02:48 +00:00
|
|
|
* Then instead of the packet type, there will be G (GET) or P (POST), in most cases.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
|
|
|
if (packet_type == 'G' || packet_type == 'P')
|
|
|
|
{
|
|
|
|
writeString("HTTP/1.0 400 Bad Request\r\n\r\n"
|
|
|
|
"Port " + server.config().getString("tcp_port") + " is for clickhouse-client program.\r\n"
|
|
|
|
"You must use port " + server.config().getString("http_port") + " for HTTP.\r\n",
|
|
|
|
*out);
|
|
|
|
|
|
|
|
throw Exception("Client has connected to wrong port", ErrorCodes::CLIENT_HAS_CONNECTED_TO_WRONG_PORT);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw NetException("Unexpected packet from client", ErrorCodes::UNEXPECTED_PACKET_FROM_CLIENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
readStringBinary(client_name, *in);
|
|
|
|
readVarUInt(client_version_major, *in);
|
|
|
|
readVarUInt(client_version_minor, *in);
|
|
|
|
readVarUInt(client_revision, *in);
|
|
|
|
readStringBinary(default_database, *in);
|
|
|
|
readStringBinary(user, *in);
|
|
|
|
readStringBinary(password, *in);
|
|
|
|
|
|
|
|
LOG_DEBUG(log, "Connected " << client_name
|
|
|
|
<< " version " << client_version_major
|
|
|
|
<< "." << client_version_minor
|
|
|
|
<< "." << client_revision
|
|
|
|
<< (!default_database.empty() ? ", database: " + default_database : "")
|
|
|
|
<< (!user.empty() ? ", user: " + user : "")
|
|
|
|
<< ".");
|
|
|
|
|
|
|
|
connection_context.setUser(user, password, socket().peerAddress(), "");
|
2012-05-16 18:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-21 06:49:05 +00:00
|
|
|
void TCPHandler::sendHello()
|
2012-03-11 08:52:56 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
writeVarUInt(Protocol::Server::Hello, *out);
|
|
|
|
writeStringBinary(DBMS_NAME, *out);
|
|
|
|
writeVarUInt(DBMS_VERSION_MAJOR, *out);
|
|
|
|
writeVarUInt(DBMS_VERSION_MINOR, *out);
|
|
|
|
writeVarUInt(ClickHouseRevision::get(), *out);
|
|
|
|
if (client_revision >= DBMS_MIN_REVISION_WITH_SERVER_TIMEZONE)
|
|
|
|
{
|
|
|
|
writeStringBinary(DateLUT::instance().getTimeZone(), *out);
|
|
|
|
}
|
2018-02-26 06:49:17 +00:00
|
|
|
if (client_revision >= DBMS_MIN_REVISION_WITH_SERVER_DISPLAY_NAME)
|
2018-02-24 12:06:53 +00:00
|
|
|
{
|
2018-03-08 07:36:58 +00:00
|
|
|
writeStringBinary(server_display_name, *out);
|
2018-02-24 12:06:53 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
out->next();
|
2012-03-11 08:52:56 +00:00
|
|
|
}
|
|
|
|
|
2012-05-17 19:15:53 +00:00
|
|
|
|
2012-05-21 06:49:05 +00:00
|
|
|
bool TCPHandler::receivePacket()
|
2012-03-11 08:52:56 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
UInt64 packet_type = 0;
|
|
|
|
readVarUInt(packet_type, *in);
|
|
|
|
|
|
|
|
// std::cerr << "Packet: " << packet_type << std::endl;
|
|
|
|
|
|
|
|
switch (packet_type)
|
|
|
|
{
|
|
|
|
case Protocol::Client::Query:
|
|
|
|
if (!state.empty())
|
|
|
|
throw NetException("Unexpected packet Query received from client", ErrorCodes::UNEXPECTED_PACKET_FROM_CLIENT);
|
|
|
|
receiveQuery();
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case Protocol::Client::Data:
|
|
|
|
if (state.empty())
|
|
|
|
throw NetException("Unexpected packet Data received from client", ErrorCodes::UNEXPECTED_PACKET_FROM_CLIENT);
|
|
|
|
return receiveData();
|
|
|
|
|
|
|
|
case Protocol::Client::Ping:
|
|
|
|
writeVarUInt(Protocol::Server::Pong, *out);
|
|
|
|
out->next();
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case Protocol::Client::Cancel:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case Protocol::Client::Hello:
|
|
|
|
throw Exception("Unexpected packet " + String(Protocol::Client::toString(packet_type)) + " received from client",
|
|
|
|
ErrorCodes::UNEXPECTED_PACKET_FROM_CLIENT);
|
|
|
|
|
2017-04-17 16:02:48 +00:00
|
|
|
case Protocol::Client::TablesStatusRequest:
|
|
|
|
if (!state.empty())
|
|
|
|
throw NetException("Unexpected packet TablesStatusRequest received from client", ErrorCodes::UNEXPECTED_PACKET_FROM_CLIENT);
|
|
|
|
processTablesStatusRequest();
|
|
|
|
out->next();
|
|
|
|
return false;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
default:
|
2018-04-04 12:11:38 +00:00
|
|
|
throw Exception("Unknown packet " + toString(packet_type) + " from client", ErrorCodes::UNKNOWN_PACKET_FROM_CLIENT);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2012-03-09 15:46:52 +00:00
|
|
|
}
|
|
|
|
|
2012-05-17 19:15:53 +00:00
|
|
|
|
2012-05-21 06:49:05 +00:00
|
|
|
void TCPHandler::receiveQuery()
|
2012-03-11 08:52:56 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
UInt64 stage = 0;
|
|
|
|
UInt64 compression = 0;
|
|
|
|
|
|
|
|
state.is_empty = false;
|
|
|
|
readStringBinary(state.query_id, *in);
|
|
|
|
|
|
|
|
query_context.setCurrentQueryId(state.query_id);
|
|
|
|
|
|
|
|
/// Client info
|
|
|
|
{
|
|
|
|
ClientInfo & client_info = query_context.getClientInfo();
|
|
|
|
if (client_revision >= DBMS_MIN_REVISION_WITH_CLIENT_INFO)
|
|
|
|
client_info.read(*in, client_revision);
|
|
|
|
|
|
|
|
/// For better support of old clients, that does not send ClientInfo.
|
|
|
|
if (client_info.query_kind == ClientInfo::QueryKind::NO_QUERY)
|
|
|
|
{
|
|
|
|
client_info.query_kind = ClientInfo::QueryKind::INITIAL_QUERY;
|
|
|
|
client_info.client_name = client_name;
|
|
|
|
client_info.client_version_major = client_version_major;
|
|
|
|
client_info.client_version_minor = client_version_minor;
|
|
|
|
client_info.client_revision = client_revision;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set fields, that are known apriori.
|
|
|
|
client_info.interface = ClientInfo::Interface::TCP;
|
|
|
|
|
|
|
|
if (client_info.query_kind == ClientInfo::QueryKind::INITIAL_QUERY)
|
|
|
|
{
|
|
|
|
/// 'Current' fields was set at receiveHello.
|
|
|
|
client_info.initial_user = client_info.current_user;
|
|
|
|
client_info.initial_query_id = client_info.current_query_id;
|
|
|
|
client_info.initial_address = client_info.current_address;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Per query settings.
|
2018-03-28 14:07:28 +00:00
|
|
|
Settings & settings = query_context.getSettingsRef();
|
|
|
|
settings.deserialize(*in);
|
|
|
|
|
|
|
|
/// Sync timeouts on client and server during current query to avoid dangling queries on server
|
2018-03-29 13:33:48 +00:00
|
|
|
/// NOTE: We use settings.send_timeout for the receive timeout and vice versa (change arguments ordering in TimeoutSetter),
|
|
|
|
/// because settings.send_timeout is client-side setting which has opposite meaning on the server side.
|
2018-03-28 14:07:28 +00:00
|
|
|
/// NOTE: these settings are applied only for current connection (not for distributed tables' connections)
|
2018-03-29 13:33:48 +00:00
|
|
|
state.timeout_setter = std::make_unique<TimeoutSetter>(socket(), settings.receive_timeout, settings.send_timeout);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
readVarUInt(stage, *in);
|
|
|
|
state.stage = QueryProcessingStage::Enum(stage);
|
|
|
|
|
|
|
|
readVarUInt(compression, *in);
|
2017-10-03 14:52:08 +00:00
|
|
|
state.compression = static_cast<Protocol::Compression>(compression);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
readStringBinary(state.query, *in);
|
2012-03-11 08:52:56 +00:00
|
|
|
}
|
|
|
|
|
2012-05-21 06:49:05 +00:00
|
|
|
|
|
|
|
bool TCPHandler::receiveData()
|
2013-09-05 20:22:43 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
initBlockInput();
|
|
|
|
|
|
|
|
/// The name of the temporary table for writing data, default to empty string
|
|
|
|
String external_table_name;
|
2017-08-16 20:27:35 +00:00
|
|
|
readStringBinary(external_table_name, *in);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/// Read one block from the network and write it down
|
|
|
|
Block block = state.block_in->read();
|
|
|
|
|
|
|
|
if (block)
|
|
|
|
{
|
|
|
|
/// If there is an insert request, then the data should be written directly to `state.io.out`.
|
|
|
|
/// Otherwise, we write the blocks in the temporary `external_table_name` table.
|
|
|
|
if (!state.need_receive_data_for_insert)
|
|
|
|
{
|
|
|
|
StoragePtr storage;
|
|
|
|
/// If such a table does not exist, create it.
|
|
|
|
if (!(storage = query_context.tryGetExternalTable(external_table_name)))
|
|
|
|
{
|
2017-12-25 21:57:29 +00:00
|
|
|
NamesAndTypesList columns = block.getNamesAndTypesList();
|
2018-03-06 20:18:34 +00:00
|
|
|
storage = StorageMemory::create(external_table_name,
|
|
|
|
ColumnsDescription{columns, NamesAndTypesList{}, NamesAndTypesList{}, ColumnDefaults{}});
|
2017-06-06 17:06:14 +00:00
|
|
|
storage->startup();
|
2017-04-01 07:20:54 +00:00
|
|
|
query_context.addExternalTable(external_table_name, storage);
|
|
|
|
}
|
|
|
|
/// The data will be written directly to the table.
|
|
|
|
state.io.out = storage->write(ASTPtr(), query_context.getSettingsRef());
|
|
|
|
}
|
|
|
|
if (block)
|
|
|
|
state.io.out->write(block);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
2013-09-05 20:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TCPHandler::initBlockInput()
|
2012-03-11 08:52:56 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!state.block_in)
|
|
|
|
{
|
|
|
|
if (state.compression == Protocol::Compression::Enable)
|
|
|
|
state.maybe_compressed_in = std::make_shared<CompressedReadBuffer>(*in);
|
|
|
|
else
|
|
|
|
state.maybe_compressed_in = in;
|
|
|
|
|
|
|
|
state.block_in = std::make_shared<NativeBlockInputStream>(
|
|
|
|
*state.maybe_compressed_in,
|
|
|
|
client_revision);
|
|
|
|
}
|
2013-09-05 20:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-19 00:45:32 +00:00
|
|
|
void TCPHandler::initBlockOutput(const Block & block)
|
2013-09-05 20:22:43 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!state.block_out)
|
|
|
|
{
|
|
|
|
if (state.compression == Protocol::Compression::Enable)
|
|
|
|
state.maybe_compressed_out = std::make_shared<CompressedWriteBuffer>(
|
2017-10-12 23:56:28 +00:00
|
|
|
*out, CompressionSettings(query_context.getSettingsRef()));
|
2017-04-01 07:20:54 +00:00
|
|
|
else
|
|
|
|
state.maybe_compressed_out = out;
|
|
|
|
|
|
|
|
state.block_out = std::make_shared<NativeBlockOutputStream>(
|
|
|
|
*state.maybe_compressed_out,
|
2018-02-19 00:45:32 +00:00
|
|
|
client_revision,
|
|
|
|
block.cloneEmpty());
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2012-03-19 12:57:56 +00:00
|
|
|
}
|
2012-03-11 08:52:56 +00:00
|
|
|
|
2012-05-21 06:49:05 +00:00
|
|
|
|
|
|
|
bool TCPHandler::isQueryCancelled()
|
2012-05-09 08:16:09 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (state.is_cancelled || state.sent_all_data)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (after_check_cancelled.elapsed() / 1000 < query_context.getSettingsRef().interactive_delay)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
after_check_cancelled.restart();
|
|
|
|
|
|
|
|
/// During request execution the only packet that can come from the client is stopping the query.
|
|
|
|
if (static_cast<ReadBufferFromPocoSocket &>(*in).poll(0))
|
|
|
|
{
|
|
|
|
UInt64 packet_type = 0;
|
|
|
|
readVarUInt(packet_type, *in);
|
|
|
|
|
|
|
|
switch (packet_type)
|
|
|
|
{
|
|
|
|
case Protocol::Client::Cancel:
|
|
|
|
if (state.empty())
|
|
|
|
throw NetException("Unexpected packet Cancel received from client", ErrorCodes::UNEXPECTED_PACKET_FROM_CLIENT);
|
|
|
|
LOG_INFO(log, "Query was cancelled.");
|
|
|
|
state.is_cancelled = true;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw NetException("Unknown packet from client", ErrorCodes::UNKNOWN_PACKET_FROM_CLIENT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2012-05-09 08:16:09 +00:00
|
|
|
}
|
|
|
|
|
2012-05-21 06:49:05 +00:00
|
|
|
|
2018-01-07 00:35:44 +00:00
|
|
|
void TCPHandler::sendData(const Block & block)
|
2012-03-19 12:57:56 +00:00
|
|
|
{
|
2018-02-19 00:45:32 +00:00
|
|
|
initBlockOutput(block);
|
2012-03-11 08:52:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
writeVarUInt(Protocol::Server::Data, *out);
|
2017-08-16 20:27:35 +00:00
|
|
|
writeStringBinary("", *out);
|
2012-05-21 06:49:05 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
state.block_out->write(block);
|
|
|
|
state.maybe_compressed_out->next();
|
|
|
|
out->next();
|
2012-03-19 12:57:56 +00:00
|
|
|
}
|
|
|
|
|
2012-05-21 06:49:05 +00:00
|
|
|
|
2012-05-30 06:46:57 +00:00
|
|
|
void TCPHandler::sendException(const Exception & e)
|
2012-03-19 12:57:56 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
writeVarUInt(Protocol::Server::Exception, *out);
|
|
|
|
writeException(e, *out);
|
|
|
|
out->next();
|
2012-03-19 12:57:56 +00:00
|
|
|
}
|
|
|
|
|
2012-05-21 06:49:05 +00:00
|
|
|
|
|
|
|
void TCPHandler::sendEndOfStream()
|
2012-05-08 11:19:00 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
state.sent_all_data = true;
|
|
|
|
writeVarUInt(Protocol::Server::EndOfStream, *out);
|
|
|
|
out->next();
|
2012-05-08 11:19:00 +00:00
|
|
|
}
|
|
|
|
|
2012-05-21 06:49:05 +00:00
|
|
|
|
2014-10-25 18:33:52 +00:00
|
|
|
void TCPHandler::updateProgress(const Progress & value)
|
2012-03-19 12:57:56 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
state.progress.incrementPiecewiseAtomically(value);
|
2013-11-02 21:18:54 +00:00
|
|
|
}
|
2012-05-09 15:15:45 +00:00
|
|
|
|
|
|
|
|
2013-11-02 21:18:54 +00:00
|
|
|
void TCPHandler::sendProgress()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
writeVarUInt(Protocol::Server::Progress, *out);
|
2018-03-09 23:04:26 +00:00
|
|
|
auto increment = state.progress.fetchAndResetPiecewiseAtomically();
|
2017-04-01 07:20:54 +00:00
|
|
|
increment.write(*out, client_revision);
|
|
|
|
out->next();
|
2012-03-11 08:52:56 +00:00
|
|
|
}
|
|
|
|
|
2012-03-09 15:46:52 +00:00
|
|
|
|
|
|
|
void TCPHandler::run()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
runImpl();
|
|
|
|
|
|
|
|
LOG_INFO(log, "Done processing connection.");
|
|
|
|
}
|
|
|
|
catch (Poco::Exception & e)
|
|
|
|
{
|
|
|
|
/// Timeout - not an error.
|
|
|
|
if (!strcmp(e.what(), "Timeout"))
|
|
|
|
{
|
|
|
|
LOG_DEBUG(log, "Poco::Exception. Code: " << ErrorCodes::POCO_EXCEPTION << ", e.code() = " << e.code()
|
|
|
|
<< ", e.displayText() = " << e.displayText() << ", e.what() = " << e.what());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw;
|
|
|
|
}
|
2012-03-09 15:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|