]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Disable deprecated DynBlock methods when DISABLE_DEPRECATED_DYNBLOCK is...
authorRemi Gacogne <remi.gacogne@powerdns.com>
Fri, 12 Nov 2021 14:59:13 +0000 (15:59 +0100)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Wed, 22 Dec 2021 08:30:44 +0000 (09:30 +0100)
pdns/dnsdist-lua-inspection.cc

index 451682a97ea629810de563f17bceb430f89ef94f..a365698c5acd0a07e1125c9d72e842e134cb5f5e 100644 (file)
@@ -28,7 +28,7 @@
 
 #include "statnode.hh"
 
-static std::unordered_map<unsigned int, vector<boost::variant<string,double>>> getGenResponses(unsigned int top, boost::optional<int> labels, std::function<bool(const Rings::Response&)> pred)
+static std::vector<std::pair<int, vector<boost::variant<string,double>>>> getGenResponses(uint64_t top, boost::optional<int> labels, std::function<bool(const Rings::Response&)> pred)
 {
   setLuaNoSideEffect();
   map<DNSName, unsigned int> counts;
@@ -69,25 +69,29 @@ static std::unordered_map<unsigned int, vector<boost::variant<string,double>>> g
          return b.first < a.first;
        });
 
-  std::unordered_map<unsigned int, vector<boost::variant<string,double>>> ret;
-  unsigned int count=1, rest=0;
+  std::vector<std::pair<int, vector<boost::variant<string,double>>>> ret;
+  ret.reserve(std::min(rcounts.size(), top + 1U));
+  uint64_t count = 1;
+  unsigned int rest = 0;
   for(const auto& rc : rcounts) {
     if(count==top+1)
       rest+=rc.first;
     else
-      ret.insert({count++, {rc.second.toString(), rc.first, 100.0*rc.first/total}});
+      ret.push_back({count++, {rc.second.toString(), rc.first, 100.0*rc.first/total}});
   }
 
   if (total > 0) {
-    ret.insert({count, {"Rest", rest, 100.0*rest/total}});
+    ret.push_back({count, {"Rest", rest, 100.0*rest/total}});
   }
   else {
-    ret.insert({count, {"Rest", rest, 100.0 }});
+    ret.push_back({count, {"Rest", rest, 100.0 }});
   }
 
   return ret;
 }
 
+#ifndef DISABLE_DEPRECATED_DYNBLOCK
+
 typedef std::unordered_map<ComboAddress, unsigned int, ComboAddress::addressOnlyHash, ComboAddress::addressOnlyEqual> counts_t;
 
 static counts_t filterScore(const counts_t& counts,
@@ -105,10 +109,9 @@ static counts_t filterScore(const counts_t& counts,
   return ret;
 }
 
-
 typedef std::function<void(const StatNode&, const StatNode::Stat&, const StatNode::Stat&)> statvisitor_t;
 
-static void statNodeRespRing(statvisitor_t visitor, unsigned int seconds)
+static void statNodeRespRing(statvisitor_t visitor, uint64_t seconds)
 {
   struct timespec cutoff, now;
   gettime(&now);
@@ -135,22 +138,23 @@ static void statNodeRespRing(statvisitor_t visitor, unsigned int seconds)
       visitor(*node_, self, children);},  node);
 }
 
-static vector<pair<unsigned int, std::unordered_map<string,string> > > getRespRing(boost::optional<int> rcode)
+static vector<pair<int, std::vector<std::pair<string,string> > > > getRespRing(boost::optional<int> rcode)
 {
-  typedef std::unordered_map<string,string>  entry_t;
-  vector<pair<unsigned int, entry_t > > ret;
+  typedef std::vector<std::pair<string,string>> entry_t;
+  vector<pair<int, entry_t > > ret;
 
   for (const auto& shard : g_rings.d_shards) {
     auto rl = shard->respRing.lock();
 
-    entry_t e;
-    unsigned int count=1;
-    for(const auto& c : *rl) {
-      if(rcode && (rcode.get() != c.dh.rcode))
+    int count = 1;
+    for (const auto& c : *rl) {
+      if (rcode && (rcode.get() != c.dh.rcode)) {
         continue;
-      e["qname"]=c.name.toString();
-      e["rcode"]=std::to_string(c.dh.rcode);
-      ret.emplace_back(count, e);
+      }
+      entry_t e;
+      e.push_back({ "qname", c.name.toString() });
+      e.push_back({ "rcode", std::to_string(c.dh.rcode) });
+      ret.emplace_back(count, std::move(e));
       count++;
     }
   }
@@ -232,9 +236,11 @@ static counts_t exceedRespByterate(unsigned int rate, int seconds)
                   });
 }
 
