Repository navigation
Route mixed settings/comment ALTER ON CLUSTER to every replica - #107142
alexey-milovidov merged 5 commits into
Conversation
DDLWorker::taskShouldBeExecutedOnLeader uses ASTAlterQuery::isSettingsAlter
and isCommentAlter, both of which require every command in the batch to be a
single type. A mixed batch such as
ALTER TABLE t ON CLUSTER 'c' MODIFY COMMENT 'x', MODIFY SETTING old_parts_lifetime = 123
matches neither, so the routing fell through to the default leader-only path.
StorageReplicatedMergeTree::alter applies settings and comments as local
metadata and never writes them to the replicated log (settings and comments
are not replicated), so leader-only routing leaves the followers diverged: the
leader has the new setting/comment, the followers do not. This is the same bug
class as issue ClickHouse#43843 (RESET SETTING ON CLUSTER), which was fixed by teaching
the routing predicate to recognise the shape.
Add ASTAlterQuery::isSettingsOrCommentAlter, the AST-side equivalent of
AlterCommands::areNonReplicatedAlterCommands: true when every command is
MODIFY SETTING, RESET SETTING, COMMENT COLUMN, MODIFY COMMENT, or a
comment-only MODIFY COLUMN. Wire it into the routing disjunction so mixed
batches execute on every replica. The comment-only MODIFY COLUMN recognition
matches the resolved AlterCommand::isCommentAlter, which the storage layer also
treats as metadata-only; it is intentionally stricter so any unexpected shape
just routes to all replicas (the safe default).
Also switch the immutable-disk gate in MergeTreeData::checkAlterIsPossible from
isSettingsAlter && isCommentAlter to areNonReplicatedAlterCommands, so the same
mixed metadata-only batches are accepted on immutable disks instead of throwing
SUPPORT_IS_DISABLED.
Add tests/integration/test_alter_settings_or_comment_on_cluster covering mixed
MODIFY COMMENT + MODIFY SETTING, MODIFY COMMENT + RESET SETTING, COMMENT COLUMN
+ MODIFY SETTING, comment-only MODIFY COLUMN (standalone and mixed), and a
negative MODIFY COLUMN-with-type-change case, asserting both replicas converge
after each ON CLUSTER ALTER. Verified both directions on a debug build: the
mixed test fails on master (follower and leader miss the setting) and passes
with this change.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-19:20260611-025200 |
|
cc @tavplubix @alesapin — could you review this? It fixes |
|
@groeneai Link the issue you are working on. Do it every time for every PR. |
|
Workflow [PR], commit [8aefff0] Summary: ✅
AI ReviewSummaryThis PR fixes Final VerdictStatus: ✅ Approve |
Address review feedback on the routing predicate. The previous helper was stricter than the storage-side classifier: it rejected positional modifiers (AFTER/FIRST) and per-column settings. As a result a comment-only MODIFY COLUMN x COMMENT 'c' AFTER y set command.column, so the helper returned false and DDLWorker routed the ALTER leader-only. But AlterCommand::isCommentAlter ignores after_column/first, so the storage layer took the local comment-only branch and never wrote a replicated log entry, reproducing the same metadata divergence this change fixes. Mirror AlterCommand::isCommentAlter's MODIFY_COLUMN branch exactly: a comment present and no type/codec/default/TTL change, regardless of placement or per-column settings. This keeps the DDLWorker routing predicate in step with the storage fast path for every shape the storage treats as non-replicated. Add an ON CLUSTER regression case for comment-only MODIFY COLUMN ... FIRST asserting both the column comment and the new column order converge on every replica. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (HEAD fe6f4de — review-feedback update)
Session id: cron:clickhouse-worker-slot-8:20260611-102900 |
|
@nikitamikhaylov Done — surfaced the issue visibly in the PR body: |
The DDLWorker router change (ASTAlterQuery::isSettingsOrCommentAlter) now sends a batch that mixes settings and comments, for example ALTER TABLE t ON CLUSTER 'c' MODIFY COMMENT 'x', MODIFY SETTING s = v, to every replica instead of the leader only. But StorageReplicatedMergeTree::alter only short-circuited the replicated log for a pure settings batch (commands.isSettingsAlter()) or a pure comment batch (commands.isCommentAlter()). A mixed batch matches neither, so it fell through to the replicated ALTER_METADATA path and did a versioned /metadata update. With the router sending that batch to all replicas, every replica then attempted the same versioned /metadata update: one succeeds, the others hit CANNOT_ASSIGN_ALTER, retry, and append additional no-op ALTER_METADATA entries after applying the local setting/comment. On a shard with several replicas a single ON CLUSTER metadata-only alter became several serial replicated alters and could keep the distributed DDL task retrying or timing out. Add a third fast path gated on commands.areNonReplicatedAlterCommands(), which is the resolved companion of the AST router predicate (all commands are settings or comments). It applies the change locally exactly like the two existing single-type paths combined: changeSettings for the live storage settings (and their side effects), then setInMemoryMetadata for the table and column comments, then alterTable, and returns before writing any replicated log entry. The DDLWorker router and the storage fast path now classify the same shapes as non-replicated. The regression test reads the ZooKeeper /metadata node version before and after the mixed ON CLUSTER alter and asserts it does not change, which proves no replicated log entry was written and no CANNOT_ASSIGN_ALTER churn occurs. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (HEAD 812f81d, storage fast-path fixup)
Session id: cron:clickhouse-worker-slot-6:20260611-120300 |
…ALTER The mixed settings/comment fast path in StorageReplicatedMergeTree::alter ran changeSettings and then committed future_metadata via setInMemoryMetadata. changeSettings is the sole writer of the setting-derived metadata fields escape_index_filenames (StorageInMemoryMetadata) and the per-index escape_filenames flag: when escape_index_filenames changes it recomputes both on its own snapshot and commits them. commands.apply never touches those fields, so future_metadata still carried the old values and the subsequent setInMemoryMetadata reverted the index filename policy. The visible effect: ALTER TABLE t ON CLUSTER ... MODIFY COMMENT 'x', MODIFY SETTING escape_index_filenames = 0 left escape_index_filenames = 0 in SHOW CREATE on every replica while the in-memory policy stayed escaped, so new parts were written with escaped index filenames (skp_idx_<escaped>.idx2) until a restart, diverging from the pure MODIFY SETTING path. Carry the setting-derived escape fields from the snapshot changeSettings just committed into future_metadata before the comment commit. changeSettings still runs first, so its sanityCheck rejects bad settings before anything is committed. The integration test writes a part on each replica after the mixed ON CLUSTER alter and asserts the on-disk skip-index filename is unescaped on both, which fails on the previous code (escaped name written) and passes now. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (HEAD 22407b5)
Session id: cron:clickhouse-worker-slot-1:20260611-134100 |
AlterCommand::isCommentAlter (and the matching DDL routing predicate ASTAlterQuery::isCommentOnlyModifyColumn) classified a positional MODIFY COLUMN x COMMENT 'c' FIRST/AFTER y, or one carrying per-column SETTINGS, as comment-only. But a placement modifier reorders the column and per-column settings change column metadata, both serialized into the replicated /columns (ColumnsDescription::operator== compares toString(false), which includes column order and settings, excluding only the comment). The comment-only classification made the storage take the local fast path: it applied the reorder/settings locally without writing a replicated /columns log entry, so a replica restart compared the locally reordered columns against the stale ZooKeeper /columns and threw INCOMPATIBLE_COLUMNS. This was a pre-existing replicated-storage bug, independent of ON CLUSTER. Tighten both predicates so only a pure comment change (no placement, no per-column settings) stays local; positional or settings MODIFY COLUMN now takes the full replicated ALTER_METADATA path, which versions /metadata and rewrites /columns in ZooKeeper, keeping every replica consistent across restarts. Extended the ON CLUSTER regression test: the positional case now asserts the ZooKeeper /metadata version moves (replicated path, not the local fast path) and that both replicas survive a restart with the new column order and no INCOMPATIBLE_COLUMNS. Related: ClickHouse#43843 Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
You are right, and thanks. Verified the chain in source: a positional This pointed in the opposite direction of the earlier relaxation, so I went to the root cause: tightened both predicates to be comment-only ONLY for a pure comment change.
Now a positional or per-column-settings Regression added to Verified both directions on a debug build (the new positional/restart assertion):
|
Pre-PR validation gate (HEAD 8aefff0)
Session id: cron:clickhouse-worker-slot-1:20260611-150400 |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 58/63 (92.06%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 14 line(s) · Uncovered code |
|
CI finished on 8aefff0. All checks green (144 success, 22 skipped, 0 failures). No PR-caused failures. Ready for review. (cron:our-pr-ci-monitor:20260611-210000) |
| if (!col_decl) | ||
| return false; | ||
|
|
||
| return col_decl->getComment() != nullptr |
There was a problem hiding this comment.
This code is correct but error-prone.
…ttings-simple-aggregate Resolve conflict in StorageReplicatedMergeTree::alter: master gained the areNonReplicatedAlterCommands() local-apply block for mixed settings/comment ALTERs (PR ClickHouse#107142). Keep it and place this PR's sortingKeyTypesChanged guard on verifySortingKey after that block, so settings/comment-only ALTERs return early and the suspicious-key check fires only when the sorting key types actually change.
…tabase
`DatabaseReplicated::shouldReplicateQuery` gated the replicated path for an
`ALTER` on a `KeeperMap` table on `ASTAlterQuery::isCommentAlter`, which
requires every command in the query to be the same single AST type. The
storage accepts a strictly larger set: `StorageKeeperMap` inherits the base
`alter`, so `IStorage::checkAlterIsPossible` accepts
`AlterCommand::isCommentAlter` per command, which also covers a comment-only
`MODIFY COLUMN`. Anything in that gap ran locally with no
`ZooKeeperMetadataTransaction` and still reached
`DatabaseReplicated::commitAlterTable`, whose first statement asserts a
transaction is present while the DDL worker is active.
Two user-reachable carriers, both against a `KeeperMap` table in a
`Replicated` database:
ALTER TABLE t MODIFY COMMENT 'a', COMMENT COLUMN c 'b';
ALTER TABLE t MODIFY COLUMN c COMMENT 'a';
The second is a single command, so the trigger is the type classification and
not the command count; a single-type comment `ALTER` was always routed
correctly.
`chassert` and `assertDigest` both compile away outside debug and sanitizer
builds, so on a release build the `ALTER` completes: local metadata and the
local digest advance while Keeper keeps the pre-`ALTER` `CREATE` statement and
digest. Other replicas therefore never receive the change, and at the next
start `local_digest != zk_digest` makes `DatabaseReplicatedWorker` declare the
replica lost (`check_consistency` defaults to true) and run
`recoverLostReplica`, which reverts the `ALTER`. Measured on a single-replica
database holding 50 rows, that recovery logged "Will DROP TABLE km, because it
does not store data on disk and can be safely dropped" and the table came back
empty with its Keeper data children going from 50 to 0, while a `MergeTree`
control table in the same database kept all 77 of its rows. That drop branch is
the pre-existing heuristic tracked in
ClickHouse#111957 and is unchanged here;
this commit removes one deterministic trigger of it.
The fix uses `ASTAlterQuery::isSettingsOrCommentAlter`, added in
ClickHouse#107142 for exactly this class and
already wired into `DDLWorker.cpp`, whose comment there names mixed batches
"that match neither single-type predicate". That routing site was fixed and
this one was missed. `isCommentAlter` implies `isSettingsOrCommentAlter`, so the
change can only widen the set of queries that take the replicated path: nothing
replicated before stops being replicated. The predicate also accepts
`MODIFY SETTING` and `RESET SETTING`, which `KeeperMap` has none of; those
still fail with an identical error code and message before and after, verified
by comparing both binaries, at the cost of one extra DDL log entry.
Found by the BuzzHouse fuzzer on the arm_asan_ubsan build:
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117591&sha=1520bd2704e60296068431f9f1144b2112ec03ff&name_0=PR&name_1=BuzzHouse%20%28arm_asan_ubsan%29
ClickHouse#117591
Co-Authored-By: Claude Opus 5 <[email protected]>
Related: #43843 (
ALTER TABLE ... ON CLUSTER ... RESET SETTINGnot applied on every replica) - same metadata-divergence class.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
ALTER TABLE ... ON CLUSTERbatches that mixMODIFY SETTING/RESET SETTINGwith a comment change (for exampleMODIFY COMMENT 'x', MODIFY SETTING old_parts_lifetime = 123) being applied only on the leader replica, leaving the other replicas diverged. Also fix a positional or per-column-SETTINGSMODIFY COLUMN ... COMMENTbeing misclassified as a local comment-only metadata change, which left the column reorder out of the replicated metadata and could causeINCOMPATIBLE_COLUMNSon replica restart.Description
DDLWorker::taskShouldBeExecutedOnLeaderrecognized only single-type settings or comment ALTERs (viaisSettingsAlter/isCommentAlter, which require every command in the batch to be the same type). A mixed batch matched neither, so it was routed leader-only. Since settings and comments are applied as local metadata and never written to the replicated log, leader-only routing left the followers without the new setting/comment.The fix adds
ASTAlterQuery::isSettingsOrCommentAlter(the AST-side equivalent ofAlterCommands::areNonReplicatedAlterCommands): it returns true when every command isMODIFY SETTING,RESET SETTING,COMMENT COLUMN,MODIFY COMMENT, or a comment-onlyMODIFY COLUMN, and wires it into the routing so such mixed batches run on every replica. It also switches the immutable-disk gate inMergeTreeData::checkAlterIsPossibletoareNonReplicatedAlterCommandsso the same mixed metadata-only batches are accepted there instead of throwingSUPPORT_IS_DISABLED.StorageReplicatedMergeTree::alteralso gets a matching fast path. It only short-circuited the replicated log for a pure settings batch or a pure comment batch; a mixed batch fell through to the versioned ALTER_METADATA path. With the router now sending mixed batches to every replica, each replica raced for the same/metadataversion, one winning and the rest retrying withCANNOT_ASSIGN_ALTER. A third path gated oncommands.areNonReplicatedAlterCommands()now applies these batches locally (settings then comments) and returns before any replicated log entry, keeping the storage classification in lockstep with the router predicate.This is the same bug class as #43843 (
RESET SETTING ON CLUSTERnot applied on every replica), fixed the same way.That storage fast path runs
changeSettingsbefore committing the comment metadata.changeSettingsis the sole writer of the setting-derived fieldsescape_index_filenames(and the per-indexescape_filenamesflag), whichcommands.applynever sets, so committing the comment metadata afterwards reverted the index filename policy until restart. The fix carries those fields from the snapshotchangeSettingsjust committed into the metadata before the comment commit, so a mixedMODIFY COMMENT 'x', MODIFY SETTING escape_index_filenames = 0keeps escaping off on every replica.Finally, a comment-only
MODIFY COLUMNis local-only only when it changes nothing but the comment. A positional modifier (FIRST/AFTER) reorders the column and per-columnSETTINGSchange column metadata, both of which are serialized into the replicated/columns(ColumnsDescription::operator==comparestoString(false), which includes column order and settings and excludes only the comment).AlterCommand::isCommentAlterpreviously ignored placement and per-column settings, soMODIFY COLUMN x COMMENT 'c' FIRSTtook the local comment fast path: the leader reordered its columns without writing the replicated/columns, and on restartcheckTableStructureAttemptcompared the locally reordered columns against the stale Keeper/columnsand threwINCOMPATIBLE_COLUMNS. This is a pre-existing replicated-storage bug, independent ofON CLUSTER. The fix tightens bothAlterCommand::isCommentAlterand the mirroringASTAlterQuery::isCommentOnlyModifyColumnto require no placement and no per-column settings; such shapes now take the full replicatedALTER_METADATApath (versioned/metadata+/columnsrewrite), while a pure comment-onlyMODIFY COLUMNstays local.Added
tests/integration/test_alter_settings_or_comment_on_cluster, a two-replicaReplicatedMergeTreeregression test covering mixed settings/comment batches, comment-onlyMODIFY COLUMN, a positionalMODIFY COLUMN ... COMMENT ... FIRSTshape (asserting the ZooKeeper/metadataversion moves and that both replicas survive a restart with the new order and noINCOMPATIBLE_COLUMNS), and a mixedMODIFY SETTING escape_index_filenames = 0batch (asserting the on-disk index filename, not justSHOW CREATE). It also asserts the ZooKeeper/metadataversion does not move for a pure settings/comment batch (no replicated log entry, noCANNOT_ASSIGN_ALTERchurn). Verified on a debug build that the cases fail on master and pass with this change.Version info
26.6.1.786