Avoid holding the async-insert shard lock during pool admission - #118352
Avoid holding the async-insert shard lock during pool admission#118352VrtxOmega wants to merge 2 commits into
Conversation
Track detached batches through their lifetime so a global forced flush waits for work awaiting pool admission. Add deterministic saturation and handoff regression coverage. Related: ClickHouse#117927
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
TL;DR: the global-flush guarantee looks sound (the counter is published under the shard mutex before queue erasure, and the decrement in
|
Clarify that removed batches are outside the table-specific wait set, including batches waiting for pool admission. The shard-wide in-flight counter also covers unrelated tables. Related: ClickHouse#118352 (comment)

When async-insert workers are saturated, a producer can wait for pool admission while holding a queue-shard mutex. Other inserts sharing that shard then block even when they only need to buffer data. Move pool admission outside the shard lock while retaining backpressure on the submitting producer.
Track batches removed by producers and deadline workers until their data is destroyed. This allows
SYSTEM FLUSH ASYNC INSERT QUEUEto wait for batches that have already left the shard queue but have not yet entered the worker pool, without holding shard locks during that wait.Add a single-worker integration fixture with eight parameterized cases for saturation, global/table-specific flushes, both
wait_for_async_insertmodes, size/deadline batch removal, and parse-error cleanup. A new failpoint makes the gap before pool admission observable.Local validation used Debug builds with failpoints enabled, Clang 22.1.8, and base
58d0e8cb852acbd70e3207cf297fb2d4e26f88c8:flush_on_shutdownenabled and disabled, verifying graceful termination, retained/discarded buffered rows, and pending-insert cleanup.test_async_insert_memory::test_memory_usageintegration test passed.A separate synthetic probe issued 16 unrelated inserts while one flush worker was deliberately paused. Before releasing that worker, the original completed 0/16 and the patched build completed 16/16. Median latency was 2316.75 ms versus 7.67 ms. The forced pause contributes to these timings; this small experiment establishes progress under saturation, not production throughput or an overall memory bound. Sanitizer coverage and broader workload characterization remain outside this validation.
Closes: #117927
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Prevent asynchronous inserts that only need to buffer data from being blocked by another insert waiting for a flush worker while holding their shared queue lock. Ensure a global forced flush waits for batches removed from the queue that are still waiting for a worker.