Skip to content

Properly handle table_readonly setting in DDLWorker and replicated engines - #105109

Merged
alexey-milovidov merged 20 commits into
masterfrom
followup-fix-readonly
Jun 12, 2026
Merged

alexey-milovidov merged 20 commits into
masterfrom
followup-fix-readonly

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented May 16, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

OPTIMIZE TABLE ... ON CLUSTER and other DDL no longer hang when the target table has table_readonly = 1. The setting now throws a new dedicated error code TABLE_IS_PERMANENTLY_READ_ONLY, which DDLWorker treats as non-retriable (distinct from the transient TABLE_IS_READ_ONLY that arises during temporary ReplicatedMergeTree ZooKeeper disconnects). The table_readonly setting is also now explicitly rejected for ReplicatedMergeTree, both at creation and via ALTER MODIFY SETTING. Follow-up to #97652. Supersedes #100950.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.6.1.726

alesapin and others added 5 commits March 27, 2026 21:16
`TABLE_IS_READ_ONLY` was overloaded for both transient (`ReplicatedMergeTree`
in temporary read-only state due to ZooKeeper disconnect) and permanent
(`table_readonly` setting, static storage) cases. `DDLWorker` retries it
indefinitely, which is correct for the transient case but caused `OPTIMIZE
TABLE ON CLUSTER` to hang when `table_readonly = 1`.

Replace the kludge in `DDLWorker::tryExecuteQuery` (which classified by
resolving the storage and probing `supportsReplication`) with a clean
split: `TABLE_IS_READ_ONLY` stays retriable; `TABLE_IS_PERMANENTLY_READ_ONLY`
is non-retriable and is thrown by `assertNotReadonly` and `isStaticStorage`
sites in `StorageMergeTree`, `StorageReplicatedMergeTree`, and the four
DML interpreters (`ALTER`, `DELETE`, `UPDATE`, `DROP TRUNCATE`).

Updates the existing tests that exercised these throw sites
(`s3_plain`, `readonly` disk, `table_disk`, `table_readonly`) to expect
the new error code.

Addresses review feedback on #100950.
@clickhouse-gh

clickhouse-gh Bot commented May 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [28c7d21]

Summary: ✅


AI Review

Summary

This PR separates permanent read-only failures from transient replicated read-only failures by adding TABLE_IS_PERMANENTLY_READ_ONLY, making DDLWorker stop retrying those failures, and rejecting table_readonly = 1 for ReplicatedMergeTree. The main logic is in good shape, but one compatibility issue from an existing review thread still remains for metadata replay and restore paths.

Findings

