Repository navigation
feat: support system.connections table for client connections metadata - #99702
Conversation
This table manages the active and idle (in case of TCP) connections from the client Signed-off-by: Kaviraj <[email protected]>
This add connection registry and it's implementation. Also get invoked in TCP and HTTP handlers for every new connections. Signed-off-by: Kaviraj <[email protected]>
This is part of in-memory storage similar to system.process tables. Signed-off-by: Kaviraj <[email protected]>
Signed-off-by: Kaviraj <[email protected]>
Signed-off-by: Kaviraj <[email protected]>
|
Workflow [PR], commit [da6bd13] Summary: ✅
AI ReviewSummaryPR #99702 adds opt-in Final VerdictStatus: ✅ Approve |
Signed-off-by: Kaviraj <[email protected]>
Also added tests to lock the behavior from regression Signed-off-by: Kaviraj <[email protected]>
Signed-off-by: Kaviraj <[email protected]>
use `getCurrenetQueryID()` to avoid setting empty query_id Signed-off-by: Kaviraj <[email protected]>
Signed-off-by: Kaviraj <[email protected]>
Changes: 1. when used with HTTP, it was not extracting the client_name properly. Now it does 2. connected_time makes no sense with http (unlike TCP). Only last_query_time matters for HTTP Signed-off-by: Kaviraj <[email protected]>
Signed-off-by: Kaviraj <[email protected]>
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 133/141 (94.33%) | Lost baseline coverage: none · Uncovered code |
|
Dear @jkartseva, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
jkartseva
left a comment
There was a problem hiding this comment.
Most importantly, please fix the HTTP semantics to support keep-alive sockets and idle intervals.
Conflict in `src/Core/ServerSettings.cpp`: both sides appended a new setting declaration at the same position in the settings list. Kept both `collect_connection_metrics` and `user_profile_events_per_cpu`.
|
Dear @jkartseva, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
|
@jkartseva Thanks for the review. Addressed the feedback and remarks. Ready for another look :) |
jkartseva
left a comment
There was a problem hiding this comment.
Mostly LGTM, I have a couple of suggestions, PTAL at the comments.
| )", 0) \ | ||
| DECLARE(UInt64, total_memory_profiler_sample_min_allocation_size, 0, R"(Collect random allocations of size greater or equal than specified value with probability equal to `total_memory_profiler_sample_probability`. 0 means disabled. You may want to set 'max_untracked_memory' to 0 to make this threshold to work as expected.)", 0) \ | ||
| DECLARE(UInt64, total_memory_profiler_sample_max_allocation_size, 0, R"(Collect random allocations of size less or equal than specified value with probability equal to `total_memory_profiler_sample_probability`. 0 means disabled. You may want to set 'max_untracked_memory' to 0 to make this threshold to work as expected.)", 0) \ | ||
| DECLARE(Bool, collect_connection_metrics, false, R"(Adds the `system.connections` table. This table shows the connections of the native TCP and HTTP query interfaces. It does not show connections of MySQL, PostgreSQL, gRPC, or other protocols. |
There was a problem hiding this comment.
Currently, SYSTEM RELOAD CONFIG does not reload this setting: it keeps the value of the previous <collect_connection_metrics>true</collect_connection_metrics> until the server is restarted. Let's fix this.
Could you please also add a test:
SYSTEM RELOAD CONFIG;
SELECT count() > 0 FROM system.connections WHERE query_id = currentQueryID();
``
There was a problem hiding this comment.
Good catch. Addressed 👍
|
|
||
| return ColumnsDescription | ||
| { | ||
| {"connection_id", std::make_shared<DataTypeUInt64>(), "Unique identifier of the connection. A TCP connection gets the identifier after authentication. An HTTP connection gets the identifier on its first query."}, |
There was a problem hiding this comment.
Wdyt about switching from sequential IDs to a UUID, which can be derived
{"connection_id", std::make_shared(), "..."},
A pseudo-random UUID can be easily assigned through the call
const UUID id = UUIDHelpers::generateV4();
It follows an established pattern in CH.
In the Cloud, we often use the clusterAllReplicas query, and connections from different pods and the same id will collide.
There was a problem hiding this comment.
I agree. This makes is super simple in cluster mode 👍 addressed
| {"client_version_major", std::make_shared<DataTypeUInt64>(), "Major version of the client from the TCP handshake. 0 for HTTP connections."}, | ||
| {"client_version_minor", std::make_shared<DataTypeUInt64>(), "Minor version of the client from the TCP handshake. 0 for HTTP connections."}, | ||
| {"client_version_patch", std::make_shared<DataTypeUInt64>(), "Patch version of the client from the TCP handshake. 0 for HTTP connections."}, | ||
| {"connected_time", std::make_shared<DataTypeDateTime>(), "Time when the connection started. For a TCP connection, this is the time after authentication."}, |
There was a problem hiding this comment.
Please expand the documentation part explaining that connected_time means different things per protocol.
HTTP records it at socket accept (HTTPServerConnection.cpp:50, before any request), while TCP records std::time(nullptr) at registration in runImpl, i.e. after authentication, so accept, handshake and auth time are excluded.
There was a problem hiding this comment.
Good point. I actually thought about a bit after adding runtime reload compatibility. And may be we can make it consistent across both HTTP and TCP instead of documenting the difference.
Before:
Http recorded the time when the server started to handle the socket. TCP recorded the time after the handshake and authentication. So same column connected_time measured two different things.
Now:
TCP also records the time when the handler is created for the socket. This is same moment as for HTTP. Why? the lazy TCP registration before made the old registration time even less meaningful. A connection registered after a switch at runtime would have shown that later time as when it "connected"?
WDYT?
Basically two minor changes 1. Make the setting `collect_connection_metrics` reloadable at the runtime (via SYSTEM RELOAD CONFIG for example). To make this possible, now we check for registeration before each query during HTTP. Common how it's done in other places 2. Make connection_Id UUID instead of Uint64. To avoid complexities and conflict during cluster mode. Signed-off-by: Kaviraj <[email protected]>
set log level to fatal similar to other tests that does system settings reload Signed-off-by: Kaviraj <[email protected]>
Signed-off-by: Kaviraj <[email protected]>
If user's username is used for checking the permission, then there is subtle bug when user is renamed. And it can be security violation. Bot review identified that. This patch addressed it by using user_id (that doesn't change even when renaming) Signed-off-by: Kaviraj <[email protected]>
|
FYI: All the failed test cases are related to |
jkartseva
left a comment
There was a problem hiding this comment.
Looks good to me, with one minor comment (see below). Thanks for following up, let's merge.
I also see this particular tests are flaky even in master branch ?
Yes, we have tests that flake on master 😞
| {"client_version_major", std::make_shared<DataTypeUInt64>(), "Major version of the client from the TCP handshake. 0 for HTTP connections."}, | ||
| {"client_version_minor", std::make_shared<DataTypeUInt64>(), "Minor version of the client from the TCP handshake. 0 for HTTP connections."}, | ||
| {"client_version_patch", std::make_shared<DataTypeUInt64>(), "Patch version of the client from the TCP handshake. 0 for HTTP connections."}, | ||
| {"connected_time", std::make_shared<DataTypeDateTime>(), "Time when the connection started. For a TCP connection, this is the time after authentication."}, |
Signed-off-by: Kaviraj <[email protected]>
This is needed as there can be following case where one user may see the connections of other users (HTTP case) one HTTP keep-alive socket, execute a query as alice with `User-Agent: alice-agent`, disable `collect_connection_metrics` via SYSTEM RELOAD CONFIG, execute a query as bob with User-Agent: bob-agent, then enable collection again. The row still reports alice / alice-agent; Alice can see it without SHOW CONNECTIONS, while Bob cannot. It stays stale until another query on that socket. Re-enabling during a running request similarly exposes the retained idle state Signed-off-by: Kaviraj <[email protected]>
Signed-off-by: Kaviraj <[email protected]>
7f17283
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
new
system.connectionsto have visibility on active/idle clients connections to ClickHouse serverDocumentation entry for user-facing changes
Primarily driven by ClickHouse cloud use cases (but I can see it's worth having it for other OSS use cases as well). Currently there is no clean way of knowing existing "client" connections to ClickHouse server.
The hardway is to parse query_logs, proxy_logs and metrics to get the idea. but that's fragile.
Plus particularly in clickhouse cloud, people often wonder why their instance is not going idle even though they think they are not actively using the ClickHouse instance. Now we can have clear picture.
This tries to address the open issue #60670
I also made sure it works with proxies in-between. So that
system.connectionsshows "real" client info as much as possible instead of just random proxy info in-between (we have run the clickhouse server withtcp_with_proxy_portandauth_use_forwarded_address=1configs. And proxy_protocol has to be enabled in the proxy (to forward the to proxy_port of clickhouse server)This the whole table columns and it's types currently supported.
It also works with
Workflow [PR]
Sync PR [sync-upstream/pr/99702]
Version info
26.11.1.360-master(included in26.11and later)