Skip to content

clickhouse-benchmark: do not ping the connection before every query - #110054

Closed
alexey-milovidov wants to merge 4 commits into
masterfrom
clickhouse-benchmark-drop-force-connected
Closed

alexey-milovidov wants to merge 4 commits into
masterfrom
clickhouse-benchmark-drop-force-connected

Conversation

@alexey-milovidov

Copy link
Copy Markdown
Member

clickhouse-benchmark acquired its connection from the pool with the single-argument ConnectionPool::get, which unconditionally calls forceConnected and therefore sends a Ping and waits for Pong before every query.

For a hot, already-established pooled connection this Ping-Pong round trip is pure overhead. With --client-side-time it roughly doubles the reported latency of small queries: the stopwatch is started before the connection is acquired, so it measures the ping round trip in addition to the query round trip (~2x RTT). The default server-side timing (progress.elapsed_ns) hides it because that number is the server's own processing time, so the ping only becomes visible under --client-side-time.

This changes the benchmark to use the force_connected = false overload, so the connection is reused as-is. A stale connection is surfaced by the query itself failing, which is handled like any other query error - matching how the failover path (ConnectionPoolWithFailover) already works. The benchmark keeps connections hot so they do not idle out between queries, and --reconnect still forces periodic reconnection when requested.

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

clickhouse-benchmark no longer sends a Ping before every query, which previously roughly doubled the latency reported with --client-side-time for small queries.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

`clickhouse-benchmark` fetched its connection from the pool with the
single-argument `ConnectionPool::get`, which unconditionally calls
`forceConnected` and therefore sends a `Ping` and waits for `Pong` before
every query. For a hot, already-established pooled connection this
Ping-Pong round trip is pure overhead: with `--client-side-time` it
roughly doubles the reported latency of small queries, because the
stopwatch (started before the connection is acquired) measures the ping
round trip in addition to the query round trip.

Use the `force_connected = false` overload instead, so the connection is
reused as-is. A stale connection is now surfaced by the query itself
failing, which is handled the same way as any other query error - this
matches how the failover path (`ConnectionPoolWithFailover`) already
works. Since the benchmark keeps connections hot, they do not idle out
between queries, and `--reconnect` still forces periodic reconnection
when requested.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8d4622e]

Summary: ❌

job_name test_name status info comment
Integration tests (amd_tsan, 3/6) FAIL
test_replicated_database/test.py::test_replicated_table_structure_alter FAIL cidb, issue

AI Review

Summary

This PR removes the pre-query Ping from clickhouse-benchmark by switching the pool acquisition to get(..., /*force_connected=*/false). That does narrow the client-side timing path for hot connections, but it also drops the benchmark's stale-socket recovery path, so an idle pooled connection now fails the next query and, with the default options, aborts the whole run. The reliability regression is still present in the current code, so the verdict remains request changes.

Findings

⚠️ Majors

  • [programs/benchmark/Benchmark.cpp:695-698,728-732] [dismissed by author -- https://github.com/clickhouse-benchmark: do not ping the connection before every query #110054#discussion_r3562110310] The benchmark used to guarantee that a pooled entry was live before a query started, because ConnectionPool::get called forceConnected, which reconnects a stale socket after a failed Ping (src/Client/Connection.cpp:743-754). After this change, an idle-but-dead pooled socket is surfaced as a query failure instead. That is user-visible here because the default error path immediately sets shutdown = true and rethrows on the first failure, so a proxy/server keep-alive timeout or a low-concurrency multi-host run can now terminate the benchmark even though the previous code would have revalidated the socket transparently.
    Suggested fix: keep the liveness check outside the measured interval instead of deleting it, for example by acquiring the pooled entry before starting Stopwatch watch;, or by adding a targeted reconnect/retry path for network failures on reused sockets.
Tests
  • ⚠️ This needs a focused regression test or a reproducible integration scenario that leaves a pooled connection idle past a keep-alive timeout and verifies that the next benchmark query still executes instead of aborting the run. The current CI stays green because its normal path does not exercise stale pooled sockets.
Final Verdict

Status: ⚠️ Request changes

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jul 10, 2026
/// Ping-Pong round trip only doubles the measured latency of small queries. A stale
/// connection is handled by the query itself failing, which is treated as a normal error.
ConnectionPool::Entry entry = connections[connection_index]->get(
ConnectionTimeouts::getTCPTimeoutsWithoutFailover(settings), settings, /*force_connected=*/false);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes more than the timing window. ConnectionPool::get used to guarantee a live pooled entry by pinging or reconnecting stale sockets, while Connection::sendQuery only reconnects when the entry is already marked disconnected. With force_connected = false, a half-closed pooled socket now turns into a failed benchmark query instead of a transparent reconnect.

That is user-visible here because the default error path in programs/benchmark/Benchmark.cpp:695-698 aborts the whole benchmark on the first failure. In long-lived or multi-host / low-concurrency runs, a pool entry can sit idle behind server or proxy keep-alive timeouts and now kill the benchmark even though the previous code would have revalidated it.

If the goal is only to stop charging the Ping against --client-side-time, I think the safer fix is to keep the old ConnectionPool::get(timeouts) behavior and move Stopwatch watch; below connection acquisition instead. That narrows the measured interval without weakening stale-connection handling.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. We should eliminate ping from connections.

@clickhouse-gh

clickhouse-gh Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 80.10% 86.00% +5.90%
Functions 91.30% 91.80% +0.50%
Branches 72.10% 78.10% +6.00%

Changed lines: Changed C/C++ lines covered: 3/4 (75.00%) · Uncovered code

Full report · Diff report

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Closing this as superseded by #110068 ("remove-force-connected-ping"), which is already merged into master.

#110068 achieves the same goal — no Ping before every query — but at the ConnectionPool / Connection::forceConnected level, so it applies to every pooled-connection user rather than just clickhouse-benchmark. Crucially, it keeps stale-socket recovery: forceConnected now detects a dead pooled socket with a single non-blocking poll(0) (no round trip) and reconnects, and a failure the poll cannot see is recovered when the connection is first used. That directly resolves the stale-connection concern raised on this PR.

As a result, programs/benchmark/Benchmark.cpp in master already benefits from the no-ping behavior through its existing get(timeouts) call, and this PR's get(..., /*force_connected=*/false) overload no longer exists (the force_connected parameter was removed by #110068), so the change would not compile against current master.

Related: #110068

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants