Skip to content

Filesystem cache: avoid redundant opendir per key directory on load - #107414

Merged
kssenii merged 1 commit into
masterfrom
filecache-load-avoid-redundant-opendir
Jun 15, 2026
Merged

kssenii merged 1 commit into
masterfrom
filecache-load-avoid-redundant-opendir

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jun 13, 2026 •

Copy link
Copy Markdown
Member

On server startup, FileCache::loadMetadataForKey opened each cache key directory twice: once via fs::is_empty to test for emptiness, then again via fs::directory_iterator to scan the segment files. For caches with many keys, startup metadata loading is dominated by filesystem metadata latency, so the redundant opendir/readdir is pure overhead.

This change opens the directory only once and reuses the iterator: an empty directory is detected when the freshly-constructed iterator already equals the end iterator. The behavior is otherwise identical.

Related: #107415
Related: #107416

Changelog category (leave one):

  • Performance Improvement

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

Speed up filesystem cache loading on startup by avoiding a redundant directory open per cache key.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.6.1.805
  • Backported to: 26.5.3.31, 26.4.5.39

`FileCache::loadMetadataForKey` opened each key directory twice during
startup metadata loading: once via `fs::is_empty` to check for emptiness,
and once more via `fs::directory_iterator` to scan the segment files.

Open the directory only once and reuse the iterator: an empty directory
is detected when the freshly-constructed iterator already equals the
end iterator. This halves the number of `opendir`/`readdir` syscalls at
the key-directory level, which matters for caches with many keys where
startup loading is dominated by filesystem metadata latency.

Pure refactor with identical behavior; covered by the existing
`test_parallel_cache_loading_on_startup` integration test.

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

clickhouse-gh Bot commented Jun 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b0eb952]

Summary: ✅


AI Review

Summary

This PR changes FileCache::loadMetadataForKey so startup metadata loading opens each key directory once with std::filesystem::directory_iterator instead of probing emptiness first through fs::is_empty. The code-level behavior looks equivalent, and the changelog category and entry match the change, but the Performance Improvement contract still needs focused evidence.

Tests
  • ⚠️ [src/Interpreters/FileCache/FileCache.cpp:1910] The existing test_parallel_cache_loading_on_startup proves metadata reload still works, but it would also pass if this iterator reuse were reverted. Please provide a before/after measurement for startup cache loading, such as FilesystemCacheLoadMetadataMicroseconds or opendir/readdir syscall counts on a cache with many key directories.
Final Verdict

Status: ⚠️ Request changes

Minimum required action: add focused before/after performance evidence for the claimed filesystem cache startup loading improvement.

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Jun 13, 2026
@alexey-milovidov
alexey-milovidov requested a review from kssenii June 13, 2026 19:19

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good and small change. LGTM.

@clickhouse-gh

clickhouse-gh Bot commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

🔴 AI verdict: degradation — 1 query(s) regressed out of 38 analysed

This sync PR's only change replaces fs::is_empty with a reused directory iterator in FileCache::loadMetadataForKey, a startup-time optimization for loading file-cache metadata. That code runs once at server boot and is not on any query execution path, so none of the flagged per-query deltas can be attributed to it. The widespread same-direction shifts (10-25% in both directions), measured with just 6 source runs against 30 base runs, point to environment and run-to-run variance, so all flagged queries are downgraded. Q16 remains flagged only because its large delta tripped the automatic trust override; given the change is boot-time-only, treat it as environment noise too.

clickbench

🔴 1 regressed · ⚠️ 14 inconclusive

