Skip to content

Keep KeeperMap data when a Replicated database replica recovers - #122917

Merged
antonio2368 merged 4 commits into
ClickHouse:masterfrom
groeneai:keeper-map-replica-recovery-keeps-data
Sep 30, 2026
Merged

antonio2368 merged 4 commits into
ClickHouse:masterfrom
groeneai:keeper-map-replica-recovery-keeps-data

Conversation

@groeneai

@groeneai groeneai commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #111957
Related: #118011

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):

Fixed replica recovery of a Replicated database deleting the data of a KeeperMap table, for example after the server was killed during an ALTER of that table. A KeeperMap table that still exists in Keeper is now kept or renamed with its data, instead of being dropped and re-created empty. Closes #111957.

Description

On 26.8.10.6 and 26.3.33.24, a recovering replica of a Replicated database drops a KeeperMap table whose definition differs from the one in Keeper, and re-creates it. When it is the last table registered on its Keeper path (a single-replica database), the drop deletes the data and the table comes back empty: 50 rows to 0.

recoverLostReplica drops every diverged table that does not store data on disk, and a KeeperMap table keeps its data in Keeper. One real trigger: a server killed between the Keeper commit and the local metadata rename of ALTER TABLE ... MODIFY COMMENT is declared lost on restart. Measured on 26.8.10.6 (SIGKILL injected with strace): 0 of 50 rows left.

Recovery already avoids re-creating replicated MergeTree tables: it matches them by UUID and keeps or renames them. This PR matches KeeperMap tables by UUID too, on both sides (Keeper metadata and local tables). A UUID identifies a KeeperMap table: its columns and engine arguments cannot be altered, only its comments. A KeeperMap table whose UUID is not in Keeper was dropped there, and recovery still drops it: a copy moved to <database>_broken_replicated_tables would stay registered on its Keeper path and give its rows to any table created there later. The issue's <database>_broken_tables is Ordinary, which KeeperMap rejects.

A kept table keeps its local comments, like a replicated MergeTree table. Not changed here: S3Queue and AzureQueue (also keeping state in Keeper) and the MaterializedView inner table case (#111935).

05293_keeper_map_replica_recovery_keeps_data recovers three tables: altered, renamed and dropped in Keeper. Master drops all three and deletes their data. The fix keeps the 50 rows of the first two (40/40 randomized runs, also after the strace crash) and drops the third, so a table re-created on its Keeper path starts empty.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.10.1.1044-master (included in 26.10 and later)

`DatabaseReplicated::recoverLostReplica` drops every diverged local table
that does not store data on disk, then re-creates it from the metadata in
Keeper. `StorageKeeperMap` does not store data on disk, but its data lives
in Keeper: `StorageKeeperMap::drop` removes the table's registration and,
when no other table is registered on the same path (a single-replica
database, or the last registrant), the whole data tree. The table then
came back empty. One real trigger is a server killed between the Keeper
commit and the local metadata rename of `ALTER TABLE ... MODIFY COMMENT`:
the digest check declares the replica lost on restart. Measured on
26.8.10.6 with an strace-injected SIGKILL: 50 rows before, 0 after.

Recovery already has the right class for tables whose data is shared
Keeper state: replicated MergeTree tables are matched by UUID, kept or
renamed in place, and an unmatched one is moved to
`<database>_broken_replicated_tables`, an `Atomic` database that keeps the
UUID. Put `KeeperMap` tables into that class:
- `getTableUUIDIfReplicated` also returns the UUID of a `KeeperMap`
  definition (Keeper side);
- the local reconciliation loop matches a local `KeeperMap` table by UUID;
- the drop/move branch never drops a `KeeperMap` table (unless
  `database_replicated_drop_broken_tables` is set) and moves an unmatched
  one to `<database>_broken_replicated_tables`.
Both identification sides must agree: with only one of them, the moved
copy and the re-created table collide on the UUID and recovery fails on
every retry. A UUID identifies a `KeeperMap` table because only its
comments can be altered. Moving it to `<database>_broken_tables` instead
does not work: that database is `Ordinary` and `KeeperMap` refuses a table
without a UUID, so recovery would fail on every attempt.

A kept table keeps its local comments, as a replicated MergeTree table
keeps its local metadata.

The test forces recovery of three tables whose Keeper definitions were
altered, renamed and dropped, and checks that each keeps its 50 rows.

Closes ClickHouse#111957

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Sep 29, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 1 finding), plus my own review of the same head.