❌ Blockers

  • [src/Storages/StorageReplicatedMergeTree.cpp:458] [dismissed by author -- https://github.com/Properly handle table_readonly setting in DDLWorker and replicated engines #105109#discussion_r3280798455] The constructor comment says old metadata can still load through FORCE_ATTACH / FORCE_RESTORE, but normal restore and non-initial DatabaseReplicated DDL replay use SECONDARY_CREATE, and SECONDARY_CREATE <= ATTACH. So a backup or replicated metadata entry created before this PR with SETTINGS table_readonly = 1 still throws NOT_IMPLEMENTED instead of loading. The author noted this is not a regression because an earlier revision of this PR already rejected SECONDARY_CREATE; relative to master, this PR introduces the validation, so it is still a backward-compatibility break for existing backups / replicated metadata.
    Suggested fix: reject only explicit user CREATE / ATTACH modes, or pass an explicit metadata-origin / restore flag and exempt replay paths.
Tests
  • ⚠️ Please add a focused regression test for loading existing ReplicatedMergeTree metadata with SETTINGS table_readonly = 1 through SECONDARY_CREATE (restore or replicated DDL replay), so this compatibility path cannot regress.
Final Verdict

Status: ❌ Block

Minimum required action: exempt metadata replay / restore from the new table_readonly constructor rejection, or prove that those paths cannot carry legacy table_readonly metadata and update the misleading comment accordingly.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label May 16, 2026
After switching the throws in these files to `TABLE_IS_PERMANENTLY_READ_ONLY`,
the `extern const int TABLE_IS_READ_ONLY;` declarations were left behind and
flagged by `Style check/cpp`:

> ErrorCodes::TABLE_IS_READ_ONLY is defined but not used in file ...

CI report: https://s3.amazonaws.com/clickhouse-test-reports/PRs/105109/dede4362a1e76008cd6b83bafe266fbb8dcfe2a1/result_pr.json (PR #105109)
…MergeTree`

The previous `mode < LoadingStrictnessLevel::ATTACH` check only rejected
`CREATE` and `SECONDARY_CREATE`, so user-initiated `ATTACH TABLE ... ENGINE =
ReplicatedMergeTree(...) SETTINGS table_readonly = 1` slipped through.

Switch to `mode <= LoadingStrictnessLevel::ATTACH` so the check also fires for
explicit `ATTACH` queries, while still letting `FORCE_ATTACH` and
`FORCE_RESTORE` (server startup, restore from backup) load tables whose
metadata may carry the setting from before this check.

Add a regression case to `04062_table_readonly_replicated`.
alexey-milovidov and others added 6 commits May 18, 2026 09:28
The `ATTACH TABLE` regression case in `04062_table_readonly_replicated`
used the `ATTACH TABLE name (cols) ENGINE = ...` form, which `Atomic`
rejects with `INCORRECT_QUERY` before reaching the storage layer, so the
test never exercised the new `mode <= LoadingStrictnessLevel::ATTACH`
guard in `StorageReplicatedMergeTree`. Switch to the UUID-form so the
query passes the `Atomic` check and reaches the storage guard, then
correctly throws `NOT_IMPLEMENTED`.

Also move the `table_readonly` guard in `StorageReplicatedMergeTree::alter`
out of the `commands.isSettingsAlter()` branch. Previously a mixed
`ALTER TABLE ... MODIFY COLUMN ..., MODIFY SETTING table_readonly = 1`
would skip the pure-settings branch and apply the unsupported setting
via the metadata path. The check now fires for any `MODIFY SETTING
table_readonly` regardless of the surrounding command shape, with a
regression case added to `04062_table_readonly_replicated`.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
When a `ReplicatedMergeTree` is attached on a static disk (e.g. `s3_plain`),
both `is_readonly` and `isStaticStorage` are true. The previous order
reported `TABLE_IS_READ_ONLY` (transient), which masks the permanent state
and trips up callers like `DDLWorker` that need to distinguish retriable
from non-retriable conditions.

Fixes `02980_s3_plain_DROP_TABLE_ReplicatedMergeTree` expecting
`TABLE_IS_PERMANENTLY_READ_ONLY` from `optimize table ... final` and
`system sync replica ...` on `s3_plain` storage.

Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=105109
PR: #105109
…per run

The `.sql` form pinned a hardcoded UUID for the `ATTACH TABLE ... UUID ...`
case, which collides under the flaky check when the same test runs more
than once on the same server (failure: `TABLE_ALREADY_EXISTS` instead of
`NOT_IMPLEMENTED`).

Generate the UUID via `generateUUIDv4` in the shell wrapper so every run
gets a distinct one, and verify each rejected statement by grepping the
client output for `NOT_IMPLEMENTED`.

Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=105109
PR: #105109
The previous test used `grep -F NOT_IMPLEMENTED -c` and expected `1` for
each failing query. With `--send_logs_level=warning` (the default), the
server-side log message of the rejection is also forwarded to the client,
so the error string appears twice in stderr, making `grep -c` return `2`.

Switch to `grep -F -q ... && echo 1 || echo 0`, which is the standard
robust pattern used elsewhere in `tests/queries/0_stateless/*.sh`.

Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=105109&sha=9498418a3be35d35b82edb6b21e516b58fe94e30&name_0=PR&name_1=Fast%20test
PR: #105109
/// Reject user-initiated `CREATE`/`ATTACH` queries with `table_readonly = 1` for
/// `ReplicatedMergeTree`, while still allowing `FORCE_ATTACH`/`FORCE_RESTORE` (server startup,
/// restore from backup) to load tables whose metadata may carry the setting from before this check.
if (mode <= LoadingStrictnessLevel::ATTACH && (*getSettings())[MergeTreeSetting::table_readonly])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This guard is broader than “user-initiated CREATE/ATTACH”: mode <= LoadingStrictnessLevel::ATTACH also includes SECONDARY_CREATE.

That hits at least two metadata-replay paths:

  • RESTORE table creation (InterpreterCreateQuery sets mode via getLoadingStrictnessLevel(create.attach, ..., is_secondary_query || is_restore_from_backup)), so restore queries are usually ATTACH/SECONDARY_CREATE, not FORCE_ATTACH/FORCE_RESTORE.
  • non-initial DatabaseReplicated DDL replay (SECONDARY_CREATE).

So backups / replicated metadata that already contain SETTINGS table_readonly = 1 can now fail to load with NOT_IMPLEMENTED, despite the comment saying restore-from-backup metadata should still load.

Can we narrow this check to true user DDL only, or thread an explicit metadata-origin / restore flag here and exempt replay paths?

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.

Good catch. Addressed in 3eb33448df5 by narrowing the condition to mode == LoadingStrictnessLevel::CREATE || mode == LoadingStrictnessLevel::ATTACH, which leaves both SECONDARY_CREATE (RESTORE and DatabaseReplicated DDL replay) and the FORCE_* modes (server startup) unaffected.

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.

Reverted in cd5bd8e9c41. On second look, pre-PR (d3f6b376eaf) already used mode < ATTACH, which itself rejected SECONDARY_CREATE, so the current mode <= ATTACH is not a regression for replay paths — it just additionally rejects explicit ATTACH. Leaving the bot thread open for @alexey-milovidov to decide.

alexey-milovidov and others added 3 commits May 25, 2026 01:35
The constructor guard introduced for `ReplicatedMergeTree` used
`mode <= LoadingStrictnessLevel::ATTACH`, which is too broad: it also
catches `SECONDARY_CREATE`, the mode used by `RESTORE` from backup and
non-initial `DatabaseReplicated` DDL replay. The comment claimed restore
metadata would still load, but the check actually rejected it.

Narrow the condition to the two strictly user-initiated modes (`CREATE`
and `ATTACH`) so that replay/restore paths can load tables whose
metadata may carry the setting from before this check.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

@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.

Good.

@alexey-milovidov alexey-milovidov self-assigned this May 30, 2026
The test verifies that the `table_readonly` setting is rejected with
`NOT_IMPLEMENTED` for `ReplicatedMergeTree` — the check lives in the
`StorageReplicatedMergeTree` constructor. When the engine is substituted
with `SharedMergeTree` (the CH Inc sync CI runs stateless tests with
`--replace-replicated-with-shared`), the first `CREATE` succeeds instead
of being rejected, and the rest of the test fails with
`TABLE_ALREADY_EXISTS`. The test is specific to `ReplicatedMergeTree`,
so exclude it from shared merge tree runs.

Co-Authored-By: Claude Fable 5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

The CH Inc sync failure was real: all three failing jobs run stateless tests with ReplicatedMergeTree replaced by SharedMergeTree, where the new test 04062_table_readonly_replicated breaks — the table_readonly rejection lives in the StorageReplicatedMergeTree constructor, so under the substituted engine the first CREATE succeeds and the rest of the test fails with TABLE_ALREADY_EXISTS. Tagged the test no-shared-merge-tree in 28c7d21, since it verifies ReplicatedMergeTree-specific behavior.

@clickhouse-gh

clickhouse-gh Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.60% 84.60% +0.00%
Functions 92.30% 92.30% +0.00%
Branches 77.20% 77.30% +0.10%

Changed lines: Changed C/C++ lines covered by tests: 52/56 (92.86%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 26 line(s) · Uncovered code

Full report · Diff report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jun 12, 2026
Merged via the queue into master with commit ea6f792 Jun 12, 2026
166 checks passed
@alexey-milovidov
alexey-milovidov deleted the followup-fix-readonly branch June 12, 2026 19:53
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 12, 2026
alexey-milovidov added a commit to matt-metivier/ClickHouse that referenced this pull request Jun 14, 2026
After ClickHouse#105109 merged, `StorageReplicatedMergeTree::alter` rejects
`ALTER TABLE ... MODIFY SETTING table_readonly` with `NOT_IMPLEMENTED`,
because the `table_readonly` setting is only supported for
`StorageMergeTree`.

`SystemLog::prepareTable` issued the readonly `ALTER` on the renamed old
table whenever `old_table->isMergeTree()` was true, which also holds for
`ReplicatedMergeTree`. A system log configured with a replicated engine
via a custom `<engine>` would therefore make the `ALTER` throw right
after the rename. The exception is caught in `flushImpl`, which logs the
error and still confirms the batch, so the current batch of log entries
is lost and an error is logged on every rotation.

Restrict the readonly marking to non-replicated `MergeTree` tables by
also checking `!supportsReplication()`. For replicated system log tables
the marking is simply skipped, which matches the engine-level guarantee
established by ClickHouse#105109. The rejection on the replicated path is already
covered by `04062_table_readonly_replicated.sh`.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
After ClickHouse#105109 merged, `StorageReplicatedMergeTree::alter` rejects
`ALTER TABLE ... MODIFY SETTING table_readonly` with `NOT_IMPLEMENTED`,
because the `table_readonly` setting is only supported for
`StorageMergeTree`.

`SystemLog::prepareTable` issued the readonly `ALTER` on the renamed old
table whenever `old_table->isMergeTree()` was true, which also holds for
`ReplicatedMergeTree`. A system log configured with a replicated engine
via a custom `<engine>` would therefore make the `ALTER` throw right
after the rename. The exception is caught in `flushImpl`, which logs the
error and still confirms the batch, so the current batch of log entries
is lost and an error is logged on every rotation.

Restrict the readonly marking to non-replicated `MergeTree` tables by
also checking `!supportsReplication()`. For replicated system log tables
the marking is simply skipped, which matches the engine-level guarantee
established by ClickHouse#105109. The rejection on the replicated path is already
covered by `04062_table_readonly_replicated.sh`.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements 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.

3 participants