Skip to content

Read a file moved into a FileLog directory from the beginning when it reuses a deleted file's inode - #123593

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai-filelog-rename-in-reused-inode
Oct 3, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai-filelog-rename-in-reused-inode

Conversation

@groeneai

@groeneai groeneai commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #123368

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fixed FileLog silently skipping the first rows of a file moved into the watched directory from elsewhere (for example a staging file renamed into place) when that file got the inode of a file just deleted from the directory. Such a file is now read from the beginning.

Description

On 26.8 and 26.3, a file renamed into a FileLog directory right after another file there was deleted can get that file's inode, and FileLog then reads it from the deleted file's offset, silently skipping its first rows (or failing every read with Last saved offsset ... is bigger than the file size when the new file is shorter). ext4 gives a freed inode to the next new file, so the "write to a staging file, then mv it into place" producer pattern hits this when the delete and the rename land in one watcher batch. Found by the review bot on #123542.

Cause: the Linux directory watcher reported every IN_MOVED_TO as a rename and dropped the inotify cookie, so a file arriving from outside the directory looked like the second half of a rename inside it, and StorageFileLog kept the old offset of the matching inode.

Change: the Linux watcher pairs IN_MOVED_FROM/IN_MOVED_TO by cookie (a bounded FIFO kept across read() calls). An unpaired IN_MOVED_TO is reported as an added file, which is already read from offset 0. The macOS watcher already reports an arrival as added, and no longer treats an unlinked name as a rename source. A file moved out of the directory and back in within one batch is now read again from the start (duplicates instead of loss). Not covered: a file that takes a deleted file's inode while the server is stopped is still matched by inode at startup.

Validation: new test 05319_file_log_rename_in_reused_inode fails on master (rows 10, 20 are missing) and passes with the fix. Its in-directory rename arm fails if every arrival is treated as new. Reproduced with a recycled inode (no hard link) on master, 26.8.10.6 and 26.3.33.24. All FileLog stateless tests pass.


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

Version info

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

On Linux the directory watcher reported every IN_MOVED_TO as DW_ITEM_MOVED_TO
and dropped the inotify cookie. StorageFileLog treats a MOVED_TO whose inode is
already tracked as a rename and keeps the old read offset. When a tracked file
is deleted and a file written elsewhere on the same filesystem is moved in
within one watcher batch, ext4 often gives the new file the freed inode, so
the new file was read from the deleted file's offset and its first rows were
silently skipped (or every read failed with "Last saved offsset ... is bigger
than the file size" when the new file was shorter).

The Linux watcher now pairs IN_MOVED_FROM and IN_MOVED_TO by cookie. Only a
rename inside the directory delivers both halves; an unpaired IN_MOVED_TO is
reported as DW_ITEM_ADDED, which resets the offset. The cookies of moves out
of the directory never get paired, so they are kept in a FIFO bounded at 1024
entries that survives read() boundaries (inotify may split a pair across two
reads). Eviction only turns a rename into a re-read, never into a skip.

The macOS snapshot watcher already reports an arrival from elsewhere as
DW_ITEM_ADDED. It still synthesized a rename when an unlinked name's inode
reappeared under another name through a hard link; is_rename now ignores
names that got NOTE_DELETE, matching what Linux emits for the same sequence.

Behaviour change toward re-reading: a file moved out of the directory and back
in within one batch is now read again from the start.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-review-bot PR origin: review-bot finding on a groeneai PR labels Oct 2, 2026
@groeneai

groeneai commented Oct 2, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 1 round, 2 findings), plus my own review (2 findings).

# Sev Finding Verdict Evidence / action
1 ❌ A read between the DW_ITEM_MOVED_FROM and DW_ITEM_MOVED_TO of one rename erases the source's offset, so the renamed file is read again DISAGREE Same on master, where the two halves also reach the storage by separate calls (DirectoryWatcherBase.cpp:139-148, FileLogDirectoryWatcher.cpp:14-20); the result is duplicate rows, not skipped rows, and this PR does not change that path
2 💡 With a selective event mask, outside arrivals are missed or renames become additions DISAGREE The only watcher is created with the default all-events mask (FileLogDirectoryWatcher.cpp:10); master behaves the same for such masks
3 💡 (own) The macOS is_rename guard has no test that fails without it AGREE, noted On macOS the rescan after rm usually runs before the mv; the guard covers both landing in one rescan
4 💡 (own) A file moved out of the directory and back in within one batch is read again AGREE, noted Intended: re-read instead of a possible skip; stated in the description

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-8:20261002-165000

@clickhouse-gh

clickhouse-gh Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [edf3781]

Summary: ✅


AI Review

Summary

This PR teaches the FileLog directory watchers to distinguish a true in-directory rename from a file moved in from elsewhere when the inode matches a recently deleted file, and adds a focused regression test for the Linux path. I did not find a correctness or safety regression in the current patch: the Linux watcher now preserves offsets only for paired move cookies, and the macOS snapshot path no longer treats an unlinked name as a rename source.

Missing context / blind spots
  • ⚠️ The OS_DARWIN branch changes behavior, but the added regression coverage exercises only the Linux watcher path. A macOS-specific repro or manual validation would close that gap.
Final Verdict

Status: ✅ Approve

LLVM Coverage Report

⚠️ No coverage measurement for commit edf3781: incomplete coverage measurement: 2 of 21 shard profiles are missing: LLVM_COVERAGE_FILE_it_6.profdata, LLVM_COVERAGE_FILE_it_7.profdata.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 2, 2026
@groeneai
groeneai requested review from Algunenano and kssenii October 2, 2026 17:29
@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
@clickhouse-gh

clickhouse-gh Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing edf378148 with master abd6a6d5e (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

25 object files changed (+17.76 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Storages/FileLog/DirectoryWatcherBase.cpp.o 81.34 KiB 99.25 KiB +17.91 KiB (+22.02%)

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

8 translation units recompiled, 9 s compile time in total, 8 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.

LGTM

@alexey-milovidov alexey-milovidov self-assigned this Oct 3, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 3, 2026
Merged via the queue into ClickHouse:master with commit 618787f Oct 3, 2026
149 of 150 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 3, 2026
alexey-milovidov added a commit to groeneai/ClickHouse that referenced this pull request Oct 4, 2026
…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]>
Algunenano pushed a commit to Algunenano/ClickHouse that referenced this pull request Nov 24, 2026
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]>
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-review-bot PR origin: review-bot finding on a groeneai PR pr-bugfix Pull request with bugfix, not backported by default 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