# Sev Finding Verdict Evidence / action
1 ⚠️ The altered-table arm checks only the row count, not that the kept table keeps its local comment DISAGREE The promise is the data: a diverged table is kept, renamed or moved with its rows. Keeping the local comment is a stated limitation, not a guarantee, so the test does not pin it. Each data line fails on master, on 26.8.10.6 and 26.3.33.24, and with any one of the three hunks reverted.
2 💡 getTableUUIDIfReplicated now also parses definitions that mention KeeperMap, under the same max_query_size bound as replicated MergeTree ones AGREE, not blocking #119220 removes that bound for the whole function; the two PRs touch adjacent lines, so the second to merge needs a trivial rebase.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-9:20260929-160200

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes: 05293_keeper_map_replica_recovery_keeps_data fails on every run on master, 26.8.10.6 and 26.3.33.24 (no randomization needed).
b Root cause explained? Yes: recovery drops a diverged table that does not store data on disk; KeeperMap keeps its data in Keeper, and dropping the last table registered on its path deletes it.
c Fix matches root cause? Yes: recovery now treats KeeperMap like replicated MergeTree (matched by UUID, never re-created), at the decision itself.
d Test intent preserved / new tests added? New test: tables altered, renamed and dropped in Keeper, then forced recovery.
e Both directions demonstrated? Fails on master and both releases, passes with the fix (50/50 randomized). A real strace-injected crash during ALTER: 0/50 rows on 26.8.10.6, 50/50 with the fix.
f Fix is general across code paths? Both identification sides and the drop/move branch; reverting any one of the three reddens the test. S3Queue/AzureQueue and #111935 are named, not changed.
g Fix generalizes across inputs (params/datatypes/wrappers)? Engine-level, type-independent; covers ALTER, RENAME and DROP divergence; database_replicated_drop_broken_tables = 1 still drops.
h Backward compatible? (maintainer-approved exception only) Yes: no setting, format or protocol change.
i Invariants and contracts preserved? Digest stays consistent (no re-recovery on the next restart, measured), UUIDs stay unique, KeeperMap registration is keyed by UUID and survives the rename or move.

Session id: cron:clickhouse-impl-slot-40:20260929-141300

@clickhouse-gh

clickhouse-gh Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [5f2c1b4]

Summary: ✅


AI Review

Summary

This PR changes DatabaseReplicated::recoverLostReplica to match KeeperMap tables by UUID, so recovery no longer drops and recreates a KeeperMap whose Keeper metadata only diverged by rename or comment, and it adds a focused stateless regression test for the altered/renamed/dropped cases. The data-loss fix itself looks right, but one metadata-replay gap remains, so I would still request a follow-up before approval.

Findings

