Skip to content

Support globs in the file name of the FileLog path - #123590

Merged
alexey-milovidov merged 18 commits into
ClickHouse:masterfrom
groeneai:groeneai/filelog-path-globs
Oct 4, 2026
Merged

alexey-milovidov merged 18 commits into
ClickHouse:masterfrom
groeneai:groeneai/filelog-path-globs

Conversation

@groeneai

@groeneai groeneai commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #123368

Changelog category (leave one):

  • New Feature

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

The FileLog table engine accepts globs (*, ?, {a,b}, {N..M}) in the file name of its path, for example FileLog('/var/lib/clickhouse/user_files/my_app/*.log', 'JSONEachRow'), and reads only the matching files of that directory, so the compressed files logrotate leaves 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 as app.log rotated to app.log.1.

Description

#123368: a FileLog table on a directory reads every file in it, so with the usual logrotate setup (compress + delaycompress) it also reads app.log.2.gz as text. Today FileLog('<dir>/*.log', ...) fails with The path ... neither a regular file, nor a directory.

The path's file name may now have globs with the syntax of the file table 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 across DETACH/ATTACH, until it is removed, so lines written to app.log.1 before 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 with LOGICAL_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 a logrotate example. As today, inode reuse can make a new file be taken for a deleted one.

Test 05316_file_log_globs covers 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 at CREATE TABLE and passes 50/50 with randomized settings.


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

Version info

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

groeneai and others added 7 commits October 2, 2026 03:38
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]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Oct 2, 2026
@groeneai

groeneai commented Oct 2, 2026

Copy link
Copy Markdown
Collaborator Author
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.

Finding Verdict
❌ The test's sort follows the locale; under CI's LC_ALL=en_US.UTF-8 two sections reorder AGREE, fixed (LC_ALL=C sort)
❌ A matching file created and renamed to a non-matching name before the table read it is never read AGREE, fixed (renames paired by the watcher's rename cookie)
❌ The pairing was per batch and one hop only: a second rename, or a rename whose two events land in different batches, still lost the file AGREE, fixed (eligibility follows the file through every rename of the batch; reworked in round 3, next two rows)
❌ Rename eligibility read the table's state, which reflects the directory as it is now: an unread file moved over a read name and renamed on was read (Gate B saw rows 700, 14), and so was a never-read rotated file after two rotations between reads AGREE, fixed (names replayed in event order; each name's last change alone is applied to the file found there)
⚠️ A read between the two events of one rename erased the source's offset, so the renamed file was re-read from the start, or dropped under a glob AGREE, fixed (the watcher hands over each pass's events together and pairs a rename split across two reads; the two-call windows are gone)
❌ On ATTACH, meta files of a rotation chain renamed while detached are renamed in hash order and overwrite each other (LOGICAL_ERROR on the next read) AGREE, fixed (two-phase rename through temporary names)
⚠️ The early-rename test step races with the macOS watcher, which compares directory listings (Fast test (arm_darwin)) AGREE, fixed (the files are listed before they are renamed)
⚠️ The test grew to 44-94 s on a debug build; untagged tests fail the flaky check above 180 s AGREE, fixed (one stream and a 100 ms poll timeout on the test table)
⚠️ With a materialized view, every write to a file the glob excludes woke the table and ran an empty read that busy-waits the poll timeout (about 1.5 s of CPU per write in a probe) AGREE, fixed (no read is started while no file has new data)
⚠️ A hard link to a read file under a name the glob excludes was read as a second file on ATTACH (debug abort on the one-meta-per-name assertion, the tail read twice in release), and so was a renamed hard link while attached AGREE, fixed (an excluded name is admitted only for an inode no other name reads)
⚠️ A hard link made under a matching name and renamed to an excluded name before the next read was still admitted through rename pairing (the same abort / double read) AGREE, fixed (a name the glob excludes is never admitted for an inode another live name reads)
⚠️ On ATTACH, an excluded name replaced during the directory scan was tracked with the replacement's inode and a fresh offset AGREE, fixed (the inode that selected the name is the one tracked)
⚠️ Rename pairing waits a fixed 10 ms for the second event; a pair split beyond that is handled as two unrelated events DISAGREE: inotify(7) calls this matching inherently racy and prescribes a follow-up read with a small timeout; any design needs a bound, since a move out of the directory has no second event; master does not pair at all. The bound is stated in the description
⚠️ No CI test fails if the materialized-view shortcut is removed DISAGREE: it changes CPU, not rows; a late modify event also runs a zero-row read, so a SQL oracle would be timing-dependent; skipping too much fails 02024_storage_filelog_mv, and the effect was measured both ways (zero-row reads per excluded write: 5 before, 0 after)
⚠️ The test is tagged long, so Fast test (arm_darwin) does not run it DISAGREE: it takes ~20 s (sleep-bound), slower than 99.9% of Fast test tests; the macOS-only part is the rename cookie, the file's inode on both events of a rename by construction, and the macOS event hand-over is exercised there by the untagged FileLog tests
💡 A batch drained before later renames reach the watcher stats the directory as it is now, so a name renamed onto and refilled within the watcher's lag can admit the refill noted; logrotate renames each name before refilling it, and directory tables have the same window
💡 A crash between the two meta-rename passes leaves .tmp metas that the next load deletes, so those files are re-read noted; the engine docs already state the offsets carry no durability guarantee
💡 A file created and renamed to a non-matching name while the table is detached is not read documented
💡 Inode reuse can make a new file inherit a deleted file's offset as today; stated in the description
💡 loadFiles stats non-matching files; a file deleted between readdir and stat fails CREATE/ATTACH AGREE, fixed (a vanished file is skipped)
💡 The inode-follow term of the MOVED_TO filter is not pinned by the test (every rename in it is cookie-paired) noted; the arm that pins it (move out and back in) races with the macOS listing-diff watcher
💡 A hard link created under a name the glob matches is read as a second file noted; unchanged code path, the same as for a directory table today; the description only promises that a non-matching hard link is not read
💡 Three of the changes also reach plain directory tables (rename hand-over in the watcher, ATTACH meta renames, the skipped empty read) noted; each is triggered by the glob, disclosed in the description and covered by the test, and can be split out on request

