Skip to content

Ping unresponsive cluster replicas - #2912

Open
ianton-ru wants to merge 11 commits into
antalya-26.8from
bugfix/antalya-26.8/ping-unresponsive-cluster-replicas
Open

ianton-ru wants to merge 11 commits into
antalya-26.8from
bugfix/antalya-26.8/ping-unresponsive-cluster-replicas

Conversation

@ianton-ru

Copy link
Copy Markdown

Solved #2896

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Ping unresponsive cluster replicas

Documentation entry for user-facing changes

PR ClickHouse#110068 had removed ping for replicas before each query.
But in Swarm cinfiguration is expected that Swarm node can be donw in any moment.
PR returns ping back for cluster requests.

CI/CD Options

Exclude tests:

  • Fast test
  • Integration Tests
  • Stateless tests
  • Stateful tests
  • Unit tests
  • Performance tests
  • Aarch64 tests
  • All with ASAN
  • All with TSAN
  • All with MSAN
  • All with UBSAN
  • All with Coverage
  • All Regression
  • Disable CI Cache

Regression jobs to run:

  • Fast suites (mostly <1h)
  • Aggregate Functions (2h)
  • Alter (1.5h)
  • Benchmark (30m)
  • CAS (content-addressed storage; Antalya only)
  • ClickHouse Keeper (1h)
  • Iceberg (2h)
  • LDAP (1h)
  • OAuth (5m)
  • Parquet (1.5h)
  • RBAC (1.5h)
  • SSL Server (1h)
  • S3 (2h)
  • S3 Export (2h)
  • Swarms (30m)
  • Tiered Storage (2h)

ianton-ru and others added 4 commits November 18, 2026 12:06
Since ClickHouse#110068 a pooled
connection is validated with a zero-timeout poll and not with a `Ping`.
The poll sees a closed socket, but not a server that stopped without
closing it (for example, `SIGSTOP`). For a cluster table function such as
`fileCluster`, `s3Cluster` or a swarm read, such a replica stays in the
pipeline and the query waits for `receive_timeout` (300 seconds by default)
before it fails with `SOCKET_TIMEOUT`. Before that PR the `Ping` skipped
the replica after `sync_request_timeout`.

`ReadFromCluster::initializePipeline` now passes `ping_pooled_connection`
to `getMany` for every shard that is not local. A local shard is excluded
because `ReplicasAsShards` gives this server's own address a real TCP
pool, and pinging that address does not reveal a dead peer.

The ping is limited by `sync_request_timeout`. A failed ping raises
`SOCKET_TIMEOUT` inside the synchronous `ConnectionEstablisher::run`, and
that path already drops the socket and reconnects once. A connection that
is not established yet is still checked by the handshake, so no extra
round trip is added.

The asynchronous path (hedged connections and `tryGetEntry` with an async
callback) keeps the poll, because a blocking ping would stall the fiber.
Other callers are unchanged and keep the poll-based validation.

Adds the `unresponsive_pooled_connection` failpoint, which makes
`Connection::ping` fail, and the stateless test
`05812_ping_pooled_connection_on_cluster_read`.

Issue: #2896
Co-authored-by: Cursor <[email protected]>
`Connection::ping` is private, so `ConnectionEstablisher::run` failed to
compile in b3f14c5. Call `Connection::checkConnected`, the public
wrapper around `ping`. The branch that calls it is already known to be
connected, so the call only runs the ping.

Co-authored-by: Cursor <[email protected]>
A cluster read pings a pooled connection to a remote shard before using it.
If the peer kept the socket open and sent no `Pong` within
`sync_request_timeout`, the establisher reconnected once and waited up to
`handshake_timeout_ms` for a `Hello` that a stopped process never sends.

`Connection::checkConnectedThrowOnTimeout` lets a `SOCKET_TIMEOUT` out of
`Connection::ping`. `ConnectionEstablisher::run` then skips the replica and
does not reconnect. A socket the server has already closed still returns
false from `ping`, so it still reconnects once. `checkConnected` keeps its
old contract for the client resync path.

