Repository navigation
Conversation
A comment-only or settings-only `ALTER` on a `ReplicatedMergeTree` could silently drop a column added by a concurrent `ALTER ... ADD COLUMN`. `StorageReplicatedMergeTree::alter` (and `StorageMergeTree::alter`) built the committed metadata from `getInMemoryMetadataPtr(context, /*bypass=*/false)`, which returns the query-scoped shared snapshot (`enable_shared_storage_snapshot_in_query`). That snapshot can be pinned earlier in the same query - e.g. by the access check in `InterpreterAlterQuery::getRequiredAccess`, which runs before `lockForAlter` - to the pre-`ALTER` structure. The comment/settings branches then commit that stale snapshot via `setInMemoryMetadata` while holding only the alter lock, overwriting an `ALTER_METADATA` that `executeMetadataAlter` applied concurrently (also under `lockForAlter`) and dropping the newly added column, while `replica_path/metadata_version` stays equal to the ZooKeeper `/metadata` version. Read the commit base fresh (`bypass_metadata_cache=true`) under the held alter lock, matching what `InterpreterAlterQuery` already does for validation and what the mutation path does, so the alter lock's serialization against `executeMetadataAlter` is effective. Surfaced as the flaky integration test `test_replicated_database::test_replicated_table_structure_alter`. Closes: ClickHouse#110036 Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Workflow [PR], commit [4336567] Summary: ❌
AI ReviewSummaryThis PR moves the stale-metadata fix into Findings
Tests
Final Verdict
|
The previous commit fixed the comment/settings early-return branches, but `StorageReplicatedMergeTree::alter`'s full (structural) ALTER path reads `current_metadata` the same stale way inside its ZooKeeper retry loop, and still commits bundled comment/settings changes from it locally via `setInMemoryMetadata`/`alterTable` before the versioned `/metadata` write. When that snapshot is pinned before `lockForAlter` (by the access check), a mixed `ALTER` (e.g. `ADD COLUMN x, MODIFY COMMENT '...'`) racing a concurrently applied `ALTER_METADATA` silently drops the concurrently added column locally and fails the query with `CANNOT_ASSIGN_ALTER`; the persisted `metadata_version` stays equal to the ZooKeeper version, hiding the divergence. Reachable on a plain `ReplicatedMergeTree` in an Atomic database (`Replicated` databases reject mixed replicated/non-replicated ALTERs). Read `current_metadata` fresh (`bypass_metadata_cache=true`) here too. Adds regression test `test_mixed_alter_races_replicated_alter_metadata` covering this full-ALTER path (fails without this commit, passes with it). Co-Authored-By: Claude Opus 4.8 <[email protected]>
| /// the access check) to a pre-ALTER structure. Here it also backs the bundled comment/settings local | ||
| /// commit below (setInMemoryMetadata/alterTable) and the versioned zookeeper_path/metadata write, so a | ||
| /// stale base would drop a concurrently applied column and write an outdated metadata_version. | ||
| auto current_metadata = getInMemoryMetadataPtr(query_context, true); |
There was a problem hiding this comment.
current_metadata is fresh now, but the bundled local settings/comment commit below still overwrites setting-derived in-memory metadata. In the structural path, metadata_copy starts from *current_metadata, changeSettings(metadata_copy.settings_changes, ...) publishes the new escape_index_filenames value, and then setInMemoryMetadata(metadata_copy) at 6997 writes the old flag back because metadata_copy only copied settings_changes and the comment. A query like ALTER TABLE ... ADD COLUMN x UInt8, MODIFY SETTING escape_index_filenames = 0, MODIFY COMMENT 'c' on a plain ReplicatedMergeTree in an Atomic database will therefore keep using the old index filename policy until restart, even though the metadata file records the new setting. This branch needs the same refresh-from-committed-metadata handling as the areNonReplicatedAlterCommands() path above.
There was a problem hiding this comment.
Confirmed real, but pre-existing and orthogonal to this stale-snapshot fix (the bypass=true change is neutral to it): the bundled setInMemoryMetadata(metadata_copy) reverts the escape_index_filenames metadata field (and per-index escape_filenames) that changeSettings set. Tracking as a separate follow-up to verify and fix across the settings-only and on-disk sub-cases, rather than widening this PR.
Trim the explanatory comments to the load-bearing point (the query cache can be pinned before lockForAlter, so the commit base must be read fresh under the lock). No behavior change. Co-Authored-By: Claude Opus 4.8 <[email protected]>
| /// (e.g. by the access check that runs before lockForAlter) to a pre-ALTER structure. The | ||
| /// comment- and settings-only branches below commit this snapshot via setInMemoryMetadata under | ||
| /// the alter lock; a stale base would overwrite a concurrently applied ALTER and drop columns. | ||
| auto metadata_snapshot = getInMemoryMetadataPtr(local_context, true); |
There was a problem hiding this comment.
| auto metadata_snapshot = getInMemoryMetadataPtr(local_context, true); | |
| auto metadata_snapshot = getInMemoryMetadataPtr(local_context, /*bypass_metadata_cache*/ true); |
There was a problem hiding this comment.
Done — removed all three duplicated comments and inlined the argument as /*bypass_metadata_cache*/ true at every call site; the rationale now lives in the commit message and PR body. (commit 4623581)
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 8/8 (100.00%) · Uncovered code |
78515f4
…ta-snapshot-cache Fix flaky test_replicated_table_structure_alter (cherry picked from commit 78515f4)
Backport #111029 to 26.7: Fix flaky test_replicated_table_structure_alter
Picks up the fixing PRs for the three trunk-flaky integration reds on the previous head: ClickHouse#111029 (test_replicated_table_structure_alter, merged 07-24) and ClickHouse#111353 (test_check_all_tables, merged 07-22). Base was 6193 commits behind, so CI was also stale.
The AI review noted both regression tests would still pass if the synchronized `ALTER` under overlap returned an exception instead of succeeding: the comment-only helper ignored `race_out` errors and the mixed helper recorded `alter_error` without failing on it. Now a cycle that reached `SYNC_FOUND=1` raises on any `DB::Exception` / `Code:` output, so the tests also prove the "no error under overlap" behavior promised by the fix, closing the false-pass mode where the race would regress from silent clobber to always-throw. ClickHouse#111029
…ent-metadata-snapshot-cache Fix flaky test_replicated_table_structure_alter
|
backporting to fix CI |
Closes: #110036
Regression of : #107491
PRs fixes the same failure: #110145, #110030
Original pr of metadata pin: #79471
Problem
A comment-only or settings-only
ALTERon aReplicatedMergeTreecan silently drop a column added by a concurrentALTER ... ADD COLUMN. This surfaced as the flaky integration testtest_replicated_database::test_replicated_table_structure_alter, which regressed on 2026-07-10.Root cause — by example
Two
ALTERs race on the same replica of aReplicateddatabase tablermt (n, v):replica1:ALTER TABLE rmt ADD COLUMN m Int32— structural, replicated as anALTER_METADATAlog entry and applied on every replica byexecuteMetadataAlter(underlockForAlter);replica2:ALTER TABLE rmt COMMENT COLUMN v 'x'— comment-only, executed locally on each replica via the early-return branch inStorageReplicatedMergeTree::alter(holds only the alter lock, commits withsetInMemoryMetadata).The commit base in
alterwas read asgetInMemoryMetadataPtr(query_context, /*bypass=*/false)— the query-scoped snapshot cache (enable_shared_storage_snapshot_in_query, default on). That cache pins the firstbypass=falseread of the query and returns it for the rest of the query, regardless of any lock taken later.The pinning read moved before the alter lock in #107491: it added
isRowExistsLightweightDeleteMarker→getInMemoryMetadataPtr(context, /*bypass=*/false)intoInterpreterAlterQuery::getRequiredAccess, i.e. into the access check, which runs beforelockForAlter. Timeline onreplica2:replica_path/metadata_versionstays equal to the ZooKeeper/metadataversion throughout, soSYSTEM SYNC REPLICAcannot detect or repair the divergence.Before #107491 there was no
bypass=falseread earlier in the query, soalter's own read at t3 was the first one — it happened afterlockForAlterand therefore returned the current, under-lock structure.bypass=falsewas effectively equivalent to a fresh read, and the bug did not occur. #107491 did not changealter; it only moved the cache-pinning point ahead of the lock, which exposed the latent dependency of a commit path on the query cache.Solution
Clear the query-scoped metadata cache (
enable_shared_storage_snapshot_in_query) right after acquiringlockForAlterinInterpreterAlterQuery::runCommandSegments. The nextgetInMemoryMetadataPtr(context, /*bypass_metadata_cache=*/false)repopulates it from the metadata committed as of holding the lock, sovalidate,prepare,checkAlterIsPossibleand the storage'salterall read one fresh under-lock snapshot. Committed metadata cannot change whilelockForAlteris held, so a snapshot pinned before the lock can no longer become the commit base and drop a concurrently added column. The storage-side reads are left unchanged.Why not bypass the cache at the reads instead. The first attempt read the commit base with
bypass=truein the storagealtermethods. That fixed the apply side but leftInterpreterAlterQueryvalidating the commands against the still-pinned (stale) snapshot; under a concurrentDROP COLUMN, a command validated against the old structure was then applied to a fresh one missing the column, aborting withLOGICAL_ERROR: Cannot find column ... in ColumnsDescription. It also left the pin itself in place.Why
clear()and why therunCommandSegmentsread flips tobypass=false. The essential fix is the singleclear(): once the pre-lock pin is discarded, everybypass=falsereader below —validate(AlterCommands::validatereadsbypass=false),checkAlterIsPossible, and the storagealter's commit-base read — repopulates from the current under-lock metadata. Themetadata_snapshotread at the top of theAlterCommandsbranch is also switched frombypass=truetobypass=falseso thatprepareuses that same repopulated cache entry instead of a separate (content-identical, since metadata is frozen under the lock) fresh read;clear()alone would already prevent the column drop, but this keeps all consumers on one snapshot object. The storagealterreads themselves need no change.Resulting behavior. For the
#110036race (comment/settingsALTERvs a concurrentADD COLUMN) no error is raised and the added column is preserved. For a genuinely conflicting command — e.g. the target column was removed by a concurrentDROP COLUMN—validateand the apply now read the same fresh snapshot, sovalidaterejects it up front with a normal user error (NOT_FOUND_COLUMN_IN_BLOCK, "Wrong column. Cannot find column X to modify") rather than the internalLOGICAL_ERRORthe first attempt produced. No hot-path impact — this is the DDL path.A regression test
test_alter_comment_races_replicated_alter_metadata(wide table +SYSTEM STOP/START REPLICATION QUEUESto overlap the twoALTERs) fails without the fix (lost column) and passes with it. A second testtest_mixed_alter_races_replicated_alter_metadatacovers the full-ALTER path the same way.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a rare race where a comment-only or settings-only
ALTERof aReplicatedMergeTreetable could silently drop a column added by a concurrentALTER ... ADD COLUMN, leaving a replica on an outdated table structure.Version info
26.8.1.79(included in26.8and later)