Repository navigation
Stream-parse the .backup metadata file - #125712
Conversation
… one string `BackupImpl::readBackupMetadata` read the whole `.backup` file into a `String` and then ran the SAX parser over it. The file has a record per file of the backup, so for a backup of millions of files the string takes gigabytes, on top of the maps built from it. Feed the parser from the read buffer through `StdIStreamFromReadBuffer` instead, so the metadata is parsed in chunks while it is read. `std::istream` turns an exception from the buffer into `badbit`, which the parser takes for the end of the data, so the stream rethrows it instead: a read error isn't reported as a malformed manifest. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Workflow [PR], commit [6ee4932] Summary: ✅
AI ReviewSummaryPR #125712 streams Missing context / blind spots
Findings
Final Verdict |
| /// a record per file and can take gigabytes for a backup of many files. | ||
| ReadBuffer & metadata_in = *in; | ||
| /// The parser reads the stream from start to end and never seeks, so the stream doesn't need the size. | ||
| StdIStreamFromReadBuffer metadata_stream(std::move(in), /*size=*/ 0); |
There was a problem hiding this comment.
The main contract of this change is that opening a large .backup no longer needs the whole manifest in memory, but there is no measurement or regression test establishing that property. The existing 04494_backup_metadata_round_trip checks restored data, and the handler tests still call parseMemoryNP; both also pass with the previous whole-file buffering implementation. The measurements in the related PR concern DOM removal, which is a different allocation.
Please provide before/after peak-memory measurements for opening the same large backup, or a focused regression that fails if whole-file buffering returns. Include the manifest size and file count, and measure opening the backup separately from creating the fixture, so the result establishes the claimed saving on this production path.
There was a problem hiding this comment.
Measured the peak memory of opening a large backup, before and after this change.
Fixture: a MergeTree table with 101 columns, 2000 wide parts (PARTITION BY k, min_bytes_for_wide_part = 0), backed up with BACKUP TABLE t TO File(...). The backup has 824,003 files, and its .backup is 140,072,415 bytes.
Opening it, separately from creating the fixture: RESTORE TABLE t FROM File(...) SETTINGS structure_only = 1 in a fresh clickhouse local, which reads the whole manifest and restores only the definition. Peak memory of the query from --memory-usage (MemoryTrackerPeakUsage), three runs each:
| Peak memory | |
|---|---|
Before (readStringUntilEOF + parseMemoryNP) |
402,882,375 bytes, in all three runs |
| After (parsing from the read buffer) | 114,051,151 - 114,051,167 bytes |
The difference, 288.8 MB, is about twice the manifest: the string grows by doubling while it is read. What remains is the file_names and file_infos maps built from the manifest, which this change doesn't touch.
The parse result is unchanged: 04494_backup_metadata_round_trip restores a full and an incremental backup and compares the data, and it passes with this change, along with the integration tests that read manifests (test_backups_from_disk, the incremental and archive tests of test_backup_restore_new).
I didn't add a memory regression test: a fixture large enough for the difference to stand out of the rest of the query's memory takes about a minute to create, and the maps grow with the same number of files, so a threshold on a small fixture would be fragile.
Backport #125712 to 26.9: Stream-parse the `.backup` metadata file
Backport #125712 to 26.8: Stream-parse the `.backup` metadata file
Related: #109107
Related:https://github.com/ClickHouse/clickhouse-private/issues/62693
BackupImpl::readBackupMetadataread the whole.backupfile into aStringand ran the SAX parser over it. The file has a record per file of the backup, so for a backup of millions of files the string takes gigabytes. #109107 replaced the DOM tree with a SAX parser but kept this string.The parser is now fed from the read buffer through
StdIStreamFromReadBuffer, so the metadata is parsed in chunks while it is read, and the peak memory of opening a backup (RESTORE, the base of an incrementalBACKUP) no longer includes the size of its metadata file. The parser sees the same chunks as before (Poco parses memory in 4 KiB chunks too), so the result is the same.std::istreamturns an exception from the buffer intobadbit, which the parser takes for the end of the data. The stream rethrows it instead, so a read error (e.g. from S3) is reported as itself, not as a malformed manifest.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Reduced the peak memory usage of
RESTOREand of incrementalBACKUP: the metadata file of a backup is now parsed while it is read, instead of being read into memory whole first. For backups of millions of files this saves gigabytes.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/125712]
Version info
26.10.1.2412-master(included in26.10and later)26.9.20.13,26.8.28.12