From: Kevin P. Fleming Date: Fri, 24 Sep 2021 20:59:45 +0000 (-0400) Subject: rec: Refactor cache-wiping code into a common function X-Git-Tag: dnsdist-1.7.0-beta1~9^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f6329e6ed111724ff357a9c41341be3b2e281154;p=thirdparty%2Fpdns.git rec: Refactor cache-wiping code into a common function Eliminates multiple copies of the code and eliminates inconsistencies between them. --- diff --git a/pdns/pdns_recursor.cc b/pdns/pdns_recursor.cc index 9f00634dce..346b437f33 100644 --- a/pdns/pdns_recursor.cc +++ b/pdns/pdns_recursor.cc @@ -4847,6 +4847,30 @@ static void checkSocketDir(void) _exit(1); } +static uint64_t* pleaseWipePacketCache(const DNSName& canon, bool subtree, uint16_t qtype) +{ + return new uint64_t(t_packetCache->doWipePacketCache(canon, qtype, subtree)); +} + +struct WipeCacheResult wipeCaches(const DNSName& canon, bool subtree, uint16_t qtype) +{ + struct WipeCacheResult res; + + try { + res.record_count = g_recCache->doWipeCache(canon, subtree, qtype); + res.packet_count = broadcastAccFunction([=]{ return pleaseWipePacketCache(canon, subtree, qtype);}); + res.negative_record_count = g_negCache->wipe(canon, subtree); + if (g_aggressiveNSECCache) { + g_aggressiveNSECCache->removeZoneInfo(canon, subtree); + } + } + catch (const std::exception& e) { + g_log<doWipePacketCache(canon, qtype, subtree)); -} - template static string doWipeCache(T begin, T end, uint16_t qtype) { @@ -492,12 +487,10 @@ static string doWipeCache(T begin, T end, uint16_t qtype) int count = 0, pcount = 0, countNeg = 0; for (auto wipe : toWipe) { try { - count += g_recCache->doWipeCache(wipe.first, wipe.second, qtype); - pcount += broadcastAccFunction([=] { return pleaseWipePacketCache(wipe.first, wipe.second, qtype); }); - countNeg += g_negCache->wipe(wipe.first, wipe.second); - if (g_aggressiveNSECCache) { - g_aggressiveNSECCache->removeZoneInfo(wipe.first, wipe.second); - } + auto res = wipeCaches(wipe.first, wipe.second, qtype); + count += res.record_count; + pcount += res.packet_count; + countNeg += res.negative_record_count; } catch (const std::exception& e) { g_log << Logger::Warning << ", failed: " << e.what() << endl; @@ -614,12 +607,7 @@ static string doAddNTA(T begin, T end) lci.negAnchors[who] = why; }); try { - g_recCache->doWipeCache(who, true, 0xffff); - broadcastAccFunction([=] { return pleaseWipePacketCache(who, true, 0xffff); }); - g_negCache->wipe(who, true); - if (g_aggressiveNSECCache) { - g_aggressiveNSECCache->removeZoneInfo(who, true); - } + wipeCaches(who, true, 0xffff); } catch (std::exception& e) { g_log << Logger::Warning << ", failed: " << e.what() << endl; @@ -671,12 +659,7 @@ static string doClearNTA(T begin, T end) g_luaconfs.modify([entry](LuaConfigItems& lci) { lci.negAnchors.erase(entry); }); - g_recCache->doWipeCache(entry, true, 0xffff); - broadcastAccFunction([=] { return pleaseWipePacketCache(entry, true, 0xffff); }); - g_negCache->wipe(entry, true); - if (g_aggressiveNSECCache) { - g_aggressiveNSECCache->removeZoneInfo(entry, true); - } + wipeCaches(entry, true, 0xffff); if (!first) { first = false; removed += ","; @@ -737,12 +720,7 @@ static string doAddTA(T begin, T end) auto ds = std::dynamic_pointer_cast(DSRecordContent::make(what)); lci.dsAnchors[who].insert(*ds); }); - g_recCache->doWipeCache(who, true, 0xffff); - broadcastAccFunction([=] { return pleaseWipePacketCache(who, true, 0xffff); }); - g_negCache->wipe(who, true); - if (g_aggressiveNSECCache) { - g_aggressiveNSECCache->removeZoneInfo(who, true); - } + wipeCaches(who, true, 0xffff); g_log << Logger::Warning << endl; return "Added Trust Anchor for " + who.toStringRootDot() + " with data " + what + "\n"; } @@ -787,12 +765,7 @@ static string doClearTA(T begin, T end) g_luaconfs.modify([entry](LuaConfigItems& lci) { lci.dsAnchors.erase(entry); }); - g_recCache->doWipeCache(entry, true, 0xffff); - broadcastAccFunction([=] { return pleaseWipePacketCache(entry, true, 0xffff); }); - g_negCache->wipe(entry, true); - if (g_aggressiveNSECCache) { - g_aggressiveNSECCache->removeZoneInfo(entry, true); - } + wipeCaches(entry, true, 0xffff); if (!first) { first = false; removed += ","; diff --git a/pdns/reczones.cc b/pdns/reczones.cc index 560f204f89..f5d417e3c8 100644 --- a/pdns/reczones.cc +++ b/pdns/reczones.cc @@ -354,9 +354,7 @@ string reloadAuthAndForwards() } for (const auto& i : oldAndNewDomains) { - g_recCache->doWipeCache(i, true, 0xffff); - broadcastAccFunction([&] { return pleaseWipePacketCache(i, true, 0xffff); }); - g_negCache->wipe(i, true); + wipeCaches(i, true, 0xffff); } broadcastFunction([=] { return pleaseUseNewSDomainsMap(newDomainMap); }); diff --git a/pdns/syncres.hh b/pdns/syncres.hh index 495dd5d337..0506fdabf8 100644 --- a/pdns/syncres.hh +++ b/pdns/syncres.hh @@ -1186,11 +1186,19 @@ uint64_t* pleaseGetConcurrentQueries(); uint64_t* pleaseGetThrottleSize(); uint64_t* pleaseGetPacketCacheHits(); uint64_t* pleaseGetPacketCacheSize(); -uint64_t* pleaseWipePacketCache(const DNSName& canon, bool subtree, uint16_t qtype=0xffff); void doCarbonDump(void*); bool primeHints(time_t now = time(nullptr)); void primeRootNSZones(bool, unsigned int depth); +struct WipeCacheResult +{ + int record_count = 0; + int negative_record_count = 0; + int packet_count = 0; +}; + +struct WipeCacheResult wipeCaches(const DNSName& canon, bool subtree, uint16_t qtype); + extern __thread struct timeval g_now; struct ThreadTimes diff --git a/pdns/ws-recursor.cc b/pdns/ws-recursor.cc index def09184ba..3c2ad0c083 100644 --- a/pdns/ws-recursor.cc +++ b/pdns/ws-recursor.cc @@ -395,11 +395,9 @@ static void apiServerCacheFlush(HttpRequest* req, HttpResponse* resp) qtype = QType::chartocode(req->getvars["type"].c_str()); } - int count = g_recCache->doWipeCache(canon, subtree, qtype); - count += broadcastAccFunction([=] { return pleaseWipePacketCache(canon, subtree, qtype); }); - count += g_negCache->wipe(canon, subtree); + struct WipeCacheResult res = wipeCaches(canon, subtree, qtype); resp->setJsonBody(Json::object{ - {"count", count}, + {"count", res.record_count + res.packet_count + res.negative_record_count}, {"result", "Flushed cache."}}); }