Fix assertion

This commit is contained in:
kssenii 2024-04-05 18:37:46 +02:00
parent 6428868843
commit eb9690016a
7 changed files with 37 additions and 38 deletions

View File

@ -10,12 +10,6 @@ public:
using FinalizeEvictionFunc = std::function<void(const CachePriorityGuard::Lock & lk)>; using FinalizeEvictionFunc = std::function<void(const CachePriorityGuard::Lock & lk)>;
EvictionCandidates() = default; EvictionCandidates() = default;
EvictionCandidates(EvictionCandidates && other) noexcept
{
candidates = std::move(other.candidates);
candidates_size = std::move(other.candidates_size);
queue_entries_to_invalidate = std::move(other.queue_entries_to_invalidate);
}
~EvictionCandidates(); ~EvictionCandidates();
void add( void add(

View File

@ -1388,8 +1388,8 @@ void FileCache::applySettingsIfPossible(const FileCacheSettings & new_settings,
if (new_settings.max_size != actual_settings.max_size if (new_settings.max_size != actual_settings.max_size
|| new_settings.max_elements != actual_settings.max_elements) || new_settings.max_elements != actual_settings.max_elements)
{ {
std::optional<EvictionCandidates> eviction_candidates; EvictionCandidates eviction_candidates;
bool modified_size_limits = false; bool limits_satisfied = false;
{ {
cache_is_being_resized.store(true, std::memory_order_relaxed); cache_is_being_resized.store(true, std::memory_order_relaxed);
SCOPE_EXIT({ SCOPE_EXIT({
@ -1399,15 +1399,12 @@ void FileCache::applySettingsIfPossible(const FileCacheSettings & new_settings,
auto cache_lock = lockCache(); auto cache_lock = lockCache();
FileCacheReserveStat stat; FileCacheReserveStat stat;
eviction_candidates.emplace(main_priority->collectCandidatesForEviction( limits_satisfied = main_priority->collectCandidatesForEviction(
new_settings.max_size, new_settings.max_elements, 0/* max_candidates_to_evict */, stat, cache_lock)); new_settings.max_size, new_settings.max_elements, 0/* max_candidates_to_evict */, stat, eviction_candidates, cache_lock);
eviction_candidates->removeQueueEntries(cache_lock); eviction_candidates.removeQueueEntries(cache_lock);
modified_size_limits = main_priority->getSize(cache_lock) <= new_settings.max_size if (limits_satisfied)
&& main_priority->getElementsCount(cache_lock) <= new_settings.max_elements;
if (modified_size_limits)
{ {
main_priority->modifySizeLimits( main_priority->modifySizeLimits(
new_settings.max_size, new_settings.max_elements, new_settings.slru_size_ratio, cache_lock); new_settings.max_size, new_settings.max_elements, new_settings.slru_size_ratio, cache_lock);
@ -1423,16 +1420,16 @@ void FileCache::applySettingsIfPossible(const FileCacheSettings & new_settings,
try try
{ {
eviction_candidates->evict(); eviction_candidates.evict();
} }
catch (...) catch (...)
{ {
auto cache_lock = lockCache(); auto cache_lock = lockCache();
eviction_candidates->finalize(nullptr, cache_lock); eviction_candidates.finalize(nullptr, cache_lock);
throw; throw;
} }
if (modified_size_limits) if (limits_satisfied)
{ {
LOG_INFO(log, "Changed max_size from {} to {}, max_elements from {} to {}", LOG_INFO(log, "Changed max_size from {} to {}, max_elements from {} to {}",
actual_settings.max_size, new_settings.max_size, actual_settings.max_size, new_settings.max_size,

View File

@ -147,11 +147,12 @@ public:
const CachePriorityGuard::Lock &) = 0; const CachePriorityGuard::Lock &) = 0;
/// Collect eviction `candidates_num` candidates for eviction. /// Collect eviction `candidates_num` candidates for eviction.
virtual EvictionCandidates collectCandidatesForEviction( virtual bool collectCandidatesForEviction(
size_t desired_size, size_t desired_size,
size_t desired_elements_count, size_t desired_elements_count,
size_t max_candidates_to_evict, size_t max_candidates_to_evict,
FileCacheReserveStat & stat, FileCacheReserveStat & stat,
EvictionCandidates & candidates,
const CachePriorityGuard::Lock &) = 0; const CachePriorityGuard::Lock &) = 0;
virtual bool modifySizeLimits( virtual bool modifySizeLimits(

View File

@ -322,14 +322,14 @@ bool LRUFileCachePriority::collectCandidatesForEviction(
} }
} }
EvictionCandidates LRUFileCachePriority::collectCandidatesForEviction( bool LRUFileCachePriority::collectCandidatesForEviction(
size_t desired_size, size_t desired_size,
size_t desired_elements_count, size_t desired_elements_count,
size_t max_candidates_to_evict, size_t max_candidates_to_evict,
FileCacheReserveStat & stat, FileCacheReserveStat & stat,
EvictionCandidates & res,
const CachePriorityGuard::Lock & lock) const CachePriorityGuard::Lock & lock)
{ {
EvictionCandidates res;
auto stop_condition = [&, this]() auto stop_condition = [&, this]()
{ {
return canFit(0, 0, stat.total_stat.releasable_size, stat.total_stat.releasable_count, return canFit(0, 0, stat.total_stat.releasable_size, stat.total_stat.releasable_count,
@ -337,7 +337,7 @@ EvictionCandidates LRUFileCachePriority::collectCandidatesForEviction(
|| (max_candidates_to_evict && res.size() >= max_candidates_to_evict); || (max_candidates_to_evict && res.size() >= max_candidates_to_evict);
}; };
iterateForEviction(res, stat, stop_condition, lock); iterateForEviction(res, stat, stop_condition, lock);
return res; return stop_condition();
} }
void LRUFileCachePriority::iterateForEviction( void LRUFileCachePriority::iterateForEviction(

View File

@ -62,11 +62,12 @@ public:
const UserID & user_id, const UserID & user_id,
const CachePriorityGuard::Lock &) override; const CachePriorityGuard::Lock &) override;
EvictionCandidates collectCandidatesForEviction( bool collectCandidatesForEviction(
size_t desired_size, size_t desired_size,
size_t desired_elements_count, size_t desired_elements_count,
size_t max_candidates_to_evict, size_t max_candidates_to_evict,
FileCacheReserveStat & stat, FileCacheReserveStat & stat,
EvictionCandidates & res,
const CachePriorityGuard::Lock &) override; const CachePriorityGuard::Lock &) override;
void shuffle(const CachePriorityGuard::Lock &) override; void shuffle(const CachePriorityGuard::Lock &) override;

View File

@ -251,42 +251,47 @@ bool SLRUFileCachePriority::collectCandidatesForEvictionInProtected(
return true; return true;
} }
EvictionCandidates SLRUFileCachePriority::collectCandidatesForEviction( bool SLRUFileCachePriority::collectCandidatesForEviction(
size_t desired_size, size_t desired_size,
size_t desired_elements_count, size_t desired_elements_count,
size_t max_candidates_to_evict, size_t max_candidates_to_evict,
FileCacheReserveStat & stat, FileCacheReserveStat & stat,
EvictionCandidates & res,
const CachePriorityGuard::Lock & lock) const CachePriorityGuard::Lock & lock)
{ {
const auto desired_probationary_size = getRatio(desired_size, 1 - size_ratio); const auto desired_probationary_size = getRatio(desired_size, 1 - size_ratio);
const auto desired_probationary_elements_num = getRatio(desired_elements_count, 1 - size_ratio); const auto desired_probationary_elements_num = getRatio(desired_elements_count, 1 - size_ratio);
auto res = probationary_queue.collectCandidatesForEviction( FileCacheReserveStat probationary_stat;
desired_probationary_size, desired_probationary_elements_num, max_candidates_to_evict, stat, lock); const bool probationary_limit_satisfied = probationary_queue.collectCandidatesForEviction(
desired_probationary_size, desired_probationary_elements_num,
max_candidates_to_evict, probationary_stat, res, lock);
LOG_TEST(log, "Collected {} eviction candidates from probationary queue (size: {})", stat += probationary_stat;
res.size(), stat.total_stat.releasable_size);
LOG_TEST(log, "Collected {} to evict from probationary queue. Total size: {}",
res.size(), probationary_stat.total_stat.releasable_size);
chassert(!max_candidates_to_evict || res.size() <= max_candidates_to_evict); chassert(!max_candidates_to_evict || res.size() <= max_candidates_to_evict);
chassert(res.size() == stat.total_stat.releasable_count); chassert(res.size() == stat.total_stat.releasable_count);
if (max_candidates_to_evict && res.size() == max_candidates_to_evict) if (max_candidates_to_evict && res.size() >= max_candidates_to_evict)
return res; return probationary_limit_satisfied;
const auto desired_protected_size = getRatio(max_size, size_ratio); const auto desired_protected_size = getRatio(max_size, size_ratio);
const auto desired_protected_elements_num = getRatio(max_elements, size_ratio); const auto desired_protected_elements_num = getRatio(max_elements, size_ratio);
FileCacheReserveStat protected_stat; FileCacheReserveStat protected_stat;
auto res_add = protected_queue.collectCandidatesForEviction( const bool protected_limit_satisfied = protected_queue.collectCandidatesForEviction(
desired_protected_size, desired_protected_elements_num, desired_protected_size, desired_protected_elements_num,
max_candidates_to_evict ? max_candidates_to_evict - res.size() : 0, protected_stat, lock); max_candidates_to_evict - res.size(), protected_stat, res, lock);
LOG_TEST(log, "Collected {} eviction candidates from protected queue (size: {})",
res_add.size(), protected_stat.total_stat.releasable_size);
stat += protected_stat; stat += protected_stat;
res.insert(std::move(res_add), lock);
return res; LOG_TEST(log, "Collected {} to evict from protected queue. Total size: {}",
res.size(), protected_stat.total_stat.releasable_size);
return probationary_limit_satisfied && protected_limit_satisfied;
} }
void SLRUFileCachePriority::downgrade(IteratorPtr iterator, const CachePriorityGuard::Lock & lock) void SLRUFileCachePriority::downgrade(IteratorPtr iterator, const CachePriorityGuard::Lock & lock)

View File

@ -58,11 +58,12 @@ public:
const UserID & user_id, const UserID & user_id,
const CachePriorityGuard::Lock &) override; const CachePriorityGuard::Lock &) override;
EvictionCandidates collectCandidatesForEviction( bool collectCandidatesForEviction(
size_t desired_size, size_t desired_size,
size_t desired_elements_count, size_t desired_elements_count,
size_t max_candidates_to_evict, size_t max_candidates_to_evict,
FileCacheReserveStat & stat, FileCacheReserveStat & stat,
EvictionCandidates & res,
const CachePriorityGuard::Lock &) override; const CachePriorityGuard::Lock &) override;
void shuffle(const CachePriorityGuard::Lock &) override; void shuffle(const CachePriorityGuard::Lock &) override;