Repository navigation
Conversation
restoreMetadataInZooKeeper raised the /metadata znode version by issuing max_parts_metadata_version SETs, and every restored replica runs it. The first replica creates /metadata and bumps it to N; restoring the next one (one by one or ON CLUSTER) added N again, leaving the shared version at 2N while the replicas hold N, so every later ALTER failed with CANNOT_ASSIGN_ALTER. Bump only while the version is below the target, each SET conditional on the version just read, so further and concurrent restores leave it at N. Co-Authored-By: Claude Opus 5.5 <[email protected]>
58c3a03 to
a9aeda6
Compare
|
Workflow [PR], commit [2ea89b7] Summary: ✅
AI ReviewSummaryReviewed head Missing context / blind spots
Findings❌ Blockers
Tests
Final Verdict❌ Block — complete safe version initialization for concurrent restores, add the focused regression case, and obtain passing results for the modified integration module. No new inline comments were posted: the remaining finding already has a thread, and the two findings specific to the reverted implementation no longer apply. |
| /// Every replica runs this and the first one to restore has already bumped the version, so bump only | ||
| /// up to the target: restoring the replicas one by one or ON CLUSTER must not add it once per replica. | ||
| /// Each SET is conditional on the version just read, so concurrent restores cannot overshoot either. | ||
| if (max_parts_metadata_version > 0) |
There was a problem hiding this comment.
The fix looks right, thanks. There is a similar issue when a lagging replica is restored first. Let's say replica1 is at version 1 and replica2 is at version 2. If replica1 is restored first, it will get its version set to 1, but when replica2 is restored it will get its version set to 2 since that is the max version it can see. Then an ALTER will fail just like in your example. Here's a reproducer:
def test_alter_after_restoring_lagging_replica_first(start_cluster):
zk = cluster.get_kazoo_client("zoo1")
table = "test_restored_lagging_first"
for node in [node_1, node_2]:
node.query(f"DROP TABLE IF EXISTS {table} SYNC")
for node in [node_1, node_2]:
node.query(
f"CREATE TABLE {table}(n UInt32) "
f"ENGINE = ReplicatedMergeTree('/clickhouse/tables/{table}/', '{node.name}') ORDER BY n"
)
node_1.query(f"ALTER TABLE {table} ADD COLUMN a1 UInt32", settings={"alter_sync": 2})
node_1.query(f"INSERT INTO {table} (n) VALUES (1)")
node_2.query(f"SYSTEM SYNC REPLICA {table}")
node_1.query(f"ALTER TABLE {table} ADD COLUMN a2 UInt32", settings={"alter_sync": 2})
# node_2 lags: its newest part is at metadata version 1, node_1 has one at version 2
node_2.query(f"SYSTEM STOP FETCHES {table}")
node_1.query(f"INSERT INTO {table} (n) VALUES (2)")
zk_rmr_with_retries(zk, f"/clickhouse/tables/{table}")
for node in [node_2, node_1]:
node.query(f"SYSTEM START FETCHES {table}")
node.query(f"SYSTEM RESTART REPLICA {table}")
node.query(f"SYSTEM RESTORE REPLICA {table}")
for node in [node_1, node_2]:
node.query(f"SYSTEM SYNC REPLICA {table}")
_, stat = zk.get(f"/clickhouse/tables/{table}/metadata")
for node in [node_1, node_2]:
version, _ = zk.get(f"/clickhouse/tables/{table}/replicas/{node.name}/metadata_version")
assert int(version) == stat.version
for i, node in enumerate([node_2, node_1]):
node.query(f"ALTER TABLE {table} ADD COLUMN b{i} UInt32", settings={"alter_sync": 2})
for node in [node_1, node_2]:
node.query(f"DROP TABLE IF EXISTS {table} SYNC")The suggested fix:
| if (max_parts_metadata_version > 0) | |
| if (is_first_replica && max_parts_metadata_version > 0) |
This will then rely on the clone of not-first replicas to set their versions from the original replica. A pre-existing issue is that in such a scenario as above some data may be lost, but I think that is out of the scope of this PR.
There was a problem hiding this comment.
Applied, and added your test. Thanks!
The replicas restored after the first are created as lost replicas and clone their metadata version from a restored one, so only the first should raise the shared /metadata version. Raising it on each of them doubled it when the replicas were restored one by one, and pushed it past the first replica's when a replica with newer parts was restored after a lagging one. Either way every later ALTER failed with CANNOT_ASSIGN_ALTER. This replaces the bounded bump of the previous commit, so the loop is master's again. The condition and the lagging-replica test come from George Larionov's review. Co-Authored-By: Claude Opus 5.5 <[email protected]>
| /// Only the first replica bumps it. The others are created as lost replicas and clone the metadata | ||
| /// version from a restored replica; bumping it again for each of them, or to their own newer parts, | ||
| /// would leave the shared version ahead of the replicas and every later ALTER would fail. | ||
| if (is_first_replica && max_parts_metadata_version > 0) |
There was a problem hiding this comment.
[P1] Finalize the first replica's version before allowing it to be cloned
Concurrent SYSTEM RESTORE REPLICA (including ON CLUSTER) can still leave a restored replica unable to execute ALTER. createTableIfNotExistsAttempt publishes the first replica with is_lost = 0 before this block, and cloneReplicaIfNeeded can select it before initialization finishes.
For example, with a target version of 2, pause the first restore after its first SET: the shared version is 1 and its replica's metadata_version is still 0. A second restore now skips this block, clones the first replica (both in-memory/source versions are 0, so cloneMetadataIfNeeded adds no ALTER_METADATA), and fixReplicaMetadataVersionIfNeeded persists the intermediate shared version 1. When the first restore finishes at 2, the second replica remains at 1, so its subsequent ALTER fails with CANNOT_ASSIGN_ALTER. There is no metadata log entry to catch it up, and the startup repair skips nonzero versions.
Please keep the first replica ineligible for cloning until both versions are finalized, or publish the initialized state atomically. Add coverage for concurrent restores that checks the versions and executes ALTER on each replica.
There was a problem hiding this comment.
Fixed in d8f9a1b: the first replica is now created with its final metadata version. Covered by test_alter_after_restoring_replicas_concurrently.
There was a problem hiding this comment.
Reverted in 2ea89b7: publishing the final version early broke recovery from an interrupted restore. Leaving the concurrent case to the maintainers.
The first replica to restore registered itself, not lost and at metadata version 0, before bumping the shared /metadata version. A replica restored at the same time (ON CLUSTER) could clone it in between: with both at version 0 it queued no ALTER_METADATA, and the startup repair copied the intermediate shared version, so it stayed behind once the bump finished and every ALTER on it failed. The first replica is now created with the version the bump reaches, so a replica cloning it at any point catches up to that version. The new pausable failpoint stops the bump after its first SET, so the test can restore the second replica in that window. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rsion" This reverts commit d8f9a1b. Publishing the first replica with the final version before the shared /metadata node reached it left a persistent mismatch when the restore was interrupted mid-bump, and a retried creation could no longer adopt its own replica. The simpler first-replica-only bump is kept; concurrent restores keep the narrow race it had. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@george-larionov After applying your suggested fix, the review bot flagged a race with concurrent restores ( Let me know what you think. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 35 s compile time in total, 7 of them have a recent master baseline. |
|
See #127656 |
|
Superseded by #127656, which covers this case too. Closing, thanks! |
Related: #96351
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
SYSTEM RESTORE REPLICAraising a table's metadata version on every restored replica instead of only the first. After restoring more than one replica of a table that had been altered, every laterALTERcould fail withCANNOT_ASSIGN_ALTER.Details
restoreMetadataInZooKeeper(added in #96351) raises the/metadataznode version to the parts' highestmetadata_versionby issuing that manySETs, and every restored replica ran it. The first replica creates/metadataand brings it to N. The next replica, restored one by one orON CLUSTER, added its own maximum again, so the shared version ended at 2N while the replicas held N, and every laterALTERfailed withMetadata on replica is not up to date with common metadata in Zookeeper(CANNOT_ASSIGN_ALTER). Restoring a lagging replica first failed the same way: a later replica with newer parts raised the shared version past it.Now only the first replica bumps the version. The others are created as lost replicas and clone the metadata version from a restored replica, as any new replica does.
Reproduced on 26.3.22.7 with two replicas: after
ADD COLUMNandDROP COLUMNthe version is 2; after deleting the table's Keeper path, restoring replica 1 leaves it at 2, restoring replica 2 makes it 4, and the nextALTERfails with code 517.test_alter_after_restoring_every_replicafollows these steps and checks that the version stays at 2 and that anALTERthen succeeds on both replicas.test_alter_after_restoring_lagging_replica_first, from review, restores the replica with older parts first and checks that both replicas end at the shared version and canALTER. The existing test in that file also restores both replicas, but only checks a TTL merge, which passes with the doubled version.We hit this in production on 26.3.22.7 when moving a two-replica cluster from ZooKeeper to Keeper: 19 tables with earlier
ALTERs refused everyALTERuntil each replica'smetadata_versionnode was set to the shared version and the table was detached and attached again.Workflow [PR]
Sync PR [sync-upstream/pr/125796]