Adds the `pooled_connection_ping_timeout` failpoint and two checks to
`05812_ping_pooled_connection_on_cluster_read`: a timed-out ping does not
increase `DistributedConnectionReconnectCount`, and it does increase
`DistributedConnectionFailTry`.

Related: #2896
Co-authored-by: Cursor <[email protected]>
A pooled remote connection that does not answer `Ping` within
`sync_request_timeout` was skipped. A live server behind a dropped flow was
then dropped from the cluster read, and the query could fail with
`ALL_CONNECTION_TRIES_FAILED` even though a new connection would succeed.

The establisher now reconnects once after a `SOCKET_TIMEOUT` from the ping,
the same way it already does for a dead socket. The handshake of that retry
is capped by `sync_request_timeout`, so a stopped server does not wait out
`handshake_timeout_ms`. The cap only shortens `handshake_timeout` on that
attempt and never raises it.

The `pooled_connection_ping_timeout` failpoint test now expects
`DistributedConnectionReconnectCount` to increase, and both remote and local
shards to be usable after the reconnect.

Related: #2896
Co-authored-by: Cursor <[email protected]>
@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Workflow [PR], commit [172c27d]

@ianton-ru ianton-ru added antalya bugfix port-antalya PRs to be ported to all new Antalya releases antalya-26.8 labels Nov 18, 2026
@ianton-ru

Copy link
Copy Markdown
Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-19T11:57:30.075696Z 8be0a0f Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a839c6dc57

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/Client/ConnectionEstablisher.cpp Outdated
Comment on lines +93 to +94
attempt_timeouts.handshake_timeout = ConnectionTimeouts::saturate(
attempt_timeouts.handshake_timeout, attempt_timeouts.sync_request_timeout);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Cap zero-valued handshake timeouts

When a cluster read uses handshake_timeout_ms=0 and a pooled peer times out on Ping, saturate(0, sync_request_timeout) leaves the retry's handshake timeout at zero. Connection::connect then installs that zero value as the socket send/receive timeout, so a paused peer that accepts TCP but never sends Hello can block indefinitely instead of being skipped after sync_request_timeout, defeating the new bounded-reconnect behavior. Treat a zero handshake timeout as unbounded here and replace it with sync_request_timeout; otherwise take the minimum.

Useful? React with 👍 / 👎.

ianton-ru and others added 3 commits November 19, 2026 12:44
The reconnect after a `Ping` timeout caps the handshake timeout by
`sync_request_timeout` with `ConnectionTimeouts::saturate`. `saturate`
keeps any value smaller than the limit, so a zero `handshake_timeout_ms`
stayed zero. A zero socket timeout never fires, so a peer that accepts
TCP and never sends `Hello` could block the retry indefinitely instead
of stopping at `sync_request_timeout`.

Treat a zero handshake timeout as no limit and replace it with
`sync_request_timeout`. Any other value is still capped to the minimum
of the two.

Related: #2912
Co-authored-by: Cursor <[email protected]>
`TimeoutSetter` with `limit_max_timeout` keeps the current socket timeout
when it is not longer than the requested one. A zero socket timeout means
no limit, but it is not longer than any finite value, so it was kept.
`Connection::ping` uses this setter with `sync_request_timeout`, so a
pooled connection with `receive_timeout = 0` could wait for a `Pong`
forever when the peer accepted the connection and never answered.

Treat a zero send or receive timeout as no limit and install the requested
timeout in its place. A finite timeout that is already shorter is still
kept. The destructor restores the original zero timeout, so the query
itself stays unlimited.

Related: #2912
Co-authored-by: Cursor <[email protected]>
The comment in `Connection::ping` said a timeout skips the replica. The
cluster-read caller does not skip it on the first timeout: it reconnects
once with the handshake capped by `sync_request_timeout`, and the replica
is skipped only if that reconnect fails.

