Skip to content

Fix flaky test_recover_from_snapshot_with_chunked_transfer - #103767

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-keeper-snapshot-chunked-transfer-zk-reuse-20260430
May 1, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-keeper-snapshot-chunked-transfer-zk-reuse-20260430

Conversation

@groeneai

@groeneai groeneai commented Apr 30, 2026 •

Copy link
Copy Markdown
Collaborator

Description

The integration test test_recover_from_snapshot_with_chunked_transfer creates a kazoo client for the lagging node before stop_clickhouse(kill=True), then reuses the same client after restart to assert post-recovery state. Under CI load the kill+restart window can exceed the 30 s default kazoo session timeout, after which the session is irrecoverably expired and the client raises ConnectionClosedError on the next request — even with the wrapper's implicit retries.

This PR re-creates the kazoo client after the restart, matching the pattern used by every other test method in the same file (test_recover_after_interrupted_transfer, test_recover_with_chunk_size_larger_than_snapshot, test_recover_after_s3_read_error_during_transfer, test_recover_from_snapshot_sent_by_old_leader).

Failure example

CI failure that triggered this fix — PR #103394, Integration tests (amd_tsan, 6/6):

File: test_keeper_snapshot_chunked_transfer/test.py:128 - in test_recover_from_snapshot_with_chunked_transfer
    assert lagging_zk.get(prefix)[0] == b"somedata"
...
kazoo.exceptions.ConnectionClosedError: Connection has been closed

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=103394&sha=ad4662b27bacbf2146f3ece6456168c7b713946c&name_0=PR&name_1=Integration%20tests%20%28amd_tsan%2C%206%2F6%29

Chronic flakiness in CIDB

30-day window:

The PR-relatedness of failing PRs has been verified — none of them touch Keeper code, and they are spread across unrelated areas (PREWHERE/IN-subquery, throw on unknown config, exact subcolumn match, JSON/text index, skip index aggregation, PK analysis stats, !subranges.empty() logical error, MongoDB cleanup, Azure validation). This rules out a real regression and confirms a chronic test-side flake.

Why kazoo cannot recover

tests/integration/helpers/kazoo_client.py wraps every operation in self.retry(...), so transient disconnections are handled. But stop_clickhouse(kill=True) followed by a slow restart eventually exceeds the kazoo session timeout (30 s by default in get_fake_zk). Kazoo distinguishes LOST (transient) from EXPIRED (terminal); once a session is EXPIRED the client moves to a closed state and no retries can resurrect it. The fresh get_fake_zk call after restart establishes a new session.

Local validation

python3 -m ci.praktika run "integration" --test "test_keeper_snapshot_chunked_transfer/test.py::test_recover_from_snapshot_with_chunked_transfer[local_disk]" --workers 1 — passes cleanly:

     setup   passed dur=18.8s
      call   passed dur=5.6s
  teardown   passed dur=6.4s

The chronic flake is timing-dependent (kazoo session expiry under CI load), so it cannot be deterministically reproduced on a fast local machine. The structural argument is the strong proof: with this change, the failing test method now follows the same client-recreation pattern as the four sibling test methods in the file that have never exhibited this failure mode.

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

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

Not for changelog.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.5.1.233

The test creates a kazoo client for the lagging node before
`stop_clickhouse(kill=True)`, then reuses the same client after restart
to assert post-recovery state. Under CI load the kill+restart window can
exceed the 30 s default kazoo session timeout, after which the session
is irrecoverably expired and the client raises `ConnectionClosedError`
on the next request — even with the wrapper's implicit retries.

Re-create the kazoo client after the restart, matching the pattern used
by every other test method in this file (e.g.
`test_recover_after_interrupted_transfer`,
`test_recover_with_chunk_size_larger_than_snapshot`,
`test_recover_after_s3_read_error_during_transfer`,
`test_recover_from_snapshot_sent_by_old_leader`).

Failure example (from CIDB):
- PR ClickHouse#103394 / amd_tsan / `local_disk` variant
- assertion `lagging_zk.get(prefix)[0] == b"somedata"` raised
  `kazoo.exceptions.ConnectionClosedError: Connection has been closed`

Chronic in CIDB: 11 distinct PRs and 1 master hit over 30 days, including
PR ClickHouse#100332 (12 hits) and PR ClickHouse#101757 (12 hits) — both completely unrelated
to Keeper. Open tracking issues: ClickHouse#102040 and ClickHouse#101969.

