From b348bb1517064ec509f1d15fe112d64f592b7ee0 Mon Sep 17 00:00:00 2001 From: n0tlu5 Date: Thu, 23 May 2024 01:09:49 +0700 Subject: [PATCH 01/18] auth: added self weighted lua function --- pdns/lua-record.cc | 70 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 63 insertions(+), 7 deletions(-) diff --git a/pdns/lua-record.cc b/pdns/lua-record.cc index e9b08e662387..8d3e5ae0cd86 100644 --- a/pdns/lua-record.cc +++ b/pdns/lua-record.cc @@ -71,6 +71,8 @@ class IsUpOracle CheckState(time_t _lastAccess): lastAccess(_lastAccess) {} /* current status */ std::atomic status{false}; + /* current weight */ + std::atomic weight{0}; /* first check ? */ std::atomic first{true}; /* last time the status was accessed */ @@ -83,9 +85,9 @@ class IsUpOracle d_checkerThreadStarted.clear(); } ~IsUpOracle() = default; - bool isUp(const ComboAddress& remote, const opts_t& opts); - bool isUp(const ComboAddress& remote, const std::string& url, const opts_t& opts); - bool isUp(const CheckDesc& cd); + int isUp(const ComboAddress& remote, const opts_t& opts); + int isUp(const ComboAddress& remote, const std::string& url, const opts_t& opts); + int isUp(const CheckDesc& cd); private: void checkURL(const CheckDesc& cd, const bool status, const bool first = false) @@ -127,8 +129,10 @@ class IsUpOracle } if(!status) { - g_log<first = false; } } + + void setWeight(const CheckDesc& cd, string content){ + ReadLock lock{&d_lock}; + auto& state = d_statuses[cd]; + state->weight = stoi(content); + } void setDown(const ComboAddress& rem, const std::string& url=std::string(), const opts_t& opts = opts_t()) { @@ -244,7 +254,7 @@ class IsUpOracle } }; -bool IsUpOracle::isUp(const CheckDesc& cd) +int IsUpOracle::isUp(const CheckDesc& cd) { if (!d_checkerThreadStarted.test_and_set()) { d_checkerThread = std::make_unique([this] { return checkThread(); }); @@ -255,6 +265,9 @@ bool IsUpOracle::isUp(const CheckDesc& cd) auto iter = statuses->find(cd); if (iter != statuses->end()) { iter->second->lastAccess = now; + if (iter->second->weight > 0) { + return iter->second->weight; + } return iter->second->status; } } @@ -272,13 +285,13 @@ bool IsUpOracle::isUp(const CheckDesc& cd) return false; } -bool IsUpOracle::isUp(const ComboAddress& remote, const opts_t& opts) +int IsUpOracle::isUp(const ComboAddress& remote, const opts_t& opts) { CheckDesc cd{remote, "", opts}; return isUp(cd); } -bool IsUpOracle::isUp(const ComboAddress& remote, const std::string& url, const opts_t& opts) +int IsUpOracle::isUp(const ComboAddress& remote, const std::string& url, const opts_t& opts) { CheckDesc cd{remote, url, opts}; return isUp(cd); @@ -1152,6 +1165,49 @@ static void setupLuaRecords(LuaContext& lua) // NOLINT(readability-function-cogn return pickRandom(items); }); + lua.writeFunction("selfweighted", [](const std::string& url, + const boost::variant& ips, + boost::optional options) { + vector > candidates; + opts_t opts; + if(options) + opts = *options; + if(auto simple = boost::get(&ips)) { + vector unit = convIplist(*simple); + candidates.push_back(unit); + } else { + auto units = boost::get(ips); + for(const auto& u : units) { + vector unit = convIplist(u.second); + candidates.push_back(unit); + } + } + + for(const auto& unit : candidates) { + vector > conv; + bool available = 0; + for(const auto& c : unit) { + int weight = 0; + weight = g_up.isUp(c, url, opts); + if(weight>0){ + available = 1; + } + conv.emplace_back(weight, c); + } + if(available) { + return pickwhashed(s_lua_record_ctx->bestwho, conv).toString(); + } + } + + // All units down, apply backupSelector on all candidates + vector ret{}; + for(const auto& unit : candidates) { + ret.insert(ret.end(), unit.begin(), unit.end()); + } + + return pickrandom(ret).toString(); + }); + lua.writeFunction("pickrandomsample", [](int n, const iplist_t& ips) { vector items = convStringList(ips); return pickRandomSample(n, items); From a8e95f6c5d780c0a7171ed262de8ef873dd80dcc Mon Sep 17 00:00:00 2001 From: n0tlu5 Date: Thu, 23 May 2024 08:38:11 +0700 Subject: [PATCH 02/18] Merge weight log to status log --- pdns/lua-record.cc | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/pdns/lua-record.cc b/pdns/lua-record.cc index 8d3e5ae0cd86..a5970a7d0f33 100644 --- a/pdns/lua-record.cc +++ b/pdns/lua-record.cc @@ -128,10 +128,18 @@ class IsUpOracle throw std::runtime_error(boost::str(boost::format("unable to match content with `%s`") % cd.opts.at("stringmatch"))); } - if(!status) { - g_log< Date: Thu, 23 May 2024 08:40:14 +0700 Subject: [PATCH 03/18] Adjust setWeight state retrieve mechanism --- pdns/lua-record.cc | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/pdns/lua-record.cc b/pdns/lua-record.cc index a5970a7d0f33..2259ca561b12 100644 --- a/pdns/lua-record.cc +++ b/pdns/lua-record.cc @@ -233,9 +233,17 @@ class IsUpOracle } void setWeight(const CheckDesc& cd, string content){ - ReadLock lock{&d_lock}; - auto& state = d_statuses[cd]; - state->weight = stoi(content); + auto statuses = d_statuses.write_lock(); + auto& state = (*statuses)[cd]; + try { + state->weight = stoi(content); + } catch (const PDNSException& e) { + // set weight to 0 + state->weight = 0; + } + if (state->first) { + state->first = false; + } } void setDown(const ComboAddress& rem, const std::string& url=std::string(), const opts_t& opts = opts_t()) From 5f3a65fdc6001aac7277555df861318eee477bd2 Mon Sep 17 00:00:00 2001 From: n0tlu5 Date: Thu, 23 May 2024 08:42:07 +0700 Subject: [PATCH 04/18] Add selfweighted func desc; reduce to 1 loop --- pdns/lua-record.cc | 53 ++++++++++++++++++---------------------------- 1 file changed, 21 insertions(+), 32 deletions(-) diff --git a/pdns/lua-record.cc b/pdns/lua-record.cc index 2259ca561b12..cf95e0ea35c0 100644 --- a/pdns/lua-record.cc +++ b/pdns/lua-record.cc @@ -1181,47 +1181,36 @@ static void setupLuaRecords(LuaContext& lua) // NOLINT(readability-function-cogn return pickRandom(items); }); - lua.writeFunction("selfweighted", [](const std::string& url, - const boost::variant& ips, + /* + * Based on the hash of `bestwho`, returns an IP address from the list + * supplied, weighted according to the results of isUp calls. + * @example selfweighted("{ "192.0.2.20", "203.0.113.4", "203.0.113.2" }) + */ + lua.writeFunction("selfweighted", [](const iplist_t& ips, boost::optional options) { - vector > candidates; + vector< pair > items; opts_t opts; if(options) opts = *options; - if(auto simple = boost::get(&ips)) { - vector unit = convIplist(*simple); - candidates.push_back(unit); - } else { - auto units = boost::get(ips); - for(const auto& u : units) { - vector unit = convIplist(u.second); - candidates.push_back(unit); - } - } - for(const auto& unit : candidates) { - vector > conv; - bool available = 0; - for(const auto& c : unit) { - int weight = 0; - weight = g_up.isUp(c, url, opts); - if(weight>0){ - available = 1; - } - conv.emplace_back(weight, c); - } - if(available) { - return pickwhashed(s_lua_record_ctx->bestwho, conv).toString(); + items.reserve(ips.capacity()); + bool available = 0; + + vector conv = convComboAddressList(ips); + for (auto& entry : conv) { + int weight = 0; + weight = g_up.isUp(entry, opts); + if(weight>0){ + available = 1; } + items.emplace_back(weight, entry); } - - // All units down, apply backupSelector on all candidates - vector ret{}; - for(const auto& unit : candidates) { - ret.insert(ret.end(), unit.begin(), unit.end()); + if(available) { + return pickWeightedHashed(s_lua_record_ctx->bestwho, items).toString(); } - return pickrandom(ret).toString(); + // All units down, apply backupSelector on all candidates + return pickWeightedRandom(items).toString(); }); lua.writeFunction("pickrandomsample", [](int n, const iplist_t& ips) { From 1676c6d3f36826da5126a8ca2ef2ab78b2265ed1 Mon Sep 17 00:00:00 2001 From: n0tlu5 Date: Fri, 24 May 2024 13:35:04 +0700 Subject: [PATCH 05/18] reduce exception handling --- pdns/lua-record.cc | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/pdns/lua-record.cc b/pdns/lua-record.cc index cf95e0ea35c0..0a0f5dbad0bb 100644 --- a/pdns/lua-record.cc +++ b/pdns/lua-record.cc @@ -128,8 +128,9 @@ class IsUpOracle throw std::runtime_error(boost::str(boost::format("unable to match content with `%s`") % cd.opts.at("stringmatch"))); } + int weight = 0; try { - stoi(content); + weight = stoi(content); if(!status) { g_log<weight = stoi(content); - } catch (const PDNSException& e) { - // set weight to 0 - state->weight = 0; - } + state->weight = weight; if (state->first) { state->first = false; } From 1d2feead3fca1ae8a23501fb893eba36d94fb1be Mon Sep 17 00:00:00 2001 From: n0tlu5 Date: Thu, 19 Dec 2024 21:15:56 +0700 Subject: [PATCH 06/18] rename selfweighted to pickselfweighted; add pickselfweighted docs --- docs/lua-records/functions.rst | 15 +++++++++++++++ pdns/lua-record.cc | 4 ++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/docs/lua-records/functions.rst b/docs/lua-records/functions.rst index 45af5dde9a4f..715980e120fc 100644 --- a/docs/lua-records/functions.rst +++ b/docs/lua-records/functions.rst @@ -128,6 +128,21 @@ Record creation functions This function also works for CNAME or TXT records. +.. function:: pickselfweighted(addresses[, options]) + + Selects an IP address from the supplied list, weighted according to the results of `isUp` checks. Each address is evaluated, and if its associated weight (from `isUp`) is greater than 0, it is considered for selection using a weighted hash based on `bestwho`. If no address is "up," the function defaults to a random selection. + + :param addresses: A list of IP addresses to evaluate. + :param options: (Optional) A table of options for this specific check. Supports: + - ``source``: Source address for the check. + - ``timeout``: Maximum time in seconds for the check (default 2). + + Example usage:: + + pickselfweighted({ "192.0.2.20", "203.0.113.4", "203.0.113.2" }, { source = "192.0.2.1", timeout = 1 }) + + This function is ideal for scenarios where weighted selection is necessary to prioritize "up" addresses while still offering fallback behavior when all addresses are down. + .. function:: pickrandomsample(number, values) Returns N random values from the list supplied. diff --git a/pdns/lua-record.cc b/pdns/lua-record.cc index 0a0f5dbad0bb..fc0314ece5cb 100644 --- a/pdns/lua-record.cc +++ b/pdns/lua-record.cc @@ -1180,9 +1180,9 @@ static void setupLuaRecords(LuaContext& lua) // NOLINT(readability-function-cogn /* * Based on the hash of `bestwho`, returns an IP address from the list * supplied, weighted according to the results of isUp calls. - * @example selfweighted("{ "192.0.2.20", "203.0.113.4", "203.0.113.2" }) + * @example pickselfweighted("{ "192.0.2.20", "203.0.113.4", "203.0.113.2" }) */ - lua.writeFunction("selfweighted", [](const iplist_t& ips, + lua.writeFunction("pickselfweighted", [](const iplist_t& ips, boost::optional options) { vector< pair > items; opts_t opts; From 68c5bfb027d7f85e4320dfff1ecc6ff018a606dc Mon Sep 17 00:00:00 2001 From: n0tlu5 Date: Sun, 22 Dec 2024 11:35:24 +0700 Subject: [PATCH 07/18] removes redundant first check flag; removes trailing whitespace --- pdns/lua-record.cc | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pdns/lua-record.cc b/pdns/lua-record.cc index fc0314ece5cb..95148b3f40bc 100644 --- a/pdns/lua-record.cc +++ b/pdns/lua-record.cc @@ -232,14 +232,11 @@ class IsUpOracle state->first = false; } } - + void setWeight(const CheckDesc& cd, int weight){ auto statuses = d_statuses.write_lock(); auto& state = (*statuses)[cd]; state->weight = weight; - if (state->first) { - state->first = false; - } } void setDown(const ComboAddress& rem, const std::string& url=std::string(), const opts_t& opts = opts_t()) From cefe1ccdfa2633d157b7c14e59df438421c2dcf2 Mon Sep 17 00:00:00 2001 From: n0tlu5 Date: Sun, 22 Dec 2024 11:42:33 +0700 Subject: [PATCH 08/18] resets weight when service is declared as down --- pdns/lua-record.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/pdns/lua-record.cc b/pdns/lua-record.cc index 95148b3f40bc..4653588f6954 100644 --- a/pdns/lua-record.cc +++ b/pdns/lua-record.cc @@ -147,6 +147,7 @@ class IsUpOracle catch(std::exception& ne) { if(status || first) g_log< Date: Sun, 22 Dec 2024 15:03:11 +0700 Subject: [PATCH 09/18] adds required checkurl url params --- pdns/lua-record.cc | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pdns/lua-record.cc b/pdns/lua-record.cc index 4653588f6954..d731698b4a2b 100644 --- a/pdns/lua-record.cc +++ b/pdns/lua-record.cc @@ -1178,10 +1178,11 @@ static void setupLuaRecords(LuaContext& lua) // NOLINT(readability-function-cogn /* * Based on the hash of `bestwho`, returns an IP address from the list * supplied, weighted according to the results of isUp calls. - * @example pickselfweighted("{ "192.0.2.20", "203.0.113.4", "203.0.113.2" }) + * @example pickselfweighted('http://example.com/weight', { "192.0.2.20", "203.0.113.4", "203.0.113.2" }) */ - lua.writeFunction("pickselfweighted", [](const iplist_t& ips, - boost::optional options) { + lua.writeFunction("pickselfweighted", [](const std::string& url, + const iplist_t& ips, + boost::optional options) { vector< pair > items; opts_t opts; if(options) @@ -1193,7 +1194,7 @@ static void setupLuaRecords(LuaContext& lua) // NOLINT(readability-function-cogn vector conv = convComboAddressList(ips); for (auto& entry : conv) { int weight = 0; - weight = g_up.isUp(entry, opts); + weight = g_up.isUp(entry, url, opts); if(weight>0){ available = 1; } From d8df1aded1280fc4f738a152512cfcc630fbfc9d Mon Sep 17 00:00:00 2001 From: n0tlu5 Date: Sun, 22 Dec 2024 15:53:30 +0700 Subject: [PATCH 10/18] adds selfweighted test with a set of A records --- regression-tests.auth-py/test_LuaRecords.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/regression-tests.auth-py/test_LuaRecords.py b/regression-tests.auth-py/test_LuaRecords.py index 973d98568c87..08d0d84583bf 100644 --- a/regression-tests.auth-py/test_LuaRecords.py +++ b/regression-tests.auth-py/test_LuaRecords.py @@ -27,6 +27,8 @@ def do_GET(self): self._set_headers() if self.path == '/ping.json': self.wfile.write(bytes('{"ping":"pong"}', 'utf-8')) + if self.path == '/weight.txt': + self.wfile.write(bytes('12', 'utf-8')) else: self.wfile.write(bytes("

hi!

Programming in Lua !

", "utf-8")) @@ -78,6 +80,7 @@ class TestLuaRecords(AuthTest): randn-txt.example.org. 3600 IN LUA TXT "pickrandomsample( 2, {{ 'bob', 'alice', 'john' }} )" v6-bogus.rand.example.org. 3600 IN LUA AAAA "pickrandom({{'{prefix}.101', '{prefix}.102'}})" v6.rand.example.org. 3600 IN LUA AAAA "pickrandom({{ '2001:db8:a0b:12f0::1', 'fe80::2a1:9bff:fe9b:f268' }})" +selfweighted.example.org. 3600 IN LUA A "pickselfweighted('http://selfweighted.example.org:8080/weight.txt',{{'{prefix}.101', '{prefix}.102'}})" closest.geo 3600 IN LUA A "pickclosest({{ '1.1.1.2', '1.2.3.4' }})" empty.rand.example.org. 3600 IN LUA A "pickrandom()" timeout.example.org. 3600 IN LUA A "; local i = 0 ; while i < 1000 do pickrandom() ; i = i + 1 end return '1.2.3.4'" @@ -260,6 +263,24 @@ def testEmptyRandom(self): res = self.sendUDPQuery(query) self.assertRcodeEqual(res, dns.rcode.SERVFAIL) + def testSelfWeighted(self): + """ + Test the selfweighted() function with a set of A records + """ + expected = [dns.rrset.from_text('selfweighted.example.org.', 0, dns.rdataclass.IN, 'A', + '{prefix}.101'.format(prefix=self._PREFIX)), + dns.rrset.from_text('selfweighted.example.org.', 0, dns.rdataclass.IN, 'A', + '{prefix}.102'.format(prefix=self._PREFIX))] + query = dns.message.make_query('selfweighted.example.org', 'A') + res = self.sendUDPQuery(query) + + # wait for health checks to happen + time.sleep(3) + + res = self.sendUDPQuery(query) + self.assertRcodeEqual(res, dns.rcode.NOERROR) + self.assertAnyRRsetInAnswer(res, expected) + def testPickRandomSampleTxt(self): """ Basic pickrandomsample() test with a set of TXT records From 261b10c6f8fb2fe26155fb3d1eb830c6a8d2ee94 Mon Sep 17 00:00:00 2001 From: n0tlu5 Date: Sun, 22 Dec 2024 16:10:28 +0700 Subject: [PATCH 11/18] add url param; copy ifurlup options doc --- docs/lua-records/functions.rst | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/docs/lua-records/functions.rst b/docs/lua-records/functions.rst index 715980e120fc..2badaffce97a 100644 --- a/docs/lua-records/functions.rst +++ b/docs/lua-records/functions.rst @@ -128,20 +128,31 @@ Record creation functions This function also works for CNAME or TXT records. -.. function:: pickselfweighted(addresses[, options]) +.. function:: pickselfweighted(url, addresses[, options]) - Selects an IP address from the supplied list, weighted according to the results of `isUp` checks. Each address is evaluated, and if its associated weight (from `isUp`) is greater than 0, it is considered for selection using a weighted hash based on `bestwho`. If no address is "up," the function defaults to a random selection. + Selects an IP address from the supplied list, weighted according to the results of `isUp` checks. Each address is evaluated, and if its associated weight (from `isUp`) is greater than 0, it is considered for selection using a weighted hash based on `bestwho`. If no address is "up" the function defaults to a random selection. + :param string url: The health check url to retrieve. :param addresses: A list of IP addresses to evaluate. - :param options: (Optional) A table of options for this specific check. Supports: - - ``source``: Source address for the check. - - ``timeout``: Maximum time in seconds for the check (default 2). - - Example usage:: + :param options: Table of options for this specific check, see below. + + Various options can be set in the ``options`` parameter: + + - ``selector``: used to pick the address(es) from the subset of available addresses of the selected set. Choices include 'pickclosest', 'random', 'hashed', 'all' (default 'random'). + - ``backupSelector``: used to pick the address from all addresses if all addresses are down. Choices include 'pickclosest', 'random', 'hashed', 'all' (default 'random'). + - ``source``: Source address to check from + - ``timeout``: Maximum time in seconds that you allow the check to take (default 2) + - ``stringmatch``: check ``url`` for this string, only declare 'up' if found + - ``useragent``: Set the HTTP "User-Agent" header in the requests. By default it is set to "PowerDNS Authoritative Server" + - ``byteslimit``: Limit the maximum download size to ``byteslimit`` bytes (default 0 meaning no limit). + + An example of a list of address sets: + + .. code-block:: lua - pickselfweighted({ "192.0.2.20", "203.0.113.4", "203.0.113.2" }, { source = "192.0.2.1", timeout = 1 }) + pickselfweighted("http://example.com/weight", { "192.0.2.20", "203.0.113.4", "203.0.113.2" }) - This function is ideal for scenarios where weighted selection is necessary to prioritize "up" addresses while still offering fallback behavior when all addresses are down. + This function is ideal for scenarios where candidates can self-determine their weights, while also providing fallback behavior when all addresses are down. .. function:: pickrandomsample(number, values) From b9b8101edded4246cc4c9947a7870821c47b88e9 Mon Sep 17 00:00:00 2001 From: n0tlu5 Date: Sat, 28 Dec 2024 16:44:00 +0700 Subject: [PATCH 12/18] add pickselfweighted to expected spelling --- .github/actions/spell-check/expect.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/actions/spell-check/expect.txt b/.github/actions/spell-check/expect.txt index cd77ab8d2094..f6232f2f1841 100644 --- a/.github/actions/spell-check/expect.txt +++ b/.github/actions/spell-check/expect.txt @@ -1012,6 +1012,7 @@ pickchashed pickclosest pickhashed picknamehashed +pickselfweighted pickrandom pickrandomsample pickwhashed From 5aaf2c9d2133e80b805d5cd914076f25b07bff11 Mon Sep 17 00:00:00 2001 From: n0tlu5 Date: Sat, 28 Dec 2024 16:51:31 +0700 Subject: [PATCH 13/18] skip readability-identifier-length lint --- pdns/lua-record.cc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pdns/lua-record.cc b/pdns/lua-record.cc index d731698b4a2b..cd10d732fe55 100644 --- a/pdns/lua-record.cc +++ b/pdns/lua-record.cc @@ -87,6 +87,7 @@ class IsUpOracle ~IsUpOracle() = default; int isUp(const ComboAddress& remote, const opts_t& opts); int isUp(const ComboAddress& remote, const std::string& url, const opts_t& opts); + //NOLINT(readability-identifier-length) int isUp(const CheckDesc& cd); private: @@ -242,6 +243,7 @@ class IsUpOracle void setDown(const ComboAddress& rem, const std::string& url=std::string(), const opts_t& opts = opts_t()) { + //NOLINT(readability-identifier-length) CheckDesc cd{rem, url, opts}; setStatus(cd, false); } @@ -272,6 +274,7 @@ int IsUpOracle::isUp(const CheckDesc& cd) time_t now = time(nullptr); { auto statuses = d_statuses.read_lock(); + //NOLINT(readability-identifier-length) auto iter = statuses->find(cd); if (iter != statuses->end()) { iter->second->lastAccess = now; From 280f44789a69f78f49439b6a80ab69da9622a966 Mon Sep 17 00:00:00 2001 From: n0tlu5 Date: Sat, 28 Dec 2024 16:55:53 +0700 Subject: [PATCH 14/18] follow readability-braces-around-statements lint --- pdns/lua-record.cc | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pdns/lua-record.cc b/pdns/lua-record.cc index cd10d732fe55..8dd0f83517f8 100644 --- a/pdns/lua-record.cc +++ b/pdns/lua-record.cc @@ -1198,14 +1198,12 @@ static void setupLuaRecords(LuaContext& lua) // NOLINT(readability-function-cogn for (auto& entry : conv) { int weight = 0; weight = g_up.isUp(entry, url, opts); - if(weight>0){ + if(weight>0) available = 1; - } items.emplace_back(weight, entry); } - if(available) { + if(available) return pickWeightedHashed(s_lua_record_ctx->bestwho, items).toString(); - } // All units down, apply backupSelector on all candidates return pickWeightedRandom(items).toString(); From 15fb57cda10fb96f92c72fff8cc3a063ed32ab54 Mon Sep 17 00:00:00 2001 From: n0tlu5 Date: Sat, 28 Dec 2024 16:56:36 +0700 Subject: [PATCH 15/18] follow readability-implicit-bool-conversion lint --- pdns/lua-record.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pdns/lua-record.cc b/pdns/lua-record.cc index 8dd0f83517f8..093503979d83 100644 --- a/pdns/lua-record.cc +++ b/pdns/lua-record.cc @@ -1192,14 +1192,14 @@ static void setupLuaRecords(LuaContext& lua) // NOLINT(readability-function-cogn opts = *options; items.reserve(ips.capacity()); - bool available = 0; + bool available = false; vector conv = convComboAddressList(ips); for (auto& entry : conv) { int weight = 0; weight = g_up.isUp(entry, url, opts); if(weight>0) - available = 1; + available = true; items.emplace_back(weight, entry); } if(available) From a34ba3853e37fba307465c626f49998f32e842b5 Mon Sep 17 00:00:00 2001 From: n0tlu5 Date: Mon, 30 Dec 2024 16:14:18 +0700 Subject: [PATCH 16/18] move nolint annotation to earliest param appearence --- pdns/lua-record.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pdns/lua-record.cc b/pdns/lua-record.cc index 093503979d83..25a05edfef75 100644 --- a/pdns/lua-record.cc +++ b/pdns/lua-record.cc @@ -266,6 +266,7 @@ class IsUpOracle } }; +//NOLINT(readability-identifier-length) int IsUpOracle::isUp(const CheckDesc& cd) { if (!d_checkerThreadStarted.test_and_set()) { @@ -274,7 +275,6 @@ int IsUpOracle::isUp(const CheckDesc& cd) time_t now = time(nullptr); { auto statuses = d_statuses.read_lock(); - //NOLINT(readability-identifier-length) auto iter = statuses->find(cd); if (iter != statuses->end()) { iter->second->lastAccess = now; From 488f5f77aa96ef5186ee7b6d169d9dbd5e2bca91 Mon Sep 17 00:00:00 2001 From: n0tlu5 Date: Mon, 6 Jan 2025 23:55:49 +0700 Subject: [PATCH 17/18] skip readability-identifier-length lint --- pdns/lua-record.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pdns/lua-record.cc b/pdns/lua-record.cc index 25a05edfef75..658bcd344efc 100644 --- a/pdns/lua-record.cc +++ b/pdns/lua-record.cc @@ -87,7 +87,7 @@ class IsUpOracle ~IsUpOracle() = default; int isUp(const ComboAddress& remote, const opts_t& opts); int isUp(const ComboAddress& remote, const std::string& url, const opts_t& opts); - //NOLINT(readability-identifier-length) + //NOLINTNEXTLINE(readability-identifier-length) int isUp(const CheckDesc& cd); private: @@ -243,7 +243,7 @@ class IsUpOracle void setDown(const ComboAddress& rem, const std::string& url=std::string(), const opts_t& opts = opts_t()) { - //NOLINT(readability-identifier-length) + //NOLINTNEXTLINE(readability-identifier-length) CheckDesc cd{rem, url, opts}; setStatus(cd, false); } @@ -266,7 +266,7 @@ class IsUpOracle } }; -//NOLINT(readability-identifier-length) +//NOLINTNEXTLINE(readability-identifier-length) int IsUpOracle::isUp(const CheckDesc& cd) { if (!d_checkerThreadStarted.test_and_set()) { From 6f392826ca73fbd7508377dd60601b5724237837 Mon Sep 17 00:00:00 2001 From: Miod Vallat Date: Mon, 13 Jan 2025 07:41:16 +0100 Subject: [PATCH 18/18] More clang-tidy conformance. --- pdns/lua-record.cc | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/pdns/lua-record.cc b/pdns/lua-record.cc index 658bcd344efc..62aeaafff481 100644 --- a/pdns/lua-record.cc +++ b/pdns/lua-record.cc @@ -235,6 +235,7 @@ class IsUpOracle } } + //NOLINTNEXTLINE(readability-identifier-length) void setWeight(const CheckDesc& cd, int weight){ auto statuses = d_statuses.write_lock(); auto& state = (*statuses)[cd]; @@ -281,7 +282,7 @@ int IsUpOracle::isUp(const CheckDesc& cd) if (iter->second->weight > 0) { return iter->second->weight; } - return iter->second->status; + return static_cast(iter->second->status); } } // try to parse options so we don't insert any malformed content @@ -295,7 +296,7 @@ int IsUpOracle::isUp(const CheckDesc& cd) (*statuses)[cd] = std::make_unique(now); } } - return false; + return 0; } int IsUpOracle::isUp(const ComboAddress& remote, const opts_t& opts) @@ -1188,8 +1189,9 @@ static void setupLuaRecords(LuaContext& lua) // NOLINT(readability-function-cogn boost::optional options) { vector< pair > items; opts_t opts; - if(options) + if(options) { opts = *options; + } items.reserve(ips.capacity()); bool available = false; @@ -1198,12 +1200,14 @@ static void setupLuaRecords(LuaContext& lua) // NOLINT(readability-function-cogn for (auto& entry : conv) { int weight = 0; weight = g_up.isUp(entry, url, opts); - if(weight>0) + if(weight>0) { available = true; + } items.emplace_back(weight, entry); } - if(available) + if(available) { return pickWeightedHashed(s_lua_record_ctx->bestwho, items).toString(); + } // All units down, apply backupSelector on all candidates return pickWeightedRandom(items).toString();