Skip to content

Commit

Permalink
Merge pull request #14001 from rgacogne/ddist-ffi-policy-no-server
Browse files Browse the repository at this point in the history
dnsdist: Support "no server available" result from Lua FFI LB policies
  • Loading branch information
rgacogne authored Mar 29, 2024
2 parents b4b92fc + 467380e commit e472f31
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 126 deletions.
1 change: 0 additions & 1 deletion .not-formatted
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
./pdns/dnsdistdist/test-delaypipe_hh.cc
./pdns/dnsdistdist/test-dnsdistdynblocks_hh.cc
./pdns/dnsdistdist/test-dnsdistkvs_cc.cc
./pdns/dnsdistdist/test-dnsdistlbpolicies_cc.cc
./pdns/dnsdistdist/test-dnsdistrings_cc.cc
./pdns/dnsdistdist/test-dnsdistrules_cc.cc
./pdns/dnsdistdist/test-dnsdisttcp_cc.cc
Expand Down
5 changes: 5 additions & 0 deletions pdns/dnsdistdist/dnsdist-lbpolicies.cc
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,11 @@ std::shared_ptr<DownstreamState> ServerPolicy::getSelectedBackend(const ServerPo
selected = policy(&serversList, &dnsq);
}

if (selected >= servers.size()) {
/* invalid offset, meaning that there is no server available */
return {};
}

selectedBackend = servers.at(selected).second;
}
}
Expand Down
29 changes: 29 additions & 0 deletions pdns/dnsdistdist/docs/guides/serverselection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,25 @@ Or::

setServerPolicyLua("splitsetup", splitSetup)

A faster, FFI version is also available since 1.5.0:

.. code-block:: lua
local ffi = require("ffi")
local C = ffi.C
local counter = 0
function luaffiroundrobin(servers_list, dq)
counter = counter + 1
return (counter % tonumber(C.dnsdist_ffi_servers_list_get_count(servers_list)))
end
setServerPolicyLuaFFI("luaffiroundrobin", luaffiroundrobin)
Note that this version returns the index (starting at 0) of the server to select,
instead of returning the server itself. It was initially not possible to indicate
that all servers were unavailable from these policies, but since 1.9.2 returning
a value equal or greater than the number of servers will be interpreted as such.

For performance reasons, 1.6.0 introduced per-thread Lua FFI policies that are run in a lock-free per-thread Lua context instead of the global one.
This reduces contention between threads at the cost of preventing sharing data between threads for these policies. Since the policy needs to be recompiled
in the context of each thread instead of the global one, Lua code that returns a function should be passed to the function as a string instead of directly
Expand All @@ -128,6 +147,10 @@ passing the name of a function:
end
]])
Note that this version, like the one above, returns the index (starting at 0) of the server to select.
It was initially not possible to indicate that all servers were unavailable from these policies, but
since 1.9.2 returning a value equal or greater than the number of servers will be interpreted as such.

ServerPolicy Objects
--------------------

Expand Down Expand Up @@ -214,6 +237,9 @@ Functions

.. versionadded:: 1.5.0

.. versionchanged:: 1.9.2
Returning a value equal or greater than the number of servers will be interpreted as all servers being unavailable.

Set server selection policy to one named ``name`` and provided by the FFI function ``function``.

:param string name: name for this policy
Expand All @@ -223,6 +249,9 @@ Functions

.. versionadded:: 1.6.0

.. versionchanged:: 1.9.2
Returning a value equal or greater than the number of servers will be interpreted as all servers being unavailable.

Set server selection policy to one named ``name`` and the Lua FFI function returned by the Lua code passed in ``code``.
The resulting policy will be executed in a lock-free per-thread context, instead of running in the global Lua context.

Expand Down
Loading

0 comments on commit e472f31

Please sign in to comment.