]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Account for size per shard
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Mon, 27 Feb 2023 10:29:46 +0000 (11:29 +0100)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Mon, 3 Apr 2023 11:31:47 +0000 (13:31 +0200)
pdns/recursordist/rec-main.cc
pdns/recursordist/rec_channel_rec.cc
pdns/recursordist/recpacketcache.cc
pdns/recursordist/recpacketcache.hh

index 5512d8ae859216fa7f65d9a5ca7c73a3bb483197..b514e7b2e054b4248c96126c81e9fb9aa2c5084d 100644 (file)
@@ -1447,7 +1447,6 @@ static int serviceMain(int argc, char* argv[], Logr::log_t log)
   g_maxNSEC3Iterations = ::arg().asNum("nsec3-max-iterations");
 
   g_maxCacheEntries = ::arg().asNum("max-cache-entries");
-  g_maxPacketCacheEntries = ::arg().asNum("max-packetcache-entries");
 
   luaConfigDelayedThreads delayedLuaThreads;
   try {
@@ -2185,7 +2184,6 @@ static void houseKeeping(void*)
       if (g_packetCache) {
         static PeriodicTask packetCacheTask{"packetCacheTask", 5};
         packetCacheTask.runIfDue(now, []() {
-          g_packetCache->setMaxSize(g_maxPacketCacheEntries); // g_maxPacketCacheEntries might have changed by rec_control
           g_packetCache->doPruneTo(g_maxPacketCacheEntries);
         });
       }
@@ -3036,8 +3034,8 @@ int main(int argc, char** argv)
     g_recCache = std::make_unique<MemRecursorCache>(::arg().asNum("record-cache-shards"));
     g_negCache = std::make_unique<NegCache>(::arg().asNum("record-cache-shards") / 8);
     if (!::arg().mustDo("disable-packetcache")) {
-      // Only enable packet cache for thread processing queries from the outside world
-      g_packetCache = std::make_unique<RecursorPacketCache>(g_maxPacketCacheEntries /*, shards */);
+      g_maxPacketCacheEntries = ::arg().asNum("max-packetcache-entries");
+      g_packetCache = std::make_unique<RecursorPacketCache>(g_maxPacketCacheEntries, ::arg().asNum("record-cache-shards")); // XXX
     }
 
     ret = serviceMain(argc, argv, startupLog);
index 4e82cf6041414312d74376db6fdb0abc780b28f4..f2a7d628e78dc453ee7f19c6a96bbccd382ea683 100644 (file)
@@ -844,6 +844,7 @@ static string setMaxPacketCacheEntries(T begin, T end)
   }
   try {
     g_maxPacketCacheEntries = pdns::checked_stoi<uint32_t>(*begin);
+    g_packetCache->setMaxSize(g_maxPacketCacheEntries);
     return "New max packetcache entries: " + std::to_string(g_maxPacketCacheEntries) + "\n";
   }
   catch (const std::exception& e) {
index 48fb138fc3f15e0494dc013b3d942cfb16ac83fb..184555ebd8e7b928e6042cce5de98e962163efc8 100644 (file)
 
 unsigned int RecursorPacketCache::s_refresh_ttlperc{0};
 
+void RecursorPacketCache::setShardSizes(size_t shardSize)
+{
+  for (auto& shard : d_maps) {
+    auto lock = shard.lock();
+    lock->d_shardSize = shardSize;
+  }
+}
+
 uint64_t RecursorPacketCache::size() const
 {
   uint64_t count = 0;
@@ -231,7 +239,7 @@ void RecursorPacketCache::insertResponsePacket(unsigned int tag, uint32_t qhash,
   shard->d_map.insert(entry);
   map.d_entriesCount++;
 
-  if (shard->d_map.size() > d_maxSize / d_maps.size()) {
+  if (shard->d_map.size() > shard->d_shardSize) {
     auto& seq_idx = shard->d_map.get<SequencedTag>();
     seq_idx.erase(seq_idx.begin());
     map.d_entriesCount--;
@@ -263,14 +271,16 @@ uint64_t RecursorPacketCache::doDump(int file)
   size_t shardNum = 0;
   size_t min = std::numeric_limits<size_t>::max();
   size_t max = 0;
+  uint64_t maxSize = 0;
 
   for (auto& shard : d_maps) {
     auto lock = shard.lock();
     const auto& sidx = lock->d_map.get<SequencedTag>();
     const auto shardSize = lock->d_map.size();
-    fprintf(filePtr.get(), "; packetcache shard %zu; size %zu\n", shardNum, shardSize);
+    fprintf(filePtr.get(), "; packetcache shard %zu; size %zu/%zu\n", shardNum, shardSize, lock->d_shardSize);
     min = std::min(min, shardSize);
     max = std::max(max, shardSize);
+    maxSize += lock->d_shardSize;
     shardNum++;
     for (const auto& entry : sidx) {
       count++;
@@ -282,6 +292,6 @@ uint64_t RecursorPacketCache::doDump(int file)
       }
     }
   }
-  fprintf(filePtr.get(), "; packetcache size: %" PRIu64 "/%zu shards: %zu min/max shard size: %zu/%zu\n", size(), d_maxSize, d_maps.size(), min, max);
+  fprintf(filePtr.get(), "; packetcache size: %" PRIu64 "/%" PRIu64 " shards: %zu min/max shard size: %zu/%zu\n", size(), maxSize, d_maps.size(), min, max);
   return count;
 }
index 10a005b8975c35bc66237f4d1767ea666a0bd2bc..be7611f93dbd2f8864626776af5ca7ecbe7ea7be 100644 (file)
@@ -57,12 +57,9 @@ public:
   using OptPBData = boost::optional<PBData>;
 
   RecursorPacketCache(size_t maxsize, size_t shards = 1024) :
-    d_maps(shards),
-    d_maxSize(maxsize)
+    d_maps(shards)
   {
-    if (d_maxSize / d_maps.size() == 0) {
-      d_maxSize = d_maps.size();
-    }
+    setMaxSize(maxsize);
   }
 
   bool getResponsePacket(unsigned int tag, const std::string& queryPacket, time_t now,
@@ -86,13 +83,16 @@ public:
   bool getResponsePacket(unsigned int tag, const std::string& queryPacket, DNSName& qname, uint16_t* qtype, uint16_t* qclass, time_t now, std::string* responsePacket, uint32_t* age, vState* valState, uint32_t* qhash, OptPBData* pbdata, bool tcp);
 
   void insertResponsePacket(unsigned int tag, uint32_t qhash, std::string&& query, const DNSName& qname, uint16_t qtype, uint16_t qclass, std::string&& responsePacket, time_t now, uint32_t ttl, const vState& valState, OptPBData&& pbdata, bool tcp);
-  void doPruneTo(size_t maxSize = 250000);
+  void doPruneTo(size_t maxSize);
   uint64_t doDump(int file);
   uint64_t doWipePacketCache(const DNSName& name, uint16_t qtype = 0xffff, bool subtree = false);
 
   void setMaxSize(size_t size)
   {
-    d_maxSize = size;
+    if (size < d_maps.size()) {
+      size = d_maps.size();
+    }
+    setShardSizes(size / d_maps.size());
   }
 
   [[nodiscard]] uint64_t size() const;
@@ -162,6 +162,7 @@ private:
     struct LockedContent
     {
       packetCache_t d_map;
+      size_t d_shardSize{0};
       uint64_t d_hits{0};
       uint64_t d_misses{0};
       uint64_t d_contended_count{0};
@@ -205,11 +206,11 @@ private:
   //   return d_maps.at(combine(hash, hash, tcp) % d_maps.size());
   // }
 
-  size_t d_maxSize;
-
   static bool qrMatch(const packetCache_t::index<HashTag>::type::iterator& iter, const std::string& queryPacket, const DNSName& qname, uint16_t qtype, uint16_t qclass);
   bool checkResponseMatches(MapCombo::LockedContent& shard, std::pair<packetCache_t::index<HashTag>::type::iterator, packetCache_t::index<HashTag>::type::iterator> range, const std::string& queryPacket, const DNSName& qname, uint16_t qtype, uint16_t qclass, time_t now, std::string* responsePacket, uint32_t* age, vState* valState, OptPBData* pbdata);
 
+  void setShardSizes(size_t shardSize);
+
 public:
   void preRemoval(MapCombo::LockedContent& map, const Entry& entry)
   {