Skip to content

Fix PartLoadingTree to correctly identify the rolled-back part when intersecting parts have transaction metadata - #100992

Merged
alexey-milovidov merged 17 commits into
ClickHouse:masterfrom
tuanpach:fix-part-loading-tree-rollback-detection
Jul 19, 2026
Merged

alexey-milovidov merged 17 commits into
ClickHouse:masterfrom
tuanpach:fix-part-loading-tree-rollback-detection

Conversation

@tuanpach

@tuanpach tuanpach commented Mar 28, 2026 •

Copy link
Copy Markdown
Member

During loadDataParts, PartLoadingTree::add builds a containment tree of on-disk parts sorted by (level, mutation) descending. When a rolled-back part already in the tree intersects an incoming committed part, the previous implementation had two bugs:

Bug 1 — wrong skip direction. The old code (has_transaction_metadata) checked whether either intersecting part has a txn_version.txt file and unconditionally skipped the incoming part. When the tree-resident part was the rolled-back one, the committed incoming part was silently discarded instead of replacing it.

Bug 2 — orphans dropped after eviction. When the rolled-back resident is evicted, its children become orphaned. They must be re-inserted via add() — together with the incoming committed part — sorted by (level, mutation) descending so that the incoming part is placed into the tree before any orphan that belongs inside it. Without this ordering an orphan sitting at sibling level causes a spurious LOGICAL_ERROR when the incoming part is re-inserted.

The LOGICAL_ERROR that caused the fatal in the stress test CI was triggered because the old helper (has_transaction_metadata) returned bool — a plain file-existence check. It could not distinguish between a definitively rolled-back part, an in-flight transaction (Tx::UnknownCSN), and a read error (e.g. an empty file read between shell truncate and write). When neither part was definitively rolled-back the code fell through to LOGICAL_ERROR, which aborts the server in sanitizer builds. The new enum class RollbackStatus with five states (RolledBack, Committed, NoMetadata, UnknownCSN, Unreadable) lets each case be handled precisely.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fixed a server abort (in sanitizer builds) and silent data loss during table startup when a rolled-back transactional MergeTree part intersects a committed part. PartLoadingTree::add now correctly identifies which of the two intersecting parts was rolled back, evicts it, and re-parents its orphaned children under the committed part in the correct order.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.7.1.1177 (included in 26.7 and later)

@tuanpach tuanpach added the can be tested Allows running workflows for external contributors label Mar 28, 2026
@clickhouse-gh

clickhouse-gh Bot commented Mar 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8207101]

Summary: ✅


AI Review

Summary

This PR hardens PartLoadingTree::add around rolled-back and unresolved transactional parts and adds good startup / ATTACH regressions for the intersection cases. I do not think it is ready to merge yet, because the reverted contains-branch handling is only safe for loadDataPartsFromDisk; the readonly refresh path still consumes the same tree without the child-promotion fallback, so committed descendants can remain hidden and the rolled-back ancestor can even be re-activated.

PR Metadata
  • Changelog category is too weak for the behavior described here. The PR fixes silent data loss during part loading, so this should be Critical Bug Fix (crash, data loss, RBAC) rather than Bug Fix (user-visible misbehavior in an official stable release).
  • Changelog entry is present and specific enough.
Findings

❌ Blockers

  • [src/Storages/MergeTree/MergeTreeData.cpp:2115] The claim that containment needs no special handling only holds in loadDataPartsFromDisk. refreshDataPartsOnce also builds PartLoadingTree, but it keeps only top-level nodes and then preparePartForCommits them (src/Storages/MergeTree/MergeTreeData.cpp:3083-3112). With the 04241 shape on a readonly refresh / SYSTEM RESTART DISK, the rolled-back ancestor stays top-level, its committed descendants are never queued, and preparePartForCommit resets the ancestor from Outdated back to PreActive. The result is the same user-visible regression this PR is trying to eliminate: committed children stay missing, and the rolled-back ancestor can become active again.
    Suggested fix: either restore containment handling for this caller too, or teach refreshDataPartsOnce to promote children of top-level parts that load as Outdated and never commit those ancestors.