⚠️ Majors

  • [src/Databases/DatabaseReplicated.cpp:1665-1684,1943] [dismissed by author -- https://github.com/Keep KeeperMap data when a Replicated database replica recovers #122917#issuecomment-5894533777] Matching a KeeperMap by UUID now skips the drop/recreate path for the ALTER ... MODIFY COMMENT case, but unlike ReplicatedMergeTree there is no storage-level metadata sync that later reapplies the missed comment change. Recovery therefore finishes successfully while the recovered replica keeps the stale local comment in SHOW CREATE TABLE / system.tables, even though Keeper metadata already contains the new one. Suggested fix: when a KeeperMap UUID matches and the metadata text still differs, detect the comment-only drift and rewrite the local metadata from Keeper (or run an internal comment-only alter) before skipping table creation.
Final Verdict
  • Status: ⚠️ Request changes
  • Apply the missed KeeperMap comment change during recovery, or make the intended comment-divergence contract explicit and handle it separately instead of silently declaring the replica caught up.

LLVM Coverage Report

Measured on commit 5f2c1b4.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.60% 79.60% +0.00%

Changed lines: Changed C/C++ lines covered: 22/22 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 29, 2026
Comment thread src/Databases/DatabaseReplicated.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-database-engines Database engine implementations (e.g., Atomic/Replicated) and database-level behavior. label Sep 29, 2026
…es its path

Moving an unmatched `KeeperMap` table to `<database>_broken_replicated_tables`
does not isolate it: `StorageKeeperMap::rename` keeps the table's registration
on its Keeper path. When another table is registered on the same path (the
table another replica re-created there after the DROP this replica missed, or
the new table of a `CREATE OR REPLACE` interrupted by a crash), the moved copy
stays a second live handle on that table's data. Writes through it change the
live table, and a later `DROP` of the live table keeps the data in Keeper, so a
table re-created on that path gets the old rows back. Measured on the previous
head: an `INSERT` into the moved copy showed up in the other table (50 -> 51
rows); after that table was dropped, 51 data nodes stayed in Keeper and a new
table on the path read 51 rows.

In that case the drop loses nothing: `StorageKeeperMap::drop` only removes
this table's registration and leaves the data to the other table. So recovery
now moves an unmatched `KeeperMap` table only when no other table is
registered on its path (dropping it would delete the data), and drops it
otherwise. `StorageKeeperMap::isPathUsedByAnotherTable` reads the
registrations.

The test gets an arm for this: `km_shared` is dropped in Keeper while
`km_twin` uses the same Keeper path. Recovery drops `km_shared`, and `km_twin`
keeps its 50 rows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@antonio2368 antonio2368 self-assigned this Sep 29, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 5f2c1b4d6 with master 5ac4352fc (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of b4c72860f; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 16 s compile time in total, 7 of them have a recent master baseline.

Job report

…table

A registration on a `KeeperMap` path is the table UUID followed by the server
UUID, so another replica of the same table also has one. Counting it made two
lagging replicas of a table dropped in Keeper both see "another table" and
both drop their copy; the last drop then deleted the data and no replica kept
a copy in `<database>_broken_replicated_tables`. Only a registration with a
different table UUID now counts: every replica of a dropped table keeps its
own copy, and a table another replica re-created on the path still makes the
drop keep the data.

The test gets a `km_replica` arm with a registration of the same UUID from
another server: the table is moved with its 50 rows (with the previous commit
it was dropped).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread src/Databases/DatabaseReplicated.cpp Resolved
A moved `KeeperMap` table keeps its registration on its Keeper path
(`StorageKeeperMap::rename` only changes the `StorageID`), and a new
`KeeperMap` table on an existing path shares the data registered there. So a
copy in `<database>_broken_replicated_tables` cannot be isolated from a table
created later on the same path, for example when the user re-creates the
table that was dropped: that table gets the old rows back, writes through the
copy change it, and its `DROP` keeps the data in Keeper. Checking the
registrations during recovery (the previous two commits) covers only a table
that already exists at that moment. Measured on the previous head: the table
re-created on the path after recovery read 50 rows, 51 after an `INSERT` into
the moved copy, and still 51 after it was dropped and created once more. On
master it reads 0.

A `KeeperMap` table whose UUID is not in Keeper was dropped there, so recovery
drops it too, as master does. `StorageKeeperMap::drop` deletes the data only
when no other table is registered on the path. Tables are still matched by
UUID, so one that was altered or renamed in Keeper keeps its data.

The test keeps the altered and renamed arms, and the dropped arm now checks
that a table re-created on the Keeper path of the dropped one starts empty.
The `km_replica`, `km_shared` and `km_twin` arms covered the removed branch.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@antonio2368
antonio2368 added this pull request to the merge queue Sep 30, 2026
Merged via the queue into ClickHouse:master with commit 12363f1 Sep 30, 2026
149 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 30, 2026
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-database-engines Database engine implementations (e.g., Atomic/Replicated) and database-level behavior. groeneai-origin-request PR origin: a maintainer pinged or directed groeneai pr-bugfix Pull request with bugfix, not backported by default 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.

Replica recovery in a Replicated database destroys KeeperMap data instead of preserving it

3 participants