]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
rec: Refactor cache-wiping code into a common function
authorKevin P. Fleming <kevin@km6g.us>
Fri, 24 Sep 2021 20:59:45 +0000 (16:59 -0400)
committerKevin P. Fleming <kevin@km6g.us>
Thu, 11 Nov 2021 14:46:34 +0000 (09:46 -0500)
Eliminates multiple copies of the code and eliminates
inconsistencies between them.

pdns/pdns_recursor.cc
pdns/rec_channel_rec.cc
pdns/reczones.cc
pdns/syncres.hh
pdns/ws-recursor.cc

index 9f00634dcee9e31ebf12323516495d3b8c014647..346b437f33cc584d06e72c6c838b83563abf0236 100644 (file)
@@ -4847,6 +4847,30 @@ static void checkSocketDir(void)
   _exit(1);
 }
 
+static uint64_t* pleaseWipePacketCache(const DNSName& canon, bool subtree, uint16_t qtype)
+{
+  return new uint64_t(t_packetCache->doWipePacketCache(canon, qtype, subtree));
+}
+
+struct WipeCacheResult wipeCaches(const DNSName& canon, bool subtree, uint16_t qtype)
+{
+  struct WipeCacheResult res;
+
+  try {
+    res.record_count = g_recCache->doWipeCache(canon, subtree, qtype);
+    res.packet_count = broadcastAccFunction<uint64_t>([=]{ return pleaseWipePacketCache(canon, subtree, qtype);});
+    res.negative_record_count = g_negCache->wipe(canon, subtree);
+    if (g_aggressiveNSECCache) {
+      g_aggressiveNSECCache->removeZoneInfo(canon, subtree);
+    }
+  }
+  catch (const std::exception& e) {
+    g_log<<Logger::Warning<<", failed: "<<e.what()<<endl;
+  }
+
+  return res;
+}
+
 static int serviceMain(int argc, char*argv[])
 {
   int ret = EXIT_SUCCESS;
index ea3e3caf6887c1791ba94a1392a73a10777c5628..3d74674d9283438652e7a8ac4541a9ed65537bec 100644 (file)
@@ -461,11 +461,6 @@ static RecursorControlChannel::Answer doDumpRPZ(int s, T begin, T end)
   return {0, "done\n"};
 }
 
-uint64_t* pleaseWipePacketCache(const DNSName& canon, bool subtree, uint16_t qtype)
-{
-  return new uint64_t(t_packetCache->doWipePacketCache(canon, qtype, subtree));
-}
-
 template <typename T>
 static string doWipeCache(T begin, T end, uint16_t qtype)
 {
@@ -492,12 +487,10 @@ static string doWipeCache(T begin, T end, uint16_t qtype)
   int count = 0, pcount = 0, countNeg = 0;
   for (auto wipe : toWipe) {
     try {
-      count += g_recCache->doWipeCache(wipe.first, wipe.second, qtype);
-      pcount += broadcastAccFunction<uint64_t>([=] { return pleaseWipePacketCache(wipe.first, wipe.second, qtype); });
-      countNeg += g_negCache->wipe(wipe.first, wipe.second);
-      if (g_aggressiveNSECCache) {
-        g_aggressiveNSECCache->removeZoneInfo(wipe.first, wipe.second);
-      }
+      auto res = wipeCaches(wipe.first, wipe.second, qtype);
+      count += res.record_count;
+      pcount += res.packet_count;
+      countNeg += res.negative_record_count;
     }
     catch (const std::exception& e) {
       g_log << Logger::Warning << ", failed: " << e.what() << endl;
@@ -614,12 +607,7 @@ static string doAddNTA(T begin, T end)
     lci.negAnchors[who] = why;
   });
   try {
-    g_recCache->doWipeCache(who, true, 0xffff);
-    broadcastAccFunction<uint64_t>([=] { return pleaseWipePacketCache(who, true, 0xffff); });
-    g_negCache->wipe(who, true);
-    if (g_aggressiveNSECCache) {
-      g_aggressiveNSECCache->removeZoneInfo(who, true);
-    }
+    wipeCaches(who, true, 0xffff);
   }
   catch (std::exception& e) {
     g_log << Logger::Warning << ", failed: " << e.what() << endl;
@@ -671,12 +659,7 @@ static string doClearNTA(T begin, T end)
       g_luaconfs.modify([entry](LuaConfigItems& lci) {
         lci.negAnchors.erase(entry);
       });
-      g_recCache->doWipeCache(entry, true, 0xffff);
-      broadcastAccFunction<uint64_t>([=] { return pleaseWipePacketCache(entry, true, 0xffff); });
-      g_negCache->wipe(entry, true);
-      if (g_aggressiveNSECCache) {
-        g_aggressiveNSECCache->removeZoneInfo(entry, true);
-      }
+      wipeCaches(entry, true, 0xffff);
       if (!first) {
         first = false;
         removed += ",";
@@ -737,12 +720,7 @@ static string doAddTA(T begin, T end)
       auto ds = std::dynamic_pointer_cast<DSRecordContent>(DSRecordContent::make(what));
       lci.dsAnchors[who].insert(*ds);
     });
-    g_recCache->doWipeCache(who, true, 0xffff);
-    broadcastAccFunction<uint64_t>([=] { return pleaseWipePacketCache(who, true, 0xffff); });
-    g_negCache->wipe(who, true);
-    if (g_aggressiveNSECCache) {
-      g_aggressiveNSECCache->removeZoneInfo(who, true);
-    }
+    wipeCaches(who, true, 0xffff);
     g_log << Logger::Warning << endl;
     return "Added Trust Anchor for " + who.toStringRootDot() + " with data " + what + "\n";
   }