Tests
  • ⚠️ Please add a focused regression for the readonly refresh path (refreshDataPartsOnce / SYSTEM RESTART DISK) using the 04241 topology, so this bug cannot hide behind the startup-only tests.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions: fix the readonly refresh path so rolled-back ancestors in the contains shape cannot bury or re-activate over committed descendants, and add a focused regression for that path.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Mar 28, 2026
@tuanpach
tuanpach force-pushed the fix-part-loading-tree-rollback-detection branch from c1fb07d to f88571b Compare March 28, 2026 11:32
@tuanpach
tuanpach force-pushed the fix-part-loading-tree-rollback-detection branch from 83ccc10 to d3af6b4 Compare March 30, 2026 04:01
@tuanpach
tuanpach force-pushed the fix-part-loading-tree-rollback-detection branch from d3af6b4 to e1b5cbb Compare March 30, 2026 05:53
@tuanpach
tuanpach force-pushed the fix-part-loading-tree-rollback-detection branch 2 times, most recently from 7e69815 to 664d85c Compare April 1, 2026 04:07
Comment thread src/Storages/MergeTree/MergeTreeData.h Resolved
@tuanpach
tuanpach force-pushed the fix-part-loading-tree-rollback-detection branch from 1e9b03c to 2bdfe94 Compare April 2, 2026 00:43
@tuanpach
tuanpach force-pushed the fix-part-loading-tree-rollback-detection branch from 2bdfe94 to 20bdf6b Compare April 2, 2026 00:55
tuanpach added a commit to tuanpach/ClickHouse that referenced this pull request Apr 2, 2026
`PartLoadingTree::add` builds a containment tree of MergeTree parts during
`loadDataParts`. Parts are sorted by `(level, mutation)` descending so that
covering parts are inserted before the parts they contain. When a rolled-back
part in the tree intersects an incoming committed part two bugs were present:

**Bug 1 — wrong skip direction.**
The old code (`has_transaction_metadata`) checked whether either part has a
`txn_version.txt` file and unconditionally skipped the *incoming* part. When
the already-resident part was the rolled-back one, the committed incoming part
was silently discarded instead of replacing it.

**Bug 2 — orphans dropped after eviction.**
When the rolled-back resident is evicted, its children are orphaned. They
must be re-inserted via `add()` — together with the incoming committed part —
sorted by `(level, mutation)` descending so that the incoming part (typically
higher level) is placed into the tree *before* any orphan that belongs inside
it. Without this ordering an orphan at sibling level causes a spurious
`LOGICAL_ERROR` when the incoming part is re-inserted.

The crash observed in the stress test CI was caused by a third issue: the
old helper (`has_transaction_metadata`) returned `bool` — a plain
file-existence check. It could not distinguish between a definitively
rolled-back part, an in-flight transaction (`Tx::UnknownCSN`), and a read
error (e.g. an empty file read between shell `truncate` and `write`). When
neither part was definitively rolled-back, the code fell through to
`LOGICAL_ERROR`, which aborts the server in sanitizer builds.

CI: ClickHouse#100992

Replace the `bool`-returning helper with a 5-state `enum class RollbackStatus`
and rename the lambda to `read_txn_status`:

  `RolledBack`  — `creation_csn == Tx::RolledBackCSN` on disk
  `Committed`   — `creation_csn` is a known committed CSN on disk
  `NoMetadata`  — `txn_version.txt` absent (prehistoric/non-transactional part)
  `UnknownCSN`  — file present, `creation_csn == Tx::UnknownCSN` (in-flight)
  `Unreadable`  — file present but could not be parsed (corruption/partial write)

