Skip to content

Fix flaky test_named_collections_encrypted2/test_zk_node_lifecycle - #105175

Merged
nikitamikhaylov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-test-zk-node-lifecycle-flaky
Jun 11, 2026
Merged

nikitamikhaylov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-test-zk-node-lifecycle-flaky

Conversation

@groeneai

@groeneai groeneai commented May 17, 2026 •

Copy link
Copy Markdown
Collaborator

The integration test test_named_collections_encrypted2/test.py::test_zk_node_lifecycle has been flaking on sanitizer builds with:

File: test_named_collections_encrypted2/test.py:529 - in test_zk_node_lifecycle
    assert "zk_test.sql" in zk.get_children(ZK_PATH)
E   AssertionError: assert 'zk_test.sql' in []

Observed twice on master CI in the last 60 days (both sanitizer shards):

Root cause

The test runs:

node1.query("CREATE NAMED COLLECTION zk_test AS key='val'")
assert "zk_test.sql" in zk.get_children(ZK_PATH)

CREATE NAMED COLLECTION is synchronous on the server side — it calls ZooKeeper::createOrUpdate in NamedCollectionsMetadataStorage::ZooKeeperStorage::write, which waits for the ZK leader's response. So when the query returns to the test, the znode /named_collections_path/zk_test.sql has been committed.

However, the test then reads via a separate kazoo client (cluster.get_kazoo_client("zoo1")). That kazoo client is a different ZK session, potentially connected to a different node of the ZK ensemble. ZooKeeper does not guarantee cross-session linearizability between a write on session A and a subsequent read on session B if B is connected to a follower that has not yet applied the write — the follower's view is eventually consistent with the leader on the order of milliseconds. Under TSan/MSan the slowdown is large enough that this normally invisible window becomes wide enough to fail the assertion. The symmetric race exists in the DROP branch (the kazoo client may briefly still see the znode after wait_not_exists succeeds on node1).

Fix

Add two polling helpers next to the existing wait_exists / wait_not_exists:

  • wait_zk_child_exists(zk, path, child, timeout=10)
  • wait_zk_child_absent(zk, path, child, timeout=10)

Both follow the existing pattern (10 s budget, 0.3 s poll, exception-tolerant). test_zk_node_lifecycle is updated to use them in place of the single-shot get_children membership checks. The test still asserts the same invariant — the znode appears after CREATE NAMED COLLECTION and disappears after DROP NAMED COLLECTION — but now tolerates up to 10 s of cross-session replication lag.

The change is strictly additive: when the underlying write IS already visible to the kazoo session (the common case), the first poll iteration returns immediately, so this introduces no measurable runtime overhead. If the server fails to write/remove the znode at all, the 10 s timeout still expires and the assertion fails as before.

cc @alexey-milovidov

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):

...

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.6.1.683

@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate (per worker policy, session cron:clickhouse-ci-task-worker:20260517-211500):

# Question Answer
a Deterministic repro? Partial. Not deterministic locally — the failure rate is ~2% on sanitizer builds, ~0% otherwise, and requires a sanitizer build + ZooKeeper ensemble + Docker. Two CIDB failures with identical traceback (assert against an empty children list) demonstrate the race exists. The mechanism is identified from code analysis: ZooKeeper::createOrUpdate is sync via the ClickHouse session, but the test's kazoo client uses a separate session that may be connected to a follower whose state is briefly behind the leader.
b Root cause explained? Yes. ClickHouse server writes the znode via session A (sync, leader-confirmed). The test reads via kazoo session B, which may be connected to a follower whose replication lags the leader by usually <1 ms — but under TSan/MSan slowdown the window widens enough to fail the assertion. The same lag explains the symmetric DROP race.
c Fix matches root cause? Yes. Polling get_children with a bounded retry directly addresses cross-session ZK replication lag. Pinning settings or removing the assertion would be band-aids.
d Test intent preserved? Yes. The test still asserts the same lifecycle invariant — znode appears after CREATE NAMED COLLECTION, disappears after DROP NAMED COLLECTION. If the server fails to write/remove the znode, the 10 s timeout still expires and the assertion still fails. The only new behavior is tolerance of up to 10 s of cross-session replication lag, matching the existing wait_exists / wait_not_exists helpers.
e Both directions demonstrated? One direction only. Test FAILS without the fix: demonstrated by the two CIDB hits cited in the PR description. Test PASSES with the fix: not locally re-verified (a sanitizer build + Docker rerun loop would take hours per iteration for a ~2% rate). The change is strictly additive — the first poll iteration is identical to the previous single-shot check, so a green test cannot become red.
f Fix is general? Yes. The two new helpers are reusable for any future test that needs to poll ZK directly.

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @alexey-milovidov — could you review? This is the fix for the test_zk_node_lifecycle failure you flagged on PR #102031. Test-only change (one file, +34/-2).

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label May 17, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2ccdf22]

