Skip to content

Fix flaky test 04043_system_asynchronous_inserts_user_filter - #105687

Closed
groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-04043-async-insert-adaptive-busy-timeout
Closed

groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-04043-async-insert-adaptive-busy-timeout

Conversation

@groeneai

@groeneai groeneai commented May 23, 2026 •

Copy link
Copy Markdown
Collaborator

The test relies on the queued async insert staying in system.asynchronous_inserts long enough for three follow-up SELECTs. To keep the entry in the queue, the INSERT sets async_insert_busy_timeout_min_ms = max_ms = 10 minutes.

With the default async_insert_use_adaptive_busy_timeout = 1, the max_busy_timeout_exceeded shortcut in AsynchronousInsertQueue::pushImpl fires when both shard.last_insert_time + max_ms < now and flush_time_points.first + max_ms < flush_time_points.second, using the current query's max_ms. With our 10-minute max_ms, any queue shard that has been idle for longer than 10 minutes - common in a CI run since each test run lands in a different shard via key.hash - matches both conditions, so the entry is moved to data_to_process and removed from shard.queue immediately. The subsequent SELECTs then see zero rows.

Smoking gun from master server log on a0a6cc08b5499fbf8c2ce88e554bdd9f7a929c23 after 36fc6e58b55c dropped this test's no-parallel tag:

AsynchronousInsertQueue: Asynchronous timeout increased from 2892 to 600000 for queue shard 13.
AsynchronousInsertQueue: Have 1 pending inserts in shard 13 with total 4 bytes ...
AsynchronousInsertQueue: Scheduling async insert processing job because maximum busy wait timeout exceeded
AsynchronousInsertQueue: Processing batch insert ...
AsynchronousInsertQueue: Flushed 1 rows, 8 bytes

Setting async_insert_use_adaptive_busy_timeout = 0 makes the lambda return on its first check, so the entry uses the fixed 10-minute deadline and survives until the SELECTs run. Verified with a deterministic reproducer (warm a shard with two short-timeout flushes, then issue a long-timeout insert: reproduces immediate drain with adaptive on, stays queued with adaptive off) and by running 50 iterations of the test at -j 16 mixed with the other async-insert stateless tests - no failures.

Failures occurred across 20+ CI runs in the 24h before this fix on Stateless tests (amd_msan, WasmEdge, parallel), (amd_tsan, parallel), (amd_tsan, s3 storage, parallel), (amd_debug, distributed plan, s3 storage, parallel), (arm_asan_ubsan, targeted), plus two master hits.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=a0a6cc08b5499fbf8c2ce88e554bdd9f7a929c23&name_0=MasterCI&name_1=Stateless%20tests%20%28amd_msan%2C%20WasmEdge%2C%20parallel%2C%202%2F2%29

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)

Note

Low Risk
Low risk: only adjusts a stateless test to make async insert behavior deterministic; no production code changes.

Overview
Makes 04043_system_asynchronous_inserts_user_filter.sh deterministic by disabling adaptive async-insert busy timeout (--async_insert_use_adaptive_busy_timeout 0) and documenting why, so the queued insert reliably remains visible in system.asynchronous_inserts long enough for the follow-up visibility assertions.

Reviewed by Cursor Bugbot for commit c22e056. Bugbot is set up for automated code reviews on this repo. Configure here.

The test relies on the queued async insert staying in
`system.asynchronous_inserts` long enough for three follow-up `SELECT`
queries from different users to observe it. To keep the entry in the
queue, the `INSERT` sets `async_insert_busy_timeout_min_ms` and
`async_insert_busy_timeout_max_ms` to 10 minutes.

