Repository navigation
Always apply filesystem_prefetches_limit - #83999
Merged
Merged
Conversation
Previously it was applied only from MergeTreePrefetchedReadPool, which was doing initial prefetch, now it will be respected for all reads.
Contributor
|
Workflow [PR], commit [3883662] Summary: ❌
|
…TreePrefetchedReadPool
kssenii
reviewed
Jul 21, 2025
kssenii
approved these changes
Jul 21, 2025
Member
Author
|
azat
enabled auto-merge
July 21, 2025 15:41
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Previously it was applied only from MergeTreePrefetchedReadPool, which was doing initial prefetch, now it will be respected for all reads.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Always apply
filesystem_prefetches_limit(not only fromMergeTreePrefetchedReadPool)Note: it has some changes in private as well