Repository navigation
Fix race between mutation registration and KILL TRANSACTION leaving an orphaned mutation - #110226
tiandiwonder merged 16 commits into
Conversation
prepareMutationEntry adds the mutation to the transaction before startMutation registers the entry in current_mutations_by_version. A rollback in that window (e.g. KILL TRANSACTION) calls killMutation, which finds nothing to remove, and the entry registered afterwards is orphaned: its transaction is gone and its CSN will never be assigned. Background jobs then raise a logical error "Cannot find transaction ... that has started mutation ..." (which aborts debug builds, as does the chassert in getIncompleteMutationsStatusUnlocked reached from waitForMutation), and the orphaned entry blocks all subsequent mutations of the affected parts until server restart. Re-check the transaction state under the same critical section as the registration: a rollback that starts after the critical section finds the registered entry and removes it via killMutation as usual; a rollback that started before is detected by the re-check and the entry is removed immediately, failing the query with INVALID_TRANSACTION. Add a pauseable failpoint mt_pause_before_register_mutation and a stateless test that reproduces the race deterministically: without the fix, the test aborts a debug server. Co-Authored-By: Claude Fable 5 <[email protected]>
|
Workflow [PR], commit [d4ecf23] Summary: ✅
AI ReviewSummaryThis PR closes the Final VerdictNo findings. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 30/30 (100.00%) · Uncovered code |
The previous comment claimed the getCSN fallback prevents a committed transaction's mutation from aborting the server via a background job after a restart. That is inaccurate: `setMutationCSN` assigns the mutation CSN before the transaction leaves the running list, and `loadMutations` resolves or removes unresolved transactional entries at startup, so a committed transaction's cached CSN is not stale once `tryGetTransactionForMutation` misses. Rewrite the comment to describe what actually happens: the cached CSN may be stale, the transaction log is authoritative, and the not-committed case (including an orphaned entry left by a mutation-registration/`KILL TRANSACTION` race, fixed at the source in #110226) is skipped instead of raising a `LOGICAL_ERROR`. Per review thread by tiandiwonder on src/Storages/StorageMergeTree.cpp. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Address review: the previous behavior of skipping a mutation whose transaction is gone and uncommitted left the entry in `current_mutations_by_version` forever. Since mutation selection always starts from the earliest pending mutation of a part, such an orphaned entry (left by a race between mutation registration in `startMutation` and the `killMutation` sweep of a concurrent `KILL TRANSACTION` rollback) blocked all subsequent mutations of the affected parts until a restart. Now `selectPartsToMutate` removes the orphaned entry, the same way `loadMutations` discards entries of uncommitted transactions at startup, and caches the CSN in the entry when the transaction log shows the transaction as committed (so `system.mutations` displays it and the log is not consulted again on every scheduling round). Add a pause failpoint `mt_start_mutation_pause_before_register` between the transaction-side and the storage-side mutation registration, and a deterministic integration test `test_kill_transaction_mutation_registration_race` that reproduces the orphaned state with `KILL TRANSACTION`: on an unfixed server the background job raises a `LOGICAL_ERROR` exception on every scheduling round and the orphaned mutation stays pending forever; with the fix it is removed and later mutations proceed. Reframe the previously added restart test as a smoke test, since it does not exercise this branch. Related: #83252 Related: #110226 Co-Authored-By: Claude Fable 5 <[email protected]>
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 30/30 (100.00%) · 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. Compile time of recompiled translation units8 translation units recompiled, 23 s compile time in total, 8 of them have a recent master baseline. |
|
🕵 Addressed the remaining CI failure in ed9fe11. |
Problem
KILL TRANSACTIONracing withALTER TABLE ... UPDATE/DELETEexecuted inside a transaction can leave an orphaned mutation entry: registered in the table's mutation map, but its transaction is rolled back and gone, and its CSN will never be assigned. The consequences (reproduced deterministically with the failpoint added in this PR):ALTER'swaitForMutationhitschassert(txn || mutation_entry.tid.isNonTransactional() || from_another_mutation)ingetIncompleteMutationsStatusUnlocked, and the backgroundselectPartsToMutateindependently raises a logical errorCannot find transaction ... that has started mutation ... and mutation CSN is still unknown.ALTERhangs, the background job throws the logical error on every scheduling round, and the orphaned entry is never removed (clearOldMutationshandles only finished mutations), blocking all subsequent mutations of the affected parts until a server restart.Root cause
prepareMutationEntrycallstxn->addMutationbeforestartMutationregisters the entry incurrent_mutations_by_version. A rollback in that window snapshots the transaction's mutation list (which already contains the new mutation) and callskillMutation, which finds nothing to remove because the entry is not registered yet. The registration then completes, creating the orphan. The window has existed since the ordering was introduced; it is merely a few instructions wide, which is why it is hit rarely in practice.Solution
After
addPreparedMutationEntry, re-check the transaction state under the same critical section as the registration. If the transaction was rolled back in the window, unregister the entry immediately, remove the mutation file (logged), and fail the query withINVALID_TRANSACTION— the same error the client would get if theKILLhad landed a moment earlier. Both interleavings are covered: a rollback that starts after the critical section finds the registered entry and removes it viakillMutationas usual; a rollback that started before is detected by the re-check (its CSN change precedes itskillMutationcall, so it is visible). Holding the lock across registration and re-check also keeps the transient state invisible toselectPartsToMutate, which reads the map under the same mutex.The metadata-
ALTERpath inStorageMergeTree::alteralso registers a prepared mutation entry with a wider gap, but killing a rename mutation on rollback there would be incorrect (the durable metadata commit cannot be rolled back, and removing the rename mutation while keeping the new metadata reopens the #80648 data-loss scenario); metadata alters inside transactions have separate unresolved semantics and are out of scope here.A
PAUSEABLEfailpointmt_pause_before_register_mutationand a stateless test reproduce the race deterministically: without the fix, the test aborts a debug server; with the fix, theALTERfails withINVALID_TRANSACTION, no mutation entry or file is left behind, and subsequent mutations proceed.Related: #110202
Related: #83252
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a race between
KILL TRANSACTIONandALTER TABLE ... UPDATE/DELETEexecuted inside a transaction: a rollback landing between the mutation's registration in the transaction and its registration in the table left an orphaned mutation entry, which raised a logical error (Cannot find transaction ... that has started mutation ...) in background jobs and blocked all subsequent mutations of the affected parts until server restart.Documentation entry for user-facing changes
Version info
26.8.1.1523(included in26.8and later)