Skip to content

Use global stopMergesAndWait in replacePartitionFrom to fix race - #97105

Merged
alexey-milovidov merged 3 commits into
masterfrom
fix-replace-partition-global-blocker
Feb 19, 2026
Merged

alexey-milovidov merged 3 commits into
masterfrom
fix-replace-partition-global-blocker

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Feb 16, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Replace stopMergesAndWaitForPartition with stopMergesAndWait in replacePartitionFrom to fix a remaining race condition from Fix race between REPLACE PARTITION and background mutations #96955
  • The per-partition blocker check in scheduleDataProcessingJob happens outside currently_processing_in_background_mutex, allowing a mutation to slip through after the blocker is set but before removePartsInRangeFromWorkingSet removes old parts

Details

PR #96955 moved the ActionLock merges_blocker to function scope, but the test 03920_replace_partition_mutation_race still failed ~20% of the time. The root cause: stopMergesAndWaitForPartition uses cancelForPartition, which only sets a per-partition blocker. In scheduleDataProcessingJob, the global isCancelled() check (line 1714) is inside the mutex, but the per-partition isCancelledForPartition() check (lines 1736-1742) is outside the mutex. This creates a race window where a mutation can be selected inside the mutex, pass the per-partition check outside the mutex, and get scheduled after the blocker was set.

Using stopMergesAndWait() (global blocker) instead eliminates this race because the global isCancelled() check is inside currently_processing_in_background_mutex, preventing any mutation from being selected while the blocker is active.

Changelog category (leave one):

  • Bug Fix

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

Fix remaining race condition between REPLACE PARTITION and background mutations in MergeTree tables that could cause old data to reappear.

🤖 Generated with Claude Code

Version info

  • Merged into: 26.2.1.909

…eplacePartitionFrom`

The per-partition blocker has a race: in `scheduleDataProcessingJob`, a mutation
can be selected inside `currently_processing_in_background_mutex`, but the
per-partition `isCancelledForPartition` check happens outside the mutex. This
allows a mutation to slip through after the blocker is set but before
`removePartsInRangeFromWorkingSet` runs, "resurrecting" old data.

The global blocker's `isCancelled` check is inside the mutex, preventing any
mutation from being selected while the blocker is active.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Feb 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [35b9687]

Summary: ❌

job_name test_name status info comment
Integration tests (amd_msan, 1/6) error
Timeout FAIL cidb

merges_blocker = stopMergesAndWaitForPartition(partition_id);
}

/// We use the global `stopMergesAndWait` (not the per-partition variant) because the

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a big change.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it essentially undoes some incorrect optimizations from someone of @Enmk @zvonand @ilejn @MyroTk @myrrc @mkmkme @arthurpassos @ianton-ru.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Feb 18, 2026
@alexey-milovidov
alexey-milovidov merged commit 261aa0b into master Feb 19, 2026
142 of 143 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-replace-partition-global-blocker branch February 19, 2026 08:11
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Feb 19, 2026
@alesapin

alesapin commented Mar 26, 2026 •

Copy link
Copy Markdown
Member

I don't understand how this patch fixes the race.
Imagine:

  1. scheduleDataProcessingJob do if (auto merge_select_result = selectPartsToMerge(metadata_snapshot, false, {}, false, shared_lock, lock, txn)) with lock and after go to sleep in if (merge_entry).
  2. Replace partition fully finishes with merge blocker.
  3. scheduleDataProcessingJob continues.

Looks like CI proves that the fix doesn't work, but degrade existing workloads: https://play.clickhouse.com/play?user=play#U0VMRUNUIGNoZWNrX3N0YXJ0X3RpbWUsIHB1bGxfcmVxdWVzdF9udW1iZXIsIGNoZWNrX25hbWUsIHRlc3RfbmFtZSwgdGVzdF9zdGF0dXMsIHJlcG9ydF91cmwgRlJPTSBjaGVja3Mgd2hlcmUgdGVzdF9uYW1lID0gJzAzOTIwX3JlcGxhY2VfcGFydGl0aW9uX211dGF0aW9uX3JhY2UnIGFuZCBjaGVja19zdGFydF90aW1lID49IHRvRGF0ZVRpbWUoJzIwMjYtMDEtMDEgMDA6MDA6MDAnKSBhbmQgdGVzdF9zdGF0dXMgaW4gKCdGQUlMJywgJ0VSUk9SJykgYW5kIHB1bGxfcmVxdWVzdF9udW1iZXIgIT0gOTY5NTUgb3JkZXIgYnkgY2hlY2tfc3RhcnRfdGltZSA7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

post-rejected Should've been rejected, but got merged. pr-improvement Pull request with some product improvements 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.

3 participants