2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/ClientInfo.h>
|
|
|
|
#include <IO/ReadBuffer.h>
|
|
|
|
#include <IO/WriteBuffer.h>
|
|
|
|
#include <IO/ReadHelpers.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
2021-10-03 09:47:38 +00:00
|
|
|
#include <Core/ProtocolDefines.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/getFQDNOrHostName.h>
|
2020-02-17 14:27:09 +00:00
|
|
|
#include <unistd.h>
|
2016-10-24 21:40:39 +00:00
|
|
|
|
2021-10-27 23:10:39 +00:00
|
|
|
#include <Common/config_version.h>
|
2020-04-16 12:31:57 +00:00
|
|
|
|
2016-10-24 21:40:39 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2016-10-25 05:07:29 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2016-10-24 21:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-13 11:59:20 +00:00
|
|
|
void ClientInfo::write(WriteBuffer & out, UInt64 server_protocol_revision) const
|
2016-10-24 21:40:39 +00:00
|
|
|
{
|
2016-11-16 20:46:02 +00:00
|
|
|
if (server_protocol_revision < DBMS_MIN_REVISION_WITH_CLIENT_INFO)
|
2016-10-25 05:07:29 +00:00
|
|
|
throw Exception("Logical error: method ClientInfo::write is called for unsupported server revision", ErrorCodes::LOGICAL_ERROR);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-04-18 08:18:31 +00:00
|
|
|
writeBinary(static_cast<UInt8>(query_kind), out);
|
2016-10-24 21:40:39 +00:00
|
|
|
if (empty())
|
|
|
|
return;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-10-24 21:40:39 +00:00
|
|
|
writeBinary(initial_user, out);
|
|
|
|
writeBinary(initial_query_id, out);
|
|
|
|
writeBinary(initial_address.toString(), out);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2021-05-20 13:21:42 +00:00
|
|
|
if (server_protocol_revision >= DBMS_MIN_PROTOCOL_VERSION_WITH_INITIAL_QUERY_START_TIME)
|
|
|
|
writeBinary(initial_query_start_time_microseconds, out);
|
|
|
|
|
2022-04-18 08:18:31 +00:00
|
|
|
writeBinary(static_cast<UInt8>(interface), out);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-10-24 21:40:39 +00:00
|
|
|
if (interface == Interface::TCP)
|
|
|
|
{
|
|
|
|
writeBinary(os_user, out);
|
|
|
|
writeBinary(client_hostname, out);
|
|
|
|
writeBinary(client_name, out);
|
|
|
|
writeVarUInt(client_version_major, out);
|
|
|
|
writeVarUInt(client_version_minor, out);
|
2020-09-17 12:15:05 +00:00
|
|
|
writeVarUInt(client_tcp_protocol_version, out);
|
2016-10-24 21:40:39 +00:00
|
|
|
}
|
|
|
|
else if (interface == Interface::HTTP)
|
|
|
|
{
|
2022-04-18 08:18:31 +00:00
|
|
|
writeBinary(static_cast<UInt8>(http_method), out);
|
2016-10-24 21:40:39 +00:00
|
|
|
writeBinary(http_user_agent, out);
|
2020-12-01 21:07:20 +00:00
|
|
|
|
|
|
|
if (server_protocol_revision >= DBMS_MIN_REVISION_WITH_X_FORWARDED_FOR_IN_CLIENT_INFO)
|
|
|
|
writeBinary(forwarded_for, out);
|
2021-01-21 22:55:45 +00:00
|
|
|
|
|
|
|
if (server_protocol_revision >= DBMS_MIN_REVISION_WITH_REFERER_IN_CLIENT_INFO)
|
|
|
|
writeBinary(http_referer, out);
|
2016-10-24 21:40:39 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-11-16 20:46:02 +00:00
|
|
|
if (server_protocol_revision >= DBMS_MIN_REVISION_WITH_QUOTA_KEY_IN_CLIENT_INFO)
|
2016-11-16 05:09:41 +00:00
|
|
|
writeBinary(quota_key, out);
|
2018-07-31 21:36:18 +00:00
|
|
|
|
2021-04-07 01:58:57 +00:00
|
|
|
if (server_protocol_revision >= DBMS_MIN_PROTOCOL_VERSION_WITH_DISTRIBUTED_DEPTH)
|
2021-04-08 12:17:47 +00:00
|
|
|
writeVarUInt(distributed_depth, out);
|
2021-03-20 12:10:22 +00:00
|
|
|
|
2018-07-31 21:36:18 +00:00
|
|
|
if (interface == Interface::TCP)
|
|
|
|
{
|
|
|
|
if (server_protocol_revision >= DBMS_MIN_REVISION_WITH_VERSION_PATCH)
|
|
|
|
writeVarUInt(client_version_patch, out);
|
|
|
|
}
|
2020-08-28 01:21:08 +00:00
|
|
|
|
|
|
|
if (server_protocol_revision >= DBMS_MIN_REVISION_WITH_OPENTELEMETRY)
|
|
|
|
{
|
2021-05-04 22:42:14 +00:00
|
|
|
if (client_trace_context.trace_id != UUID())
|
2020-09-08 13:19:27 +00:00
|
|
|
{
|
2020-09-30 16:16:33 +00:00
|
|
|
// Have OpenTelemetry header.
|
2020-09-08 13:19:27 +00:00
|
|
|
writeBinary(uint8_t(1), out);
|
2020-09-30 16:16:33 +00:00
|
|
|
// No point writing these numbers with variable length, because they
|
|
|
|
// are random and will probably require the full length anyway.
|
2020-11-18 17:43:18 +00:00
|
|
|
writeBinary(client_trace_context.trace_id, out);
|
|
|
|
writeBinary(client_trace_context.span_id, out);
|
|
|
|
writeBinary(client_trace_context.tracestate, out);
|
|
|
|
writeBinary(client_trace_context.trace_flags, out);
|
2020-09-08 13:19:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-09-30 16:16:33 +00:00
|
|
|
// Don't have OpenTelemetry header.
|
2022-04-18 08:18:31 +00:00
|
|
|
writeBinary(static_cast<UInt8>(0), out);
|
2020-09-08 13:19:27 +00:00
|
|
|
}
|
2020-08-28 01:21:08 +00:00
|
|
|
}
|
2021-12-09 10:39:28 +00:00
|
|
|
|
|
|
|
if (server_protocol_revision >= DBMS_MIN_REVISION_WITH_PARALLEL_REPLICAS)
|
|
|
|
{
|
|
|
|
writeVarUInt(static_cast<UInt64>(collaborate_with_initiator), out);
|
|
|
|
writeVarUInt(count_participating_replicas, out);
|
|
|
|
writeVarUInt(number_of_current_replica, out);
|
|
|
|
}
|
2016-10-24 21:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-13 11:59:20 +00:00
|
|
|
void ClientInfo::read(ReadBuffer & in, UInt64 client_protocol_revision)
|
2016-10-24 21:40:39 +00:00
|
|
|
{
|
2016-11-16 20:46:02 +00:00
|
|
|
if (client_protocol_revision < DBMS_MIN_REVISION_WITH_CLIENT_INFO)
|
2016-10-25 05:07:29 +00:00
|
|
|
throw Exception("Logical error: method ClientInfo::read is called for unsupported client revision", ErrorCodes::LOGICAL_ERROR);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-10-24 21:40:39 +00:00
|
|
|
UInt8 read_query_kind = 0;
|
|
|
|
readBinary(read_query_kind, in);
|
|
|
|
query_kind = QueryKind(read_query_kind);
|
|
|
|
if (empty())
|
|
|
|
return;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-10-24 21:40:39 +00:00
|
|
|
readBinary(initial_user, in);
|
|
|
|
readBinary(initial_query_id, in);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-10-24 21:40:39 +00:00
|
|
|
String initial_address_string;
|
|
|
|
readBinary(initial_address_string, in);
|
|
|
|
initial_address = Poco::Net::SocketAddress(initial_address_string);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2021-05-20 13:21:42 +00:00
|
|
|
if (client_protocol_revision >= DBMS_MIN_PROTOCOL_VERSION_WITH_INITIAL_QUERY_START_TIME)
|
|
|
|
{
|
|
|
|
readBinary(initial_query_start_time_microseconds, in);
|
2021-06-11 15:06:25 +00:00
|
|
|
initial_query_start_time = initial_query_start_time_microseconds / 1000000;
|
2021-05-20 13:21:42 +00:00
|
|
|
}
|
|
|
|
|
2016-10-24 21:40:39 +00:00
|
|
|
UInt8 read_interface = 0;
|
|
|
|
readBinary(read_interface, in);
|
|
|
|
interface = Interface(read_interface);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-10-24 21:40:39 +00:00
|
|
|
if (interface == Interface::TCP)
|
|
|
|
{
|
|
|
|
readBinary(os_user, in);
|
|
|
|
readBinary(client_hostname, in);
|
|
|
|
readBinary(client_name, in);
|
|
|
|
readVarUInt(client_version_major, in);
|
|
|
|
readVarUInt(client_version_minor, in);
|
2020-09-17 12:15:05 +00:00
|
|
|
readVarUInt(client_tcp_protocol_version, in);
|
2016-10-24 21:40:39 +00:00
|
|
|
}
|
|
|
|
else if (interface == Interface::HTTP)
|
|
|
|
{
|
|
|
|
UInt8 read_http_method = 0;
|
|
|
|
readBinary(read_http_method, in);
|
|
|
|
http_method = HTTPMethod(read_http_method);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-10-24 21:40:39 +00:00
|
|
|
readBinary(http_user_agent, in);
|
2020-12-01 21:07:20 +00:00
|
|
|
|
|
|
|
if (client_protocol_revision >= DBMS_MIN_REVISION_WITH_X_FORWARDED_FOR_IN_CLIENT_INFO)
|
|
|
|
readBinary(forwarded_for, in);
|
2021-01-21 22:55:45 +00:00
|
|
|
|
|
|
|
if (client_protocol_revision >= DBMS_MIN_REVISION_WITH_REFERER_IN_CLIENT_INFO)
|
|
|
|
readBinary(http_referer, in);
|
2016-10-24 21:40:39 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-11-16 20:46:02 +00:00
|
|
|
if (client_protocol_revision >= DBMS_MIN_REVISION_WITH_QUOTA_KEY_IN_CLIENT_INFO)
|
2016-11-16 05:09:41 +00:00
|
|
|
readBinary(quota_key, in);
|
2018-07-31 21:36:18 +00:00
|
|
|
|
2021-04-07 01:58:57 +00:00
|
|
|
if (client_protocol_revision >= DBMS_MIN_PROTOCOL_VERSION_WITH_DISTRIBUTED_DEPTH)
|
2021-04-08 12:17:47 +00:00
|
|
|
readVarUInt(distributed_depth, in);
|
2021-03-20 12:10:22 +00:00
|
|
|
|
2018-07-31 21:36:18 +00:00
|
|
|
if (interface == Interface::TCP)
|
|
|
|
{
|
|
|
|
if (client_protocol_revision >= DBMS_MIN_REVISION_WITH_VERSION_PATCH)
|
|
|
|
readVarUInt(client_version_patch, in);
|
|
|
|
else
|
2020-09-17 12:15:05 +00:00
|
|
|
client_version_patch = client_tcp_protocol_version;
|
2018-07-31 21:36:18 +00:00
|
|
|
}
|
2020-08-28 01:21:08 +00:00
|
|
|
|
|
|
|
if (client_protocol_revision >= DBMS_MIN_REVISION_WITH_OPENTELEMETRY)
|
|
|
|
{
|
2020-09-08 13:19:27 +00:00
|
|
|
uint8_t have_trace_id = 0;
|
|
|
|
readBinary(have_trace_id, in);
|
|
|
|
if (have_trace_id)
|
|
|
|
{
|
2020-11-18 17:43:18 +00:00
|
|
|
readBinary(client_trace_context.trace_id, in);
|
|
|
|
readBinary(client_trace_context.span_id, in);
|
|
|
|
readBinary(client_trace_context.tracestate, in);
|
|
|
|
readBinary(client_trace_context.trace_flags, in);
|
2020-09-08 13:19:27 +00:00
|
|
|
}
|
2020-08-28 01:21:08 +00:00
|
|
|
}
|
2021-12-09 10:39:28 +00:00
|
|
|
|
|
|
|
if (client_protocol_revision >= DBMS_MIN_REVISION_WITH_PARALLEL_REPLICAS)
|
|
|
|
{
|
|
|
|
UInt64 value;
|
|
|
|
readVarUInt(value, in);
|
|
|
|
collaborate_with_initiator = static_cast<bool>(value);
|
|
|
|
readVarUInt(count_participating_replicas, in);
|
|
|
|
readVarUInt(number_of_current_replica, in);
|
|
|
|
}
|
2016-10-24 21:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-15 01:58:10 +00:00
|
|
|
void ClientInfo::setInitialQuery()
|
|
|
|
{
|
|
|
|
query_kind = QueryKind::INITIAL_QUERY;
|
|
|
|
fillOSUserHostNameAndVersionInfo();
|
2022-04-08 05:05:04 +00:00
|
|
|
if (client_name.empty())
|
|
|
|
client_name = DBMS_NAME;
|
|
|
|
else
|
|
|
|
client_name = (DBMS_NAME " ") + client_name;
|
2020-04-15 01:58:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-24 21:40:39 +00:00
|
|
|
void ClientInfo::fillOSUserHostNameAndVersionInfo()
|
|
|
|
{
|
2016-10-24 22:30:51 +00:00
|
|
|
os_user.resize(256, '\0');
|
2018-09-02 03:00:04 +00:00
|
|
|
if (0 == getlogin_r(os_user.data(), os_user.size() - 1))
|
|
|
|
os_user.resize(strlen(os_user.c_str()));
|
2016-10-24 21:40:39 +00:00
|
|
|
else
|
|
|
|
os_user.clear(); /// Don't mind if we cannot determine user login.
|
|
|
|
|
|
|
|
client_hostname = getFQDNOrHostName();
|
|
|
|
|
|
|
|
client_version_major = DBMS_VERSION_MAJOR;
|
|
|
|
client_version_minor = DBMS_VERSION_MINOR;
|
2018-07-31 21:36:18 +00:00
|
|
|
client_version_patch = DBMS_VERSION_PATCH;
|
2020-09-17 12:15:05 +00:00
|
|
|
client_tcp_protocol_version = DBMS_TCP_PROTOCOL_VERSION;
|
2016-10-24 21:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|