Repository navigation
Use pread when preadv2 with RWF_NOWAIT cannot be used, and recognize EPERM from it - #112945
Conversation
…d` then The `pread_threadpool` read method hands every read off to a thread pool, unless the data is already in the page cache, which it checks with the `preadv2` system call and the `RWF_NOWAIT` flag. Two things can go wrong with that check, and both were handled badly. `EPERM` - what a `seccomp` profile of a container runtime answers for a system call that is not in its allow list - was not recognized as the system call being unavailable. Instead of handing the read off to the thread pool, as it does for `ENOSYS` and `EOPNOTSUPP`, the read failed the query with `CANNOT_READ_FROM_FILE_DESCRIPTOR`. And when the check cannot be used at all - the kernel is older than 5.11, where `RWF_NOWAIT` can report the end of the file instead of the data, or the system call is blocked - `pread_threadpool` kept paying for a thread pool hand-off on every read, including the reads that only had to copy the data from the page cache. The system call is now probed once, before it is used. An invalid file descriptor is passed to it on purpose: `seccomp` filters and the system call table are consulted before the descriptor is looked up, so an available system call answers `EBADF` without reading anything. When it is not available, `local_filesystem_read_method = 'pread_threadpool'` is treated as `'pread'`, which reads the same data in the calling thread, and the server explains why in `system.warnings` on startup. Reads with `O_DIRECT` never look at the page cache and keep using the thread pool. The `preadv2` system call and the knowledge about when it is usable moved to `preadNoWait`, so that the reader and the read method selection agree on it. Related: #104634 Related: #49149 Related: #39753 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [1a8beb0] Summary: ✅
AI ReviewSummaryThis PR changes the default behavior of Final Verdict✅ No remaining findings from this review pass. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 77/102 (75.49%) · Uncovered code |
…pareRead` `DiskLocal::prepareRead` disables the userspace page cache for the read methods that are incompatible with it, and it branched on the requested `local_fs_settings.method`. On a system where `preadNoWait` cannot be used, `pread_threadpool` reads with `pread`, which is compatible with the page cache, but the check still saw `pread_threadpool` and turned the page cache off. It now resolves the method with `resolveLocalFSReadMethod` first, the same way `createReadBufferFromFileBase` does, using the same estimated size basis to tell whether the read will use `O_DIRECT`. Also add braces around `__msan_unpoison`: it expands to nothing outside of an MSan build, so `clang-tidy` reported the `if` statement as `bugprone-suspicious-semicolon` in the `arm_tidy` build.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Object file sizes5 object files changed (+23.86 KiB total), 1 added.
737 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units10 translation units recompiled, 24 s compile time in total, 9 of them have a recent master baseline. |
…ead method On a system where `preadNoWait` is unusable, `pread_threadpool` reads with `pread`, so `ReadFromMergeTree` must not enable `MergeTreePrefetchedReadPool` from the raw requested method. Co-Authored-By: Claude Fable 5 <[email protected]>
The downgrade applies only to `local_filesystem_read_method = 'pread_threadpool'` and only to reads without O_DIRECT; say so in the warning. Co-Authored-By: Claude Fable 5 <[email protected]>
|
🕵 The |
…s as `prepareRead` `DiskLocal::prepareRead` resolved `direct_io` (and with it the effective read method and the page cache decision) from `read_hint.value_or(file_size)`, while the legacy build path in `ReadPipeline` called `createReadBufferFromFileBase` with the hint only. For a null-hint read of a large file with `min_bytes_to_use_direct_io` set, `prepareRead` kept `pread_threadpool` and disabled the userspace page cache, while the buffer was opened without `O_DIRECT` and resolved to `pread`. The stored object already carries the stat'ed size, so pass it to the builder, and treat a failed stat as size zero in `prepareRead` (the builder sees `UnknownSize` then), so both sides resolve from the same value. Co-Authored-By: Claude Fable 5 <[email protected]>
…` reads with `pread` `02910_prefetch_unexpceted_exception` and `04640_query_log_map_columns_serialization` assert side effects of the local prefetched read pool: a failpoint inside `MergeTreePrefetchedReadPool` and the `asynchronous_read_counters` keys in `system.query_log`. macOS has no `preadv2`, so `pread_threadpool` is now treated as `pread` there and the pool never runs for local reads - the intended behavior of this change - so the two tests cannot pass on Darwin. The tests keep full coverage on Linux. Failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=112945&sha=aee807c7c843036e7d38ae48b4ecef4df738628b&name_0=PR&name_1=Fast%20test%20%28arm_darwin%29 PR: #112945 Co-Authored-By: Claude Fable 5 <[email protected]>
…lly requested A `seccomp` profile that kills the process on unknown system calls must not see the probe from read methods that do not depend on it. The resolution now goes through a `resolveLocalFSReadMethod` overload that short-circuits before `getPreadNoWaitSupport` unless the requested method is `pread_threadpool`, and the startup warning in `sanityChecks` is emitted only when the default profile actually selects `pread_threadpool`.
|
🕵 Triage of the CI failures on 5edbad2:
@groeneai, investigate the two stateless failures above and provide fixes in separate PRs if they are real bugs (the client segfault on |
|
All three are real, and all three now have an owner. The first one is not #111677, which changes its
One correction to the triage: the randomized draw also had A fix task is created for this half, at full effort. I will post the fixing PR link here when it opens. |
azat
left a comment
There was a problem hiding this comment.
Please look at the comments, and also I would suggest to squash everything to a single commit.
|
The fixing PR for the It fixes the stale-read at the settings layer: One thing worth flagging for your own PR, because it changes the reachability of this bug and I only Your branch changes that. That does not make the bug yours: the stale read is pre-existing and the size pass-through is a |
…ize basis as `prepareRead`" This reverts commit 5b23637.
…alFSReadMethod` comment Co-Authored-By: Claude Fable 5 <[email protected]>
…s enabled The prefetched-pool gate in `ReadFromMergeTree` resolved 'pread_threadpool' with `direct_io = false`, so on a host without `preadNoWait` it rejected `MergeTreePrefetchedReadPool` even for queries whose reads are forced onto O_DIRECT by `min_bytes_to_use_direct_io`, although those reads keep the thread pool. Resolve with `direct_io = true` while `direct_io_threshold` is set, so such queries keep the pool; when direct IO is disabled entirely, the downgrade to 'pread' still disables the pool as before. Co-Authored-By: Claude Fable 5 <[email protected]>
|
🕵 Triage of the CI failures on 901ef4c:
@groeneai, investigate the second failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=112945&sha=901ef4cb81f2987e1705be572ef50e0be9be1086&name_0=PR&name_1=Stress%20test%20%28arm_msan%29 and provide a fix in a separate PR (and file a tracking issue). If a fix is already in progress, link it here. |
…query_log_map_columns_serialization` as `no-darwin` `ci/defs/darwin.skip` only affects the Darwin fast-test harness, while ordinary `clickhouse-test` runs on macOS skip only tests tagged `no-darwin`. Both tests assert local prefetched-read-pool side effects that never happen when `pread_threadpool` resolves to `pread` on Darwin. Co-Authored-By: Claude Fable 5 <[email protected]>
|
🕵 Pushed Triage of the two CI reds on
|
…nstead of predicting it `willUseDirectIO` proves only that `createReadBufferFromFileBase` will attempt O_DIRECT. When the filesystem rejects the O_DIRECT open, the reader falls back to cached IO and resolves the method as a non-direct read - downgrading 'pread_threadpool' to 'pread' on a host without `preadNoWait` - while `DiskLocal::prepareRead` had already kept the method and disabled the userspace page cache for a read that ends up page-cache-compatible. New `canOpenWithDirectIO` probes the O_DIRECT open with the same flags the reader uses, only when the page-cache decision depends on it. On probe failure, direct IO is disabled for the read in the settings handed to the pipeline source, so the reader deterministically resolves the same non-direct read. New gtest `ReadMethod.DirectIOOpenProbeMatchesTheReader` asserts that the probe and the reader take the same branch on whatever filesystem the test runs on, and that a nonexistent file is never claimed O_DIRECT-capable. Co-Authored-By: Claude Fable 5 <[email protected]>
|
🕵 Triage of the four stress reds on
Pushed |
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=112945&sha=f4da4a7e1b9928b3d2dac3986f70a7b2a96e916f&name_0=PR&name_1=Stress%20test%20%28arm_asan_ubsan%29 — |
|
The Breadth confirmed independently, so I agree it is not caused by the PRs it lands on: 5 runs in a 10-day window, One correction on the signature: this report pools two distinct defects, and #113926 is only one of them.
So the STID is aliasing the two, which is why it does not match #114291 as a whole. #113926 removes the direct roots; the indirect remainder keeps the post-check red on its own until #114291 is fixed. There is no tracking issue for the |
This isn't true. We just keep all the development history on GitHub, that simple. |
|
But it was on point: We can use SettingsQuirks to switch from pread_threadpool to pread at start time. We can tolerate the fact that if a user has a server configured with pread, and thus the startup check is missed, and then switches to pread_threadpool at runtime, they get a worse performance. This will simplify the code a lot. Like a totally different, and trivial, implementation. |
… resolving it at every read `preadv2` with the `RWF_NOWAIT` flag is what the `pread_threadpool` read method needs to read the data that is already in the page cache without handing the read off to a thread pool. Whether it can be used was resolved separately at every place that inspects the read method, which needed `resolveLocalFSReadMethod`, `willUseDirectIO` and `canOpenWithDirectIO`, and changes in `DiskLocal::prepareRead`, `createReadBufferFromFileBase`, `ReadFromMergeTree::readFromPool` and `Server`. Resolve it once instead, where the other quirks of this kind live: `applySettingsQuirks` switches the default value of `local_filesystem_read_method` from 'pread_threadpool' to 'pread' when the one-time probe says the system call cannot be used, and reports the reason in the log. Everything downstream then sees a plain 'pread' setting and needs no changes of its own, so all of the above is deleted. Only the default is switched. A value set explicitly, in the configuration or with `SET`, is left alone, as with the other settings quirks: such a session keeps 'pread_threadpool' and keeps paying for the hand-off, which is what happens today. The per-read `errno` handling in `ThreadPoolReader::submit`, which now also recognizes `EPERM`, keeps that session working instead of failing it with `CANNOT_READ_FROM_FILE_DESCRIPTOR`. The `system.warnings` entry goes away with `Server.cpp`: `applySettingsQuirks` has no `Context` to add it to. The reason is in the server log instead.
|
🕵 Reimplemented that way in What is left is the one-time probe in Taking the tolerance you offered: only the default is switched, so a value set explicitly - in the configuration or with One thing that did not survive the move: the Re-measured on a 2M row
With an explicit Unit tests cover the Noted on the history point - new commits only, nothing squashed or rewritten. |
Outside of an MSan build the macro expands to nothing, so an unbraced
`if (res > 0) __msan_unpoison(buf, res);` leaves an empty statement, and
`bugprone-suspicious-semicolon` rejects it:
src/IO/preadNoWait.cpp:63:34: error: potentially unintended semicolon
[bugprone-suspicious-semicolon,-warnings-as-errors]
https://s3.amazonaws.com/clickhouse-test-reports/PRs/112945/637243cb8083a15db724d93dd9baf2bb444a90bb/build_arm_tidy/build_clickhouse/build_clickhouse.log
Three findings from the review of the `SettingsQuirks` rewrite. **The switched value was forwarded to the remote shards.** Assigning through `settings[...]` marks the setting as changed, `BaseSettings::write` serializes exactly the changed settings, and `Connection::sendQuery` sends those to every shard. An initiator whose `preadv2` is blocked - or that runs an old kernel - therefore imposed `local_filesystem_read_method = 'pread'` on shards where the system call works, losing 'pread_threadpool' cluster-wide. The switch is a property of the host, not of the query, so the changed bit is cleared again and the setting stays out of `Settings::changes()`. Verified through the real `Context`: under a `seccomp` filter that rejects `preadv2`, `system.settings` now reports `value = 'pread'` with `changed = 0`, while an explicitly requested 'pread_threadpool' still reports `changed = 1` and is still sent. **Two Darwin skips are no longer warranted.** Only the default is switched now, and `03552_filesystem_read_prefetches_log` and `04640_query_log_map_columns_serialization` both set `local_filesystem_read_method = 'pread_threadpool'` in the query, so the prefetched read pool still runs for them on macOS. Their `no-darwin` tags, and the `ci/defs/darwin.skip` entry, are removed. `02910_prefetch_unexpceted_exception` keeps its skip: it asks for the pool without asking for the read method, so it relies on the default. **The setting description overpromised.** The startup check detects a `seccomp` profile only if the profile rejects the system call with an error code, which is what container runtimes do; a profile that terminates the process instead cannot be detected, and the wording now says so.
|
🕵 All three findings of the 15th AI review round are addressed in Blocker - the switched value was forwarded to the shards. Verified through the real
The last row is the one that must still be sent, and is. Major - the Darwin skips. Correct, and it is a leftover from the previous design, which downgraded explicit values too. Checked each of the three:
Major - the kill-on-deny wording. Dismissed in an earlier round, but the situation changed with this design: the probe now runs at startup for the default read method, so a Also in |
On a host without `preadv2` (e.g. Darwin), the quirk fired in every program that applies settings changes, and `clickhouse-client` writes its log to stderr - so every stateless test failed with the warning in the client's stderr. Report the reason like the other quirks do: only when the caller passes a logger, which is `Context::setDefaultProfiles` at server startup. CI: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=112945&sha=c98e1cb973f25c20d6ee69527f92ed9424d9adef&name_0=PR&name_1=Fast%20test%20%28arm_darwin%29 PR: #112945 Co-Authored-By: Claude Fable 5 <[email protected]>
|
🕵 Pushed |
alexey-milovidov
left a comment
There was a problem hiding this comment.
Ok, now it is clean.
Related: #104634
Related: #49149
Related: #39753
The
pread_threadpoolread method hands every read off to a thread pool, unless the data is already in the page cache, which it checks with thepreadv2system call and theRWF_NOWAITflag. Two things can go wrong with that check, and both were handled badly.EPERMwas not recognized. It is what aseccompprofile of a container runtime answers for a system call that is not in its allow list.ThreadPoolReader::submithanded the read off to the thread pool forENOSYSandEOPNOTSUPP, but letEPERMthrough to the throw, failing the query withCANNOT_READ_FROM_FILE_DESCRIPTOR. This is the signature reported in #49149.The check was never verified in advance.
hasBugInPreadV2only compared the kernel version, and nothing else was checked, so on a system where the check cannot workpread_threadpoolkept paying for a thread pool hand-off on every read - including the reads that only had to copy the data from the page cache. In #104634, on Amazon Linux 2 (kernel 5.10), the profile showsThreadPoolReaderPageCacheMiss1,971,514 out ofLocalThreadPoolJobs1,972,484 - every read went to the pool - while the device only moved ~22 GB of the 258 GB the file descriptor delivered, i.e. ~90% of the data was in the page cache and still paid for the hand-off.The system call is now probed once, before it is used, by
preadNoWaitUnavailableReason. The probe passes an invalid file descriptor on purpose:seccompfilters and the system call table are consulted before the descriptor is looked up, so an available system call answersEBADFwithout reading anything, while a blocked one answersEPERMorENOSYS. When it says the check cannot be used,applySettingsQuirksswitches the default value oflocal_filesystem_read_methodfrompread_threadpooltopreadat start time, and says why in the server log. Nothing downstream has to know: the reader, the userspace page cache eligibility inDiskLocal::prepareReadand the prefetched read pool all see a plainpreadsetting.As with the other settings quirks, a value set explicitly - in the configuration, or with
SETat runtime - is left alone. Such a session keepspread_threadpooland keeps paying for the hand-off, which is what happens today.The switch is a property of the host, so the setting is left marked as unchanged: only the changed settings are serialized into the query the initiator sends to the remote shards, and a host that cannot use the system call must not impose
preadon the shards that can. An explicitly requested value stays changed and is still sent.The per-read
errnohandling inThreadPoolReader::submitis kept, withEPERMadded to it: the probe answers for the system call, but a particular filesystem can still reject the flag (tmpfsanswersEOPNOTSUPP, for example), and such a read is handed off to the thread pool instead of failing the query.How it was tested
preadv2was rejected the way a container runtime does it, with aseccompfilter installed by a small wrapper (SECCOMP_RET_ERRNO), and an old kernel was simulated withsetarch --uname-2.6. Reading a 2 million rowMergeTreetable with the defaultlocal_filesystem_read_method, before (the released 26.7.1 binary) and after:ThreadPoolReaderPageCacheHit39,LocalThreadPoolJobs90ThreadPoolReaderPageCacheHit34,ThreadPoolReaderPageCacheMiss3,LocalThreadPoolJobs93 - the read method stayspread_threadpoolpreadv2→EPERMCode: 74 ... errno: 1, Operation not permitted (CANNOT_READ_FROM_FILE_DESCRIPTOR), already while attaching the tablepread, the query succeeds, noThreadPoolReadereventspreadv2→ENOSYSThreadPoolReaderPageCacheMiss45,LocalThreadPoolJobs135 - every read to the poolpread, noThreadPoolReaderevents,LocalThreadPoolJobs80ThreadPoolReaderPageCacheMiss45,LocalThreadPoolJobs135pread, noThreadPoolReaderevents,LocalThreadPoolJobs81In all three rejected cases the reason is in the log, for example:
An explicitly requested
local_filesystem_read_method = 'pread_threadpool'is kept, on every one of those systems, and this is where the per-readEPERMhandling earns its place: under theEPERMfilter the same query fails on the released binary and succeeds here, withThreadPoolReaderPageCacheMiss45 andLocalThreadPoolJobs135 - every read handed off to the pool, which is the documented cost of asking for it there.Under the same filters,
system.settingsreportslocal_filesystem_read_method = 'pread'withchanged = 0, so nothing is forwarded to the remote shards, while an explicitly requestedpread_threadpoolreportschanged = 1and is still sent.Unit tests cover the
errnoclassification, the probe'sEBADFcontract, and the quirk itself (the default is switched exactly when the probe says the system call is unusable, the switched value stays out ofSettings::changes(), and an explicitly set value is never switched). An automated end-to-end test would need an instance with a restrictiveseccompprofile, which the integration test framework cannot express today - it starts every instance withseccomp:unconfined.Documented behavior impact
On a system where the page cache cannot be checked without waiting for the disk - Linux older than 5.11, a sandbox that rejects
preadv2with an error code, and systems other than Linux, wherepreadv2does not exist and the method never checked the page cache in the first place - the default value oflocal_filesystem_read_methodbecomespreadinstead ofpread_threadpool, so local reads are performed in the calling thread. This includes the reads withO_DIRECT, which never look at the page cache and do not need the check; the read method is now resolved once, for the whole server, so they follow the same value. Nothing changes on a supported system, and nothing changes for an explicitly configured read method. The setting description insrc/Core/Settings.cpp, from which the docs are generated, is updated in this PR.A
seccompprofile that terminates the process instead of rejecting the system call with an error code cannot be detected: the startup probe is itself apreadv2call, so such a profile kills the server there. Onmasterit kills it at the first read instead, under the same default read method.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
The
pread_threadpoolread method needs thepreadv2system call with theRWF_NOWAITflag to read the data that is already in the page cache without handing the read off to a thread pool. It is now checked at start time whether that system call can be used, and if it cannot - the Linux kernel is older than 5.11, or aseccompprofile of a container runtime rejects the system call - the default value oflocal_filesystem_read_methodis switched topread, and the reason is reported in the server log. Previously, every read paid for a thread pool hand-off on such systems, and aseccompprofile that answersEPERMmade queries fail withCANNOT_READ_FROM_FILE_DESCRIPTOR.Version info
26.8.1.1369(included in26.8and later)