From e581fd14632cdfe5d35b97ec1166d91f4b5b797c Mon Sep 17 00:00:00 2001 From: Vlad Zolotarov Date: Thu, 30 Aug 2018 15:55:30 -0400 Subject: [PATCH] loading_cache: make size() return the size of lru_list instead of loading_shared_values reloading flow may hold the items in the underlying loading_shared_values after they have been removed (e.g. via remove(key) API) thereby loading_shared_values.size() doesn't represent the correct value for the loading_cache. lru_list.size() on the other hand - does. Signed-off-by: Vlad Zolotarov (cherry picked from commit 1e56c7dd5896f95522a12c6cc789a12d29879042) --- utils/loading_cache.hh | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/utils/loading_cache.hh b/utils/loading_cache.hh index ab3117d061..bfc00a3bd4 100644 --- a/utils/loading_cache.hh +++ b/utils/loading_cache.hh @@ -44,7 +44,7 @@ namespace bi = boost::intrusive; namespace utils { using loading_cache_clock_type = seastar::lowres_clock; -using auto_unlink_list_hook = bi::list_base_hook>; +using safe_link_list_hook = bi::list_base_hook>; template class timestamped_val { @@ -149,13 +149,13 @@ public: /// \brief This is and LRU list entry which is also an anchor for a loading_cache value. template -class timestamped_val::lru_entry : public auto_unlink_list_hook { +class timestamped_val::lru_entry : public safe_link_list_hook { private: using ts_value_type = timestamped_val; using loading_values_type = typename ts_value_type::loading_values_type; public: - using lru_list_type = bi::list>; + using lru_list_type = bi::list; using timestamped_val_ptr = typename loading_values_type::entry_ptr; private: @@ -174,6 +174,9 @@ public: } ~lru_entry() { + if (safe_link_list_hook::is_linked()) { + _lru_list.erase(_lru_list.iterator_to(*this)); + } _cache_size -= _ts_val_ptr->size(); _ts_val_ptr->set_anchor_back_reference(nullptr); } @@ -185,7 +188,9 @@ public: /// Set this item as the most recently used item. /// The MRU item is going to be at the front of the _lru_list, the LRU item - at the back. void touch() noexcept { - auto_unlink_list_hook::unlink(); + if (safe_link_list_hook::is_linked()) { + _lru_list.erase(_lru_list.iterator_to(*this)); + } _lru_list.push_front(*this); } @@ -426,7 +431,7 @@ public: } size_t size() const { - return _loading_values.size(); + return _lru_list.size(); } /// \brief returns the memory size the currently cached entries occupy according to the EntrySize predicate.