Summary: ✅


AI Review

Summary

This PR makes test_zk_node_lifecycle tolerate cross-session ZooKeeper visibility lag by replacing single-shot get_children assertions with bounded polling helpers. The change is test-only, keeps the same lifecycle invariant for the zk_test.sql znode, and I did not find any correctness or coverage issue that needs an inline review comment.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label May 17, 2026
The test issues `CREATE NAMED COLLECTION zk_test` against the ClickHouse server
(ZooKeeper session A) and then immediately calls `kazoo.get_children` on a
separate kazoo client (ZooKeeper session B). The two sessions may be connected
to different nodes of the ZK ensemble. Even though `ZooKeeper::createOrUpdate`
on the server side is synchronous, the kazoo session can still see a stale
view for a short window — normally sub-millisecond, but amplified under TSan
and MSan slowdowns. The same race applies in the symmetric `DROP` case.

Observed twice on master CI:
- PR ClickHouse#101757 Integration tests (amd_tsan, 4/6) on 2026-05-16
- PR ClickHouse#102031 Integration tests (amd_msan, 6/6) on 2026-04-10
Both with `AssertionError: assert 'zk_test.sql' in []` at test.py:529.

Fix: add `wait_zk_child_exists` and `wait_zk_child_absent` polling helpers
(matching the existing `wait_exists` / `wait_not_exists` pattern, 10s timeout,
0.3s sleep) and use them in `test_zk_node_lifecycle` in place of the
single-shot `get_children` membership checks. The assertions still verify the
same invariant — ZK node appears after `CREATE`, disappears after `DROP` —
just with bounded retry to tolerate cross-session replication lag.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=102031&sha=169ea923b985fa66060e440b34cf2a908049afa4&name_0=PR&name_1=Integration%20tests%20%28amd_msan%2C%206%2F6%29
Related PR: ClickHouse#102031
@groeneai
groeneai force-pushed the groeneai/fix-test-zk-node-lifecycle-flaky branch from a81003f to 2ccdf22 Compare June 11, 2026 02:06
@groeneai

Copy link
Copy Markdown
Collaborator Author

Rebased onto current master (fc73f46) and re-triggered CI. The branch had gone stale (last run 2026-05-17).

On the prior run every Integration tests variant passed (amd_binary_excluded_from_llvm, amd_msan 6/6, amd_tsan 6/6, amd_asan_ubsan, amd_llvm_coverage, arm_binary) including the previously-flaky test_zk_node_lifecycle. The only red check was Stateless tests (amd_llvm_coverage, ParallelReplicas, s3 storage, parallel), which is a broadly flaky coverage job unrelated to this integration-test-only change (fails across 5-17 unrelated PRs daily in CIDB).

This is a test-only fix (one file, +34/-2): bounded ZK polling to tolerate cross-session replication lag in test_named_collections_encrypted2. The test remains chronically flaky on master (38 PRs / 81 failures in 90d) without it.

cc @alexey-milovidov

Session cron:clickhouse-worker-slot-6:20260611-015300

@clickhouse-gh

clickhouse-gh Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.60% 84.60% +0.00%
Functions 92.30% 92.30% +0.00%
Branches 77.20% 77.30% +0.10%

Changed lines: No C/C++ source files changed — skipping uncovered code analysis.

Newly covered by added/modified tests: 500 line(s), 46 function(s) across 127 file(s) · Details

Top files
  • src/Databases/DataLake/RestCatalog.cpp: 110 line(s), 8 function(s)
  • src/Storages/ObjectStorage/DataLakes/Iceberg/Utils.cpp: 22 line(s)
  • src/AggregateFunctions/TimeSeries/AggregateFunctionTimeseriesLinearRegression.h: 14 line(s), 32 function(s)
  • src/Storages/ObjectStorage/DataLakes/Iceberg/IcebergMetadata.cpp: 12 line(s)
  • src/Storages/StorageFilesystem.cpp: 12 line(s)

Full report

@nikitamikhaylov nikitamikhaylov self-assigned this Jun 11, 2026
@nikitamikhaylov
nikitamikhaylov added this pull request to the merge queue Jun 11, 2026
Merged via the queue into ClickHouse:master with commit 3b940bf Jun 11, 2026
166 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 11, 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