Intersection decision table (applied to both prev/next branches):

  `Unreadable` on either side       → `CORRUPTED_DATA` (not a code bug)
  incoming `RolledBack`             → skip incoming
  existing `RolledBack`             → evict existing; collect orphans + incoming
                                       sorted by `(level, mutation)` desc;
                                       re-add all via `add()`; `return`
  either `UnknownCSN`               → skip incoming (`loadDataPart` resolves later)
  both `NoMetadata` or `Committed`  → `LOGICAL_ERROR` (genuine bug / manual edit)

The `traverse` template body is moved inline into the header and `PartLoadingTree`
is made `public` so external tests can exercise it directly.

Add `tests/queries/0_stateless/04063_part_loading_tree_rollback_reparent.sh`.
The test manually constructs four part directories:

  `all_1_2_2_1` (1-2, L2, M1) — rolled-back parent; `txn_version.txt` holds
                                  `Tx::RolledBackCSN`; `creation_tid` uses
                                  `start_csn=2` (not `Tx::PrehistoricCSN`)
                                  so `wasInvolvedInTransaction` returns true
  `all_1_1_1_1` (1-1, L1, M1) — committed child; contained in 1-2 → inserted
                                  as a child of `all_1_2_2_1` during build
  `all_2_3_1_0` (2-3, L1, M0) — committed part; intersects `all_1_2_2_1`,
                                  triggering the eviction and re-parenting path

After `ATTACH TABLE`: `all_1_1_1_1` must be active (orphan successfully
re-parented) and `all_1_2_2_1` must not be active (rolled-back part discarded).

`txn_version.txt` is written atomically (`printf` to `.tmp` then `mv`) to
eliminate a stress-test race where the server reads the file in the window
between shell truncation and the actual write, producing an empty file and
a parse error (`Unreadable` state → `CORRUPTED_DATA` instead of server abort).

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@tuanpach
tuanpach force-pushed the fix-part-loading-tree-rollback-detection branch from 20bdf6b to f2a0273 Compare April 2, 2026 01:02
@alexey-milovidov

Copy link
Copy Markdown
Member

The Stress test (arm_msan) failure is fixed by #101239, which should be merged first. After it is merged, please update the branch to include the fix.

tuanpach and others added 2 commits May 17, 2026 04:59
`PartLoadingTree::add` builds a containment tree of MergeTree parts during
`loadDataParts`. Parts are sorted by `(level, mutation)` descending so that
covering parts are inserted before the parts they contain. When a rolled-back
part in the tree intersects an incoming committed part two bugs were present:

**Bug 1 — wrong skip direction.**
The old code (`has_transaction_metadata`) checked whether either part has a
`txn_version.txt` file and unconditionally skipped the *incoming* part. When
the already-resident part was the rolled-back one, the committed incoming part
was silently discarded instead of replacing it.

**Bug 2 — orphans dropped after eviction.**
When the rolled-back resident is evicted, its children are orphaned. They
must be re-inserted via `add()` — together with the incoming committed part —
sorted by `(level, mutation)` descending so that the incoming part (typically
higher level) is placed into the tree *before* any orphan that belongs inside
it. Without this ordering an orphan at sibling level causes a spurious
`LOGICAL_ERROR` when the incoming part is re-inserted.

The crash observed in the stress test CI was caused by a third issue: the
old helper (`has_transaction_metadata`) returned `bool` — a plain
file-existence check. It could not distinguish between a definitively
rolled-back part, an in-flight transaction (`Tx::UnknownCSN`), and a read
error (e.g. an empty file read between shell `truncate` and `write`). When
neither part was definitively rolled-back, the code fell through to
`LOGICAL_ERROR`, which aborts the server in sanitizer builds.

CI: ClickHouse#100992

Replace the `bool`-returning helper with a 5-state `enum class RollbackStatus`
and rename the lambda to `read_txn_status`:

  `RolledBack`  — `creation_csn == Tx::RolledBackCSN` on disk
  `Committed`   — `creation_csn` is a known committed CSN on disk
  `NoMetadata`  — `txn_version.txt` absent (prehistoric/non-transactional part)
  `UnknownCSN`  — file present, `creation_csn == Tx::UnknownCSN` (in-flight)
  `Unreadable`  — file present but could not be parsed (corruption/partial write)

