Skip to content

Sync BackupImpl lock-file handling and the Cloud-only resume state - #115387

Merged
nikitamikhaylov merged 8 commits into
masterfrom
jkartseva/sync-resumable-backup-create-params
Aug 21, 2026
Merged

nikitamikhaylov merged 8 commits into
masterfrom
jkartseva/sync-resumable-backup-create-params

Conversation

@jkartseva

@jkartseva jkartseva commented Aug 19, 2026 •

Copy link
Copy Markdown
Member

Related: https://github.com/ClickHouse/clickhouse-private/pull/69709

This is the public side of the private pull request above, and most of it is that sync. The shared files both touch -- BackupImpl.h, BackupImpl.cpp, BackupSettings.h, BackupSettings.cpp, BackupFactory.h -- are identical here and there, down to the blank lines, so Check public build can overlay the private ones unchanged and later syncs have nothing to reconcile in them. What stays private is where continuing an interrupted backup is implemented, because nothing outside ClickHouse Cloud resumes one.

The part that changes this build is the lock-file handling.

open runs from the constructor, so a failure there leaves no backup behind for anything else to clean up: whatever open does not undo, nothing will. It now removes the lock it created when anything that follows throws -- the verification, the rest of claiming the destination, and opening the archive. Otherwise the lock outlives the failed attempt and fences the destination against every later one, which cannot match it either: a retry picks a fresh backup UUID, so the orphaned lock belongs to nobody, and every subsequent BACKUP to that destination fails until someone deletes the file by hand.

removeLockFile now reports whether the destination still holds the lock. checkLockFile(false) is false both for a foreign lock and for one it could not read, so a caller could not tell "not ours" from "unknown" while the lock was still there; tryRemoveAllFiles reports that as a failure rather than claiming the destination is clean.

createLockFile asks for the lock with writeFileIfNotExists and, when that fails, compares what is there against what it would have written: its own contents mean a concurrent backup, a completed .backup means the backup already exists, and a lock that has since disappeared rethrows the original failure rather than guessing. writeFileIfNotExists is only exclusive where the storage can be -- S3 and Azure implement it, every other writer falls back to an ordinary write.

Known gaps

Two of them, both predating this change and both worth naming rather than implying they are fixed.

File, Disk and Memory destinations still have no exclusive create, so two backups started against one of them can each write .lock, each verify their own contents, and both proceed. The comparison above detects a lock that is already there; it cannot order two writers racing to create one. Closing this needs an exclusive create in those writers.

An archive whose type createArchiveWriter refuses -- an unsupported extension, a compiled-out library -- still leaves the archive object behind, because the buffer it is given is created before the type is checked. The lock is removed, but the next attempt finds the archive and reports the destination as an existing backup.

The rest

Behind CLICKHOUSE_CLOUD, and compiling to nothing here: CreateParams carries the resume parameters, open hands them to a BackupResumer this build does not have, and the places that read them are the ownership check before anything becomes visible, the timestamp a continuation republishes, the lock a continuation is allowed to find, and the manifest it does not write again. BackupSettings carries the two batch-size settings that bound the work a continuation redoes; a macro list cannot hold a preprocessor conditional, so the conditional selects the list. BackupImpl keeps one member, metadata_already_published. So this build behaves as it did: it writes its own timestamp and always publishes metadata.

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

BACKUP no longer leaves a lock file behind in its destination when the attempt that created it fails before writing anything -- whether it fails while claiming the destination or while opening an archive. No later backup could match such a lock, so every retry to the same destination failed until the file was deleted by hand. Backups to S3 and Azure destinations also create the lock exclusively, and a lock that belongs to another backup is reported as a concurrent backup rather than as whatever error the storage returned.


Workflow [PR]
Sync PR [sync-upstream/pr/115387]

Version info

  • Merged into: 26.8.1.1800 (included in 26.8 and later)

… needs

`createLockFile` creates the lock with `writeFileIfNotExists`, so two backups
racing for the same destination cannot both believe they own it. When the create
fails it compares what is there against what it would have written: its own
contents mean a concurrent backup, a completed `.backup` means the backup
already exists, and a lock that has since disappeared rethrows the original
failure rather than guessing.

`removeLockFile` now reports whether the destination still holds the lock.
`checkLockFile(false)` is false both for a foreign lock and for one it could not
read, so a caller could not tell "not ours" from "unknown" while the lock was
still there; `tryRemoveAllFiles` reports that as a failure rather than claiming
the destination is clean.

`open` also removes a lock it created when the verification that follows throws.
Otherwise the lock outlives the failed attempt and fences the destination
against every later one, which cannot match it either: a retry picks a fresh
backup UUID, so the orphaned lock belongs to nobody.

ClickHouse Cloud can continue an interrupted backup, and `BackupImpl` is where
that is implemented, so the state it keeps for one belongs here: whether this
attempt is a continuation, the timestamp text the earlier attempt wrote into
`.backup`, the ownership check to run before publishing anything, and whether
`.backup` is already published. All of it, the two helpers that reconcile an
already-published manifest, and every place the shared implementation reads or
reports it, are behind `CLICKHOUSE_CLOUD`, so this build behaves as it did: it
writes its own timestamp and always publishes metadata. The parameters that fill
the state in stay out, along with the code that reads them.

Co-Authored-By: Claude Opus 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [5473e8b]

Summary: ✅


AI Review

Summary

This PR syncs the public BackupImpl changes with the Cloud-only resumable-backup scaffolding and, on the public side, tries to stop failed BACKUP attempts from leaving stale destination locks behind. The current head fixes the earlier gaps around openArchive() and late lock creation failures, but two correctness issues are still live in the touched code: archive-backed destinations can still be left dirty when archive creation fails, and File / Disk writers still do not create .lock atomically. I would keep this PR in changes-requested state until those paths are fixed or explicitly scoped away in code.

Missing context / blind spots
  • ⚠️ BackupResumer and the Cloud-only tests that exercise params.resume are not present in this checkout, so I could only review the shared/public scaffolding around resumable backups, not the private implementation itself.
Findings
  • ❌ [src/Backups/BackupImpl.cpp:321] openArchive() still materializes archive_name before createArchiveWriter() validates the extension or compiled-in archive backend. On File and Disk destinations, writer->writeFile(archive_name) creates or truncates the file immediately, so BACKUP ... TO File('backup.badext') now removes the .lock but still leaves an empty archive behind. The next retry then fails with BACKUP_ALREADY_EXISTS, so the destination is still not restored to its pre-attempt state on this error path. Validate the archive type before opening archive_name, or delete the partially created archive object in the failure path alongside the lock.
  • ⚠️ [src/Backups/BackupImpl.cpp:907], [src/Backups/BackupIO_Default.cpp:20], [src/Backups/BackupIO_File.cpp:100], [src/Backups/BackupIO_Disk.cpp:85] createLockFile() now routes every backend through writeFileIfNotExists(), but File and Disk still inherit the default implementation that falls back to plain rewrite-mode writeFile(). Two concurrent BACKUP ... TO File(...) or Disk(...) commands can therefore still overwrite each other's .lock and both continue, so the lock file still does not serialize writers on those backends. Either add real exclusive-create support there or gate the stronger lock path to backends that can actually enforce it.
Final Verdict

Changes requested. The live head fixes two earlier cleanup holes, but it still leaves archive destinations dirty on openArchive() failures and still relies on non-atomic lock creation for File / Disk backups.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 87.10% 87.00% -0.10%
Functions 91.90% 91.90% +0.00%
Branches 79.40% 79.30% -0.10%

Changed lines: Changed C/C++ lines covered: 95/123 (77.24%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-build Pull request with build/testing/packaging improvement label Aug 19, 2026
@jkartseva jkartseva changed the title Harden backup lock-file handling, and keep the state a resumed backup needs Sync backup lock-file handling, and the state a resumed backup needs Aug 19, 2026
@jkartseva jkartseva changed the title Sync backup lock-file handling, and the state a resumed backup needs Sync BackupImpl lock-file handling and the Cloud-only resume state Aug 19, 2026
bool lock_created = false;
try
{
auto out = writer->writeFileIfNotExists(lock_file_name);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createLockFile() now assumes writeFileIfNotExists() is an atomic create, but that is only true for the dedicated object-storage writers. IBackupWriter::writeFileIfNotExists() still falls back to plain writeFile(), and BackupWriterFile / BackupWriterDisk only implement rewrite-mode opens, so concurrent BACKUP ... TO File(...) / Disk(...) calls can still overwrite each other's .lock and both continue.

That breaks the new contract and changelog claim that lock creation is now atomic across backup destinations. Please either add real exclusive-create implementations for those writers or scope this path so the stronger behavior is only used on backends that can actually enforce it.

Comment thread src/Backups/BackupImpl.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 5473e8b7f with master 756b18a39 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 0b043a6c6; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 704.45 MiB 701.40 MiB -3.05 MiB (-0.43%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Compile time of recompiled translation units

12 translation units recompiled, 30 s compile time in total, 12 of them have a recent master baseline.

Job report

jkartseva and others added 3 commits August 19, 2026 20:19
The resumer is ClickHouse Cloud only, and so is this call site; what changes
here is that it compares against a named state instead of taking a bare bool
whose meaning was only in the field it was assigned to.

Co-Authored-By: Claude Opus 5 <[email protected]>
Only the ClickHouse Cloud resumer called it, and it needs nothing of
`BackupImpl` beyond the writer, the UUID and the name for logging, so it belongs
in the resumer -- which this build does not have. Its two includes were the only
users of `IO/StdStreamFromReadBuffer.h` and `Poco/SAX/InputSource.h` here, and go
with it.

Co-Authored-By: Claude Opus 5 <[email protected]>
…kupImpl`

`resuming`, `timestamp_text` and `resume_check_owner` were copies of fields of
`params.resume`, which the ClickHouse Cloud resumer filled in while it reopened
a destination that build already owns. The guarded code reads those parameters
directly now, so this build carries three members fewer -- it had them declared
but never set.

Co-Authored-By: Claude Opus 5 <[email protected]>
Comment thread src/Backups/BackupImpl.cpp Resolved
Comment thread src/Backups/BackupImpl.cpp Outdated
catch (...)
{
if (!params.is_internal_backup)
tryRemoveOwnLockFile();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

openArchive() can still leave the destination dirty even if the lock-cleanup catch is widened. createArchiveWriter() is called with writer->writeFile(archive_name) already materialized, and BackupWriterFile::writeFile() / BackupWriterDisk::writeFile() open rewrite-mode buffers immediately. A concrete repro is BACKUP ... TO File('backup.badext'): archive_name is created or truncated before createArchiveWriter() throws CANNOT_PACK_ARCHIVE, so the constructor unwinds with an empty archive file already present.

The next retry then fails in checkBackupDoesntExist() with BACKUP_ALREADY_EXISTS even if the .lock were removed. Please validate the archive type before opening archive_name, or remove the partially created archive object on this failure path as well.

`BackupImpl` kept a `metadata_already_published` member and a getter for it, both
describing something only the ClickHouse Cloud resumer can find out: that the
attempt being continued had already published `.backup`. That now lives with the
progress record of the attempt, which this build does not have either, and the
guarded code reads it back through the resume parameters. `BackupImpl` keeps no
resume state at all.

The write in `writeBackupMetadata` went with it: nothing could observe it. Its
only caller had already branched on the flag, and set `writing_finalized` right
after, which is what every later reader checks first.

Co-Authored-By: Claude Opus 5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added pr-improvement Pull request with some product improvements and removed pr-build Pull request with build/testing/packaging improvement labels Aug 20, 2026
jkartseva and others added 2 commits August 20, 2026 01:38
It promised the write-open state this class no longer keeps. What the ClickHouse
Cloud resumer needs is the writer, the lock file and the helpers that claim and
release it.

Co-Authored-By: Claude Opus 5 <[email protected]>
`resumable_backup_batch_size` and `resumable_backup_batch_size_bytes` bound the
work a continued `BACKUP FROM SNAPSHOT` has to redo. Nothing outside ClickHouse
Cloud resumes one, so both are behind `CLICKHOUSE_CLOUD` and this build has
neither -- what it gains is that `BackupSettings` no longer differs from the
private copy here.

A macro list cannot hold a preprocessor conditional, so the conditional selects
the list: `LIST_OF_CLOUD_BACKUP_SETTINGS` is the two settings or nothing.

Co-Authored-By: Claude Opus 5 <[email protected]>
@jkartseva

Copy link
Copy Markdown
Member Author

Taking a look at the bot's comments. I promise I did not add anything on top of what is already merged in private.

`open` runs from the constructor, so a throw leaves no backup for anything else
to clean up: the cleanup in `open` is all there is. It covered the verification
that follows creating the lock, and nothing else, so two paths still left the
lock behind -- `createLockFile` throwing after the write committed, and
`openArchive` failing afterwards. A lock that outlives its attempt fences the
destination for good, because a retry picks a fresh backup UUID and can never
match it.

The guard now spans claiming the destination and opening the archive, which is
everything a write does before the backup exists. Reading and unlocking keep
their own `openArchive` call, where there is no lock to lose.

Co-Authored-By: Claude Opus 5 <[email protected]>
@jkartseva

Copy link
Copy Markdown
Member Author

openArchive() still materializes archive_name before createArchiveWriter() validates the extension or compiled-in archive backend.

Is beyond the scope of this PR which is supposed to be private-to-public sync.

@nikitamikhaylov nikitamikhaylov self-assigned this Aug 20, 2026
@nikitamikhaylov
nikitamikhaylov added this pull request to the merge queue Aug 21, 2026
Merged via the queue into master with commit 0107491 Aug 21, 2026
176 checks passed
@nikitamikhaylov
nikitamikhaylov deleted the jkartseva/sync-resumable-backup-create-params branch August 21, 2026 00:49
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants