Repository navigation
Refactor VersionMetadata - #92141
Refactor VersionMetadata#92141
Conversation
|
Workflow [PR], commit [b06d9b3] Summary: ✅ AI ReviewSummaryThis PR refactors MergeTree transaction version metadata into Missing context
ClickHouse Rules
Final Verdict
|
557eb21 to
780bc5f
Compare
There was a problem hiding this comment.
Pull request overview
This PR refactors the VersionMetadata class to decouple version logic from IMergeTreeDataPart by introducing a base class (VersionMetadata) and a subclass (VersionMetadataOnDisk) that persists metadata on disk. The refactoring moves transaction version metadata handling from src/Interpreters/TransactionVersionMetadata.{h,cpp} to a new directory structure under src/Interpreters/MergeTreeTransaction/, and introduces VersionInfo to encapsulate version-related data storage and retrieval.
Key changes:
- Separated removal TID locking logic from removal TID storage
- Added support for append-mode metadata writing on compatible storage backends
- Introduced
VersionInfostructure for cleaner metadata serialization - Updated test assertions to reflect behavioral changes in removal TID handling
Reviewed changes
Copilot reviewed 35 out of 35 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
tests/integration/test_transactions/test.py |
Refactored test assertions to use a helper function and added detailed comments explaining expected transaction metadata states |
src/Storages/System/StorageSystemTransactions.cpp |
Updated include path for VersionMetadata header |
src/Storages/System/StorageSystemParts.cpp |
Changed from direct member access to getter methods for version metadata fields |
src/Storages/StorageReplicatedMergeTree.cpp |
Updated to use pointer-based access to version metadata |
src/Storages/StorageMergeTree.cpp |
Modified to call storeMetadata instead of storeVersionMetadata |
src/Storages/MergeTree/ReplicatedMergeTreeSink.cpp |
Reorganized includes and updated version metadata access |
src/Storages/MergeTree/MutateTask.cpp |
Updated method calls for storing version metadata |
src/Storages/MergeTree/MergedBlockOutputStream.cpp |
Changed to use new metadata storage API |
src/Storages/MergeTree/MergeTreePartsMover.cpp |
Updated to call loadAndVerifyMetadata instead of loadVersionMetadata |
src/Storages/MergeTree/MergeTreeData.{h,cpp} |
Added friend declarations, removed inline metadata handling logic, delegated to VersionMetadata class methods |
src/Storages/MergeTree/IMergeTreeDataPart.{h,cpp} |
Changed version from value to unique_ptr, removed metadata writing methods, delegated to VersionMetadata |
src/Storages/MergeTree/IDataPartStorage.h |
Added filename parameter to writeTransactionFile method |
src/Storages/MergeTree/DataPartsExchange.cpp |
Updated version metadata access pattern |
src/Storages/MergeTree/DataPartStorageOnDiskBase.{h,cpp} |
Implemented new writeTransactionFile signature, updated file removal references |
src/Storages/MergeTree/Compaction/PartsCollectors/MergeTreePartsCollector.cpp |
Updated to use getter methods for version metadata fields |
src/Interpreters/TransactionsInfoLog.cpp |
Updated include path for VersionMetadata |
src/Interpreters/TransactionVersionMetadata.{h,cpp} |
Deleted original files (moved to new location) |
src/Interpreters/TransactionLog.cpp |
Added support for commit-time requests from transactions |
src/Interpreters/QueryLog.cpp |
Updated include path for VersionMetadata |
src/Interpreters/MergeTreeTransaction/VersionMetadataOnDisk.{h,cpp} |
New subclass implementing disk-based metadata persistence with append support |
src/Interpreters/MergeTreeTransaction/VersionMetadata.{h,cpp} |
New base class containing core version metadata logic |
src/Interpreters/MergeTreeTransaction/VersionInfo.{h,cpp} |
New structure for serializing/deserializing version information |
src/Interpreters/MergeTreeTransaction.{h,cpp} |
Updated to use new metadata API, added request handling on commit |
src/Interpreters/FilesystemCacheLog.h |
Removed unused include |
src/Functions/FunctionsTransactionCounters.cpp |
Updated include path for VersionMetadata |
src/CMakeLists.txt |
Added new object library for MergeTree transaction code |
src/Backups/BackupEntryWrappedWith.h |
Added missing include for SeekableReadBuffer |
b9f24bd to
70cd387
Compare
|
@tiandiwonder Please help to review this PR. |
|
8e3c0fc to
c045716
Compare
|
The changelog entry is missing (still |
Updated |
c045716 to
f16fa02
Compare
The stateless test CI job "ParallelReplicas, s3 storage" runs with `max_parallel_replicas=10` and a 3-node cluster. Under this configuration, `SELECT count() FROM t` is distributed across all 3 replica nodes. Transaction context (including `SET TRANSACTION SNAPSHOT`) is not propagated to remote parallel-replica workers. Consequently, each remote worker reads all parts without the snapshot restriction: - `rows_at_snapshot_1`: initiator sees 2 (snapshot=1), 2 remotes see 3 each → 2+3+3=8 (expected 2) - `rows_in_txn_before_drop`: all 3 workers see 4 rows → 4+4+4=12 (expected 4) Fix: add `no-parallel-replicas` tag to both tests so they are skipped in the ParallelReplicas CI run. Transaction-snapshot isolation cannot be tested correctly when queries are fanned out to replica workers that lack the originating transaction's context. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
7f8a27a to
b06d9b3
Compare
LLVM Coverage Report
Changed lines: 75.02% (772/1029) | lost baseline coverage: 108 line(s) · Uncovered code |
Tests `04057_transaction_version_metadata_lifecycle` and `04060_transaction_snapshot_visibility` (introduced by PR ClickHouse#92141) use INSERT inside BEGIN TRANSACTION. Async inserts inside transactions are NOT_IMPLEMENTED, so these tests fail 100% of the time on the AsyncInsert CI configuration. The existing transaction tests (01172, 01173, etc.) are already in the blacklist — these two were missed. Co-Authored-By: Claude Opus 4.6 <[email protected]>
|
@tuanpach This is backward incompatible change since it changes the format of metadata - adding Is it hard to make it compatible? |
Not only
It can be done, not too hard. We need to detach the old format and convert it to the new one. But this is an experimental feature. Is it necessary? |
|
It's not necessary, we can break compatibility of experimental features. But we need to suppress the failure |
It makes sense. I will update the parsing logic to avoid throwing an exception in that case. |
|
Updated it here: #103031 |
|
@tuanpach I also find this logs too noisy - https://pastila.nl/?0018d7ff/70b402a436870f5f3a2a40c28adaa13e#X3xfZCboT+I/B2LhIdxGmw==GCM Is it ok to make them |
I will do it. |
Regression from ClickHouse#92141: server aborts with signal 6 during `loadDataPart` in debug and sanitizer builds when a part directory contains a leftover `txn_version.txt.tmp` file with no corresponding `txn_version.txt`. This state is produced by `VersionMetadataOnDisk::loadMetadata` case 2 (an incomplete write that was not atomically renamed). It returns a `VersionInfo` with `creation_tid = Tx::DummyTID` and `creation_csn = Tx::RolledBackCSN`. `Tx::DummyTID = {NonTransactionalCSN, DummyLocalTID, Nil} = {1, 2, Nil}` has `start_csn == NonTransactionalCSN` but `local_tid == DummyLocalTID` — which fails the invariant asserted in `TransactionID::isNonTransactional`. The new `VersionMetadata::validateInfo` (added by ClickHouse#92141) and `VersionInfo::wasInvolvedInTransaction` both call `isNonTransactional` on the loaded `creation_tid`, so the assertion aborts the server. Two changes: - `TransactionID::isNonTransactional`: widen the assertion to allow the `DummyTID` special case. The function still correctly returns `false` for `DummyTID` because `local_tid == DummyLocalTID != NonTransactionalLocalTID`. - `VersionMetadata::validateInfo`: early-return for rolled-back parts (`creation_csn == RolledBackCSN`). Such parts are marked `Outdated` immediately after loading by `MergeTreeData::loadDataPart` and cleaned up, so the live-part invariants validated here do not apply. CI reports showing the crash: - PR ClickHouse#103047: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=103047&sha=ec0124d77b94c97a4b1b48049b0215b1dddb94a5&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20distributed%20plan%2C%20s3%20storage%2C%20parallel%29 - PR ClickHouse#102961: same `Stateless tests (amd_debug, distributed plan, s3 storage, parallel)` check Regression test `04104_transaction_version_metadata_dummy_tid_load.sh` deterministically creates the scenario by dropping a bogus `txn_version.txt.tmp` into a part directory and running `DETACH` + `ATTACH`. Before the fix the server aborts with signal 6 at `TransactionID.h:90`; after the fix the rolled-back part is marked `Outdated` and invisible. Session: cron:clickhouse-ci-task-worker:20260418-221500
After ClickHouse#92141 refactored version metadata to use atomic tmp+rename writes instead of `WriteMode::Append`, the race that previously corrupted `txn_version.txt` on encrypted disks (see ClickHouse#59898 discussion) is no longer possible: concurrent `creation_csn` and `removal_tid` updates can no longer interleave inside the encrypted append path. Install `transactions.xml` in encrypted-storage CI jobs and drop the `no-encrypted-storage` tag from transaction-related stateless tests.
Refactor: Decouple version metadata logic from
IMergeTreeDataPartThis PR introduces a class hierarchy for managing transactional version metadata, separating data representation from persistence logic.
Motivation
The old
VersionMetadatamixed data storage, business logic, and disk persistence in a single struct. This refactoring creates a clean separation of concerns to enable future extensions (e.g., metadata stored in ZooKeeper for replicated tables).Architecture Changes
New class hierarchy:
VersionInfo- Pure data structure holding transaction IDs, CSNs, and storing versionVersionMetadata- Abstract base class with core MVCC logic (visibility, locking, CSN management)VersionMetadataOnDisk- Concrete implementation that persists metadata totxn_version.txtfiles on diskIntegration with
IMergeTreeDataPart:versionfrom value member tostd::unique_ptr<VersionMetadata>to enable polymorphismwriteVersionMetadata,loadVersionMetadata,appendCSNToVersionMetadata, etc.)VersionMetadataandVersionMetadataOnDiskKey Implementation Details
VersionInfo(new pure data class):creation_tid,removal_tid,creation_csn,removal_csn, andstoring_versionisVisiblelogic for MVCC snapshot isolationstoring_versionfield for optimistic concurrency control (detect concurrent metadata updates)VersionMetadata(abstract base class):setAndStoreCreationTID,setAndStoreRemovalTID,setAndStoreCreationCSN,setAndStoreRemovalCSNloadAndAdjustMetadatato recover CSNs fromTransactionLogafter server restartstoreInfoImpl,loadMetadata,tryLockRemovalTID, etc.updateStoreAndSetMetadataWithRefreshVersionMetadataOnDisk(disk persistence implementation):txn_version.txtfile within data part directory.tmpfile, then renameremoval_tid_lock_hashasstd::atomic<TIDHash>for lock-free removal lockingRemoval TID lock handling (key change):
tryLockRemovalTIDset bothremoval_tid_lockANDremoval_tid, thenappendRemovalTIDToVersionMetadatapersisted ittryLockRemovalTIDonly setsremoval_tid_lock_hashatomically;removal_tidis set and persisted separately viasetAndStoreRemovalTIDTransaction terminology updates:
Tx::PrehistoricTID/Tx::PrehistoricCSN→Tx::NonTransactionalTID/Tx::NonTransactionalCSNfor parts created without transactionsFile Organization
src/Interpreters/TransactionVersionMetadata.{cpp,h}deletedsrc/Interpreters/MergeTreeTransaction/VersionInfo.{cpp,h}addedsrc/Interpreters/MergeTreeTransaction/VersionMetadata.{cpp,h}addedsrc/Interpreters/MergeTreeTransaction/VersionMetadataOnDisk.{cpp,h}addedTesting
Updated existing integration and stateless tests to reflect terminology changes and new metadata validation logic.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Refactor MergeTree transaction version metadata management to improve code maintainability and enable future extensions.
Documentation entry for user-facing changes
Version info
26.4.1.996