]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
rec: Move the NOD code to LockGuarded
authorRemi Gacogne <remi.gacogne@powerdns.com>
Mon, 10 May 2021 16:19:14 +0000 (18:19 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 17 Aug 2021 12:10:34 +0000 (14:10 +0200)
pdns/nod.cc
pdns/nod.hh
pdns/recursordist/stable-bloom.hh

index cc2e9de73cdcfc0236e7f02d54f5fc3ca92356c6..07d39282385c9c8544e814caa1c61ebfa4f65982 100644 (file)
@@ -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<std::mutex> 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";
index 0def55606b89618f3e0fe8b4609388252bb6978d..e6ecf6c2513f8ebaa5657b29bfb8e4daaf64f871 100644 (file)
@@ -25,6 +25,7 @@
 #include <thread>
 #include <boost/filesystem.hpp>
 #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<std::mutex> 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<std::mutex> 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<std::mutex>&);
 
     bool d_init{false};
-    bf::stableBF d_sbf; // Stable Bloom Filter
+    LockGuarded<bf::stableBF> 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
   };
 
index 947e92ed0ce2b1803b01b3d667e49aaad4fe4782..ee5c7b4375848d03e45bad226266f111ce6abc29 100644 (file)
@@ -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<uint32_t> hash(const std::string& data)
+  std::vector<uint32_t> hash(const std::string& data) const
   {
     uint32_t h1, h2;
     MurmurHash3_x86_32(data.c_str(), data.length(), 1, (void*)&h1);