]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: consistent hash - compute hashes at object initialization
authorCharles-Henri Bruyand <charles-henri.bruyand@open-xchange.com>
Fri, 15 Jun 2018 08:52:29 +0000 (10:52 +0200)
committerCharles-Henri Bruyand <charles-henri.bruyand@open-xchange.com>
Fri, 15 Jun 2018 08:52:29 +0000 (10:52 +0200)
pdns/dnsdist-lua.cc
pdns/dnsdist.cc
pdns/dnsdist.hh

index 2355d503ccf571a057e3c92477a46bc96f8a0ea7..1b5967f721f4de59ff80c61298dc3f987e0fb0b2 100644 (file)
@@ -237,7 +237,7 @@ void setupLuaConfig(bool client)
                              return ret;
                            }
 
-                           ret->weight=weightVal;
+                           ret->setWeight(weightVal);
                          }
                          catch(std::exception& e) {
                            // std::stoi will throw an exception if the string isn't in a value int range
@@ -278,7 +278,7 @@ void setupLuaConfig(bool client)
                        }
 
                         if (vars.count("id")) {
-                          ret->id = boost::lexical_cast<boost::uuids::uuid>(boost::get<string>(vars["id"]));
+                          ret->setId(boost::lexical_cast<boost::uuids::uuid>(boost::get<string>(vars["id"])));
                         }
 
                        if(vars.count("checkName")) {
index d4bffecf98c4b4d93fe6382d7d984c773ea71a85..63789847af9d99f0a635230208a57d5a3019420a 100644 (file)
@@ -624,6 +624,29 @@ bool DownstreamState::reconnect()
 
   return connected;
 }
+void DownstreamState::hash()
+{
+  auto w = weight;
+  hashes.clear();
+  while (w > 0) {
+    std::string uuid = boost::str(boost::format("%s-%d") % id % w);
+    unsigned int wshash = burtleCI((const unsigned char*)uuid.c_str(), uuid.size(), g_hashperturb);
+    hashes.insert(wshash);
+    --w;
+  }
+}
+
+void DownstreamState::setId(const boost::uuids::uuid& newId)
+{
+  id = newId;
+  hash();
+}
+
+void DownstreamState::setWeight(int newWeight)
+{
+  weight = newWeight;
+  hash();
+}
 
 DownstreamState::DownstreamState(const ComboAddress& remote_, const ComboAddress& sourceAddr_, unsigned int sourceItf_, size_t numberOfSockets): remote(remote_), sourceAddr(sourceAddr_), sourceItf(sourceItf_)
 {
@@ -636,6 +659,7 @@ DownstreamState::DownstreamState(const ComboAddress& remote_, const ComboAddress
   for (auto& fd : sockets) {
     fd = -1;
   }
+  hash();
 
   if (!IsAnyAddress(remote)) {
     reconnect();
@@ -737,17 +761,6 @@ shared_ptr<DownstreamState> chashed(const NumberedServerVector& servers, const D
 
   for (const auto& d: servers) {
     if (d.second->isUp()) {
-      if (d.second->hashes.empty()) {
-        // computes server's points
-        // @todo check if server's weight can change over time
-        auto w = d.second->weight;
-        while (w > 0) {
-          std::string uuid = boost::str(boost::format("%s-%d") % d.second->id % w);
-          unsigned int wshash = burtleCI((const unsigned char*)uuid.c_str(), uuid.size(), g_hashperturb);
-          d.second->hashes.insert(wshash);
-          --w;
-        }
-      }
       for (const auto& h: d.second->hashes) {
         // put server's hashes on the circle
         circle.insert(std::make_pair(h, d.second));
index 0a035b6634079d22aac6b10d69a3913723057cdc..0681812df93e0886df532e22dcf3cd64bf7c9ae4 100644 (file)
@@ -618,6 +618,9 @@ struct DownstreamState
     return status;
   }
   bool reconnect();
+  void hash();
+  void setId(const boost::uuids::uuid& newId);
+  void setWeight(int newWeight);
 };
 using servers_t =vector<std::shared_ptr<DownstreamState>>;