Repository navigation
Replace per-table FileLog watch threads with a single shared reactor - #125794
Conversation
Use the reactor design pattern to use one thread to wacth all files changes and multiplex the changes to watchers.
|
Workflow [PR], commit [3757575] Summary: ✅
AI ReviewSummaryThis PR replaces per-table Findings❌ Blockers
Final Verdict❌ Request changes — the shared reactor architecture is promising, but the current revision still has unresolved data-loss paths and two new reactor failure-mode bugs. |
Don't open one inotify per table, instead use only one inotify for all tables.
close kqueue_fd if the DirectoryWatcherReactor constructor throws.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes135 object files changed (+122.64 KiB total), 2 added.
711 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units1482 translation units recompiled, 9622 s compile time in total, 1480 of them have a recent master baseline. |
7b954c5 to
624c037
Compare
Co-authored-by: János Benjamin Antal <[email protected]>
Co-authored-by: János Benjamin Antal <[email protected]>
96fed04 to
2e51b69
Compare
2e51b69 to
454fba7
Compare
antaljanosbenjamin
left a comment
There was a problem hiding this comment.
Last comment from me, because I missed that point from the AI before. If you want, I am okay merging this as is and fixing this in an upcoming PR, but please don't forget it :)
| void DirectoryWatchReactor::wakeUp() | ||
| { | ||
| /// EAGAIN means that the pipe is full of wake-ups the reactor thread has not read yet, which is as good as this one. | ||
| [[maybe_unused]] auto res = ::write(control_pipe.fds_rw[1], "\0", 1); |
There was a problem hiding this comment.
::write is interrupted before it enqueues a byte (EINTR), this helper still returns as though the reactor had been woken. The caller then blocks in submit().result.get() or stop().join() while epoll/kevent can sleep indefinitely because no wake-up was actually queued.
Please mirror WakeupFd::notify here: retry EINTR, treat EAGAIN/EWOULDBLOCK as “wake-up already pending”, and surface any other error instead of silently losing the signal.
| } | ||
| catch (...) | ||
| { | ||
| /// The loop fails only if `epoll` itself does, and there is nothing to continue with then. Do not leave the |
There was a problem hiding this comment.
runLoop() throws, this catch only marks the reactor stopped and rejects future commands. Existing watchers stay registered and are never told that the shared thread died, so every FileLog table behind this reactor silently stops seeing new directory changes. Later DirectoryWatcherBase::~DirectoryWatcherBase suppresses the failing remove(), leaving registrations non-empty until ~DirectoryWatchReactor() hits its chassert.
Please actively fail or detach the current registrations here (and surface an error to their owners) before letting the thread exit.
The test compared the server-wide `GlobalThread` metric before and after creating 200 FileLog tables and required a growth below 100. That metric also counts the threads of unrelated, lazily growing pools. In the failing master runs (4a88506, fc8a491, 149b9fa) a background merge of the wide `system.metric_log` table on the `cached_s3` disk grew the threadpool remote-FS reader by 119-163 threads inside the CREATE window, while the CREATEs added about one. Only the DatabaseReplicated lane failed: each CREATE there goes through Keeper, so the window is ~18 s instead of ~1 s. Count the shared watch thread itself: rows of `system.stack_trace` named `FileLogWatch`. There is one reactor per server and `DirectoryWatchReactor::add` waits for its thread, so after the CREATEs the count is 1 whatever else runs on the server. It reads 0 on 26.10, which still has a thread per table. Reading only `thread_name` sends no signals to other threads. Related: ClickHouse#125794 Co-Authored-By: Claude Opus 5.5 <[email protected]>
05319_file_log_rename_in_reused_inode timed out in Fast test (arm_darwin) of ClickHouse#117738: one macOS scan saw a.csv gone and b.csv, moved in from a hard link outside the directory, with the same inode, diffed them as MOVED_FROM a.csv + MOVED_TO b.csv, and StorageFileLog kept a.csv's offset, skipping rows of b.csv. ClickHouse#123593 made is_rename refuse a name in deleted_files as a rename source. Before ClickHouse#125794 every NOTE_DELETE of a watched descriptor put its name there; ClickHouse#125794 (4d75ef8) returns early while the file still has links, because kqueue reports an unlink of any name to every descriptor of the file and the scan has to find which name went away (hidden excluded aliases of 05316_file_log_globs). When the remaining link is outside the directory, nothing recorded the unlink any more. Record the inode in that branch (unlinked_inodes), and in is_rename refuse the pair when the inode's current name matches the path: the scan lists every matching name and is_rename requires one listed name per side, so such a name was not a link of the file at the last scan and the file came back, i.e. it is new here, as on Linux (an IN_MOVED_TO without its IN_MOVED_FROM is ADDED). An excluded current name can be a hidden alias and keeps ClickHouse#125794's behaviour. The set lives next to deleted_files: kept across discarded and failed passes, cleared after a committed one, the first snapshot included (its listing already reflects every unlink recorded before it, and it pairs nothing). The unlink is drained before the diff (scanFilesAndUpdateWatches drains before syncFileWatches closes descriptors), so the decision does not depend on scan timing. The test gains the same steps in one process so a macOS scan sees the unlink and the move-in together; the new content is longer than c.csv's offset, so a carried- over offset gives wrong rows (checked on Linux with a same-inode rename, which keeps the offset: only 110 is read). Co-Authored-By: Claude Opus 5.5 <[email protected]>
For FileLog tables use the reactor design pattern to use one thread to watch all files changes and multiplex the changes to watchers, where each table has a single watcher.
Closes: #116688
Changelog category :
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Instead of having one thread per FileLog table to watch files changes, use a single thread to watch file changes for all tables. This should eliminate thread starvation bugs that happens when we have too many FileLog tables.
...
Workflow [PR]
Sync PR [sync-upstream/pr/125794]
Version info
26.11.1.1981-master(included in26.11and later)