With the default `async_insert_use_adaptive_busy_timeout = 1`, the
`max_busy_timeout_exceeded` shortcut inside
`AsynchronousInsertQueue::pushImpl`
(`src/Interpreters/AsynchronousInsertQueue.cpp`) fires when both
`shard.last_insert_time + max_ms < now` and
`flush_time_points.first + max_ms < flush_time_points.second` hold for
the chosen queue shard, using the current query's `max_ms`. With our 10
minute `max_ms`, any shard that has been idle for longer than 10
minutes - common in a busy CI run, since each test run lands in a
different shard via `key.hash` - matches both conditions, so our entry
is moved to `data_to_process` and `shard.queue.erase(queue_it)`
immediately. The subsequent `SELECT` queries then see zero rows.

Smoking gun from the master CI server log
(`https://s3.amazonaws.com/clickhouse-test-reports/REFs/master/a0a6cc08b5499fbf8c2ce88e554bdd9f7a929c23/stateless_tests_amd_msan_wasmedge_parallel_2_2/clickhouse-server.log.zst`,
PR-induced sweep starting after `36fc6e58b55c` which dropped the test's
`no-parallel` tag):

    AsynchronousInsertQueue: Asynchronous timeout increased from 2892 to 600000 for queue shard 13.
    AsynchronousInsertQueue: Have 1 pending inserts in shard 13 with total 4 bytes ...
    AsynchronousInsertQueue: Scheduling async insert processing job because maximum busy wait timeout exceeded
    AsynchronousInsertQueue: Processing batch insert ...
    AsynchronousInsertQueue: Flushed 1 rows, 8 bytes

Setting `async_insert_use_adaptive_busy_timeout = 0` makes the lambda
return on its first check, so the entry uses the fixed 10 minute
deadline and survives until the `SELECT`s run. Verified locally with a
deterministic reproducer (warm shard with two short-timeout flushes,
then a long-timeout insert) - reproduces immediate drain with adaptive
on, stays queued with adaptive off - and by running 50 iterations of
the test at `-j 16` mixed with the other async-insert stateless tests:
no failures.

Failure was visible across 20+ CI runs in the 24h before this fix on
`Stateless tests (amd_msan, WasmEdge, parallel)`, `(amd_tsan, parallel)`,
`(amd_tsan, s3 storage, parallel)`, `(amd_debug, distributed plan, s3
storage, parallel)`, `(arm_asan_ubsan, targeted)`, and two `master`
hits.
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate self-check:

a) Deterministic repro? Yes. Local reproducer: warm a queue shard with two INSERTs using async_insert_busy_timeout_max_ms=100, async_insert_use_adaptive_busy_timeout=1, wait_for_async_insert=1 spaced > 100 ms apart, then a third INSERT to the same shard with the same settings and wait_for_async_insert=0. With adaptive on, the server logs Scheduling async insert processing job because maximum busy wait timeout exceeded and system.asynchronous_inserts reports 0 rows immediately. With adaptive off, the entry stays queued and system.asynchronous_inserts reports 1 row.

b) Root cause explained? Yes. AsynchronousInsertQueue::pushImpl evaluates max_busy_timeout_exceeded() under the shard mutex before deciding whether to keep the new entry queued. The lambda checks *shard.last_insert_time + max_ms < now and *flush_time_points.first + max_ms < *flush_time_points.second using the CURRENT query's async_insert_busy_timeout_max_ms. The test uses max_ms = 600000, so any queue shard that has been idle for > 10 minutes - common in a busy CI job since each test run lands in a different shard via key.hash - satisfies both checks. The lambda returns true, the entry is moved to data_to_process, and shard.queue.erase(queue_it) runs before pushImpl returns to the client. The subsequent SELECT against system.asynchronous_inserts sees zero rows.

c) Fix matches root cause? Yes. Setting async_insert_use_adaptive_busy_timeout = 0 short-circuits the lambda's first check (!settings[Setting::async_insert_use_adaptive_busy_timeout]) so it always returns false. The fixed-timeout path getBusyWaitTimeoutMs then returns settings[max_ms] directly (600 000 ms), so the entry's deadline is now + 600 s and it survives in the queue until the test's three follow-up SELECTs run. No widening of bounds, no broad no-random-* tag, no reduction in test coverage - the targeted setting disables exactly the optimization that was misfiring.

