Skip to content

Fix flaky test_replication_credentials: two waits in front of SYSTEM SYNC REPLICA - #119897

Merged
PedroTadim merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-replication-credentials-sync-timeout
Sep 18, 2026
Merged

PedroTadim merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-replication-credentials-sync-timeout

Conversation

@groeneai

@groeneai groeneai commented Sep 14, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #113473
Related: #108433

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

test_different_credentials and test_credentials_and_no_credentials mismatch the interserver credentials on purpose, so the reader's GET_PART is rejected 401. Each then repairs them and calls SYSTEM SYNC REPLICA, which with no mode keyword waits for every entry present at call time, including that stale one. Two waits can sit in front of it.

First, an attempt already in flight, which is the one CI hit. In the failing run node8's GET_PART for 201706_0_0_0 is rejected 401 in 2ms but releases the entry only 15.04s later, after the 10s client timer has already fired; num_tries was 0 throughout that sync, so the backoff played no part in it. No table setting shortens an attempt already running, so only the client budget covers it: both syncs get 30s, twice that attempt.

Second, the backoff once that attempt fails: min(2^num_tries ms, max_postpone_time_for_failed_replicated_{fetches,merges}_ms) from the last exception, shortened only by a queue reload. Driving that window locally I measured 58705ms still outstanding at sync start and even a 60s cap losing by 0.03s, so no client budget covers this half; the bounds have to go to 0, as in #108433 for test_parallel_quorum_actually_quorum.

Setting them on all four shards is neutral: with no failed entry last_exception_time_ms stays 0 and the bound is never consulted.

The two syncs keeping a 10s budget follow a TRUNCATE between peers whose credentials match, so they drain a DROP_RANGE and no fetch can be rejected.

Validation: the two syncs drop from 32.9s and 16.0s to 0.017s and 0.015s; at the 30s budget the module is green 44/44 across a plain run and a --count 10, and removing only the setting reddens both sites with the original traceback. Recorded failures: 2 in 120 days, both on Integration tests (amd_llvm_coverage, 7/8), none on master. Example report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=119821&sha=96446fd4c17e3113a698f14b1414971327f4cf5e&name_0=PR

node8 log from the failing run (the 15.04s attempt the sync waited on)
03:30:57.822 ReplicatedMergeTreeQueue: Insert entry queue-0000000000 to queue with type GET_PART
03:30:57.830 Fetching part 201706_0_0_0 from .../replicas/node7
03:30:57.832 ReadWriteBufferFromHTTP: Failed to make request ... HTTP status code: 401 'Unauthorized'
03:31:02.480 executeQuery: SYSTEM SYNC REPLICA test_table
03:31:02.492 ReplicatedMergeTreeQueue: Waiting for 1 entries to be processed: queue-0000000000
                                                        <- 10s client timer fires at ~03:31:12.480
03:31:12.867 processQueueEntry: Code: 86 ... 401 'Unauthorized'    (attempt ends, entry released)
03:31:17.160 ReplicatedMergeTreeQueue: ... put aside this log entry for ... ms   (first postpone)

That 15.04s is an outlier of that run, not general build slowness: node7 on the same
build made 12 rejected fetch attempts in the same window, each releasing the entry
within 0.1s (11 of them within 6ms).


Workflow [PR]
Sync PR [sync-upstream/pr/119897]

…ff the sync must wait out

test_different_credentials and test_credentials_and_no_credentials each run a
phase with mismatched interserver credentials on purpose, so the reader
replica's GET_PART for the writer's part is rejected 401 and enters the
failed-entry backoff. ReplicatedMergeTreeQueue::getPostponeTimeMsForEntry gates
re-execution for min(2^num_tries ms, <bound>) measured from
last_exception_time_ms, where <bound> is
max_postpone_time_for_failed_replicated_fetches_ms for GET_PART and
..._merges_ms for MERGE_PARTS, both defaulting to 60000 ms; the 2^num_tries term
saturates against the bound from num_tries = 16, and nothing but a queue reload
shortens it. Both tests then repair the credentials and call SYSTEM SYNC REPLICA
with no mode keyword, i.e. SyncReplicaMode::DEFAULT, which snapshots every queue
entry present at call time and waits for all of them, so the sync sits out
whatever remains of that window while the client-side kill timer runs.

Raising the client timeout cannot fix this, because the reachable window is the
bound itself. Driving the entry to num_tries = 16 on the unfixed test, I
measured a window of 59299 ms armed on it, 58705 ms still outstanding when the
sync started, and a 60000 ms client cap still losing: the fetch succeeded 0.03 s
after the client had been killed. So this sets both bounds to 0 on the table and
keeps the two client budgets aligned with the four other sync sites in this file
as a secondary guard. It is the same pairing that fixed
test_parallel_quorum_actually_quorum (ClickHouse#108433); test_lost_part sets both bounds
the same way, and tests/config/config.d/backoff_policy_25_4.xml zeroes them for
the whole stateless suite.

_fill_nodes builds the table for all four shards. That is neutral for the two
matched-credential shards: with no failed entry last_exception_time_ms stays 0,
so next_min_allowed_time_ms is already in the past and
getPostponeTimeMsForEntry returns 0 whatever the bound is.

With the bounds at 0 both sites pass at the old 10 s budget with the sync taking
0.115 s, and redden again with the original traceback when only the setting is
removed. On the shipped tree the two syncs drop from 32.859 s and 16.043 s to
0.017 s and 0.015 s, so both tests stop waiting out a recovery window they
previously sat through on every green run. No assertion changes, so genuinely
broken replication still fails.

The two syncs that keep a 10 s budget directly follow a TRUNCATE on the peer, so
they drain a DROP_RANGE and need no interserver fetch at all.

Recorded failures: 2 in 120 days, both on Integration tests
(amd_llvm_coverage, 7/8), none on master.

Co-Authored-By: Claude Opus 5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding labels Sep 14, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex) plus my own cold pass, over
1 full pass, 1 fix round and 1 delta recheck. The recheck returned 0 findings.

# Sev Finding Verdict Evidence / action
1 ⚠️ A 60s client cap does not exceed the bound it has to survive: a failed GET_PART is postponed for min(2^num_tries ms, max_postpone_time_for_failed_replicated_fetches_ms), default 60000 ms (ReplicatedMergeTreeQueue.cpp:1649, MergeTreeSettings.cpp:1845) AGREE, fixed @ 02a8e80 Raised independently by the gate and by my own pass. Confirmed in the wild: node8's log in the failing run carries 71 postpone lines on the one stale GET_PART, with up to 59257 ms still to run. The bound is now neutralised on the table instead of out-waited
2 💡 max_postpone_time_for_failed_replicated_merges_ms is the sibling carrier: a MERGE_PARTS entry whose source part was rejected fails on the reader and postpones under it, and a DEFAULT-mode sync waits for that entry too AGREE, fixed @ 02a8e80 Both bounds set together, as tests/integration/test_lost_part/test.py does. Reachable here rather than pattern-mirrored: both tests leave two parts in partition 201706 on the writer
3 💡 "the backoff path is unreachable" for the two sites left at 10s was broader than what had been shown AGREE, fixed @ 02a8e80 Reworded: those two syncs follow a TRUNCATE, so they drain a DROP_RANGE and need no interserver fetch at all
4 💡 The body closed with an undisposed product anomaly (one already-rejected fetch holding the entry 15s on the instrumented build) AGREE, fixed @ 02a8e80 Removed from the body. Not reproduced outside that build, and I am not changing the fetcher here
5 ⚠️ Alternative remedy: keep the bound and raise the client cap to 120s instead DISAGREE It would out-wait the bound, but both tests would keep sitting out the recovery window on every green run: measured 32.859s and 16.043s before, 0.017s and 0.015s after. It also adds a constant no other sync site in this file uses (the other four use 60)
6 💡 Zero the third bound too, max_postpone_time_for_failed_replicated_tasks_ms, as tests/config/config.d/backoff_policy_25_4.xml does for the stateless suite DISAGREE That bound is only read by the default: branch of getPostponeTimeMsForEntry, i.e. by non-fetch/merge/mutate entries. The only such entries here are the DROP_RANGEs drained by the two syncs that keep a 10s budget, on matched-credential pairs where no fetch is needed, and there are 0 recorded failures at those two sites in 120 days. Happy to widen it if a reviewer prefers all three

Noted, not blocking: the four time.sleep(1) barriers in this file survive. They cost about 4s but
cannot fail spuriously, since every assertion around them is time-independent, so I am not raising
them.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding. Findings on hunks unchanged by the fix round are auto-dropped.

Session id: cron:clickhouse-review-slot-8:20260914-071700

