]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
wip2
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Wed, 4 Dec 2019 14:35:40 +0000 (15:35 +0100)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Wed, 4 Mar 2020 09:27:18 +0000 (10:27 +0100)
pdns/auth-packetcache.hh
pdns/auth-querycache.hh
pdns/cachecleaner.hh
pdns/pdns_recursor.cc
pdns/rec_channel_rec.cc
pdns/recursor_cache.cc
pdns/recursor_cache.hh
pdns/syncres.hh

index bc3c2f35bd8075b47db12ce0d106f1254ab0b6ba..0d3af47a251ef2cc7f85373a86001b424178f8e6 100644 (file)
@@ -89,11 +89,6 @@ private:
     uint32_t hash{0};
     uint16_t qtype{0};
     bool tcp{false};
-
-    time_t getTTD() const
-    {
-      return ttd;
-    }
   };
 
   struct HashTag{};
@@ -150,9 +145,4 @@ private:
   bool d_cleanskipped{false};
 
   static const unsigned int s_mincleaninterval=1000, s_maxcleaninterval=300000;
-
-public:
-  void preRemoval(const CacheEntry&)
-  {
-  }
 };
index 00465ad73e1338bcac90bef2380a52d7a484821b..889634bb60dd88dc8a7abc469ca9404df53bed02 100644 (file)
@@ -68,11 +68,6 @@ private:
     mutable time_t ttd{0};
     uint16_t qtype{0};
     int zoneID{-1};
-    
-    time_t getTTD() const
-    {
-      return ttd;
-    }
   };
 
   struct HashTag{};
@@ -131,9 +126,4 @@ private:
   bool d_cleanskipped{false};
 
   static const unsigned int s_mincleaninterval=1000, s_maxcleaninterval=300000;
-  
-public:
-  void preRemoval(const CacheEntry&)
-  {
-  }
 };
index 9d57ede00981097b133f98b3adcdfba59eeeed82..a4dfe8863d9ced3f2837fc199a346b360092dfd7 100644 (file)
@@ -133,7 +133,7 @@ template <typename S, typename T> uint64_t pruneLockedCollectionsVector(vector<T
     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->getTTD() < now) {
+      if (i->ttd < now) {
         i = sidx.erase(i);
         erased++;
       } else {
@@ -177,6 +177,7 @@ template <typename S, typename C, typename T> uint64_t pruneMutexCollectionsVect
         container.preRemoval(*i);
         i = sidx.erase(i);
         erased++;
+        mc.d_entriesCount--;
       } else {
         ++i;
       }
@@ -206,6 +207,7 @@ template <typename S, typename C, typename T> uint64_t pruneMutexCollectionsVect
       auto i = sidx.begin();
       container.preRemoval(*i);
       i = sidx.erase(i);
+      mc.d_entriesCount--;
       totErased++;
       toTrim--;
       if (toTrim == 0)
index d8a09f3af6fc04766dd1114e41497ac9bf86ab3a..b54a91fbe482ff9748f14921d1ee383df88f3673 100644 (file)
@@ -2844,12 +2844,13 @@ static void doStats(void)
   static time_t lastOutputTime;
   static uint64_t lastQueryCount;
 
-  uint64_t cacheHits = broadcastAccFunction<uint64_t>(pleaseGetCacheHits);
-  uint64_t cacheMisses = broadcastAccFunction<uint64_t>(pleaseGetCacheMisses);
+  uint64_t cacheHits = s_RC->cacheHits;
+  uint64_t cacheMisses = s_RC->cacheMisses;
+  uint64_t cacheSize = s_RC->size();
 
   if(g_stats.qcounter && (cacheHits + cacheMisses) && SyncRes::s_queries && SyncRes::s_outqueries) {
     g_log<<Logger::Notice<<"stats: "<<g_stats.qcounter<<" questions, "<<
-      broadcastAccFunction<uint64_t>(pleaseGetCacheSize)<< " cache entries, "<<
+      cacheSize << " cache entries, "<<
       broadcastAccFunction<uint64_t>(pleaseGetNegCacheSize)<<" negative entries, "<<
       (int)((cacheHits*100.0)/(cacheHits+cacheMisses))<<"% cache hits"<<endl;
 
@@ -2893,8 +2894,9 @@ static void doStats(void)
 
 static void houseKeeping(void *)
 {
-  static thread_local time_t last_rootupdate, last_secpoll, last_trustAnchorUpdate{0};
-  static thread_local timeval last_prune;
+  static thread_local time_t last_rootupdate, last_secpoll, last_trustAnchorUpdate{0}, last_RC_prune;
+  static thread_local struct timeval last_prune;
+
   static thread_local int cleanCounter=0;
   static thread_local bool s_running;  // houseKeeping can get suspended in secpoll, and be restarted, which makes us do duplicate work
   auto luaconfsLocal = g_luaconfs.getLocal();
@@ -2916,7 +2918,6 @@ static void houseKeeping(void *)
     past.tv_sec -= 5;
     if (last_prune < past) {
       t_packetCache->doPruneTo(g_maxPacketCacheEntries / g_numWorkerThreads);
-
       SyncRes::pruneNegCache(g_maxCacheEntries / (g_numWorkerThreads * 10));
 
       time_t limit;
@@ -2932,15 +2933,19 @@ static void houseKeeping(void *)
       Utility::gettimeofday(&last_prune, nullptr);
     }
 
-    if(now.tv_sec - last_rootupdate > 7200) {
-      int res = SyncRes::getRootNS(g_now, nullptr);
-      if (!res) {
-        last_rootupdate=now.tv_sec;
-        primeRootNSZones(g_dnssecmode != DNSSECMode::Off);
-      }
-    }
-
     if(isHandlerThread()) {
+      if (now.tv_sec - last_RC_prune > 5) {
+        s_RC->doPrune(g_maxCacheEntries);
+        last_RC_prune = now.tv_sec;
+      }
+      // XXX !!! global
+      if(now.tv_sec - last_rootupdate > 7200) {
+        int res = SyncRes::getRootNS(g_now, nullptr);
+        if (!res) {
+          last_rootupdate=now.tv_sec;
+          primeRootNSZones(g_dnssecmode != DNSSECMode::Off);
+        }
+      }
 
       if(now.tv_sec - last_secpoll >= 3600) {
        try {
index b09f3b6d7cb981916e77ba8e0f68521bf6a9c1d6..8829be099d969dcf1c78c089492b193c2f0bab44 100644 (file)
@@ -213,7 +213,7 @@ static uint64_t dumpNegCache(NegCache& negcache, int fd)
 
 static uint64_t* pleaseDump(int fd)
 {
-  return new uint64_t(s_RC->doDump(fd) + dumpNegCache(SyncRes::t_sstorage.negcache, fd) + t_packetCache->doDump(fd));
+  return new uint64_t(dumpNegCache(SyncRes::t_sstorage.negcache, fd) + t_packetCache->doDump(fd));
 }
 
 static uint64_t* pleaseDumpEDNSMap(int fd)
@@ -281,7 +281,7 @@ static string doDumpCache(T begin, T end)
     return "Error opening dump file for writing: "+stringerror()+"\n";
   uint64_t total = 0;
   try {
-    total = broadcastAccFunction<uint64_t>(boost::bind(pleaseDump, fd));
+    total = s_RC->doDump(fd) + broadcastAccFunction<uint64_t>(boost::bind(pleaseDump, fd));
   }
   catch(...){}
   
@@ -932,19 +932,9 @@ static uint64_t getConcurrentQueries()
   return broadcastAccFunction<uint64_t>(pleaseGetConcurrentQueries);
 }
 
-uint64_t* pleaseGetCacheSize()
-{
-  return new uint64_t(s_RC ? s_RC->size() : 0);
-}
-
-static uint64_t* pleaseGetCacheBytes()
-{
-  return new uint64_t(s_RC ? s_RC->bytes() : 0);
-}
-
 static uint64_t doGetCacheSize()
 {
-  return broadcastAccFunction<uint64_t>(pleaseGetCacheSize);
+  return s_RC->size();
 }
 
 static uint64_t doGetAvgLatencyUsec()
@@ -954,27 +944,17 @@ static uint64_t doGetAvgLatencyUsec()
 
 static uint64_t doGetCacheBytes()
 {
-  return broadcastAccFunction<uint64_t>(pleaseGetCacheBytes);
-}
-
-uint64_t* pleaseGetCacheHits()
-{
-  return new uint64_t(s_RC->cacheHits);
+  return s_RC->bytes();
 }
 
 static uint64_t doGetCacheHits()
 {
-  return broadcastAccFunction<uint64_t>(pleaseGetCacheHits);
-}
-
-uint64_t* pleaseGetCacheMisses()
-{
-  return new uint64_t(s_RC->cacheMisses);
+  return s_RC->cacheHits;
 }
 
 static uint64_t doGetCacheMisses()
 {
-  return broadcastAccFunction<uint64_t>(pleaseGetCacheMisses);
+  return s_RC->cacheMisses;
 }
 
 uint64_t* pleaseGetPacketCacheSize()
index cfe65173e94d687c09b3456a937ea0e27586ed83..3464b7f756b6b1f1cf45c39ee5d7b1e56fae29ef 100644 (file)
@@ -36,8 +36,7 @@ size_t MemRecursorCache::size()
   // XXX!
   size_t count = 0;
   for (auto& map : d_maps) {
-    const std::lock_guard<std::mutex> lock(map.mutex);
-    count += map.d_map.size();
+    count += map.d_entriesCount;
   }
   return count;
 }
@@ -286,6 +285,7 @@ void MemRecursorCache::replace(time_t now, const DNSName &qname, const QType& qt
   cache_t::iterator stored = map.d_map.find(key);
   if (stored == map.d_map.end()) {
     stored = map.d_map.insert(CacheEntry(key, auth)).first;
+    map.d_entriesCount++;
     isNew = true;
   }
 
@@ -362,21 +362,22 @@ size_t MemRecursorCache::doWipeCache(const DNSName& name, bool sub, uint16_t qty
   size_t count = 0;
 
   if (!sub) {
-    for (auto& map : d_maps) {
-      const std::lock_guard<std::mutex> lock(map.mutex);
-      map.d_cachecachevalid = false;
-      auto& idx = map.d_map.get<NameOnlyHashedTag>();
-      count += idx.erase(name);
-      if (qtype == 0xffff) {
-        auto& ecsIdx = map.d_ecsIndex.get<OrderedTag>();
-        auto ecsIndexRange = ecsIdx.equal_range(name);
-        ecsIdx.erase(ecsIndexRange.first, ecsIndexRange.second);
-      }
-      else {
-        auto& ecsIdx = map.d_ecsIndex.get<HashedTag>();
-        auto ecsIndexRange = ecsIdx.equal_range(tie(name, qtype));
-        ecsIdx.erase(ecsIndexRange.first, ecsIndexRange.second);
-      }
+    auto& map = getMap(name);
+    const std::lock_guard<std::mutex> lock(map.mutex);
+    map.d_cachecachevalid = false;
+    auto& idx = map.d_map.get<NameOnlyHashedTag>();
+    size_t n = idx.erase(name);
+    count += n;
+    map.d_entriesCount -= n;
+    if (qtype == 0xffff) {
+      auto& ecsIdx = map.d_ecsIndex.get<OrderedTag>();
+      auto ecsIndexRange = ecsIdx.equal_range(name);
+      ecsIdx.erase(ecsIndexRange.first, ecsIndexRange.second);
+    }
+    else {
+      auto& ecsIdx = map.d_ecsIndex.get<HashedTag>();
+      auto ecsIndexRange = ecsIdx.equal_range(tie(name, qtype));
+      ecsIdx.erase(ecsIndexRange.first, ecsIndexRange.second);
     }
   }
   else {
@@ -390,6 +391,7 @@ size_t MemRecursorCache::doWipeCache(const DNSName& name, bool sub, uint16_t qty
         if (i->d_qtype == qtype || qtype == 0xffff) {
           count++;
           i = idx.erase(i);
+          map.d_entriesCount--;
         } else {
           ++i;
         }
index 48bcbb29548e02caba02574d46c51916df420aa2..db5bd4766e45402ffac0c9bfa960edf7d75ab8cf 100644 (file)
@@ -202,8 +202,9 @@ private:
     ecsIndex_t d_ecsIndex;
     DNSName d_cachedqname;
     std::pair<MemRecursorCache::NameOnlyHashedTagIterator_t, MemRecursorCache::NameOnlyHashedTagIterator_t> d_cachecache;
-    bool d_cachecachevalid{false};
     std::mutex mutex;
+    bool d_cachecachevalid{false};
+    std::atomic<uint64_t> d_entriesCount{0};
   };
   
   vector<MapCombo> d_maps;
index ae9187a906ec4a698d05c337814bbdd20311994c..b8021447d7ac9ec90719f94de515d3a9243db2b3 100644 (file)
@@ -1091,10 +1091,7 @@ std::shared_ptr<SyncRes::domainmap_t> parseAuthAndForwards();
 uint64_t* pleaseGetNsSpeedsSize();
 uint64_t* pleaseGetFailedServersSize();
 uint64_t* pleaseGetEDNSStatusesSize();
-uint64_t* pleaseGetCacheSize();
 uint64_t* pleaseGetNegCacheSize();
-uint64_t* pleaseGetCacheHits();
-uint64_t* pleaseGetCacheMisses();
 uint64_t* pleaseGetConcurrentQueries();
 uint64_t* pleaseGetThrottleSize();
 uint64_t* pleaseGetPacketCacheHits();