Intersection decision table (applied to both prev/next branches):

  `Unreadable` on either side       → `CORRUPTED_DATA` (not a code bug)
  incoming `RolledBack`             → skip incoming
  existing `RolledBack`             → evict existing; collect orphans + incoming
                                       sorted by `(level, mutation)` desc;
                                       re-add all via `add()`; `return`
  either `UnknownCSN`               → skip incoming (`loadDataPart` resolves later)
  both `NoMetadata` or `Committed`  → `LOGICAL_ERROR` (genuine bug / manual edit)

The `traverse` template body is moved inline into the header and `PartLoadingTree`
is made `public` so external tests can exercise it directly.

Add `tests/queries/0_stateless/04063_part_loading_tree_rollback_reparent.sh`.
The test manually constructs four part directories:

  `all_1_2_2_1` (1-2, L2, M1) — rolled-back parent; `txn_version.txt` holds
                                  `Tx::RolledBackCSN`; `creation_tid` uses
                                  `start_csn=2` (not `Tx::PrehistoricCSN`)
                                  so `wasInvolvedInTransaction` returns true
  `all_1_1_1_1` (1-1, L1, M1) — committed child; contained in 1-2 → inserted
                                  as a child of `all_1_2_2_1` during build
  `all_2_3_1_0` (2-3, L1, M0) — committed part; intersects `all_1_2_2_1`,
                                  triggering the eviction and re-parenting path

After `ATTACH TABLE`: `all_1_1_1_1` must be active (orphan successfully
re-parented) and `all_1_2_2_1` must not be active (rolled-back part discarded).

