]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Use a single NOD / UDR DB, sahred by all threads
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Wed, 20 Mar 2024 12:08:39 +0000 (13:08 +0100)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Wed, 20 Mar 2024 12:08:39 +0000 (13:08 +0100)
pdns/recursordist/nod.hh
pdns/recursordist/pdns_recursor.cc
pdns/recursordist/rec-main.cc
pdns/recursordist/rec-main.hh

index bc5c8c26be65122e364cd1270d5941bee567daff..4e6d1e7d7d167974607cfc082477144c84590add 100644 (file)
@@ -33,7 +33,7 @@ namespace nod
 const float c_fp_rate = 0.01;
 const size_t c_num_cells = 67108864;
 const uint8_t c_num_dec = 10;
-const unsigned int snapshot_interval_default = 600;
+  const unsigned int snapshot_interval_default = 30; // XXX 600;
 const std::string bf_suffix = "bf";
 const std::string sbf_prefix = "sbf";
 
@@ -96,15 +96,11 @@ public:
   void setSnapshotInterval(unsigned int secs) { d_snapshot_interval = secs; }
   void setCacheDir(const std::string& cachedir) { d_psbf.setCacheDir(cachedir); }
   bool snapshotCurrent(std::thread::id tid) { return d_psbf.snapshotCurrent(tid); }
-  static void startHousekeepingThread(const std::shared_ptr<NODDB>& noddbp, std::thread::id tid)
-  {
-    noddbp->housekeepingThread(tid);
-  }
+  void housekeepingThread(std::thread::id tid);
 
 private:
   PersistentSBF d_psbf;
   unsigned int d_snapshot_interval{snapshot_interval_default}; // Number seconds between snapshots
-  void housekeepingThread(std::thread::id tid);
 };
 
 class UniqueResponseDB
@@ -123,15 +119,11 @@ public:
   void setSnapshotInterval(unsigned int secs) { d_snapshot_interval = secs; }
   void setCacheDir(const std::string& cachedir) { d_psbf.setCacheDir(cachedir); }
   bool snapshotCurrent(std::thread::id tid) { return d_psbf.snapshotCurrent(tid); }
-  static void startHousekeepingThread(const std::shared_ptr<UniqueResponseDB>& udrdbp, std::thread::id tid)
-  {
-    udrdbp->housekeepingThread(tid);
-  }
+  void housekeepingThread(std::thread::id tid);
 
 private:
   PersistentSBF d_psbf;
   unsigned int d_snapshot_interval{snapshot_interval_default}; // Number seconds between snapshots
-  void housekeepingThread(std::thread::id tid);
 };
 
 }
index fc22f0dfbdb438adb08eb1b78e1ee403be4c69c2..5ba50b7f232867889347f9183f730849ddb25fea 100644 (file)
@@ -603,7 +603,7 @@ static bool nodCheckNewDomain(Logr::log_t nodlogger, const DNSName& dname)
   // First check the (sub)domain isn't ignored for NOD purposes
   if (!g_nodDomainWL.check(dname)) {
     // Now check the NODDB (note this is probabilistic so can have FNs/FPs)
-    if (t_nodDBp && t_nodDBp->isNewDomain(dname)) {
+    if (g_nodDBp && g_nodDBp->isNewDomain(dname)) {
       if (g_nodLog) {
         // This should probably log to a dedicated log file
         SLOG(g_log << Logger::Notice << "Newly observed domain nod=" << dname << endl,
@@ -644,7 +644,7 @@ static bool udrCheckUniqueDNSRecord(Logr::log_t nodlogger, const DNSName& dname,
     // Create a string that represent a triplet of (qname, qtype and RR[type, name, content])
     std::stringstream strStream;
     strStream << dname.toDNSStringLC() << ":" << qtype << ":" << qtype << ":" << record.d_type << ":" << record.d_name.toDNSStringLC() << ":" << record.getContent()->getZoneRepresentation();
-    if (t_udrDBp && t_udrDBp->isUniqueResponse(strStream.str())) {
+    if (g_udrDBp && g_udrDBp->isUniqueResponse(strStream.str())) {
       if (g_udrLog) {
         // This should also probably log to a dedicated file.
         SLOG(g_log << Logger::Notice << "Unique response observed: qname=" << dname << " qtype=" << QType(qtype) << " rrtype=" << QType(record.d_type) << " rrname=" << record.d_name << " rrcontent=" << record.getContent()->getZoneRepresentation() << endl,
index e652ddb0cc7ff970cffd51adf6712ec711fe39cf..075fbec059f0eaf46b976e9069ccecb49ae1b3ab 100644 (file)
@@ -79,8 +79,8 @@ std::string g_nod_pbtag;
 bool g_udrEnabled;
 bool g_udrLog;
 std::string g_udr_pbtag;
-thread_local std::shared_ptr<nod::NODDB> t_nodDBp;
-thread_local std::shared_ptr<nod::UniqueResponseDB> t_udrDBp;
+std::unique_ptr<nod::NODDB> g_nodDBp;
+std::unique_ptr<nod::UniqueResponseDB> g_udrDBp;
 #endif /* NOD_ENABLED */
 
 std::atomic<bool> statsWanted;
@@ -782,40 +782,44 @@ static void setupNODThread(Logr::log_t log)
 {
   if (g_nodEnabled) {
     uint32_t num_cells = ::arg().asNum("new-domain-db-size");
-    t_nodDBp = std::make_shared<nod::NODDB>(num_cells);
+    g_nodDBp = std::make_unique<nod::NODDB>(num_cells);
     try {
-      t_nodDBp->setCacheDir(::arg()["new-domain-history-dir"]);
+      g_nodDBp->setCacheDir(::arg()["new-domain-history-dir"]);
     }
     catch (const PDNSException& e) {
       SLOG(g_log << Logger::Error << "new-domain-history-dir (" << ::arg()["new-domain-history-dir"] << ") is not readable or does not exist" << endl,
            log->error(Logr::Error, e.reason, "new-domain-history-dir is not readable or does not exists", "dir", Logging::Loggable(::arg()["new-domain-history-dir"])));
       _exit(1);
     }
-    if (!t_nodDBp->init()) {
+    if (!g_nodDBp->init()) {
       SLOG(g_log << Logger::Error << "Could not initialize domain tracking" << endl,
            log->info(Logr::Error, "Could not initialize domain tracking"));
       _exit(1);
     }
-    std::thread thread(nod::NODDB::startHousekeepingThread, t_nodDBp, std::this_thread::get_id());
+    std::thread thread([tid = std::this_thread::get_id()]() {
+      g_nodDBp->housekeepingThread(tid);
+    });
     thread.detach();
   }
   if (g_udrEnabled) {
     uint32_t num_cells = ::arg().asNum("unique-response-db-size");
-    t_udrDBp = std::make_shared<nod::UniqueResponseDB>(num_cells);
+    g_udrDBp = std::make_unique<nod::UniqueResponseDB>(num_cells);
     try {
-      t_udrDBp->setCacheDir(::arg()["unique-response-history-dir"]);
+      g_udrDBp->setCacheDir(::arg()["unique-response-history-dir"]);
     }
     catch (const PDNSException& e) {
       SLOG(g_log << Logger::Error << "unique-response-history-dir (" << ::arg()["unique-response-history-dir"] << ") is not readable or does not exist" << endl,
            log->info(Logr::Error, "unique-response-history-dir is not readable or does not exist", "dir", Logging::Loggable(::arg()["unique-response-history-dir"])));
       _exit(1);
     }
-    if (!t_udrDBp->init()) {
+    if (!g_udrDBp->init()) {
       SLOG(g_log << Logger::Error << "Could not initialize unique response tracking" << endl,
            log->info(Logr::Error, "Could not initialize unique response tracking"));
       _exit(1);
     }
-    std::thread thread(nod::UniqueResponseDB::startHousekeepingThread, t_udrDBp, std::this_thread::get_id());
+    std::thread thread([tid = std::this_thread::get_id()]() {
+      g_udrDBp->housekeepingThread(tid);
+    });
     thread.detach();
   }
 }
@@ -2253,6 +2257,10 @@ static int serviceMain(Logr::log_t log)
     return ret;
   }
 
+#ifdef NOD_ENABLED
+  setupNODThread(log);
+#endif /* NOD_ENABLED */
+
   return RecThreadInfo::runThreads(log);
 }
 
@@ -2729,12 +2737,6 @@ static void recursorThread()
       }
     }
 
-#ifdef NOD_ENABLED
-    if (threadInfo.isWorker()) {
-      setupNODThread(log);
-    }
-#endif /* NOD_ENABLED */
-
     /* the listener threads handle TCP queries */
     if (threadInfo.isWorker() || threadInfo.isListener()) {
       try {
index 9fc1d96bc8f0be2808a89688f2b116ab16ed2c90..bf83b1b985d4c6d228db746e7a6c83704a0375a5 100644 (file)
@@ -247,8 +247,8 @@ extern std::string g_nod_pbtag;
 extern bool g_udrEnabled;
 extern bool g_udrLog;
 extern std::string g_udr_pbtag;
-extern thread_local std::shared_ptr<nod::NODDB> t_nodDBp;
-extern thread_local std::shared_ptr<nod::UniqueResponseDB> t_udrDBp;
+extern std::unique_ptr<nod::NODDB> g_nodDBp;
+extern std::unique_ptr<nod::UniqueResponseDB> g_udrDBp;
 #endif
 
 struct ProtobufServersInfo