Describe the actual flow. No code change.

Related: #2912
Co-authored-by: Cursor <[email protected]>
@ianton-ru

Copy link
Copy Markdown
Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8be0a0f986

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

/// closing the socket otherwise stays in the pipeline and the query waits for
/// `receive_timeout`. No `Pong` within `sync_request_timeout` drops that socket and opens
/// one new connection. The handshake on that connection is capped by `sync_request_timeout`.
const bool ping_pooled_connection = !shard_info.isLocal();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Avoid serial preflight pings across remote shards

For a cluster function with many remote shards and pooled connections, this loop invokes getMany synchronously for each shard, and enabling ping_pooled_connection makes every invocation complete a Ping/Pong round trip before the next shard is considered. Healthy queries therefore pay the sum of all shard RTTs before the pipeline can start, while several slow peers can each consume the five-second sync_request_timeout; previously the resulting RemoteSource pipes performed their network work concurrently. Ping or acquire these connections concurrently so startup latency is bounded by the slowest shard rather than multiplied by the shard count.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I think it is not critical.

@arthurpassos

Copy link
Copy Markdown
Collaborator

PR ClickHouse#110068 had removed ping for replicas before each query.
But in Swarm cinfiguration is expected that Swarm node can be donw in any moment.

This is a very annoying type of bug we are starting to face everywhere. Upstream features our antalya features depend on keep changing and silently break..

@arthurpassos

Copy link
Copy Markdown
Collaborator

As discussed offline, AI review found:

Medium: the fix does not cover every swarm path
File/function: StorageDistributed.cpp, the getMany call in distributedWriteFromClusterStorage (about line 1951 on the PR branch).

`StorageDistributed::distributedWriteFromClusterStorage` takes pooled
connections to the destination cluster without a ping. A cluster read
already pings remote pooled connections before reuse, but this second
caller did not. A peer that stopped without closing its socket was
therefore not detected, and the query waited for `receive_timeout`.

Pass `ping_pooled_connection=true` to that `getMany` call, so this path
uses the same ping and single reconnect as the cluster read. The path is
reached with `parallel_distributed_insert_select` set to any non-zero
value, and the default is 2.

Add the test `05813_distributed_insert_select_ping_pooled_connection`,
which uses the `unresponsive_pooled_connection` and
`pooled_connection_ping_timeout` failpoints, as `05812` does.

Related: #2912
Co-authored-by: Cursor <[email protected]>
@ianton-ru

Copy link
Copy Markdown
Author

@arthurpassos Fixed

@arthurpassos

Copy link
Copy Markdown
Collaborator

@arthurpassos Fixed

Maybe you forgot to push the commit?

@ianton-ru

ianton-ru commented Nov 23, 2026 •

Copy link
Copy Markdown
Author

Ups
9ef54ef

arthurpassos
arthurpassos previously approved these changes Nov 23, 2026
@Selfeer

Selfeer commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

PR 2912 — confirmed issues

1. The single reconnect can be spent on another stale pooled connection

Severity: Medium

ConnectionEstablisher::run allows one retry after a pooled Ping fails. That retry calls ConnectionPool::getUnchecked again, and PoolBase::get returns the first idle object in the pool. It does not force a new TCP connection.

If another query releases an earlier, still-connected socket while the first Ping is blocked, the retry receives that socket. It is still connected, so it is pinged instead of reconnected. tries == 0 is already false, so a failed second Ping ends the attempt. The shard is skipped, or the query fails with ALL_CONNECTION_TRIES_FAILED, even though connect on a new socket would succeed.

This is the case the reconnect was added for: the old flow is dead and the server accepts a new connection. It shows up when the pool holds more than one connection to the same host (the default distributed_connections_pool_size is 1024) and two queries use that pool at the same time.