@clickhouse-gh clickhouse-gh Bot closed this Sep 14, 2026
@clickhouse-gh clickhouse-gh Bot reopened this Sep 14, 2026
@clickhouse-gh clickhouse-gh Bot closed this Sep 14, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. One scaffold, used by every arm and never committed: it holds the test inside the mismatched-credentials phase, polling system.replication_queue.num_tries until the stale GET_PART has failed 16 times (so 2^num_tries has saturated against the bound), reads the entry's remaining postpone, then falls through to the credential repair and the sync. The sync therefore starts at the top of a fully-saturated window. Reddens and greens on demand at both sites. Not a "~1%" repro.
b Root cause explained? Yes. Each test deliberately runs a phase with mismatched interserver credentials, so the reader's GET_PART is rejected 401 and enters the failed-entry backoff. ReplicatedMergeTreeQueue::getPostponeTimeMsForEntry gates re-execution for min(2^num_tries ms, <bound>) from last_exception_time_ms, where the bound is max_postpone_time_for_failed_replicated_fetches_ms for GET_PART and ..._merges_ms for MERGE_PARTS, both defaulting to 60000ms; only a queue reload shortens it. The test then repairs the credentials and calls SYSTEM SYNC REPLICA with no mode keyword, which waits for every entry present at call time, so it sits out the remaining window while the client kill timer runs. The defect is the bound, not the literal: any budget the test picks is racing a recovery window it does not control.
c Fix matches root cause? Yes. Rather than out-waiting the bound, I set it to 0 for this table (both the fetches and the merges bound, since getPostponeTimeMsForEntry switches on entry type), which removes the wait at its source; the aligned client budget stays as defence in depth. This is measured, not assumed: on the unfixed tree the reachable window was 59299ms and a 60s client cap still failed, with the fetch succeeding 0.03s after the client was killed. No assertion widened, no dataset shrunk, no no-random-* tag, no product-side guard. Same pairing as #108433.
d Test intent preserved / new tests added? Yes. Both asserts are untouched and still require 111\n222\n333\n, so genuinely broken replication still fails. The changed tests are themselves the regression coverage; no source change, so no new test is owed.
e Both directions demonstrated? Yes, at both sites on one scaffold. With the bounds at 0 both sites pass at the old, tight timeout=10 with the sync taking 0.115s; removing only the DDL setting reddens both again with the byte-identical QueryTimeoutExceedException traceback. Independent corroboration: with the bounds at 0 the entry reaches 16 failures in 4.9s/6.5s, versus 73.0s/71.4s at the default.
f Fix is general across code paths? Yes. Invariant: a sync must not have to wait out a failed entry's backoff. Carriers were enumerated from getPostponeTimeMsForEntry's own switch, not by grepping the token, so both reachable bounds are set. The third bound, max_postpone_time_for_failed_replicated_tasks_ms (5 min default, default: branch), is deliberately not set, matching precedent test_lost_part/test.py:50. Site-level: the two changed syncs are the only ones preceded by a deliberate credential mismatch; the two syncs that keep timeout=10 directly follow a TRUNCATE on the peer, so they drain a DROP_RANGE and need no interserver fetch at all.
g Fix generalizes across inputs (params/datatypes/wrappers)? N/A (test-only change; no source path, no type wrappers, no parameter matrix).
h Backward compatible? Yes. No product default changes and no SettingsChangesHistory.cpp entry is owed: both settings are applied per-table in a test DDL, so no existing setup can be affected.
i Invariants and contracts preserved? Yes. No product invariant is touched; both settings are documented table-level knobs already used this way in three in-tree precedents (test_lost_part, test_quorum_inserts_parallel, and tests/config/config.d/backoff_policy_25_4.xml for the whole stateless suite). Setting them on all four shards is neutral where nothing fails: with no failed entry last_exception_time_ms stays 0, so the computed retry time is already in the past and the bound is never consulted. The test's contract is strengthened, not relaxed: the same assertions must now hold within a smaller budget than before (both sites pass at 10s).

Validation: whole module green 84/84 (one plain run plus two --count 10), 0 timeouts. The two syncs drop from 32.9s and 16.0s to 0.017s and 0.015s.

Session id: cron:clickhouse-impl-slot-5:20260914-063700

@clickhouse-gh clickhouse-gh Bot reopened this Sep 14, 2026
@groeneai
groeneai requested a review from CheSema September 14, 2026 07:39
@clickhouse-gh

clickhouse-gh Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e21da3a]

Summary: ✅


AI Review

Summary