@groeneai

groeneai commented Oct 2, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. On master CREATE TABLE t (v UInt64) ENGINE = FileLog('<dir>/*.log', 'TSV') fails every time with Code: 36 ... neither a regular file, nor a directory. A rotation chain renamed while the table is detached (a.2 -> a.3, a.1 -> a.2, a -> a.1) fails the next read with LOGICAL_ERROR on master and on 26.8.10.6, also for a directory table.
b Root cause explained? loadFiles listed every regular file of a directory and the DW_ITEM_ADDED / DW_ITEM_MOVED_TO handlers added every new file: no file-name filter anywhere. On ATTACH, meta files were renamed in hash order, so one rename could overwrite the meta of a file renamed later in the chain. With a materialized view, every watcher event started a read before the events were applied, so a write to an excluded file ran an empty read until the poll timeout. An excluded name was admitted by its inode without checking that no other name already read it, so a hard link became a second file on one meta.
c Fix matches root cause? Yes. The glob filter is applied at those three entry points; a file already read is followed by inode across renames. Events are replayed in order, only the last change of a name is applied, and renames are paired by the watcher's cookie; the watcher hands over a pass's events together. ATTACH meta renames go through temporary names. The materialized-view loop applies the events without a read when no file has data. An excluded name is admitted by inode only when no other live name reads that inode, on ATTACH and in DW_ITEM_MOVED_TO.
d Test intent preserved / new tests added? New 05316_file_log_globs: initial scan, new and moved-in files, files renamed once or twice before the first read, rotations (also while detached, also a chain), compression, renames over read and new names, hard links to read files, both new errors. Fifteen mutations of the change each fail it; skipping every read in the materialized-view loop fails 02024_storage_filelog_mv. The CPU cost (row e) and a rename split across two reads were shown with a probe and with a build that widens that window.
e Both directions demonstrated? Master snapshot: FAIL at CREATE TABLE. Fixed build: OK, and 50/50 with randomized settings under LC_ALL=en_US.UTF-8. Without the hard-link rule, the hard-link steps abort the debug server on file_names.size() == meta_by_inode.size(). Build-ID verified. Materialized-view probe, an excluded file written every 100 ms for 5 s: before, 5 empty reads and about +420 CPU ticks; after, 0 and no added CPU.
f Fix is general across code paths? All watcher event kinds on Linux and macOS (macOS passes the inode as the rename cookie), both readers (direct SELECT and the materialized-view loop go through updateFileInfos), ATTACH with stored offsets, both paths that admit an excluded name. Single-file mode has no matcher. The skipped read happens only when every file is NO_CHANGE, which the reader skips anyway.
g Fix generalizes across inputs (params/datatypes/wrappers)? *, ?, {a,b}, {N..M}, several enum groups, a glob matching nothing, an over-long range (rejected by the shared glob parser), an existing directory whose name contains a glob character (used literally).
h Backward compatible? (maintainer-approved exception only) Yes. A path that exists literally is used as before; the glob branch is only reached by a path that was an error before. No setting, no metadata format change; leftover temporary meta files are already removed on load.
i Invariants and contracts preserved? file_names, context_by_name and meta_by_inode stay one to one, also with hard links (the existing debug assertions held through 50 randomized runs and all 18 existing FileLog tests). Path validation throws before any state is touched.

