Skip to content

Commit

Permalink
dnsdist: Move LB policy and pools 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 a465144 commit 7ffd13a
Show file tree
Hide file tree
Showing 15 changed files with 202 additions and 187 deletions.
3 changes: 1 addition & 2 deletions pdns/dnsdistdist/dnsdist-carbon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,7 @@ static bool doOneCarbonExport(const Carbon::Endpoint& endpoint)
}
}

auto localPools = g_pools.getLocal();
for (const auto& entry : *localPools) {
for (const auto& entry : dnsdist::configuration::getCurrentRuntimeConfiguration().d_pools) {
string poolName = entry.first;
boost::replace_all(poolName, ".", "_");
if (poolName.empty()) {
Expand Down
5 changes: 5 additions & 0 deletions pdns/dnsdistdist/dnsdist-configuration.hh
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ public:
}
};

class ServerPolicy;
struct ServerPool;

namespace dnsdist::configuration
{
/* when we add EDNS to a query, we don't want to advertise
Expand Down Expand Up @@ -178,6 +181,8 @@ struct Configuration
a RCU-like mechanism */
struct RuntimeConfiguration
{
std::map<std::string, std::shared_ptr<ServerPool>> d_pools;
std::shared_ptr<ServerPolicy> d_lbPolicy;
NetmaskGroup d_ACL;
NetmaskGroup d_proxyProtocolACL;
NetmaskGroup d_consoleACL;
Expand Down
10 changes: 4 additions & 6 deletions pdns/dnsdistdist/dnsdist-discovery.cc
Original file line number Diff line number Diff line change
Expand Up @@ -448,14 +448,13 @@ bool ServiceDiscovery::tryToUpgradeBackend(const UpgradeableBackend& backend)

infolog("Added automatically upgraded server %s", newServer->getNameWithAddr());

auto localPools = g_pools.getCopy();
if (!newServer->d_config.pools.empty()) {
for (const auto& poolName : newServer->d_config.pools) {
addServerToPool(localPools, poolName, newServer);
addServerToPool(poolName, newServer);
}
}
else {
addServerToPool(localPools, "", newServer);
addServerToPool("", newServer);
}

newServer->start();
Expand All @@ -472,17 +471,16 @@ bool ServiceDiscovery::tryToUpgradeBackend(const UpgradeableBackend& backend)
}

for (const string& poolName : backend.d_ds->d_config.pools) {
removeServerFromPool(localPools, poolName, backend.d_ds);
removeServerFromPool(poolName, backend.d_ds);
}
/* the server might also be in the default pool */
removeServerFromPool(localPools, "", backend.d_ds);
removeServerFromPool("", backend.d_ds);
}

std::stable_sort(states.begin(), states.end(), [](const decltype(newServer)& a, const decltype(newServer)& b) {
return a->d_config.order < b->d_config.order;
});

g_pools.setState(localPools);
g_dstates.setState(states);
if (!backend.keepAfterUpgrade) {
backend.d_ds->stop();
Expand Down
50 changes: 27 additions & 23 deletions pdns/dnsdistdist/dnsdist-lbpolicies.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
#include "dolog.hh"
#include "dns_random.hh"

GlobalStateHolder<ServerPolicy> g_policy;

static constexpr size_t s_staticArrayCutOff = 16;
template <typename T> using DynamicIndexArray = std::vector<std::pair<T, size_t>>;
template <typename T> using StaticIndexArray = std::array<std::pair<T, size_t>, s_staticArrayCutOff>;
Expand Down Expand Up @@ -258,31 +256,37 @@ shared_ptr<DownstreamState> roundrobin(const ServerPolicy::NumberedServerVector&
return servers.at(candidates.at((counter++) % candidates.size()) - 1).second;
}

const std::shared_ptr<const ServerPolicy::NumberedServerVector> getDownstreamCandidates(const pools_t& pools, const std::string& poolName)
const std::shared_ptr<const ServerPolicy::NumberedServerVector> getDownstreamCandidates(const std::string& poolName)
{
std::shared_ptr<ServerPool> pool = getPool(pools, poolName);
std::shared_ptr<ServerPool> pool = getPool(poolName);
return pool->getServers();
}

std::shared_ptr<ServerPool> createPoolIfNotExists(pools_t& pools, const string& poolName)
std::shared_ptr<ServerPool> createPoolIfNotExists(const string& poolName)
{
std::shared_ptr<ServerPool> pool;
pools_t::iterator it = pools.find(poolName);
if (it != pools.end()) {
pool = it->second;
{
const auto& pools = dnsdist::configuration::getCurrentRuntimeConfiguration().d_pools;
const auto it = pools.find(poolName);
if (it != pools.end()) {
return it->second;
}
}
else {
if (!poolName.empty())
vinfolog("Creating pool %s", poolName);
pool = std::make_shared<ServerPool>();
pools.insert(std::pair<std::string, std::shared_ptr<ServerPool> >(poolName, pool));

if (!poolName.empty()) {
vinfolog("Creating pool %s", poolName);
}

auto pool = std::make_shared<ServerPool>();
dnsdist::configuration::updateRuntimeConfiguration([&poolName,&pool](dnsdist::configuration::RuntimeConfiguration& config) {
config.d_pools.emplace(poolName, pool);
});

return pool;
}

void setPoolPolicy(pools_t& pools, const string& poolName, std::shared_ptr<ServerPolicy> policy)
void setPoolPolicy(const string& poolName, std::shared_ptr<ServerPolicy> policy)
{
std::shared_ptr<ServerPool> pool = createPoolIfNotExists(pools, poolName);
std::shared_ptr<ServerPool> pool = createPoolIfNotExists(poolName);
if (!poolName.empty()) {
vinfolog("Setting pool %s server selection policy to %s", poolName, policy->getName());
} else {
Expand All @@ -291,9 +295,9 @@ void setPoolPolicy(pools_t& pools, const string& poolName, std::shared_ptr<Serve
pool->policy = std::move(policy);
}

void addServerToPool(pools_t& pools, const string& poolName, std::shared_ptr<DownstreamState> server)
void addServerToPool(const string& poolName, std::shared_ptr<DownstreamState> server)
{
std::shared_ptr<ServerPool> pool = createPoolIfNotExists(pools, poolName);
std::shared_ptr<ServerPool> pool = createPoolIfNotExists(poolName);
if (!poolName.empty()) {
vinfolog("Adding server to pool %s", poolName);
} else {
Expand All @@ -302,9 +306,9 @@ void addServerToPool(pools_t& pools, const string& poolName, std::shared_ptr<Dow
pool->addServer(server);
}

void removeServerFromPool(pools_t& pools, const string& poolName, std::shared_ptr<DownstreamState> server)
void removeServerFromPool(const string& poolName, std::shared_ptr<DownstreamState> server)
{
std::shared_ptr<ServerPool> pool = getPool(pools, poolName);
std::shared_ptr<ServerPool> pool = getPool(poolName);

if (!poolName.empty()) {
vinfolog("Removing server from pool %s", poolName);
Expand All @@ -316,10 +320,10 @@ void removeServerFromPool(pools_t& pools, const string& poolName, std::shared_pt
pool->removeServer(server);
}

std::shared_ptr<ServerPool> getPool(const pools_t& pools, const std::string& poolName)
std::shared_ptr<ServerPool> getPool(const std::string& poolName)
{
pools_t::const_iterator it = pools.find(poolName);

const auto& pools = dnsdist::configuration::getCurrentRuntimeConfiguration().d_pools;
auto it = pools.find(poolName);
if (it == pools.end()) {
throw std::out_of_range("No pool named " + poolName);
}
Expand Down
18 changes: 9 additions & 9 deletions pdns/dnsdistdist/dnsdist-lbpolicies.hh
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public:
template <class T>
using NumberedVector = std::vector<std::pair<unsigned int, T>>;
using NumberedServerVector = NumberedVector<shared_ptr<DownstreamState>>;
typedef std::function<shared_ptr<DownstreamState>(const NumberedServerVector& servers, const DNSQuestion*)> policyfunc_t;
typedef std::function<unsigned int(dnsdist_ffi_servers_list_t* servers, dnsdist_ffi_dnsquestion_t* dq)> ffipolicyfunc_t;
using policyfunc_t = std::function<std::shared_ptr<DownstreamState>(const NumberedServerVector& servers, const DNSQuestion*)>;
using ffipolicyfunc_t = std::function<unsigned int(dnsdist_ffi_servers_list_t* servers, dnsdist_ffi_dnsquestion_t* dq)>;

ServerPolicy(const std::string& name_, policyfunc_t policy_, bool isLua_) :
d_name(name_), d_policy(std::move(policy_)), d_isLua(isLua_)
Expand Down Expand Up @@ -92,14 +92,14 @@ public:

struct ServerPool;

using pools_t = map<std::string, std::shared_ptr<ServerPool>>;
std::shared_ptr<ServerPool> getPool(const pools_t& pools, const std::string& poolName);
std::shared_ptr<ServerPool> createPoolIfNotExists(pools_t& pools, const string& poolName);
void setPoolPolicy(pools_t& pools, const string& poolName, std::shared_ptr<ServerPolicy> policy);
void addServerToPool(pools_t& pools, const string& poolName, std::shared_ptr<DownstreamState> server);
void removeServerFromPool(pools_t& pools, const string& poolName, std::shared_ptr<DownstreamState> server);
using pools_t = std::map<std::string, std::shared_ptr<ServerPool>>;
std::shared_ptr<ServerPool> getPool(const std::string& poolName);
std::shared_ptr<ServerPool> createPoolIfNotExists(const string& poolName);
void setPoolPolicy(const string& poolName, std::shared_ptr<ServerPolicy> policy);
void addServerToPool(const string& poolName, std::shared_ptr<DownstreamState> server);
void removeServerFromPool(const string& poolName, std::shared_ptr<DownstreamState> server);

const std::shared_ptr<const ServerPolicy::NumberedServerVector> getDownstreamCandidates(const map<std::string, std::shared_ptr<ServerPool>>& pools, const std::string& poolName);
const std::shared_ptr<const ServerPolicy::NumberedServerVector> getDownstreamCandidates(const std::string& poolName);

std::shared_ptr<DownstreamState> firstAvailable(const ServerPolicy::NumberedServerVector& servers, const DNSQuestion* dq);

Expand Down
8 changes: 2 additions & 6 deletions pdns/dnsdistdist/dnsdist-lua-bindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,11 @@ void setupLuaBindings(LuaContext& luaCtx, bool client, bool configCheck)
/* DownstreamState */
luaCtx.registerFunction<void (DownstreamState::*)(int)>("setQPS", [](DownstreamState& state, int lim) { state.qps = lim > 0 ? QPSLimiter(lim, lim) : QPSLimiter(); });
luaCtx.registerFunction<void (std::shared_ptr<DownstreamState>::*)(string)>("addPool", [](const std::shared_ptr<DownstreamState>& state, const string& pool) {
auto localPools = g_pools.getCopy();
addServerToPool(localPools, pool, state);
g_pools.setState(localPools);
addServerToPool( pool, state);
state->d_config.pools.insert(pool);
});
luaCtx.registerFunction<void (std::shared_ptr<DownstreamState>::*)(string)>("rmPool", [](const std::shared_ptr<DownstreamState>& state, const string& pool) {
auto localPools = g_pools.getCopy();
removeServerFromPool(localPools, pool, state);
g_pools.setState(localPools);
removeServerFromPool(pool, state);
state->d_config.pools.erase(pool);
});
luaCtx.registerFunction<uint64_t (DownstreamState::*)() const>("getOutstanding", [](const DownstreamState& state) { return state.outstanding.load(); });
Expand Down
12 changes: 6 additions & 6 deletions pdns/dnsdistdist/dnsdist-lua-ffi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1159,9 +1159,9 @@ size_t dnsdist_ffi_packetcache_get_domain_list_by_addr(const char* poolName, con
return 0;
}

const auto localPools = g_pools.getCopy();
auto it = localPools.find(poolName);
if (it == localPools.end()) {
const auto& pools = dnsdist::configuration::getCurrentRuntimeConfiguration().d_pools;
auto it = pools.find(poolName);
if (it == pools.end()) {
return 0;
}

Expand Down Expand Up @@ -1208,9 +1208,9 @@ size_t dnsdist_ffi_packetcache_get_address_list_by_domain(const char* poolName,
return 0;
}

const auto localPools = g_pools.getCopy();
auto it = localPools.find(poolName);
if (it == localPools.end()) {
const auto& pools = dnsdist::configuration::getCurrentRuntimeConfiguration().d_pools;
auto it = pools.find(poolName);
if (it == pools.end()) {
return 0;
}

Expand Down
Loading

0 comments on commit 7ffd13a

Please sign in to comment.