Repository navigation
Fix data loss backing up a Log family table with a zero-byte data file - #119722
alexey-milovidov merged 1 commit into
Conversation
…en a column occupies zero bytes A column of aggregate function states can hold rows while occupying no bytes on disk: countResample(10, 5, 1) covers an empty range, so every state serializes to nothing, and 100 rows take 0 bytes in s.bin while __marks.mrk records them. Two independent sites read "no bytes" as "no rows", so a BACKUP/RESTORE round trip of such a table reported success and did not preserve the data. StorageLog::backupData used the size of the first data file as a proxy for an empty table. When that column is the zero-byte one, the whole data directory was omitted from the backup (every .bin, __marks.mrk, sizes.json, columns.txt, count.txt), and restoreDataFromBackup then restored nothing: 100 rows in, 0 rows out, with BACKUP_CREATED, RESTORED and CHECK TABLE all passing. Decide emptiness from the recorded size of every column first: bytes anywhere mean there is something worth preserving, whatever the row signal says. That one clause covers both engines and two more states that looking at index 0 cannot see. The first is the same defect mirrored: data_files follows physical column declaration order, so TinyLog(s, k) puts the 0-byte s.bin at index 0 and dropped k.bin's 172 recorded bytes from the backup. The second is a table whose __marks.mrk has gone missing while sizes.json still records data, which ATTACH reaches because file_checker.repair() is wrapped in a catch that only logs and loadMarks reads an absent file as an empty marks vector; classifying that empty would replace its existing loud failure at createHardLink with a successful backup holding no data. Only when no column occupies bytes does the per-engine row signal decide, and there Log asks its marks, which is where totalRows() already reads its count. TinyLog keeps none, so a TinyLog table in which no column occupies any bytes still restores empty: no row count exists on disk to recover it from. BackupImpl::copyFileToDisk materialised the destination of a zero-size entry only in Rewrite mode, while the Log and StripeLog restores are its only Append callers. Their zero-byte file was therefore never created, and every query reading that column failed with FILE_DOESNT_EXIST on a table whose RESTORE had been reported successful, permanently and past a restart. The non-empty path below reaches writeFile(), which does create a missing file, so the empty entry now does the same. The existsFile() guard is required rather than defensive: createFile() opens with O_WRONLY | O_CREAT | O_EXCL, so without it a restore onto a table that already holds data would fail with CANNOT_CREATE_FILE. Neither change is observable on its own, which is why they are one commit: with only the second there is nothing in the backup to copy, and with only the first the restore walks into the second and produces the unreadable column instead of the empty table. Splitting them would leave a state in git blame that reports a fix that does not work. The regression test is tagged log-engine: the stress and upgrade lanes rewrite Log and TinyLog to MergeTree in the test file itself while leaving the reference alone, which would invert its two negative controls instead of skipping them. Co-Authored-By: Claude Opus 5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex) plus my own cold read of the resulting
Severity: ❌ blocker / I also verified, rather than assumed, the two claims the diff's own comments make: Checks worth naming from the final round, each done by reading the source rather than trusting the
Session id: cron:clickhouse-review-self-slot-58:20260913-022400 |
Pre-PR validation gate (click to expand)
Validation: 50 of 50 randomized runs of the new test passed. Every backup and restore test that passes on this base (46 of them) still passes with the change; the rest fail identically on both binaries in this sandbox, which has no MinIO and no reachable cluster. Local style check: 14 of 15 legs pass, the last one being the settings-history leg, which refuses to run outside the CI workflow and has no setting change to look at here. Session id: cron:clickhouse-impl-self-slot-57:20260913-020800 |
|
Workflow [PR], commit [98afba8] Summary: ✅
AI ReviewSummaryThis PR fixes the two places that previously treated "zero bytes" as "no rows" during Final Verdict✅ No new findings. LLVM Coverage ReportMeasured on commit 98afba8.
Changed lines: Changed C/C++ lines covered: 17/20 (85.00%) · Uncovered code |
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, 19 s compile time in total, 8 of them have a recent master baseline. |
|
Hi — could this be a backport candidate for Affected code: Why this might apply: Both defective lines are present on every supported branch: Caveat: I verified that the modified code exists in those branches, but not whether the bug is actually reachable there — a new caller in master may be what makes this newly observable. If older branches can't reach the affected path, please ignore this; otherwise consider |
|
@groeneai @alexey-milovidov — title/description suggestions: Description (currently
Ignore if intentional. |
|
I checked the CREATE TABLE t (a Array(UInt8), k UInt64) ENGINE = Log;
INSERT INTO t SELECT [], number FROM numbers(100);
SELECT count() FROM t WHERE NOT ignore(a); -- 100
BACKUP TABLE t TO File('b'); -- BACKUP_CREATED
RESTORE TABLE t AS tr FROM File('b'); -- RESTORED
SELECT count() FROM tr WHERE NOT ignore(a); -- Code: 107 ... a.bin (FILE_DOESNT_EXIST)
Three things the measurement adds to the claim:
On backport reachability, which the candidacy comment explicitly left open: both defective lines are present on I have updated the changelog entry to name the ordinary-column case, since as written it reads as an aggregate-state-only corner. The merged test pins the aggregate-state shapes; it does not pin the |
Reproducer-- Log table holding 100 rows whose only column occupies zero bytes on disk
CREATE TABLE t1 (s AggregateFunction(countResample(10, 5, 1), UInt64, UInt64)) ENGINE = Log;
INSERT INTO t1 SELECT countResampleState(10, 5, 1)(number, number) FROM numbers(100) GROUP BY number;
SELECT total_rows FROM system.tables WHERE database = currentDatabase() AND name = 't1'; -- 100
BACKUP TABLE t1 TO Disk('backups', 'b1'); -- BACKUP_CREATED
RESTORE TABLE t1 AS t1r FROM Disk('backups', 'b1'); -- RESTORED
SELECT total_rows FROM system.tables WHERE database = currentDatabase() AND name = 't1r'; -- 0
-- Observed on 26.3/26.6/26.7/26.8: BACKUP_CREATED + RESTORED are both reported and the
-- restored table holds 0 of the 100 rows. Fixed on master by #119722 (restores 100).
--
-- Same root cause, no exotic column needed - a leading normal column keeps the data
-- directory in the backup, but the zero-byte .bin is never recreated on restore:
CREATE TABLE t2 (a Array(UInt64)) ENGINE = Log;
INSERT INTO t2 SELECT [] FROM numbers(100);
BACKUP TABLE t2 TO Disk('backups', 'b2');
RESTORE TABLE t2 AS t2r FROM Disk('backups', 'b2');
SELECT count() FROM t2r; -- Code: 107 FILE_DOESNT_EXIST: Cannot open file .../a.binResults:
Backport the fix to CC component owner: @alexey-milovidov @Michicosun Analysis metadata
Introducing change: predates `v22.8.21.38` (oldest testable release)
|
|
Follow-up on the label mechanics only, since the earlier candidacy comment suggested The affectedness behind the request is measured rather than inferred: both defective lines and the |
Related: #119700
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
BACKUPandRESTOREof aLogorTinyLogtable one of whose columns occupies zero bytes on disk while the table has rows, such as anArray,MaporNestedcolumn that is empty in every row, or a column ofcountResamplestates over an empty range. When that column came first the backup silently left out the whole data directory, so the restored table held no rows at all; otherwise it left out the zero-byte data file, so every query reading that column failed withFILE_DOESNT_EXISTon a table whoseRESTOREhad been reported successful. One case stays lossy: aTinyLogtable in which no column occupies any bytes restores empty, because such a table records no row count on disk anywhere.Description
Two sites read "no bytes" as "no rows".
StorageLog::backupDataused the size of the first data file as a proxy for an empty table. With such a column first, the whole data directory (every.bin, the marks and the size metadata) was left out of the backup andrestoreDataFromBackuprestored nothing: 100 rows in, 0 rows out, withBACKUP_CREATED,RESTOREDandCHECK TABLEall passing. Emptiness is now decided from the recorded size of every column first, and only then from the engine's row signal, which forLogis the markstotalRows()already counts from. Asking about every column also covers the mirror case, where the zero-byte column comes first and a later one holds the bytes.TinyLogkeeps no marks, so the single state it still cannot tell apart is a table in which no column occupies any bytes; no row count exists on disk to recover there, so that is left as it is.BackupImpl::copyFileToDiskmaterialised the destination of a zero-size entry only inRewritemode, while theLogandStripeLogrestores are its onlyAppendcallers. Their zero-byte file was therefore never created, and reads of that column failed permanently, past a restart. The non-empty path below reacheswriteFile(), which does create a missing file, so an empty entry now does the same. TheexistsFile()guard is required rather than defensive:createFile()opens withO_EXCL, so without it a restore onto a table that already holds data would fail.Neither change is observable alone, so they are one commit.
StripeLogandMergeTreewere measured not to be carriers. The test pins what must not change too: aTinyLogtable holding only such a column still restores empty, and a never-inserted table still backs up nothing.Workflow [PR]
Sync PR [sync-upstream/pr/119722]
Version info
26.9.1.1417(included in26.9and later)