Repository navigation
Fix flaky FileCacheTest.SLRUFreeSpaceKeepingProtectedOnly - #104418
Conversation
Pre-PR validation gate(session: cron:clickhouse-ci-task-worker:20260508-184500) a) Deterministic repro? Yes. b) Root cause explained? Yes. With c) Fix matches root cause? Yes. The new test exercises d) Test intent preserved? Yes. The new test still exercises the exact code path that the removed e) Demonstrated in both directions? Yes -- see (a). The reintroduced f) Fix is general, not a narrow patch? N/A -- this is a CI fix for a flaky test, not a code change. |
|
cc @kssenii — could you review this? It's a test-only follow-up to #104313 that replaces the racy integration test (which fails on |
|
Workflow [PR], commit [381f35e] Summary: ❌
AI ReviewSummaryThis PR replaces the flaky integration-style Final Verdict
|
…ilecache `FileCache::getCommonOrigin` returns `const FileCacheOriginInfo &`, so binding the result to `const auto` made an unnecessary copy. Bind to `const auto &` to silence the `performance-unnecessary-copy-initialization` clang-tidy diagnostic that broke the `Build (arm_tidy)` job for PR ClickHouse#104418. Build (arm_tidy) log: https://github.com/ClickHouse/ClickHouse/actions/runs/25574233246/job/75093398873 Session: cron:clickhouse-ci-task-worker:20260509-224500
|
Pushing an empty commit ( The previous PR run was blocked by a CIDB evidence for
(5 failures over 14 days across multiple unrelated PRs and master itself.) Why this PR cannot have caused it: #104418 touches a single file — Meanwhile, the bug this PR fixes keeps hitting master and many unrelated PRs:
cc @kssenii @alexey-milovidov — could one of you take a look so we can get this merged? The new unit test deterministically reproduces the original (session: cron:clickhouse-ci-task-worker:20260511-034500) |
| /// Verify the precondition: 3 entries / 15 bytes in protected, probationary empty. | ||
| ASSERT_EQ(priority.getElementsCount(state_guard.lock()), 3); | ||
| ASSERT_EQ(priority.getSize(state_guard.lock()), 15); |
There was a problem hiding this comment.
This check does not verify that probationary is empty like the comment states
There was a problem hiding this comment.
@kssenii Thanks for the review — addressed in 8ff6220 (just pushed).
The previous comment claimed "probationary empty" but the assertions only checked the SLRU totals (getElementsCount / getSize). I considered using assertProbationary(priority.dump(...), Ranges{}) to make the empty-probationary claim explicit, but LRUFileCachePriority::iterate (which dump goes through) chasserts on entries whose KeyMetadata has no FileSegmentMetadata attached — the unit test uses addForRestore precisely to skip that setup and avoid the FileCache::initialize race that made the original test flaky.
Instead, I added small inline per-sub-queue accessors to SLRUFileCachePriority:
size_t getProtectedSize(...) const { return protected_queue.getSize(...); }
size_t getProtectedElementsCount(...) const { return protected_queue.getElementsCount(...); }
size_t getProbationarySize(...) const { return probationary_queue.getSize(...); }
size_t getProbationaryElementsCount(...) const { return probationary_queue.getElementsCount(...); }They use the exact same access pattern already in getStateInfoForLog (which reads protected_queue.getSize/... directly), so the new surface area is minimal.
The precondition block now reads:
ASSERT_EQ(priority.getElementsCount(state_guard.lock()), 3);
ASSERT_EQ(priority.getSize(state_guard.lock()), 15);
ASSERT_EQ(priority.getProtectedElementsCount(state_guard.lock()), 3);
ASSERT_EQ(priority.getProtectedSize(state_guard.lock()), 15);
ASSERT_EQ(priority.getProbationaryElementsCount(state_guard.lock()), 0);
ASSERT_EQ(priority.getProbationarySize(state_guard.lock()), 0);The empty-probationary ASSERT_EQ is what you specifically asked for; the populated-protected pair is its mirror.
Verified locally: unit_tests_dbms --gtest_filter=FileCacheTest.SLRUFreeSpaceKeepingProtectedOnly 30/30 passes on debug build.
Diff: +17/-1 across two files.
|
@groeneai, why did the FunctionsStress fail? Provide a fix. |
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
…ilecache `FileCache::getCommonOrigin` returns `const FileCacheOriginInfo &`, so binding the result to `const auto` made an unnecessary copy. Bind to `const auto &` to silence the `performance-unnecessary-copy-initialization` clang-tidy diagnostic that broke the `Build (arm_tidy)` job for PR ClickHouse#104418. Build (arm_tidy) log: https://github.com/ClickHouse/ClickHouse/actions/runs/25574233246/job/75093398873 Session: cron:clickhouse-ci-task-worker:20260509-224500
The previous Fast test failure on 02932_refreshable_materialized_views_2 is a known chronic flaky test (5 failures over 14 days across multiple PRs including master). This PR only modifies src/Interpreters/tests/gtest_filecache.cpp and cannot affect refreshable materialized view behavior.
Reviewer @kssenii noted that the precondition comment said "probationary empty" but the assertions only checked the SLRU totals (`getSize` / `getElementsCount`). With three `addForRestore` calls all targeting `SLRU_Protected`, the structural invariant guarantees probationary is empty, but a future regression that misroutes the entry type or hashes the `KeyMetadata` differently would still satisfy the total counters. This change makes the precondition explicit: - Add per-sub-queue accessors `getProtectedSize`, `getProtectedElementsCount`, `getProbationarySize`, `getProbationaryElementsCount` to `SLRUFileCachePriority`. They are small inline wrappers over `protected_queue.*` / `probationary_queue.*` (the same access pattern already used by `getStateInfoForLog`), so the new surface area is minimal and the implementation is trivially correct. - Replace the precondition block in the regression test with four per-queue `ASSERT_EQ` calls. The empty-probationary assertion is the one the reviewer specifically asked for; the populated-protected assertion is its mirror. Note: the original draft used `assertProtected` / `assertProbationary` against `priority.dump(...)`. That path goes through `LRUFileCachePriority::iterate`, which `chassert`s on entries whose `KeyMetadata` has no `FileSegmentMetadata` attached (the unit test uses `addForRestore` precisely to skip that setup and avoid the `FileCache::initialize` race that made the previous test flaky). Direct per-queue counters via the new accessors give the same coverage without requiring `dump` to iterate. Verified locally: `unit_tests_dbms --gtest_filter=FileCacheTest.SLRUFreeSpaceKeepingProtectedOnly` 30/30 passes.
8ff6220 to
b004a52
Compare
|
@alexey-milovidov — the Rebased onto current master ( Session: cron:clickhouse-ci-task-worker:20260511-161500 |
| /// `SLRUFileCachePriority::collectEvictionInfo` is invoked from | ||
| /// `FileCache::freeSpaceRatioKeepingThreadFunc` (driven by the | ||
| /// `keep_free_space_size(elements)_ratio` features) with `is_total_space_cleanup=true`. | ||
| /// With a high enough free-space target the function used to `chassert` that we | ||
| /// evict at least one element/byte from the probationary queue. This is wrong when | ||
| /// entries have all been promoted to the protected queue and the probationary queue | ||
| /// is empty: the function must still be able to evict from the protected queue. | ||
| /// Without the fix, the assertion aborts the server in debug/sanitizer builds and | ||
| /// throws a `LOGICAL_ERROR` in release. | ||
| /// | ||
| /// We exercise `SLRUFileCachePriority::collectEvictionInfo` directly rather than | ||
| /// going through `FileCache::freeSpaceRatioKeepingThreadFunc` to avoid the timing | ||
| /// race with the asynchronous background eviction task that `FileCache` schedules | ||
| /// when `keep_free_space_*_ratio` is set: that task evicts entries between the | ||
| /// populate and assert steps, especially on slow builds (e.g. coverage), which | ||
| /// makes the higher-level test inherently flaky. The unit-level test below | ||
| /// reproduces the exact bug condition deterministically and on every build flavor. |
There was a problem hiding this comment.
@groeneai this comment is too huge, please make it consize
| /// Per-sub-queue accessors. Used by `getStateInfoForLog` and by unit tests that | ||
| /// need to verify that an entry landed in the expected SLRU sub-queue without | ||
| /// going through `dump`/`iterate` (which require full `KeyMetadata` setup that | ||
| /// the `addForRestore` test path does not perform). |
There was a problem hiding this comment.
Remove this comment, the methods are self explanatory
There was a problem hiding this comment.
Done in 381f35e — removed the comment block, methods left self-explanatory.
Removes the comment block above the per-sub-queue accessors in `SLRUFileCachePriority.h`. Per @kssenii's inline review on PR ClickHouse#104418, the method names are self-explanatory. ClickHouse#104418 (comment)
LLVM Coverage Report
Changed lines: 100.00% (49/49) · Uncovered code |
55a1c75
The regression test introduced in #104313 (covering issue #104307) is flaky on
Unit tests (amd_llvm_coverage). It exercises the bug condition throughFileCache::freeSpaceRatioKeepingThreadFuncwithkeep_free_space_elements_ratio = 1.0,which causes
cache->initialize()to schedule the asynchronous background evictiontask. That task races with the test's
read_and_checkand intermediateassertProbationary/assertProtectedcalls, especially on slow builds (notablycoverage), evicting entries between the populate and assert steps and producing
spurious failures.
Failures observed
74e1c5ed2c4(PR Fix SLRU free-space-keeping crash when probationary queue is empty #104313 merge) at 16:03:32 UTC -- mismatch atgtest_filecache.cpp:1626(getFileSegmentsNum() == 3, expected 0).407d6ccaeb4at 16:24:46 UTC -- mismatch at line 214 (called from line 1616)in
assertProtected: actual0, expected3.b5c32eefat 18:01:22 UTC -- two mismatches at line 214 with the samepattern.
All failures are exclusive to the
amd_llvm_coverageshard.tsan,asan_ubsan, andmsanruns on the same commits all pass, confirming this is a timing race on slowbuilds rather than a real regression of the underlying fix.
CI reports:
Approach
Replace the high-level integration test with a focused unit-level test that exercises
SLRUFileCachePriority::collectEvictionInfodirectly. The new test constructs thepriority queue, adds three protected-only entries via
addForRestore(... QueueEntryType::SLRU_Protected ...), and callscollectEvictionInfowith the bug-triggering parameters (
is_total_space_cleanup=true, probationary empty,protected populated). This reproduces the exact precondition that used to fire the
removed
chassert(evict_size_from_probationary || evict_elements_from_probationary),without involving the file cache's asynchronous machinery, making the test
deterministic on every build flavor.
Verification
SLRUFileCachePriority::collectEvictionInfo, the test passes 10/10runs in 0 ms.
chassertaborts the unit test process withLogical error: 'evict_size_from_probationary || evict_elements_from_probationary'and exit code 134 (SIGABRT), confirming the test still catches the regression.
FileCacheTest.*tests still pass.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
N/A -- CI fix only.
Documentation entry for user-facing changes
Version info
26.5.1.571