2012-03-09 15:46:52 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
2012-03-11 08:52:56 +00:00
|
|
|
|
#include <DB/Core/Protocol.h>
|
2012-05-09 13:12:38 +00:00
|
|
|
|
#include <DB/Core/QueryProcessingStage.h>
|
2012-03-11 08:52:56 +00:00
|
|
|
|
|
2012-03-25 03:47:13 +00:00
|
|
|
|
#include <DB/IO/ChunkedReadBuffer.h>
|
|
|
|
|
#include <DB/IO/ChunkedWriteBuffer.h>
|
2012-03-19 12:57:56 +00:00
|
|
|
|
#include <DB/IO/ReadHelpers.h>
|
|
|
|
|
#include <DB/IO/WriteHelpers.h>
|
2012-05-09 08:16:09 +00:00
|
|
|
|
#include <DB/IO/ReadBufferFromPocoSocket.h>
|
2012-05-09 15:15:45 +00:00
|
|
|
|
#include <DB/IO/WriteBufferFromPocoSocket.h>
|
2012-03-19 12:57:56 +00:00
|
|
|
|
|
2012-03-11 08:52:56 +00:00
|
|
|
|
#include <DB/DataStreams/BlockIO.h>
|
|
|
|
|
|
2012-05-09 15:15:45 +00:00
|
|
|
|
#include <statdaemons/Stopwatch.h>
|
|
|
|
|
|
2012-03-09 15:46:52 +00:00
|
|
|
|
#include "Server.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
2012-03-11 08:52:56 +00:00
|
|
|
|
|
|
|
|
|
/// Состояние обработки запроса.
|
|
|
|
|
struct QueryState
|
|
|
|
|
{
|
|
|
|
|
/// Идентификатор запроса.
|
|
|
|
|
UInt64 query_id;
|
|
|
|
|
|
2012-05-09 13:12:38 +00:00
|
|
|
|
QueryProcessingStage::Enum stage;
|
2012-03-11 08:52:56 +00:00
|
|
|
|
Protocol::Compression::Enum compression;
|
|
|
|
|
String in_format;
|
|
|
|
|
String out_format;
|
|
|
|
|
|
2012-03-19 12:57:56 +00:00
|
|
|
|
/// Откуда читать данные для INSERT-а.
|
|
|
|
|
SharedPtr<ReadBuffer> chunked_in;
|
|
|
|
|
SharedPtr<ReadBuffer> maybe_compressed_in;
|
|
|
|
|
BlockInputStreamPtr block_in;
|
|
|
|
|
|
|
|
|
|
/// Куда писать возвращаемые данные.
|
|
|
|
|
SharedPtr<WriteBuffer> chunked_out;
|
|
|
|
|
SharedPtr<WriteBuffer> maybe_compressed_out;
|
|
|
|
|
BlockOutputStreamPtr block_out;
|
|
|
|
|
|
2012-03-11 08:52:56 +00:00
|
|
|
|
/// Текст запроса.
|
|
|
|
|
String query;
|
|
|
|
|
/// Потоки блоков, с помощью которых выполнять запрос.
|
|
|
|
|
BlockIO io;
|
|
|
|
|
|
|
|
|
|
Context context;
|
|
|
|
|
|
2012-05-08 05:42:05 +00:00
|
|
|
|
/** Исключение во время выполнения запроса (его надо отдать по сети клиенту).
|
|
|
|
|
* Клиент сможет его принять, если оно не произошло во время отправки другого пакета.
|
|
|
|
|
*/
|
|
|
|
|
SharedPtr<Exception> exception;
|
|
|
|
|
|
2012-05-09 15:50:42 +00:00
|
|
|
|
bool is_cancelled;
|
2012-05-09 15:15:45 +00:00
|
|
|
|
/// Данные были отправлены.
|
|
|
|
|
bool sent_all_data;
|
|
|
|
|
|
2012-05-09 15:50:42 +00:00
|
|
|
|
/// Для вывода прогресса.
|
|
|
|
|
size_t rows_processed;
|
|
|
|
|
size_t bytes_processed;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QueryState() : query_id(0), stage(QueryProcessingStage::Complete), compression(Protocol::Compression::Disable),
|
|
|
|
|
is_cancelled(false), sent_all_data(false), rows_processed(0), bytes_processed(0) {}
|
2012-03-11 08:52:56 +00:00
|
|
|
|
|
|
|
|
|
void reset()
|
|
|
|
|
{
|
2012-05-09 08:16:09 +00:00
|
|
|
|
*this = QueryState();
|
2012-03-11 08:52:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool empty()
|
|
|
|
|
{
|
|
|
|
|
return query_id == 0;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2012-03-09 15:46:52 +00:00
|
|
|
|
class TCPHandler : public Poco::Net::TCPServerConnection
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
TCPHandler(Server & server_, const Poco::Net::StreamSocket & socket_)
|
|
|
|
|
: Poco::Net::TCPServerConnection(socket_), server(server_)
|
|
|
|
|
, log(&Logger::get("TCPHandler"))
|
|
|
|
|
{
|
|
|
|
|
LOG_TRACE(log, "In constructor.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void run();
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
Server & server;
|
|
|
|
|
Logger * log;
|
|
|
|
|
|
2012-03-11 08:52:56 +00:00
|
|
|
|
/// На данный момент, поддерживается одновременное выполнение только одного запроса в соединении.
|
|
|
|
|
QueryState state;
|
|
|
|
|
|
2012-05-09 08:16:09 +00:00
|
|
|
|
Poco::FastMutex is_cancelled_mutex;
|
2012-05-09 15:15:45 +00:00
|
|
|
|
/// Для сериализации пакетов "данные" и "прогресс" (пакет типа "прогресс" может отправляться из другого потока).
|
|
|
|
|
Poco::FastMutex send_mutex;
|
|
|
|
|
|
|
|
|
|
/// Время после последней проверки остановки запроса и отправки прогресса.
|
|
|
|
|
Stopwatch after_check_cancelled;
|
|
|
|
|
Stopwatch after_send_progress;
|
2012-05-09 08:16:09 +00:00
|
|
|
|
|
|
|
|
|
|
2012-03-09 15:46:52 +00:00
|
|
|
|
void runImpl();
|
2012-03-11 08:52:56 +00:00
|
|
|
|
|
|
|
|
|
void sendHello(WriteBuffer & out);
|
2012-03-19 12:57:56 +00:00
|
|
|
|
bool sendData(WriteBuffer & out, WriteBuffer & out_for_chunks);
|
2012-03-11 08:52:56 +00:00
|
|
|
|
void sendException(WriteBuffer & out);
|
2012-05-09 15:15:45 +00:00
|
|
|
|
void sendProgress(WriteBuffer & out, size_t rows, size_t bytes);
|
2012-05-08 11:19:00 +00:00
|
|
|
|
void sendOk(WriteBuffer & out);
|
2012-03-11 08:52:56 +00:00
|
|
|
|
|
2012-05-09 13:12:38 +00:00
|
|
|
|
bool receivePacket(ReadBuffer & in, WriteBuffer & out);
|
2012-03-11 08:52:56 +00:00
|
|
|
|
void receiveQuery(ReadBuffer & in);
|
2012-03-19 12:57:56 +00:00
|
|
|
|
bool receiveData(ReadBuffer & in);
|
2012-05-09 08:16:09 +00:00
|
|
|
|
|
|
|
|
|
bool isQueryCancelled(ReadBufferFromPocoSocket & in);
|
2012-03-09 15:46:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
2012-03-11 08:52:56 +00:00
|
|
|
|
|
2012-03-09 15:46:52 +00:00
|
|
|
|
}
|