Skip to content

Keep the caches of a part when it is moved to another disk - #125404

Open
RinChanNOWWW wants to merge 4 commits into
ClickHouse:masterfrom
RinChanNOWWW:clickhouse/move-cache
Open

RinChanNOWWW wants to merge 4 commits into
ClickHouse:masterfrom
RinChanNOWWW:clickhouse/move-cache

Conversation

@RinChanNOWWW

@RinChanNOWWW RinChanNOWWW commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

The keys of the mark cache, the index mark cache and the primary index cache contain the disk and the path of the part. So a part moved to another disk (ALTER TABLE ... MOVE PART|PARTITION ... TO DISK|VOLUME, a move by the storage policy, or a move TTL) started with cold caches, and its first reads loaded the marks and the primary index again from the new disk, which is costly when that disk is S3.

This PR:

  • Adds the MergeTree setting transfer_caches_on_disk_move (enabled by default). After the copy replaced the part in MergeTreePartsMover::swapClonedPart, the new IMergeTreeDataPart::moveCachesTo takes the entries of the part out of these caches and puts them back under the keys of the copy. It also hands over the primary index that the part keeps in memory (without use_primary_key_cache) when the copy has not loaded one (primary_key_lazy_load). Projection parts are handled recursively. This runs outside of the lock of the parts.
  • Adds take to ICachePolicy (LRU, SLRU, TTL) and CacheBase: it removes an entry and returns it with one lookup, and counts neither as a hit nor as a miss.
  • Fixes the columns cache: its entries are keyed by the UUID of the table and the name of the part, which a move keeps, so destroying the replaced part (or a copy whose move failed) removed the entries the active part still used. Only one of the parts with the same name owns the entries now (owns_columns_cache_entries, handed over in MergeTreeData::swapActivePart). This does not depend on the setting.
  • Fixes the cache keys of projection parts: getRelativePathOfActivePart returned <part>/<projection>/, while the marks are loaded from <part>/<projection>.proj/. So the marks of projection parts were never removed from the mark cache when the part was removed, and the marks prewarmed for them on insert and merge were never found.

Tests: 05366_move_part_to_disk_transfers_caches, 05367_columns_cache_move_part_to_disk, 05368_move_part_to_disk_primary_index_in_memory, 05369_projection_part_marks_removed_from_mark_cache, and gtests for take in gtest_lru_cache.cpp and gtest_slru_cache.cpp.

Changelog category (leave one):

  • Improvement

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

A part moved to another disk or volume keeps its entries in the mark cache, the index mark cache and the primary index cache, and its primary index in memory, so the first queries after the move do not load them again from the new disk. Controlled by the new MergeTree setting transfer_caches_on_disk_move, enabled by default. Also, the entries of the columns cache are no longer removed by a move, and the marks of projection parts are now removed from the mark cache when the part is removed.


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

@clickhouse-gh clickhouse-gh Bot added comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. external Author is not a member of the ClickHouse organization labels Oct 12, 2026
@george-larionov george-larionov added the can be tested Allows running workflows for external contributors label Oct 13, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [44a33dc]

Summary: ❌

job_name test_name status info comment
Build (arm_tidy, 3/4) FAIL
Build ClickHouse FAIL cidb
Build (amd_debug) DROPPED
Build (amd_asan_ubsan) DROPPED
Build (amd_tsan) DROPPED
Build (amd_msan) DROPPED
Build (amd_binary) DROPPED
Build (arm_asan_ubsan) DROPPED
Build (arm_binary) DROPPED
Build (amd_release) DROPPED
Build (arm_release) DROPPED

AI Review

Summary

The PR transfers part caches across disk moves and corrects projection cache paths. At commit 44a33dc1a18cc96eab8a11358bf7e98f0983db02, two gaps require changes: dynamic column marks are omitted from the transfer, and columns-cache cleanup can finish before readers of earlier physical copies stop writing.

Missing context / blind spots
  • ⚠️ No runnable local ClickHouse build or functional-test results were available for the reviewed head. The findings below follow concrete source paths; focused stateless test results would close the runtime validation gap.
Findings

⚠️ Majors

  • src/Storages/MergeTree/MergeTreeDataPartWide.cpp:405: Dynamic data-stream marks are not transferred. The new transfer uses enumerateStreams without a sample column or deserialization state. SerializationDynamic enumerates only its structure stream; SerializationObject omits dynamic paths and shared data. Warmed Dynamic and dynamic JSON data therefore reload their marks after a move to S3 despite the enabled setting. Enumerate the persisted substreams or relevant marks checksums instead.
  • src/Storages/MergeTree/IMergeTreeDataPart.cpp:1208: Columns-cache ownership does not protect the lifetime of surviving readers. A reader can retain original part A while moved copy B is merged and cleared. The reader can subsequently repopulate the removed part's entries through flushPendingColumnsCacheWrites: removePart does not invalidate its generation. Destruction of A now skips cleanup, so unreachable deserialized columns occupy cache capacity until eviction. Share cleanup ownership across physical copies, or invalidate/reject their writes once the final active copy is cleared.
Tests
  • ⚠️ Add a focused warm/read–move–read case for a Wide part with Dynamic values or dynamic JSON paths, asserting no marks reload after the move.
  • ⚠️ Hold an old reader across a move, merge and clear the moved copy, then finish the reader and verify that no entries remain for the removed part. The existing columns_cache_reader_pause_before_later_range failpoint provides a stateless test seam.
