Repository navigation
Upgrade check: recover the read-only replicas that refuse KILL MUTATION - #121409
Conversation
The pre-upgrade kill loop added in ClickHouse#120978 reds with "N mutations could not be killed before the upgrade" on pull requests unrelated to mutations: 4 failures against 331 OK runs of the counterpart assertion since it landed on 2026-09-20, on 4 unrelated pull requests, in 3 of them the job's only red. In the three occurrences whose job logs I read, the kill threw Code: 242 TABLE_IS_READ_ONLY and the deliberate `||:` discarded it. The chain: 04070_truncate_all_tables_skip_views.sql runs TRUNCATE ALL TABLES FROM currentDatabase(), which in stress mode is the runner's shared test_N database, so it covers every table earlier tests left behind (--upgrade-check implies --fake-drop, so their DROPs were no-ops). On the previous release the unfiltered form still calls flushAndPrepareForShutdown() on every table in the database; StorageReplicatedMergeTree::flushAndPrepareForShutdown does restarting_thread.shutdown(part_of_full_shutdown = true), which sets need_stop, deactivates the task and calls setReadonly(), so the replica is read-only permanently with no thread left to re-activate it. The per-table truncate then throws, because truncate() begins with assertNotReadonly(), so the tables are left read-only and un-truncated with their mutations intact. Thirteen minutes later the kill loop reaches killMutation(), which also begins with assertNotReadonly(). ClickHouse#119425 fixed the product half on master, the `if (!truncate || !query.has_tables)` guard in InterpreterDropQuery, but it is not backported and the stress phase runs the previous release's binary and queries, so only this script can act. Reproduced on v26.8.10.6-lts, which find_previous_release picks today. Three changes, all in the kill step: - Each KILL MUTATION keeps its stderr in unkilled_mutation_errors.txt, and the FAIL line names it. Today the survivor is reported with an empty latest_fail_reason, so the artifact says nothing about why the kill did not happen, and attributing one occurrence cost a whole investigation. - If a mutation survives, SYSTEM RESTART REPLICA the read-only replicas that still hold one, then run the same kill pass again. Re-attaching the table builds it from its metadata and starts a replica that can remove the mutation's ZooKeeper node. Waiting instead cannot work for this cause: the restarting thread is deactivated, so no session-expiry check re-activates the replica. The no-argument SYSTEM RESTART REPLICAS is unusable here because restartReplicas() passes throw_on_error = false, so a table it fails to restart is left detached and its mutation silently disappears from system.mutations. - A restart that does not report success, that changes the number of rows in system.replicas, or that leaves its own replica read-only, writes its own FAIL row whatever the count says. Without this the change would be fail-open, which is worse than the red it removes, and it is fail-open in two distinct ways. doRestartReplica() detaches before rebuilding, so a failure after that point leaves the table detached; and because it re-creates the table with LoadingStrictnessLevel::ATTACH the storage always goes through ReplicatedMergeTreeAttachThread, whose startup() only waits for the first initialization attempt and never rethrows, so a restart whose first attempt fails returns success and leaves the replica read-only with a retry scheduled initialization_retry_period later. system.mutations reports only what an attached, initialized replica holds, because the mutation queue is filled by pullLogsToQueue(..., LOAD) on the same success path that later calls setNotReadonly(). Either state would otherwise drive the count to zero and report success for a mutation still in ZooKeeper. That load order is also why the per-replica check is race-free: is_readonly = 0 implies the mutations are already loaded. Validated against a real v26.8.10.6-lts server, since no harness executes upgrade_runner.sh. On one fixture of two bricked replicated tables the old block reports FAIL and the new one OK; on a clean server it does nothing, leaves no artifact, and never enters the recovery block. Both fail-open shapes were injected and both produce the new FAIL row while system.mutations has already reached zero: the restart_replica_fail_after_detach failpoint for the detached table, and a stopped Keeper for the replica that stays read-only, where the unguarded block reported "The stress phase left no unfinished mutation to the upgrade" for a mutation whose znode was still there and which executed as soon as the replica came back. shellcheck reports the same findings as before. Co-Authored-By: Claude Opus 5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Reviewed cold against the resulting code, then with an independent second model (engine: codex), then
Severity: ❌ blocker / One property bounds the change: the recovery block is entered only when the unfinished-mutation count Session id: cron:clickhouse-review-slot-8:20260921-171300 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20260921-162900 |
|
Workflow [PR], commit [f2c15f0] Summary: ✅ AI ReviewSummaryThis PR tightens the upgrade-check mutation cleanup path by recording Final Verdict✅ No actionable findings on the current head. |
The kill step compares `system.replicas` before and after the restart pass to catch a restart that detaches a table and leaves it detached: the count that decides the red reports only what an attached replica holds, so that state would report success for a mutation the upgraded server then resumes. The comparison also ran when the selection of read-only replicas came back empty and the pass restarted nothing. Such a run has none of those failures to answer for, yet an unreadable or changed count still wrote `Cannot restart a read-only replica ...`. Measured on 26.8.10.6, with the post-loop count timing out: the step reds a run that killed every mutation and reported `The stress phase left no unfinished mutation to the upgrade` in the same file, pointing at an errors artifact that was never created. Gate the comparison on the number of replicas the pass actually restarted. The per-table check inside the loop, and the same clause for a run that did restart something, are unchanged: with `restart_replica_fail_after_detach` enabled the step still FAILs while the mutation count reads zero.
The recovery pass compared `count() FROM system.replicas` before and after its restarts, which answers for every replicated table on the server rather than for the ones it restarted. An unrelated table attaching, or being dropped, inside that window made the step write `Cannot restart a read-only replica holding a mutation left unfinished by the stress phase` on a run where every restart it issued had succeeded and every mutation it targeted was killed. The stress phase leaves other replicated tables behind, so the window is not quiet. Measured on v26.8.10.6-lts, the release the job downloads, with two bricked ReplicatedMergeTree tables and one independent replica changing inside the window: `recovered_replica` returned 1 for both targets, no unfinished mutation and no read-only replica was left, and the FAIL row was written anyway - in both directions, 2 rows to 3 and 3 rows to 2. The per-table check inside the loop is what the pass is accountable for, and it is strictly stronger for those tables: a table left detached and a replica whose initialization failed both return 0 from `... WHERE database = ... AND table = ... AND NOT is_readonly`. The global count was blind exactly where that check is needed - with Keeper stopped the restart exits 0, the replica stays read-only, `system.mutations` reads 0 and the replica count does not move - so scoping the comparison to the restarted pairs would only restate the per-table check. Removing it also retires the `restarted_replicas` gate, which existed to keep the comparison from firing when the pass restarted nothing. Re-measured on the same rig after the change: the `restart_replica_fail_after_detach` failpoint still reds while the mutation count reads 0, Keeper stopped still reds with both tables named in the errors file, and a clean server still issues no restart at all. Co-Authored-By: Claude Opus 5 <[email protected]>
Related: #120978
Related: #119425
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Upgrade check: report why a pre-upgrade
KILL MUTATIONfailed, and recover the read-only replicas that refused it.Description
Upgrade check (amd_release)reds with1 mutations could not be killed before the upgrade:4 failures against 331 OK runs of the counterpart assertion since #120978 landed, on 4 unrelated
pull requests, the only red in 3.
https://github.com/ClickHouse/ClickHouse/actions/runs/35581391859/job/106309904192
Root cause.
04070_truncate_all_tables_skip_views.sqlrunsTRUNCATE ALL TABLES FROM {CLICKHOUSE_DATABASE:Identifier}, in stress mode the runner's sharedtest_Ndatabase, holdingearlier tests' leftovers. On the previous release the unfiltered form still calls
flushAndPrepareForShutdown()on each table, which forReplicatedMergeTreestops the restartingthread and sets the replica permanently read-only; the truncate then throws
TABLE_IS_READ_ONLY,leaving the mutations.
killMutationalso begins withassertNotReadonly(), so everyKILL MUTATIONthrowsCode: 242, which||:discards. #119425 fixed the product half onmaster, but the stress phase runs the previous release's binary and queries (today
v26.8.10.6-lts, where I reproduced this), so only this script can act. A26.9release removesthat trigger; the fault-injection route and the reporting half do not depend on it.
Change, in the kill step only: each
KILL MUTATIONkeeps its stderr inunkilled_mutation_errors.txt, now named in the FAIL line, since the survivor is reported with anempty
latest_fail_reason; if one survives,SYSTEM RESTART REPLICAthe read-only replicas stillholding it and kill again; and a restart that fails, or that leaves its replica read-only, writes
its own FAIL row whatever the count says, since
system.mutationsreports only what an attached,initialized replica holds.
Validation. No harness runs
upgrade_runner.sh, so I rebuilt the state on 26.8.10.6 and ran theblock. One fixture, both directions: FAIL with the old block, OK with the new. It still FAILs when
the count reaches zero: restart detaching the table (failpoint), or leaving the replica read-only
(Keeper stopped). On a clean server it does nothing;
shellcheckunchanged.Workflow [PR]
Sync PR [sync-upstream/pr/121409]
Version info
26.10.1.798-master(included in26.10and later)