]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Move carbon settings to the new configuration
authorRemi Gacogne <remi.gacogne@powerdns.com>
Wed, 12 Jun 2024 09:22:48 +0000 (11:22 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Mon, 15 Jul 2024 09:47:55 +0000 (11:47 +0200)
pdns/dnsdistdist/dnsdist-carbon.cc
pdns/dnsdistdist/dnsdist-carbon.hh
pdns/dnsdistdist/dnsdist-configuration.hh
pdns/dnsdistdist/dnsdist-lua.cc
pdns/dnsdistdist/dnsdist.cc

index a0f493d8f3b9950cfe5c10e690a7e1e8da3ebcca..76b35e8f5131a6b05706090411f8e1a16e2dd5ac 100644 (file)
@@ -37,8 +37,6 @@
 namespace dnsdist
 {
 
-LockGuarded<Carbon::Config> Carbon::s_config;
-
 static bool doOneCarbonExport(const Carbon::Endpoint& endpoint)
 {
   const auto& server = endpoint.server;
@@ -296,7 +294,7 @@ static bool doOneCarbonExport(const Carbon::Endpoint& endpoint)
   return true;
 }
 
-static void carbonHandler(Carbon::Endpoint&& endpoint)
+static void carbonHandler(Carbon::Endpoint endpoint)
 {
   setThreadName("dnsdist/carbon");
   const auto intervalUSec = endpoint.interval * 1000 * 1000;
@@ -337,41 +335,29 @@ static void carbonHandler(Carbon::Endpoint&& endpoint)
   }
 }
 
-bool Carbon::addEndpoint(Carbon::Endpoint&& endpoint)
+Carbon::Endpoint Carbon::newEndpoint(const std::string& address, std::string ourName, uint64_t interval, const std::string& namespace_name, const std::string& instance_name)
 {
-  if (endpoint.ourname.empty()) {
+  if (ourName.empty()) {
     try {
-      endpoint.ourname = getCarbonHostName();
+      ourName = getCarbonHostName();
     }
-    catch (const std::exception& e) {
-      throw std::runtime_error(std::string("The 'ourname' setting in 'carbonServer()' has not been set and we are unable to determine the system's hostname: ") + e.what());
+    catch (const std::exception& exp) {
+      throw std::runtime_error(std::string("The 'ourname' setting in 'carbonServer()' has not been set and we are unable to determine the system's hostname: ") + exp.what());
     }
   }
-
-  auto config = s_config.lock();
-  if (config->d_running) {
-    // we already started the threads, let's just spawn a new one
-    std::thread newHandler(carbonHandler, std::move(endpoint));
-    newHandler.detach();
-  }
-  else {
-    config->d_endpoints.push_back(std::move(endpoint));
-  }
-  return true;
+  return Carbon::Endpoint{ComboAddress(address, 2003),
+                          !namespace_name.empty() ? namespace_name : "dnsdist",
+                          ourName,
+                          !instance_name.empty() ? instance_name : "main",
+                          interval < std::numeric_limits<unsigned int>::max() ? static_cast<unsigned int>(interval) : 30};
 }
 
-void Carbon::run()
+void Carbon::run(const std::vector<Carbon::Endpoint>& endpoints)
 {
-  auto config = s_config.lock();
-  if (config->d_running) {
-    throw std::runtime_error("The carbon threads are already running");
-  }
-  for (auto& endpoint : config->d_endpoints) {
-    std::thread newHandler(carbonHandler, std::move(endpoint));
+  for (auto& endpoint : endpoints) {
+    std::thread newHandler(carbonHandler, endpoint);
     newHandler.detach();
   }
-  config->d_endpoints.clear();
-  config->d_running = true;
 }
 
 }
index 0fb04d4f03352d346e4dd38cbb4ff7c4e697bd21..e0162a69c360b7ac374ffb0b85415d978fed0cb1 100644 (file)
 #include "config.h"
 
 #ifndef DISABLE_CARBON
-
-#include <thread>
+#include <string>
 #include "iputils.hh"
-#include "lock.hh"
 
 namespace dnsdist
 {
@@ -43,17 +41,9 @@ public:
     unsigned int interval;
   };
 
-  static bool addEndpoint(Endpoint&& endpoint);
-  static void run();
-
-private:
-  struct Config
-  {
-    std::vector<Endpoint> d_endpoints;
-    bool d_running{false};
-  };
-
-  static LockGuarded<Config> s_config;
+  static Endpoint newEndpoint(const std::string& address, std::string ourName, uint64_t interval, const std::string& namespace_name, const std::string& instance_name);
+  static void run(const std::vector<Endpoint>& endpoints);
+  static void addEndpointAtRuntime(const Endpoint& endpoint);
 };
 
 }
index 91a41f208232fa05955a578fd924afe44753bbdd..c21845b567d19dee621011aa662695b241f6b105 100644 (file)
@@ -28,6 +28,7 @@
 #include <string>
 
 #include "credentials.hh"
+#include "dnsdist-carbon.hh"
 #include "dnsdist-query-count.hh"
 #include "dnsdist-rule-chains.hh"
 #include "iputils.hh"
@@ -196,6 +197,7 @@ struct RuntimeConfiguration
 {
   rules::RuleChains d_ruleChains;
   servers_t d_backends;
+  std::vector<dnsdist::Carbon::Endpoint> d_carbonEndpoints;
   std::map<std::string, std::shared_ptr<ServerPool>> d_pools;
   std::shared_ptr<const CredentialsHolder> d_webPassword;
   std::shared_ptr<const CredentialsHolder> d_webAPIKey;
index 8d05c3137ece14363c058d3cc30a95f7d24ff10c..9ccd8c905fd70f1b2c7d063344457b44eb7889b8 100644 (file)
@@ -1262,12 +1262,17 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck)
 #ifndef DISABLE_CARBON
   luaCtx.writeFunction("carbonServer", [](const std::string& address, boost::optional<string> ourName, boost::optional<uint64_t> interval, boost::optional<string> namespace_name, boost::optional<string> instance_name) {
     setLuaSideEffect();
-    dnsdist::Carbon::Endpoint endpoint{ComboAddress(address, 2003),
-                                       (namespace_name && !namespace_name->empty()) ? *namespace_name : "dnsdist",
-                                       ourName ? *ourName : "",
-                                       (instance_name && !instance_name->empty()) ? *instance_name : "main",
-                                       (interval && *interval < std::numeric_limits<unsigned int>::max()) ? static_cast<unsigned int>(*interval) : 30};
-    dnsdist::Carbon::addEndpoint(std::move(endpoint));
+    auto newEndpoint = dnsdist::Carbon::newEndpoint(address,
+                                                    (ourName ? *ourName : ""),
+                                                    (interval ? *interval : 30),
+                                                    (namespace_name ? *namespace_name : "dnsdist"),
+                                                    (instance_name ? *instance_name : "main"));
+    if (dnsdist::configuration::isConfigurationDone()) {
+      dnsdist::Carbon::run({newEndpoint});
+    }
+    dnsdist::configuration::updateRuntimeConfiguration([&newEndpoint](dnsdist::configuration::RuntimeConfiguration& config) {
+      config.d_carbonEndpoints.push_back(std::move(newEndpoint));
+    });
   });
 #endif /* DISABLE_CARBON */
 
index 489553b08dcc5ad2f4584f9b2f4cd9821876f431..b47a2d44e1e630c5d81009b788ddb046884a8ebc 100644 (file)
@@ -3535,7 +3535,7 @@ int main(int argc, char** argv)
     dnsdist::ServiceDiscovery::run();
 
 #ifndef DISABLE_CARBON
-    dnsdist::Carbon::run();
+    dnsdist::Carbon::run(dnsdist::configuration::getCurrentRuntimeConfiguration().d_carbonEndpoints);
 #endif /* DISABLE_CARBON */
 
     thread stattid(maintThread);