]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
auth: make sure we look at 10% of all cached items during cleanup 8929/head
authorKees Monshouwer <mind04@monshouwer.org>
Thu, 12 Mar 2020 18:17:19 +0000 (19:17 +0100)
committermind04 <mind04@monshouwer.org>
Fri, 13 Mar 2020 15:19:12 +0000 (16:19 +0100)
pdns/auth-packetcache.cc
pdns/auth-querycache.cc
pdns/cachecleaner.hh

index fd96a16257336a5436ce4011cffc0f3e2579439a..71dbf134d50abdddf88086eb070266400d712b8a 100644 (file)
@@ -248,11 +248,7 @@ uint64_t AuthPacketCache::purge(const string &match)
                           
 void AuthPacketCache::cleanup()
 {
-  uint64_t maxCached = d_maxEntries;
-  uint64_t cacheSize = *d_statnumentries;
-  uint64_t totErased = 0;
-
-  totErased = pruneLockedCollectionsVector<SequencedTag>(d_maps, maxCached, cacheSize);
+  uint64_t totErased = pruneLockedCollectionsVector<SequencedTag>(d_maps);
   *d_statnumentries -= totErased;
 
   DLOG(g_log<<"Done with cache clean, cacheSize: "<<(*d_statnumentries)<<", totErased"<<totErased<<endl);
index 1baa52109229ed0ede439640f419c1f6350bd87b..d4970bd137a8cd7d6851afebb295861ca00327a0 100644 (file)
@@ -209,13 +209,9 @@ uint64_t AuthQueryCache::purge(const string &match)
 
 void AuthQueryCache::cleanup()
 {
-  uint64_t maxCached = d_maxEntries;
-  uint64_t cacheSize = *d_statnumentries;
-  uint64_t totErased = 0;
-
-  totErased = pruneLockedCollectionsVector<SequencedTag>(d_maps, maxCached, cacheSize);
-
+  uint64_t totErased = pruneLockedCollectionsVector<SequencedTag>(d_maps);
   *d_statnumentries -= totErased;
+
   DLOG(g_log<<"Done with cache clean, cacheSize: "<<*d_statnumentries<<", totErased"<<totErased<<endl);
 }
 
index 9b748e8e60bb0344d96b9894ba7f42f4f18285de..18e5ea147494a65d3ec505ff993aab50e1276d58 100644 (file)
@@ -112,39 +112,25 @@ template <typename S, typename T> void moveCacheItemToBack(T& collection, typena
   moveCacheItemToFrontOrBack<S>(collection, iter, false);
 }
 
-template <typename S, typename T> uint64_t pruneLockedCollectionsVector(vector<T>& maps, uint64_t maxCached, uint64_t cacheSize)
+template <typename S, typename T> uint64_t pruneLockedCollectionsVector(vector<T>& maps)
 {
-  time_t now = time(nullptr);
   uint64_t totErased = 0;
-  uint64_t toTrim = 0;
-  uint64_t lookAt = 0;
-
-  // two modes - if toTrim is 0, just look through 10%  of the cache and nuke everything that is expired
-  // otherwise, scan first 5*toTrim records, and stop once we've nuked enough
-  if (maxCached && cacheSize > maxCached) {
-    toTrim = cacheSize - maxCached;
-    lookAt = 5 * toTrim;
-  } else {
-    lookAt = cacheSize / 10;
-  }
+  time_t now = time(nullptr);
 
   for(auto& mc : maps) {
     WriteLock wl(&mc.d_mut);
+
+    uint64_t lookAt = (mc.d_map.size() + 9) / 10; // Look at 10% of this shard
+    uint64_t erased = 0;
+
     auto& sidx = boost::multi_index::get<S>(mc.d_map);
-    uint64_t erased = 0, lookedAt = 0;
-    for(auto i = sidx.begin(); i != sidx.end(); lookedAt++) {
-      if (i->ttd < now) {
+    for(auto i = sidx.begin(); i != sidx.end() && lookAt > 0; lookAt--) {
+      if(i->ttd < now) {
         i = sidx.erase(i);
         erased++;
       } else {
         ++i;
       }
-
-      if(toTrim && erased > toTrim / maps.size())
-        break;
-
-      if(lookedAt > lookAt / maps.size())
-        break;
     }
     totErased += erased;
   }