From: Haomai Wang Date: Wed, 23 Jul 2014 03:26:18 +0000 (+0800) Subject: common/RandomCache: Fix inconsistence between contents and count X-Git-Tag: v0.84~73^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=refs%2Fpull%2F2136%2Fhead;p=ceph.git common/RandomCache: Fix inconsistence between contents and count The add/clear method may cause count inconsistent with the real size of contents. Signed-off-by: Haomai Wang --- diff --git a/src/common/random_cache.hpp b/src/common/random_cache.hpp index 9b86108c86d3..da195f8f40e6 100644 --- a/src/common/random_cache.hpp +++ b/src/common/random_cache.hpp @@ -31,7 +31,7 @@ class RandomCache { // The first element of pair is the frequency of item, it's used to evict item ceph::unordered_map > contents; Mutex lock; - uint64_t max_size, count; + uint64_t max_size; K last_trim_key; // When cache reach full, consider to evict a certain number of items @@ -62,7 +62,6 @@ class RandomCache { for (typename map::iterator j = candidates.begin(); j != candidates.end(); j++) { contents.erase(j->second); - count--; evict_count--; if (!evict_count) break; @@ -71,23 +70,21 @@ class RandomCache { public: RandomCache(size_t max_size=20) : lock("RandomCache::lock"), - max_size(max_size), count(0) {} + max_size(max_size) {} ~RandomCache() { contents.clear(); - count = 0; } void clear(K key) { Mutex::Locker l(lock); - contents.erase(key); - count--; + contents.erase(key) } void set_size(size_t new_size) { Mutex::Locker l(lock); max_size = new_size; - if (max_size <= count) { - trim_cache(count - max_size); + if (max_size <= contents.size()) { + trim_cache(contents.size() - max_size); } } @@ -104,11 +101,10 @@ class RandomCache { void add(K key, V value) { Mutex::Locker l(lock); - if (max_size <= count) { + if (max_size <= contents.size()) { trim_cache(EVICT_COUNT); } contents[key] = make_pair(1, value); - count++; } };