From: Remi Gacogne Date: Mon, 10 May 2021 16:19:14 +0000 (+0200) Subject: rec: Move the NOD code to LockGuarded X-Git-Tag: dnsdist-1.7.0-alpha1~62^2~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e837d8d8142a19d7e83d147c8aed0c504d7b0694;p=thirdparty%2Fpdns.git rec: Move the NOD code to LockGuarded --- diff --git a/pdns/nod.cc b/pdns/nod.cc index cc2e9de73c..07d3928238 100644 --- a/pdns/nod.cc +++ b/pdns/nod.cc @@ -88,7 +88,7 @@ bool PersistentSBF::init(bool ignore_pid) { infile.open(filename, std::ios::in | std::ios::binary); g_log << Logger::Warning << "Found SBF file " << filename << endl; // read the file into the sbf - d_sbf.restore(infile); + d_sbf.lock()->restore(infile); infile.close(); // now dump it out again with new thread id & process id snapshotCurrent(std::this_thread::get_id()); @@ -142,8 +142,7 @@ bool PersistentSBF::snapshotCurrent(std::thread::id tid) std::stringstream iss; { // only lock while dumping to a stringstream - std::lock_guard lock(d_sbf_mutex); - d_sbf.dump(iss); + d_sbf.lock()->dump(iss); } // Now write it out to the file std::string ftmp = f.string() + ".XXXXXXXX"; diff --git a/pdns/nod.hh b/pdns/nod.hh index 0def55606b..e6ecf6c251 100644 --- a/pdns/nod.hh +++ b/pdns/nod.hh @@ -25,6 +25,7 @@ #include #include #include "dnsname.hh" +#include "lock.hh" #include "stable-bloom.hh" namespace nod { @@ -42,31 +43,28 @@ namespace nod { // Synchronization (at the instance level) is needed when snapshotting class PersistentSBF { public: - PersistentSBF() : d_sbf{c_fp_rate, c_num_cells, c_num_dec} {} - PersistentSBF(uint32_t num_cells) : d_sbf{c_fp_rate, num_cells, c_num_dec} {} + PersistentSBF() : d_sbf(bf::stableBF(c_fp_rate, c_num_cells, c_num_dec)) {} + PersistentSBF(uint32_t num_cells) : d_sbf(bf::stableBF(c_fp_rate, num_cells, c_num_dec)) {} bool init(bool ignore_pid=false); void setPrefix(const std::string& prefix) { d_prefix = prefix; } // Added to filenames in cachedir void setCacheDir(const std::string& cachedir); bool snapshotCurrent(std::thread::id tid); // Write the current file out to disk void add(const std::string& data) { // The only time this should block is when snapshotting - std::lock_guard lock(d_sbf_mutex); - d_sbf.add(data); + d_sbf.lock()->add(data); } - bool test(const std::string& data) { return d_sbf.test(data); } + bool test(const std::string& data) { return d_sbf.lock()->test(data); } bool testAndAdd(const std::string& data) { // The only time this should block is when snapshotting - std::lock_guard lock(d_sbf_mutex); - return d_sbf.testAndAdd(data); + return d_sbf.lock()->testAndAdd(data); } private: void remove_tmp_files(const boost::filesystem::path&, std::lock_guard&); bool d_init{false}; - bf::stableBF d_sbf; // Stable Bloom Filter + LockGuarded d_sbf; // Stable Bloom Filter std::string d_cachedir; std::string d_prefix = sbf_prefix; - std::mutex d_sbf_mutex; // Per-instance mutex for snapshots static std::mutex d_cachedir_mutex; // One mutex for all instances of this class }; diff --git a/pdns/recursordist/stable-bloom.hh b/pdns/recursordist/stable-bloom.hh index 947e92ed0c..ee5c7b4375 100644 --- a/pdns/recursordist/stable-bloom.hh +++ b/pdns/recursordist/stable-bloom.hh @@ -61,7 +61,7 @@ public: d_cells.set(i % d_num_cells); } } - bool test(const std::string& data) + bool test(const std::string& data) const { auto hashes = hash(data); for (auto& i : hashes) { @@ -161,7 +161,7 @@ private: } // This is a double hash implementation returning an array of // k hashes - std::vector hash(const std::string& data) + std::vector hash(const std::string& data) const { uint32_t h1, h2; MurmurHash3_x86_32(data.c_str(), data.length(), 1, (void*)&h1);