This PR hardens tests/integration/test_replication_credentials/test.py against the observed SYSTEM SYNC REPLICA timeout by disabling failed replicated fetch/merge postpone windows on the test table and by raising the two affected client-side sync budgets to 30 seconds. I did not find a remaining correctness, coverage, or metadata issue in the current patch: the helper-level setting change stays scoped to this test module, the previously discussed timeout sites are updated, and the current CI report for 2026-09-15 is green.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Sep 14, 2026
@PedroTadim PedroTadim self-assigned this Sep 14, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-testing Test content only (no production code). label Sep 14, 2026
node5.query("SYSTEM RELOAD CONFIG")
node5.query("INSERT INTO test_table values('2017-06-21', 333, 1)")
node6.query("SYSTEM SYNC REPLICA test_table", timeout=10)
node6.query("SYSTEM SYNC REPLICA test_table", timeout=60)

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.

@groeneai do you have to increase the timeout this much?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes, though not for the reason my description gave, which I have now corrected.

The failing run never reached the backoff. node8's GET_PART for 201706_0_0_0 starts at 03:30:57.830, is rejected 401 in 2ms, and the attempt releases the entry only at 03:31:12.867, so it held it for 15.04s on that coverage build. The sync starts at 03:31:02.480 (Waiting for 1 entries to be processed: queue-0000000000) and the 10s client timer fires about 0.4s before the attempt ends. num_tries was 0 for the whole sync: node8's first put aside this log entry line is at 03:31:17.160, after the client was already killed. The settings in this PR cannot shorten an attempt that is already running, so the client budget itself has to outlast it, and 10s does not.

I cannot account for those 15s on that build. It is not HTTP retries (401 is in non_retriable_errors, so doWithRetries rethrows at try 1), and it does not reproduce on a local debug build, where the same rejected attempt ends in milliseconds. Whatever it is, the sync has to sit through it.

The two bounds at 0 are for the other wait, the one that starts once that attempt fails: min(2^num_tries ms, max_postpone_time_for_failed_replicated_fetches_ms) reaches the 60s default, and driving it locally I measured 58705ms still outstanding at sync start, which a 60s cap loses to by 0.03s.

On the magnitude itself: 60 is what the four other sync calls in this file already use, so it is not a new constant, and it costs nothing on a green run (with the bounds at 0 these two syncs return in 0.017s and 0.015s). If you would rather keep the ceiling small, 30s is still twice the longest attempt I have measured on that lane and I will push that instead.

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.

yes, reduce to 30

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done in e21da3a: this call and the node8 one in test_credentials_and_no_credentials are both at 30s now, and the four pre-existing 60s calls in this file are untouched.

30s is twice the 15.04s attempt, and that attempt was the outlier of the run rather than the build: node7 made 12 rejected fetches in the same window on the same binary, each releasing the queue entry within 0.1s (11 of them within 6ms). The module is green 44/44 at 30s (one plain run plus a --count 10), and with the two bounds dropped both sites still time out at 30s, so the setting is still carrying the other wait.

@groeneai groeneai changed the title Fix flaky test_replication_credentials: remove the failed-fetch backoff the sync must wait out Fix flaky test_replication_credentials: two waits in front of SYSTEM SYNC REPLICA Sep 15, 2026
…on_credentials

These two syncs have to outlast one interserver fetch attempt that is already in
flight when the sync starts: in the failing coverage run node8's GET_PART for
201706_0_0_0 was rejected 401 in 2 ms yet held the queue entry for 15.04 s, and a
plain SYSTEM SYNC REPLICA waits for every entry present at call time. So 10 s is
structurally below the requirement, while 30 s is twice the longest attempt I have
measured on that lane.

The other wait, the failed-entry backoff that starts once that attempt fails, is
covered by the two bounds set to 0 in _fill_nodes, so the client budget does not
have to match the 60 s the four other sync calls in this file use.

Reduced from the 60 s of the previous commit as requested in review.

Co-Authored-By: Claude Opus 5 <[email protected]>
@PedroTadim

Copy link
Copy Markdown
Member

Good to go

@PedroTadim
PedroTadim added this pull request to the merge queue Sep 18, 2026
Merged via the queue into ClickHouse:master with commit 14ad2f5 Sep 18, 2026
176 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 18, 2026
@clickhouse-gh clickhouse-gh Bot added comp-replication ReplicatedMergeTree* + replication log/state transitions, eventual consistency mechanics. and removed comp-testing Test content only (no production code). labels Nov 22, 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 comp-replication ReplicatedMergeTree* + replication log/state transitions, eventual consistency mechanics. groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding 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