Repository navigation
Do not fail FileLog reads on a last line that is still being written - #123404
Conversation
`FileLogConsumer::readNewRecords` splits every file into records with
`std::getline`. When the bytes before the end of a file have no trailing
'\n' (a writer in the middle of a line, a buffered writer that flushed
mid-line, a file without a final newline), `getline` stops at EOF and
sets `eofbit`, and the next `assertStreamGood` threw `CANNOT_READ_ALL_DATA`
("Stream is in bad state") before the offset was stored. Every read of
the file then failed the same way, outside the parser, so
`handle_error_mode = 'stream'` could not catch it.
Now an unterminated last line is not emitted: the reader is reset to the
start of that line, the offset is stored there, and the file is marked
done for this read (otherwise `pollBatch` re-reads the same tail until
`poll_timeout_ms`). The line is read from its start once it is completed:
directory tables get a MODIFIED event from the watcher, single-file
tables re-read the file on every poll. `clear()` is needed when `getline`
extracted nothing (the file shrank after it was opened); `seekg` alone
only resets `eofbit`.
The "stream reach to end" check becomes `>=`: a line completed while it
was being read ends past the size recorded at open, so the file was
never marked done and every read of it spun for about three
`poll_timeout_ms` (measured 15.5 s vs 0.5 s with `poll_timeout_ms = 5000`
and a 256 MiB line).
A file whose last line never gets a newline now keeps that line unread
instead of failing every read of the file.
Related: ClickHouse#123368
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The directory watch of a FileLog table is installed on the watcher thread after `CREATE` returns. An append made before that is never reported, so both files would stay unchanged and the bounded loop would end without their completed lines. Create a new file per iteration until a `SELECT` reads one: its `ADDED` event proves the watch is armed before the completions are written. Those sentinel rows are filtered out of the completion loop. 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; 2 findings in the first pass, 0 in the delta recheck after the fix round) plus my own review.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261002-030800 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20261002-024237 |
|
Workflow [PR], commit [317b348] Summary: ✅
AI ReviewSummaryThis PR changes Final Verdict
|
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units8 translation units recompiled, 16 s compile time in total, 8 of them have a recent master baseline. |
`reader.eof()` after `getline` also happens when the file is truncated in place after `openFilesAndSetPos` recorded `last_open_end`. Deferring the tail in that case saved the truncated offset and returned no error; once the same inode grew past that offset again, the next read started there and silently skipped the first rows of the rewritten file (measured: 100-row file truncated during a throttled direct SELECT, 29 rows returned without error, then rows 1001..1023 of the regrown file never read). Only a read that reached at least `last_open_end` is an incomplete last line. If EOF came earlier, the bytes seen at open are gone, so the read fails with `CANNOT_READ_ALL_DATA` as it does on master. The new test arm truncates the file after the first row of a throttled read and expects that error. Co-Authored-By: Claude Opus 5.5 <[email protected]>
| reader.clear(); | ||
| reader.seekg(offset); | ||
| StorageFileLog::assertStreamGood(reader); | ||
| file_ctx.status = StorageFileLog::FileStatus::NO_CHANGE; |
There was a problem hiding this comment.
Is it a right status for the case when we have already read some records from the file but this happened on the last record?
There was a problem hiding this comment.
Yes. Here NO_CHANGE means "nothing left to read until the file changes", not "nothing was read": master sets it the same way after a read that consumed records and reached last_open_end (L145-148). The status is only used to skip the file in the next readNewRecords calls of the same pollBatch and in openFilesAndSetPos.
The records read before the partial line stay in new_records, and last_writen_position becomes tellg() (L140-143), which is the start of the partial line, so those records are committed and the line is re-read from its start. The file goes back to UPDATED on its next DW_ITEM_MODIFIED (or on every read for a single-file table). Keeping UPDATED would only make pollBatch re-read the same incomplete line on its next iterations.
05317 covers this case: {"a":1}\n{"a":2 returns 1, then 2 at _offset 8 once the line is completed.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Dear @antaljanosbenjamin, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
Related: #123368
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
FileLogno longer fails withCANNOT_READ_ALL_DATA(Stream is in bad state) when a file ends with a line that has no newline yet: the complete lines are read, and the last line is read from its start once it is completed. Related to #123368.Description
On 26.8 (and 26.3), reading a
FileLogtable fails withCANNOT_READ_ALL_DATA(Stream is in bad state) whenever a file ends with a line that has no newline yet, such as a log line that is still being written, so none of that file's rows can be read until the line is completed (item 1 of #123368; measured on the official 26.8.10.6 and 26.3.33.24 images).FileLogConsumer::readNewRecordssplits files into records withstd::getline. On a last line without\nit stops at EOF and setseofbit, and the next stream check threw before the offset was stored. This happens before any parser runs, sohandle_error_mode = 'stream'cannot catch it.Now an unterminated last line is not emitted: the reader goes back to the start of that line, the offset is stored there, and the file is done for this read. The line is read whole once it is completed (directory tables through the watcher's modify event, single-file tables on the next poll). The "reached the end" check becomes
>=, so a line completed while it is being read no longer makes the read spin for about threepoll_timeout_ms(15.5 s vs 0.5 s withpoll_timeout_ms = 5000).Behavior note: a file whose last line never gets a newline keeps that line unread, instead of failing every read of the file.
The table staying stuck after such an error (
Another select query is running on this table) is the other half of item 1 and goes in a separate PR.Validation: the new test fails on master with this error and passes with the fix (50/50 randomized, also with
-j 10); the issue's materialized-view repro now ingests/a1 /a2 /a32 s after the line is completed; all existingFileLogtests pass.Workflow [PR]
Sync PR [sync-upstream/pr/123404]
Version info
26.11.1.1108-master(included in26.11and later)