Final Verdict

Status: ⚠️ Request changes. Include dynamic data-stream marks in the transfer and preserve columns-cache cleanup across surviving copies, with focused regression coverage for both cases.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Oct 13, 2026
The keys of the mark cache, the index mark cache and the primary index cache
contain the disk and the path of the part, so a part moved to another disk
(`ALTER TABLE ... MOVE PART|PARTITION ... TO DISK|VOLUME`, a move by the storage
policy or a move TTL) started with cold caches, and its first reads loaded the
marks and the primary index again from the new disk.

- Add the MergeTree setting `transfer_caches_on_disk_move` (enabled by default).
  When it is enabled, `MergeTreePartsMover::swapClonedPart` calls the new
  `IMergeTreeDataPart::moveCachesTo` after the copy replaced the part: it takes
  the entries of the part out of the caches and puts them back under the keys
  of the copy, and hands over the primary index the part keeps in memory when
  the copy has none (`primary_key_lazy_load`). Projection parts are handled
  recursively.
- Add `take` to `ICachePolicy` and `CacheBase`: it removes an entry and returns
  it with one lookup, and counts neither as a hit nor as a miss.
- Fix the columns cache: its entries are keyed by the name of the part, which a
  move keeps, so destroying the replaced part (or a copy whose move failed)
  removed the entries the active part still used. Only one of the parts with
  the same name owns them now, see `owns_columns_cache_entries`.
- Fix the keys of the projection parts: `getRelativePathOfActivePart` returned
  `<part>/<projection>/` instead of `<part>/<projection>.proj/`, so the marks of
  projection parts were never removed from the mark cache when the part was
  removed, and the marks prewarmed for them on insert and merge were never used.
The key of the vector similarity index cache contains the disk and the path
of the part, like the mark cache and the primary index cache, so a part
moved to another disk (`ALTER TABLE ... MOVE PART|PARTITION ... TO
DISK|VOLUME`) started with a cold cache and its first vector search loaded
and deserialized the vector index granules again.

`IMergeTreeDataPart::moveCachesTo` now transfers them as well: it enumerates
the vector similarity indices of the table and the index granules of the
part (the same keys `MergeTreeIndexReader::read` inserts under) and moves
the entries to the keys of the copy with `take` + `set`.

Tested by 05370_vector_similarity_index_cache_move_part_to_disk.
@RinChanNOWWW
RinChanNOWWW force-pushed the clickhouse/move-cache branch from 5421ecd to e52f08b Compare October 16, 2026 08:49
The record was written for 26.10, but the setting has not shipped in any
release and the current version is 26.11, which the settings_changes_history
check requires.
The rebase brought a new SIEVE cache policy which does not implement the
`take` method this PR added to `ICachePolicy`, so every explicit
instantiation of `CacheBase` with the SIEVE policy failed to compile
(allocation of an abstract class, e.g. `LowCardinalityDictionaryCache` in
`ColumnsHashing.cpp`).

Implement it like `LRUCachePolicy` does: `take` removes the entry, returns
its value and keeps the hand consistent when it points at the removed
entry; `remove` delegates to `take`. Tested in `gtest_sieve_cache.cpp` the
same way as the LRU one.
}

void MergeTreeDataPartWide::removeMarksFromCache(MarkCache * mark_cache) const
void MergeTreeDataPartWide::forEachMarksFile(const std::function<void(const String & marks_file)> & func) const

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.

[P2] Include dynamic data streams in the marks transfer

moveCachesTo now relies on this enumeration, but this overload of enumerateStreams receives neither a column sample nor a deserialization state. SerializationDynamic::enumerateStreams consequently returns after the structure stream, and SerializationObject::enumerateStreams skips the dynamic paths and shared data. Thus, warming a Wide part containing Dynamic values or dynamic JSON paths and moving it to S3 still leaves its data-stream marks behind: the first read reloads them despite transfer_caches_on_disk_move = 1.

Please enumerate the persisted column substreams or the relevant marks entries in the part's checksums, so the transfer covers the actual data streams without additional I/O. A warm/read–move–read test with a dynamic column should assert that its marks are not loaded again.

/// Remove deserialized columns from cache
if (mayStoreColumnsInColumnsCache())
/// Remove deserialized columns from cache, unless another part with the same name owns them.
if (mayStoreColumnsInColumnsCache() && owns_columns_cache_entries)

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.

[P2] Preserve cleanup until readers of every moved copy finish

The latest copy can become uniquely owned while a reader still holds an earlier copy. For example: a reader holds part A; a move replaces it with B and disables A's ownership; a merge replaces B; background cleanup clears B's columns-cache entries. The reader of A can then call MergeTreeReaderWide::flushPendingColumnsCacheWrites and recreate those entries, because ColumnsCache::removePart does not advance the invalidation generation. When that reader releases A, this new condition skips its cleanup, leaving deserialized columns for a removed part resident until eviction and consuming cache capacity needed by live parts.

Please share the cleanup lifetime across the physical copies, or invalidate/reject writes from surviving copies when the last active copy is cleared. The existing columns_cache_reader_pause_before_later_range failpoint can cover a reader spanning the move and subsequent merge/cleanup.

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-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. external Author is not a member of the ClickHouse organization pr-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants