Repository navigation
Remove the racing async-insert flush from 02015_async_inserts_2 - #112214
PedroTadim merged 2 commits into
Conversation
The test fires three concurrent wait_for_async_insert=1 HTTP inserts, then issues an explicit SYSTEM FLUSH ASYNC INSERT QUEUE after sleep 2. flush(tables) is a snapshot which, unlike flushAll, deliberately does not set flush_stopped, so a concurrent push can create a new queue entry immediately after it. When the third insert has not yet been appended at that moment, the flush drains a partial batch of two and the late insert lands in a fresh entry whose only remaining trigger is its 600s async_insert_busy_timeout_ms, so its waiting client parks until curl gives up after 120s. That is the observed failure: Code: 27 plus 2 of 4 rows, all_1_1_0 with 2 rows, and curl: (28) Operation timed out with 0 bytes received. sleep 2 does not prevent it, since bash forks the three background children and reaches it immediately. Deleting both lines restores async_insert_max_query_number = 3 as the sole trigger, which is order-independent: entries are appended unconditionally at push time (the malformed one counts too), so whichever request arrives third completes the batch and flushes all three atomically. Both lines were added by PR ClickHouse#89140; the failure first appears in CI after that merge, so this is a revert of that PR's test-side half. No assertion is weakened - the settings, the three inserts, the Code: 27 check and the reference file are unchanged, and the single-part assertion gets stronger. The test also stops paying an unconditional 2 second sleep. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=107974&sha=fbe4bc270336b23fbb9759890a617bd1519ec25f&name_0=PR&name_1=Stateless%20tests%20%28arm_binary%2C%20parallel%29 Related PR: ClickHouse#107974 Co-Authored-By: Claude Opus 5 <[email protected]>
The code change is unaffected: this commit is empty and the diff against master is still the same 3 insertions and 2 deletions in `tests/queries/0_stateless/02015_async_inserts_2.sh`. Only the previous message's account of when the failure started is wrong. That message claimed the failure only starts showing up in CI once the `ClickHouse#89140` merge landed, and concluded that this change is therefore a revert of that PR's test-side half. Both claims are false. Keying the CI database on this test name plus `curl: (28)` in `test_context_raw` returns 30 failing rows all time. The earliest is 2024-03-11 16:55:56 on PR ClickHouse#61112, `Stateless tests (aarch64)`, carrying the same partial shape the previous message quotes (`1 a`, `2 b`, `all_1_1_0 2 0` and a curl that received 0 bytes). 27 of those 30 predate the `ClickHouse#89140` merge (`dc5086b93f0959`, 2025-12-24 19:18:34 UTC) and 3 follow it. Counting whole runs since the URL settings reached their current form (`7c715e66a270e5`, authored 2024-01-30): 475006 runs and 27 such failures before that merge, 563673 runs and 3 after. The rate is lower in the era that has the flush, so the failure is older than the flush and this change is not a revert of the only cause. Both facts hold at once because two routes produced the same visible signature, and the older one is already closed. Route A was a global `SYSTEM FLUSH ASYNC INSERT QUEUE` issued by some other stateless test: it drains every table's queue, so it could partially drain a concurrently running `02015_async_inserts_2` that had no flush of its own. That route was removed by `36fc6e58b55cff` (2026-05-21) and the bare form is now rejected by `ci/jobs/scripts/check_style/various_checks.sh` (added `c3d9664bcee66b`); no test file on master trips that check today. Route B is this test's own flush, the mechanism the previous message describes correctly. Both remaining failures (2026-07-21 and 2026-07-26) are after route A was closed, so route B is the route that is still live, and deleting these two lines closes it. The causal claim for route B rests on the forced-ordering experiment, not on CI rates, which cannot separate the two routes. Under one forced interleaving and one binary: unfixed 10/10 FAIL, fixed 10/10 PASS, re-adding only the explicit flush 4/10 FAIL, re-adding only `sleep 2` 10/10 PASS. `Both lines were added by PR ClickHouse#89140` stands and is verified: `bf6826835ce5c7` adds exactly those 2 lines to this file and nothing else, it leaves the reference file untouched, and its merge commit is an ancestor of master. Co-Authored-By: Claude Opus 5 <[email protected]>
Internal second-model review (2 review rounds, 10 findings, all adjudicated) - click to expandBefore opening this PR I reviewed it twice with an independent model that does not see the Round 1❌ The provenance claim was false, and the correction is the interesting part. The first ❌ The
💡 Two further nits were dropped after checking them: a suggested style-check grep is already Round 2 (after the correction commit)❌ The corrected description still contradicted itself on the post-merge count. It said 4 ❌ A line count was published as a file count. The description said "16 such test files at 💡 One acceptance criterion I had written for the correction round was unsatisfiable by The shipped diff is unchanged by both review rounds: 1 file, |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-review-slot-54:20260728-023400 |
|
cc @hanfei1991 - could you review this? It is a test-only change: |
|
Workflow [PR], commit [f68457f] Summary: ✅ AI ReviewSummaryThis PR removes the explicit Final Verdict
|
CI finish ledger - f68457fCI has fully finished on this head with no failures: 174 checks reported, 0 failed Session id: cron:our-pr-ci-monitor:20260728-163000 |
Related: #107974
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Description
02015_async_inserts_2intermittently fails on master and on unrelated PRs with one of its threeconcurrent HTTP inserts never getting a response:
Only 2 of the 4 expected rows land, the part carries 2 rows instead of 4, and the test burns its
whole 120 s curl budget. The two most recent occurrences are 2026-07-21 (
pull_request_number = 0,head_ref = 'master',Stateless tests (amd_tsan, parallel)) and 2026-07-26 on an unrelated PR, soit is not caused by any change under test.
Root cause
The test pins
async_insert_busy_timeout_ms=600000,async_insert_max_query_number=3,async_insert_deduplicate=1andasync_insert_use_adaptive_busy_timeout=0, then fires threewait_for_async_insert=1inserts concurrently in the background (two well-formed, one malformed).All three share one batching key, so with those settings the only reachable flush trigger is
has_enough_queries(AsynchronousInsertQueue.cpp:604):async_insert_max_data_sizeis 10 MiBagainst ~12 bytes of data,
async_insert_use_adaptive_busy_timeout = 0makesmax_busy_timeout_exceededreturn false on its first clause, and the deadline flush is 600 s away.That threshold trigger is timing-independent.
data->entries.emplace_backatAsynchronousInsertQueue.cpp:595is unconditional at push time, so the malformed entry counts towardthe threshold as well - parsing happens later at flush time, where the parse error finishes only its
own promise while its batch-mates succeed. Whichever request arrives third therefore completes the
batch and flushes all three atomically under the shard mutex, acking all three waiters and producing
one part with 4 rows.
The explicit
SYSTEM FLUSH ASYNC INSERT QUEUEin the test defeats it.flush(tables)is a snapshot:unlike
flushAllit deliberately does not setflush_stopped, so the guard atAsynchronousInsertQueue.cpp:620stays open and a concurrent push can create a brand-new queue entryimmediately after. Its own comments say so - "jobs scheduled before the call of 'flush' are not
counted here" (
:801) and "Other pending inserts are not blocked and can be processedconcurrently" (
:809). If the third insert has not reached:595when the flush snapshots the shardqueue, the flush drains a partial batch of two and the late insert lands in a fresh entry whose only
remaining trigger is its 600 s deadline. Its waiting client then parks until curl gives up - exactly
the observed output.
sleep 2does not prevent this:bashforks the three&children and reachessleep 2immediately, so it only bets that three HTTP requests reach the queue within 2 s of the fork. On a
loaded runner that bet occasionally loses. In the failing job's own debug log
sleep 2is eventraced before the three curls.
Two routes to the same symptom, and which one is left
This visible signature is older than the two lines being deleted here. Keyed on the test name and
curl: (28)intest_context_raw, the CI database has 30 such rows all the way back to 2024-03-11(PR #61112,
Stateless tests (aarch64), with the sameall_1_1_0 2 0partial shape), i.e. longbefore
sleep 2and the explicit flush were added to this test bybf6826835ce5c7(merged asdc5086b93f0959from #89140). So this change is not arevert that removes the only cause; it removes one of two routes, and the other route is already
closed:
36fc6e58b55cff("Run async-insert queuetests in parallel via table-scoped flush", 2026-05-21) several stateless tests issued the
table-less
SYSTEM FLUSH ASYNC INSERT QUEUE, which drains every table's queue. A concurrentlyrunning
02015_async_inserts_2could therefore be partially drained by an unrelated test and hangin exactly this way while having no flush of its own. Counting the sites that the style check now
rejects: 16 of them across 10 test files at 2025-08-14, 13 across 9 at 2026-05-18, and 0 from
36fc6e58b55cffonwards.ci/jobs/scripts/check_style/various_checks.shnow rejects the globalform outright, so the route cannot reopen.
(2026-07-21 and 2026-07-26) are after route A was removed, so route B is what is still live, and
it is what this change deletes.
For completeness, the per-era counts (the four settings pins reached their current form at
7c715e66a270e5, 2024-01-30, so only rows after that date are comparable): 475006 runs and 27curl: (28)failures while route A was open and this test had no flush of its own, then 563983 runsand 3 failures once the flush was added, of which one predates route A's removal and two follow it.
The raw rates do not isolate either route, which is why the causal claim below rests on a
forced-ordering experiment rather than on CI statistics.
Fix
Delete
sleep 2and the explicit flush, restoringasync_insert_max_query_number = 3as the sole,order-independent trigger, and leave a short comment so the lines are not reinstated later.
Nothing is weakened. The URL settings, all three inserts, the
Code: 27check, thewait, bothSELECTs and the.referenceare byte-identical. The single-part assertion actually becomesstronger: with the flush gone the only reachable outcome is one batch, hence one part with 4 rows,
whereas today a partial flush can legitimately produce two parts.
SYSTEM FLUSH ASYNC INSERT QUEUEcoverage is unaffected -
02726_async_insert_flush_queue,03708_flush_async_insert_queue_for_table,02726_async_insert_flush_stressand around 25 further tests exercise that statement; this test isthe carrier for waiting-client partial-batch parse-error isolation, which it keeps asserting.
Verification
The natural race window is sub-second, so the losing interleaving was forced instead of looped for:
the third background curl was wrapped in
( sleep 3; ... ) &so it registers after the flush, withCLICKHOUSE_CURL_TIMEOUTreduced to 10 s. Both are local harness knobs, not part of the change.Same binary throughout (a debug build of this PR's merge base).
sleep 2+ explicit flush)sleep 2re-addedThe last two rows isolate the cause within this test: re-adding the flush alone brings the failure
back, re-adding
sleep 2alone never fails. The flush-only arm is 4/10 rather than 10/10 becausewithout
sleep 2the flush often arrives before any push registers, drains nothing, and leaves thethreshold to do its job - which is precisely why the shipped combination was worse than either line
by itself.
The server's own log shows the mechanism directly: before the change,
Will wait for finishing of 1 flushing jobs (about 2 inserts, ...)followed byFlushed 2 rows;after it,
Scheduling async insert processing job because enough queries accumulatedfollowed byFlushed 4 rows.--test-runs 50with randomization: 50 passed, 0 failed. The test also gets faster on every run,since the deleted
sleep 2was paid unconditionally: median duration goes from 3.04 s to1.04 s.
The whole async-insert-flush family (
00408_http_keep_alive,02015_async_inserts_1..7,02726_async_insert_flush_queue,02726_async_insert_flush_stress,03708_flush_async_insert_queue_for_table,04326_async_insert_flush_processors_profile_log,04547_async_insert_flush_cancellation) is 13/13 green both before and after, with an identicalfailure set.
Why the threshold trigger is trustworthy without an explicit flush
00408_http_keep_aliveruns the same three requests (two well-formed plus oneqqqqqqqqqqq) withthe same five pinned settings, in the background, with a bare
waitand nosleepand noflush before that
wait- itsSYSTEM FLUSH ASYNC INSERT QUEUEruns afterwards as inter-iterationcleanup - and it covers both
wait_for_async_insert=0and=1. Over the last 30 days it recorded136038 OK / 2 FAIL, and neither failure is a hang (both are a host-memory-pressure window).
One caveat so this is not read as more than it is:
00408pipes each curl through|& grep 'Connection:' > $file_N, so acurl: (28)message never reaches the runner's stderr - ahang there would surface as an empty file and a missing
< Connection: Keep-Aliveline, i.e. asresult differs, not as acurl: (28)row. Its zerocurl: (28)count is therefore a property ofits output routing and not a hang count, and the two tests' failure counts are not comparable on that
signature. What
00408does establish is that these three requests plus these pins complete on theirown, without an explicit flush, at comparable volume.
I also re-derived which tests can carry this failure mode by scanning every blob under
tests/queries/onmasterforFLUSH ASYNC INSERT QUEUE(33 files, 28 excluding.reference) andkeeping those that combine a background async insert,
wait_for_async_insert=1, and an explicit flushbefore their
wait:02015_async_inserts_2is the only one. In the three other tests where a flushand a
waitcoexist the flush runs after thewait(00408_http_keep_alive), or inside a backgroundloop with every insert at
wait_for_async_insert=0(02726_async_insert_flush_stress), or after awaitthat has no background jobs to wait for (02884_async_insert_native_protocol_3). No siblingtest needs the same change.
What I deliberately did not change
Making table-scoped
flush(tables)setflush_stoppedlikeflushAllwould fix the test the otherway round, but
flush_stoppedis process-wide, so a table-scoped flush would stall inserts to everyother table on the server for its duration - a user-visible regression traded for a test convenience.
The engine behaves as documented here; the test's ordering assumption was the problem.
Version info
26.8.1.434(included in26.8and later)