Flagged queries (15 of 43)
Query Verdict Baseline med (ms) PR med (ms) Change q-value Hint
🔴 16 regression 796 996 +25.2% <0.0001 cache: Large delta tripped the automatic trust override; note PR only touches startup file-cache load, likely environment
⚠️ 4 not_sure 264 232 -12.1% <0.0001 cache: PR only optimizes file-cache metadata loading at startup; off query path, delta is run-to-run variance
⚠️ 9 not_sure 373 411 +10.2% <0.0001 cache: PR touches startup file-cache metadata scan only; cannot affect this query's execution path
⚠️ 12 not_sure 325 358 +10.2% <0.0001 cache: Startup-only file-cache load change; unrelated to query execution, treat as variance
⚠️ 13 not_sure 497 587 +18.1% <0.0001 cache: PR is a boot-time directory-iterator tweak; off-path for query execution
⚠️ 14 not_sure 385 444 +15.3% <0.0001 cache: File-cache metadata-load optimization runs once at startup; not in this query's hot path
⚠️ 17 not_sure 564 673 +19.2% <0.0001 cache: PR changes only startup metadata loading; cannot plausibly regress this query
⚠️ 18 not_sure 1403 1668 +18.9% <0.0001 cache: Boot-time file-cache scan change; off query path, noisy with only 6 source runs
⚠️ 22 not_sure 334 270 -19.2% <0.0001 cache: Flagged improvement unrelated to a startup-only PR; high measurement noise, treat as variance
⚠️ 23 not_sure 297 215 -27.6% <0.0001 cache: Improvement off the PR's path (startup metadata load); run-to-run variance
⚠️ 31 not_sure 310 382 +23.2% <0.0001 cache: Startup file-cache load change cannot touch this query; delta is variance
⚠️ 32 not_sure 1367 1694 +23.9% <0.0001 cache: Off-path for a boot-time-only PR; treat as environment variance
⚠️ 33 not_sure 1488 1688 +13.4% <0.0001 cache: PR optimizes startup metadata loading only; unrelated to query execution
⚠️ 34 not_sure 1486 1722 +15.9% <0.0001 cache: Boot-time directory-iterator change; off query path, variance
⚠️ 35 not_sure 176 212 +20.7% <0.0001 cache: Startup-only file-cache change cannot affect this query; 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.

tpch_adapted_1_official

⚠️ 3 inconclusive

Flagged queries (3 of 22)
Query Verdict Baseline med (ms) PR med (ms) Change q-value Hint
⚠️ 7 not_sure 113 74 -34.5% <0.0001 cache: Flagged improvement unrelated to a startup-only file-cache PR; variance
⚠️ 8 not_sure 156 180 +15.8% <0.0001 cache: PR touches startup metadata loading only; off this query's execution path
⚠️ 20 not_sure 248 130 -47.6% <0.0001 cache: Noisy history and high variance; off-path for a startup-only PR

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: 2f712735-a208-44b7-bd0f-e6b85e0d435d
  • MIRAI run: 50e0a3cf-905d-4715-b39a-24f8f73d38dd
  • PR check IDs:
    • clickbench_50480_1781384197
    • clickbench_50494_1781384197
    • clickbench_50500_1781384197
    • tpch_adapted_1_official_50504_1781384197
    • tpch_adapted_1_official_50509_1781384197
    • tpch_adapted_1_official_50517_1781384197

@clickhouse-gh

