Skip to content

Always apply filesystem_prefetches_limit - #83999

Merged
azat merged 9 commits into
ClickHouse:masterfrom
azat:better-prefetches-limits
Jul 21, 2025
Merged

azat merged 9 commits into
ClickHouse:masterfrom
azat:better-prefetches-limits

Conversation

@azat

@azat azat commented Jul 18, 2025 •

Copy link
Copy Markdown
Member

Previously it was applied only from MergeTreePrefetchedReadPool, which was doing initial prefetch, now it will be respected for all reads.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Always apply filesystem_prefetches_limit (not only from MergeTreePrefetchedReadPool)

Note: it has some changes in private as well

Previously it was applied only from MergeTreePrefetchedReadPool, which
was doing initial prefetch, now it will be respected for all reads.
@azat
azat requested a review from kssenii July 18, 2025 16:49
@clickhouse-gh

clickhouse-gh Bot commented Jul 18, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3883662]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_binary, ParallelReplicas, s3 storage, parallel) failure
03376_bloom_filter_has_const_array FAIL
Stateless tests (amd_debug, sequential) failure
02241_filesystem_cache_on_write_operations FAIL
Stateless tests (amd_tsan, s3 storage, parallel) failure
00002_log_and_exception_messages_formatting FAIL
Scraping minio_audit_logs FAIL
Upgrade check (amd_debug) failure
S3_ERROR No such key thrown (see clickhouse-server.log or no_such_key_errors.txt) FAIL
BuzzHouse (amd_ubsan) failure
Task failed: $?=1 FAIL

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 18, 2025
@kssenii kssenii self-assigned this Jul 18, 2025
@azat

azat commented Jul 21, 2025

Copy link
Copy Markdown
Member Author

BuzzHouse (amd_ubsan) — Task failed: $?=1

  • reported

Stateless tests (amd_binary, ParallelReplicas, s3 storage, parallel) — Failed: 1, Passed: 6723, Skipped: 764

  • 03376_bloom_filter_has_const_array - will be reverted

Stateless tests (amd_debug, sequential) — Failed: 1, Passed: 620, Skipped: 22

Stateless tests (amd_tsan, s3 storage, parallel) — Failed: 1, Passed: 7314, Skipped: 307

  • Hm, client got stopped by some reason, I don't see any gdb attached

@azat
azat enabled auto-merge July 21, 2025 15:41
@azat
azat added this pull request to the merge queue Jul 21, 2025
Merged via the queue into ClickHouse:master with commit 57b27de Jul 21, 2025
@azat
azat deleted the better-prefetches-limits branch July 21, 2025 17:17
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 21, 2025
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Sep 10, 2026
filesystem_prefetches_limit and filesystem_prefetch_max_memory_usage were enforced
where prefetching is decided, in units that are not the unit that consumes memory.
MergeTreePrefetchedReadPool's admission counts required_readers_num, which
update_stat_for_column increments once per column, and MergeTreeReaderWide compared the
limit against num_columns. One JSON column expands into ~122 prefetched substreams, so
both tests compared the limit against 1, while the reader allocated one ~1 MiB read
buffer per substream.

The two enforcement points also compose multiplicatively: the pool admits up to
`limit` tasks and each reader may then issue up to `limit` prefetches of its own, so
`limit * limit` buffers can be alive with every individual check satisfied. A per-reader
counter cannot see that, which is why this replaces the one added in
f9b1245 rather than tightening it.

Add MergeTreePrefetchBudget, one shared count-and-bytes budget per read step, created in
MergeTreeReaderSettings::createForQuery and carried by the reader settings that every
read pool copies into its tasks. A reservation is an RAII slot held by the
MergeTreeReaderStream that owns the buffer, so it is returned when that buffer is
destroyed and never earlier: the slot is declared before read_buffer_holder precisely so
member teardown destroys the buffer first. ~AsynchronousBoundedReadBuffer blocks waiting
for an in-flight prefetch, so releasing before that wait would let another reader take
the capacity while the memory is still held.

The charge is once per stream rather than once per prefetch() call. Consuming a prefetch
moves the allocation into the read buffer (prefetch_buffer.swap(memory)) rather than
freeing it, so a stream allocates one prefetch buffer however many times it is
prefetched; charging per call would instead exhaust the budget after the first granule
batch, since prefetched_streams is cleared at the end of every readRows and every
substream is prefetched again on the next one. That also removes the three
issued_prefetches resets, which handed budget back while the buffers stayed allocated.

The byte figure has to come from the buffer, not from the reader settings: what
prefetch() allocates is min(remote_fs_settings.buffer_size, object size), the page cache
replaces it with page_cache_block_size, and DiskObjectStorage::prepareRead can raise it
to large_buffer_size under filesystem_cache_prefer_bigger_buffer_size. Deriving it from
settings would undercount by up to 8x, the unsafe direction for a memory budget. Hence
ReadBuffer::prefetchBufferSize, a narrow query beside prefetch() itself, overridden in
the classes a MergeTree part-read chain resolves to, including
CachedCompressedReadBuffer (whose file_in is created lazily and is still null when the
reservation is decided) and ReadBufferFromEncryptedFile (whose absence would silently
zero the byte accounting on every encrypted disk). The page-cache path reports zero
because it allocates nothing of its own.

The reader-side num_columns early-out from ClickHouse#83999 is deleted: it made a read of more
columns than the limit prefetch nothing at all, where the budget now prefetches the
first N. The pool's admission is left exactly as it is, as a coarse pre-filter that
avoids creating readers eagerly. A refused reservation means a synchronous read, never a
failure, and results are unchanged. Merges, mutations and part maintenance leave the
budget null, which preserves today's behaviour: filesystem_prefetches_limit is 0 on
those paths, so the deleted early-out never bounded them either.

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

pr-bugfix Pull request with bugfix, not backported by default 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.

3 participants