Repository navigation
Do not let one bad file stop a FileLog table - #123595
alexey-milovidov merged 9 commits into
Conversation
A FileLog table opened every changed file in openFilesAndSetPos() and threw CANNOT_READ_ALL_DATA "Stream is in bad state" for the whole table when one of them could not be opened (removed after the watcher event, a dangling symlink, not readable by the server). In the materialized view path that call runs before updateFileInfos(), the only place where watcher events are consumed, so even the REMOVED event of that file was never applied: every later round threw at the same file and the table stopped for all files until it was re-attached. Now a file that cannot be opened for a reason of its own (ENOENT, EACCES, EPERM, ELOOP) is logged once, skipped and retried on every round. While such a file exists, a directory table polls with the existing backoff instead of waiting for a directory event, because fixing a permission or restoring a symlink target produces none. Other open errors (EMFILE, ENFILE, ...) fail the round as before. With the default handle_error_mode, one record that does not parse failed the whole insert of the round in the materialized view path, so the rows already read from the other files and the good rows of the same batch were not inserted. While streaming to views, such a record (a parse error, isParseError()) is now skipped and one error per file per chunk is logged; other exceptions still fail the round. A call that only skipped records while the files still have data returns a chunk without rows instead of finishing the source, so the remaining records are read in the same round. A direct SELECT still throws. Related: ClickHouse#123368 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…opened file A call of FileLogSource::generate() that only skipped broken records used to return a chunk without rows to keep reading in the same round. Every view runs its query on every block it receives, so a view whose query gives a row on empty input (an aggregate without GROUP BY, such as SELECT count()) wrote a fake row for each such chunk. Now such a call closes the stream as before and tells the storage that its files still have data; streamToViews() then reports the round as not stalled and threadFunc() starts the next round at once, from the offset close() stored, instead of waiting for a directory event. Each such round consumes at least one record, so this terminates. While a file could not be opened, its path may start to point to another file (the target of a symlink is created again). openFilesAndSetPos() kept the old inode and its offset, so the new file was read from the old file's offset and its meta was stored under the old inode; after a reload the table did not find that inode and read the file again. A reopened file whose inode changed now drops the old meta (in memory and on disk) and is read from the start. The engine documentation now says which files are retried: a symlink is read only if its target exists when the table finds it, which is unchanged. The test gains a count() view (no zero rows), a direct SELECT table (a removed symlink target logged once, the new target read once across DETACH/ATTACH, a parse error still fails the query) and exact log counts for the skipped records. Related: ClickHouse#123368 Co-Authored-By: Claude Opus 5.5 <[email protected]>
The direct `SELECT` arm of `05318_file_log_one_bad_file` used a `FileLog` table on a directory. On macOS the directory watcher keeps a watch opened through the symlink, so removing its target makes it report `link.jsonl` as REMOVED; the first direct `SELECT` consumed that event in `updateFileInfos` before `openFilesAndSetPos`, the file was dropped without an open attempt, and the `Cannot open file` log count was 0 instead of 1. Linux has no event for a target outside the watched directory, so it passed there. The arm now uses a table on the single file `link.jsonl`, which has no directory watcher: every direct `SELECT` opens the file on both platforms. The record that does not parse is appended to the same file, so no second file and no extra reload are needed. The skipped-record summaries are now also checked to count at most 10 records each (one `generate` call stops after 10 failed polls), so a single summary for the whole file no longer passes. Related: ClickHouse#123368 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ld inode A file that a round saw missing (`ENOENT`, `ELOOP`) is now read from its start when it can be opened again, even if the new file has the old inode number, which ext4 gives to the next file created after a removal. Before, only a changed inode number reset the offset in `openFilesAndSetPos`, so such a file was read from the old offset: its first bytes were skipped, or, if it was shorter, the shrink check failed the round. The new `path_missing` flag of `FileContext` records the missing path until the file is opened again; it is cleared after `getInode`, so a file that vanishes again before the `stat` is still treated as new in the next round. The test's direct `SELECT` arm now recreates its target with the same inode after it was read. Related: ClickHouse#123368 Co-Authored-By: Claude Opus 5.5 <[email protected]>
When a round finds the path of a file missing (`ENOENT`, `ELOOP`), its saved offset is now reset to 0 at once, in memory and in the meta file. Before, only an in-memory flag recorded it, so after `DETACH`/`ATTACH` or a server restart a file created again with the old inode number before the next read was read from the old offset and its first bytes were lost. The `path_missing` flag is removed; a reopened file whose path now leads to another inode is still re-keyed. The test's direct `SELECT` arm reloads the table before it reads the recreated target, and recreates it once more with a new inode to check that it is not read again after a reload. Related: ClickHouse#123368 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…directory events A regular file of a watched directory whose path is gone (`ENOENT`) or holds another inode when it is opened is now left to the pending directory events, which drop it or give its offset to the new name. Before, the restart from offset 0 of a missing path also hit a file renamed inside the directory, so it was read again from its start, and the reopen of a file that could not be opened before took over the offset of a file moved over it (and, in a debug build, failed the `meta_by_inode` size assertion). The restart and the re-key stay for single-file tables and symlinks, whose target changes produce no directory event. Two test arms: a file renamed after a partial read, and a file moved over a symlink that cannot be opened. Related: ClickHouse#123368 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…file fails the round The flag that makes a directory table retry a file that could not be opened on a timer (`has_files_to_reopen`) is now set as soon as the file fails in `openFilesAndSetPos`, so an exception from a later file of the same round no longer turns the retry into a wait for a directory event. A test arm checks that skipped records are reported per file when one read covers two files. The test waits 60 s instead of 120 s per step. The documentation says the retry holds while the table is loaded. Related: ClickHouse#123368 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop two comments that restate the read_more_after_skipped_records field doc and the isTrackedByDirectoryEvents doc; shorten the inode-change comment. 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; 16 findings over six rounds) plus my own review (14 findings). Bounce cap reached: published as-is after round 6, the final round. Its fix commit
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261002-172600 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20261002-164503 |
|
Workflow [PR], commit [f04cc38] Summary: ✅
AI ReviewSummaryThis PR hardens Findings
Final Verdict
LLVM Coverage ReportMeasured on commit f04cc38.
Changed lines: Changed C/C++ lines covered: 131/133 (98.50%) · Uncovered code |
|
|
||
| return updateFileInfos(); | ||
| bool stalled = updateFileInfos(); | ||
| return stalled && !read_more_after_skipped_records; |
There was a problem hiding this comment.
read_more_after_skipped_records overrides the stalled return unconditionally here, so it also bypasses the new backoff path for has_files_to_reopen. If one file is still failing to open and another file hits a long run of parse errors, streamToViews keeps returning false, the outer loop stays inside the same background task, and the unreadable file is retried immediately until MAX_THREAD_WORK_DURATION_MS expires instead of on poll_directory_watch_events_backoff_*.
That breaks the retry contract this PR is adding for ENOENT/EACCES/EPERM and can monopolize the background thread for up to 60 s. I think the stalled/backoff path still needs to win whenever has_files_to_reopen is set, even if another stream asked for one more immediate round to drain skipped records.
Build profile diff (arm_release)Comparing Binary sizes
The official master build is compiled with Object file sizes
|
| Object file | Master | PR | Δ |
|---|---|---|---|
src/CMakeFiles/clickhouse_common_io.dir/Common/FrequencyHolder.cpp.o |
1.12 MiB | 207.93 KiB | -943.97 KiB (-81.95%) |
src/CMakeFiles/dbms.dir/Storages/FileLog/FileLogSource.cpp.o |
108.90 KiB | 129.45 KiB | +20.55 KiB (+18.87%) |
720 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, 30 s compile time in total, 11 of them have a recent master baseline.
Median compile-time ratio to the baselines is ×1.11 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost +3.7 s (+14%) in total before that adjustment.
`threadFunc` counts the attached views twice: once to decide whether to stream, and again at the end to choose between a timed reschedule and waiting for a directory event. Both calls take the global `databases_mutex`. When a materialized view is created between the two calls, the first one sees no view and nothing is read, the second one sees the view, and the task waits for a directory event. Files that were already in the directory are then not read until one of them changes, or forever in a directory that does not change. Under parallel DDL the second call can wait for the mutex long enough for this to happen. All 5 failures of the new test in its flaky check (100 runs) were this: 3 tables started their first round 203 to 371 s after `CREATE MATERIALIZED VIEW`, when another file was moved into the directory, and 2 never started; every other table started within about a second. When the first check finds no view, reschedule after the backoff, as is already done when the second check finds none. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…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]>
Related: #123368
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
A
FileLogfile that cannot be opened (removed, a symlink whose target was removed, not readable by the server) no longer stops the table: the other files keep being consumed, and it is skipped, logged once and retried until it can be opened while the table is loaded, unless the directory reports that it was removed, renamed or replaced. With the defaulthandle_error_mode, a record that cannot be parsed is now logged and skipped while the table streams into materialized views, instead of failing the whole insert together with the rows read from the other files; a directSELECTstill throws.Description
On 26.8.10.6 and 26.3.33.24, one file of a
FileLogdirectory that cannot be opened makes every read fail withCANNOT_READ_ALL_DATA, and a table that streams into materialized views inserts nothing for any file, even after that file is removed. With the defaulthandle_error_mode, one line that does not parse fails the whole insert, so the rows read from the other files are lost.Root cause:
openFilesAndSetPosthrows for the whole table when one file cannot be opened, and in the materialized view path it runs beforeupdateFileInfosconsumes the watcher events, so the file's removal is never applied.FileLogSourceturns the parse error of one record into an exception for the whole insert.Change:
ENOENT,EACCES,EPERM,ELOOP) is logged once, skipped and retried on every round, and read from its start if it was missing. A regular file of a watched directory that is gone or replaced is left to the directory events, which keep a renamed file's offset. While a file is retried, a directory table polls with the existing backoff, because restoring a symlink target or a permission produces no directory event. Other errors (EMFILE) still fail the round.Validation: new test
05318_file_log_one_bad_filefails on master and each of seventeen mutations turns it red; 100 randomized runs and the 19 FileLog tests pass. With items 1a (#123395) and 1b (#123404), a.gzmoved into the directory no longer stops the table.Workflow [PR]
Sync PR [sync-upstream/pr/123595]
Version info
26.10.1.1437-master(included in26.10and later)