Repository navigation
Remove a few FileSegment::mutex locks from a few places - #108932
Conversation
`FileSegment::state`, `getReservedSize`, `isDownloaded` and `isDetached` each return a single-field snapshot of an already-atomic field (`download_state`, `reserved_size`). Taking `segment_guard` added no consistency and only contended hot segments, so read these fields without the lock. To keep the lockless `download_state` reads safe, publish `DOWNLOADED` as the last write of each transition in `setDownloadedUnlocked` and `shrinkFileSegmentToDownloadedSize`: previously `DOWNLOADED` was set before finalizing the writer, releasing the reader and (when shrinking) settling the range and reserved size. A reader observing `DOWNLOADED` reads the cache file from disk directly, so it must happen-after all the finalizing writes. Since `download_state` is a seq-cst atomic, publishing it last guarantees an observer of `DOWNLOADED` sees a consistent, fully downloaded segment. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Workflow [PR], commit [977c99a] Summary: ❌
AI ReviewSummaryThis PR removes a few PR Metadata
Missing context / blind spots
Final Verdict✅ No remaining code blockers in the current head from manual review. Clean up the changelog metadata and rely on the pending debug/sanitizer CI for final confidence. |
`setDownloadedUnlocked` now publishes `DOWNLOADED` as its last write, so if `cache_writer->finalize()` throws, the segment would unwind with `download_state` still `DOWNLOADING` and the downloader gone. `resetDownloader` / `complete` wake waiters on scope exit, so readers blocked in `FileSegment::wait` would wake up, observe `DOWNLOADING` again and spin forever with no terminal state to progress to. Wrap the `finalize` call in a try/catch and, on failure, move the segment to a non-`DOWNLOADING` state via `setDownloadFailedUnlocked` instead of propagating. #108932 (comment) Co-Authored-By: Claude Fable 5 <[email protected]>
When completing a `PARTIALLY_DOWNLOADED` / `PARTIALLY_DOWNLOADED_NO_CONTINUATION` file segment as the last holder, `complete` finalizes the cache writer and then calls `shrinkFileSegmentToDownloadedSize`, which reclaims the reserve-ahead surplus (reserved but not downloaded space) back to the cache quota. If `cache_writer->finalize()` threw, the shrink was skipped and the surplus stayed charged against the quota forever. Wrap the `finalize` call so a failure is logged and the segment is still resized and its reserved surplus reclaimed. Also log the swallowed exception in `setDownloadedUnlocked`. Co-Authored-By: Claude Fable 5 <[email protected]>
`FileSegment::complete` uses `isDetached` to confirm a benign concurrent detach when `lockKeyMetadata(false)` returns `nullptr`. `setDetachedState` sets `DETACHED` and resets `key_metadata` under the segment lock, so a lockless atomic load in `isDetached` can race: `lockKeyMetadata` can observe the key metadata gone while the load still returns a non-`DETACHED` state, turning a benign concurrent detach into `LOGICAL_ERROR: Cannot complete file segment`. Take the segment lock in `isDetached` so the read has a happens-after edge with `setDetachedState`. This path is not hot, so the lock costs nothing on the hot read path (which uses `state` / `isDownloaded`). #108932 (comment) Co-Authored-By: Claude Fable 5 <[email protected]>
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 50/51 (98.04%) · Uncovered code |
`setDownloadedUnlocked` now publishes `DOWNLOADED` as its last write, so if `cache_writer->finalize()` throws, the segment would unwind with `download_state` still `DOWNLOADING` and the downloader gone. `resetDownloader` / `complete` wake waiters on scope exit, so readers blocked in `FileSegment::wait` would wake up, observe `DOWNLOADING` again and spin forever with no terminal state to progress to. Wrap the `finalize` call in a try/catch and, on failure, move the segment to a non-`DOWNLOADING` state via `setDownloadFailedUnlocked` instead of propagating. ClickHouse#108932 (comment) Co-Authored-By: Claude Fable 5 <[email protected]>
`FileSegment::complete` uses `isDetached` to confirm a benign concurrent detach when `lockKeyMetadata(false)` returns `nullptr`. `setDetachedState` sets `DETACHED` and resets `key_metadata` under the segment lock, so a lockless atomic load in `isDetached` can race: `lockKeyMetadata` can observe the key metadata gone while the load still returns a non-`DETACHED` state, turning a benign concurrent detach into `LOGICAL_ERROR: Cannot complete file segment`. Take the segment lock in `isDetached` so the read has a happens-after edge with `setDetachedState`. This path is not hot, so the lock costs nothing on the hot read path (which uses `state` / `isDownloaded`). ClickHouse#108932 (comment) Co-Authored-By: Claude Fable 5 <[email protected]>
…ove-mutex-locks Remove a few FileSegment::mutex locks from a few places
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Remove a few FileSegment::mutex locks in a few places.
Version info
26.7.1.593(included in26.7and later)