Repository navigation
Conversation
…ge for final-mark-only ranges For a mark range that contains only the final mark (which has no rows), `end - begin` underflowed after clamping `end` to `num_marks_without_final - 1`, and the function returned a garbage row count instead of 0. Such ranges are produced by `RangesInPatchParts` for patch parts in `MergeOnKey` mode. Co-Authored-By: Claude Fable 5 <[email protected]>
With `MergeOnKey` patch parts, one patch part typically covers many original parts, and readers for different original parts (running in different threads) re-read and re-postprocess the same ranges of the patch part. Add `PatchRangesCache`: a query-level LRU cache of post-processed patch-part blocks, living in `MergeTreeReadPoolBase` (like `PatchJoinCache`) and used by `MergeTreePatchReaderMergeOnKey`. A request is served from the cache only when cached entries fully cover the requested range (zero-copy for an exact match, a cut for a containing entry, or a concatenation of adjacent entries); a partially covered range is re-read whole and stored zero-copy: entries reference slices of the whole read block, so both the miss path and the insertion path perform no extra copies. Stitching around partially covering pieces was measured to cost more in copies and page faults than re-reading. The cache size is limited by the new setting `apply_patch_parts_ranges_cache_max_bytes` (default 1 GiB, 0 disables it). New profile events: `PatchRangesCacheHits`, `PatchRangesCachePartialHits`, `PatchRangesCacheMisses`, `PatchRangesCacheEvictedBytes`. On a table with several original parts covering the same range of the sorting key, patch rows read drop by up to 89% and queries reading patched columns get up to 15-18% faster, with no regression when there is nothing to reuse. Co-Authored-By: Claude Fable 5 <[email protected]>
|
Workflow [PR], commit [4a6e200] Summary: ❌
AI ReviewSummaryThis PR adds a cache for ranges read from patch parts in PR Metadata
Findings
💡 Nits
Final VerdictChanges requested: the cache still is not query-scoped, and the new cache metrics currently over-promise what they measure. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 234/246 (95.12%) · Uncovered code |
Blocks in `PatchRangesCache` are retained for the lifetime of the reading pipeline and are accounted in the memory tracker of the query. With the default budget of 1 GiB it could take a large part of the memory budget of the query: `03100_lwu_31_merge_memory_usage` (a patch that covers all rows of the only original part, `max_memory_usage = '100M'`) started to fail with `MEMORY_LIMIT_EXCEEDED`, because the peak memory grew from 54 MiB to ~190 MiB. The cache is only an optimization, so limit its size additionally by a quarter of the memory limit of the query. Queries with large or absent memory limits are not affected, and for queries with small limits the cache degrades gracefully, because a miss is zero-copy: on the shape of the test the peak memory is 57 MiB now, and on a shape where the cache wins (a sparse patch, several read tasks requesting the same merged patch ranges) it still serves 69 of 118 requests from the cache with a limit of 100 MiB, and all of them without a limit. Note that the number of original parts is not a usable signal for enabling the cache: reuse comes from the sparsity of a patch part (many read tasks of one original part are mapped to the same merged range of the patch part), so a single original part with a sparse patch part still gets 105 hits per query and reads 71% fewer patch rows. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=115297&sha=309f912e5009f3922a0091090b7452c7fd01dda0&name_0=PR&name_1=Fast%20test Pull request: ClickHouse#115297 Co-Authored-By: Claude Opus 5 <[email protected]>
| { | ||
| UInt64 max_bytes = context->getSettingsRef()[Setting::apply_patch_parts_ranges_cache_max_bytes]; | ||
|
|
||
| if (auto memory_limit = getCurrentQueryHardLimit()) |
There was a problem hiding this comment.
createPatchRangesCache runs once per MergeTreeReadPoolBase, not once per query. Clamping each instance to query_limit / 4 therefore does not bound the query-wide cache footprint: a plan that reads several patched tables (or otherwise creates several read pools) can still spend the whole max_memory_usage on this optional cache alone and fail with MEMORY_LIMIT_EXCEEDED. Since the cache is only an optimization, its budget needs to be coordinated at query scope (shared limiter / shared cache), not applied independently per pool.
| M(PatchesReadUncompressedBytes, "Total number of uncompressed bytes read from patch parts", ValueType::Number) \ | ||
| M(PatchesJoinRowsAddedToHashTable, "Total number of rows added to hash tables when applying patch parts with Join mode", ValueType::Number) \ | ||
| M(PatchRangesCacheHits, "Number of range read requests to patch parts (MergeOnKey mode) fully served from the patch ranges cache", ValueType::Number) \ | ||
| M(PatchRangesCachePartialHits, "Number of range read requests to patch parts (MergeOnKey mode) partially served from the patch ranges cache", ValueType::Number) \ |
There was a problem hiding this comment.
PatchRangesCachePartialHits is not actually a request that was "partially served from the cache". PatchRangesCache::getOrRead explicitly discards partial coverage and re-reads the whole range; this flag only changes which counter is incremented. The counter is still useful, but the current description will mislead anyone reading system.events or dashboards into thinking some rows came from cache. Please rename or reword it to "partial coverage found in the cache" (or similar).
Four findings in `PatchRangesCache`: - `cppcoreguidelines-pro-type-member-init` for `LRUItem::begin_mark`, `Entry::range` and `Piece::covered`. `MarkRange` has a defaulted default constructor over two raw `size_t`, so such members are indeterminate until they are assigned. - `clang-analyzer-deadcode.DeadStores` for `has_cached_pieces` in the branch that re-reads the whole range on partial coverage: nothing reads the variable after that branch, because the code downstream uses `missing_ranges` and `had_partial_coverage`. Drop the store and move the variable into the scope of the lock, where it is used. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=115297&sha=71d2a5a215c1ed52d88ad6719da524d15fa7923c&name_0=PR&name_1=Build%20%28arm_tidy%29 Pull request: ClickHouse#115297 Co-Authored-By: Claude Opus 5 <[email protected]>
| M(PatchRangesCacheHits, "Number of range read requests to patch parts (MergeOnKey mode) fully served from the patch ranges cache", ValueType::Number) \ | ||
| M(PatchRangesCachePartialHits, "Number of range read requests to patch parts (MergeOnKey mode) partially served from the patch ranges cache", ValueType::Number) \ | ||
| M(PatchRangesCacheMisses, "Number of range read requests to patch parts (MergeOnKey mode) not served from the patch ranges cache", ValueType::Number) \ | ||
| M(PatchRangesCacheEvictedBytes, "Bytes evicted from the patch ranges cache", ValueType::Bytes) \ |
There was a problem hiding this comment.
PatchRangesCacheEvictedBytes is not measuring physical bytes reclaimed from the cache. Each gap entry stores a reference to the whole read_block, and insertPieces charges every one of those entries with the full read_block.allocatedBytes(), so evicting N entries from one read can report N times the actual freed memory (or report bytes even while sibling entries still keep the block alive). Please either track unique block ownership for this metric or rename/reword the event so system.events makes it clear that this is "charged bytes evicted", not real memory released.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Object file sizes17 object files changed (+182.21 KiB total), 1 added.
737 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units131 translation units recompiled, 1380 s compile time in total, 130 of them have a recent master baseline. |
|
Dear @CurtizJ, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message. |
|
A production data point for this, in case it helps prioritise it. On Cloud |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
TODO...
Workflow [PR]
Sync PR [sync-upstream/pr/115297]