Skip to content

Do not throttle local reads that are served from the OS page cache - #118495

Merged
alexey-milovidov merged 17 commits into
masterfrom
dont-throttle-page-cache-reads
Oct 8, 2026
Merged

alexey-milovidov merged 17 commits into
masterfrom
dont-throttle-page-cache-reads

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 7, 2026 •

Copy link
Copy Markdown
Member

max_local_read_bandwidth_for_server and the per-query max_local_read_bandwidth throttled the bytes copied from the OS page cache exactly like the bytes that hit the disk. A query whose data is fully cached in RAM was slowed down to the configured disk bandwidth while producing almost no block device I/O, which makes the setting useless for the purpose it was introduced for (#48242, "do not let ClickHouse exceed what the underlying disk can deliver"): it has to be set at the disk bandwidth or above, and then it protects nothing, or below it, and then it penalizes cached reads that never touch the device.

ThreadPoolReader already knows whether a read was served from the page cache - it tries preadv2 with RWF_NOWAIT first and hands the request off to the thread pool on EAGAIN. This reports that in IAsynchronousReader::Result and skips the throttler for such reads, so the local read throttler now accounts only what was read from the device.

Only the throttlers of max_local_read_bandwidth_for_server and max_local_read_bandwidth are exempted. The throttlers of max_backup_bandwidth, max_merges_bandwidth and max_mutations_bandwidth share the same read path (they are combined into ReadSettings::local_throttler), but they limit the amount of work regardless of where the data came from, so they keep accounting such reads.

This covers the default local_filesystem_read_method = 'pread_threadpool'. The synchronous pread (also used for the files of the filesystem cache by CachedOnDiskReadBufferFromFile and DiskCacheProvider, regardless of the read method) does the same: when there is a throttler, it tries preadv2 with RWF_NOWAIT first and falls through to pread on EAGAIN; such reads are counted in the new ReadBufferFromFileDescriptorPageCacheHitBytes profile event. The read method cannot tell whether the data was cached, and reads with O_DIRECT always reach the device, so both keep being accounted as before.

Measured on a 177 MiB scan of a table that is fully in the page cache, with max_local_read_bandwidth = 20000000 (20 MB/s):

read method QueryLocalReadThrottlerBytes QueryLocalReadThrottlerSleepMicroseconds query_duration_ms
pread_threadpool (before) 177 MiB 600 s (summed over threads) 7860
pread_threadpool (after) 0 0 15
pread_threadpool + O_DIRECT (unchanged) 177 MiB 604 s 7909

Closes: #118463
Related: #48242

Changelog category (leave one):

  • Improvement

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

Reads that are served from the OS page cache no longer consume the budget of the local read bandwidth throttler (max_local_read_bandwidth_for_server and max_local_read_bandwidth), so a query over cached data is not slowed down to the configured disk bandwidth. This works with the default local_filesystem_read_method = 'pread_threadpool', with pread, and for the files of the filesystem cache; the read method cannot detect cached reads and is accounted as before.


Workflow [PR]
Sync PR [sync-upstream/pr/118495]

Version info

  • Merged into: 26.10.1.1875-master (included in 26.10 and later)

`max_local_read_bandwidth_for_server` and the per-query `max_local_read_bandwidth`
exist to keep the load that ClickHouse puts on the block devices within a budget,
but they accounted the bytes copied from the OS page cache exactly like the bytes
that hit the disk. A query whose data is fully cached in RAM was slowed down to the
configured disk bandwidth while producing almost no block device I/O, which makes
the setting useless for its purpose: it has to be set at the disk bandwidth or above
(and then it protects nothing), or below it (and then it penalizes cached reads).

`ThreadPoolReader` already knows whether a read was served from the page cache - it
tries `preadv2` with `RWF_NOWAIT` first and falls back to the thread pool on `EAGAIN`.
Report that in `IAsynchronousReader::Result` and skip the throttler for such reads.
This covers the default `local_filesystem_read_method = 'pread_threadpool'`; the
synchronous `pread` methods cannot tell whether the data was cached, and reads with
`O_DIRECT` always reach the device, so both keep being accounted.

Closes: #118463
`02703_max_local_read_bandwidth` and `test_throttling` read data that they have
just written, so it is in the OS page cache and is no longer accounted by the
local read throttler when the default `pread_threadpool` read method is used.
Read with `pread`, which cannot tell a cached read from a device read, and cover
the `pread_threadpool` accounting in `05111_local_read_throttler_page_cache`.
@mintlify

mintlify Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated
ClickHouse-docs 🟢 Ready View Preview Oct 8, 2026, 5:40 AM

@clickhouse-gh

clickhouse-gh Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [48e5e66]

Summary: ✅


AI Review

Summary

This PR teaches the local-read throttlers to stop charging bytes that were served from the OS page cache, covering the default pread_threadpool path, synchronous pread, and filesystem-cache file reads while keeping merge/mutation/backup throttlers accounting as before. I re-checked the current diff together with every prior review thread and did not find any remaining correctness, performance, or compatibility issues in the updated code.

Final Verdict

✅ No new findings. The previously raised review issues appear to be addressed in the current revision.

LLVM Coverage Report

Measured on commit 48e5e66.

Metric Baseline Current Δ
Lines 88.30% 88.30% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 98/102 (96.08%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Sep 7, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-scheduler Workload scheduler and resource management: CPU/IO/memory scheduling, concurrency control, worklo... label Sep 7, 2026
alexey-milovidov and others added 2 commits September 11, 2026 10:00
…cache-reads

# Conflicts:
#	src/Core/ServerSettings.cpp
`ReadSettings::local_throttler` is not always the throttler of
`max_local_read_bandwidth`: `BackupsWorker` and `MergeTreeSequentialSource`
combine it with the throttlers of `max_backup_bandwidth`,
`max_merges_bandwidth` and `max_mutations_bandwidth` through
`ThrottlerArray`. Skipping the whole composite throttler for the data that
came from the OS page cache disabled those limits as well, which broke
`02704_max_backup_bandwidth` - a backup of a freshly inserted table was not
throttled at all.

Only the throttlers created by `Context::getLocalReadThrottler` model the
bandwidth of the block device, so mark them with
`Throttler::setLimitsBlockDeviceBandwidth` and route the page cache reads
through the new `IThrottler::throttleOSPageCacheRead`, which skips exactly
those and accounts the read in all the others.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 48e5e665f with master cd023af83 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

1990 translation units recompiled, 12214 s compile time in total, 1990 of them have a recent master baseline.

Job report

@serxa serxa mentioned this pull request Sep 19, 2026
24 of 33 tasks
alexey-milovidov and others added 2 commits September 23, 2026 10:43
…hrottler

The test required `QueryLocalReadThrottlerBytes` to be exactly
`ThreadPoolReaderPageCacheMissBytes`, but the query also reads small metadata
files synchronously with the `read` method (which cannot tell a cached read
from a device read, so it is throttled), and a prefetched buffer can be
discarded unconsumed. Reproduced locally with the randomized settings: one
4096-byte synchronous read made the throttler count 4096 bytes with zero
thread pool misses. Check instead that the throttler accounts at most the
bytes that were not page cache hits.

The test cannot pass on macOS, where there is no `preadv2` with
`RWF_NOWAIT`, so mark it `no-darwin`.

Also address the review: `test_local_read_throttling_reload` now checks that
a `pread_threadpool` scan of cached data is not throttled by the
`max_local_read_bandwidth_for_server` throttler created on config reload.

CI: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118495&sha=816d62734cc54d0ed78a622bb3643de7da03a028&name_0=PR
PR: #118495

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
alexey-milovidov and others added 2 commits October 2, 2026 10:47
# Conflicts:
#	src/Interpreters/Context.cpp
`ThreadPoolReader` reports `ThreadPoolReaderPageCacheHitBytes` only when
`preadv2` with `RWF_NOWAIT` is usable. On old kernels, under `seccomp`
profiles, or on filesystems rejecting the flag, every read falls back to
`pread`, so requiring page cache hits made the tests fail on supported
environments. Keep only the accounting invariant in
`05111_local_read_throttler_page_cache`, and check the duration in
`test_local_read_throttling_reload` only when page cache hits happened.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…age cache

The files of the filesystem cache (`CachedOnDiskReadBufferFromFile` and
`DiskCacheProvider`) are always read with `LocalFSReadMethod::pread`, so a
query served from a hot cache file still consumed the tokens of
`max_local_read_bandwidth` and was slowed down to the disk speed.

`ReadBufferFromFileDescriptor` with `pread` now tries `preadNoWait` first
when there is a throttler, the file is not opened with `O_DIRECT`, and the
system call is usable, and reports such reads with `throttleOSPageCacheRead`.
This covers the cache files and `local_filesystem_read_method = 'pread'`;
the `read` method still cannot tell a cached read from a device read.
The new `ReadBufferFromFileDescriptorPageCacheHitBytes` profile event counts
the detected bytes.

The throttling tests that relied on `pread` being throttled for cached data
now use `read`, and `05317_local_read_throttler_filesystem_cache` checks the
accounting for a warmed-up filesystem cache.

#118495 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread src/IO/ReadBufferFromFileDescriptor.cpp Resolved
alexey-milovidov and others added 2 commits October 4, 2026 20:10
…re its reads

`ReadSettings::local_throttler` can also carry `max_merges_bandwidth`,
`max_mutations_bandwidth` and `max_backup_bandwidth` throttlers, which account
the reads served from the OS page cache as usual. For them the `preadv2` with
`RWF_NOWAIT` probe was an extra system call on every page cache miss without
any effect on throttling. Add `IThrottler::ignoresOSPageCacheReads` (true if
the throttler, one of its parents, or one of the throttlers of a
`ThrottlerArray` limits the block device bandwidth) and enable the detection
only when it returns true.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118495&sha=ed580b74f8c4042e7540fb9a2141f40d56437854&name_0=PR&name_1=Fast%20test and provide a fix in a separate PR. If the fix is already in progress, link it here. The .expect tests 05210_client_progress_throttler_waited, 05212_client_progress_history_under_annotation and 05218_client_progress_history_while_bar_hidden time out after 60 s in Fast test. They test the client progress bar, which this PR does not touch, and they fail the same way in other PRs (#123643, #116577). I merged master into this branch to re-run CI.

@groeneai

groeneai commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator

Acknowledged. I'm investigating the Fast test timeouts of 05210_client_progress_throttler_waited, 05212_client_progress_history_under_annotation and 05218_client_progress_history_while_bar_hidden; I'll follow up here with a PR (or my findings) shortly.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Thanks @groeneai — the analysis is right: the four throttled progress-bar .expect tests relied on cached reads being throttled, which this PR intentionally stops doing. #124104 (switching them to local_filesystem_read_method = 'read') is merged, and I merged master into this branch to pick it up and re-run CI.

Comment thread src/Common/Throttler.h Resolved
alexey-milovidov and others added 3 commits October 7, 2026 01:44
With the randomized `use_columns_cache = 1`, the measured query of
`05317_local_read_throttler_filesystem_cache` is served from the columns cache
and does not read from the filesystem cache at all, so the check
`CachedReadBufferReadFromCacheBytes > 0` failed.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118495&sha=fd1b31c7daaec94c8090e5fb5c199ba6a2883831&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20flaky%20check%29
#118495

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…hout a limit

`Context::reloadLocalThrottlerConfig` keeps the server local read throttler
after `max_local_read_bandwidth_for_server` is reloaded back to 0 and only sets
its speed to 0. `ignoresOSPageCacheReads` kept returning true for it, so the
synchronous `pread` (including the files of the filesystem cache) kept paying
for an extra `preadv2` with `RWF_NOWAIT` although nothing was limited anymore.
Now it requires an active speed limit. Covered in `test_local_read_throttling_reload`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118495&sha=98758e9e93a9215d0dbb534feac1d292653670dd&name_0=PR&name_1=Fast%20test%20(arm_darwin) and provide a fix in a separate PR. If the fix is already in progress, link it here. 03826_calculate_subcolumn_size_during_merge_tree_read failed with NOT_ENOUGH_SPACE (Cannot reserve 380.45 MiB, not enough space) on the macOS runner, which looks like the runner disk filling up (possibly related to #113605).

@groeneai

groeneai commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

This job ran on ip-10-0-47-138: / was 97% used (3.6G free) at start and 99% (1.6G free) at the end. 03826 is a victim, not the cause: its insert ... from numbers(1000000) has to reserve 380 MiB on a disk that is already full.

It is the same class as on #122362 (#122362 (comment)), now on a third host that is not recycling: in the job samples it was at 42G used on 2026-09-29, 48G on 10-04, 77-80G on 10-06, 97G today, and never dropped back to the ~43G post-reboot level.

The fix is #113605: it runs the free-space check before every job on macOS too, so a host this full refuses the job and reboots instead of taking it. It is waiting for review and deploy by @ leshikus. #124316 is the diagnostic that prints what fills the disk on such a host.

Comment thread src/IO/ReadBufferFromFileDescriptor.cpp Resolved
alexey-milovidov and others added 2 commits October 8, 2026 04:49
…s `RWF_NOWAIT`

On filesystems that reject `RWF_NOWAIT` for a real file descriptor (e.g. `tmpfs`
answers `EOPNOTSUPP`), the synchronous `pread` path did a failing `preadNoWait`
before every `pread`. Turn the detection off after the first such error.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118495&sha=7bd5866a7c611e5c932e93d17ac9da36d4dc0203&name_0=PR&name_1=Unit%20tests%20%28asan_ubsan%2C%20function_prop_fuzzer%29 and provide a fix in a separate PR. If the fix is already in progress, link it here. FunctionsStress.stress reported a stuck thread executing SELECT hopEnd(CAST('1970-01-01 01:02:07' AS DateTime), CAST(413844 AS IntervalQuarter), CAST(413844 AS IntervalQuarter)) (FunctionsTimeWindow.cpp, which this PR does not touch); that single query returns immediately in a release build, so the hang probably needs the fuzzer's column inputs. I also addressed the review thread and merged master.

@groeneai

groeneai commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator

Acknowledged. I'm investigating the FunctionsStress.stress stuck thread in hopEnd (FunctionsTimeWindow.cpp); I'll follow up here with a PR (or my findings) shortly.

Repro on master (debug build of 44b568f): SELECT hopEnd(toDateTime(0, 'UTC'), toIntervalQuarter(413844), toIntervalQuarter(413844)) never returns, while the printed timestamp (toDateTime(3727, 'UTC')) returns immediately. toIntervalMonth(100000) hangs the same way.

@groeneai

groeneai commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator

Fix: #124570.

addQuarters saturates at 0000-01-01, which is day 1368 once narrowed to UInt16, and subtracting the hop from 1368 saturates to the same value. The window end never changes, and the check from #114607 only caught an end that grows. It now requires a strict decrease and throws Time overflow otherwise. The release build returned because the test image prints the DateTime in Europe/Amsterdam: the raw time is 127 (below 1368, so it loops); in UTC it is 3727, which does not loop.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Thanks @groeneai — the hopEnd hang is a pre-existing bug in FunctionsTimeWindow.cpp, unrelated to this PR, and #124570 tracks the fix. CI on the current head (48e5e665) is fully green and there are no unresolved review threads, so no further changes are needed here.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reasonable.

@alexey-milovidov alexey-milovidov self-assigned this Oct 8, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 8, 2026
Merged via the queue into master with commit cb0c711 Oct 8, 2026
152 checks passed
@alexey-milovidov
alexey-milovidov deleted the dont-throttle-page-cache-reads branch October 8, 2026 23:04
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 8, 2026
PedroTadim pushed a commit to clickgapai/ClickHouse that referenced this pull request Oct 9, 2026
…e the window end

`executeHop` finds the window end by subtracting the hop until the end is not
after the time. ClickHouse#114607 made the loop throw when the subtraction makes the end
larger (a wrap around zero), but not when it leaves the end unchanged, and then
the loop never exits and cannot be cancelled.

That happens for `Date` interval kinds when the bound saturates at the end of
the calendar: `addQuarters`/`addMonths`/`addYears` clamp to `0000-01-01`
(ExtendedDayNum -719528), which narrows to day 1368 in `UInt16`, and subtracting
the hop from 1368 saturates to the same value again. The loop condition compares
that day number with the time in seconds, so it stays true for every time below
1368 seconds. It also happens for time kinds when the span of the hop is a
multiple of 2^32 seconds (a no-op in the wrapping `UInt32` arithmetic) and the
window start computed by `ToStartOfTransform` is after the time, e.g. local
midnight 39600 in `US/Samoa` with `toIntervalDay(33554432)`.

The loop terminates only if every iteration strictly decreases the end, so
require exactly that and throw the existing `Time overflow` error otherwise.
Subtracting a positive hop from a representable value always decreases it; the
only other inputs affected returned a window that does not contain the time.

Found by `FunctionsStress` in `Unit tests (asan_ubsan, function_prop_fuzzer)` on
PR ClickHouse#118495, query `hopEnd(CAST('1970-01-01 01:02:07' AS DateTime), ...)` with the
test image time zone `Europe/Amsterdam`, i.e. raw time 127.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickgapai

Copy link
Copy Markdown
Contributor

@alexey-milovidov — ClickGap found the following in this PR:

Close anything that's wrong or already addressed.

This branch was successfully deployed

1 active deployment
staging - docs — 48e5e665 Deployed Oct 8, 2026 by mintlify[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-scheduler Workload scheduler and resource management: CPU/IO/memory scheduling, concurrency control, worklo... pr-improvement Pull request with some product improvements 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.

max_local_read_bandwidth_for_server throttles reads served from the OS page cache

4 participants