Skip to content

Fix flaky test_replicated_users grant/rename under async replication - #111239

Merged
PedroTadim merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-test-replicated-users-flaky-grant-rename
Aug 8, 2026
Merged

PedroTadim merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-test-replicated-users-flaky-grant-rename

Conversation

@groeneai

@groeneai groeneai commented Jul 21, 2026 •

Copy link
Copy Markdown
Collaborator

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

...

Description

Fixes two flaky failures in test_replicated_users seen under amd_tsan, reported by @ alexey-milovidov on #111007:

  • test_grant_revoke_replicated[With_ignored_on_cluster] — empty SHOW GRANTS right after a replicated GRANT.
  • test_rename_replicated[USER] — ACCESS_ENTITY_ALREADY_EXISTS on ALTER USER ... RENAME.

Both trace to a single test bug, not a server bug.

Root cause. test_grant_revoke_replicated reads the grant back from node2 at line 167 with a bare, un-retried node2.query("SHOW GRANTS FOR theuser2"). It is the only cross-replica read in the file without a retry; every other one already uses query_with_retry / assert_eq_with_retry. ReplicatedAccessStorage propagates the write to node2 asynchronously, so a fast read can observe stale state. In the failing run node2 was read 265 ms after the node1 grant and returned empty.

The second failure is a cascade of the first: when line 167 fails, the test aborts before its DROP USER theuser2, leaking theuser2; the next test then does ALTER USER theuser RENAME TO theuser2 and correctly hits the leftover entity (all 20 retries fail identically; the server-side collision check is behaving as intended). CI history confirms the two tests fail together in the same commit, never independently.

Fix (test only):

  • Read the grant back with assert_eq_with_retry so it tolerates the replication window.
  • Wrap the theuser2 create/drop in try/finally so the user and the profile config are always restored, even on an earlier assertion failure, so nothing can leak into later tests.

Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=111007&sha=472b806e1820703eafeb6fb2dcd4e928bb482baf&name_0=PR&name_1=Integration%20tests%20%28amd_tsan%2C%204%2F6%29

Verification: the two failing tests plus their siblings pass in the integration harness (7 passed, no retries). Reverting to the bare read reproduces the stale-node2 read against a local 2-node replicated-access cluster.

Version info

  • Merged into: 26.8.1.1003 (included in 26.8 and later)

test_grant_revoke_replicated read the grant back from node2 with a bare,
un-retried query (the only cross-replica read in the file without a retry).
ReplicatedAccessStorage replicates the grant to node2 asynchronously, so a
fast read can observe stale state and return empty grants.

When that assert failed, the test aborted before dropping theuser2, leaking
the user into the next test, whose ALTER USER ... RENAME TO theuser2 then hit
the leftover entity with ACCESS_ENTITY_ALREADY_EXISTS. So both amd_tsan
failures reported on ClickHouse#111007 stem from this one test bug, not a server bug.

Read the grant back with assert_eq_with_retry, and drop theuser2 / restore the
profile in a finally block so nothing can leak into later tests.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes for the mechanism: a fast reader (curl/HTTP) against a local 2-node replicated-access cluster deterministically observes a stale node2 right after a node1 grant (UNKNOWN_ROLE/empty). The natural CI race is timing-sensitive (why it is flaky and retry_ok); measured node2 convergence ~67 ms, the failing run read at 265 ms.
b Root cause explained? Yes. ReplicatedAccessStorage replicates the grant to node2 asynchronously; test.py:167 read it back with the only un-retried cross-replica read in the file, so a fast read saw empty grants. The second failure (ACCESS_ENTITY_ALREADY_EXISTS on rename) is a cascade: the first failure aborts before DROP USER theuser2, leaking the user into the next test.
c Fix matches root cause? Yes. Adds the missing retry to the eventually-consistent read (assert_eq_with_retry) and makes cleanup exception-safe. No assertion weakened, no no-* tag, no data reduced.
d Test intent preserved / new tests? Yes. The test still asserts the exact same grant text on node2; it now tolerates the replication window, matching the pattern already used at lines 209/231/274. No new test needed (test-reliability fix).
e Both directions shown? Yes. With fix: the two flagged tests + siblings pass in the integration harness (7 passed, no retries). Without fix: a fast bare read reproduces the stale-node2 read.
f General, not a narrow patch? Yes. Line 167 was the sole un-retried cross-replica read; the try/finally fixes the leak at its source rather than guarding the victim test_rename_replicated (that would be a band-aid). test_rename_replicated was left untouched.
g Generalizes across inputs? N/A (test-logic fix; sibling rename params already use query_with_retry).
h Backward compatible? N/A (test-only change; no server/setting/format change).
i Invariants/contracts preserved? Yes. No source change; the server-side rename collision check and the eventual-consistency contract are untouched and correct.