d) Test intent preserved? Yes. The test still verifies that secret_user sees its own pending insert, restricted_user sees zero, and admin sees all - the assertions and the .reference file are unchanged. The fix only changes how the entry is kept in the queue (long fixed deadline instead of long adaptive deadline), which is what the test's existing 600 s min/max settings were already trying to express.

e) Both directions demonstrated? Yes. Without the fix the deterministic reproducer above shows system.asynchronous_inserts count = 0 and the server logs "maximum busy wait timeout exceeded". With the fix the count = 1 and the log line is absent. The test itself ran 50 iterations at -j 16 mixed with the other async-insert stateless tests (02726_async_insert_flush_queue, 02726_async_insert_flush_stress, 02884_async_insert_skip_settings, 02884_async_insert_native_protocol_1/3, 03148_async_queries_in_query_log_errors, 03228_async_insert_query_params, 00408_http_keep_alive) with no failures.

f) Fix is general, not a narrow patch? This is a flaky-test setting pin, not a real code bug fix; the underlying adaptive-timeout optimization is correct for the production use case (low-latency batching with short max_ms). The test's expectation of indefinite queue residence is a regression-test pattern, and adaptive timeout is the wrong setting for it.

Session id: cron:clickhouse-ci-task-worker:20260523-011500

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @CheSema - could you review? Test-only setting pin for the async-insert queue test: 04043 was relying on a 10-minute busy_timeout to keep its entry in system.asynchronous_inserts, but the adaptive busy_timeout shortcut in pushImpl flushes it immediately on idle shards. Setting async_insert_use_adaptive_busy_timeout = 0 keeps the entry queued for the 600 s deadline.

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label May 23, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c22e056]


AI Review

Summary

The PR makes 04043_system_asynchronous_inserts_user_filter.sh deterministic by disabling async_insert_use_adaptive_busy_timeout for the test INSERT, which aligns with the stated root cause of intermittent immediate queue drain on idle shards. The change is narrowly scoped to test behavior, preserves the test’s intended visibility assertions, and I found no unresolved contract violations in the current diff.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label May 23, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 23, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.40% 91.40% +0.00%
Branches 76.50% 76.50% +0.00%

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

Newly covered by added/modified tests: 481 line(s), 26 function(s) across 127 file(s) · Details

Top files
  • src/Storages/ObjectStorage/DataLakes/Iceberg/Utils.cpp: 61 line(s), 1 function(s)
  • src/Common/QueryFuzzer.cpp: 27 line(s), 2 function(s)
  • src/Storages/ObjectStorage/DataLakes/Iceberg/MetadataGenerator.cpp: 23 line(s), 1 function(s)
  • src/Storages/ObjectStorage/DataLakes/Iceberg/IcebergMetadata.cpp: 14 line(s)
  • src/AggregateFunctions/AggregateFunctionWindowFunnel.cpp: 11 line(s), 6 function(s)

Full report

@groeneai

Copy link
Copy Markdown
Collaborator Author

Closing as superseded by master fix c9fe327 ("Fix flaky test 04043_system_asynchronous_inserts_user_filter", 2026-05-25 01:06:58 UTC) by @alexey-milovidov.

The master fix is a strict superset of mine:

  • Same setting pin: async_insert_use_adaptive_busy_timeout = 0
  • Re-adds no-parallel (correct invariant for this test class: it consumes pending state, it does not produce it, so concurrent unscoped SYSTEM FLUSH ASYNC INSERT QUEUE can drain the queued entry)
  • Adds a 30s polling loop over system.asynchronous_inserts for visibility lag on slow sanitizer/S3 builds

CIDB: 0 master failures since c9fe327 (32+ hours clean), down from 4 master failures across 2026-05-22 and 2026-05-23.

Session: cron:clickhouse-ci-task-worker:20260526-024500

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants