Skip to content

Read a FileLog file again from the beginning after it is truncated in place - #123390

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:filelog-copytruncate-restart
Oct 2, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:filelog-copytruncate-restart

Conversation

@groeneai

@groeneai groeneai commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • Improvement

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

FileLog now reads a file again from the beginning if, at the next read, it is shorter than the offset already read from it (for example after logrotate with copytruncate), instead of failing every read of the table with CANNOT_READ_ALL_DATA until the file grows back to its old size.

Description

Related: #123368 (the copytruncate rotation item).

copytruncate keeps the inode and drops the size, so FileLog keeps the saved offset of that inode, and StorageFileLog::openFilesAndSetPos threw Last saved offsset for File app.log is bigger than the file size. That check runs for all files before any source is built, so every round failed: the materialized view stopped reading every file of the table, and a direct SELECT failed. Once the file grew back to its old size, reading resumed at the old offset, skipping the lines written after the truncation. The same applied to the first read after ATTACH or a restart.

Now a file smaller than its saved offset is read again from offset 0: its offset file is removed first (else serialize raises LOGICAL_ERROR on the smaller offset, as the DW_ITEM_ADDED path already handles), the offset is reset, and an Information line is logged. A file truncated and regrown to at least its old size between two reads cannot be detected by size and is still read from the old offset. The rotated copy app.log.1, if left in the watched directory, is a new inode and is read as a new file; that belongs to the issue's file selection item.

02023 and 02025 asserted the old exception; those arms are removed in favor of the new test (after ATTACH and live).

Validation: the new test fails on master ((6 > 5), (6 > 2), the untouched file not read) and passes with the fix; the issue's materialized view setup loses no row; dropping the offset-file removal fails the test with that LOGICAL_ERROR; all FileLog tests and 50/50 randomized runs pass.

copytruncate was never supported (the exception was deliberate and asserted by tests since 2021), hence Improvement.

🤖 Generated with Claude Code


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

Version info

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

… place

logrotate with copytruncate truncates the file in place: the inode stays,
the size drops. FileLog keeps one offset per inode, so the saved offset
survived and openFilesAndSetPos threw CANNOT_READ_ALL_DATA ("Last saved
offsset ... is bigger than the file size"). That function runs once per
round for all files of the table before any source is built, so one
truncated file failed every round of the whole table until the file grew
back to its old size, and then the bytes before the old offset were
skipped.

Now a file smaller than its saved offset is read again from offset 0. The
on-disk offset file is removed before the reset, because serialize ->
checkOffsetIsValid raises LOGICAL_ERROR when the stored offset is larger
than the new one (as the DW_ITEM_ADDED path already handles). Removing
first keeps the old offset in memory if the removal throws, so the next
round retries.

A file truncated and regrown to at least its old size between two reads is
not detectable by size and is still read from the old offset.

The arms of 02023 and 02025 that asserted the removed exception are
deleted; 05316 covers the new behavior after ATTACH and live.

Related: ClickHouse#123368

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: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 0 findings) plus my own cold review (1 nit).

# Sev Finding Verdict Evidence / action
1 💡 05316 removes ${logs_dir} without the :? guard the other FileLog tests use AGREE, noted, not blocking shell_config.sh always exports CLICKHOUSE_TEST_UNIQUE_NAME, so the path is never empty

Severity: ❌ blocker / ⚠️ major / 💡 nit.

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

@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. 05316_file_log_copytruncate fails on every run on master: the trigger is a file truncation, not a setting.
b Root cause explained? Truncation in place keeps the inode, so the saved offset survives while the file is shorter. openFilesAndSetPos threw on that, and it runs for every file before any source is built, so the whole table stopped.
c Fix matches root cause? Yes. The check that detects the shrink now restarts that file from 0 instead of failing the round; the stale offset file is removed first because serialize refuses a smaller offset.
d Test intent preserved / new tests added? New test with an ATTACH arm and a live arm, plus an untouched file in the same table. The 02023/02025 arms that asserted the removed exception are deleted.
e Both directions demonstrated? Master: (6 > 5) and (6 > 2) CANNOT_READ_ALL_DATA, untouched file not read. Fix: passes. Without the offset-file removal: LOGICAL_ERROR (6 > 0).
f Fix is general across code paths? Both readers (materialized view round, direct SELECT) and both entry states (live, after ATTACH/restart) go through this one function; single-file tables too. A truncation in the middle of a read is reset on the next round.
g Fix generalizes across inputs (params/datatypes/wrappers)? Byte offsets, so format-independent. Checked truncation to 0, to shorter non-empty content, and a regrow to exactly the old size (not a shrink, unchanged).
h Backward compatible? (maintainer-approved exception only) No setting and no metadata format change. Only the path that used to throw behaves differently.
i Invariants and contracts preserved? The on-disk offset is never larger than the in-memory one when serialize runs (remove, then reset), under file_infos_mutex and before any source exists. A crash between the two leaves no offset file, which also starts the file at 0.

Session id: cron:clickhouse-impl-slot-40:20261002-011100

@clickhouse-gh

clickhouse-gh Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [d99b059]

Summary: ✅


AI Review

Summary

This PR teaches FileLog to recover from in-place truncation by resetting a file's saved offset to 0 when the next open sees the file shorter than the recorded position, and it adds a focused stateless regression test covering both the ATTACH path and the live directory-watcher path. I do not see any unresolved correctness, metadata-consistency, or coverage gaps in the current revision.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit d99b059.

Metric Baseline Current Δ
Lines 88.30% 88.30% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 14/14 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Oct 2, 2026
Comment thread src/Storages/FileLog/StorageFileLog.cpp Resolved
The reset to offset 0 happens only when the file is still shorter than its
saved offset at the next read. A file truncated and regrown to at least that
offset before then is read from the old offset, so the docs now say so.

The live arm of 05316 truncated app.log once, right after ATTACH. The
directory watcher installs its inotify watch asynchronously, so an overwrite
that lands first produces no event and the polling reads time out. The
overwrite is now repeated on every attempt; the content is the same each time.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@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 d99b0591e with master bbba881bc (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.

Compile time of recompiled translation units

7 translation units recompiled, 11 s compile time in total, 7 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 2, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 2, 2026
Merged via the queue into ClickHouse:master with commit e3097ed Oct 2, 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 2, 2026
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-improvement Pull request with some product improvements 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