Skip to content

File cache: make reserve granularity dynamic - #123255

Merged
kssenii merged 13 commits into
masterfrom
filecache-dynamic-reserve-granularity
Oct 6, 2026
Merged

kssenii merged 13 commits into
masterfrom
filecache-dynamic-reserve-granularity

Conversation

@kssenii

@kssenii kssenii commented Oct 1, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

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

Filesystem cache: make reserve granularity dynamic


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

Version info

  • Merged into: 26.10.1.1632-master (included in 26.10 and later)
  • Backported to: 26.9.12.7, 26.8.19.9

kssenii and others added 3 commits October 1, 2026 14:44
The reserve-ahead used by `FileSegment::reserve` now starts from 0 for every
reader/writer (the first reservation is exact), then equals the requested size
and doubles with each further reservation the same reader/writer makes, up to
`reserve_granularity` (default 4Mi), which now acts as the upper limit. It is
reset after a failed reservation. A one-off small read no longer charges a
whole granule against the cache, while long sequential downloads still reach
the limit quickly.

The state (`FileCacheReserveAhead`) lives in the caller: the cached read
buffer (kept across file segments and seeks), the write-through range writer,
`WriteBufferToFileSegment` and the background download loop. Callers that do
not pass it reserve exactly the requested size.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [05ba339]

Summary: ✅


AI Review

Summary

This PR makes filesystem-cache reserve_granularity dynamic by keeping per-caller reserve-ahead state, falling back to exact-size reservations when a larger reserve-ahead does not fit, and wiring that state through the read, write-through, background-download, and DiskCacheWriter paths. I do not see any remaining correctness, capacity-accounting, or metrics issues in the current code, and the PR’s CI run is green.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit 05ba339.

Metric Baseline Current Δ
Lines 88.30% 88.30% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 116/122 (95.08%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Oct 1, 2026
…ahead at twice the request

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@clickhouse-gh clickhouse-gh Bot added the comp-filesystem-cache Filesystem cache for remote/object storage, page cache, and read caching infrastructure. label Oct 1, 2026
@kssenii
kssenii requested a review from CheSema October 1, 2026 14:18
Comment thread src/Interpreters/FileCache/Metadata.cpp Resolved
kssenii and others added 3 commits October 1, 2026 17:12
@clickhouse-gh

clickhouse-gh Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 05ba339c7 with master 407e25ae0 (stripped binary size, per-symbol sizes and ThinLTO time; 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

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

Job report

…tries as failures

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
… it on jumpToPosition

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
struct FileCacheReserveAhead
{
/// Returns the size to reserve, at least `size_to_reserve`, and grows the reserve-ahead.
size_t getReserveSize(size_t size_to_reserve, size_t max_reserve_size, size_t limit)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read this paraments as:
limit - > max_reserve_step / max_step, do not reserve more that this right now, actually very close to max_reserve_size but this is about experiential growth
granularity -> next_reserve_step / next_step, It is not granularity. This is experiential growth.
max_reserve_size -> size_left_to_reserve / reserve_upper_bound, very close to limit but about the global limitation, we do not need more

may be some of them is good to rename in this way.

size_t getReserveSize(size_t size_to_reserve, size_t max_reserve_size, size_t limit)
{
const size_t result = std::max(size_to_reserve, std::min({granularity, limit, max_reserve_size}));
granularity = std::min(limit, granularity ? granularity * 2 : size_to_reserve * 2);

@CheSema CheSema Oct 5, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest

granularity = std::min(limit, std::max(granularity, size_to_reserve) * 2);

to exclude the case when the start is too small.

The scenario: other reader become the leader/downloader or the background downloader need to finish the work, it writes to the reserverd space and the last write need few more bytes, that triggers getReserveSize with short size_to_reserve, then the granularity is inited short and next calls do not reserve data ahead, they go with exact request.

In that way we start or/and adjust granularity at least up to size_to_reserve.

@CheSema CheSema self-assigned this Oct 5, 2026
kssenii and others added 2 commits October 5, 2026 16:27
`limit` -> `max_reserve_granularity`, `max_reserve_size` -> `segment_reserve_limit`,
to tell the cap on the reserve-ahead growth from the cap on the total reservation.

Related: #123255 (comment)

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
A short first request (e.g. a reader taking over a segment with leftover
reservation) started the reserve-ahead tiny, so the following larger requests
were reserved exactly for many calls until the doubling caught up.

Related: #123255 (comment)

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
`DiskCacheWriter::write` called `FileSegment::reserve` without a reserve-ahead
state, so the cache-miss fill path reserved exactly per window and took the
cache lock on every write. Keep one state per writer (it owns a single
append-only segment) and cap reserve-ahead at the end of the miss range.

Related: #123255 (comment)

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@kssenii
kssenii added this pull request to the merge queue Oct 6, 2026
@kssenii kssenii added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Oct 6, 2026
Merged via the queue into master with commit 7405dae Oct 6, 2026
149 checks passed
@kssenii
kssenii deleted the filecache-dynamic-reserve-granularity branch October 6, 2026 08:10
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 6, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 6, 2026
@alexey-milovidov alexey-milovidov mentioned this pull request Oct 6, 2026
1 task done
clickhouse-gh Bot added a commit that referenced this pull request Oct 6, 2026
Backport #123255 to 26.9: File cache: make reserve granularity dynamic
clickhouse-gh Bot added a commit that referenced this pull request Oct 6, 2026
Backport #123255 to 26.8: File cache: make reserve granularity dynamic
strtgbb pushed a commit to Altinity/ClickHouse that referenced this pull request Oct 6, 2026
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Oct 9, 2026
CheSema added a commit that referenced this pull request Oct 12, 2026
…o master

`SegmentSizes` released a reserved but unwritten temporary segment, which aborts on
`EMPTY => !queue_iterator` in builds with `chassert`. `EfficiencyWindow` relied on implicit
reserve-ahead, which #123255 made explicit.
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-filesystem-cache Filesystem cache for remote/object storage, page cache, and read caching infrastructure. pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-improvement Pull request with some product improvements pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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.

5 participants