From cecaf854b502dd92c54a53958086e110b278bf87 Mon Sep 17 00:00:00 2001 From: Nicolae Vartolomei Date: Fri, 20 Dec 2024 17:07:23 +0000 Subject: [PATCH] cst/cache: skip trim if free disk info is not available Otherwise it ends up removing everything from the cache when disk info is not yet available. --- src/v/cloud_storage/cache_service.cc | 13 +++++++++++-- src/v/cloud_storage/cache_service.h | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/v/cloud_storage/cache_service.cc b/src/v/cloud_storage/cache_service.cc index afc20f8f16b8f..6cb0614200e1e 100644 --- a/src/v/cloud_storage/cache_service.cc +++ b/src/v/cloud_storage/cache_service.cc @@ -356,6 +356,10 @@ ss::future<> cache::trim( target_size *= _cache_size_low_watermark; } + if (!_free_space.has_value()) { + throw std::runtime_error("Free space information is not available."); + } + // In the extreme case where even trimming to the low watermark wouldn't // free enough space to enable writing to the cache, go even further. if (_free_space < config::shard_local_cfg().storage_min_free_bytes()) { @@ -1533,7 +1537,7 @@ bool cache::may_exceed_limits(uint64_t bytes, size_t objects) { auto would_fit_in_cache = _current_cache_size + bytes <= _max_bytes; - return !_block_puts && _free_space > bytes * 10 + return !_block_puts && _free_space.value_or(0) > bytes * 10 && _current_cache_objects + _reserved_cache_objects + objects < _max_objects() && !would_fit_in_cache; @@ -1871,7 +1875,12 @@ ss::future<> cache::do_reserve_space(uint64_t bytes, size_t objects) { // all skipping the cache limit based on the same apparent // free bytes. This counter will get reset to ground // truth the next time we get a disk status notification. - _free_space -= bytes; + if (unlikely(!_free_space.has_value())) { + throw std::runtime_error( + "Free space information must be available by the " + "time we execute this code path"); + } + *_free_space -= bytes; break; } else { // No allowance, and the disk does not have a lot of diff --git a/src/v/cloud_storage/cache_service.h b/src/v/cloud_storage/cache_service.h index 885a464f442f6..386aac358fc1d 100644 --- a/src/v/cloud_storage/cache_service.h +++ b/src/v/cloud_storage/cache_service.h @@ -335,7 +335,7 @@ class cache /// and have to decide whether to block writes, or exceed our configured /// limit. /// (shard 0 only) - uint64_t _free_space{0}; + std::optional _free_space; ssx::semaphore _cleanup_sm{1, "cloud/cache"}; std::set _files_in_progress;