Skip to content

Fix flaky 03652_threads_count_insert: tighten query_log filter - #106671

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-flaky-03652-threads-count-insert
Jun 7, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-flaky-03652-threads-count-insert

Conversation

@groeneai

@groeneai groeneai commented Jun 7, 2026

Copy link
Copy Markdown
Collaborator

The flaky-test pattern was an extra 1 row appearing before the expected 7 in the
max_threads = 7 iterations of tests/queries/0_stateless/03652_threads_count_insert.sh,
seen across multiple unrelated PRs and on master over the last ~3 months
(observed under at least amd_msan + WasmEdge, arm_binary, amd_asan_ubsan db disk distributed plan, and amd_debug sequential variants).

The per-iteration query against system.query_log was filtering only by
current_database = currentDatabase() AND type != 'QueryStart' AND query_id = '$QUERY_ID'.
Under CI's randomized settings, a second query_log row can match the same query_id
(a subquery, a distributed sub-fragment, or a non-Insert follow-up running with the
same id), so the test sometimes saw two rows instead of the expected one and printed
an extra entry whose peak_threads_usage was small (typically mapped to 1 by the
if(peak_threads_usage >= 6, 7, peak_threads_usage) bucketing).

Tighten the filter to match only the successful initial INSERT log row:

type = 'QueryFinish' AND is_initial_query = 1 AND query_kind = 'Insert'

This is the same shape used by the sibling test 02871_peak_threads_usage
(which filters on type = 'QueryFinish').

Verified locally that the new filter correctly suppresses a duplicate query_log row
sharing the same query_id while still returning exactly one row for the real INSERT.

CI report (one of the recent failures):
https://s3.amazonaws.com/clickhouse-test-reports/101143/

No related open issue found.

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)

The test was occasionally returning two rows from the per-iteration
SELECT against `system.query_log` instead of one, observed as a
spurious extra `1` in the diff before the expected `7` for
`max_threads = 7` iterations across multiple unrelated PRs (ClickHouse#101143,
ClickHouse#105406, ClickHouse#102308, ClickHouse#100752, ClickHouse#104473, ClickHouse#98779, ClickHouse#97302) and on master.

The original filter accepted any `type != 'QueryStart'` row matching
the iteration's `query_id`. Under randomized CI settings a second
`query_log` entry can match the same `query_id` (subquery, distributed
sub-fragment, or non-`Insert` follow-up), giving the test two rows.

Tighten the filter to require the successful initial INSERT only:
`type = 'QueryFinish' AND is_initial_query = 1 AND query_kind = 'Insert'`.
This matches the well-known sibling test `02871_peak_threads_usage`,
which already filters on `type = 'QueryFinish'`.

Verified locally that the new filter correctly suppresses a duplicate
log row sharing the same `query_id` while still returning exactly one
row for the real INSERT.

CI report (one of the recent failures):
https://s3.amazonaws.com/clickhouse-test-reports/101143/

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

groeneai commented Jun 7, 2026

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

# Question Answer
a Deterministic repro? Yes. Locally, the duplicate row pattern is reproduced by reusing the iteration's query_id from a second client invocation against the same database (the test's old filter then returns the two-row [1, 7] shape that matches the CI diff).
b Root cause explained? Under CI's randomized settings a second query_log row can be written with the same query_id as the iteration's INSERT (subquery, distributed fragment, or non-Insert follow-up). The test's filter type != 'QueryStart' AND query_id = '$QUERY_ID' AND current_database = currentDatabase() accepts that row, so the SELECT returns two rows instead of one, with the spurious row's small peak_threads_usage bucketed to 1. The bug was always in the filter, not in peak_threads_usage accounting itself.
c Fix matches root cause? Yes. The filter is tightened to type = 'QueryFinish' AND is_initial_query = 1 AND query_kind = 'Insert' AND query_id = '$QUERY_ID', which excludes any subquery / non-INSERT row that happens to share the iteration's query_id. This mirrors the sibling test 02871_peak_threads_usage, which already filters on type = 'QueryFinish'.
d Test intent preserved / new tests added? Yes. The test still asserts the peak_threads_usage of the per-iteration INSERT, bucketed by (max_threads, max_insert_threads). No new test added because this is a flaky-test filter fix, not a code change.
e Both directions demonstrated? Yes. Locally, with the OLD filter the simulated duplicate produces [1, 7] (matching the CI failure shape). With the NEW filter the same scenario returns [1], the single intended row.
f Fix is general, not a narrow patch? N/A — flaky-test setting / query-shape fix, not a code bug. No sibling code paths to widen.

Session id: cron:clickhouse-ci-task-worker:20260607-091500

@groeneai

groeneai commented Jun 7, 2026

