Skip to content

Commit

Permalink
dnsdist: Move carbon settings to the new configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
rgacogne committed Jul 5, 2024
1 parent 9490b67 commit 3d82e6b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 49 deletions.
42 changes: 14 additions & 28 deletions pdns/dnsdistdist/dnsdist-carbon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
namespace dnsdist
{

LockGuarded<Carbon::Config> Carbon::s_config;

static bool doOneCarbonExport(const Carbon::Endpoint& endpoint)
{
const auto& server = endpoint.server;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

}
Expand Down
18 changes: 4 additions & 14 deletions pdns/dnsdistdist/dnsdist-carbon.hh
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@
#include "config.h"

#ifndef DISABLE_CARBON

#include <thread>
#include <string>
#include "iputils.hh"
#include "lock.hh"

namespace dnsdist
{
Expand All @@ -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);
};

}
Expand Down
2 changes: 2 additions & 0 deletions pdns/dnsdistdist/dnsdist-configuration.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down
17 changes: 11 additions & 6 deletions pdns/dnsdistdist/dnsdist-lua.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 */

Expand Down
2 changes: 1 addition & 1 deletion pdns/dnsdistdist/dnsdist.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 3d82e6b

Please sign in to comment.