+#endif /* DISABLE_DEPRECATED_DYNBLOCK */
+
 void setupLuaInspection(LuaContext& luaCtx)
 {
-  luaCtx.writeFunction("topClients", [](boost::optional<unsigned int> top_) {
+  luaCtx.writeFunction("topClients", [](boost::optional<uint64_t> top_) {
       setLuaNoSideEffect();
       auto top = top_.get_value_or(10);
       map<ComboAddress, unsigned int,ComboAddress::addressOnlyLessThan > counts;
@@ -268,7 +274,7 @@ void setupLuaInspection(LuaContext& luaCtx)
       g_outputBuffer += (fmt % (count) % "Rest" % rest % (total > 0 ? 100.0*rest/total : 100.0)).str();
     });
 
-  luaCtx.writeFunction("getTopQueries", [](unsigned int top, boost::optional<int> labels) {
+  luaCtx.writeFunction("getTopQueries", [](uint64_t top, boost::optional<int> labels) {
       setLuaNoSideEffect();
       map<DNSName, unsigned int> counts;
       unsigned int total=0;
@@ -352,21 +358,21 @@ void setupLuaInspection(LuaContext& luaCtx)
       return ret;
     });
 
-  luaCtx.writeFunction("getTopResponses", [](unsigned int top, unsigned int kind, boost::optional<int> labels) {
+  luaCtx.writeFunction("getTopResponses", [](uint64_t top, uint64_t kind, boost::optional<int> labels) {
       return getGenResponses(top, labels, [kind](const Rings::Response& r) { return r.dh.rcode == kind; });
     });
 
   luaCtx.executeCode(R"(function topResponses(top, kind, labels) top = top or 10; kind = kind or 0; for k,v in ipairs(getTopResponses(top, kind, labels)) do show(string.format("%4d  %-40s %4d %4.1f%%",k,v[1],v[2],v[3])) end end)");
 
 
-  luaCtx.writeFunction("getSlowResponses", [](unsigned int top, unsigned int msec, boost::optional<int> labels) {
+  luaCtx.writeFunction("getSlowResponses", [](uint64_t top, uint64_t msec, boost::optional<int> labels) {
       return getGenResponses(top, labels, [msec](const Rings::Response& r) { return r.usec > msec*1000; });
     });
 
 
   luaCtx.executeCode(R"(function topSlow(top, msec, labels) top = top or 10; msec = msec or 500; for k,v in ipairs(getSlowResponses(top, msec, labels)) do show(string.format("%4d  %-40s %4d %4.1f%%",k,v[1],v[2],v[3])) end end)");
 
-  luaCtx.writeFunction("getTopBandwidth", [](unsigned int top) {
+  luaCtx.writeFunction("getTopBandwidth", [](uint64_t top) {
       setLuaNoSideEffect();
       return g_rings.getTopBandwidth(top);
     });
@@ -710,6 +716,7 @@ void setupLuaInspection(LuaContext& luaCtx)
       }
     });
 
+#ifndef DISABLE_DEPRECATED_DYNBLOCK
   luaCtx.writeFunction("exceedServFails", [](unsigned int rate, int seconds) {
       setLuaNoSideEffect();
       return exceedRCode(rate, seconds, RCode::ServFail);
@@ -755,9 +762,10 @@ void setupLuaInspection(LuaContext& luaCtx)
   luaCtx.registerMember("drops", &StatNode::Stat::drops);
   luaCtx.registerMember("bytes", &StatNode::Stat::bytes);
 
-  luaCtx.writeFunction("statNodeRespRing", [](statvisitor_t visitor, boost::optional<unsigned int> seconds) {
-      statNodeRespRing(visitor, seconds ? *seconds : 0);
+  luaCtx.writeFunction("statNodeRespRing", [](statvisitor_t visitor, boost::optional<uint64_t> seconds) {
+      statNodeRespRing(visitor, seconds ? *seconds : 0U);
     });
+#endif /* DISABLE_DEPRECATED_DYNBLOCK */
 
   /* DynBlockRulesGroup */
   luaCtx.writeFunction("dynBlockRulesGroup", []() { return std::make_shared<DynBlockRulesGroup>(); });