`txn_version.txt` is written atomically (`printf` to `.tmp` then `mv`) to
eliminate a stress-test race where the server reads the file in the window
between shell truncation and the actual write, producing an empty file and
a parse error (`Unreadable` state → `CORRUPTED_DATA` instead of server abort).

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
…nfo` format

The test writes `txn_version.txt` directly to mark `all_1_2_2_1` as rolled back.
Master switched to `VersionInfo::readFromMultiLineBuffer`, which has a silent
fallback for the old format that overrides `creation_csn` with
`Tx::NonTransactionalCSN`. As a result the test's `creation_csn: 18446744073709551615`
(`Tx::RolledBackCSN`) was ignored, `read_txn_status` returned `Committed` for
both intersecting parts, and `ATTACH TABLE` threw `LOGICAL_ERROR` again.

Write the full new format including `storing_version`, `removal_tid` and
`removal_csn` so the rolled-back state is detected.

Also make the test stricter:
- `ATTACH TABLE` exit code is now checked (previously stderr was suppressed
  and a thrown exception was silently ignored).
- The presence-of-active checks for `all_1_1_1_1`, `all_1_2_2_1` and
  `all_2_3_1_0` are now explicit `count` comparisons that exit with a clear
  `FAIL:` message when they don't match.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/PRs/100992/c7f836579347baebec2d4a153b9c4a540d236ac0/fast_test/
@tuanpach
tuanpach force-pushed the fix-part-loading-tree-rollback-detection branch from c7f8365 to 4ff63d0 Compare May 17, 2026 10:03
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 19, 2026
pull Bot pushed a commit to Mu-L/ClickHouse that referenced this pull request Jul 27, 2026
…-encrypted-storage

The Upgrade check configures the server with object storage as the default
MergeTree policy (configure --s3-storage) and randomly enables encrypted
storage, but ran clickhouse-test without --s3-storage and never forwarded
--encrypted-storage. So clickhouse-test had args.s3_storage / args.encrypted_storage
= False and ignored its storage skip tags (no-object-storage, no-s3-storage,
no-encrypted-storage): those branches in should_skip_test fire only when the
matching argument is set. Tests declared incompatible with the configured
backend then ran on an unsupported one.

This produced the Upgrade check (amd_release) Code 27 / Code 246 failures on
the four 04063/04241/04417/04500_part_loading_tree_rollback_* tests (all tagged
no-object-storage): they fabricate a raw plaintext txn_version.txt in a part
directory (valid only on a local disk), which is unreadable on an object-storage
disk, so PartLoadingTree::add (from ClickHouse#100992) reports CORRUPTED_DATA at the test's
own ATTACH. With --upgrade-check (which implies --fake-drop) the corrupt part
survives into the new server, which logs the parse error at part load.
ClickHouse#111055 / ClickHouse#111661 hardened these tests on master, but the Upgrade check runs the
frozen previous-release test suite, so those changes cannot reach it.

Forward --s3-storage (in the clickhouse-test command) and --encrypted-storage
(mirroring the coin flip, via stress.py) so clickhouse-test matches the server's
actual configuration and skips incompatible tests, as the regular stress runner
already does.

Related: ClickHouse#111053

Co-Authored-By: Claude Opus 4.8 <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
`PartLoadingTree::add` builds a containment tree of MergeTree parts during
`loadDataParts`. Parts are sorted by `(level, mutation)` descending so that
covering parts are inserted before the parts they contain. When a rolled-back
part in the tree intersects an incoming committed part two bugs were present:

**Bug 1 — wrong skip direction.**
The old code (`has_transaction_metadata`) checked whether either part has a
`txn_version.txt` file and unconditionally skipped the *incoming* part. When
the already-resident part was the rolled-back one, the committed incoming part
was silently discarded instead of replacing it.

**Bug 2 — orphans dropped after eviction.**
When the rolled-back resident is evicted, its children are orphaned. They
must be re-inserted via `add()` — together with the incoming committed part —
sorted by `(level, mutation)` descending so that the incoming part (typically
higher level) is placed into the tree *before* any orphan that belongs inside
it. Without this ordering an orphan at sibling level causes a spurious
`LOGICAL_ERROR` when the incoming part is re-inserted.

The crash observed in the stress test CI was caused by a third issue: the
old helper (`has_transaction_metadata`) returned `bool` — a plain
file-existence check. It could not distinguish between a definitively
rolled-back part, an in-flight transaction (`Tx::UnknownCSN`), and a read
error (e.g. an empty file read between shell `truncate` and `write`). When
neither part was definitively rolled-back, the code fell through to
`LOGICAL_ERROR`, which aborts the server in sanitizer builds.

CI: ClickHouse#100992

Replace the `bool`-returning helper with a 5-state `enum class RollbackStatus`
and rename the lambda to `read_txn_status`:

  `RolledBack`  — `creation_csn == Tx::RolledBackCSN` on disk
  `Committed`   — `creation_csn` is a known committed CSN on disk
  `NoMetadata`  — `txn_version.txt` absent (prehistoric/non-transactional part)
  `UnknownCSN`  — file present, `creation_csn == Tx::UnknownCSN` (in-flight)
  `Unreadable`  — file present but could not be parsed (corruption/partial write)

Intersection decision table (applied to both prev/next branches):

  `Unreadable` on either side       → `CORRUPTED_DATA` (not a code bug)
  incoming `RolledBack`             → skip incoming
  existing `RolledBack`             → evict existing; collect orphans + incoming
                                       sorted by `(level, mutation)` desc;
                                       re-add all via `add()`; `return`
  either `UnknownCSN`               → skip incoming (`loadDataPart` resolves later)
  both `NoMetadata` or `Committed`  → `LOGICAL_ERROR` (genuine bug / manual edit)

The `traverse` template body is moved inline into the header and `PartLoadingTree`
is made `public` so external tests can exercise it directly.

Add `tests/queries/0_stateless/04063_part_loading_tree_rollback_reparent.sh`.
The test manually constructs four part directories:

  `all_1_2_2_1` (1-2, L2, M1) — rolled-back parent; `txn_version.txt` holds
                                  `Tx::RolledBackCSN`; `creation_tid` uses
                                  `start_csn=2` (not `Tx::PrehistoricCSN`)
                                  so `wasInvolvedInTransaction` returns true
  `all_1_1_1_1` (1-1, L1, M1) — committed child; contained in 1-2 → inserted
                                  as a child of `all_1_2_2_1` during build
  `all_2_3_1_0` (2-3, L1, M0) — committed part; intersects `all_1_2_2_1`,
                                  triggering the eviction and re-parenting path

After `ATTACH TABLE`: `all_1_1_1_1` must be active (orphan successfully
re-parented) and `all_1_2_2_1` must not be active (rolled-back part discarded).

`txn_version.txt` is written atomically (`printf` to `.tmp` then `mv`) to
eliminate a stress-test race where the server reads the file in the window
between shell truncation and the actual write, producing an empty file and
a parse error (`Unreadable` state → `CORRUPTED_DATA` instead of server abort).

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…loss

Addresses ClickHouse#100992 (comment)

Previously, when an intersecting tree-resident part had `creation_csn = Tx::UnknownCSN`
on disk, the incoming committed peer was dropped from the loading tree. The plan
was that `loadDataPart` would resolve the CSN afterwards. But `loadDataPart` only
runs on the top-level nodes of the loading tree, and the dropped peer was never
re-introduced — so if the in-flight transaction turned out to be rolled back, the
committed peer was permanently absent for that startup (data only became visible
after a second restart, once `Tx::RolledBackCSN` was persisted to disk).

`read_txn_status` now consults `TransactionLog` when the on-disk CSN is unresolved,
mirroring the logic of `VersionMetadata::tryGetCSN`: if the tid has no CSN and no
running transaction, it's treated as rolled back. The `UnknownCSN` `RollbackStatus`
arm now only fires for transactions that are genuinely still running (an unusual
case during `loadDataParts`), and the rolled-back arm correctly evicts the dead
part and keeps the committed peer.
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…rtLoadingTree::add`

