From: Otto Moerbeek Date: Wed, 20 Mar 2024 12:08:39 +0000 (+0100) Subject: Use a single NOD / UDR DB, sahred by all threads X-Git-Tag: rec-5.1.0-alpha1~77^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=053757b1693443d624be383a74e3466ce33f8d6e;p=thirdparty%2Fpdns.git Use a single NOD / UDR DB, sahred by all threads --- diff --git a/pdns/recursordist/nod.hh b/pdns/recursordist/nod.hh index bc5c8c26be..4e6d1e7d7d 100644 --- a/pdns/recursordist/nod.hh +++ b/pdns/recursordist/nod.hh @@ -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& 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& 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); }; } diff --git a/pdns/recursordist/pdns_recursor.cc b/pdns/recursordist/pdns_recursor.cc index fc22f0dfbd..5ba50b7f23 100644 --- a/pdns/recursordist/pdns_recursor.cc +++ b/pdns/recursordist/pdns_recursor.cc @@ -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, diff --git a/pdns/recursordist/rec-main.cc b/pdns/recursordist/rec-main.cc index e652ddb0cc..075fbec059 100644 --- a/pdns/recursordist/rec-main.cc +++ b/pdns/recursordist/rec-main.cc @@ -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 t_nodDBp; -thread_local std::shared_ptr t_udrDBp; +std::unique_ptr g_nodDBp; +std::unique_ptr g_udrDBp; #endif /* NOD_ENABLED */ std::atomic 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(num_cells); + g_nodDBp = std::make_unique(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(num_cells); + g_udrDBp = std::make_unique(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 { diff --git a/pdns/recursordist/rec-main.hh b/pdns/recursordist/rec-main.hh index 9fc1d96bc8..bf83b1b985 100644 --- a/pdns/recursordist/rec-main.hh +++ b/pdns/recursordist/rec-main.hh @@ -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 t_nodDBp; -extern thread_local std::shared_ptr t_udrDBp; +extern std::unique_ptr g_nodDBp; +extern std::unique_ptr g_udrDBp; #endif struct ProtobufServersInfo