Repository navigation
Fix ATTACH AS REPLICATED discarding committed data via stale txn_version.txt.tmp - #108772
Conversation
…ion.txt.tmp `InterpreterCreateQuery::clearTransactionMetadata`, run by `ATTACH TABLE ... AS REPLICATED`, removed only `txn_version.txt` from each part, leaving any `txn_version.txt.tmp` behind. A `txn_version.txt.tmp` can legitimately linger on a part: when a mutation clones its source part by hardlinking, a transient `.tmp` written by an in-flight merge (during `storeInfoToDataPartStorage`) can be hardlinked onto the mutated part, and on object storage the queued hardlink is committed after the rename. With `txn_version.txt` removed but the `.tmp` still present, `VersionMetadataOnDisk::loadMetadata` treats the part as a rolled-back transaction (`creation_tid == Tx::DummyTID`, `creation_csn == Tx::RolledBackCSN`), so it is marked `Outdated` and discarded. The pre-mutation part is then loaded as active instead, resurrecting deleted data. This is exactly what made `03752_attach_as_replicated_transaction_metadata` flaky: after a fault-injected merge commit left a `.tmp` on the mutated part, `SELECT count()` returned the pre-`DELETE` count (1271) instead of 0, and the load logged a `found file txn_version.txt.tmp` warning. Fix: `clearTransactionMetadata` now removes both `txn_version.txt` and `txn_version.txt.tmp`, so every part loads as plain committed data after the conversion. The temporary file name is promoted to a shared constant `VersionMetadata::TMP_TXN_VERSION_METADATA_FILE_NAME`. Add a deterministic regression test that injects a stale `txn_version.txt.tmp` into a part and asserts the row survives `ATTACH AS REPLICATED`. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=7dda23a8ec1552f04de5601a1fe59708a5406ee1&name_0=MasterCI&name_1=Stateless%20tests%20%28amd_debug%2C%20distributed%20plan%2C%20s3%20storage%2C%20parallel%29 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Workflow [PR], commit [eafdde8] Summary: ✅
AI ReviewSummaryThis PR fixes stale Final Verdict✅ Approve. No new inline findings to post. |
…cated-tmp-txn-version
Master gained several 04416_* tests since this branch was created, so the existing regression test is renumbered to the next free number (highest was 04488).
…g paths The previous commit promoted the temporary version file to the shared constant `VersionMetadata::TMP_TXN_VERSION_METADATA_FILE_NAME` but only handled it in `InterpreterCreateQuery::clearTransactionMetadata` (the `ATTACH AS REPLICATED` conversion). Every other path that strips, skips, or removes transaction metadata still handled only `txn_version.txt`, so the same stale-`.tmp` state could resurface there. A part with a stale `txn_version.txt.tmp` but no `txn_version.txt` is read by `VersionMetadataOnDisk::loadMetadata` as a rolled-back transaction (`creation_tid == Tx::DummyTID`, `creation_csn == Tx::RolledBackCSN`); since `DummyTID` is not non-transactional, `wasInvolvedInTransaction` is true and `MergeTreeData::loadDataPart` marks the part `Outdated` and discards it, silently losing committed data. The most direct case is `ALTER TABLE ... ATTACH PART`: it strips the main file (`MergeTreeData::loadPartAndFixMetadataImpl` -> `removeVersionMetadata`) and reloads, but a leftover `.tmp` survives and gets the part discarded on the next full table load. Clear the temporary file together with the main file in every such path: - `IMergeTreeDataPart::removeVersionMetadata` (used by `ATTACH PART` reload), - the `ATTACH PART` pre-load strip in `MergeTreeData::tryLoadPartsToAttach`, - the backup-restore copy filter in `MergeTreeData::restorePartFromBackup`, - `DataPartStorageOnDiskBase::freeze` / `freezeRemote` after cloning, - `DataPartStorageOnDiskBase::clearDirectory` (also avoids the slow recursive removal fallback on a non-empty directory). The hardlink-tracking exclusions in `MergeTreeData::cloneAndLoadDataPart` are left as-is: the destination part writes its own fresh `txn_version.txt` via `setAndStoreCreationTID`, so it loads as committed regardless of a hardlinked stale `.tmp` (which `loadMetadata` removes on load). Add a deterministic regression test (04490) that injects a stale `txn_version.txt.tmp` into a detached part, runs `ATTACH PART`, then forces a full reload and asserts the committed row survives (count 1, not 0). Verified against the pre-fix binary: the row is lost (count 0) without this fix. Addresses the AI review "Request changes" finding on #108772 CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=7dda23a8ec1552f04de5601a1fe59708a5406ee1&name_0=MasterCI&name_1=Stateless%20tests%20%28amd_debug%2C%20distributed%20plan%2C%20s3%20storage%2C%20parallel%29 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…cated-tmp-txn-version
|
Merged the latest The only failing CI checks were |
…irst Address the AI Review blocker: every path that strips transaction metadata from a part removed `txn_version.txt` before `txn_version.txt.tmp`. If the main-file removal succeeded and the subsequent `.tmp` removal then threw (for example a transient object-storage error), the part was left in exactly the data-discarding state this PR fixes: no `txn_version.txt` and a stale `txn_version.txt.tmp`, which `VersionMetadataOnDisk::loadMetadata` reads as a rolled-back transaction (`creation_csn == Tx::RolledBackCSN`), so the part is marked `Outdated` and committed data is discarded. Reorder the removals so the temporary file is deleted before the main file in `InterpreterCreateQuery::clearTransactionMetadata`, `IMergeTreeDataPart::removeVersionMetadata`, `MergeTreeData::tryLoadPartsToAttach`, and both branches of `DataPartStorageOnDiskBase::freeze` and `freezeRemote`. A failure between the two removals now leaves a valid `txn_version.txt` (still a committed part) and the operation stays retryable. `DataPartStorageOnDiskBase::clearDirectory` is left unchanged: it removes the entire part directory (with a recursive-removal fallback on any failure), so no re-loadable tmp-only state can persist there. `restorePartFromBackup` only filters file names while copying and performs no removal, so ordering does not apply. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Addressed the AI Review ❌ Block (commit 67705ca). The blocker was a fail-close ordering issue: every path that strips transaction metadata removed CI triage of the remaining red checks — all unrelated to this change:
|
|
CI triage for the latest report (commit
The reds could not be re-run yet because the workflow run is still AI Review verdict on the current commit is ✅ Approve and there are no unresolved review threads. |
|
The workflow run `28458592657` finished (completed/failure at 20:30Z), so I re-triggered its four failed jobs with All four reds are the pre-existing, master-wide flakes already triaged above and are categorically unrelated to this change (the PR touches only
No re-merge (merge-base is from earlier today and |
…cated-tmp-txn-version
Merging the latest `master` (610 commits) brought in tests that reused numbers `04489` (`04489_max_threads_auto_parsing_compat`) and `04490` (`04490_dict_get_keys_float_signed_zero_consistency`, `04490_table_readonly_partition_commands`), colliding with this PR's `04489_attach_as_replicated_clears_tmp_txn_version` and `04490_attach_part_clears_tmp_txn_version`. Renumber the two regression tests to the next free numbers `04492` and `04493`. The tests use fixed table names and contain no internal references to their own number, so only the file names change. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Re-merged the latest
New head is |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 40/51 (78.43%) · Uncovered code |
alexey-milovidov
left a comment
There was a problem hiding this comment.
Yes, sounds reasonable.
…g paths The previous commit promoted the temporary version file to the shared constant `VersionMetadata::TMP_TXN_VERSION_METADATA_FILE_NAME` but only handled it in `InterpreterCreateQuery::clearTransactionMetadata` (the `ATTACH AS REPLICATED` conversion). Every other path that strips, skips, or removes transaction metadata still handled only `txn_version.txt`, so the same stale-`.tmp` state could resurface there. A part with a stale `txn_version.txt.tmp` but no `txn_version.txt` is read by `VersionMetadataOnDisk::loadMetadata` as a rolled-back transaction (`creation_tid == Tx::DummyTID`, `creation_csn == Tx::RolledBackCSN`); since `DummyTID` is not non-transactional, `wasInvolvedInTransaction` is true and `MergeTreeData::loadDataPart` marks the part `Outdated` and discards it, silently losing committed data. The most direct case is `ALTER TABLE ... ATTACH PART`: it strips the main file (`MergeTreeData::loadPartAndFixMetadataImpl` -> `removeVersionMetadata`) and reloads, but a leftover `.tmp` survives and gets the part discarded on the next full table load. Clear the temporary file together with the main file in every such path: - `IMergeTreeDataPart::removeVersionMetadata` (used by `ATTACH PART` reload), - the `ATTACH PART` pre-load strip in `MergeTreeData::tryLoadPartsToAttach`, - the backup-restore copy filter in `MergeTreeData::restorePartFromBackup`, - `DataPartStorageOnDiskBase::freeze` / `freezeRemote` after cloning, - `DataPartStorageOnDiskBase::clearDirectory` (also avoids the slow recursive removal fallback on a non-empty directory). The hardlink-tracking exclusions in `MergeTreeData::cloneAndLoadDataPart` are left as-is: the destination part writes its own fresh `txn_version.txt` via `setAndStoreCreationTID`, so it loads as committed regardless of a hardlinked stale `.tmp` (which `loadMetadata` removes on load). Add a deterministic regression test (04490) that injects a stale `txn_version.txt.tmp` into a detached part, runs `ATTACH PART`, then forces a full reload and asserts the committed row survives (count 1, not 0). Verified against the pre-fix binary: the row is lost (count 0) without this fix. Addresses the AI review "Request changes" finding on ClickHouse#108772 CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=7dda23a8ec1552f04de5601a1fe59708a5406ee1&name_0=MasterCI&name_1=Stateless%20tests%20%28amd_debug%2C%20distributed%20plan%2C%20s3%20storage%2C%20parallel%29 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…plicated-tmp-txn-version Fix ATTACH AS REPLICATED discarding committed data via stale txn_version.txt.tmp
`MutateSomePartColumnsTask` hardlinks every file of the source part that is not in `collectFilesToSkip`. That set took `txn_version.txt` from `getFileNamesWithoutChecksums`, which lists it only if it exists at that moment, and never contained `txn_version.txt.tmp`. When the source part's version metadata is rewritten concurrently (a commit stamping its creation CSN, e.g. a background merge transaction finalized by the TransactionLog updating thread), `VersionMetadataOnDisk::storeInfoToDataPartStorage` writes `txn_version.txt.tmp` and then renames it over `txn_version.txt`, and the mutation copies the transient file: - on a local disk the mutated part keeps a `.tmp` with the source part's version info and no `txn_version.txt`, so on the next load `VersionMetadataOnDisk::loadMetadata` treats it as rolled back and the part's rows disappear (the source part is already removed); - on object storage the hardlink is deferred to the disk transaction commit, and if the rename happened first the mutation fails with `Can't create hardlink for file .../txn_version.txt.tmp (FILE_DOESNT_EXIST)`. The new part always writes its own version metadata, so skip both files by name. ClickHouse#108772 made the consumers tolerate a stray `.tmp`; this closes the producer. This is the failure of `03752_attach_as_replicated_transaction_metadata` on `Stateless tests (amd_debug, distributed plan, s3 storage, parallel)`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
ATTACH TABLE ... AS REPLICATEDcould discard committed data and resurrect pre-mutation rows when a staletxn_version.txt.tmpwas left on a part.InterpreterCreateQuery::clearTransactionMetadata(run by the conversion) removed onlytxn_version.txt, leaving anytxn_version.txt.tmpbehind. A.tmpcan legitimately linger on a part: when a mutation clones its source part by hardlinking, a transient.tmpwritten by an in-flight merge (instoreInfoToDataPartStorage) can be hardlinked onto the mutated part, and on object storage the queued hardlink is committed after the rename. Withtxn_version.txtremoved but the.tmpstill present,VersionMetadataOnDisk::loadMetadatatreats the part as a rolled-back transaction (creation_tid == Tx::DummyTID,creation_csn == Tx::RolledBackCSN), so it is markedOutdatedand discarded. The pre-mutation part is then loaded as active instead.This is what made
03752_attach_as_replicated_transaction_metadataflaky onStateless tests (amd_debug, distributed plan, s3 storage, parallel): after a fault-injected merge commit left a.tmpon the mutated part,SELECT count()returned the pre-DELETEcount (1271) instead of 0, and the load logged afound file txn_version.txt.tmpwarning.Fix:
clearTransactionMetadatanow removes bothtxn_version.txtandtxn_version.txt.tmp, so every part loads as plain committed data after the conversion. The temporary file name is promoted to a shared constantVersionMetadata::TMP_TXN_VERSION_METADATA_FILE_NAME. A deterministic regression test injects a staletxn_version.txt.tmpinto a part and asserts the row survivesATTACH AS REPLICATED(it fails with count 0 before the fix, passes with count 1 after).The affected
ATTACH AS REPLICATEDcode path is new and not in a stable release, so this is filed as a CI fix.CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=7dda23a8ec1552f04de5601a1fe59708a5406ee1&name_0=MasterCI&name_1=Stateless%20tests%20%28amd_debug%2C%20distributed%20plan%2C%20s3%20storage%2C%20parallel%29
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Documentation entry for user-facing changes
🤖 Generated with Claude Code
Version info
26.7.1.540(included in26.7and later)