2012-03-09 15:46:52 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/types.h>
|
2021-10-03 09:47:38 +00:00
|
|
|
#include <Core/ProtocolDefines.h>
|
2013-08-04 00:42:35 +00:00
|
|
|
|
|
|
|
|
2012-03-09 15:46:52 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-04-17 19:26:59 +00:00
|
|
|
|
|
|
|
/// Client-server protocol.
|
|
|
|
///
|
|
|
|
/// Client opens a connection and sends Hello packet.
|
|
|
|
/// If client version is incompatible, the server can terminate the connection.
|
|
|
|
/// Server responds with Hello packet.
|
|
|
|
/// If server version is incompatible, the client can terminate the connection.
|
|
|
|
///
|
|
|
|
/// The main loop follows:
|
|
|
|
///
|
|
|
|
/// 1. The client sends Query packet.
|
|
|
|
///
|
|
|
|
/// Starting from version 50263 immediately after sending the Query packet the client starts
|
|
|
|
/// transfer of external (temporary) table (external storages) - one or several Data packets.
|
|
|
|
/// End of transmission is marked by an empty block.
|
|
|
|
/// At present, non-empty tables can be sent only along with SELECT query.
|
|
|
|
///
|
|
|
|
/// If the query is an INSERT (and thus requires data transfer from client), then the server transmits
|
|
|
|
/// Data packet containing empty block that describes the table structure.
|
|
|
|
/// Then the client sends one or several Data packets - data for insertion.
|
|
|
|
/// End of data is marked by the transmission of empty block.
|
|
|
|
/// Then the server sends EndOfStream packet.
|
|
|
|
///
|
|
|
|
/// If the query is a SELECT or a query of other type, then the server transmits packets of
|
|
|
|
/// one of the following types:
|
|
|
|
/// - Data - data corresponding to one block of query results.
|
|
|
|
/// - Progress - query execution progress.
|
|
|
|
/// - Exception - error description.
|
|
|
|
/// - EndOfStream - the end of data transmission.
|
|
|
|
///
|
|
|
|
/// The client should read packets until EndOfStream or Exception.
|
|
|
|
///
|
|
|
|
/// The client can also send Cancel packet - a request to cancel the query.
|
|
|
|
/// In this case the server can stop executing the query and return incomplete data,
|
|
|
|
/// but the client must still read until EndOfStream packet.
|
|
|
|
///
|
|
|
|
/// Also if there is profiling info and the client revision is recent enough, the server can
|
|
|
|
/// send one of the following packets before EndOfStream:
|
|
|
|
/// - Totals - a block with total values
|
|
|
|
/// - ProfileInfo - serialized BlockStreamProfileInfo structure.
|
|
|
|
///
|
|
|
|
/// If a query returns data, the server sends an empty header block containing
|
|
|
|
/// the description of resulting columns before executing the query.
|
2020-06-27 19:05:00 +00:00
|
|
|
/// Using this block the client can initialize the output formatter and display the prefix of resulting table
|
2017-04-17 19:26:59 +00:00
|
|
|
/// beforehand.
|
2012-03-09 15:46:52 +00:00
|
|
|
|
2023-09-26 15:50:19 +00:00
|
|
|
namespace EncodedUserInfo
|
|
|
|
{
|
|
|
|
|
2024-04-04 20:58:35 +00:00
|
|
|
/// Marker for the inter-server secret (passed as the user name)
|
2020-09-14 21:55:43 +00:00
|
|
|
/// (anyway user cannot be started with a whitespace)
|
|
|
|
const char USER_INTERSERVER_MARKER[] = " INTERSERVER SECRET ";
|
2024-04-04 20:58:35 +00:00
|
|
|
|
|
|
|
/// Marker for SSH-keys-based authentication (passed as the user name)
|
2023-09-26 15:50:19 +00:00
|
|
|
const char SSH_KEY_AUTHENTICAION_MARKER[] = " SSH KEY AUTHENTICATION ";
|
|
|
|
|
|
|
|
};
|
2020-09-14 21:55:43 +00:00
|
|
|
|
2012-03-09 15:46:52 +00:00
|
|
|
namespace Protocol
|
|
|
|
{
|
2017-04-17 19:26:59 +00:00
|
|
|
/// Packet types that server transmits.
|
2012-03-09 15:46:52 +00:00
|
|
|
namespace Server
|
|
|
|
{
|
|
|
|
enum Enum
|
|
|
|
{
|
2021-12-09 10:39:28 +00:00
|
|
|
Hello = 0, /// Name, version, revision.
|
|
|
|
Data = 1, /// A block of data (compressed or not).
|
|
|
|
Exception = 2, /// The exception during query execution.
|
|
|
|
Progress = 3, /// Query execution progress: rows read, bytes read.
|
|
|
|
Pong = 4, /// Ping response
|
|
|
|
EndOfStream = 5, /// All packets were transmitted
|
|
|
|
ProfileInfo = 6, /// Packet with profiling info.
|
|
|
|
Totals = 7, /// A block with totals (compressed or not).
|
|
|
|
Extremes = 8, /// A block with minimums and maximums (compressed or not).
|
|
|
|
TablesStatusResponse = 9, /// A response to TablesStatus request.
|
|
|
|
Log = 10, /// System logs of the query execution
|
|
|
|
TableColumns = 11, /// Columns' description for default values calculation
|
|
|
|
PartUUIDs = 12, /// List of unique parts ids.
|
|
|
|
ReadTaskRequest = 13, /// String (UUID) describes a request for which next task is needed
|
|
|
|
/// This is such an inverted logic, where server sends requests
|
|
|
|
/// And client returns back response
|
|
|
|
ProfileEvents = 14, /// Packet with profile events from server.
|
2023-09-04 13:53:06 +00:00
|
|
|
MergeTreeAllRangesAnnouncement = 15,
|
2023-02-03 13:34:18 +00:00
|
|
|
MergeTreeReadTaskRequest = 16, /// Request from a MergeTree replica to a coordinator
|
2023-03-07 15:05:23 +00:00
|
|
|
TimezoneUpdate = 17, /// Receive server's (session-wide) default timezone
|
2023-09-26 15:50:19 +00:00
|
|
|
SSHChallenge = 18, /// Return challenge for SSH signature signing
|
|
|
|
MAX = SSHChallenge,
|
2021-12-09 10:39:28 +00:00
|
|
|
|
2012-03-09 15:46:52 +00:00
|
|
|
};
|
2012-05-23 19:51:30 +00:00
|
|
|
|
2017-04-17 19:26:59 +00:00
|
|
|
/// NOTE: If the type of packet argument would be Enum, the comparison packet >= 0 && packet < 10
|
2023-11-23 14:51:49 +00:00
|
|
|
/// would always be true because of compiler optimization. That would lead to out-of-bounds error
|
2017-04-17 19:26:59 +00:00
|
|
|
/// if the packet is invalid.
|
|
|
|
/// See https://www.securecoding.cert.org/confluence/display/cplusplus/INT36-CPP.+Do+not+use+out-of-range+enumeration+values
|
2013-08-04 00:42:35 +00:00
|
|
|
inline const char * toString(UInt64 packet)
|
2012-05-23 19:51:30 +00:00
|
|
|
{
|
2020-09-14 21:55:43 +00:00
|
|
|
static const char * data[] = {
|
|
|
|
"Hello",
|
|
|
|
"Data",
|
|
|
|
"Exception",
|
|
|
|
"Progress",
|
|
|
|
"Pong",
|
|
|
|
"EndOfStream",
|
|
|
|
"ProfileInfo",
|
|
|
|
"Totals",
|
|
|
|
"Extremes",
|
|
|
|
"TablesStatusResponse",
|
|
|
|
"Log",
|
|
|
|
"TableColumns",
|
2020-11-20 17:23:53 +00:00
|
|
|
"PartUUIDs",
|
2021-09-02 14:27:19 +00:00
|
|
|
"ReadTaskRequest",
|
|
|
|
"ProfileEvents",
|
2023-09-04 13:53:06 +00:00
|
|
|
"MergeTreeAllRangesAnnouncement",
|
2021-12-09 10:39:28 +00:00
|
|
|
"MergeTreeReadTaskRequest",
|
2023-03-07 15:05:23 +00:00
|
|
|
"TimezoneUpdate",
|
2023-09-26 15:50:19 +00:00
|
|
|
"SSHChallenge",
|
2020-09-14 21:55:43 +00:00
|
|
|
};
|
|
|
|
return packet <= MAX
|
2012-10-12 17:54:26 +00:00
|
|
|
? data[packet]
|
|
|
|
: "Unknown packet";
|
2012-05-23 19:51:30 +00:00
|
|
|
}
|
2018-12-04 20:03:04 +00:00
|
|
|
|
2018-12-06 17:20:17 +00:00
|
|
|
inline size_t stringsInMessage(UInt64 msg_type)
|
2018-12-04 20:03:04 +00:00
|
|
|
{
|
|
|
|
switch (msg_type)
|
|
|
|
{
|
|
|
|
case TableColumns:
|
|
|
|
return 2;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2012-03-09 15:46:52 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:26:59 +00:00
|
|
|
/// Packet types that client transmits.
|
2012-03-09 15:46:52 +00:00
|
|
|
namespace Client
|
|
|
|
{
|
|
|
|
enum Enum
|
|
|
|
{
|
2021-12-09 10:39:28 +00:00
|
|
|
Hello = 0, /// Name, version, revision, default DB
|
|
|
|
Query = 1, /// Query id, query settings, stage up to which the query must be executed,
|
|
|
|
/// whether the compression must be used,
|
|
|
|
/// query text (without data for INSERTs).
|
|
|
|
Data = 2, /// A block of data (compressed or not).
|
|
|
|
Cancel = 3, /// Cancel the query execution.
|
|
|
|
Ping = 4, /// Check that connection to the server is alive.
|
|
|
|
TablesStatusRequest = 5, /// Check status of tables on the server.
|
|
|
|
KeepAlive = 6, /// Keep the connection alive
|
|
|
|
Scalar = 7, /// A block of data (compressed or not).
|
|
|
|
IgnoredPartUUIDs = 8, /// List of unique parts ids to exclude from query processing
|
|
|
|
ReadTaskResponse = 9, /// A filename to read from s3 (used in s3Cluster)
|
|
|
|
MergeTreeReadTaskResponse = 10, /// Coordinator's decision with a modified set of mark ranges allowed to read
|
2023-09-26 15:50:19 +00:00
|
|
|
|
2024-04-04 20:58:35 +00:00
|
|
|
SSHChallengeRequest = 11, /// Request SSH signature challenge
|
|
|
|
SSHChallengeResponse = 12, /// Reply to SSH signature challenge
|
2023-09-26 15:50:19 +00:00
|
|
|
MAX = SSHChallengeResponse,
|
2012-03-09 15:46:52 +00:00
|
|
|
};
|
2012-05-23 19:51:30 +00:00
|
|
|
|
2013-08-04 00:42:35 +00:00
|
|
|
inline const char * toString(UInt64 packet)
|
2012-05-23 19:51:30 +00:00
|
|
|
{
|
2020-09-14 21:55:43 +00:00
|
|
|
static const char * data[] = {
|
|
|
|
"Hello",
|
|
|
|
"Query",
|
|
|
|
"Data",
|
|
|
|
"Cancel",
|
|
|
|
"Ping",
|
|
|
|
"TablesStatusRequest",
|
|
|
|
"KeepAlive",
|
2020-10-02 14:10:19 +00:00
|
|
|
"Scalar",
|
2020-11-20 17:23:53 +00:00
|
|
|
"IgnoredPartUUIDs",
|
2021-04-06 11:05:47 +00:00
|
|
|
"ReadTaskResponse",
|
2023-09-26 15:50:19 +00:00
|
|
|
"MergeTreeReadTaskResponse",
|
|
|
|
"SSHChallengeRequest",
|
|
|
|
"SSHChallengeResponse"
|
2020-09-14 21:55:43 +00:00
|
|
|
};
|
|
|
|
return packet <= MAX
|
2018-11-14 15:23:00 +00:00
|
|
|
? data[packet]
|
|
|
|
: "Unknown packet";
|
2012-05-23 19:51:30 +00:00
|
|
|
}
|
2012-03-09 15:46:52 +00:00
|
|
|
}
|
2012-03-11 08:52:56 +00:00
|
|
|
|
2017-04-17 19:26:59 +00:00
|
|
|
/// Whether the compression must be used.
|
2017-10-03 14:52:08 +00:00
|
|
|
enum class Compression
|
2012-03-11 08:52:56 +00:00
|
|
|
{
|
2017-10-03 14:52:08 +00:00
|
|
|
Disable = 0,
|
|
|
|
Enable = 1,
|
|
|
|
};
|
2017-09-28 19:43:31 +00:00
|
|
|
|
|
|
|
/// Whether the ssl must be used.
|
2018-03-29 01:41:06 +00:00
|
|
|
enum class Secure
|
2017-09-28 19:43:31 +00:00
|
|
|
{
|
2017-10-03 14:52:08 +00:00
|
|
|
Disable = 0,
|
|
|
|
Enable = 1,
|
|
|
|
};
|
|
|
|
|
2012-03-09 15:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|