Repository navigation
Fix EmbeddedRocksDB backup to include table data - #109327
Conversation
StorageEmbeddedRocksDB did not override backupData()/restoreDataFromBackup(), so BACKUP only captured table metadata and RESTORE silently produced an empty table (data loss). Implement a logical dump: back up all key/value pairs into a single compressed data.bin entry and re-insert them on restore. RocksDB stores keys/values as raw serialized strings, so the bytes are copied verbatim and every primary key layout and value type round-trips without type-specific handling. Closes: ClickHouse#109213 Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-2:20260703-155000 |
|
cc @kssenii @vitlibar — could you review this? |
|
Workflow [PR], commit [418b437] Summary: ✅
AI ReviewSummaryThis PR implements Findings❌ Blockers
Final Verdict
LLVM Coverage ReportMeasured on commit 418b437.
Changed lines: Changed C/C++ lines covered: 291/304 (95.72%) · Uncovered code |
Address review feedback on EmbeddedRocksDB backup/restore: - TTL tables: back up and restore raw values through GetRootDB() instead of the DBWithTTL wrapper. The wrapper's iterator strips the trailing 4-byte creation timestamp and its Write() appends a fresh one, so the previous code reset every row's expiration on restore (rows about to expire were resurrected for another full TTL interval). GetRootDB() copies the timestamp suffix verbatim in both directions, preserving the original expiration. For non-ttl tables GetRootDB() is the DB itself, so the bytes are unchanged. - Restore no longer accumulates the whole table into a single WriteBatch before writing. It flushes once the batch reaches 64 MiB, so restoring a large on-disk table does not require a second full copy of the data in RAM. Regression test covers TTL expiration preservation and a large-table restore that crosses the flush threshold. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Follow-up commit 6bb0a1a addresses the two review comments (TTL expiration preservation + incremental restore flush). Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-7:20260703-171000 |
A read_only table opens its RocksDB handle with OpenForReadOnly() (ttl = 0) or
DBWithTTL::Open(..., read_only) (ttl > 0), both of which reject writes. The
restore path issues an unconditional Write(), so restoring a backup whose table
definition has read_only = true failed with an opaque RocksDB write error
("Not supported in compacted db mode"). Reject it up front in
restoreDataFromBackup() with a clear CANNOT_RESTORE_TABLE error instead.
Backup of a read_only table still works (it only reads); only the data restore
is rejected, so users get an actionable message pointing at a writable table.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Follow-up: handle read_only EmbeddedRocksDB tables on RESTORE (commit cfcd962). Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-3:20260703-182200 |
…_only restore restoreDataFromBackup() now distinguishes three cases instead of treating a missing data.bin as an empty table. backupData() always writes data.bin (zero records for an empty table), so an absent data.bin no longer means "empty". It means the backup predates the data fix (metadata only) or is corrupted. Restoring it as an empty table would silently reintroduce the issue ClickHouse#109213 data loss, so restore now fails closed with CANNOT_RESTORE_TABLE. A structure_only restore still skips data and is unaffected. The read_only rejection is narrowed: a read_only table over an external RocksDB directory cannot take the restore Write(), but an empty backup needs no Write(), so an empty read_only table is restored as pure metadata. A read_only backup that carries rows is still rejected up front. Tests: 04401 covers the data-less fail-closed case (plus structure_only restore still allowed); 04402 covers the empty read_only metadata restore alongside the existing non-empty read_only rejection. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Follow-up to the two inline change requests (data-less backup + empty read_only restore). Updated pre-PR validation gate for commit eba2b8c. Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-6:20260703-192400 |
An empty backup restored into a read_only table whose external RocksDB directory already holds rows was silently accepted, leaving the stale rows in place. The zero-row read_only branch returned early and skipped the allow_non_empty_tables non-empty-table guard. Compute whether the backup carries rows once, reject a read_only restore only when the backup is non-empty, run the non-empty-table guard for read_only tables too, and skip the actual Write() only when the backup has no rows. An empty backup over a pre-populated read_only directory now fails with CANNOT_RESTORE_TABLE unless allow_non_empty_tables is set. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Pre-PR validation gate for 7736cb1 (empty read_only restore non-empty guard): Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-7:20260703-202600 |
CI finish ledger — 7736cb1Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only
None of these are PR-caused: this PR changes only Session id: cron:our-pr-ci-monitor:20260704-010000 |
|
Fixing PR for the two It adds Post-merge CIDB confirms it is taking effect: both tests now run and pass on re-provisioned hosts (32 OK vs 8 FAIL per test since the merge), with the remaining failures confined to a single not-yet-re-provisioned straggler host. These lines were never PR-caused (0 master failures over 30 days; a RocksDB backup C++ change cannot affect the macOS Python env). |
Address review feedback on the two CANNOT_RESTORE_TABLE messages in restoreDataFromBackup(), text only, no logic change. - data.bin-absent branch: a structure_only = true backup is a legitimate supported mode that intentionally omits data.bin, so the message no longer implies old/corrupt. It now tells the user to restore such a backup with SETTINGS structure_only = true, and only calls the backup pre-fix or corrupted otherwise. - read_only-reject branch: spell out the concrete writable-restore workaround (pre-create a writable EmbeddedRocksDB table with the same schema and ttl, then RESTORE ... AS <writable_table> SETTINGS allow_different_table_def = 1). Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Pushed 1b1e1a9 (rebased onto current PR head 8e64a1e, which merged master): reworded the two CANNOT_RESTORE_TABLE exception messages per review, text only, no logic change. Verified compiled into the binary and that the structure_only data-restore rejection fires with the new wording; existing 04401/04402 cases assert only the error-code name (CANNOT_RESTORE_TABLE), so no .reference change needed. Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-3:20260716-084100 |
Several EmbeddedRocksDB tables (one writable plus any number of read_only)
may share a single rocksdb_dir. The previous backup path dumped the shared
RocksDB once per table from independent snapshots on different handles, and
restore replayed each dump. That broke the supported shared-dir case: a
BACKUP DATABASE of {rw, ro} was unrestorable (the read_only sibling got its
own non-empty data.bin and tripped the read_only restore rejection), and
racing writes between the per-table dumps could restore divergent copies.
Elect a single owner per rocksdb_dir, mirroring StorageKeeperMap. The owner
(the writable table when present, whose handle also sees the freshest data)
backs up the shared RocksDB once; sibling tables reference that data entry
instead of dumping again. On restore only the owner replays the data; the
read_only rejection now applies only when a read_only table is itself the
owner (no writable sibling exists). Coordination keys are host-qualified in
the on-cluster case because rocksdb_dir is a host-local filesystem path.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
Co-Authored-By: Claude Opus 4.8 <[email protected]>
CI finish ledger — 7ec0b35Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
The
Session id: cron:our-pr-ci-monitor:20260807-010000 |
|
Dear @jkartseva, 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. |
|
@groeneai merge master into this PR |
|
Merged Verified locally before pushing, since the branch was 18358 commits behind:
On the CI that is starting: the One item from my pre-push review, from reading the code rather than from a run, so please treat it as a question and not as a report: |
|
The feedback from the failing style checker:
Replace the stateless test with an integration test. |
|
Doing that. All seven cases in
I will post the commit here once the integration test passes locally. |
…ration test 04402_rocksdb_backup_restore_readonly.sh created and removed RocksDB directories under the server's user_files path, which a stateless test may not do (check_no_server_data_manipulation, added in e1fc810): the suite runs against object storage, shared MergeTree and encrypted disks, where a server-side path is not the test's to touch. All seven cases move to tests/integration/test_rocksdb_backup_restore, which owns its server: the read_only restore rejection, an empty read_only backup, an empty backup over an already-populated read_only directory, the shared-dir {rw, ro} single-owner dedup, an all-read_only group keeping independent snapshots, and the ttl and schema mismatch rejections. The assertions are rewritten to match the specific rejection message rather than only the error code, so one guard's failure can no longer be reported as another guard's success: the shell version printed its success marker whenever grep found nothing, including when RESTORE failed with an unrelated error. The ttl and schema cases also gain the reverse direction they described but did not exercise (a ttl 5 backup into a ttl 0 target, and a target whose value columns are in a different order). 04401_rocksdb_backup_restore.sql stays in 0_stateless: it only uses BACKUP and RESTORE with Memory() backups and never reads a server path.
|
Done in Two things I changed in the move rather than porting as they were:
Verified with the integration runner: 7/7, and 21/21 over three repeats in random order. As a control I ran the same test against a master binary without this PR and 6 of the 7 fail; the exception is the empty- |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes15 object files changed (+185.60 KiB total), 0 added.
716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units30 translation units recompiled, 174 s compile time in total, 30 of them have a recent master baseline. |
finalizeRestoreFromBackup() reopens a read_only table's RocksDB handle so that a
read_only sibling sharing a rocksdb_dir observes the rows the writable owner
replayed. RestorerFromBackup::finalizeTables() calls it holding only
lockForShare, so unlike truncate() and drop(), which run under an exclusive
table lock, it does not exclude readers. The full-scan read path takes
rocksdb_ptr_mx only to create a rocksdb::Iterator and then iterates without it,
and rocksdb_ptr was a unique_ptr, so closing the handle there destroyed a
database that an iterator still pointed into. RocksDB traps that in
~ColumnFamilySet(), which asserts the last reference is gone: a debug build
aborts the server, and a build with assertions disabled reads freed memory.
So RESTORE DATABASE of a {writable, read_only} pair on one rocksdb_dir killed
the server whenever a full scan of the read_only table was in flight.
rocksdb_ptr becomes a shared_ptr and the reading source keeps the handle it took
its iterator from, which also means a scan that starts before the reopen
finishes on the snapshot it began with. finalizeRestoreFromBackup() releases its
reference rather than closing the handle, leaving the close to whichever owner
turns out to be last.
Co-Authored-By: Claude Opus 5 <[email protected]>
Each case in 04401_rocksdb_backup_restore drops a table and then restores into the same name. InterpreterDropQuery silently ignores a DROP of a table that stores data on disk when ignore_drop_queries_probability is non-zero, which the stress runner sets to 0.2 for every client, so the table would survive and the RESTORE that follows would take the already-exists path and produce a different answer. EmbeddedRocksDB stores data on disk, so the test has to pin the setting. Co-Authored-By: Claude Opus 5 <[email protected]>
Closes: #109213
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
BACKUPofEmbeddedRocksDBtables. Previously only the table metadata was stored in the backup and the actual data was silently dropped, soRESTOREproduced an empty table. The backup now includes all key-value pairs and restores them correctly.Description
StorageEmbeddedRocksDBdid not overridebackupData()/restoreDataFromBackup(), so it fell through to the no-opIStoragedefaults:BACKUPcaptured onlymetadata/<db>/<table>.sqland no data files, andRESTOREreported success while producing an empty table (silent data loss).Reproduction before the fix
Fix
Implement both entrypoints as a logical dump, following the existing
StorageKeeperMap/StorageMemorypattern:backupData()iterates every key-value pair over an implicit RocksDB snapshot and writes them as length-prefixed binary records into one compresseddata.binentry, produced lazily viaIBackupEntriesLazyBatch.restoreDataFromBackup()readsdata.binback and re-inserts the pairs with arocksdb::WriteBatch. Restoring into a non-empty table is rejected unlessallow_non_empty_tablesis set, matching the other engines.rocksdb_dir(one writable plusread_onlysiblings) elect a single owner, so the shared database is dumped and replayed once, andfinalizeRestoreFromBackup()reopens the siblings' handles so they see the restored rows. That step holds only a shared table lock, so a full scan may be iterating a handle as it is replaced:rocksdb_ptris now ashared_ptrthe reading source keeps alive for its iterator's lifetime, and the reopen releases its reference instead of closing, since closing a database under a live iterator aborts inside RocksDB. A running scan keeps the snapshot it started on.Keys and values are already raw serialized strings, so bytes are copied verbatim and every key layout and value type round-trips without type-specific handling.
Restoring into a shared directory that no longer exists is unsupported: a
read_onlyhandle can only open an existing RocksDB directory, which is a property of the engine.Coverage:
04401_rocksdb_backup_restorefor the round-trip, the non-empty guard, composite keys and complex value types;tests/integration/test_rocksdb_backup_restorefor the on-diskrocksdb_dircases, including the shared-dir dedup, theread_only, ttl and schema rejections, and aRESTOREracing a full scan of a sibling.Workflow [PR]
Sync PR [sync-upstream/pr/109327]