Local validation:
- pytest run of `[local_disk]` variant passes cleanly with the fix
  (setup 18.8 s, call 5.6 s, teardown 6.4 s).
- Structural argument: the four sibling test methods in the same file
  already create `lagging_zk` after the restart and never exhibit this
  failure mode.
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR Validation Gate (Session cron:clickhouse-ci-task-worker:20260430-054500)

a) Deterministic repro? The chronic flake is timing-dependent — kazoo session expiry under CI load (kill+restart > 30 s default session timeout). Cannot deterministically repro on a fast local machine. The deterministic indicator is the CIDB pattern: 11 distinct UNRELATED PRs hit it in 30 days, plus 1 master hit on 2026-04-21. The recent failure on PR #103394 is a CI-load timing manifestation of this same chronic bug.

b) Root cause explained? lagging_zk = keeper_utils.get_fake_zk(...) is created BEFORE node_lagging.stop_clickhouse(kill=True). The kazoo session timeout is 30 s by default. When the kill+restart cycle exceeds 30 s (frequent under TSAN / heavy CI load), the kazoo session transitions from LOST (transient — recoverable via retry) to EXPIRED (terminal — not recoverable). The wrapper's self.retry(...) only handles LOST. Once the session is EXPIRED, the client moves to a closed state and raises ConnectionClosedError on the next request. Restarting the server creates a new session pool — the old session ID is gone forever.

c) Fix matches root cause? Re-create the kazoo client AFTER start_clickhouse + wait_until_connected. This is exactly the pattern used by every other test method in the same file:

  • test_recover_after_interrupted_transfer — line 213
  • test_recover_with_chunk_size_larger_than_snapshot — line 255
  • test_recover_after_s3_read_error_during_transfer — line 294
  • test_recover_from_snapshot_sent_by_old_leader — line 328

Not a band-aid — it brings the failing test method into structural alignment with its 4 sibling methods that have never exhibited this failure.

d) Test intent preserved? All assertions retained:

  • Pre-kill state checks (lines 115-117) unchanged
  • Snapshot-receipt check (get_received_snapshot_info + received is not None) unchanged
  • Post-recovery assert lagging_zk.get(prefix)[0] == b"somedata" unchanged
  • verify_test_tree(leader_zk, lagging_zk, ...) unchanged (uses fresh client; verify_test_tree does only sync/get/exists, no session-specific state)
  • Chunk-count check n_chunks == expected_chunks unchanged
  • assert_obj_ids and assert_receiving_snapshot_logged unchanged

The kazoo client object is just an inspection tool for ZK state — it has no semantic meaning across the kill. Re-creating it changes nothing observable about the test.

e) Both directions demonstrated?

  • WITHOUT fix: chronic CIDB pattern (11 distinct PRs in 30 days, 1 master hit) is the empirical proof of the bug.
  • WITH fix (just verified locally): python3 -m ci.praktika run "integration" --test "test_keeper_snapshot_chunked_transfer/test.py::test_recover_from_snapshot_with_chunked_transfer[local_disk]" --workers 1 passes — setup 18.8 s, call 5.6 s, teardown 6.4 s.
  • Strong structural argument: the four sibling test methods already use the post-restart re-creation pattern and never exhibit this failure mode.

f) Fix is general? Verified — only test_recover_from_snapshot_with_chunked_transfer has this anti-pattern. The other four test methods in the same file already create lagging_zk after the restart correctly. Fix targets the only place the bug exists.

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @kssenii — could you review this? Test-only fix per @alexey-milovidov's directive on #103394. The failing test method created its lagging-node kazoo client before stop_clickhouse(kill=True) and reused it after restart — under CI load the kill+restart window can exceed kazoo's 30 s session timeout, expiring the session permanently and causing ConnectionClosedError. Re-creating the client after restart matches the pattern already used by the four other test methods in the same file.

@pufit pufit added the can be tested Allows running workflows for external contributors label May 1, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 1, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [859aaee]

Summary: ✅


AI Review

Summary

This PR fixes a flaky integration test by recreating the lagging-node kazoo client after restart, avoiding reuse of an expired ZooKeeper session across stop_clickhouse(kill=True). The change is small, targeted, and consistent with the established pattern in sibling tests; I did not find correctness, safety, or performance issues in the patch.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label May 1, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good.

@alexey-milovidov alexey-milovidov self-assigned this May 1, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue May 1, 2026
Merged via the queue into ClickHouse:master with commit 1ffba8f May 1, 2026
165 of 166 checks passed
@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-ci pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants