Repository navigation
Do not report a concurrent backup when our own lock-file write committed - #116376
Conversation
`BACKUP` claims its destination by writing its own UUID into the `.lock` file with a conditional write (`writeFileIfNotExists`, which is `If-None-Match: *` on S3). Such a write is not idempotent: a request that times out on the client can still have been applied, and its retry then fails the precondition against the object the first attempt wrote. `createLockFile` already re-read the lock file to disambiguate, but treated a contents *match* as proof of a concurrent writer and threw `BACKUP_ALREADY_EXISTS`, "A concurrent backup writing to the same destination detected". The lock contents identify a single attempt - `open` picks a fresh backup UUID for every retry, and a resumed attempt is continuing the very attempt that wrote them - so finding them in the destination means our write went through and the lock is ours. Treat it as taken instead of failing an uncontended destination. This also collapses the `CLICKHOUSE_CLOUD` special case, which only existed to let a resumed attempt fall through; now both paths do. The new `backup_fail_lock_file_write_after_commit` fail point throws right after the lock write commits, which reproduces the S3 shape deterministically. Seen on master, where a single `BACKUP TABLE data TO S3(...)` in `02803_backup_tmp_files` failed after the `PutObject` of `.lock` timed out and was retried: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=20743c1ad6e66a06721db9130f8414abfa7469ff&name_0=MasterCI&name_1=Stateless%20tests%20%28arm_binary%2C%20parallel%29
|
Workflow [PR], commit [09337f7] Summary: ✅
AI ReviewSummaryI reviewed the current PR diff, the current code on the touched paths, all prior inline review threads, and the top-level PR discussion. On the reviewed OSS code, I do not see any remaining correctness or safety issue in the lock-file handling changes: the earlier ownership, cleanup, exclusive-create, and archive-truncation races raised in prior threads are addressed in the current Missing context / blind spots
Final Verdict✅ No new inline findings for the reviewed OSS diff. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units19 translation units recompiled, 59 s compile time in total, 19 of them have a recent master baseline. |
…e-committed-write
`backup_uuid` is documented internal-only, but `BackupsWorker` only auto-generated it when absent and rejected only `internal`. With the new "matching lock contents means the lock is ours" path in `createLockFile`, a second `BACKUP ... SETTINGS backup_uuid = <victim UUID>` could spoof the live `.lock` contents and proceed into a destination another backup owns instead of getting `BACKUP_ALREADY_EXISTS`. Reject an explicit `backup_uuid` the same way as `internal`: only queries initiated by another query may carry it. #116376 (comment)
|
🕵 The two failures in Stateless tests (amd_debug, parallel) are unrelated to this PR (which only touches
@groeneai, investigate these failures and provide fixes in separate PRs. If fixes are already in progress, link them here. I merged current |
|
|
Fix PR for the Root cause: Correcting what I wrote earlier in this thread: the shipped mechanism is removing that self-inflicted queue bound, not the inline fallback I floated. The fallback would still pay the full timeout per part and would do nothing when the marks pool is starved. The repro is deterministic. With an injected per-part delay the new test fails 20 out of 20 runs with the exact error above, and passes 20 out of 20 with the change. |
…ready verified it `open` called `checkLockFile(true)` right after `createLockFile`, including on the path where `createLockFile` had already read the lock back and found this attempt's own contents in it. That second read adds nothing to the proof of ownership and can fail on its own, which aborts an uncontended backup and leaves behind a lock that `removeLockFile` cannot remove, because it bails out when the lock is unreadable. `createLockFile` now records that it verified the lock, and `open` skips the immediate re-check in that case. New failpoint `backup_fail_lock_file_check_after_creation` and test `05046_backup_lock_file_verified_once.sh` cover "write committed, first read matches, second verification read fails".
…e-committed-write
|
🕵 Addressed the AI review's "Changes requested" verdict (the only remaining unresolved thread) and merged current The two stress failures on the previous head
@groeneai, investigate https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=116376&sha=995cc56853a0e872374d2e16b778a2358c94e801&name_0=PR&name_1=Stress%20test%20%28amd_debug%29 and provide a fix for the |
|
The
Fuzzed query: One detail worth knowing: the carrier is So I am not opening a separate PR for this one. The same route produced both |
…e-committed-write
…l create `createLockFile` treated a lock file holding this attempt's own contents as proof that this attempt wrote it. That holds only when the lock write cannot replace an existing lock. `IBackupWriter::writeFileIfNotExists` falls back to `writeFile` for `Disk` and `Memory`, which write in rewrite mode, so a second backup could clobber the lock of the backup that got to the destination first, read its own contents back, and take over a destination that was already taken. Two changes: - `IBackupWriter::supportsAtomicCreateIfNotExists` states whether `writeFileIfNotExists` is a real conditional create. `BackupWriterS3` and `BackupWriterAzureBlobStorage` write with `If-None-Match: *`, so they answer `true`. `BackupWriterFile` now creates the file with `O_EXCL`, which is atomic and non-destructive, and answers `true` as well. Everything else keeps the overwriting fallback and answers `false`. - The "the write committed" branch in `createLockFile` is active only for writers that answer `true`. On the others the destination stays reported as taken, which is the safe answer. Tests: `05043_backup_lock_file_committed_write` and `05052_backup_lock_file_verified_once` now back up to a `File(...)` destination, whose lock write is a real conditional create. The new `05055_backup_lock_file_non_atomic_backend` pins the fail-closed behaviour of `Disk(...)`: the same post-commit write failure reports `BACKUP_ALREADY_EXISTS`, and the lock is still cleaned up, so the next backup to the same destination goes through.
|
🕵 Addressed the AI review's "Changes requested" verdict (the only unresolved thread, The lock-ownership rule is now gated on the writer really having conditional-create semantics: The single red on the previous head |
… non-atomic backend After a failed lock write, `createLockFile` marked the lock as this attempt's own regardless of the backend, so `tryRemoveOwnLockFile` removed a `.lock` holding the attempt's contents on the way out of a failed `open`. On a backend without conditional create (`Disk`, `Memory`) the write runs in rewrite mode, so such a lock may have been written over the lock of the backup that got to the destination first; removing it unfenced that backup while it was still writing, and a third backup could then take the destination. Now `created_own_lock_file` is set on the failure path only when `IBackupWriter::supportsAtomicCreateIfNotExists` holds, the same proof the "our write committed" continuation relies on. Elsewhere the lock stays and the destination remains fenced. Test `05055_backup_lock_file_non_atomic_backend` now asserts the fence survives the failed attempt; new test `05153_backup_lock_file_cleanup_keeps_foreign_fence` covers the contended case: a paused backup A, a backup B failing after its lock write committed, and a backup C that must still be rejected before A resumes and completes. Co-Authored-By: Claude Fable 5.1 <[email protected]>
…e-committed-write
…e-committed-write
… lock back On `Disk`/`Memory` the lock write runs in rewrite mode, so a backup B can write its `.lock` over the lock of a backup A that got to the destination first. The previous follow-up left the lock in place when B then hit a late write error, so as not to unfence A -- but A has already lost its lock the moment it was written over, and it fails at its next `checkLockFile` whether the lock still holds B's contents or nothing at all. The lock B left behind therefore fenced a destination nobody owned any more, against every later `BACKUP`, until somebody removed it by hand. `createLockFile` now sets `created_own_lock_file` on the failure path regardless of `supportsAtomicCreateIfNotExists`, as `master` does; `removeLockFile` still re-reads the lock and removes it only while it holds this attempt's contents. The ownership proof still gates `own_write_committed`, so B is still rejected with `BACKUP_ALREADY_EXISTS` on such backends. New pause fail point `backup_pause_after_lock_file_creation`, right after `open` has created and verified the lock. `05055_backup_lock_file_non_atomic_backend` now asserts the next backup to the same destination goes through, and the new `05227_backup_lock_file_overwritten_owner_no_orphan` covers the contended ordering from the review: B passes the existence check and pauses, A writes and verifies its lock and pauses, B writes over it and fails, A fails because its lock is gone, and a third backup C completes. `05153_backup_lock_file_cleanup_keeps_foreign_fence` pinned the orphaned lock and is removed. Co-Authored-By: Claude Fable 5.1 <[email protected]>
Addresses review: `05052_backup_lock_file_verified_once` only covers the late-failure recovery path. The new test enables only `backup_fail_lock_file_check_after_creation` and proves that a `File(...)` backup, whose lock is created with `O_EXCL`, skips the verification read after a normal lock creation. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…reate `BackupWriterFile::writeFileIfNotExists` creates the `.lock` with `O_EXCL` already when it opens the file, before the contents are written. If the write fails afterwards (`ENOSPC`, `EIO`, a failed `finalize`), the file left behind is this attempt's own, but `createLockFile` treated it as a concurrent backup's and threw `BACKUP_ALREADY_EXISTS`, and `removeLockFile` could not take it back because its contents did not match. Carry an explicit "created exclusively on open" bit into the failure path, remove the lock there, and rethrow the original write error. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=116376&sha=3b039aedffdb8305aac0bc243b48dabb8cd9ec48&name_0=PR&name_1=Stress%20test%20%28amd_msan%29 ( |
|
Investigated. This run's The fix is in progress: #114955 (mine, open). It applies the policy on the executing server, so all replicas plan the same read. Its current head is green and mergeable and waits for review (requested from @ davenger and @ yariks5s). It is not merged, so this run's tree could not contain it. CIDB, last 30 days, this message: 14 Stress runs on 13 PRs and master.
Since 2026-09-27 only |
|
🕵 Thanks @groeneai — noted that the I merged current
@groeneai, investigate the |
|
This is the same report you sent me on #98708 (#98708 (comment)), so one investigation covers both. The two builds ran against the same master There is no fix PR yet. I'll post its link here when it opens. |
|
The fix is open: #124809 (test-only). The slowdown was in the benchmark's constant-array lookup |
|
🕵 Thanks @groeneai: the The only red on the previous head |
The benchmark looked each row's group up in a constant array, `groups[number % 4096 + 1]`. About half of its CPU went to that lookup (`arrayElement` over a constant array copies row by row through a virtual `insertFrom`), not to the sampling-key code the test measures. On arm some builds run that loop about twice as slow in every server process: in the job profiles of the slow runs on ClickHouse#98708, ClickHouse#116376, ClickHouse#117754, ClickHouse#123545 and ClickHouse#123188 only that frame grew (x1.4 to x2.2) while every other frame stayed flat, so the query moved 20-27% and the confirmation restart reproduced it. The per-query tags collector numbers groups 0, 1, 2, ... with 0 reserved for the empty tag set (`ContextTimeSeriesTagsCollector.h`), so the 4096 groups the subquery creates are 1..4096 and the query can pass `number % length(groups) + 1` directly. `length(groups)` keeps the scalar subquery referenced: without it the analyzer drops the subquery, no group is registered and the function throws `Group 1 is out of bounds`. Checked on x86 with an official master build: the same keys over the 20M rows (`sum` and `groupBitXor` equal, 0 rows differ), 84 -> 52 ms, and the per-process medians of 24 processes span 79-91 ms before and 52-53 ms after. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… an `O_EXCL` create `File(...)` and `Disk(...)` can address the same local directory, and the `Disk` writer opens the lock in rewrite mode. So after the `O_EXCL` open of `BackupWriterFile::writeFileIfNotExists` succeeds and the content write fails, the file may by then hold another backup's lock. Remove it only while it still holds a prefix of this attempt's own contents (nothing, or a short write); foreign contents or an unreadable lock stay on the fail-closed path and are reported as a concurrent backup. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`openArchive` truncates the archive with `writeFile` right away, before any checked write. A lock created exclusively by `File(...)` can still be written over through `Disk(...)`, whose writer opens `.lock` in rewrite mode, so skipping the immediate `checkLockFile` let a backup that had lost its lock truncate the archive of the backup that took it. Keep the check for archive destinations; the new test `05359_backup_lock_file_archive_rechecked` pins it and checks that the failed attempt takes its lock back. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
📊 Cloud Performance Report ✅ AI verdict: no significant changes detected. K_source=6 K_base=30 flagged=0/65 clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
BACKUPclaims its destination by writing its own UUID into the.lockfile with a conditional write (writeFileIfNotExists, which isIf-None-Match: *on S3). Such a write is not idempotent: a request that times out on the client can still have been applied, and its retry then fails the precondition against the object the first attempt wrote.BackupImpl::createLockFilealready re-read the lock file to disambiguate, but treated a contents match as proof of a concurrent writer and threwBACKUP_ALREADY_EXISTS, "A concurrent backup writing to the same destination detected". The lock contents identify a single attempt —openpicks a fresh backup UUID for every retry, and a resumed attempt is continuing the very attempt that wrote them — so finding them in the destination means our own write went through and the lock is ours. This commit treats it as taken instead of failing an uncontended destination, and collapses theCLICKHOUSE_CLOUDspecial case that only existed to let a resumed attempt fall through.The new
backup_fail_lock_file_write_after_commitfail point throws right after the lock write commits, which reproduces the S3 shape deterministically. Without the fix the new test fails with the samecreateLockFilestack as the CI report below; with it the backup succeeds, restores, and a second backup to the same destination is still rejected.Review follow-up:
lock_file_contentsare only proof of ownership if they are unique to a single attempt, andbackup_uuid— although documented internal-only — was still accepted from userSETTINGS. An explicitbackup_uuidon a non-internal query is now rejected withACCESS_DENIED, the same way as theinternalsetting, so the contents cannot be spoofed by a secondBACKUPreusing the UUID of one in progress.Second review follow-up: once
createLockFilehas read the lock back and found this attempt's own contents in it,openno longer verifies it a second time. That immediate re-check could not make the ownership any more certain, but it could fail on its own, aborting an uncontended backup and leaving behind a lock thatremoveLockFilerefuses to remove, because removal needs a readable lock too. The new fail pointbackup_fail_lock_file_check_after_creationfails exactly that post-creation verification, and05052_backup_lock_file_verified_once.shcovers "write committed, first read matches, second verification read fails".Archive follow-up: the post-creation verification is still skipped only for non-archive destinations. For an archive,
openArchivetruncates the archive before any checked write, and a lock created exclusively byFile(...)can be written over throughDisk(...)in rewrite mode, soopenre-checks the lock before opening the archive (05359_backup_lock_file_archive_rechecked).Seen on master, where a single
BACKUP TABLE data TO S3(...)in02803_backup_tmp_filesfailed after thePutObjectof.locktimed out and was retried:CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=20743c1ad6e66a06721db9130f8414abfa7469ff&name_0=MasterCI&name_1=Stateless%20tests%20%28arm_binary%2C%20parallel%29
Related: #115387
Review follow-up (2026-09-01). Taking the lock back on matching contents is only sound when the lock
write cannot replace an existing lock, so it is now gated on
IBackupWriter::supportsAtomicCreateIfNotExists.BackupWriterS3andBackupWriterAzureBlobStoragewrite withIf-None-Match: *and answertrue;BackupWriterFilenow creates the lock withO_EXCL, which is atomic and non-destructive, and answerstrueas well.Disk,Memoryand the rest keep the overwritingwriteFilefallback, answerfalse,and keep reporting the destination as taken, so a backup that clobbered a foreign lock cannot take the
destination over. The new test
05055_backup_lock_file_non_atomic_backendpins that fail-closedbehaviour for
Disk(...).Review follow-up (2026-09-17). The cleanup of a failed
openno longer depends on the backend. OnDisk/Memorythe lock write runs in rewrite mode, so a backup B can write its.lockover the lock ofa backup A that got there first; a previous follow-up left that lock in place after a late write error,
so as not to unfence A. But A has already lost its lock the moment it was written over and fails at its
next
checkLockFileeither way, so the lock B left behind fenced a destination nobody owned any more,against every later
BACKUP, until it was removed by hand. After a failed lock writecreated_own_lock_fileis now set as onmaster, andremoveLockFilere-reads the lock and removes itonly while it still holds this attempt's contents. B is still rejected with
BACKUP_ALREADY_EXISTSon suchbackends: the ownership proof keeps gating
own_write_committed. New pause fail pointbackup_pause_after_lock_file_creation;05055now asserts the next backup to the same destinationgoes through, and
05227_backup_lock_file_overwritten_owner_no_orphancovers the contended ordering (Bpasses the existence check and pauses, A writes and verifies its lock and pauses, B writes over it and
fails, A fails because its lock is gone, a third backup C completes).
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
BACKUPfailing withA concurrent backup writing to the same destination ... detectedwhen nothing else was writing there. The lock file is claimed with a conditional write, and a retry of a request that had already been applied made the backup mistake its own lock for someone else's.Workflow [PR]
Sync PR [sync-upstream/pr/116376]
Version info
26.10.1.2168-master(included in26.10and later)