Addresses ClickHouse#100992 (comment)

A committed part can be silently buried under a rolled-back ancestor when the
ancestor contains it (rather than just intersects). The previous code only
checked rollback status in the `isDisjoint == false` (intersection) branch, so
when an incoming committed part descended through the `contains` branch into a
rolled-back ancestor it became a "covered" child and was loaded as `Outdated`
instead of active — invisible to queries until the next restart resolved the
ancestor's `creation_csn` to `Tx::RolledBackCSN` on disk.

Add the same eviction check in both `contains` branches (`prev` and `next`).
On match, the helper `evict_rolled_back_and_reinsert` collects the rolled-back
node's orphaned descendants, sorts them with the incoming part by
`(level, mutation)` descending, and re-inserts the lot — so any new
top-level ancestor is added before its descendants and re-parenting happens
cleanly.

Also extract the inline eviction code from the two intersection branches into
the same helper, removing the duplicated logic.

New regression test `04241_part_loading_tree_rollback_contains` exercises the
`R-contains-{C, N}` topology that triggered a `LOGICAL_ERROR` (or silent data
loss, depending on the levels) before this fix.
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Addresses https://github.com/ClickHouse/ClickHouse/actions/runs/26031517236
(`test_transactions/test.py::test_rollback_unfinished_on_restart{1,2}`
fail with `len(res) == 5` because the rolled-back mutation parts are
not in `system.parts`) and
ClickHouse#100992 (comment)
(bot flags an `UnknownCSN` race in the same branch).

