Repository navigation
Fix wrong results and a server abort from stale caches after SYSTEM RESTORE REPLICA - #132791
Conversation
… parts `StorageReplicatedMergeTree::restoreMetadataInZooKeeper` moves every part to `detached/`, re-creates the table's Keeper nodes (which resets the block number counters) and attaches the active parts again, so they get new names numbered from 0 under the same table UUID, possibly the old names of other parts of the table. The query condition cache keys entries by (table UUID, part name, condition hash), so a part could read the verdicts of another part: matching marks were skipped (wrong results), and a part with more marks than the cached entry was read past the cached bitmap (libc++ hardening abort in `filterPartsByQueryConditionCache`). Reproduced on 26.10.5.57 and master. The per-part cache eviction on this path (`forcefullyMovePartToDetachedAndRemoveFromMemory` -> `IMergeTreeDataPart::clearCaches`) cannot reach the query condition cache, which can only be cleared as a whole. Clear it once, after all parts left the working set and before any part is attached or fetched under a new name. This covers every caller (SYSTEM RESTORE REPLICA, the `convert_to_replicated` flag, the forced restore of the attach thread) and non-first replicas, which fetch the parts under the first replica's new names. A table without UUID never has entries, so it does not clear the cache of the other tables (same guard as the `StorageMergeTree` attach clear of ClickHouse#132457). Residual: a query that was already reading the old parts when the restore started can still write entries for their old names after the clear. Closing that needs per-part keys, rejected as too heavy in ClickHouse#128677. See ClickHouse#132674. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 3 findings over 2 passes) plus my own review.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-10:20261118-202600 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-40:20261118-192800 |
|
Workflow [PR], commit [798f98b] Summary: ✅
AI ReviewSummaryThe PR clears caches in Final VerdictStatus: ✅ Approve |
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, 28 s compile time in total, 7 of them have a recent master baseline. |
…LICA renumbers parts The re-attached parts take the names, and so the file paths, of other parts. Clearing only the query condition cache left the mmap cache and the uncompressed cache serving the old files' data: with `local_filesystem_read_method = 'mmap'` or `use_uncompressed_cache = 1` the reproducer returns 0 instead of 2 after the restore (reported by the review bot on the mmap cache; the uncompressed cache behaves the same). Both are keyed by file path, and the per-part eviction on this path does not reach them. `Context::clearCaches` is what explicit-UUID CREATE and Replicated database recovery already call when part names and paths can be reused, and it covers every cache keyed by them, so use it here too. The hasUUID() guard is dropped: paths are reused in an Ordinary database as well. The test gets a third table for the mmap and uncompressed caches. It pins `min_bytes_for_full_part_storage = 0`, since a packed part does not go through the per-file mmap path and the mmap line would pass on the unfixed server, and `use_columns_cache = 0`, since the columns cache could serve these queries without reading the files and so hide a missing uncompressed cache clear. Co-Authored-By: Claude Opus 5.5 <[email protected]>
SYSTEM RESTORE REPLICASYSTEM RESTORE REPLICA
|
🕵️ @groeneai A question about the scope of the new
Only the query result cache is kept. So each I think this is acceptable:
Two cases where it hurts more:
Cheap follow-ups, not blockers: flush once per statement instead of once per table, and mention the cache flush in the |
addc006
|
Thanks for checking the scope. On the two follow-ups:
|
|
The docs follow-up is #134022: the |
Since ClickHouse#132791, restoring a replica clears the server-wide data caches, because the restored parts get new names that can be the former names and paths of other parts. Document this in the SYSTEM RESTORE REPLICA section: every table on the server starts with cold caches, every server with ON CLUSTER, and the query cache and the filesystem cache are kept. The cleared caches are listed with links to their SYSTEM CLEAR sections. The reference page docs/reference/statements/system.mdx is generated from the documentation in ParserSystemQuery.cpp, so the paragraph is added there. Related: ClickHouse#132791 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Context::clearCaches also clears the encryption headers cache and the UNIQUE KEY delete bitmap cache, so "other caches are kept" was wrong. List the encryption headers cache with the other cleared caches and name only the query cache and the filesystem cache as kept. With ON CLUSTER, the caches are cleared only on the servers where the restore actually runs. Related: ClickHouse#132791 Co-Authored-By: Claude Opus 5.5 <[email protected]>
… RESTORE REPLICA clears Context::clearCaches clears the UNIQUE KEY delete bitmap cache too; it has no SYSTEM CLEAR statement, so it is listed without a link. Related: ClickHouse#132791 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed wrong results and a possible server abort after
SYSTEM RESTORE REPLICA(for example after converting a table withATTACH TABLE ... AS REPLICATED): the restore attaches the parts again under the names and paths that other parts of the table had before, and the query condition cache, the mmap cache and the uncompressed cache returned those parts' verdicts or data.Description
On 26.10.5.57, by default this returns 0 instead of 2:
A renamed part with more granules than the cached one aborts the server (
vector<bool>::operator[] index out of boundsinfilterPartsByQueryConditionCache).The restore (
SYSTEM RESTORE REPLICA, theconvert_to_replicatedflag, the forced restore on attach) moves all parts todetached/, re-creates the Keeper nodes (resetting the block counters) and attaches the parts again: 'apple' becomesall_1_1_0, the old name of the 'pear' part, under the same table UUID. The query condition cache keys entries by table UUID, part name and condition and can only be cleared as a whole, so the per-part eviction here misses it. The mmap and uncompressed caches are keyed by file path and are not evicted per part either: withlocal_filesystem_read_method = 'mmap'oruse_uncompressed_cache = 1it also returns 0.StorageReplicatedMergeTree::restoreMetadataInZooKeepernow callsContext::clearCaches, as explicit-UUIDCREATEand Replicated database recovery do, after all parts left the working set and before any is attached or fetched again. With #128677 and #132457 this covers every event restarting the block numbers of a table; the open #128715 fixes the uncompressed and text index caches on a plainMergeTreeATTACH. Not covered: a query reading the old parts during the restore can write entries after the clear.The test fails on master (wrong result, then abort), its mmap and uncompressed cache queries also on this PR's previous head (40/40 randomized), and passes with the fix, 50/50 randomized.
Closes: #132674
Related: #132457
Related: #128677
Related: #128715
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/132791]
Version info
26.12.1.307-master(included in26.12and later)