Steps to reproduce:

  1. Use a cluster with one remote shard and run two overlapping reads that take pooled connections to that host (fileCluster / s3Cluster, or INSERT SELECT with parallel_distributed_insert_select).
  2. Leave connection A checked out by query 1. Let query 2 check out a later pooled connection B.
  3. Make Ping time out on an established socket while a new TCP connection would still complete. SYSTEM ENABLE FAILPOINT pooled_connection_ping_timeout does this, or drop packets on established flows and allow new SYNs.
  4. While query 2 is blocked in Ping on B, finish query 1 so A returns to the pool still connected.
  5. Query 2’s retry calls getUnchecked, receives A, pings A, and stops. The shard is omitted or the query fails. A later connection attempt with the failpoint disabled succeeds.

2. A frozen cluster is still probed one shard at a time

Severity: Medium

ReadFromCluster::initializePipeline calls getMany inside a plain for loop over shards. With ping_pooled_connection, each remote shard completes a Ping before the next shard starts. A Ping that reaches sync_request_timeout then opens one new connection whose Hello wait is capped by the same timeout.

ConnectionTimeouts::getTCPTimeoutsWithFailover never copies a setting into sync_request_timeout, so the server-side value stays at DBMS_DEFAULT_SYNC_REQUEST_TIMEOUT_SEC (5 seconds). handshake_timeout_ms defaults to 10 seconds and is capped down to those 5 seconds on the retry. A stopped process still accepts TCP (the kernel queues the connection), so the retry waits out the capped handshake instead of failing at connect time.

Each frozen remote shard therefore costs about 10 seconds, and the waits add up. Two shards are already ~20 seconds. A few dozen shards reach the same multi-minute stall this change is meant to remove (receive_timeout, 300 seconds). Healthy shards add their round trips on top of that, still in series, before the pipeline can start.

INSERT INTO Distributed SELECT FROM <cluster storage> does this twice when every destination shard fails. distributedWriteFromClusterStorage returns an empty QueryPipeline. InterpreterInsertQuery treats an uninitialized pipeline as “this strategy does not apply” and builds the ordinary INSERT SELECT. That plan reads through ReadFromCluster, which pings the same shards again from the start.

Steps to reproduce:

  1. Configure a cluster of N remote replicas (no local replica) and warm the pools with one successful fileCluster / s3Cluster / swarm read so each shard has a connected socket.
  2. kill -STOP every replica process. Leave the initiator running.
  3. Run the same cluster read again.
  4. Time until ALL_CONNECTION_TRIES_FAILED. It grows by about 10 seconds per stopped replica, not by one sync_request_timeout for the whole cluster.
  5. For the second pass, use INSERT INTO dist SELECT FROM s3Cluster(...) (or fileCluster) with parallel_distributed_insert_select = 2, same freeze, and no shard that answers. The insert path pings every shard, builds no pipeline, then the fallback SELECT pings every shard again before the query fails.

On a soft failure of the first attempt, `ConnectionEstablisher::run`
disconnected the entry, released it to the pool, and asked the pool again
for the retry. `PoolBase::get` returns the first idle object, so the retry
could receive another still-connected socket that a concurrent query had
just released. That socket was pinged or polled instead of reconnected, and
the single retry was spent on it even though a new connection to the same
server would succeed.

Keep the failed entry checked out for the retry and reconnect it. The entry
is released to the pool as before when no retry is allowed.

This applies to every first-attempt retry, including the stale-connection
retry for the table status request.

Related: #2912
Co-authored-by: Cursor <[email protected]>
@ianton-ru

Copy link
Copy Markdown
Author

@Selfeer
Add a small fix for the first issue.
Second finding is a known thing, the same as #2912 (comment). I think this is not a critical,

@Selfeer Selfeer added the verified Approved for release label Nov 24, 2026

This branch has not been deployed

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

Labels

antalya antalya-26.8 antalya-26.8.39.20001 bugfix port-antalya PRs to be ported to all new Antalya releases verified Approved for release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants