Skip to content

Handle ReplicatedMT table metadata_version properly on BACKUP, RESTORE - #79412

Open
zvonand wants to merge 35 commits into
ClickHouse:masterfrom
zvonand:zvonand-metadata-attach
Open

zvonand wants to merge 35 commits into
ClickHouse:masterfrom
zvonand:zvonand-metadata-attach

Conversation

@zvonand

@zvonand zvonand commented Apr 22, 2025 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Save table metadata_version on BACKUP and restore it on RESTORE. Resolves #67457

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Details

Handle table metadata_version properly on BACKUP, RESTORE. Resolves #67457


Workflow [PR]
Sync PR [sync-upstream/pr/79412]

@zvonand
zvonand force-pushed the zvonand-metadata-attach branch from 876e478 to d6e75b9 Compare April 22, 2025 16:49
@zvonand zvonand changed the title Handle table metadata_version properly on BACKUP, RESTORE Handle RMT table metadata_version properly on BACKUP, RESTORE Apr 22, 2025
@GrigoryPervakov GrigoryPervakov added the can be tested Allows running workflows for external contributors label Apr 23, 2025
@clickhouse-gh

clickhouse-gh Bot commented Apr 23, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0e89a266]

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Apr 23, 2025
@zvonand
zvonand force-pushed the zvonand-metadata-attach branch 7 times, most recently from f08f59b to f6c5819 Compare April 25, 2025 16:46
@zvonand zvonand changed the title Handle RMT table metadata_version properly on BACKUP, RESTORE Handle ReplicatedMT table metadata_version properly on BACKUP, RESTORE Apr 28, 2025
Comment thread src/Backups/BackupEntriesCollector.cpp Resolved
Comment thread src/Backups/BackupEntriesCollector.cpp Resolved
Comment thread src/Backups/RestorerFromBackup.cpp Resolved

@ilejn ilejn left a comment

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.

LGTM

@zvonand

zvonand commented Apr 30, 2025

Copy link
Copy Markdown
Contributor Author

hmm, test_backup_restore_on_cluster is failing from time to time

It is also sometimes failing in master, but will double-check whether it is not related

@zvonand
zvonand force-pushed the zvonand-metadata-attach branch from eff7a8f to d8690fc Compare June 3, 2025 12:59
@clickhouse-gh

clickhouse-gh Bot commented Jul 10, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [fc8cfab]

Summary: ✅


AI Review

Summary

The PR saves the Keeper metadata version alongside each replicated table definition and restores it through atomic version bumps and replicated ALTER_METADATA entries. Reviewed the current diff at fc8cfab349d6748677350ffc3b252ba0322e3a9d and all prior discussion. One previously reported concurrency blocker remains: the initialization guard covers attach, but does not synchronize later replica recovery writes.

Findings

❌ Blockers

  • src/Storages/StorageReplicatedMergeTree.cpp:7790: The recovery-status read still has a concurrent writer after initialization. [dismissed by author -- https://github.com/Handle ReplicatedMT table metadata_version properly on BACKUP, RESTORE  #79412#discussion_r4474054548] The attach-path fix does not cover SYSTEM RESTORE REPLICA: restoreMetadataInZooKeeper assigns has_metadata_in_zookeeper = true at line 7733 while initialization_done is already true and is_readonly is still true. A concurrent RESTORE with a saved version can therefore reach this read while recovery writes the same plain std::optional<bool>. InterpreterSystemQuery::restoreReplica takes no table or alter lock, and the initialization flag does not order this later write. This is a C++ data race and undefined behavior. Make the status atomic, synchronize its readers and writers with the same lock, or use only atomic state in this wait. The author-resolved thread was left unchanged as instructed.
Missing context / blind spots
  • ⚠️ No local ClickHouse build or binary was available. The current-head Fast test report shows the focused version, access, pending-rename, large-metadata, and readonly tests passing; the fetched integration reports contain no test results. Runtime verification of concurrent SYSTEM RESTORE REPLICA and RESTORE under TSan would close the remaining concurrency evidence gap.
Final Verdict

Status: ❌ Block

Synchronize the recovery status across the new restore reader and all writers, including restoreMetadataInZooKeeper after initialization.

@clickhouse-gh clickhouse-gh Bot added the submodule changed At least one submodule changed in this PR. label Jul 14, 2025
@zvonand
zvonand force-pushed the zvonand-metadata-attach branch from 417302e to bdfbb08 Compare July 14, 2025 23:49
@zvonand
zvonand force-pushed the zvonand-metadata-attach branch 2 times, most recently from 37706cb to 1d0753a Compare July 29, 2025 16:15
@zvonand

zvonand commented Aug 1, 2025

Copy link
Copy Markdown
Contributor Author

Could someone pls take a look? I think I resolved all issues here, I see no related test fails

cc @GrigoryPervakov (tagging you as you were the first visitor here 🙃)

@zvonand
zvonand requested a review from ilejn August 1, 2025 09:03
@GrigoryPervakov GrigoryPervakov self-assigned this Aug 7, 2025
Comment thread src/Backups/RestorerFromBackup.cpp Resolved
Comment thread src/Backups/BackupEntriesCollector.cpp Resolved
RESTORE of a table defined as `AS file(...)` with a missing file failed with `CANNOT_STAT` in
05339_backup_table_function_table_without_source, because applying the saved metadata version cast
the storage with `DeferredTable::Load`, which runs the table function behind a
`StorageTableFunctionProxy`. Load only a lazily loaded table, as `BackupUtils` does.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=79412&sha=19235e5f717b673bd3b84fdbbe5a507b410ba136&name_0=PR&name_1=Fast%20test
ClickHouse#79412
alexey-milovidov pushed a commit to amosbird/ClickHouse that referenced this pull request Oct 26, 2026
`Stress test (amd_tsan)` on ClickHouse#79412 failed with `Cannot start clickhouse-server`:
the restart could not load `test_7.in_subquery_ttl__fuzz_2`, a server-side AST
fuzzer copy of the test's `TTL ... WHERE x IN (SELECT x FROM in_subquery_dist)`
table, because `in_subquery_source` behind the `Distributed` table had been
dropped (`Code: 60 ... Unknown table expression identifier`).

Stress runs the test in the shared database `test_7`, which is loaded again on
restart. The fuzzer copy is never dropped by the test and keeps
`in_subquery_dist` alive, while `DROP TABLE in_subquery_source` succeeds. An
ignored `DROP` (`ignore_drop_queries_probability = 0.2`) of `in_subquery_ttl`
and `in_subquery_dist` leaves the original table in the same state; the
fuzzer's own internal `DROP` replays mostly hide that path today, so pinning
only the fuzzer would expose it.

Set `ast_fuzzer_any_query = 0` and `ignore_drop_queries_probability = 0` at the
top of the file, as ClickHouse#124548 did for 05321. `SELECT`s are still fuzzed.

The leading `DROP TABLE IF EXISTS` lines now also drop dependents first, like
the cleanup at the end: an in-place retry of a run that failed before its
cleanup otherwise drops `in_subquery_source`, then stops on `in_subquery_dist`,
which the leftover `in_subquery_ttl` depends on, and leaves the same unloadable
table.

Validated on a master debug build with restarts under
`async_load_databases = false`: a surviving fuzzer copy, and the original
table with only the fuzzer pin, both fail with the CI error, and so does a
retry of the previous version of the file over a run cut before its cleanup
(it stops with `HAVE_DEPENDENT_OBJECTS` on `in_subquery_dist`). With the
change, 30 runs with the fuzzer on and every `DROP` ignored leave no tables, a
run cut before the cleanup restarts cleanly, a retry over it drops every table,
and 50/50 randomized runs pass.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=79412&sha=fabad86e8e451c8e584218575118d6b2c258191d&name_0=PR&name_1=Stress%20test%20%28amd_tsan%29
Failing job: https://github.com/ClickHouse/ClickHouse/actions/runs/39813182646/job/120003879638
PR: ClickHouse#79412

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@maxknv maxknv added the external Author is not a member of the ClickHouse organization label Nov 2, 2026
alexey-milovidov and others added 2 commits November 2, 2026 18:13
The private CI replaces `ReplicatedMergeTree` with `SharedMergeTree` in
stateless tests, and these tests check the metadata version handling of
`StorageReplicatedMergeTree` on `BACKUP` and `RESTORE`, so their results
differ there (CH Inc sync: ClickHouse/clickhouse-private#29473).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 The CH Inc sync failure was caused by the new stateless tests of this PR: the private CI replaces ReplicatedMergeTree with SharedMergeTree, and these tests check the metadata version handling of StorageReplicatedMergeTree, so their results differ there (05340, 05342-05346, 05448 failed in https://github.com/ClickHouse/clickhouse-private/pull/29473). Tagged them no-shared-merge-tree in 5030870.

@zvonand

zvonand commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@alexey-milovidov the CI is green and there are no conflicts

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-backup Backup/restore infrastructure and backup formats. external Author is not a member of the ClickHouse organization pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Source part metadata version X is newer then the table metadata Y after table restore

7 participants