Copy link
Copy Markdown
Collaborator Author

cc @CheSema — could you review this? Test-only fix for the per-iteration system.query_log SELECT in 03652_threads_count_insert.sh: the old type != 'QueryStart' filter occasionally matched a second log row sharing the iteration's query_id under randomized CI settings (subquery / non-INSERT follow-up), giving the test two rows where it expects one. Tightened to type = 'QueryFinish' AND is_initial_query = 1 AND query_kind = 'Insert', mirroring the sibling test 02871_peak_threads_usage.

@nikitamikhaylov nikitamikhaylov added the can be tested Allows running workflows for external contributors label Jun 7, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [132d68f]

Summary: ✅


AI Review

Summary
  • The PR tightens the system.query_log filter in 03652_threads_count_insert.sh so the test reads only the successful initial Insert QueryFinish row for the iteration query_id, avoiding duplicate matching rows from other logged activity. I found no correctness issue in the changed predicate or its interaction with the test's expected output.
Final Verdict
  • Status: ✅ Approve

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

clickhouse-gh Bot commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

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

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

Newly covered by added/modified tests: 370 line(s), 65 function(s) across 133 file(s) · Details

Top files
  • src/Backups/BackupEntriesCollector.cpp: 29 line(s), 2 function(s)
  • src/Functions/catboostEvaluate.cpp: 17 line(s), 4 function(s)
  • src/AggregateFunctions/TimeSeries/AggregateFunctionTimeseriesChanges.h: 14 line(s), 32 function(s)
  • src/Parsers/parseFieldFromCastedLiteral.cpp: 10 line(s), 11 function(s)
  • src/Analyzer/Resolve/QueryAnalyzer.cpp: 10 line(s)

Full report

@groeneai

groeneai commented Jun 7, 2026

Copy link
Copy Markdown
Collaborator Author

CI Status — HEAD 132d68f65121dde0c0504c79edfac3cc4e0d8e07

CI fully finished and passed cleanly. All 166 checks completed; Finish Workflow, Mergeable Check, and the PR aggregator are all green. mergeStateStatus = CLEAN. Zero test failures in CIDB.

Ready for review. cc'd @CheSema in the earlier review-request comment.

Session: cron:our-pr-ci-monitor:20260607-160000

@alexey-milovidov alexey-milovidov self-assigned this Jun 7, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jun 7, 2026
Merged via the queue into ClickHouse:master with commit 67a0384 Jun 7, 2026
166 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 8, 2026
zex-hyd pushed a commit to zex-hyd/ClickHouse that referenced this pull request Aug 1, 2026
The test built its query_id as 03652_query_id_$RANDOM, drawing 4 times per
run from bash's 15-bit $RANDOM (0..32767). When two of the 4 loop iterations
draw the same number they share one query_id, and every predicate of the
test's system.query_log filter is true of both INSERTs, so the later SELECT
returns two rows instead of one. order by ALL prints the smaller first, which
is the extra 1 above a 7 seen in CI: 11 of the 13 failures recorded in the
last 90 days have that shape, spread over 11 unrelated pull requests.

This is a test bug and not a query_log defect. query_id uniqueness is
enforced only among concurrently running queries, in ProcessList::insert, and
system.query_log is append-only history, so several rows for one query_id are
by design. My earlier fix in ClickHouse#106671 tightened this same filter, which lowered
the rate but could not help, because each predicate it added is also true of
both colliding rows.

There is a second collision axis. current_database = currentDatabase()
isolates executions only when each execution gets a fresh database, whereas
the stress arms pass a fixed --database, so one database serves many
executions and a repeat draw from an earlier execution matches as well.

The id is now composed from the per-iteration loop variables, which closes
the within-run axis by construction with no randomness involved, plus a
server-generated UUIDv4, which closes the across-execution axis.

The natural failure rate is roughly 1 in 11,000, so the mechanism was proven
by forcing the collision rather than by repetition: replacing $RANDOM with a
constant makes the test fail deterministically with the same diff class in
maximal form, and adding only the loop-variable component makes it pass again.
The second axis reproduces with a fixed --database, 1 pass and 2 failures in 3
runs, and passes 5 of 5 with the fix. Two mutations confirm each component is
load-bearing. Randomized 50/50 run: 50 OK, 0 FAIL. No blanket
no-random-settings tag was added, since the mechanism is independent of the
randomizer.

One of the 13 failures, on ClickHouse#110015, is a different mechanism: a thread-count
shortfall printing 4 instead of 7, with the same line count rather than an
extra line. It is deliberately not addressed here and is tracked separately,
so the residual rate should fall to about 1 per 90 days rather than to zero.

Co-Authored-By: Claude Opus 5 <[email protected]>
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