Repository navigation
Fix SLRU free-space-keeping crash when probationary queue is empty - #104313
Conversation
`SLRUFileCachePriority::collectEvictionInfo` had a `chassert` requiring that `evict_size_from_probationary || evict_elements_from_probationary` in the `is_total_space_cleanup` branch. This is wrong when the probationary queue is empty but the protected queue still has entries: the function must still be able to evict from the protected queue. This is reachable in production via the `keep_free_space_size_ratio` / `keep_free_space_elements_ratio` features. The background `freeSpaceRatioKeepingThreadFunc` aborts the server (debug/sanitizer) or throws `LOGICAL_ERROR` (release) whenever all entries have been promoted to the protected queue and the free-space target asks the cache to evict them. Removed the assertion. The downstream code already handles the case correctly: it passes zero size/elements to `probationary_queue.collectEvictionInfo` (which returns an empty info immediately) and routes the full requested amount to the protected queue. Added a regression unit test `FileCacheTest.SLRUFreeSpaceKeepingProtectedOnly` that reproduces the exact stack trace from the issue. Closes ClickHouse#104307
|
Pre-PR validation gate a) Deterministic repro? Yes — the new unit test b) Root cause explained? Yes. With c) Fix matches root cause? Yes — removed the wrong assertion. The downstream code already handles d) Test intent preserved? / New tests added? Existing tests are unchanged. Added e) Demonstrated in both directions? Yes:
f) Fix is general, not a narrow patch? Yes — same bug applies for Session: |
|
cc @kssenii @alesapin — could you review this? It removes a wrong |
|
Workflow [PR], commit [6774371] Summary: ✅ AI ReviewSummaryThis PR removes an invalid assertion in ClickHouse Rules
Final VerdictStatus: ✅ Approve |
LLVM Coverage Report
Changed lines: 100.00% (50/50) · Uncovered code |
Cherry pick #104313 to 26.4: Fix SLRU free-space-keeping crash when probationary queue is empty
…ationary queue is empty
Backport #104313 to 26.4: Fix SLRU free-space-keeping crash when probationary queue is empty
The original test (added in ClickHouse#104313 to cover ClickHouse#104307) drove the bug condition through `FileCache::freeSpaceRatioKeepingThreadFunc` with `keep_free_space_elements_ratio = 1.0`. That setting causes `cache->initialize()` to schedule the asynchronous background eviction task at `FileCache.cpp:444`, which then races with the test's `read_and_check` and intermediate `assertProbationary` / `assertProtected` calls. On slow builds (notably `Unit tests (amd_llvm_coverage)`) the background thread evicts entries between the populate and assert steps, causing the test to fail spuriously even though the underlying fix is correct. Confirmed master-branch failures on commits `74e1c5ed` (16:03:32 UTC, line 1626 mismatch) and `407d6cca` (16:24:46 UTC, `assertProtected` mismatch at line 214 from line 1616). The same failure pattern reproduces in PR run for ClickHouse#89334 (b5c32ee, line 214 mismatches). All failures are exclusive to the coverage shard; tsan / asan_ubsan / msan all pass. The replacement test exercises `SLRUFileCachePriority::collectEvictionInfo` directly, constructing the priority queue and adding three protected-only entries via `addForRestore(... QueueEntryType::SLRU_Protected ...)`. This reproduces the exact bug condition (probationary empty, protected populated, `is_total_space_cleanup=true`) without involving the file cache's asynchronous machinery, making it deterministic across all build flavors. Verified locally: - With the fix in `SLRUFileCachePriority::collectEvictionInfo`, the test passes 10/10 in 0 ms. - Reintroducing the original `chassert` aborts the unit test process with `Logical error: 'evict_size_from_probationary || evict_elements_from_probationary'` and exit code 134 (SIGABRT), confirming the test still catches the regression. - All 11 `FileCacheTest.*` tests still pass. CI failures observed in https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=74e1c5ed2c4437352b486d59b16ee8bd98a8b688&name_0=MasterCI&name_1=Unit%20tests%20%28amd_llvm_coverage%29 and https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=407d6ccaeb432fbd86b15dc6d341651a64f447dc&name_0=MasterCI&name_1=Unit%20tests%20%28amd_llvm_coverage%29
… when probationary queue is empty
…4313 Backport ClickHouse#104313 to 26.4: Fix SLRU free-space-keeping crash when probationary queue is empty
Fixes #104307.
Motivation
The background
freeSpaceRatioKeepingThreadFuncenforceskeep_free_space_size_ratio/keep_free_space_elements_ratiobycalling
SLRUFileCachePriority::collectEvictionInfowithis_total_space_cleanup=true. The function had achassertrequiringthat we evict at least one byte or element from the probationary queue.
This is wrong: when entries have all been promoted to the protected
queue (probationary empty) and the free-space target asks the cache to
evict everything, the function must still be allowed to evict from the
protected queue. Without the fix the server aborts in debug/sanitizer
builds and throws
LOGICAL_ERRORin release builds. The bug isreachable with the user-facing config in #104307:
Approach
Removed the over-zealous assertion in
SLRUFileCachePriority::collectEvictionInfo. The downstream codealready handles the empty-probationary case correctly: it passes zeros
to
probationary_queue.collectEvictionInfo(which returns an empty infoimmediately) and routes the full requested amount to
protected_queue.collectEvictionInfo.Added a regression unit test
FileCacheTest.SLRUFreeSpaceKeepingProtectedOnlyinsrc/Interpreters/tests/gtest_filecache.cppthat:keep_free_space_elements_ratio = 1.0.queue (probationary stays empty).
freeSpaceRatioKeepingThreadFuncdirectly.(
SLRUFileCachePriority.cpp:217 -> FileCache.cpp:1446). With thefix the call returns cleanly and all 3 protected entries are evicted
in one batch.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a server abort/
LOGICAL_ERRORin the filesystem cache background eviction thread (SLRUFileCachePriority::collectEvictionInfo) that could fire whenkeep_free_space_size_ratioorkeep_free_space_elements_ratiowas high enough to trigger eviction while all cache entries had already been promoted to the SLRU protected queue (probationary empty).Documentation entry for user-facing changes
Session: cron:clickhouse-ci-task-worker:20260507-164500
Version info
26.5.1.45326.4.3.4