Repository navigation
Fix flaky 03752_attach_as_replicated_transaction_metadata on object storage - #103599
alexey-milovidov merged 2 commits into
Conversation
…torage
Test2 of 03752_attach_as_replicated_transaction_metadata was racy on object
storage. After 6 active inserts, a background merge of the source parts could
run concurrently with the `DELETE FROM t0 WHERE TRUE` mutation that follows.
The merge writes a transient `txn_version.txt.tmp` on the resulting merged
part during `storeInfoToDataPartStorage` (two-phase write: create `.tmp` →
rename to `txn_version.txt`). On object storage the mutation's clone uses an
external metadata transaction, so the queued `createHardLink` for that `.tmp`
file is not committed until later — by which time the rename has completed,
the source `.tmp` is gone, and the mutation fails with:
Code: 107. Can't create hardlink for file
store/.../all_1_7_1/txn_version.txt.tmp:
While committing metadata operation #0. (FILE_DOESNT_EXIST)
Force the merge to complete deterministically before the mutation runs:
`OPTIMIZE TABLE t0 FINAL` is synchronous, so when `COMMIT` returns the merged
part's metadata is fully stable. After it, only one active part remains, so no
further background merges can be scheduled before the mutation. The merge has
to run inside a transaction because the table already had transactions used
on it (`BEGIN TRANSACTION ... ROLLBACK` above) and a non-transactional merge
is rejected with `Cancelling merge ... transactions were enabled for this
table`.
Test1 and test3 only insert two parts each, so they are not exposed to this
race in practice and are left unchanged. The output of all three tests is
unchanged (`SELECT COUNT(*)` returns the same values), so the `.reference`
file does not need updating.
Verified locally with `--test-runs 50` (50/50 passes; runs took 61s).
CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100976&sha=49f741a8f4d15de6fdbb6d32fc9d996694fa1799&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20distributed%20plan%2C%20s3%20storage%2C%20parallel%29
PR: ClickHouse#100976
Pre-PR validation gate
Session: cron:clickhouse-ci-task-worker:20260427-081500 |
|
cc @CurtizJ @alexey-milovidov — could you review this? Test-only fix for |
|
Workflow [PR], commit [a2f8d9a] Summary: ✅ AI ReviewSummaryThis PR stabilizes ClickHouse Rules
Final Verdict
|
alexey-milovidov
left a comment
There was a problem hiding this comment.
This is quite complex, but ok to stabilize the test for low-priority features (transactions and attach as replicated).
Fix flakiness in
03752_attach_as_replicated_transaction_metadata(test2) on object-storage CI configurations. Requested by @alexey-milovidov on #100976 (comment).Failure mode
CI failure (
Stateless tests (amd_debug, distributed plan, s3 storage, parallel)on PR #100976, 2026-04-27):The failing query is
DELETE FROM t0 WHERE TRUE;in test2, which spawns a mutation. On object storage the mutation's part-clone goes throughDataPartStorageOnDiskBase::freeze→Backup→ queuedcreateHardLinkoperations on an external metadata transaction (committed later, inMetadataOperationsHolder::commit).Test2 inserts 6 active blocks (plus a rolled-back transactional one). A background merge of those source parts runs concurrently with the subsequent
DELETEmutation. The merge writes a transienttxn_version.txt.tmpon the merged part duringVersionMetadataOnDisk::storeInfoToDataPartStorage(two-phase write:createFile(*.tmp)→writeFile(*.tmp)→replaceFile(*.tmp -> txn_version.txt)).If the mutation iterates the merged part's directory while
txn_version.txt.tmpexists, it queues a hardlink operation for that file. By the time the queued op is committed, the rename has completed, the source.tmpis gone, andCreateHardlinkOperation::executefails:(
src/Disks/DiskObjectStorage/MetadataStorages/Local/MetadataStorageFromDiskTransactionOperations.cpp:368)CIDB confirms 4 hits in 30 days across 4 unrelated PRs, three of which are on object-storage configurations (
s3 storage,azure).Fix
Force the merge to complete deterministically before the
DELETEmutation runs:OPTIMIZE FINALis synchronous, so whenCOMMITreturns the merged part'stxn_version.txtis fully stable (no transient.tmp). After it, only one active part remains, so no further background merges can be scheduled before the mutation. The merge has to run inside a transaction because the table already has transactions used on it (BEGIN TRANSACTION ... ROLLBACKabove) and a non-transactional merge is rejected withCancelling merge ... transactions were enabled for this table(MergeTreeDataMergerMutator::renameMergedTemporaryPart).Only test2 is changed; test1 and test3 only insert two parts each, so they are not exposed to this race in practice.
The output of all three tests is unchanged (
SELECT COUNT(*)returns0,0,418as before), so.referencedoes not need updating.Verification
tests/clickhouse-test --test-runs 50 03752_attach_as_replicated_transaction_metadata→ 50/50 passes (61s) on debug local-disk build.Note on the underlying race
The deeper bug — that
Backupqueues hardlinks for transient*.tmpfiles which can disappear before the queued op is committed on object storage — exists insrc/Storages/MergeTree/Backup.cpp(no filter for transient version-metadata files) andsrc/Storages/MergeTree/DataPartStorageOnDiskBase.cpp(post-Backup cleanup removestxn_version.txtfrom the destination but nottxn_version.txt.tmp). Other tests/code paths could theoretically hit the same race. That's worth a separate, source-side fix; this PR only stabilizes the test as requested.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
NO ENTRY
Documentation entry for user-facing changes
CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100976&sha=49f741a8f4d15de6fdbb6d32fc9d996694fa1799&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20distributed%20plan%2C%20s3%20storage%2C%20parallel%29
Version info
26.5.1.149