From: Remi Gacogne Date: Thu, 20 May 2021 07:12:25 +0000 (+0200) Subject: rec: Store the carbon configuration in a StateHolder X-Git-Tag: dnsdist-1.7.0-alpha1~62^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=58d7ad6bfb0d46b7617ca984b90feacae35752c8;p=thirdparty%2Fpdns.git rec: Store the carbon configuration in a StateHolder --- diff --git a/pdns/pdns_recursor.cc b/pdns/pdns_recursor.cc index 28470b55e2..f29d5a8285 100644 --- a/pdns/pdns_recursor.cc +++ b/pdns/pdns_recursor.cc @@ -5119,6 +5119,15 @@ static int serviceMain(int argc, char*argv[]) g_DoTToAuthNames.setState(std::move(dotauthNames)); } + { + CarbonConfig config; + stringtok(config.servers, arg()["carbon-server"], ", "); + config.hostname = arg()["carbon-server"]; + config.instance_name = arg()["carbon-instance"]; + config.namespace_name = arg()["carbon-namespace"]; + g_carbonConfig.setState(std::move(config)); + } + s_balancingFactor = ::arg().asDouble("distribution-load-factor"); if (s_balancingFactor != 0.0 && s_balancingFactor < 1.0) { s_balancingFactor = 0.0; diff --git a/pdns/rec-carbon.cc b/pdns/rec-carbon.cc index 479d12ace9..55604fdbed 100644 --- a/pdns/rec-carbon.cc +++ b/pdns/rec-carbon.cc @@ -9,44 +9,38 @@ #include "arguments.hh" #include "lock.hh" +GlobalStateHolder g_carbonConfig; void doCarbonDump(void*) +{ try { - string hostname; - string instance_name; - string namespace_name; - vector carbonServers; - - { - std::lock_guard l(g_carbon_config_lock); - stringtok(carbonServers, arg()["carbon-server"], ", "); - namespace_name=arg()["carbon-namespace"]; - hostname=arg()["carbon-ourname"]; - instance_name=arg()["carbon-instance"]; - } + static thread_local auto configHolder = g_carbonConfig.getLocal(); - if(carbonServers.empty()) + auto config = *configHolder; + if (config.servers.empty()) { return; + } - if(namespace_name.empty()) { - namespace_name="pdns"; + if (config.namespace_name.empty()) { + config.namespace_name = "pdns"; } - if (hostname.empty()) { + + if (config.hostname.empty()) { try { - hostname = getCarbonHostName(); + config.hostname = getCarbonHostName(); } catch(const std::exception& e) { throw std::runtime_error(std::string("The 'carbon-ourname' setting has not been set and we are unable to determine the system's hostname: ") + e.what()); } } - if(instance_name.empty()) { - instance_name="recursor"; + if (config.instance_name.empty()) { + config.instance_name = "recursor"; } registerAllStats(); PacketBuffer msg; - for(const auto& carbonServer: carbonServers) { + for (const auto& carbonServer: config.servers) { ComboAddress remote(carbonServer, 2003); Socket s(remote.sin4.sin_family, SOCK_STREAM); s.setNonBlocking(); @@ -62,7 +56,7 @@ try time_t now=time(0); for(const auto& val : all) { - str<close(); } } -catch(PDNSException& e) +catch (const PDNSException& e) { g_log< StatsMap; StatsMap getAllStatsMap(StatComponent component); -extern std::mutex g_carbon_config_lock; +struct CarbonConfig +{ + std::string hostname; + std::string instance_name; + std::string namespace_name; + std::vector servers; +}; + +extern GlobalStateHolder g_carbonConfig; + std::vector >* pleaseGetQueryRing(); std::vector >* pleaseGetServfailQueryRing(); std::vector >* pleaseGetBogusQueryRing(); diff --git a/pdns/rec_channel_rec.cc b/pdns/rec_channel_rec.cc index b8231e4ab5..5ad9c634b3 100644 --- a/pdns/rec_channel_rec.cc +++ b/pdns/rec_channel_rec.cc @@ -72,8 +72,6 @@ bool PrefixDashNumberCompare::operator()(const std::string& a, const std::string return aa < bb; } -std::mutex g_carbon_config_lock; - static map d_get32bitpointers; static map*> d_getatomics; static map> d_get64bitmembers; @@ -533,33 +531,42 @@ static string doWipeCache(T begin, T end, uint16_t qtype) template static string doSetCarbonServer(T begin, T end) { - std::lock_guard l(g_carbon_config_lock); - if(begin==end) { - ::arg().set("carbon-server").clear(); + auto config = g_carbonConfig.getCopy(); + if (begin == end) { + config.servers.clear(); + g_carbonConfig.setState(std::move(config)); return "cleared carbon-server setting\n"; } + string ret; - ::arg().set("carbon-server")=*begin; - ret="set carbon-server to '"+::arg()["carbon-server"]+"'\n"; + stringtok(config.servers, *begin, ", "); + ret = "set carbon-server to '" + *begin + "'\n"; + ++begin; - if(begin != end) { - ::arg().set("carbon-ourname")=*begin; - ret+="set carbon-ourname to '"+*begin+"'\n"; + if (begin != end) { + config.hostname = *begin; + ret += "set carbon-ourname to '" + *begin + "'\n"; } else { + g_carbonConfig.setState(std::move(config)); return ret; } + ++begin; - if(begin != end) { - ::arg().set("carbon-namespace")=*begin; - ret+="set carbon-namespace to '"+*begin+"'\n"; + if (begin != end) { + config.namespace_name = *begin; + ret += "set carbon-namespace to '" + *begin + "'\n"; } else { + g_carbonConfig.setState(std::move(config)); return ret; } + ++begin; - if(begin != end) { - ::arg().set("carbon-instance")=*begin; - ret+="set carbon-instance to '"+*begin+"'\n"; + if (begin != end) { + config.instance_name = *begin; + ret += "set carbon-instance to '" + *begin + "'\n"; } + + g_carbonConfig.setState(std::move(config)); return ret; }