clickhouse-gh Bot commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.70% 84.70% +0.00%
Functions 92.30% 92.30% +0.00%
Branches 77.30% 77.30% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 6/8 (75.00%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 4 line(s) · Uncovered code

Full report · Diff report

@alexey-milovidov

Copy link
Copy Markdown
Member Author

@groeneai, why did CI fail here?

@groeneai

Copy link
Copy Markdown
Collaborator

Runner OOM on the azure ASan shard, not this change.

All 4 reds on Stateless tests (arm_asan_ubsan, azure, parallel) share one signature, and all 4 passed on rerun (0/3 failed):

  • 00084_external_aggregation, 04312_reverse_key_obsolete_setting: Code 241, (total) memory limit exceeded ... current RSS: 53.69 GiB, maximum: 53.69 GiB. OvercommitTracker.
  • 02235_remote_fs_cache_stress: curl (56) Recv failure: Connection reset by peer / (52) Empty reply from server.
  • 02941_variant_type_1: Code 210 Connection reset by peer ... (localhost:9000).

current RSS == maximum == 53.69 GiB is the host server RSS cap, so the whole process was memory-starved: heavy queries got OvercommitTracker-killed (241) and the rest had their connections dropped (210 / curl 56/52). Every other check on b0eb952 is green.

This is the known azure ASan runner-OOM cascade. The same host-RSS-cap (53.69 GiB) signature hit 58 distinct PRs and 433 distinct tests on this shard over 2026-06-09..06-13, including 18 master rows. None of the 4 tests touch FileCache load, and a +6/-2 change to loadMetadataForKey (replacing the fs::is_empty opendir with reuse of the directory_iterator) cannot affect GROUP BY memory, INSERT, Variant, or remote-fs runtime. Safe to rerun.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

The single failing check Stateless tests (arm_asan_ubsan, azure, parallel) had 4 failing tests, all from one global memory-exhaustion event in that run, not from this change:

Test Error
00084_external_aggregation MEMORY_LIMIT_EXCEEDED, RSS 53.69 GiB
02235_remote_fs_cache_stress MEMORY_LIMIT_EXCEEDED, RSS 53.69 GiB
04312_reverse_key_obsolete_setting MEMORY_LIMIT_EXCEEDED, RSS 53.69 GiB
02941_variant_type_1 ran 151s in the same window

All four hit the same current RSS: 53.69 GiB, maximum: 53.69 GiB hard limit within a ~30s window (00:34:08–00:34:35), where the OvercommitTracker started killing queries across all concurrent tests. This is provably unrelated to this PR, which only changes startup directory scanning in loadMetadataForKey and has no effect on runtime query memory.

Master history (play.clickhouse.com, last 14 days) confirms these are recurring flakes, not a regression:

I re-ran the failed job.

@groeneai, the arm_asan_ubsan, azure, parallel runner appears to be over-committing global memory (RSS pinned at the 53.69 GiB hard limit, knocking out 4 unrelated tests at once); 00084_external_aggregation alone has hit this on 6 PRs in two weeks. Could you take a look and, if it's a runner concurrency/memory-budget issue, provide a fix in a separate PR?

@groeneai

Copy link
Copy Markdown
Collaborator

@alexey-milovidov It is a runner concurrency/memory-budget issue, and the fix is up in a separate PR: #107441

The arm_asan_ubsan, azure, parallel shard fell through to the default worker count in ci/jobs/functional_tests.py (cpu_count() * 0.6, ~19 on the ARM_MEDIUM runner) with no memory-aware reduction, unlike MSan and DatabaseReplicated (both * 0.4). It runs only under ASan (resident set roughly tripled) plus azurite, so ~19 co-scheduled heavy queries (2-3.5 GiB each) overrun the shared server cap (current RSS: 53.69 GiB, maximum: 53.69 GiB) and the OvercommitTracker stops queries across all concurrent tests at once, which is the cascade you saw.

CIDB over the last ~28 days excluding the 2026-06-09/06-10 Azure outage days: 187 OvercommitTracker/RSS-cap failures across 148 distinct tests and 59 PRs (00084_external_aggregation on 6 PRs).

The PR lowers that shard to cpu_count() * 0.4 (19 -> 12 workers on a 32-core runner), the same lever already accepted for MSan in e1705ed. No other shard is affected.

@kssenii kssenii self-assigned this Jun 14, 2026
@kssenii
kssenii added this pull request to the merge queue Jun 15, 2026
@kssenii kssenii added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Jun 15, 2026
Merged via the queue into master with commit aec9d00 Jun 15, 2026
327 of 328 checks passed
@kssenii
kssenii deleted the filecache-load-avoid-redundant-opendir branch June 15, 2026 10:11
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 15, 2026
robot-clickhouse-ci-1 added a commit that referenced this pull request Jun 15, 2026
Cherry pick #107414 to 26.4: Filesystem cache: avoid redundant opendir per key directory on load
robot-clickhouse added a commit that referenced this pull request Jun 15, 2026
robot-clickhouse-ci-1 added a commit that referenced this pull request Jun 15, 2026
Cherry pick #107414 to 26.5: Filesystem cache: avoid redundant opendir per key directory on load
robot-clickhouse added a commit that referenced this pull request Jun 15, 2026
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Jun 15, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jun 15, 2026
alexey-milovidov added a commit that referenced this pull request Jun 15, 2026
Backport #107414 to 26.4: Filesystem cache: avoid redundant opendir per key directory on load
alexey-milovidov added a commit that referenced this pull request Jun 15, 2026
Backport #107414 to 26.5: Filesystem cache: avoid redundant opendir per key directory on load
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…7414

Backport ClickHouse#107414 to 26.4: Filesystem cache: avoid redundant opendir per key directory on load
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-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-performance Pull request with some performance improvements 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