Repository navigation
Bypass index uncompressed cache when its size is zero - #104063
Conversation
`index_uncompressed_cache_size = 0` (the server default) is documented as disabling the cache, but `Context::getIndexUncompressedCache` returned a non-null `UncompressedCache` of capacity zero. `MergeTreeIndexReader` -> `MergeTreeReaderStream::init` then took the `CachedCompressedReadBuffer` path, where every skip-index block read acquired the global `CacheBase` mutex twice (lookup + insert), evicted itself immediately via `removeOverflow`, and unconditionally bumped `UncompressedCacheMisses` and `UncompressedCacheWeightLost`. On a query evaluating skip indexes with small `GRANULARITY` this materialized as hundreds of thousands of contended mutex acquisitions per query and tens of GB of phantom `UncompressedCacheWeightLost`, with zero `UncompressedCacheHits`. The original code created the cache object only when the size was non-zero. The `if (size)` guard at the call site was dropped to allow runtime resizing via `SYSTEM RELOAD CONFIG`. Restore the bypass by tracking whether the cache should be used in a separate flag, set when the cache is constructed and re-evaluated by `updateIndexUncompressedCacheConfiguration`. `getIndexUncompressedCache` returns nullptr when the flag is false; `MergeTreeReaderStream::init` already falls through to `CompressedReadBufferFromFile` on a null pointer. Runtime resize 0 -> N still works because the flag is refreshed after `setMaxSizeInBytes`. The data uncompressed cache is unaffected because `MergeTreeReadPoolBase` already gates `getUncompressedCache` on `pool_settings.use_uncompressed_cache`; the index path had no such guard. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
Workflow [PR], commit [f78db8e] Summary: ❌
AI ReviewSummaryThis PR makes ClickHouse Rules
Final Verdict
|
Spins up a server with `index_uncompressed_cache_size = 0`, runs a query that exercises a `bloom_filter` skip index, and asserts that `UncompressedCacheHits`, `UncompressedCacheMisses`, and `UncompressedCacheWeightLost` in `system.query_log` are all zero - the bypass path verified. Then enables the cache via `SYSTEM RELOAD CONFIG`, asserts misses appear on the first query and hits on a repeated one, and finally restores the original config (also via `SYSTEM RELOAD CONFIG`) and re-asserts that the profile events are zero again. This covers the runtime resize path in `Context::updateIndexUncompressedCacheConfiguration` in both directions. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
LLVM Coverage Report
Changed lines: 93.75% (15/16) | lost baseline coverage: 1 line(s) · Uncovered code |
Algunenano
left a comment
There was a problem hiding this comment.
Looks good for an artisanal PR 😄 . I left a small comment
| UncompressedCachePtr Context::getIndexUncompressedCache(bool only_if_enabled) const | ||
| { | ||
| SharedLockGuard lock(shared->mutex); | ||
| if (only_if_enabled && !shared->index_uncompressed_cache_enabled) |
There was a problem hiding this comment.
Why do we need index_uncompressed_cache_enabled? Could we do if (only_if_enabled && shared->index_uncompressed_cache->maxSizeInBytes() == 0) instead and avoid adding extra members?
There was a problem hiding this comment.
maxSizeInBytes() locks an std::mutex, I avoided it just in case, though I don't have a good mental model of what's important or not important for performance here.
(We could go further and make index_uncompressed_cache_enabled atomic so we can check it without locking shared->mutex. But that seemed too weird, and the caller of getIndexUncompressedCache does a few similar calls that lock shared->mutex the same way anyway.)
(EDIT: Or we could make maxSizeInBytes() thread safe by making the field atomic in all 3 ICachePolicy implementations.)
|
Test failures looked unrelated, ran EDIT: |
|
Hi — could this be a backport candidate for Affected code: Why this might apply: Fixes a performance regression affecting all users running the default config ( Caveat: I verified that the modified code exists in those branches, but not whether the bug is actually reachable there — a new caller in master may be what makes this newly observable. If older branches can't reach the affected path, please ignore this; otherwise consider |
Cherry pick #104063 to 26.4: Bypass index uncompressed cache when its size is zero
… size is zero (#104156) * Bypass index uncompressed cache when its size is zero `index_uncompressed_cache_size = 0` (the server default) is documented as disabling the cache, but `Context::getIndexUncompressedCache` returned a non-null `UncompressedCache` of capacity zero. `MergeTreeIndexReader` -> `MergeTreeReaderStream::init` then took the `CachedCompressedReadBuffer` path, where every skip-index block read acquired the global `CacheBase` mutex twice (lookup + insert), evicted itself immediately via `removeOverflow`, and unconditionally bumped `UncompressedCacheMisses` and `UncompressedCacheWeightLost`. On a query evaluating skip indexes with small `GRANULARITY` this materialized as hundreds of thousands of contended mutex acquisitions per query and tens of GB of phantom `UncompressedCacheWeightLost`, with zero `UncompressedCacheHits`. The original code created the cache object only when the size was non-zero. The `if (size)` guard at the call site was dropped to allow runtime resizing via `SYSTEM RELOAD CONFIG`. Restore the bypass by tracking whether the cache should be used in a separate flag, set when the cache is constructed and re-evaluated by `updateIndexUncompressedCacheConfiguration`. `getIndexUncompressedCache` returns nullptr when the flag is false; `MergeTreeReaderStream::init` already falls through to `CompressedReadBufferFromFile` on a null pointer. Runtime resize 0 -> N still works because the flag is refreshed after `setMaxSizeInBytes`. The data uncompressed cache is unaffected because `MergeTreeReadPoolBase` already gates `getUncompressedCache` on `pool_settings.use_uncompressed_cache`; the index path had no such guard. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * oops * Add integration test for index_uncompressed_cache_size = 0 Spins up a server with `index_uncompressed_cache_size = 0`, runs a query that exercises a `bloom_filter` skip index, and asserts that `UncompressedCacheHits`, `UncompressedCacheMisses`, and `UncompressedCacheWeightLost` in `system.query_log` are all zero - the bypass path verified. Then enables the cache via `SYSTEM RELOAD CONFIG`, asserts misses appear on the first query and hits on a repeated one, and finally restores the original config (also via `SYSTEM RELOAD CONFIG`) and re-asserts that the profile events are zero again. This covers the runtime resize path in `Context::updateIndexUncompressedCacheConfiguration` in both directions. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * Fix system.server_settings --------- Co-authored-by: Michael Kolupaev <[email protected]> Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
… size is zero (#104157) * Bypass index uncompressed cache when its size is zero `index_uncompressed_cache_size = 0` (the server default) is documented as disabling the cache, but `Context::getIndexUncompressedCache` returned a non-null `UncompressedCache` of capacity zero. `MergeTreeIndexReader` -> `MergeTreeReaderStream::init` then took the `CachedCompressedReadBuffer` path, where every skip-index block read acquired the global `CacheBase` mutex twice (lookup + insert), evicted itself immediately via `removeOverflow`, and unconditionally bumped `UncompressedCacheMisses` and `UncompressedCacheWeightLost`. On a query evaluating skip indexes with small `GRANULARITY` this materialized as hundreds of thousands of contended mutex acquisitions per query and tens of GB of phantom `UncompressedCacheWeightLost`, with zero `UncompressedCacheHits`. The original code created the cache object only when the size was non-zero. The `if (size)` guard at the call site was dropped to allow runtime resizing via `SYSTEM RELOAD CONFIG`. Restore the bypass by tracking whether the cache should be used in a separate flag, set when the cache is constructed and re-evaluated by `updateIndexUncompressedCacheConfiguration`. `getIndexUncompressedCache` returns nullptr when the flag is false; `MergeTreeReaderStream::init` already falls through to `CompressedReadBufferFromFile` on a null pointer. Runtime resize 0 -> N still works because the flag is refreshed after `setMaxSizeInBytes`. The data uncompressed cache is unaffected because `MergeTreeReadPoolBase` already gates `getUncompressedCache` on `pool_settings.use_uncompressed_cache`; the index path had no such guard. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * oops * Add integration test for index_uncompressed_cache_size = 0 Spins up a server with `index_uncompressed_cache_size = 0`, runs a query that exercises a `bloom_filter` skip index, and asserts that `UncompressedCacheHits`, `UncompressedCacheMisses`, and `UncompressedCacheWeightLost` in `system.query_log` are all zero - the bypass path verified. Then enables the cache via `SYSTEM RELOAD CONFIG`, asserts misses appear on the first query and hits on a repeated one, and finally restores the original config (also via `SYSTEM RELOAD CONFIG`) and re-asserts that the profile events are zero again. This covers the runtime resize path in `Context::updateIndexUncompressedCacheConfiguration` in both directions. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * Fix system.server_settings --------- Co-authored-by: Michael Kolupaev <[email protected]> Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
… size is zero (#104158) * Bypass index uncompressed cache when its size is zero `index_uncompressed_cache_size = 0` (the server default) is documented as disabling the cache, but `Context::getIndexUncompressedCache` returned a non-null `UncompressedCache` of capacity zero. `MergeTreeIndexReader` -> `MergeTreeReaderStream::init` then took the `CachedCompressedReadBuffer` path, where every skip-index block read acquired the global `CacheBase` mutex twice (lookup + insert), evicted itself immediately via `removeOverflow`, and unconditionally bumped `UncompressedCacheMisses` and `UncompressedCacheWeightLost`. On a query evaluating skip indexes with small `GRANULARITY` this materialized as hundreds of thousands of contended mutex acquisitions per query and tens of GB of phantom `UncompressedCacheWeightLost`, with zero `UncompressedCacheHits`. The original code created the cache object only when the size was non-zero. The `if (size)` guard at the call site was dropped to allow runtime resizing via `SYSTEM RELOAD CONFIG`. Restore the bypass by tracking whether the cache should be used in a separate flag, set when the cache is constructed and re-evaluated by `updateIndexUncompressedCacheConfiguration`. `getIndexUncompressedCache` returns nullptr when the flag is false; `MergeTreeReaderStream::init` already falls through to `CompressedReadBufferFromFile` on a null pointer. Runtime resize 0 -> N still works because the flag is refreshed after `setMaxSizeInBytes`. The data uncompressed cache is unaffected because `MergeTreeReadPoolBase` already gates `getUncompressedCache` on `pool_settings.use_uncompressed_cache`; the index path had no such guard. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * oops * Add integration test for index_uncompressed_cache_size = 0 Spins up a server with `index_uncompressed_cache_size = 0`, runs a query that exercises a `bloom_filter` skip index, and asserts that `UncompressedCacheHits`, `UncompressedCacheMisses`, and `UncompressedCacheWeightLost` in `system.query_log` are all zero - the bypass path verified. Then enables the cache via `SYSTEM RELOAD CONFIG`, asserts misses appear on the first query and hits on a repeated one, and finally restores the original config (also via `SYSTEM RELOAD CONFIG`) and re-asserts that the profile events are zero again. This covers the runtime resize path in `Context::updateIndexUncompressedCacheConfiguration` in both directions. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * Fix system.server_settings --------- Co-authored-by: Michael Kolupaev <[email protected]> Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Backport #104063 to 26.4: Bypass index uncompressed cache when its size is zero
…ze is zero (#104164) * Backport #104063 to 25.8: Bypass index uncompressed cache when its size is zero * Add reload_before/reload_after to with_replace_config Cherry-picked from b514957 (the cluster.py portion). The whitespace-only KEEPER_PUBLISH_CLIENT hunk was skipped because that code does not exist on this branch. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> --------- Co-authored-by: robot-clickhouse <[email protected]> Co-authored-by: Michael Kolupaev <[email protected]> Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
…ze is zero (#104166) * Backport #104063 to 26.3: Bypass index uncompressed cache when its size is zero * Add reload_before/reload_after to with_replace_config Cherry-picked from b514957 (the cluster.py portion). The whitespace-only KEEPER_PUBLISH_CLIENT hunk was skipped because that code does not exist on this branch. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> --------- Co-authored-by: robot-clickhouse <[email protected]> Co-authored-by: Michael Kolupaev <[email protected]> Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
…ze is zero (#104165) * Backport #104063 to 26.2: Bypass index uncompressed cache when its size is zero * Add reload_before/reload_after to with_replace_config Cherry-picked from b514957 (the cluster.py portion). The whitespace-only KEEPER_PUBLISH_CLIENT hunk was skipped because that code does not exist on this branch. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> --------- Co-authored-by: robot-clickhouse <[email protected]> Co-authored-by: Michael Kolupaev <[email protected]> Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
…hen its size is zero
…4063 Backport ClickHouse#104063 to 26.4: Bypass index uncompressed cache when its size is zero
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Avoid index uncompressed cache overhead when the cache is disabled (server setting
index_uncompressed_cache_size = 0, which is the default).Simple change.
(Artisanally written changelog entry and C++ code. Vibecoded unreviewed test, but I did check that it fails the expected way without this PR. The rest of this PR summary is from claude.)
(See the reverse-linked support escalation for customer report.)
With
index_uncompressed_cache_size = 0(the server default, advertised as "disabled"),Context::getIndexUncompressedCachestill returned a non-nullUncompressedCacheof capacity zero.MergeTreeIndexReaderthen took theCachedCompressedReadBufferpath inMergeTreeReaderStream::init, so every skip-index block read acquired the globalCacheBasemutex twice, evicted itself viaremoveOverflow, and unconditionally incrementedUncompressedCacheMissesandUncompressedCacheWeightLost. With small-GRANULARITYskip indexes (ngrambf_v1,bloom_filter, etc.) this can produce hundreds of thousands of contended mutex acquisitions per query and tens of GB of phantomUncompressedCacheWeightLost, with zeroUncompressedCacheHits.The original 2021 code only constructed the cache when the configured size was non-zero. The
if (size)guard at the call site inServer.cppwas later removed so the cache could be resized at runtime viaSYSTEM RELOAD CONFIG; the data uncompressed cache survived this becauseMergeTreeReadPoolBasealready gates it onpool_settings.use_uncompressed_cache, but the index-read path had no such guard.This change tracks whether the cache should actually be used in a separate flag, set in
setIndexUncompressedCacheand refreshed inupdateIndexUncompressedCacheConfiguration.getIndexUncompressedCachereturns nullptr when the flag is false;MergeTreeReaderStream::initalready falls through toCompressedReadBufferFromFileon a null pointer. Runtime resize from0 -> Nstill works.Version info
26.5.1.31426.4.2.4,26.3.10.62,26.2.18.10,25.8.24.4