@@ -787,12 +765,7 @@ static string doClearTA(T begin, T end)
       g_luaconfs.modify([entry](LuaConfigItems& lci) {
         lci.dsAnchors.erase(entry);
       });
-      g_recCache->doWipeCache(entry, true, 0xffff);
-      broadcastAccFunction<uint64_t>([=] { return pleaseWipePacketCache(entry, true, 0xffff); });
-      g_negCache->wipe(entry, true);
-      if (g_aggressiveNSECCache) {
-        g_aggressiveNSECCache->removeZoneInfo(entry, true);
-      }
+      wipeCaches(entry, true, 0xffff);
       if (!first) {
         first = false;
         removed += ",";
index 560f204f8989b4aa43f55735142ac82e7609823c..f5d417e3c8d73a17e33509b47c1d4174beef2d34 100644 (file)
@@ -354,9 +354,7 @@ string reloadAuthAndForwards()
     }
 
     for (const auto& i : oldAndNewDomains) {
-      g_recCache->doWipeCache(i, true, 0xffff);
-      broadcastAccFunction<uint64_t>([&] { return pleaseWipePacketCache(i, true, 0xffff); });
-      g_negCache->wipe(i, true);
+      wipeCaches(i, true, 0xffff);
     }
 
     broadcastFunction([=] { return pleaseUseNewSDomainsMap(newDomainMap); });
index 495dd5d33727519719a6de395e3cdced37e4d163..0506fdabf8b63aad8b9c430fd9db19146e417779 100644 (file)
@@ -1186,11 +1186,19 @@ uint64_t* pleaseGetConcurrentQueries();
 uint64_t* pleaseGetThrottleSize();
 uint64_t* pleaseGetPacketCacheHits();
 uint64_t* pleaseGetPacketCacheSize();
-uint64_t* pleaseWipePacketCache(const DNSName& canon, bool subtree, uint16_t qtype=0xffff);
 void doCarbonDump(void*);
 bool primeHints(time_t now = time(nullptr));
 void primeRootNSZones(bool, unsigned int depth);
 
+struct WipeCacheResult
+{
+  int record_count = 0;
+  int negative_record_count = 0;
+  int packet_count = 0;
+};
+
+struct WipeCacheResult wipeCaches(const DNSName& canon, bool subtree, uint16_t qtype);
+
 extern __thread struct timeval g_now;
 
 struct ThreadTimes
index def09184bad5b6148e2136f499c92074b2465cd1..3c2ad0c083d1b3cc818475b59cf3afc02643ff65 100644 (file)
@@ -395,11 +395,9 @@ static void apiServerCacheFlush(HttpRequest* req, HttpResponse* resp)
     qtype = QType::chartocode(req->getvars["type"].c_str());
   }
 
-  int count = g_recCache->doWipeCache(canon, subtree, qtype);
-  count += broadcastAccFunction<uint64_t>([=] { return pleaseWipePacketCache(canon, subtree, qtype); });
-  count += g_negCache->wipe(canon, subtree);
+  struct WipeCacheResult res = wipeCaches(canon, subtree, qtype);
   resp->setJsonBody(Json::object{
-    {"count", count},
+    {"count", res.record_count + res.packet_count + res.negative_record_count},
     {"result", "Flushed cache."}});
 }