Repository navigation
Support globs in the file name of the FileLog path - #123590
Conversation
A FileLog table on a directory reads every regular file in it, so with
the usual logrotate setup (compress + delaycompress) it also reads the
compressed app.log.2.gz as text. FileLog('<dir>/*.log', ...) failed with
"neither a regular file, nor a directory" (issue ClickHouse#123368, item 3).
The file name of the path may now have globs with the syntax of the
file() table function (makeRegexpPatternFromGlobs, full match on the
bare name). The directory part is watched as before; globs in it are
rejected. A path that exists literally is checked first and used exactly
as before, so no existing table changes behavior.
A file is read when its name matches: at creation, on DW_ITEM_ADDED and
on DW_ITEM_MOVED_TO. A file the table already reads keeps being read
after a rename to a non-matching name, until it is removed, also across
DETACH/ATTACH (loadFiles keeps a non-matching file whose inode has a
stored meta). Name-only filtering would drop app.log the moment it
becomes app.log.1 and lose the lines appended since the last read plus
what the application writes there before it reopens its log; compressed
files are always new inodes, so they are never followed.
When an unread file is renamed over a read name, that name is marked
REMOVED so the existing cleanup drops its context and meta, and
DW_ITEM_MODIFIED no longer resurrects a REMOVED name in the same batch;
otherwise the replacing file would be read from the replaced file's
offset.
Documented in the engine docs with the logrotate example.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ames
A matching file created and renamed to a non-matching name within one
watcher batch was never read: DW_ITEM_ADDED checks the file when the
batch is processed, and the MOVED_TO filter rejected the new name of an
unknown inode. The watcher now carries the rename cookie (inotify cookie
on Linux, the inode on macOS) into the event, and a MOVED_TO whose
MOVED_FROM in the same batch had a matching name is admitted.
On ATTACH, loadFiles renamed meta files one by one while iterating an
unordered map, so for a rotation chain renamed while the table was
detached (a.2 -> a.3, a.1 -> a.2, a -> a.1) one rename could overwrite
the meta of another file of the chain, and the next serialize() failed
with LOGICAL_ERROR ("Last stored last_written_position ... is bigger").
This also happens for plain directory tables (seen on 26.8.10.6, where
the next start also re-read rows). The renames now go through
TMP_SUFFIX names in two passes; leftover temporary meta files are
already removed by deserialize().
The test sorts with LC_ALL=C (CI runs with LC_ALL=en_US.UTF-8, where
collation reorders "app.log" and "app.log.1"), and gains a step for a
file renamed before it was read and for a three-file chain renamed
while detached. Documented that a file created and renamed while the
table is detached is not read.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…argets The cookies of renames from a matching name were kept for one updateFileInfos call and only one hop, so a matching file renamed twice before the table read it (fresh.log -> fresh.log.1 -> fresh.log.2) was rejected: the first MOVED_TO target no longer exists when processed, and the second MOVED_FROM name does not match. A rename whose MOVED_FROM and MOVED_TO land in two watcher drains was rejected as well, and for a tracked non-matching file (app.log.1 -> app.log.2) that also lost the unread tail. The cookie sets now live in the storage for the current and the previous call. A MOVED_FROM is recorded when its name matches, is read, or is the target of such a rename that was already gone when processed; such a vanished target is remembered for the next hop. Test: a file renamed twice before the first read; a one-second pause before the renames so the macOS watcher, which diffs directory listings, sees the files first; table SETTINGS max_threads = 1, poll_timeout_ms = 100, since every idle file stream waits a full poll timeout per read (one debug run 55 s -> 15 s, 50 randomized runs max 94 s -> 17 s). Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ver together updateFileInfos replays a batch of watcher events against the directory as it is now, so a rename's eligibility asked about a name whose file had already changed: an unread file moved over a read name and renamed on was read, and an eligible MOVED_TO whose name was refilled later in the batch admitted the refill. Only the last add, removal or rename of a name is now applied to the file found there; earlier events only record whether the file they leave under the name is read, and a rename's cookie is eligible when its source name was read at that point of the batch. The watcher called one callback per event, each taking the mutex, so a read could drain the MOVED_FROM of a rename without its MOVED_TO; the cleanup then erased the source's meta and the file restarted at offset 0 (directory table) or was rejected (glob). The callbacks now collect a pass's events and commit them together; on Linux the watcher reads on for up to 10 ms while a MOVED_FROM has no MOVED_TO. This replaces the two-call cookie windows of the previous commit. loadFiles stats the non-matching files without throwing, so a file unlinked during the scan no longer fails the table's startup. Test: an unread file renamed into and through a read name, a read file renamed over a new file, a file renamed twice whose first name is refilled. Tagged long: 50 randomized runs on a loaded box took up to 109 s (about 20 s when the box is idle). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Every watcher commit wakes the materialized-view loop, and streamToViews built and ran the INSERT pipeline before it applied the watcher events. When every file is NO_CHANGE the pipeline reads nothing (the consumer and openFilesAndSetPos both skip such files), yet each consumer polled until the poll timeout, and updateFileInfos returned false for the nonempty batch, so a second empty pipeline followed. Under a glob, writes to files the glob excludes took this path: one write cost two ~1 s empty reads. streamToViews now applies the events without a pipeline in that case. Measured with an excluded file written every 100 ms for 5 s and a materialized view attached: 5 zero-row pushes and about +420 CPU ticks over an idle window before, none after; a matching line still reaches the view. Skipping every read instead makes 02024_storage_filelog_mv fail. loadFiles uses containsGlobs for its glob checks. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A name the glob excludes is admitted when its inode is already read (a rotated file), but neither admission path checked that no other name reads that inode. A hard link to a read file under an excluded name was then a second file on one meta: debug builds abort on the one-to-one assertion between file names and metas, release reads the tail twice. On ATTACH, loadFiles now collects excluded names with the inode of the scan, gives contexts to the matching names first, then admits the excluded names in sorted order, one name per inode not held already. The context takes the scan inode, so a name replaced between the scan and the second stat no longer tracks the replacement with offset 0. DW_ITEM_MOVED_TO rejects an excluded name whose inode another live name reads; a real rename is unaffected, since its MOVED_FROM comes first and marks the source REMOVED. The test gains hard links made while the table is detached and a hard link renamed while it is attached; both abort the debug server without this change. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`ln app.log alias.log; mv alias.log alias.bak` between two reads is one batch. The MOVED_FROM of the matching alias recorded the rename cookie, and that cookie disabled the hard-link check of the MOVED_TO filter too, so alias.bak was admitted while app.log still read the same inode: two contexts on one meta, a debug abort on the end-of-batch assertion, the tail read twice in release. A rename from a read name now only admits an inode that has no meta yet; a name whose inode another live name reads is always rejected. A real rename is unaffected, since its MOVED_FROM marks the source REMOVED first. The test gains a matching hard link renamed to an excluded name. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (click to expand)Rounds 1-7: own cold review each round, Gate B in rounds 1-6. Agreed findings are fixed in the next round, the rest are noted. Bounce cap reached - published as is after round 7; residual findings are listed below as noted, none blocking.
|
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261002-163015 |
|
Workflow [PR], commit [9906e51] Summary: ✅
AI ReviewSummaryThis PR adds file-name globs to Findings
Final Verdict❌ Needs changes before merge: the macOS watcher still breaks the core rename-following contract in the matching-hard-link case. LLVM Coverage ReportMeasured on commit 9906e51.
Changed lines: Changed C/C++ lines covered: 200/226 (88.50%) · Uncovered code |
`FileLog('<dir>/logs{old}/*.log', ...)` was rejected with "Globs are
supported only in the file name" even when `logs{old}` is an existing
directory, because the directory part was checked lexically. Reject the
directory part only when it does not exist, so a literal directory name is
used as is, the same way an existing file or directory path is.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
… name When a file read under a name the glob excludes also has an earlier-sorting hard link that the glob excludes, ATTACH picked the hard link because the candidates were sorted by name. Removing only the hard link then dropped the file, and later appends to the original name were never read. While the table is attached such a hard link is never read in place of the name the file is read under, so prefer the stored name on ATTACH too and fall back to the first name only when the stored name no longer has the file. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes6 object files changed (+98.40 KiB total), 0 added.
719 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 units11 translation units recompiled, 27 s compile time in total, 11 of them have a recent master baseline. |
# Conflicts: # src/Storages/FileLog/DirectoryWatcherBase.cpp # src/Storages/FileLog/StorageFileLog.cpp
…hanged The early return of `streamToViews` added for globs skipped the read when every file had `NO_CHANGE` status. A file that could not be opened (ClickHouse#123595) keeps `NO_CHANGE` status with `open_failed` set and is retried only by `openFilesAndSetPos`, so after the merge with master it would not be retried until another file changed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…them A hard link to a file that is read was rejected only under a name the glob excludes. Under a matching name (`ln app.log alias.bak; mv alias.bak alias.log`, a plain `ln app.log alias.log`, or a hard link made while the table is detached) the inode got a second live name in `context_by_name` while `meta_by_inode` kept one entry, so the file was read twice and the size `chassert` of `updateFileInfos` failed in debug builds. `DW_ITEM_ADDED` and `DW_ITEM_MOVED_TO` now skip a name whose inode is read under another name that still has it on disk, and `loadFiles` keeps one name per inode for matching files as it already did for rotated ones, preferring the name the meta file records. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…lsewhere After the merge with master (ClickHouse#123593), a file moved into the directory from elsewhere is reported as `DW_ITEM_ADDED` rather than `DW_ITEM_MOVED_TO`. The `DW_ITEM_ADDED` handler ignored names that the glob excludes, so when such a file replaced a rotated file that was read (`mv outside/replacement app.log.0`), the table kept the old name and read the new file from the old file's offset. `05316_file_log_globs` caught it in the "renamed file replaced" section. Like `DW_ITEM_MOVED_TO`, `DW_ITEM_ADDED` now drops the name when the file found there is not read. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… link on ATTACH `loadFiles` claimed a name for each inode from the matching files first, so a hard link with a matching name made while the table was detached took over a rotated file that was read under a non-matching name. Removing the link then dropped the inode, and later appends to the rotated file were lost. Now the name recorded in the meta is preferred among both matching and non-matching names, before the other names. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The macOS watcher opened a kqueue descriptor for every regular file in the
directory, so `FileLog('/dir/*.log', ...)` held one descriptor per excluded
archive and could run into `EMFILE`. It now watches only the names that match
the globs of the path and the files it watched before under any name (on the
first scan, the files the table reads), so a log renamed by rotation to a name
the globs exclude is still followed until it is removed.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
… is listed The macOS watcher compares directory listings, so a file created and renamed to a name that the globs exclude between two scans is never seen under the matching name and looks like a new excluded file. This is inherent to the listing-based watcher (it was not read before the scans were narrowed to watched files either), so narrow the documented contract of the engine for macOS and note it next to the scan. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…acOS The macOS watcher listed every hard link of a followed file whose name the globs exclude. With a hard link left next to a rotated log, the inode was listed under two names on both sides of the rotation, which is not recognized as a rename, so the table stopped reading the rotated log. Now a followed file is listed under at most one name the globs exclude (the name it was followed under, otherwise the smallest), and not at all when a matching name lists it. The watcher receives the names the table reads the files under, so on `ATTACH` the stored name is preferred. Co-Authored-By: Claude Opus 5.5 <[email protected]>
| if (owner.storage.fileNameMatches(name)) | ||
| { | ||
| matching_inodes.insert(inode); | ||
| out.emplace(std::move(name), state); | ||
| continue; | ||
| } | ||
| auto followed_it = followed_files.find(inode); | ||
| if (followed_it == followed_files.end()) | ||
| continue; | ||
| auto [alias_it, inserted] = followed_aliases.try_emplace(inode, name, state); | ||
| if (inserted) | ||
| continue; | ||
| /// Pick one alias deterministically: the followed name, otherwise the smallest name. | ||
| const auto & chosen = alias_it->second.first; | ||
| if (chosen != followed_it->second && (name == followed_it->second || name < chosen)) | ||
| alias_it->second = {std::move(name), state}; | ||
| } | ||
| for (auto & [inode, alias] : followed_aliases) | ||
| { | ||
| if (!matching_inodes.contains(inode)) | ||
| out.emplace(std::move(alias.first), alias.second); |
There was a problem hiding this comment.
On Darwin, this scan block still keeps matching hard-link names separate, while only the non-matching aliases are deduplicated. That breaks the new “keep tailing an already-read file across rename” contract after ATTACH: if a followed file has a matching hard link, the seed snapshot either contains both names (snapshot_inode_count[ino] > 1, so is_rename stops recognizing app.log -> app.log.1) or, for a followed non-matching file like chain.log.2, keeps only the matching alias and makes chain.log.2 -> chain.log.3 invisible. In both cases StorageFileLog stops tailing the rotated inode even though it was already being read. I think scan needs to collapse all names to one representative per inode here, with the followed/stored name winning over any alias.
…ead-once Conflicts with ClickHouse#123590 (FileLog path globs) in StorageFileLog.cpp, resolved by hand: - loadFiles: one ranking for every candidate name of a file (matching names and the rotated non-matching names whose inode has a stored meta). A hard link comes before a symbolic link, then the stored read name, then a matching name, then the smallest; the other candidates become other names of the file. - DW_ITEM_ADDED / DW_ITEM_MOVED_TO: isReadUnderOtherName is replaced by addOtherName, which records a new name of a read file whether or not it matches the glob, so removing the read name hands the file over instead of dropping it. The glob filter applies to a file that is new to the table. - Docs: the several-names sentence keeps this PR's wording. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #123368
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
The
FileLogtable engine accepts globs (*,?,{a,b},{N..M}) in the file name of its path, for exampleFileLog('/var/lib/clickhouse/user_files/my_app/*.log', 'JSONEachRow'), and reads only the matching files of that directory, so the compressed fileslogrotateleaves next to the log are not read. A file that is already read keeps being read after it is renamed to a non-matching name, such asapp.logrotated toapp.log.1.Description
#123368: a
FileLogtable on a directory reads every file in it, so with the usuallogrotatesetup (compress+delaycompress) it also readsapp.log.2.gzas text. TodayFileLog('<dir>/*.log', ...)fails withThe path ... neither a regular file, nor a directory.The path's file name may now have globs with the syntax of the
filetable function (full name match); the directory part is taken literally. Matching files are read, including later ones. A file the table already reads keeps being read after a rename to a non-matching name, also acrossDETACH/ATTACH, until it is removed, so lines written toapp.log.1before the application reopens its log are not lost; a non-matching hard link to it is not read while the original name remains. Watcher events are replayed in order and renames are paired by cookie, so a matching file renamed before the table saw it is read and an unread file stays unread (on macOS, whose watcher compares directory listings, only once the table has listed it under the matching name).The watcher waits up to 10 ms for the second event of a rename and hands both over together; a read between them made directory tables re-read the file from the start. On
ATTACH, meta files of renamed files move through temporary names, so a rotation chain renamed while detached no longer overwrites a meta file (directory tables could fail withLOGICAL_ERROR, seen on 26.8.10.6). A materialized view no longer runs an empty read when no file has new data. Existing literal paths are unchanged; documented with alogrotateexample. As today, inode reuse can make a new file be taken for a deleted one.Test
05316_file_log_globscovers new, moved-in and early-renamed files, rotations (also while detached and chained), compression, renames over read and new names, hard links, and both errors; it fails on master atCREATE TABLEand passes 50/50 with randomized settings.Workflow [PR]
Sync PR [sync-upstream/pr/123590]
Version info
26.10.1.1505-master(included in26.10and later)