Skip to content

Fix memory usage growth in fs cache - #106387

Merged
kssenii merged 11 commits into
masterfrom
proactive-fs-cache-invalidated-entries-cleanup
Jun 16, 2026
Merged

kssenii merged 11 commits into
masterfrom
proactive-fs-cache-invalidated-entries-cleanup

Conversation

@kssenii

@kssenii kssenii commented Jun 3, 2026 •

Copy link
Copy Markdown
Member

Before this PR invalidated entries removal (delayed cleanup) was made either during eviction or by keep_up_free_space background thread, which misses a case when eviction is not made since cache size limit is not reached. This PR adds unconditional cleanup to cover eviction-free case. This is a pre-existing issue and not related to any recent release.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fix memory usage growth in filesystem cache.

Version info

  • Merged into: 26.6.1.883 (included in 26.6 and later)
  • Backported to: 26.5.6.72

@clickhouse-gh

clickhouse-gh Bot commented Jun 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ff8117a]

Summary: ✅


AI Review

Summary

The PR adds proactive background cleanup for invalidated filesystem cache priority queue entries, exposes cleanup tuning settings and profile events, avoids registering soon-to-be-removed entries, and adds an integration regression test for delayed cleanup. I traced the final LRU, SLRU, split-cache, eviction, resize, startup, and shutdown paths against the prior discussion and did not find an unresolved correctness issue.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 3, 2026
@kssenii
kssenii force-pushed the proactive-fs-cache-invalidated-entries-cleanup branch from 11e1565 to 12aec51 Compare June 3, 2026 14:06
@kssenii
kssenii force-pushed the proactive-fs-cache-invalidated-entries-cleanup branch from 6e7db3e to 6ec0d63 Compare June 4, 2026 17:05
@kssenii kssenii added pr-must-backport Pull request should be backported intentionally. Use this label with great care! v26.2-must-backport labels Jun 5, 2026
if (entry_size)
cache_priority->state->sub(entry_size, 1);

cache_priority->addInvalidatedRef(entry, iterator);

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 guess we need to make sure this function doesn't throw exceptions? eg block memory exceeded

…invalidated-entries-cleanup

# Conflicts:
#	src/Interpreters/FileCache/IFileCachePriority.cpp
#	src/Interpreters/FileCache/IFileCachePriority.h
@kssenii
kssenii marked this pull request as draft June 9, 2026 14:14
…atedEntries

The invalidate contract must not throw: mark `IFileCachePriority::Iterator::invalidate`
and its `SLRU`/`Split` overrides `noexcept` (only `LRUIterator` was), and mark the
`addInvalidatedRef` helper `noexcept` too so the compiler enforces it.

Also guard the `push_front` in the `removeInvalidatedEntries` catch block with
`LockMemoryExceptionInThread`: it runs while handling a caught exception (so
`std::uncaught_exceptions()` no longer blocks the tracker), and a throw there would
lose the ref and mask the original exception.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@kssenii
kssenii force-pushed the proactive-fs-cache-invalidated-entries-cleanup branch from 51383b3 to a739872 Compare June 9, 2026 16:32
@kssenii
kssenii force-pushed the proactive-fs-cache-invalidated-entries-cleanup branch from a739872 to 8f0ed68 Compare June 9, 2026 17:41
@kssenii
kssenii marked this pull request as ready for review June 9, 2026 17:41
@clickhouse-gh

clickhouse-gh Bot commented Jun 10, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 38 queries analysed

This PR adds a background cleanup task that purges lazily-invalidated filesystem-cache priority-queue entries (plus new settings and profile events). That logic only runs during cache eviction and DROP CACHE, not on the warm-cache read path that the benchmark queries execute, so it can't plausibly make these SELECTs 20-48% faster. ClickBench Q22 and TPC-H Q20 also had very noisy baselines, and the TPC-H queries are intrinsically flaky, so all four flagged improvements have been downgraded to not-sure as likely baseline variance rather than a real effect of this change. No regressions were observed.

clickbench

⚠️ 2 inconclusive

Flagged queries (2 of 43)
Query Verdict Baseline med (ms) PR med (ms) Change q-value Hint
⚠️ 22 not_sure 334 262 -21.6% <0.0001 cache: PR only adds FS-cache invalidated-entry cleanup, off the warm-cache read path; -21% with very noisy base looks like vari
⚠️ 23 not_sure 297 202 -32.0% <0.0001 cache: Same FS-cache cleanup PR; delta is clean but the changed eviction/drop housekeeping isn't exercised by this SELECT

q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

⚠️ 2 inconclusive

Flagged queries (2 of 22)
Query Verdict Baseline med (ms) PR med (ms) Change q-value Hint
⚠️ 7 not_sure 113 72 -36.3% <0.0001 cache: Scale-1 data is fully cached; FS-cache cleanup housekeeping can't speed query execution. History is flaky too
⚠️ 20 not_sure 248 129 -48.0% <0.0001 cache: Measurements very noisy both sides and history flaky; cleanup PR doesn't touch this path - run-to-run variance

q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

Debug info
  • StressHouse run: d95b543d-7b82-4a88-b9be-a292b945e43a
  • MIRAI run: 7ec9b634-c982-43ca-96bb-80fe0abfb8f3
  • PR check IDs:
    • clickbench_256445_1781633806
    • clickbench_256546_1781633837
    • clickbench_256656_1781633935
    • tpch_adapted_1_official_256778_1781634125
    • tpch_adapted_1_official_256890_1781634255
    • tpch_adapted_1_official_256987_1781634286

kssenii and others added 2 commits June 12, 2026 15:09
`LRUIterator::invalidate` recorded every invalidated queue entry into
`invalidated_refs` for the background cleanup task to drain. However, that
cleanup task is only started in `startup`, which is called for `main_priority`
only. A `Query` priority (constructed by `FileCacheQueryLimit::QueryContext`)
never drains `invalidated_refs`, so when per-query recache evicts entries with
`enable_filesystem_query_cache_limit` enabled, `EvictionCandidates` calls
`invalidate` and one stale ref accumulated per eviction until query end.

Only register refs for the `Main` priority, which is the one actually drained.
The queue node itself is still removed opportunistically through `remove`.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@kssenii kssenii removed the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Jun 16, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.30% 85.10% -0.20%
Functions 92.30% 92.30% +0.00%
Branches 77.40% 77.30% -0.10%

Changed lines: Changed C/C++ lines covered by tests: 130/154 (84.42%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 10 line(s) · Uncovered code

Full report · Diff report

@kssenii
kssenii added this pull request to the merge queue Jun 16, 2026
Merged via the queue into master with commit 588845d Jun 16, 2026
166 checks passed
@kssenii
kssenii deleted the proactive-fs-cache-invalidated-entries-cleanup branch June 16, 2026 18:09
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 16, 2026
@kssenii

kssenii commented Jun 18, 2026 •

Copy link
Copy Markdown
Member Author

Added above labels (26.5 and long term support 26.3) for public backports only, backports to private branches I already did separately.

@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 Jun 18, 2026
robot-ch-test-poll added a commit that referenced this pull request Jun 22, 2026
Cherry pick #106387 to 26.5: Fix memory usage growth in fs cache
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jun 28, 2026
kssenii added a commit that referenced this pull request Jul 24, 2026
Backport #106387 to 26.5: Fix memory usage growth in fs cache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default 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 v26.3-must-backport v26.5-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants