From: Oliver Chen Date: Mon, 23 Jun 2025 06:15:06 +0000 (+0000) Subject: dnsdist: support server state change lua callback X-Git-Tag: dnsdist-2.1.0-alpha0~8^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f348f4ebb8254958ca157a54bf1f3d8d76e4ee70;p=thirdparty%2Fpdns.git dnsdist: support server state change lua callback --- diff --git a/pdns/dnsdistdist/dnsdist-backend.cc b/pdns/dnsdistdist/dnsdist-backend.cc index 6664084fd6..ab32818a20 100644 --- a/pdns/dnsdistdist/dnsdist-backend.cc +++ b/pdns/dnsdistdist/dnsdist-backend.cc @@ -817,6 +817,7 @@ void DownstreamState::submitHealthCheckResult(bool initial, bool newResult) updateNextLazyHealthCheck(*stats, false); } } + handleServerStateChange(getNameWithAddr(), newResult); return; } @@ -891,6 +892,7 @@ void DownstreamState::submitHealthCheckResult(bool initial, bool newResult) if (g_snmpAgent != nullptr && dnsdist::configuration::getImmutableConfiguration().d_snmpTrapsEnabled) { g_snmpAgent->sendBackendStatusChangeTrap(*this); } + handleServerStateChange(getNameWithAddr(), newResult); } } diff --git a/pdns/dnsdistdist/dnsdist-lua-hooks.cc b/pdns/dnsdistdist/dnsdist-lua-hooks.cc index 45cc25e1e6..25718ab417 100644 --- a/pdns/dnsdistdist/dnsdist-lua-hooks.cc +++ b/pdns/dnsdistdist/dnsdist-lua-hooks.cc @@ -9,9 +9,11 @@ namespace dnsdist::lua::hooks using ExitCallback = std::function; using MaintenanceCallback = std::function; using TicketsKeyAddedHook = std::function; +using ServerStateChangeCallback = std::function; static LockGuarded> s_exitCallbacks; static LockGuarded> s_maintenanceHooks; +static LockGuarded> s_serverStateChangeHooks; void runMaintenanceHooks(const LuaContext& context) { @@ -65,6 +67,25 @@ static void setTicketsKeyAddedHook(const LuaContext& context, const TicketsKeyAd }); } +void runServerStateChangeHooks(const LuaContext& context, const std::string& nameWithAddr, bool newState) +{ + (void)context; + for (const auto& callback : *(s_serverStateChangeHooks.lock())) { + callback(nameWithAddr, newState); + } +} + +static void addServerStateChangeCallback(const LuaContext& context, ServerStateChangeCallback callback) +{ + (void)context; + s_serverStateChangeHooks.lock()->push_back(std::move(callback)); +} + +void clearServerStateChangeCallbacks() +{ + s_serverStateChangeHooks.lock()->clear(); +} + void setupLuaHooks(LuaContext& luaCtx) { luaCtx.writeFunction("addMaintenanceCallback", [&luaCtx](const MaintenanceCallback& callback) { @@ -79,6 +100,10 @@ void setupLuaHooks(LuaContext& luaCtx) setLuaSideEffect(); setTicketsKeyAddedHook(luaCtx, hook); }); + luaCtx.writeFunction("addServerStateChangeCallback", [&luaCtx](const ServerStateChangeCallback& hook) { + setLuaSideEffect(); + addServerStateChangeCallback(luaCtx, hook); + }); } } diff --git a/pdns/dnsdistdist/dnsdist-lua-hooks.hh b/pdns/dnsdistdist/dnsdist-lua-hooks.hh index a1b8c3ca8f..abd0f29161 100644 --- a/pdns/dnsdistdist/dnsdist-lua-hooks.hh +++ b/pdns/dnsdistdist/dnsdist-lua-hooks.hh @@ -22,6 +22,7 @@ #pragma once #include +#include class LuaContext; @@ -31,5 +32,6 @@ void runMaintenanceHooks(const LuaContext& context); void clearMaintenanceHooks(); void runExitCallbacks(const LuaContext& context); void clearExitCallbacks(); +void runServerStateChangeHooks(const LuaContext& context, const std::string& nameWithAddr, bool newState); void setupLuaHooks(LuaContext& luaCtx); } diff --git a/pdns/dnsdistdist/dnsdist.cc b/pdns/dnsdistdist/dnsdist.cc index d89c590ec3..9d760f842d 100644 --- a/pdns/dnsdistdist/dnsdist.cc +++ b/pdns/dnsdistdist/dnsdist.cc @@ -1596,6 +1596,17 @@ bool handleTimeoutResponseRules(const std::vector& rules, InternalQueryState& ids, const std::shared_ptr& ds, const std::shared_ptr& sender); +void handleServerStateChange(const std::string& nameWithAddr, bool newResult); diff --git a/pdns/dnsdistdist/docs/reference/config.rst b/pdns/dnsdistdist/docs/reference/config.rst index a3934abd28..db7315cba9 100644 --- a/pdns/dnsdistdist/docs/reference/config.rst +++ b/pdns/dnsdistdist/docs/reference/config.rst @@ -2236,6 +2236,23 @@ Other functions end addMaintenanceCallback(myCallback) +.. function:: addServerStateChangeCallback(callback) + + .. versionadded:: 2.0.0 + + Register a Lua function to be called when a server state changed during the health check process. + The function should not block for a long period of time, as it would otherwise delay the execution of the other functions registered for this hook, as well as the execution of the health check process. + + :param function callback: The function to be called. It returns no value and takes two parameters, first parameter is a string with format as the same as return from :func:`Server:getNameWithAddr()` to identify the server, second parameter is a bool value indicating server state is up if true else down. + + .. code-block:: lua + + function serverStateChanged(nameAddr, newState) + if newState then state = 'up' else state = 'down' end + print(string.format('Server State Changed: %s -> %s', nameAddr, state)) + end + addServerStateChangeCallback(serverStateChanged) + .. function:: getAddressInfo(hostname, callback) diff --git a/pdns/dnsdistdist/test-dnsdist_cc.cc b/pdns/dnsdistdist/test-dnsdist_cc.cc index ebe7142554..286f3b42ae 100644 --- a/pdns/dnsdistdist/test-dnsdist_cc.cc +++ b/pdns/dnsdistdist/test-dnsdist_cc.cc @@ -74,6 +74,13 @@ bool handleTimeoutResponseRules(const std::vector