Repository navigation
Fix flaky 04320_query_condition_cache_topk_correctness via deterministic data placement - #113011
Conversation
…tic placement The test asserts that the first `ORDER BY k ASC LIMIT 5` run under the TopK plan writes a query condition cache entry (`count() > 0`). An entry is only written when at least one chunk is reduced to zero rows at the `WHERE` filter, which depends on where the smallest `w = 7` rows happen to land: placement was `rand()`, so a run could get a `__topKFilter` threshold trajectory under which every chunk kept at least one row and no entry was written (2 of 300 runs in a local loop on a release build). #111492 made this much more likely by shrinking the table from 1M rows (~123 chunks per scan) to 100k (~12 chunks); the same mode had been seen once with 1M rows on a flaky-check job before that. Place the five smallest `w = 7` rows at the front of the part and scatter every other row with a deterministic hash above the first chunk: the threshold tracker arms at its final value (`k = 4007`) right after the first chunk, and every later chunk deterministically empties at the `WHERE` filter and is recorded (1436 of 1564 marks). Verified locally: 30/30 runs pass, plus 10/10 with the failing run's randomized settings; the reference output is unchanged. Failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=112958&sha=ab076f814382fccafac0dd27dbe5e152d7204619&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20sequential%2C%202%2F2%29 PR with the failure: #112958 Co-Authored-By: Claude Fable 5 <[email protected]>
|
Workflow [PR], commit [dc056e6] Summary: ✅ AI ReviewSummaryThis PR updates Final Verdict
|
|
🕵 The only red check, A fix is already in progress: #110916 ("Fix LOGICAL_ERROR in OPTIMIZE ... DRY RUN racing a concurrent merge"). Merged |
|
🕵 Status update: the previously noted
@groeneai, investigate failure 2 (report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=113011&sha=143d963289028c262af645b2b433c3ee72620ff5&name_0=PR&name_1=Stress%20test%20%28arm_asan_ubsan%2C%20s3%29) and provide a fix in a separate PR — e.g. making the wait in I merged |
|
Confirmed, and this is a defect I already have the analysis for. Root cause, re-verified against master
The reachable channel is One correction to the diagnosis: index analysis is covered by No fix is in progress yet, so I will send a separate PR for the interruption point and link it here. I am deliberately leaving finer-grained cancellation for index analysis as a whole out of that PR: it is a broader change and it overlaps a separate hang I am tracking, a cancelled Caveat worth stating up front: this removes the "ignores cancellation" half only. A genuinely slow load that is never cancelled can still exceed the hung-check window, so it will not by itself clear the |
|
Fixing PR: #113436. As promised above, it only adds the interruption point: |
|
🕵 The only red check on commit 486d99e, A fix is already open: #113534, whose description names this exact STID. Once it merges, a re-run (or a |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units6 translation units recompiled, 3 s compile time in total, 6 of them have a recent master baseline. |
A query killed or detached while loading skip-index marks kept reading from object storage for minutes afterwards, because nothing between the marks loader and S3 observes QueryStatus::is_killed. Reported by alexey-milovidov on ClickHouse#113011: a query from 04010_text_index_incompelete_granules.sql sat for ~815 s with is_cancelled = 1 and read_rows = 0, tripping the hung check. In that run's hung_check.log the query thread waits on the marks-load future during skip-index analysis while the marks pool thread is inside ReadBuffer::next below MergeTreeMarksLoader::loadMarksImpl, in ReadBufferFromRemoteFSGather -> CachedOnDiskReadBufferFromFile -> S3. throwIfKilled and checkIfNotCancelled appear zero times in the gather, cached, encrypted-file and file-view read buffers, and under the thread fuzzer every buffer fill costs an injection sleep. Two nearer candidates do not work. The loader's own is_canceled flag is written only by ~MergeTreeMarksLoader, which cannot run while loadMarks() is on the stack, so it can only drop a task still queued in the pool. And the granule loop in loadMarksImpl runs at most 26 trivial iterations over a ~624 byte marks file in this carrier, while the whole wait sits inside one reader->eof() several frames lower. The interruption point therefore has to be in the read buffer. ReadBufferFromRemoteFSGather::readImpl now calls throwIfKilled before current_buf->next(). readImpl is the complete set of blocking-fill sites: it has exactly two callers, both in nextImpl, and everything nextImpl does before the first is non-blocking, so a check at the top of nextImpl would be redundant. The short-forward-seek drain in seek() is covered transitively, since ReadBuffer::ignore loops on next -> nextImpl -> readImpl. The throw precedes the SwapHelper, so the buffer is never left partially advanced. The check is gated on an opt-in ReadSettings flag that only makeIndexReaderStream sets, on its own copy of the settings, so every other read is byte-identical. That scoping is required rather than stylistic: QUERY_WAS_CANCELLED is not in isRetryableException, so an unscoped throw makes CHECK TABLE plus KILL delete a healthy part, and ATTACH plus KILL rename one broken-on-start. The generic form of this change, ClickHouse#109785, had to invent a thread-local blocker for exactly those reads. The QueryStatusPtr is resolved in ReadPipeline::build on the calling thread and threaded in, beside the existing query_id hoist that exists for the same reason: gather creators run lazily and may execute on threadpool workers with no query context, where tryGetQueryContext returns null and the check would be silently inert. The new test is deterministic via a PAUSEABLE failpoint placed inside the opt-in branch, so reaching it also proves the marks reader asked for interruptible reads: park the marks read, KILL, single-step exactly one fill with SYSTEM NOTIFY FAILPOINT, then assert the read unwinds instead of fetching another buffer. Removing only throwIfKilled, and removing only the opt-in, each redden it with a distinct message. Index-analysis cancellation granularity as a whole, and the experimental use_reader_executor path, are deliberately out of scope. This removes the "ignores cancellation" half only, so it does not on its own clear the hung check class. Co-Authored-By: Claude Fable 5 <[email protected]>
Related: #111492
Related: #104478
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix flaky test
04320_query_condition_cache_topk_correctness.Documentation entry for user-facing changes
The test asserts that the first
ORDER BY k ASC LIMIT 5run under the TopK plan writes a query condition cache entry (count() > 0oversystem.query_condition_cache). It failed with0on a TSan sequential run: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=112958&sha=ab076f814382fccafac0dd27dbe5e152d7204619&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20sequential%2C%202%2F2%29 (no tracking issue exists yet; the CI rerun diagnosis passed 29/29, and the test has no direct-master failures because the failing variant is one day old).Root cause. An entry is written only when at least one chunk is reduced to zero rows at the
WHEREFilterStep(FilterTransform::writeIntoQueryConditionCache). Whether that happens depends on where the smallestw = 7rows land relative to chunk boundaries and on the resulting__topKFilterthreshold trajectory — and the placement wasrand(). With an unlucky layout, every chunk keeps at least one surviving row, nothing is buffered, and the assertion reads0. #111492 shrank the table from 1M rows (~123 chunks per scan) to 100k (~12 chunks), which raised the per-run probability of that from negligible to roughly 1 in a few hundred — reproduced locally with the pre-fix test on a release build: 2 of 300 runs wrote no entry. The same failure mode was seen once with the 1M-row version on a flaky-check job (2026-07-27, PR #110015), so the race was inherent; the row-count reduction only amplified it.Fix. Make the QCC population deterministic by construction instead of probabilistic: place the five smallest
w = 7rows (k <= 4007) at the very front of the part and scatter every other row with a deterministic hash (intHash32) above the first 8192-row chunk. The first chunk then contains all five top rows, soPartialSortingTransformarms the threshold tracker at its final value (k = 4007) right after that chunk, and every later chunk deterministically loses all of its rows at theWHEREfilter and is recorded as skippable (1436 of 1564 marks, vs the previous data-dependent 20–50%). This also sharpens the salt assertions: the granules skipped by theLIMIT 5plan now always contain the rows theLIMIT 12andDESCplans need, so any wrong cache-entry reuse is guaranteed (not just likely) to be caught. The reference output is unchanged.Verification (with the CI-built binary of the failing run): the fixed test passes 30/30 against a local server and 10/10 with the failing run's randomized settings; the pre-fix test fails 2/300 with the exact
1→0diff from the report.