Skip to content

Fix a replication queue stuck forever on a duplicated ALTER_METADATA entry - #117107

Merged
alexey-milovidov merged 1 commit into
masterfrom
fix-replicated-alter-sequence-duplicate-version
Aug 30, 2026
Merged

alexey-milovidov merged 1 commit into
masterfrom
fix-replicated-alter-sequence-duplicate-version

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 30, 2026 •

Copy link
Copy Markdown
Member

A replica created by cloning another one ends up with two ALTER_METADATA entries of the same alter_version in its replication queue:

  • the dummy one prepended by StorageReplicatedMergeTree::cloneMetadataIfNeeded, and
  • the one copied from the source replica's queue (the source replica has already advanced its /metadata_version but has not drained that entry yet).

Both entries finish the same alter, and ReplicatedMergeTreeAltersSequence::finishMetadataAlter used operator[], which inserts. So the second completion put the already erased alter back into the sequence as unfinished. Being the smallest version it stayed at the head forever, and every later metadata alter was postponed:

Cannot execute alter metadata queue-0000000003 with version 9 because another alter 7 must be executed before

The replication queue never drained, so SYSTEM SYNC REPLICA hung until receive_timeout and the replica never picked up the newer schema.

In the opposite interleaving, where the second entry is examined only after the first one has completed, canExecuteMetaAlter postponed that entry forever instead, because its version was already below the head - so the queue got stuck either way.

Both cases are fixed:

  • a repeated finishMetadataAlter is a no-op instead of resurrecting the alter, and
  • an ALTER_METADATA entry whose alter is already finished is allowed to execute, which StorageReplicatedMergeTree::executeMetadataAlter already handles as a no-op ("Attempt to update metadata of version ... to older version ...").

Added src/Storages/tests/gtest_replicated_merge_tree_alters_sequence.cpp, which replays the exact sequence of sequence operations taken from the failing CI log, in both interleavings.

This is the root cause of the long-standing flakiness of test_replicated_database/test.py::test_simple_alter_table[ReplicatedMergeTree], which is the test that creates a replica after a delay and then synchronizes it.

CI report:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=db05a1cdad9653e2ac26ae6c1ecd819a07ac77e5&name_0=MasterCI&name_1=Integration%20tests%20%28arm_binary%2C%20distributed%20plan%2C%204%2F4%29

Closes: #53498

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 replication queue of a ReplicatedMergeTree table getting stuck forever after a new replica was added by cloning an existing one. The queue could contain two ALTER_METADATA entries for the same ALTER, and finishing the second one made all subsequent ALTERs wait indefinitely, so SYSTEM SYNC REPLICA timed out and the new replica never picked up the newer schema.


Workflow [PR]
Sync PR [sync-upstream/pr/117107]

…` entry

A replica created by cloning another one ends up with two `ALTER_METADATA`
entries of the same `alter_version` in its queue: the dummy one prepended by
`StorageReplicatedMergeTree::cloneMetadataIfNeeded`, and the one copied from
the source replica's queue. Both of them finish the same alter, and
`ReplicatedMergeTreeAltersSequence::finishMetadataAlter` used `operator[]`, so
the second completion inserted the already erased alter back into the sequence
as unfinished. Being the smallest version, it stayed at the head forever and
every later metadata alter was postponed with

    Cannot execute alter metadata queue-0000000003 with version 9 because another alter 7 must be executed before

The replication queue never drained, so `SYSTEM SYNC REPLICA` hung until
`receive_timeout` and the replica never picked up the newer schema.

In the opposite interleaving, where the second entry is examined only after the
first one has completed, `canExecuteMetaAlter` postponed that entry forever
instead, because its version was already below the head - so the queue got
stuck either way. Both cases are fixed: a repeated completion is a no-op, and
an entry whose alter is already finished is allowed to execute, which
`StorageReplicatedMergeTree::executeMetadataAlter` handles as a no-op.

CI report:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=db05a1cdad9653e2ac26ae6c1ecd819a07ac77e5&name_0=MasterCI&name_1=Integration%20tests%20%28arm_binary%2C%20distributed%20plan%2C%204%2F4%29

Closes: #53498
@clickhouse-gh

clickhouse-gh Bot commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [706967b]

Summary: ✅


AI Review

Summary

This PR fixes a real sequencing bug in ReplicatedMergeTreeAltersSequence when a cloned replica ends up with duplicate ALTER_METADATA entries for the same version. I traced the changed invariant through queue insertion, scheduling, successful removal, mutation finalization, the clone path that creates the duplicate entry, and the replicated-database metadata gate. The change addresses both failure modes described in the PR: it stops finishMetadataAlter from resurrecting an already-finished alter, and it lets an already-finished duplicate metadata entry drain instead of being postponed forever. The added gtest exercises the relevant interleavings directly, and the linked PR CI is all green.

Final Verdict

Status: ✅ Approve

LLVM Coverage Report

Measured on commit 706967b.

Metric Baseline Current Δ
Lines 88.50% 88.50% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 80.80% 80.80% +0.00%

Changed lines: Changed C/C++ lines covered: 74/74 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Aug 30, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 706967b1e with master 6fbc63426 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of c81268c0f; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

276 object files changed (+40.55 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Processors/Formats/Impl/Parquet/Reader.cpp.o 644.67 KiB 684.22 KiB +39.55 KiB (+6.14%)

716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

376 translation units recompiled, 3123 s compile time in total, 375 of them have a recent master baseline.

Job report

@alexey-milovidov alexey-milovidov left a comment

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.

Ok.

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

Labels

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.

test_simple_alter_table: replica couldn't apply an ALTER for > 10 seconds

2 participants