Repository navigation
Fix FileLog reading a file twice when it has a hard link or a symbolic link in the watched directory - #123673
Fix FileLog reading a file twice when it has a hard link or a symbolic link in the watched directory#123673groeneai wants to merge 25 commits into
Conversation
A hard link or a symbolic link to a file that a FileLog table already reads gave the inode a second tracked name. Both names were then read from the same shared offset (reset to 0 on DW_ITEM_ADDED), so consumed rows were delivered again, and debug builds aborted on the file_names/meta_by_inode chasserts. With such a link present at CREATE, loadFiles renamed a meta file that did not exist yet and CREATE failed with ENOENT. The inode is now read under one name. Its other names are kept in FileInfos::inode_by_other_name and are not read. When the read name loses the inode (removed, replaced, or becoming an other name itself), the file is handed over to its smallest other name that still stats to the inode, at the same offset. loadFiles groups listed names by inode and reads each inode under the name stored in its meta, else the smallest name. A write through an other name marks the read name updated. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…olic links The other names of a file read under another name were checked with stat when the table processed the events, which is after the fact. In `rm app.log; ln alias.log new.log; rm alias.log`, alias.log is already gone when ADDED new.log is processed, although its own REMOVED event comes later, so the file was taken for a new one and read again from the start. The watcher can also deliver such a sequence in two batches, so the answer must not depend on where a batch ends. A hard link is now a name of the file from the event that records it until its own removal event is processed, with no stat: a drained batch is a prefix of the kernel's event order, and an inode can only be reused once all of its names were removed. A symbolic link can stop pointing to the file with no event, so it is still checked with stat. When the read name loses the file, the file goes to an other name that still has it, a hard link before a symbolic link, then the smallest name. A hard link whose removal is still pending takes the file over without being read, so a read does not fail on it, and its removal event hands the file on. loadFiles and the handover prefer a hard link as the read name. A file read under a symbolic link moves to a new name of it: the link becomes an other name if it still points to the file, else it is dropped. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…link in the directory A symbolic link that took the read name's place (renamed over it, or added right after the read name's removal in the same batch) became the read name even when hard links of the file were recorded. Once its target was removed the link dangled, and a write through another hard link then failed every read of the table with CANNOT_READ_ALL_DATA. addOtherName now records such a link as an other name whenever a present hard link of the file is recorded; the read name is handed to that hard link at the same position. The engine documentation promised that a file keeps its position while it has any name in the directory. A name whose addition is processed after it is already gone is never recorded, and that sequence has the same events as an inode reuse, so the sentence now states what holds: the file is read on under a name the table has already seen. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ed file's offset A symbolic link to the read name (`s.log -> a.log`) was checked with `stat` only, so after `a.log` was removed and re-created with the same inode (inode reuse, or relinking a pin held outside the directory) the link still counted as a present other name. The re-created file then kept the old offset, both when the events came in one batch and when the removal was handed over first, and its head was never read. A symbolic link whose target is directly in the watched directory never needs to carry the file: its target is a name the table tracks with its own events. `findOtherName` now drops such a link, like a gone one. A link that resolves outside the directory is still checked with `stat`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… directory follow that name `resolvesIntoDirectory` looked only at the final target, so in a chain `s.log -> a.log -> ../outside.log` the link `s.log` stayed a stat-checked carrier although it reaches the file only through the watched name `a.log`. Removing `a.log`, rewriting the outside file and linking `a.log` again kept the old offset, and the head of the new content was never read. The same chain made the link the read name at runtime (the swap path) and at `CREATE`/`ATTACH` (the `loadFiles` ranking), with the same outcome. `resolvesIntoDirectory` now walks the link hop by hop and is true as soon as a hop's target is directly in the directory. Such a link is only an other name (`addOtherName`), never a carrier (`findOtherName`), and ranks after a link whose whole chain stays outside (`loadFiles`). The walk uses `FS::isSymlinkNoThrow` and `FS::readSymlink` (the style check forbids the `std::filesystem` ones) and still never throws: a `filesystem_error` from `readSymlink` returns `false`, so the `stat` check decides, as before. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`releaseInode` changed `meta_by_inode`, `other_names` and `context_by_name` before its fallible disk call (`moveMetaFile` is a `rename`, the no-other branch a `removeFileIfExists`), so a failed meta move left a meta with no read name. The cleanup loop made it worse: it had already swapped `file_names` out and erased the removed name's context, so a throw dropped every later name from `file_names`. Those files stayed in the other maps and were not read again until `ATTACH`; a debug build aborted on the next 1:1 `chassert`. The symbolic-link swap and the ADDED "kept another name" path had the same order: re-point the maps, then move the meta file. Now every site this change adds moves or removes the meta file before the maps change, so a throw leaves them as they were: - `releaseInode` moves or removes the meta file first. - The new `untrackReadName` (releasing a name before it becomes another name) is shared by the swap, the `addOtherName` tail and the kept path. - The swap and kept paths move the meta file before re-pointing the maps. - On a throw the cleanup loop keeps the failing name, which is still `REMOVED`, and every name after it in `file_names`, so the next call retries. Checked by local fault injection with the table's metadata directory made read-only (a non-root server): a failed handover rename, and a failed meta removal before two more files. The previous head aborts on the next read; now the read fails with the disk error, and after the directory is writable again the next read returns the appended rows. The test sets `poll_timeout_ms = 100`: a direct SELECT waits the poll timeout per read file, one stream after another under a low random `max_threads`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…cked when its meta file move fails The directory watcher keeps one modification event per name and batch. When a name that was another name of file A is removed and linked to file B in the same batch ([MODIFIED x, REMOVED x, ADDED x, MODIFIED x]), the second MODIFIED is dropped. The ADDED recorded `x` as another name of B but did not mark B's read name, so a write through `x` was not read until B's next event. Recording another name now marks the file's read name updated, through the `markReadNameUpdated` helper that the MODIFIED mapping uses too. At the symbolic-link swap and the ADDED "kept another name" path, the new name was untracked first and recorded only after `moveMetaFile` succeeded, so a failed move left it in no map and later writes through it were ignored until another structural event or `ATTACH`. The new name is now another name of the file until its meta file has moved: a failed move leaves it a carrier, the kept path retries the handover in the cleanup loop, and in the swap writes through it reach the symbolic-link read name. The new test arm first lets the watcher record the hard link (a read of the row written through it), so the re-link batch has the shape above. Checked by local fault injection with the table's metadata directory made read-only: at the swap and at the kept path the previous head reads nothing after the directory is writable again, and now the appended row is read. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Recording another name of a read file marks the file's read name updated (the watcher keeps one modification event per name and batch), and so does a write through another name. Neither checked that the read name still had the file. In `ln new.log next.log; rm new.log` and in `ln next.log keep.log; mv tmp next.log` the new name's event comes before the read name's removal or replacement in kernel order, so when a watcher batch ends after it, the read name is marked although it is already gone or holds another file: the next read failed with `CANNOT_READ_ALL_DATA`, or read the replacement file with the old file's meta, after which the old file's consumed rows were delivered again under the surviving name. A read name that no longer has the file is now not marked. A hard link has a pending removal or rename event that hands the file over to another name, which reads it on from the offset. A symbolic link gets no such event when its target leaves, so it is set removed, as recording another name already does for a gone symbolic link, and the cleanup loop hands the file over. This also covers a symbolic read name kept after a failed meta file move while a hard link exists: once its target moved away, every write through the hard link failed every read until `ATTACH`. Checked with a throwaway build whose watcher sleeps after each read() buffer (the trace log shows the first read's batch holds only the new name's event): the previous head fails the first read or re-delivers consumed rows, and now the first read returns nothing and the next ones return each new row once. The symbolic-link case was checked by local fault injection with the table's metadata directory made read-only. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Remove five comment lines in StorageFileLog.cpp that narrate the new code (two duplicated untrackReadName notes, the findOtherName preference already given by the tuple order, an ordering note, and the MODIFIED mapping note). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; six rounds, 14 findings) plus my own review (seven rounds, 3 majors, 9 nits). Bounce cap reached: published as-is. The last fix round (row 16) was checked by my own review and by watcher-delay and fault-injection probes, not by another second-model run. Residual findings are the noted and DISAGREE rows below.
Severity: ❌ blocker / |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261003-090026 |
|
Workflow [PR], commit [d569b02] Summary: ❌
AI ReviewSummaryThe PR fixes duplicate delivery for Final Verdict✅ Approve |
…inode Releases before this change wrote a meta file under every name of a file that had a hard link or a symbolic link in the watched directory: 26.8.10.6 leaves `alias.log` at offset 6 and `app.log` at offset 4 for one inode. `deserialize` kept whichever it listed first and left the other on disk, so an upgraded table could re-deliver consumed rows on the first ATTACH, and the surviving file could win again on a later one: after `mv alias.log b.log` the next ATTACH re-delivered `app.log 3, 4, 5` (measured on xfs). Keep the meta with the larger offset, which is how far the file was read, and remove the others. 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 sizes7 object files changed (+67.76 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, 47 s compile time in total, 11 of them have a recent master baseline. |
The test checks the order-dependent handover between the names of one file, which needs the directory events in the order they happened. The macOS watcher builds its events from a diff of two directory snapshots: it emits every removal before every addition in a pass, and a name whose watch saw a delete is reported as removed and added again. In the `Fast test (arm_darwin)` run, `rm a.log` with a hard link `b.log` in the directory re-read `b.log` from the start (`b.log 1, 2, 3` instead of `b.log 3`), and the other handover arms re-read the same way. The first arms (a hard link is not read, a write through it is read under the read name) pass there. Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - 2ad8d29Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
None of these touches FileLog, and Session id: cron:our-pr-ci-monitor:20261003-200032 |
|
Fixing PR for 05237_with_expression_alias_scope_descendant (DBReplicated): #123774 |
|
This also fixes a master abort: Stress test (arm_asan_ubsan) on fb091ad hit The table was an AST fuzzer copy of the |
…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]>
After the merge with the glob support, a new name of a read file is recorded as another name whether or not it matches the glob, so removing the read name hands the file to that name at its offset. Before, the glob filter ran first, the name was dropped, and the file stopped being read once its read name was removed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The macOS directory watcher diffs two directory snapshots and reports every name of an unlinked inode as removed, so when one hard link of a file is removed the other names are reported as removed and added again, and the file is read from the beginning. This is why 05320 is `no-darwin`; the engine documentation now says so. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@groeneai, it's merged. Update the branch. |
…ectory With a glob, a matching symbolic link whose target in the directory is a name the glob excludes is read under the link, as on master: the excluded name's events are not applied, so the link does not follow that name. The sentence said it always does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
With `FileLog('*.log')` and `alias.log -> hidden.txt` in the directory, the
live table reads the file under `alias.log`, because the glob excludes
`hidden.txt`. On `DETACH`/`ATTACH`, `loadFiles` found `hidden.txt` through
`rotated_files` (its inode has a stored meta) and ranked the hard link over
the stored symbolic link name, so the meta moved to `hidden.txt` and later
rows were reported with `_filename = hidden.txt`. Master keeps `alias.log`.
Now a name the glob excludes does not outrank the name the file was read
under while that name is found; it is still recorded as another name, so a
write through it is read under the read name, and it still takes over when
the read name is gone (rotation through a hard link across a restart).
The test step fails on the previous head with `hidden.txt 3` instead of
`alias.log 3`.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nk to an untracked one With a glob, `loadFiles` took a name the glob excludes only when its inode already had stored meta. So on a fresh CREATE over `app.log` plus a hard link `app.log.1`, only `app.log` was recorded, and after `rm app.log` the file was dropped and rows appended to `app.log.1` were never read, although the same link made after CREATE reads on. Now an excluded name is also recorded when it shares the inode of a matching name in the same scan. It is ranked after the matching names unless the name the file was read under is gone, so CREATE keeps reading a symbolic link to an excluded name under the link, as the runtime path does. `resolvesIntoDirectory` vetoed a symbolic link whose in-directory target the glob excludes, although that target has no events the table handles. With `hidden.txt`, a hard link `app.log` and `alias.log -> hidden.txt`, removing the read name dropped the file and a later reload delivered its first rows again. The veto now applies only to a target the glob does not exclude or the table records, as the docs say. Measured against the previous head (761fe560) in .scratch/m15: both cases lost all appended rows there, and the second re-delivered row 1 after DETACH/ATTACH; with this change every row is read once and on time. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A symbolic link follows an in-directory name the glob excludes when the table already records that name, for example a hard link of a matching file: `resolvesIntoDirectory` checks the recorded names as well as the glob. The `05320_file_log_hard_links` step d6 shows it (`alias.log -> hidden.txt`, with `hidden.txt` a hard link of `b.log`, reads on under `hidden.txt` after `rm b.log`), so the exception in the docs holds only for a name the table has not seen. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A rename over a name emits no removal event for what it replaces, and a rename from outside the directory is reported as DW_ITEM_ADDED. Both appearance branches dropped a recorded hard link only after `is_regular_file` succeeded, and DW_ITEM_MOVED_TO also kept the read name, so a dangling symbolic link renamed over either one left it recorded: - over a hard link: removing the read name then handed the file to that stale alias as NO_CHANGE, waiting for a removal event that never comes, and appends through a name linked back later were not read until the table was reloaded; - over the read name, from inside the directory: writes through a surviving hard link were not read until the dangling link was removed. Drop the alias in both non-regular branches, and mark the read name removed in DW_ITEM_MOVED_TO as DW_ITEM_ADDED already does. 05320 gains a step that renames dangling symbolic links over two hard links, one from outside the directory (ADDED) and one from inside it (MOVED_TO), removes the read name and links the file back, and a step that renames one over the read name. Without any one of the three changes a step fails. Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - 5917576Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
All 175 check runs completed and Session id: cron:our-pr-ci-monitor:20261021-193005 |
…der another name When a symbolic link read name that failed to open leads to another file on the retry, `openFilesAndSetPos` replaced its inode and metadata unconditionally. If that file was already read under another name, both names shared one `FileMeta`: its rows were read again under the link, and removing the link dropped the other name's metadata, so every later read failed with `The key ... doesn't exist`. Now such a link keeps waiting like one that leads to no file. Addresses review: ClickHouse#123673 (comment) Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 Merged master (b1eff66) as 1c1c986. The only CI failure, |
Only the last add, removal or rename of a name in one watcher batch is applied to the file found there now, and the earlier ones were skipped entirely. So when a name was replaced twice in one batch, the first replacement did not take the name from the file it had: a replaced hard link still counted as a name of the file, and a replaced read name still looked like it held the file. A file whose last name in the directory was then removed, and which was linked back from outside, kept its old offset instead of being read from the start, so the rows written before it was linked back were lost. Now every add and rename into the directory drops what the name had (its other name record, and the read name), also when it is not the last event of the name. A file renamed into the directory over a read name whose file had been re-keyed to a new name then reaches the branch for an untracked rename source, which kept the stale meta file under the name, and the next read failed with "Last stored last_written_position in meta file ... is bigger than current last_written_pos". Like an added file, such a file is read from offset 0, so its stale meta file is removed too. The same LOGICAL_ERROR is reachable on master without hard links in the directory: `rm a.log; ln ../held c.log; mv x.tmp a.log` in one batch, with `../held` a hard link of `a.log`. Addresses review: ClickHouse#123673 (comment) Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - d569b02Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
All 179 check runs completed and Session id: cron:our-pr-ci-monitor:20261030-063003 |
|
Do we really need this? It seems like a very niche problem and nobody really complained about this apart from AI. |
|
No user has reported it. It came from a bot finding on my earlier FileLog PR #123590. I re-checked on today's master: the case in the title (a hard link or a symbolic link to a file that is being read) is already handled there since #123590, also in 26.10. What still goes wrong on master is link-then-unlink, The stress test aborts on So what is left is a niche case, for +377/-99 lines in |
A name that leads, with no directory event, to a file whose offset is kept for another name was skipped and never visited again. When that other name then moved on to a new file, as with symlink targets rotated by logrotate, no name read the file until the table was attached again (master reads it). The open now marks such a name to be retried, and so does a reset of all names that skips it. The rotated file's old offset is still not passed to the new name; that is the releaseInode follow-up agreed with ClickHouse#123673. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #123590 (comment)
Related: #123368
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
FileLogdelivering a file's rows again when a hard link or a symbolic link to that file appears in the watched directory. Such a file is now read once, under one of its names, and when that name is removed it is read on from the same position under another of its names that the table has already seen. On macOS, the file can still be read again from the beginning when one of its hard links is removed or replaced.Description
On 26.8.10.6, a hard link to a file that a
FileLogdirectory table already reads (ln app.log alias.log) makes the next read deliver consumed rows again:alias.log 1, 2, 3, app.log 1, 2, 3instead ofapp.log 3. A symbolic link does the same. Debug builds abort (master stress test STID 2508-417e).Cause:
StorageFileLogassumed one name per inode.Change: a file is read under one name; its other names are recorded, not read, and writes through them are read under the read name. Skipping the second name (master since #123590) fails: after
ln app.log new.log; rm app.logmaster stops reading the file. When the read name goes away, another recorded name reads on from the same offset, hard links first, even one the glob excludes. A hard link counts until removed or renamed over, so batching does not matter. A file renamed in over a name drops that name's stale meta file (master:LOGICAL_ERRORon the next read). A symbolic link counts whilestatshows it points to the file, unless it resolves through a name in the directory that the table reads or records, which it follows.CREATEandATTACHdo the same.Limit: a name already gone when its event is processed is not recorded, so
rm a; ln b c; rm b; ln c d; rm cin one batch readsdfrom the start. These are an inode reuse's events, where keeping the offset loses rows. On macOS the snapshot watcher reports the other names as new when one name is removed, so the file is read again there.Validation: the new test aborts on master and passes 50/50; each rule has an arm failing without it.
Upgrade: of the meta files 26.8 left per name, startup keeps the one with the larger offset.
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/123673]