The prior commit (`7587b5c5101`) added a rollback check in the `contains`
branches and evicted the ancestor when found rolled-back. But the rest of
the load path already handles this — `MergeTreeData::loadDataPartsFromDisk`
detects that a top-level part was demoted to `Outdated` (e.g. by
`loadDataPart`'s `creation_csn == Tx::RolledBackCSN` check) and pushes its
children onto `parts_to_load` to be loaded as `Active`. So the eviction in
`PartLoadingTree::add` was redundant, and worse it removed the rolled-back
parts from `system.parts` entirely, which the integration test relies on.

This commit keeps:
- the intersection-branch eviction (the original PR's fix — there the
  promotion mechanism does not apply because the committed peer is a
  sibling, not a child, of the rolled-back tree-resident);
- the eager `UnknownCSN` resolution in `read_txn_status`;
- the helper `evict_rolled_back_and_reinsert` (still used by both
  intersection branches);
- `04241_part_loading_tree_rollback_contains` — the test now passes via
  the existing promotion mechanism rather than tree-level eviction, but
  the assertion remains the same (committed children are active).
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Address AI review (clickhouse-gh) on PR ClickHouse#100992: `read_txn_status` in
`PartLoadingTree::add` only probed the final `txn_version.txt` and returned
`NoMetadata` when it was absent. But `VersionMetadataOnDisk::loadMetadata`
already treats a part that has only a leftover `txn_version.txt.tmp` (and no
final `txn_version.txt`) as rolled back: the creating transaction was
interrupted before atomically renaming its metadata into place, so it never
committed.

Because of the mismatch, such a tmp-only leftover was classified as a
non-transactional part during tree construction. When it intersected a
committed peer on startup or `ATTACH`, the code fell through to the generic
intersecting-parts `LOGICAL_ERROR` instead of evicting the rolled-back
leftover and keeping the committed part.

`read_txn_status` now mirrors `VersionMetadataOnDisk::loadMetadata`: when the
final `txn_version.txt` is absent it checks for `txn_version.txt.tmp` and, if
present, returns `RolledBack`. The tmp filename is promoted to a shared
constant `VersionMetadata::TMP_TXN_VERSION_METADATA_FILE_NAME` so both call
sites classify the metadata the same way.

Adds `04500_part_loading_tree_rollback_tmp_metadata.sh`: a rolled-back
leftover carrying only `txn_version.txt.tmp` intersects a committed peer;
`ATTACH` must succeed and keep the committed part active.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…ree-rollback-detection

Fix `PartLoadingTree` to correctly identify the rolled-back part when intersecting parts have transaction metadata
robinld pushed a commit to robinld/ClickHouse that referenced this pull request Oct 4, 2026
Follow-up to ClickHouse#100992. That PR taught the startup part loader
(loadDataPartsFromDisk) to promote committed descendants of a rolled-back
top-level part, and reverted the tree-level contains-branch eviction because
loadDataPartsFromDisk already handles containment via orphan promotion. The
read-only refresh path (refreshDataPartsOnce, used by the background refresh
task on all-readonly-disk tables and by SYSTEM RESTART DISK) had no such
promotion: it traverses only top-level loading-tree nodes and unconditionally
preparePartForCommit()s each one.

On the containment topology (a rolled-back ancestor covering committed
children) a read-only refresh therefore:
- committed the rolled-back top-level part. loadDataPart demotes it to
  Outdated, and preparePartForCommit then resets it to PreActive, re-activating
  a rolled-back part (LOGICAL_ERROR in assertHasVersionMetadata, server abort
  in debug/sanitizer builds); and
- never surfaced the committed children, which are covered by the ancestor in
  the tree and so are never reached by the top-level traversal.

Skip committing a part that loaded as Outdated (rolled back) and instead
re-enqueue its committed children, mirroring the orphan promotion
loadDataPartsFromDisk performs. The loading tree is left untouched, so this
does not re-open test_transactions::test_rollback_unfinished_on_restart{1,2}
(that regression came from tree-level eviction removing rolled-back parts from
system.parts; here the rolled-back part is still loaded, just left Outdated).

Add a regression test on the 04241 containment topology that triggers the
read-only refresh path via SYSTEM RESTART DISK.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-bugfix Pull request with bugfix, not backported by default 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.

4 participants