Session id: cron:clickhouse-author-slot-4:20260721-125300

@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review (1 finding, adjudicated)

An independent second model reviewed the diff. Findings and dispositions:

❌ Blockers

  • retry-query-exception — DISAGREE (refuted). Claim: assert_eq_with_retry retries mismatched results but not exceptions, so an UNKNOWN_ROLE raised while theuser2 has not yet propagated to node2 would still fail immediately.
    Evidence it is incorrect: assert_eq_with_retry (tests/integration/helpers/test_tools.py:79-100) wraps instance.query in try: ... except Exception as ex: logging.exception(...); time.sleep(sleep_time), so it retries on any raised exception, including UNKNOWN_ROLE, not only on result mismatch. Verified empirically: a fake instance raising UNKNOWN_ROLE on its first 3 calls then returning the grant was tolerated and passed on the 4th call. Only the final post-loop query is uncaught, and it runs solely after all 20 retries (~10 s) are exhausted — far beyond the sub-second propagation window (~67 ms measured). No change needed.

⚠️ Majors / 💡 Nits: none.

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @pufit — could you review this? It fixes two flaky test_replicated_users failures (reported by @ alexey-milovidov on #111007): the test read a grant back from node2 with the file's only un-retried cross-replica read, which loses the async ReplicatedAccessStorage propagation race; when that assert fails it also leaks theuser2 into the next test (the RENAME collision). Fix adds assert_eq_with_retry and a try/finally cleanup — test-only.

@groeneai groeneai mentioned this pull request Jul 21, 2026
1 task done
@PedroTadim PedroTadim self-assigned this Aug 7, 2026
@PedroTadim PedroTadim added the can be tested Allows running workflows for external contributors label Aug 7, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ed797f3]

Summary: ✅


AI Review

Summary

The PR updates test_grant_revoke_replicated so the cross-replica SHOW GRANTS check tolerates the asynchronous replication window in ReplicatedAccessStorage, and it moves the theuser2 / profile cleanup into a finally block so a failed assertion no longer leaks state into later tests. That matches the failure mode described in the PR and I did not find a remaining correctness or test-isolation problem in the current diff; the current PR CI report is also clean.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Aug 7, 2026
@groeneai

groeneai commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — ed797f3

CI is fully finished on this head and there are no failures: 174 check-runs, 0 incomplete, Finish Workflow success, Config Workflow success. CIDB reports 5173 test rows across 24 checks on this commit and zero with status FAIL or ERROR.

45 jobs ran, including the whole integration matrix this fix is about: Integration tests (amd_tsan, 1..6/6), (amd_msan, 1..8/8), (amd_llvm_coverage, 1..8/8), (amd_asan_ubsan, db disk, old analyzer, 1..6/6), (amd_asan_ubsan, targeted), (amd_asan_ubsan, flaky) and (arm_binary, distributed plan, 1..4/4). The two test_replicated_users symptoms this PR addresses are green on every one of them.

Nothing to attribute, so there is no owner column to fill.

For the record on timing rather than on this result: the workflow only started today, after the can be tested label was applied at 15:27:26Z. Before that the head had 0 check-runs since 2026-07-21, which is why no ledger appeared earlier.

Session id: cron:our-pr-ci-monitor:20260807-200000

@PedroTadim
PedroTadim added this pull request to the merge queue Aug 8, 2026
Merged via the queue into ClickHouse:master with commit fbb21f1 Aug 8, 2026
180 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 8, 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.

3 participants