Repository navigation
Undo non-transactional removal marks when MergeTreeData::Transaction::commit fails - #115442
kirillshokhin wants to merge 3 commits into
Conversation
…::commit` fails `MergeTreeData::Transaction::commit` marks the covered parts as removed before the `NOEXCEPT_SCOPE` that moves them to `Outdated`, because that marking can fail: a concurrent transaction may hold the removal lock, and storing txn_version.txt can fail on I/O. For a non-transactional commit nothing undid the marks that had already been written, so those parts stayed `Active` with `removal_tid = Tx::NonTransactionalTID` and `removal_csn = Tx::NonTransactionalCSN`. Such a part is inconsistent in several ways: * it is invisible to every transactional read while non-transactional reads still return its rows; * `TRUNCATE` skips it silently from then on; * `canBeRemoved` short-circuits to true for a non-transactional removal, so the part may be deleted as soon as anything moves it to `Outdated`, ignoring `old_parts_lifetime`; * if the part carries `txn_version.txt`, i.e. it was created or removed by a transaction, the inconsistency is persisted and survives a restart and the part is then loaded as removed. The failure is reachable because `MergeTreeTransaction::rollback` restores the parts of a rolled-back removal to `Active` and only resets and unlocks their removal TID in a second loop, without holding the parts lock in between. A non-transactional `TRUNCATE` or `DROP PARTITION` that covers such a part gets `SERIALIZATION_ERROR` in the middle of the loop, after earlier parts have been marked. The vector that used to collect these parts in commit was dead code — it was filled but never read. Track the covered parts that were marked and reset `removal_tid` and `removal_csn` for them if the loop throws. Only the non-transactional branch needs it: for transactions `MergeTreeTransaction::rollback` already resets and unlocks. The undo is best effort: every failure is logged per part, and the number of parts left inconsistent is appended to the propagated exception with a pointer to `system.parts`.
`MergeTreeData::Transaction::commit` holds the parts lock for its whole duration, so no other thread can change the set of active parts. The loop inside `NOEXCEPT_SCOPE` does change it via `modifyPartState`, but parts in `precommitted_parts` never cover each other, so the changes made for one part cannot affect the result for another. The values computed before `NOEXCEPT_SCOPE` therefore stay valid and are now reused from `overlapping_parts_for_commit` instead of being recalculated. This also removes a termination path: `getActivePartsToReplace` throws `LOGICAL_ERROR` for intersecting or duplicate parts, and any exception thrown inside `NOEXCEPT_SCOPE` calls `std::terminate`.
|
Workflow [PR], commit [db4de03] Summary: ❌
AI ReviewSummaryThis PR fixes a real non-transactional Final Verdict✅ No new findings. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 128/142 (90.14%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Object file sizes8 object files changed (+38.75 KiB total), 0 added.
737 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units42 translation units recompiled, 399 s compile time in total, 42 of them have a recent master baseline. |
|
Yes for the case in the title, and #105116 is what fixed it: it prevents the partial state instead of undoing it. On master ( Against the two tests in this PR:
The second commit here is independent of #105116: master still recomputes SELECT count() FROM default.checks
WHERE check_start_time > now() - INTERVAL 45 DAY AND test_status IN ('FAIL', 'ERROR')
AND position(test_context_raw, 'getActivePartsToReplace') > 0
-- 0 |
MergeTreeData::Transaction::commitmarks covered parts as removed before theNOEXCEPT_SCOPEthat moves them toOutdated, because that marking can fail: aconcurrent transaction may hold the removal lock, and storing
txn_version.txtcanfail on I/O. For a non-transactional commit nothing undid the marks that had already
been written (#97211 fixed only the dangling lock part, not the marks), so those parts stayed
Activewithremoval_tid = Tx::NonTransactionalTIDand
removal_csn = Tx::NonTransactionalCSN.After #92141 the lock is released inline and that lock cleanup became obsolete, yet the vector #97211 introduced to collect the parts was still filled and never read.
Such a part is inconsistent in several ways:
return its rows;
TRUNCATEskips it silently from then on;canBeRemovedshort-circuits to true for a non-transactional removal, so the partmay be deleted as soon as anything moves it to
Outdated, ignoringold_parts_lifetime;txn_version.txt, i.e. it was created or removed by atransaction, the inconsistency is persisted, survives a restart, and the part is
then loaded as removed.
The failure is reachable because
MergeTreeTransaction::rollbackrestores the parts ofa rolled-back removal to
Activeand only resets and unlocks their removal TID in asecond loop, without holding the parts lock in between. A non-transactional
TRUNCATEor
DROP PARTITIONcovering such a part getsSERIALIZATION_ERRORin the middle ofthe loop, after earlier parts have already been marked.
Track the covered parts that were marked and reset
removal_tidandremoval_csnforthem if the loop throws. Only the non-transactional branch needs this: for transactions
MergeTreeTransaction::rollbackalready resets and unlocks. The undo is best effort —every failure is logged per part, and the number of parts left inconsistent is appended
to the propagated exception together with a pointer to
system.parts.The second commit removes the
getActivePartsToReplacecall that was repeated insideNOEXCEPT_SCOPE. The parts lock is held for the whole ofcommit, and parts inprecommitted_partsnever cover each other, so the values computed before the scopestay valid and are now reused. That also removes a termination path, since
getActivePartsToReplacethrowsLOGICAL_ERRORfor intersecting or duplicate parts andany exception inside
NOEXCEPT_SCOPEreachesstd::terminate.Two failpoints and two tests cover the two ways the marking loop can fail:
04838_truncate_and_drop_part_transaction_rollback_racefreezes a transaction rollbackbetween restoring the parts and unlocking their removal TID, then checks that a
concurrent
TRUNCATEfails withSERIALIZATION_ERRORand leaves no part marked as removed;04839_fail_to_remove_covered_part_in_the_middle_of_the_listinjects a failure afterthe first covered part of a merge has been marked.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a case where a failed non-transactional
TRUNCATE,DROP PARTITIONor merge left covered partsActivebut marked as removed. Such parts were invisible to transactional reads while non-transactional reads still returned their rows, were silently skipped by laterTRUNCATEqueries, could be deleted ignoringold_parts_lifetime, and for parts carryingtxn_version.txtthe inconsistency survived a restart. This was only reachable when transaction were enabled.Related: #96023
Related: #97211
Related: #92141
Workflow [PR]
Sync PR [sync-upstream/pr/115442]