From b14628411d9936d1df732d6978aa4179820c32da Mon Sep 17 00:00:00 2001 From: XieHan Date: Tue, 28 Sep 2021 20:51:30 +0800 Subject: [PATCH 1/2] remove mutex from LRUCache --- src/manager/DnsCache.cc | 28 +++++++++------------------- src/manager/DnsCache.h | 2 ++ src/util/LRUCache.h | 22 +++------------------- 3 files changed, 14 insertions(+), 38 deletions(-) diff --git a/src/manager/DnsCache.cc b/src/manager/DnsCache.cc index 8b3ba049b0..fbafd40df7 100644 --- a/src/manager/DnsCache.cc +++ b/src/manager/DnsCache.cc @@ -14,6 +14,7 @@ limitations under the License. Authors: Wu Jiaxu (wujiaxu@sogou-inc.com) + Xie Han (xiehan@sogou-inc.com) */ #include @@ -27,25 +28,20 @@ const DnsCache::DnsHandle *DnsCache::get_inner(const HostPort& host_port, int type) { + int64_t cur_time = GET_CURRENT_SECOND; + std::lock_guard lock(mutex_); const DnsHandle *handle = cache_pool_.get(host_port); if (handle) { - int64_t cur_time = GET_CURRENT_SECOND; - switch (type) { case GET_TYPE_TTL: if (cur_time > handle->value.expire_time) { - std::lock_guard lock(mutex_); - - if (cur_time > handle->value.expire_time) - { - const_cast(handle)->value.expire_time += TTL_INC; - cache_pool_.release(handle); - return NULL; - } + const_cast(handle)->value.expire_time += TTL_INC; + cache_pool_.release(handle); + return NULL; } break; @@ -53,14 +49,9 @@ const DnsCache::DnsHandle *DnsCache::get_inner(const HostPort& host_port, int ty case GET_TYPE_CONFIDENT: if (cur_time > handle->value.confident_time) { - std::lock_guard lock(mutex_); - - if (cur_time > handle->value.confident_time) - { - const_cast(handle)->value.confident_time += CONFIDENT_INC; - cache_pool_.release(handle); - return NULL; - } + const_cast(handle)->value.confident_time += CONFIDENT_INC; + cache_pool_.release(handle); + return NULL; } break; @@ -96,7 +87,6 @@ const DnsCache::DnsHandle *DnsCache::put(const HostPort& host_port, expire_time = cur_time + dns_ttl_default; std::lock_guard lock(mutex_); - return cache_pool_.put(host_port, {addrinfo, confident_time, expire_time}); } diff --git a/src/manager/DnsCache.h b/src/manager/DnsCache.h index 04784e22c3..cc6e066f98 100644 --- a/src/manager/DnsCache.h +++ b/src/manager/DnsCache.h @@ -65,6 +65,7 @@ class DnsCache //Handle *get(const KEY &key); const DnsHandle *get(const HostPort& host_port) { + std::lock_guard lock(mutex_); return cache_pool_.get(host_port); } @@ -134,6 +135,7 @@ class DnsCache // delete from cache, deleter delay called when all inuse-handle release. void del(const HostPort& key) { + std::lock_guard lock(mutex_); cache_pool_.del(key); } diff --git a/src/util/LRUCache.h b/src/util/LRUCache.h index a665e10e61..ad237ce933 100644 --- a/src/util/LRUCache.h +++ b/src/util/LRUCache.h @@ -14,14 +14,14 @@ limitations under the License. Authors: Wu Jiaxu (wujiaxu@sogou-inc.com) + Xie Han (xiehan@sogou-inc.com) -- remove mutex */ #ifndef _LRUCACHE_H_ #define _LRUCACHE_H_ + #include #include -#include -//#include /** * @file LRUCache.h @@ -101,8 +101,6 @@ typedef typename Map::const_iterator MapConstIterator; ~LRUCache() { - std::lock_guard lock(mutex_); - // Error if caller has an unreleased handle assert(in_use_.next == &in_use_); for (Handle *e = not_use_.next; e != ¬_use_; ) @@ -121,8 +119,6 @@ typedef typename Map::const_iterator MapConstIterator; // max_size means max cache number of key-value pairs void set_max_size(size_t max_size) { - std::lock_guard lock(mutex_); - max_size_ = max_size; } @@ -132,8 +128,6 @@ typedef typename Map::const_iterator MapConstIterator; // Remove all cache that are not actively in use. void prune() { - std::lock_guard lock(mutex_); - while (not_use_.next != ¬_use_) { Handle *e = not_use_.next; @@ -147,12 +141,7 @@ typedef typename Map::const_iterator MapConstIterator; // release handle by get/put void release(Handle *handle) { - if (handle) - { - std::lock_guard lock(mutex_); - - unref(handle); - } + unref(handle); } void release(const Handle *handle) @@ -164,7 +153,6 @@ typedef typename Map::const_iterator MapConstIterator; // Need call release when handle no longer needed const Handle *get(const KEY& key) { - std::lock_guard lock(mutex_); MapConstIterator it = cache_map_.find(key); if (it != cache_map_.end()) @@ -183,8 +171,6 @@ typedef typename Map::const_iterator MapConstIterator; Handle *e = new Handle(key, value); e->ref = 1; - std::lock_guard lock(mutex_); - size_++; e->in_cache = true; e->ref++; @@ -216,7 +202,6 @@ typedef typename Map::const_iterator MapConstIterator; // delete from cache, deleter delay called when all inuse-handle release. void del(const KEY& key) { - std::lock_guard lock(mutex_); MapConstIterator it = cache_map_.find(key); if (it != cache_map_.end()) @@ -280,7 +265,6 @@ typedef typename Map::const_iterator MapConstIterator; unref(e); } - std::mutex mutex_; size_t max_size_; size_t size_; From efa27846caf1b5176ec7cf83e5897e3e1ec8f7cb Mon Sep 17 00:00:00 2001 From: XieHan Date: Tue, 28 Sep 2021 20:58:47 +0800 Subject: [PATCH 2/2] lock on Dns cache release --- src/manager/DnsCache.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/manager/DnsCache.h b/src/manager/DnsCache.h index cc6e066f98..6cbf377c62 100644 --- a/src/manager/DnsCache.h +++ b/src/manager/DnsCache.h @@ -52,11 +52,13 @@ class DnsCache // release handle by get/put void release(DnsHandle *handle) { + std::lock_guard lock(mutex_); cache_pool_.release(handle); } void release(const DnsHandle *handle) { + std::lock_guard lock(mutex_); cache_pool_.release(handle); }