Repository navigation
Do not wait for unused Parquet reads after LIMIT or a cancellation when reading from object storage - #127624
Do not wait for unused Parquet reads after LIMIT or a cancellation when reading from object storage#127624groeneai wants to merge 8 commits into
LIMIT or a cancellation when reading from object storage#127624Conversation
Reading Parquet from object storage, the reader starts reads of the row groups beyond the first one. After a satisfied `LIMIT`, the reader teardown (`ReadManager::shutdownTasks`) waited for those reads: it drained the decode tasks first, which waited for the reads, and the reads passed no progress callback to `readBigAt`, so nothing stopped them. Through a throttled connection that is seconds (7.1 s against 33 ms for the same rows read with one parsing thread, ClickHouse#127592). A cancellation (`max_execution_time`, `KILL QUERY`, a client cancel) did not reach the nested reader of `StorageObjectStorageSource` while its `pull` waited for a decoded chunk, so the query ended only after a whole read had completed, and then waited in the teardown as well. - Prefetcher reads pass a progress callback that stops them once the prefetcher's shutdown is requested, and reads that have not started fail at once. `ReadManager::shutdownTasks` requests it before waiting for the decode tasks; the waits keep their order, so no task reads through the buffer after `shutdownTasks` returns. - `ReadManager::cancel` makes `read` return the end of data instead of an exception, so a forwarded cancellation never becomes an error of its own and the query reports its own reason (159, 394, 735). - `IInputFormat::supportsCancellationDuringRead` (Parquet, and the `AggregateFunctionStatesFromValuesInputFormat` decorator in front of it) lets `StorageObjectStorageSource::onCancel` cancel the format of the current file from another thread. A read that was cancelled may have ended early, so the source caches nothing about the file then (row count, query condition cache). - `ParquetV3BlockInputFormat::initializeIfNeeded` holds `reader_mutex` only around the emplace, and applies a cancellation that came before it, so a cancel is neither lost nor kept waiting by the footer read. Not covered: an in-flight read through the userspace page cache, Azure, HDFS or a buffer without positional reads completes as before, and the footer reads are not interrupted. Closes ClickHouse#127592 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Write method names as `f` instead of `f()` in the added comments, drop a redundant line on `ReadManager::shutdownTasks`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 4 findings) plus my own review.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261023-172100 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20261023-144400 |
|
cc @al13n321 @scanhex12, could you review this and add the |
|
Workflow [PR], commit [68b6629] Summary: ✅
AI ReviewSummaryReviewed #127624 at Missing context / blind spots
Final VerdictStatus: ✅ Approve |
…thread With `max_parsing_threads = 1` the manual executor runs the decode tasks inside `ReadManager::read`, in the query thread. A decode task waiting in `Prefetcher::getRangeData` for a remote read could not be woken up by `ReadManager::cancel`, which only set `cancelled` and notified `delivery_cv`; the prefetcher's reads were stopped only by `shutdownTasks`, after the inline task had returned. So a timed out or killed query still waited for the whole range read in progress: 5.4 s after a `max_execution_time = 2` deadline for a 16 MiB row group at 2 MiB/s. `cancel` now also stops the prefetcher's reads, once `init` has read the file metadata (reading the metadata is still not interrupted, as documented in `IInputFormat::supportsCancellationDuringRead`). The tasks failed by the stopped reads record an exception, so `read` checks `cancelled` before `exception` and still returns end of data: the query keeps its own error (159 instead of 394 from the stopped read). The test gets the same timed out query with one parsing thread through a 2 MiB/s connection; it fails without this change (next statement 7.4 s after the start instead of at most 4.5 s). Its `s3` call now names the structure, so the request-count premise does not depend on schema inference. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Fixes the `Build (arm_tidy)` failure `cppcoreguidelines-init-variables` on `ReadManager.cpp:195`. The variable was always assigned under `delivery_mutex` before use, so the behavior does not change. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units167 translation units recompiled, 1138 s compile time in total, 167 of them have a recent master baseline. |
CI finish ledger - ea071f2Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no other Session id: cron:our-pr-ci-monitor:20261024-013003 |
…for-unused-reads # Conflicts: # src/Processors/Formats/Impl/Parquet/ReadManager.h # src/Processors/Formats/Impl/ParquetV3BlockInputFormat.cpp # src/Storages/ObjectStorage/StorageObjectStorageSource.cpp
|
@groeneai this PR also removes the
|
|
@groeneai also merge latest master, and resolve conflicts |
…for-unused-reads # Conflicts: # src/Processors/Formats/Impl/Parquet/ReadManager.cpp # src/Processors/Formats/Impl/Parquet/ReadManager.h # src/Storages/ObjectStorage/StorageObjectStorageSource.cpp # src/Storages/ObjectStorage/StorageObjectStorageSource.h
Master now randomizes the Parquet reader and writer settings in stateless tests. Under some of the values the test's premise does not hold: - `input_format_parquet_max_block_size` 1000 or 8192, `input_format_parquet_prefer_block_bytes` 65536 and `input_format_parquet_memory_low_watermark` 64 MiB make the one-thread control read all row groups before its first block (3-7 s, also on master); - `input_format_parquet_memory_high_watermark` 1 MiB or less leaves no background reads to stop; - a smaller `output_format_parquet_row_group_size_bytes` splits the row groups, and without a dictionary and compression row group 0 is 16 MiB like the others instead of 5 KiB. Clamp them with a `Random settings limits:` line. Before: 1 of 1 and 5 of 24 randomized runs failed; after: 32 of 32 pass, and the test still fails on master. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Done: master is merged in 4497880 (conflicts in
One more change: master now randomizes the Parquet reader and writer settings in tests, and some values broke the premise of the test. For example, with |
|
📊 Cloud Performance Report ✅ AI verdict: no significant changes detected. K_source=4 K_base=30 flagged=0/65 clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
The test writes 10M UInt64 values, 8M of them random hashes, into one Parquet file. With the randomized writer settings, 1 run in 8 compresses them with brotli. In `Stateless tests (amd_msan, flaky check)` the 8 slowest of 50 runs were exactly the brotli runs (CI query_log): their INSERT took 26-205 s, and one run took 218 s in total, over the 180 s limit. Every other codec, data page size and batch size kept a run under 36 s. Measured on this PR's msan binary, one copy: the INSERT costs 3.9 s CPU with zstd, 17.7 s with brotli, and 56.2 s with brotli plus 1-byte data pages and 100-row batches (the failed run's draw). The codec does not affect what the test checks, so it is pinned to the default `zstd`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #127592
Related: #131938
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Queries that read Parquet from object storage no longer wait for the background reads of row groups they do not need once a
LIMITis satisfied or the query is cancelled (max_execution_time,KILL QUERY, client cancel): these reads are now stopped. For example,SELECT * FROM s3(...) LIMIT 2through a throttled connection finishes in under a second instead of several seconds.Description
SELECT * FROM s3(<file>, 'Parquet') LIMIT 2over a throttled connection took 7.1 s against 33 ms with one parsing thread, and a read stopped bymax_execution_time = 2ended about 6 s after the deadline (#127592;KILL QUERYand Ctrl+C likewise).The reader teardown (
ReadManager::shutdownTasks) waited for the decode tasks before stopping the prefetcher, whose reads passed no progress callback toreadBigAt, so the 16 MiB reads of unused row groups ran to the end. And a cancellation did not reach the nested reader ofStorageObjectStorageSourcewhile itspullwaited for data.Changes:
shutdownTasksrequests it before waiting for the decode tasks.ReadManager::cancelmakesreadreturn the end of data instead of throwing, so the query reports its own error (159, 394, 735 unchanged), and stops the prefetcher's data reads, whichreaditself waits for withmax_parsing_threads = 1. It no longer builds anException(QUERY_WAS_CANCELLED)with a stack trace for every file, also one read to the end (proposal 4 of Overview: per-file overhead when reading many small cached Parquet files of Iceberg tables (reader setup,INset probes, query-level format settings) #131938).StorageObjectStorageSource::onCancelcancels the current file's format when it supports that (IInputFormat::supportsCancellationDuringRead: Parquet and theaggregate_function_input_formatwrapper), and a cancelled read caches nothing about the file.Validation: the issue's script on release builds, base vs fix: A1 7130-7135 vs 17-633 ms, A3
end_ms - 20006031-6139 vs 493-599 ms; one parsing thread at 2 MiB/s (debug): 5.4 s without stopping the reads incancel, 0.4-0.5 s with it. Over 4,000 small cached files (debug),ReadManager::cancelis in 1.3-1.8% of the CPU samples on master and in none of 2,150 here. The new test fails on master and passes 32/32 randomized runs.Not covered: in-flight reads through the userspace page cache, Azure, HDFS or a non-positional buffer and footer reads still complete.
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/127624]