Repository navigation
Don't copy all file infos when writing backup metadata - #109861
Conversation
|
Workflow [PR], commit [2734aae] Summary: ❌
AI ReviewSummaryThis PR replaces the copy-all Findings
Final Verdict
|
There was a problem hiding this comment.
Pull request overview
This PR reduces peak memory usage when finalizing BACKUP by avoiding materializing a full BackupFileInfos copy (potentially millions of entries) just to write .backup metadata or to clean up a corrupted backup.
Changes:
- Add
IBackupCoordination::forEachFileInfoForAllHostswith a copying default implementation. - Implement in-place iteration for local and on-cluster coordination by iterating the prepared
BackupCoordinationFileInfosstructures. - Update
BackupImpl::writeBackupMetadataandBackupImpl::tryRemoveAllFilesto iterate file infos via the new API instead of copying.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Backups/IBackupCoordination.h | Adds the new virtual iteration API for file infos across all hosts. |
| src/Backups/IBackupCoordination.cpp | Provides default implementation that falls back to getFileInfosForAllHosts. |
| src/Backups/BackupImpl.cpp | Switches metadata writing and corrupted-backup cleanup to iterate in place. |
| src/Backups/BackupCoordinationOnCluster.h | Declares override for the new iterator API. |
| src/Backups/BackupCoordinationOnCluster.cpp | Implements iterator API with coordination mutex protection. |
| src/Backups/BackupCoordinationLocal.h | Declares override for the new iterator API. |
| src/Backups/BackupCoordinationLocal.cpp | Implements iterator API with coordination mutex protection. |
| src/Backups/BackupCoordinationFileInfos.h | Adds iteration helper to traverse prepared file infos without copying. |
| src/Backups/BackupCoordinationFileInfos.cpp | Implements iteration over the prepared pointer list. |
| files_to_remove.push_back(".backup"); | ||
| for (const auto & file_info : coordination->getFileInfosForAllHosts()) | ||
| coordination->forEachFileInfoForAllHosts([&](const BackupFileInfo & file_info) | ||
| { | ||
| files_to_remove.push_back(file_info.data_file_name); | ||
| }); |
There was a problem hiding this comment.
This issue pre-existed and is out of scope of this PR.
| for (const auto & file_info : coordination->getFileInfosForAllHosts()) | ||
| coordination->forEachFileInfoForAllHosts([&](const BackupFileInfo & file_info) | ||
| { | ||
| files_to_remove.push_back(file_info.data_file_name); |
There was a problem hiding this comment.
tryRemoveAllFiles currently appends one entry per BackupFileInfo, but with deduplicate_files = 1 multiple infos intentionally share the same data_file_name. BackupWriterMemory inherits BackupWriterDefault::removeFiles, which deletes sequentially, and BackupInMemory::removeFile throws on the second delete of the same name. That means a failed deduplicated BACKUP ... TO Memory(...) will abort cleanup partway through and leave the corrupted backup behind. This list needs deduplication before it is passed to writer->removeFiles.
There was a problem hiding this comment.
This issue pre-existed and is out of scope of this PR
| void BackupCoordinationOnCluster::forEachFileInfoForAllHosts(const std::function<void(const BackupFileInfo &)> & callback) const | ||
| { | ||
| auto component_guard = Coordination::setCurrentComponent("BackupCoordinationOnCluster::forEachFileInfoForAllHosts"); | ||
| std::lock_guard lock{file_infos_mutex}; | ||
| prepareFileInfos(); | ||
| file_infos->forEachFileInfoForAllHosts(callback); | ||
| } |
There was a problem hiding this comment.
Both writeBackupMetadata and tryRemoveAllFiles run at finalize and nothing else touches file_infos_mutex then.
A vector of pointers costs memory, which is the opposite of this PR.
| void BackupCoordinationLocal::forEachFileInfoForAllHosts(const std::function<void(const BackupFileInfo &)> & callback) const | ||
| { | ||
| std::lock_guard lock{file_infos_mutex}; | ||
| file_infos.forEachFileInfoForAllHosts(callback); | ||
| } |
There was a problem hiding this comment.
Both writeBackupMetadata and tryRemoveAllFiles run at finalize and nothing else touches file_infos_mutex then.
A vector of pointers costs memory, which is the opposite of this PR.
| /// bitset is cheaper than a hash set. Skip entries without a data file (empty file or | ||
| /// base-backup reference); an empty name could resolve to the backup root. | ||
| std::vector<bool> data_file_index_seen; | ||
| coordination->forEachFileInfoForAllHosts([&](const BackupFileInfo & file_info) |
There was a problem hiding this comment.
tryRemoveAllFiles still assumes .backup exists for every non-archive backup, but that file is only created in finalizeWriting. If the backup fails earlier, files_to_remove starts with a missing .backup; for BACKUP ... TO Memory(...), BackupWriterDefault::removeFiles then calls BackupInMemory::removeFile(".backup"), which throws BACKUP_ENTRY_NOT_FOUND and aborts cleanup before any of the real data files or the lock file are removed. Please make this cleanup path tolerate an absent .backup file and add a focused Memory() regression for a pre-finalization failure.
There was a problem hiding this comment.
This issue pre-existed and is out of scope of this PR
There was a problem hiding this comment.
I rechecked the current head after the master merge, and the issue is still present. src/Backups/BackupImpl.cpp:1276 still unconditionally prepends .backup before calling writer->removeFiles, and BackupWriterDefault::removeFiles still deletes sequentially. For BACKUP ... TO Memory(...), a failure before finalizeWriting still reaches BackupInMemory::removeFile(".backup"), throws BACKUP_ENTRY_NOT_FOUND, and aborts cleanup before any real data file or the lock file is removed.
BackupImpl::writeBackupMetadata copied every BackupFileInfo of all hosts (getFileInfosForAllHosts) to write the .backup contents. With millions of files this transiently cost several GiB at finalize. Add IBackupCoordination::forEachFileInfoForAllHosts to iterate in place instead, and use it in writeBackupMetadata and tryRemoveAllFiles. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…ault Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
7bb2319 to
b5bb330
Compare
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
There was a problem hiding this comment.
getFileInfosForAllHosts is kept for other callers.
Why? I don't see any other callers using that method. I think we could remove it entirely.
Apart from that and a minor nitpick, looks good to me. I haven't approved it yet waiting to see if we can remove getFileInfosForAllHosts.
Co-authored-by: Pablo Marcos <[email protected]>
|
Your PR hit the |
…-copy-file-infos-on-finalize
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 40/42 (95.24%) · Uncovered code |
…-copy-file-infos-on-finalize
A BACKUP ... TO Memory(...) that fails before finalization never writes the `.backup` metadata file. tryRemoveAllFiles still tried to remove it, and BackupInMemory::removeFile threw BACKUP_ENTRY_NOT_FOUND, aborting cleanup and leaving the backup registered so the name could not be reused. Make the in-memory writer's removal best-effort, matching the S3, Disk and File writers, and log a missing entry instead of ignoring it silently. Adds a backup_fail_before_writing_metadata failpoint and a regression test. Related: ClickHouse#109861 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Backport #109861 to 26.6: Don't copy all file infos when writing backup metadata
…iting backup metadata
…-no-copy-file-infos-on-finalize Don't copy all file infos when writing backup metadata
A BACKUP ... TO Memory(...) that fails before finalization never writes the `.backup` metadata file. tryRemoveAllFiles still tried to remove it, and BackupInMemory::removeFile threw BACKUP_ENTRY_NOT_FOUND, aborting cleanup and leaving the backup registered so the name could not be reused. Make the in-memory writer's removal best-effort, matching the S3, Disk and File writers, and log a missing entry instead of ignoring it silently. Adds a backup_fail_before_writing_metadata failpoint and a regression test. Related: ClickHouse#109861 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Related: https://github.com/ClickHouse/clickhouse-private/issues/63523
When finalizing a
BACKUP,BackupImpl::writeBackupMetadataobtained the file infos of all hosts viaIBackupCoordination::getFileInfosForAllHosts, which materializes a copy of everyBackupFileInfointo a vector. A backup can contain millions of files (one per part file across all tables), so this transiently allocated several gigabytes right at the end of the operation — on top of the infos already held by the coordination. This was observed as a significant contributor to backup memory usage on a production instance whose backups list ~9 million files.This adds
IBackupCoordination::forEachFileInfoForAllHosts, which iterates the file infos in place (under the coordination lock) instead of copying them, and uses it in:writeBackupMetadata— one pass to count entries and detect base-backup use, then a pass to stream the<contents>of the.backupmetadata;tryRemoveAllFiles— the corrupted-backup cleanup path (where memory is already tight).Notes:
.backupmetadata format is unchanged; this only changes how the in-memory file infos are traversed while writing it.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Reduced peak memory usage when finalizing a
BACKUP. Writing the backup metadata no longer copies the file infos of all files into a temporary vector (which, for backups of millions of files, transiently cost several gigabytes); the infos are now iterated in place.Version info
26.7.1.1006(included in26.7and later)26.6.2.153