From 3d82e6b7f33f71f19f2f186bc4afd46a3f12f843 Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Wed, 12 Jun 2024 11:22:48 +0200 Subject: [PATCH] dnsdist: Move carbon settings to the new configuration --- pdns/dnsdistdist/dnsdist-carbon.cc | 42 ++++++++--------------- pdns/dnsdistdist/dnsdist-carbon.hh | 18 +++------- pdns/dnsdistdist/dnsdist-configuration.hh | 2 ++ pdns/dnsdistdist/dnsdist-lua.cc | 17 +++++---- pdns/dnsdistdist/dnsdist.cc | 2 +- 5 files changed, 32 insertions(+), 49 deletions(-) diff --git a/pdns/dnsdistdist/dnsdist-carbon.cc b/pdns/dnsdistdist/dnsdist-carbon.cc index a0f493d8f3b9..76b35e8f5131 100644 --- a/pdns/dnsdistdist/dnsdist-carbon.cc +++ b/pdns/dnsdistdist/dnsdist-carbon.cc @@ -37,8 +37,6 @@ namespace dnsdist { -LockGuarded 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::max() ? static_cast(interval) : 30}; } -void Carbon::run() +void Carbon::run(const std::vector& 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; } } diff --git a/pdns/dnsdistdist/dnsdist-carbon.hh b/pdns/dnsdistdist/dnsdist-carbon.hh index 0fb04d4f0335..e0162a69c360 100644 --- a/pdns/dnsdistdist/dnsdist-carbon.hh +++ b/pdns/dnsdistdist/dnsdist-carbon.hh @@ -24,10 +24,8 @@ #include "config.h" #ifndef DISABLE_CARBON - -#include +#include #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 d_endpoints; - bool d_running{false}; - }; - - static LockGuarded 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& endpoints); + static void addEndpointAtRuntime(const Endpoint& endpoint); }; } diff --git a/pdns/dnsdistdist/dnsdist-configuration.hh b/pdns/dnsdistdist/dnsdist-configuration.hh index 91a41f208232..c21845b567d1 100644 --- a/pdns/dnsdistdist/dnsdist-configuration.hh +++ b/pdns/dnsdistdist/dnsdist-configuration.hh @@ -28,6 +28,7 @@ #include #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 d_carbonEndpoints; std::map> d_pools; std::shared_ptr d_webPassword; std::shared_ptr d_webAPIKey; diff --git a/pdns/dnsdistdist/dnsdist-lua.cc b/pdns/dnsdistdist/dnsdist-lua.cc index c4e3ac95f6a7..7f154dbfc687 100644 --- a/pdns/dnsdistdist/dnsdist-lua.cc +++ b/pdns/dnsdistdist/dnsdist-lua.cc @@ -1260,12 +1260,17 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) #ifndef DISABLE_CARBON luaCtx.writeFunction("carbonServer", [](const std::string& address, boost::optional ourName, boost::optional interval, boost::optional namespace_name, boost::optional 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::max()) ? static_cast(*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 */ diff --git a/pdns/dnsdistdist/dnsdist.cc b/pdns/dnsdistdist/dnsdist.cc index 09bd364f88aa..410310e52703 100644 --- a/pdns/dnsdistdist/dnsdist.cc +++ b/pdns/dnsdistdist/dnsdist.cc @@ -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);