Session id: cron:clickhouse-impl-slot-4:20261002-163015

@clickhouse-gh

clickhouse-gh Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9906e51]

Summary: ✅


AI Review

Summary

This PR adds file-name globs to FileLog paths and does a lot of careful work to preserve offsets across renames, detach/attach, and hard-link edge cases. The Linux/event-ordering work looks solid, but the Darwin directory scan still loses the followed inode when a matching hard link exists, so I do not think the change is ready to merge yet.

Findings
  • ❌ Blocker — src/Storages/FileLog/DirectoryWatcherBase.cpp:269: on Darwin, scan still tracks matching hard-link names separately and suppresses the followed non-matching name when any matching alias exists. After ATTACH, that makes the next rename of an already-read file either non-unique (snapshot_inode_count > 1) or completely invisible, so StorageFileLog stops tailing the rotated inode and misses later rows. The scan needs one representative name per inode here as well, with the followed/stored name preferred over any alias.
Final Verdict

❌ Needs changes before merge: the macOS watcher still breaks the core rename-following contract in the matching-hard-link case.

LLVM Coverage Report

Measured on commit 9906e51.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 92.10% 91.80% -0.30%
Branches 79.60% 79.60% +0.00%

Changed lines: Changed C/C++ lines covered: 200/226 (88.50%) · Uncovered code

Full report · Diff report

Comment thread src/Storages/FileLog/StorageFileLog.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-message-queues Message queue integrations (Kafka, RabbitMQ, NATS table engines for stream ingestion/egress). label Oct 2, 2026
groeneai and others added 2 commits October 2, 2026 19:00
`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]>
Comment thread src/Storages/FileLog/StorageFileLog.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 9906e51f7 with master 37a251ffa (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.

Object file sizes

6 object files changed (+98.40 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Storages/FileLog/StorageFileLog.cpp.o 622.09 KiB 708.22 KiB +86.12 KiB (+13.84%)

719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

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

Job report

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I like it.

@alexey-milovidov alexey-milovidov self-assigned this Oct 3, 2026
alexey-milovidov and others added 4 commits October 3, 2026 23:28
# 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]>
Comment thread src/Storages/FileLog/StorageFileLog.cpp Resolved
alexey-milovidov and others added 2 commits October 4, 2026 03:18
… 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]>
Comment thread src/Storages/FileLog/StorageFileLog.cpp Resolved
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]>
Comment on lines +269 to +289
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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 4, 2026
Merged via the queue into ClickHouse:master with commit 7644dea Oct 4, 2026
149 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 4, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Oct 6, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-message-queues Message queue integrations (Kafka, RabbitMQ, NATS table engines for stream ingestion/egress). groeneai-origin-request PR origin: a maintainer pinged or directed groeneai pr-feature Pull request with new product feature 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.

3 participants