Repository navigation
Make test_cluster_all_replicas_query deterministic by gating on a pause-effective probe - #104167
Conversation
…heck The test previously asserted that pausing `node1` would cause `clusterAllReplicas(test_cluster, default.local_table)` to fail with `ALL_CONNECTION_TRIES_FAILED`. `cluster.pause_container` uses Docker's cgroup freezer, which takes effect quickly but not synchronously with in-flight TCP traffic, so the SELECT can race the freezer: `node1` can complete the TCP handshake and the tiny server greeting before being suspended, every per-replica shard succeeds, and the query returns a clean `count()` with `error == ''`. The assertion then fails spuriously. This is the chronic flake tracked under issues ClickHouse#103819 and ClickHouse#103820, which surfaced after the `857 ms` formatting fix in PR ClickHouse#103780. Fix: poll a fast probe `SELECT 1 FROM remote('node1:9000', system.one)` inside the pause context until it raises `QueryRuntimeException`. Once that probe fails, connections to `node1` are demonstrably blocked and the actual `clusterAllReplicas` test can run deterministically. The 30-second deadline accommodates slow CI runners; in practice the probe fails on the first or second iteration. The original assertions (`ALL_CONNECTION_TRIES_FAILED in error`, `857 ms in error`) are unchanged — the fix only removes the timing race.
|
Pre-PR validation gate (test-only fix)
Session: |
|
cc @alexey-milovidov — could you review this? It deflakes |
|
Workflow [PR], commit [e66dc2d] Summary: ✅ AI ReviewSummaryThis PR makes ClickHouse Rules
Final VerdictStatus: ✅ Approve |
| node3.query("SYSTEM DROP DNS CACHE") | ||
| error = "" | ||
| with cluster.pause_container("node1"): | ||
| # `cluster.pause_container` uses Docker's cgroup freezer to suspend |
There was a problem hiding this comment.
Shouldn't we fix the function pause_container globally? So that it actually waits for the freeze?
Per @alexey-milovidov's review on PR ClickHouse#104167: instead of a per-test polling loop in `test_cluster_all_replicas_query`, fix `cluster.pause_container` itself so every caller gets a deterministic freeze. After `docker compose pause` returns, Docker's cgroup freezer suspends user-space tasks asynchronously with respect to in-flight TCP traffic. The kernel can still complete the TCP three-way handshake for fresh connections, and a short application greeting that is already buffered in the socket can flow back to the client before the freeze fully blocks the server's accept/read/write loop. Tests that assert on connection failure under `pause_container` see the very first probe succeed and flake (issues `ClickHouse#103819`, `ClickHouse#103820`). The new helper `_wait_for_pause_effective` polls a sibling ClickHouse node with a tight `remote(<paused>:9000, system.one)` probe until it raises `QueryRuntimeException` — the moment fresh ClickHouse handshakes to the paused container can no longer complete. Both `pause_container` and `pause_container_using_signal` now block on this probe by default; callers can opt out with `wait_for_paused=False`. For non-ClickHouse containers (Kafka, MongoDB, etc.) the helper is a no-op because we cannot speak the application protocol from a sibling. The current Kafka callers do not assert on freeze timing — they wait for log lines emitted well after the freeze takes effect — so the absence of a probe is benign. Reverts the per-test polling loop added earlier in this PR; the global helper subsumes it. Other ClickHouse-pause callers (`test_distributed_load_balancing`, `test_hedged_requests`, `test_distributed_frozen_replica::test_distributed_query`) automatically inherit the same guarantee. Co-Authored-By: Claude Opus 4.7 <[email protected]>
|
@alexey-milovidov — good call. Moved the probe into The new helper
Both Reverted the per-test polling loop in Pre-PR validation gatea) Deterministic repro? Yes — same probe logic as the per-test version which was already passing CI on this PR (Integration tests amd_tsan, amd_msan, amd_asan_ubsan, amd_llvm_coverage, arm_binary all green on the previous push). b) Root cause? c) Fix matches root cause? Yes — the helper waits until a fresh ClickHouse handshake to the paused container fails, which is the precise property every existing assertion depends on. No widening of bounds, no d) Test intent preserved? Yes — e) Both directions demonstrated? The previous push (with the per-test version of the same probe) passed all relevant CI shards. The diff is purely a code-move from the test into the helper, so both directions transfer. |
| used_signal = True | ||
|
|
||
| if wait_for_paused: | ||
| self._wait_for_pause_effective(instance_name, wait_timeout) |
There was a problem hiding this comment.
pause_container can leave the container permanently paused when _wait_for_pause_effective times out or throws.
Right now the unpause logic is only in the finally around yield, so an exception before yield skips cleanup. In that failure path the context manager exits with node1 still paused, which can cascade into unrelated test failures in the same suite.
Please move _wait_for_pause_effective inside the same try/finally that guarantees unpause (and do the same for pause_container_using_signal).
The previous version of `pause_container` and `pause_container_using_signal` called `_wait_for_pause_effective` outside the `try` that wraps `yield`. If the probe timed out and raised, the container was paused but `unpause()` was never reached, leaving the container permanently paused and cascading into unrelated test failures in the same suite. Move `_wait_for_pause_effective` inside the same `try` whose `finally` guarantees the matching unpause call. Cleanup now runs in every exit path: probe success, probe timeout, or caller exception. Same fix applied to both `pause_container` (docker compose pause + SIGSTOP fallback) and `pause_container_using_signal` (SIGSTOP-only). When the docker pause itself fails AND the SIGSTOP fallback also fails, no container is paused, so the existing "no inner try" behavior is correct and unchanged.
|
Good catch — fixed in Cleanup now runs on every exit path:
The only path with no inner try is when both Session: cron:clickhouse-ci-task-worker:20260506-151500 |
alexey-milovidov
left a comment
There was a problem hiding this comment.
This is smart. Hopefully, it is also true.
This will not work as expected when the container uses an alternative port, but the behavior will be ok still.
Per @alexey-milovidov's review on PR #104167: instead of a per-test polling loop in `test_cluster_all_replicas_query`, fix `cluster.pause_container` itself so every caller gets a deterministic freeze. After `docker compose pause` returns, Docker's cgroup freezer suspends user-space tasks asynchronously with respect to in-flight TCP traffic. The kernel can still complete the TCP three-way handshake for fresh connections, and a short application greeting that is already buffered in the socket can flow back to the client before the freeze fully blocks the server's accept/read/write loop. Tests that assert on connection failure under `pause_container` see the very first probe succeed and flake (issues `#103819`, `#103820`). The new helper `_wait_for_pause_effective` polls a sibling ClickHouse node with a tight `remote(<paused>:9000, system.one)` probe until it raises `QueryRuntimeException` — the moment fresh ClickHouse handshakes to the paused container can no longer complete. Both `pause_container` and `pause_container_using_signal` now block on this probe by default; callers can opt out with `wait_for_paused=False`. For non-ClickHouse containers (Kafka, MongoDB, etc.) the helper is a no-op because we cannot speak the application protocol from a sibling. The current Kafka callers do not assert on freeze timing — they wait for log lines emitted well after the freeze takes effect — so the absence of a probe is benign. Reverts the per-test polling loop added earlier in this PR; the global helper subsumes it. Other ClickHouse-pause callers (`test_distributed_load_balancing`, `test_hedged_requests`, `test_distributed_frozen_replica::test_distributed_query`) automatically inherit the same guarantee. Co-Authored-By: Claude Opus 4.7 <[email protected]>
The sibling-node probe added in ClickHouse#104167 still let `test_distributed_frozen_replica` flake on slow sanitizer CI shards even after the wait_timeout was raised to 90s in this PR's first commit. CIDB shows ~3-5 PR hits/day and recurrent master hits (2026-05-12 and 2026-05-13) on `Integration tests (amd_msan, 1/6)` and `Integration tests (amd_asan_ubsan, db disk, old analyzer, 1/6)`, all with the same exception: Exception: pause_container('node1') did not become observably effective within 30.0s — connections to node1 from 'node3' still succeed Root cause: the prober runs `SELECT 1 FROM remote(<paused>:9000, system.one) SETTINGS handshake_timeout_ms=200, ...` on a sibling ClickHouse server. The sibling resolves `remote()` through its internal `ConnectionPoolFactory`, which keys pools by `(host, port, user, password, ...)` only — query timeouts are NOT part of the key. A cached native-protocol entry to node1 is therefore reused on every probe iteration. The reused socket is still alive at the kernel level (`docker compose pause` only freezes user-space tasks), so `Connection::forceConnected` skips the connect/handshake path entirely; the only timeout that ends up applying is `sync_request_timeout` (default 5s) on the internal `ping`, which can succeed against buffered data or take long enough that only a handful of probes fit in the 30s budget. The `handshake_timeout_ms=200` we set never gets a chance to fire. The prober keeps reporting the paused node as reachable for the full timeout, even after the freeze is fully in effect, and `_wait_for_pause_effective` raises. Switch the probe to a raw TCP connection from the test process directly to `<instance>:9000`. Each iteration does a fresh connect + one-byte send + 0.5s recv. A live ClickHouse server replies (with an error packet) in milliseconds; a paused server cannot send anything from user space, so the recv times out and the probe declares the pause effective. No ClickHouse server is in the loop, no connection pool to bypass, no need to guess what `sync_request_timeout` is doing. As a side benefit the helper no longer needs a sibling ClickHouse instance — it also works for single-node setups. A diagnostic `logging.warning` line is emitted every ~5s while the probe is still in the loop, recording the last probe outcome (`server replied (N bytes)` / `recv timed out` / `connect failed: ConnectionRefusedError` / etc.). If the probe ever fails again, the CI log will tell us exactly what the test process saw on each iteration. The 90-second `wait_timeout` introduced in the first commit of this PR is kept as defense-in-depth — the new probe should exit on the very first iteration once the freeze is in effect, but the 90s margin costs nothing in the happy path and gives us room if a future kernel/docker quirk slows things down again. Closes ClickHouse#103819, ClickHouse#103820
The `_wait_for_pause_effective` probe added in ClickHouse#104167 polls a sibling ClickHouse instance with a tight `remote(...)` query until the freeze of the target container blocks fresh handshakes. The 30-second default was sufficient on debug and release builds but not on the slowest CI jobs: - `Integration tests (amd_msan, 1/6)` — 2026-05-13 master run - `Integration tests (amd_asan_ubsan, db disk, old analyzer, 1/6)` — 2026-05-12 master run both produced `pause_container('node1') did not become observably effective within 30.0s`. Under MSan/ASan+UBSan instrumentation each probe iteration (subprocess spawn, sibling-node query, handshake failure path) takes a few seconds, so only a handful of probes fit in a 30-second budget; meanwhile the Docker cgroup freezer + kernel TCP race window is itself wider when the whole node graph is sanitizer-slow. Bump the default `wait_timeout` in `ClickHouseCluster.pause_container` and `pause_container_using_signal` from 30s to 90s. This is a 3x budget, keeps the probe useful when something is genuinely wrong with the pause, and matches the empirical worst case observed on those CI shards. No caller in `tests/integration/` overrides `wait_timeout`, so this is the only place the value needs to change. Tracked in ClickHouse#103819. Surfaced on PR ClickHouse#104268 by @alexey-milovidov.
|
Backporting to fix Ci |
…e probe Conflict in `pause_container`: master wraps `_pause_container` in a try/except that falls back to `SIGSTOP` and records `used_signal`, and branches the `finally` on it. That fallback is not part of #104167 and does not exist on 26.3 (`used_signal` appears nowhere on this branch), so taking it would leave a NameError path. Resolved to 26.3's plain `self._pause_container(instance_name)`. `_wait_for_pause_effective` and `pause_container_using_signal` are byte identical to the cherry-picked versions. The docstring no longer mentions the `SIGSTOP` fallback, which this branch's `pause_container` does not have. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…inistic by gating on a pause-effective probe
Backport #104167 to 26.3: Make test_cluster_all_replicas_query deterministic by gating on a pause-effective probe
Cherry-pick of #105112 (fix chronic flaky test_distributed_frozen_replica: bypass connection pool in pause-effective probe) onto 26.3. The bot's base branch was cut before #119447 landed the prerequisite #104167, so `_wait_for_pause_effective` was absent entirely. Merged the current 26.3 tip first, which resolved that. The one remaining conflict was in the `pause_container` docstring. Kept the new paragraph describing the 90s `wait_timeout` default (part of this PR) and dropped the reinstated "(whether via `docker compose pause` or the `SIGSTOP` fallback)" clause: 26.3's `pause_container` has no SIGSTOP fallback, as resolved in #119446. `used_signal` predates #105112 in the merge base and is not touched by this PR, so it stays out of the backport. `_wait_for_pause_effective` and `pause_container_using_signal` are identical to master. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Make
test_distributed_frozen_replica/test.py::test_cluster_all_replicas_querydeterministic.Motivation
test_cluster_all_replicas_queryis a chronic flaky test (issues #103819 and #103820) that asserts pausingnode1makesclusterAllReplicas(test_cluster, default.local_table)fail withALL_CONNECTION_TRIES_FAILED.cluster.pause_containeruses Docker's cgroup freezer to suspendnode1's process tree. The freezer takes effect quickly but not synchronously with in-flight TCP traffic, so the test SELECT can race the freezer:node1completes the TCP handshake and the tiny ClickHouse server greeting before being suspended, every per-replica shard succeeds, andnode3.query(...)returns a cleancount()witherror == ''. The assertion then fails spuriously with:This failure mode emerged after the prior
857 msformatting fix in PR #103780. Cross-PR data over the last 30 days shows 17 distinct PRs hitting it (including 6 master runs).Approach
Inside the pause context, poll a fast probe —
SELECT 1 FROM remote('node1:9000', system.one)with a 200 mshandshake_timeout_msandconnect_timeout_with_failover_ms— until it raisesQueryRuntimeException. Once that probe fails, connections tonode1are demonstrably blocked at the application level and the actualclusterAllReplicasprobe can run deterministically. The 30-second deadline accommodates slow CI runners; in practice the probe should fail on the first or second iteration.The original assertions (
ALL_CONNECTION_TRIES_FAILED in error,857 ms in error) are unchanged — the fix only removes the timing race.Related
test_cluster_all_replicas_query[1-test_cluster]test_cluster_all_replicas_query[3-test_cluster]857 msformat-parity assertion (merged 2026-04-30)Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Documentation entry for user-facing changes
Version info
26.5.1.351(included in26.5and later)26.3.33.53