Skip to content

Fix SLRU race bug in fs cache 26.1+ - #101991

Merged
kssenii merged 5 commits into
masterfrom
fix-fs-cache-logical-error
Apr 9, 2026
Merged

kssenii merged 5 commits into
masterfrom
fix-fs-cache-logical-error

Conversation

@kssenii

@kssenii kssenii commented Apr 7, 2026 •

Copy link
Copy Markdown
Member

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 SLRU race bug in filesystem cache 26.1+, which can lead to space reservation logical error. In debug build it can also lead to failed assert: 'Previous state is Evicting, but expected state to be Active while setting Evicting flag for 2c1e3484ecdc6b78a8978fa5b17c5097:0:339 (state: Evicting)'.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.4.1.722
  • Backported to: 26.3.6.4, 26.2.10.7, 26.1.10.8

@clickhouse-gh

clickhouse-gh Bot commented Apr 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [5f6a8f7]

Summary: ❌

job_name test_name status info comment
Stateless tests (arm_asan_ubsan, flaky check) failure
00084_external_aggregation FAIL cidb IGNORED
00084_external_aggregation FAIL cidb IGNORED
Stateless tests (amd_asan_ubsan, flaky check) failure
00084_external_aggregation FAIL cidb IGNORED
Stateless tests (amd_tsan, flaky check) failure
00084_external_aggregation FAIL cidb IGNORED
00084_external_aggregation FAIL cidb IGNORED
Bugfix validation (integration tests) failure
test_filesystem_cache/test.py::test_parallel_cache_loading_on_startup[node] FAIL cidb IGNORED
test_filesystem_cache/test.py::test_caches_with_the_same_configuration[node] FAIL cidb IGNORED
test_filesystem_cache/test.py::test_caches_with_the_same_configuration_2[node_caches_with_same_path] FAIL cidb IGNORED
test_filesystem_cache/test.py::test_custom_cached_disk FAIL cidb IGNORED
test_filesystem_cache/test.py::test_force_filesystem_cache_on_merges FAIL cidb IGNORED
test_filesystem_cache/test.py::test_system_sync_filesystem_cache FAIL cidb IGNORED
test_filesystem_cache/test.py::test_keep_up_size_ratio FAIL cidb IGNORED
test_filesystem_cache/test.py::test_dynamic_resize FAIL cidb IGNORED
test_filesystem_cache/test.py::test_filesystem_cache_log FAIL cidb IGNORED
test_filesystem_cache/test.py::test_dynamic_resize_disabled FAIL cidb IGNORED
4 more test cases not shown

AI Review

Summary

This PR fixes an SLRU eviction race in filesystem cache by introducing a temporary PreActive entry state and atomically switching iterator pointer/state in SLRUIterator::setIterator, plus adds a stress test for concurrent eviction. After reviewing the modified cache logic and test changes at PR head, I did not find correctness, safety, or performance regressions that warrant blocking comments.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Apr 7, 2026
@nikitamikhaylov

Copy link
Copy Markdown
Member

Is it possible to write a unit test?

@kssenii

kssenii commented Apr 8, 2026

Copy link
Copy Markdown
Member Author

Is it possible to write a unit test?

No, it depends on too many things to coincide at a certain period of time which is not possible to form in a unit test since the methods used are complex by itself and cannot be used in a unit test in a dismantled way.

@kssenii

kssenii commented Apr 8, 2026

Copy link
Copy Markdown
Member Author

Is it possible to write a unit test?

Reproduced in integration stress test, added in this PR.

@kssenii
kssenii force-pushed the fix-fs-cache-logical-error branch from 1b6cd75 to 4f3827e Compare April 8, 2026 11:43
@kssenii
kssenii force-pushed the fix-fs-cache-logical-error branch from 4f3827e to 4b6a4ec Compare April 8, 2026 11:50
)
finally:
stop_event.set()
drop_thread.join(timeout=5)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

drop_thread.join(timeout=5) can return while the daemon thread is still alive. In that case the background loop keeps issuing SYSTEM CLEAR FILESYSTEM CACHE after the test body exits, which can leak activity into subsequent tests and make failures non-deterministic.

Please make thread shutdown strict here (for example, join without timeout, or keep timeout but then assert not drop_thread.is_alive() and fail fast).

@kssenii
kssenii force-pushed the fix-fs-cache-logical-error branch from 4b6a4ec to 8e75d32 Compare April 8, 2026 11:58
- Increase value size from `randomString(100)` to `randomString(500)` so the
  dataset (~50 MB) is much larger than the 1 MB cache, ensuring every scan
  triggers eviction and continuous probationary ↔ protected queue transitions.
- Replace the `SELECT count()` benchmark + concurrent `SYSTEM CLEAR FILESYSTEM CACHE`
  loop with a simpler `SELECT sum(key)` benchmark. The cache-drop loop was
  racy and could suppress the very errors we want to detect.
- Change the error check from `system.errors` to `node.contains_in_log`
  because `chassert` failures in a debug or sanitizer build abort the server
  process, so `LOGICAL_ERROR` never reaches `system.errors` but always
  appears in the server log.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Apr 9, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.00% -0.10%
Functions 90.90% 90.90% +0.00%
Branches 76.60% 76.50% -0.10%

Changed lines: 92.96% (66/71) | lost baseline coverage: 3 line(s) · Uncovered code

Full report · Diff report

@kssenii
kssenii added this pull request to the merge queue Apr 9, 2026
Merged via the queue into master with commit 9eaa3e8 Apr 9, 2026
158 of 163 checks passed
@kssenii
kssenii deleted the fix-fs-cache-logical-error branch April 9, 2026 16:41
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 9, 2026
@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Apr 9, 2026
clickhouse-gh Bot added a commit that referenced this pull request Apr 9, 2026
Backport #101991 to 26.2: Fix SLRU race bug in fs cache 26.1+
clickhouse-gh Bot added a commit that referenced this pull request Apr 9, 2026
Backport #101991 to 26.3: Fix SLRU race bug in fs cache 26.1+
@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 Apr 14, 2026
kssenii added a commit that referenced this pull request Apr 15, 2026
Backport #101991 to 26.1: Fix SLRU race bug in fs cache 26.1+
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.2-must-backport v26.3-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants