Skip to content

Fix REPLACE PARTITION waiting for mutations/merges on unrelated partitions - #59138

Merged
alexey-milovidov merged 43 commits into
ClickHouse:masterfrom
Enmk:fix_replace_partition_stop_the_world
Sep 30, 2024
Merged

alexey-milovidov merged 43 commits into
ClickHouse:masterfrom
Enmk:fix_replace_partition_stop_the_world

Conversation

@Enmk

@Enmk Enmk commented Jan 23, 2024

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Improvement

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

ALTER TABLE .. REPLACE PARTITION doesn't wait anymore for mutations/merges that happen in other partitions

Add a user-readable short description of the changes that should be added to docs.clickhouse.com below.
On a big table with lots of parts/partitions and lots of merges/mutations scheduled on a table, ALTER TABLE .. REPLACE PARTITION waits very long until all of those finish, even if the target partition is not affected by those merges and/or mutations. This leads to poor maintenance experience.

With this improvement, ALTER TABLE .. REPLACE PARTITION only waits for merges/mutations to the target partition, greatly reducing up the waiting period.

Technically this stops the merges on all partitions and prevents new ones from starting, rather than stopping merges on the target partition only. But since REPLACE is relatively simple, there is a high chance that merges and mutations are re-allowed relatively fast.

Related: #45335
Continuation of: #54272

@Enmk

Enmk commented Jan 31, 2024

Copy link
Copy Markdown
Contributor Author

@alexey-milovidov could you please take a look or assign anyone to do it?
The thread-safety issues were fixed.

P.S. I had to create a new PR since I wasn't able to re-open the old.

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Jan 31, 2024
@robot-clickhouse robot-clickhouse added pr-improvement Pull request with some product improvements submodule changed At least one submodule changed in this PR. labels Jan 31, 2024
@robot-clickhouse

robot-clickhouse commented Jan 31, 2024 •

Copy link
Copy Markdown
Member

This is an automated comment for commit b04aa50 with description of existing statuses. It's updated for the latest CI running

❌ Click here to open a full report in a separate page

Check nameDescriptionStatus
Stateless testsRuns stateless functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc❌ failure
Successful checks
Check nameDescriptionStatus
AST fuzzerRuns randomly generated queries to catch program errors. The build type is optionally given in parenthesis. If it fails, ask a maintainer for help✅ success
BuildsThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
ClickBenchRuns [ClickBench](https://github.com/ClickHouse/ClickBench/) with instant-attach table✅ success
Compatibility checkChecks that clickhouse binary runs on distributions with old libc versions. If it fails, ask a maintainer for help✅ success
Docker keeper imageThe check to build and optionally push the mentioned image to docker hub✅ success
Docker server imageThe check to build and optionally push the mentioned image to docker hub✅ success
Docs checkBuilds and tests the documentation✅ success
Fast testNormally this is the first check that is ran for a PR. It builds ClickHouse and runs most of stateless functional tests, omitting some. If it fails, further checks are not started until it is fixed. Look at the report to see which tests fail, then reproduce the failure locally as described here✅ success
Flaky testsChecks if new added or modified tests are flaky by running them repeatedly, in parallel, with more randomization. Functional tests are run 100 times with address sanitizer, and additional randomization of thread scheduling. Integration tests are run up to 10 times. If at least once a new test has failed, or was too long, this check will be red. We don't allow flaky tests, read the doc✅ success
Install packagesChecks that the built packages are installable in a clear environment✅ success
Integration testsThe integration tests report. In parenthesis the package type is given, and in square brackets are the optional part/total tests✅ success
Performance ComparisonMeasure changes in query performance. The performance test report is described in detail here. In square brackets are the optional part/total tests✅ success
Stateful testsRuns stateful functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc✅ success
Stress testRuns stateless functional tests concurrently from several clients to detect concurrency-related errors✅ success
Style checkRuns a set of checks to keep the code style clean. If some of tests failed, see the related log from the report✅ success
Unit testsRuns the unit tests for different release types✅ success
Upgrade checkRuns stress tests on server version from last release and then tries to upgrade it to the version from the PR. It checks if the new server can successfully startup without any errors, crashes or sanitizer asserts✅ success

@alexey-milovidov

Copy link
Copy Markdown
Member

Tests have failed.

@robot-clickhouse-ci-1 robot-clickhouse-ci-1 removed the submodule changed At least one submodule changed in this PR. label Mar 13, 2024
$CLICKHOUSE_CLIENT -nq "OPTIMIZE TABLE t1 PARTITION id '1' DEDUPLICATE BY p;" &
merge_pid=$!

sleep 0.1 && echo "Give the server a moment to start merge"

This comment was marked as outdated.

}
};

// TODO: remove this before merging

This comment was marked as outdated.

@zvonand

zvonand commented Jul 22, 2024

Copy link
Copy Markdown
Contributor

Only failing test is:

@zvonand

zvonand commented Sep 27, 2024

Copy link
Copy Markdown
Contributor

CI is green, and looks like the .sh test is stable -- it did not fail in the latest CI runs

(@alexey-milovidov)

@alexey-milovidov

Copy link
Copy Markdown
Member

Thank you! I'm very eager to merge... but I don't see who checked/read this code.
We need at least one glance through it. Maybe I will do it.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 30, 2024
Merged via the queue into ClickHouse:master with commit 8e355ed Sep 30, 2024
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 30, 2024
Enmk pushed a commit to Altinity/ClickHouse that referenced this pull request Feb 18, 2025
…top_the_world

Fix REPLACE PARTITION waiting for mutations/merges on unrelated partitions
Enmk added a commit to Altinity/ClickHouse that referenced this pull request Feb 27, 2025
…e_parition_waiting

24.8 Backport of ClickHouse#59138 - Fix `REPLACE PARTITION` waiting for mutations/merges on unrelated partitions
arthurpassos pushed a commit to Altinity/ClickHouse that referenced this pull request Apr 22, 2025
…top_the_world

Fix REPLACE PARTITION waiting for mutations/merges on unrelated partitions
Enmk added a commit to Altinity/ClickHouse that referenced this pull request May 15, 2025
…s_without_written_blocks

24.3 Backport of ClickHouse#77766 and ClickHouse#59138 - Stop merges without written blocks / Fix REPLACE PARTITION waiting for mutations/merges on unrelated partitions
alexey-milovidov added a commit that referenced this pull request Feb 15, 2026
The `merges_blocker` `ActionLock` in `replacePartitionFrom` was scoped
inside the `if/else` block and destroyed before the actual partition
replacement, allowing background mutations to start on parts in the
target partition. A mutation could then complete on an already-removed
part and add a new version back to the working set, "resurrecting" old
data alongside the new data from `REPLACE PARTITION`.

Move the `merges_blocker` to function scope so it stays alive until
`replacePartitionFrom` returns.

The bug was introduced in #59138.

Closes #73783

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

Copy link
Copy Markdown
Contributor

Hi @Enmk @alexey-milovidov — while reviewing this PR I found the following:

Happy to discuss — close anything that's wrong or already addressed.

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-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.

7 participants