From 94306029ae0d112d931e38780591297ec0635423 Mon Sep 17 00:00:00 2001 From: Otto Moerbeek Date: Mon, 25 Nov 2019 12:49:41 +0100 Subject: [PATCH] Avoid having to use numbered indexes by given all indexes a tag and specifying them in the call if needed. --- pdns/auth-packetcache.cc | 6 ++--- pdns/auth-packetcache.hh | 4 ++-- pdns/auth-querycache.cc | 6 ++--- pdns/auth-querycache.hh | 4 ++-- pdns/cachecleaner.hh | 40 +++++++++++++++---------------- pdns/dbdnsseckeeper.cc | 8 +++---- pdns/dnsdistdist/dnsdist-rules.hh | 2 +- pdns/dnsseckeeper.hh | 11 +++++---- pdns/recpacketcache.cc | 8 +++---- pdns/recpacketcache.hh | 3 ++- pdns/recursor_cache.cc | 12 +++++----- pdns/recursordist/negcache.cc | 14 +++++------ pdns/recursordist/negcache.hh | 12 +++++++--- 13 files changed, 70 insertions(+), 60 deletions(-) diff --git a/pdns/auth-packetcache.cc b/pdns/auth-packetcache.cc index 715a5008f8..038d752368 100644 --- a/pdns/auth-packetcache.cc +++ b/pdns/auth-packetcache.cc @@ -207,7 +207,7 @@ uint64_t AuthPacketCache::purge() uint64_t AuthPacketCache::purgeExact(const DNSName& qname) { auto& mc = getMap(qname); - uint64_t delcount = purgeExactLockedCollection(mc, qname); + uint64_t delcount = purgeExactLockedCollection(mc, qname); *d_statnumentries -= delcount; @@ -224,7 +224,7 @@ uint64_t AuthPacketCache::purge(const string &match) uint64_t delcount = 0; if(ends_with(match, "$")) { - delcount = purgeLockedCollectionsVector(d_maps, match); + delcount = purgeLockedCollectionsVector(d_maps, match); *d_statnumentries -= delcount; } else { @@ -240,7 +240,7 @@ void AuthPacketCache::cleanup() uint64_t cacheSize = *d_statnumentries; uint64_t totErased = 0; - totErased = pruneLockedCollectionsVector(d_maps, maxCached, cacheSize); + totErased = pruneLockedCollectionsVector(d_maps, maxCached, cacheSize); *d_statnumentries -= totErased; DLOG(g_log<<"Done with cache clean, cacheSize: "<<(*d_statnumentries)<<", totErased"<, member >, ordered_non_unique, member, CanonDNSNameCompare >, - sequenced> + sequenced> > > cmap_t; diff --git a/pdns/auth-querycache.cc b/pdns/auth-querycache.cc index 033ed32312..2e39d2d225 100644 --- a/pdns/auth-querycache.cc +++ b/pdns/auth-querycache.cc @@ -173,7 +173,7 @@ uint64_t AuthQueryCache::purge() uint64_t AuthQueryCache::purgeExact(const DNSName& qname) { auto& mc = getMap(qname); - uint64_t delcount = purgeExactLockedCollection(mc, qname); + uint64_t delcount = purgeExactLockedCollection(mc, qname); *d_statnumentries -= delcount; @@ -186,7 +186,7 @@ uint64_t AuthQueryCache::purge(const string &match) uint64_t delcount = 0; if(ends_with(match, "$")) { - delcount = purgeLockedCollectionsVector(d_maps, match); + delcount = purgeLockedCollectionsVector(d_maps, match); *d_statnumentries -= delcount; } else { @@ -202,7 +202,7 @@ void AuthQueryCache::cleanup() uint64_t cacheSize = *d_statnumentries; uint64_t totErased = 0; - totErased = pruneLockedCollectionsVector(d_maps, maxCached, cacheSize); + totErased = pruneLockedCollectionsVector(d_maps, maxCached, cacheSize); *d_statnumentries -= totErased; DLOG(g_log<<"Done with cache clean, cacheSize: "<<*d_statnumentries<<", totErased"<, member > > , ordered_non_unique, member, CanonDNSNameCompare >, - sequenced> + sequenced> > > cmap_t; diff --git a/pdns/cachecleaner.hh b/pdns/cachecleaner.hh index 884029858e..732631d001 100644 --- a/pdns/cachecleaner.hh +++ b/pdns/cachecleaner.hh @@ -26,7 +26,7 @@ // this function can clean any cache that has a getTTD() method on its entries, a preRemoval() method and a 'sequence' index as its second index // the ritual is that the oldest entries are in *front* of the sequence collection, so on a hit, move an item to the end // on a miss, move it to the beginning -template void pruneCollection(C& container, T& collection, unsigned int maxCached, unsigned int scanFraction=1000) +template void pruneCollection(C& container, T& collection, unsigned int maxCached, unsigned int scanFraction=1000) { time_t now=time(0); unsigned int toTrim=0; @@ -39,8 +39,8 @@ template void pruneCollection(C& container, T& collecti // cout<<"Need to trim "<::type sequence_t; - sequence_t& sidx=collection.template get<1>(); + typedef typename T::template index::type sequence_t; + sequence_t& sidx=collection.template get(); unsigned int tried=0, lookAt, erased=0; @@ -89,29 +89,29 @@ template void pruneCollection(C& container, T& collecti } } -// note: this expects iterator from first index, and sequence MUST be second index! -template void moveCacheItemToFrontOrBack(T& collection, typename T::iterator& iter, bool front) +// note: this expects iterator from first index +template void moveCacheItemToFrontOrBack(T& collection, typename T::iterator& iter, bool front) { - typedef typename T::template nth_index<1>::type sequence_t; - sequence_t& sidx=collection.template get<1>(); - typename sequence_t::iterator si=collection.template project<1>(iter); + typedef typename T::template index::type sequence_t; + sequence_t& sidx=collection.template get(); + typename sequence_t::iterator si=collection.template project(iter); if(front) sidx.relocate(sidx.begin(), si); // at the beginning of the delete queue else sidx.relocate(sidx.end(), si); // back } -template void moveCacheItemToFront(T& collection, typename T::iterator& iter) +template void moveCacheItemToFront(T& collection, typename T::iterator& iter) { - moveCacheItemToFrontOrBack(collection, iter, true); + moveCacheItemToFrontOrBack(collection, iter, true); } -template void moveCacheItemToBack(T& collection, typename T::iterator& iter) +template void moveCacheItemToBack(T& collection, typename T::iterator& iter) { - moveCacheItemToFrontOrBack(collection, iter, false); + moveCacheItemToFrontOrBack(collection, iter, false); } -template uint64_t pruneLockedCollectionsVector(vector& maps, uint64_t maxCached, uint64_t cacheSize) +template uint64_t pruneLockedCollectionsVector(vector& maps, uint64_t maxCached, uint64_t cacheSize) { time_t now = time(nullptr); uint64_t totErased = 0; @@ -129,7 +129,7 @@ template uint64_t pruneLockedCollectionsVector(vector& maps, uin for(auto& mc : maps) { WriteLock wl(&mc.d_mut); - auto& sidx = boost::multi_index::get<2>(mc.d_map); + auto& sidx = boost::multi_index::get(mc.d_map); uint64_t erased = 0, lookedAt = 0; for(auto i = sidx.begin(); i != sidx.end(); lookedAt++) { if(i->ttd < now) { @@ -164,7 +164,7 @@ template uint64_t purgeLockedCollectionsVector(vector& maps) return delcount; } -template uint64_t purgeLockedCollectionsVector(vector& maps, const std::string& match) +template uint64_t purgeLockedCollectionsVector(vector& maps, const std::string& match) { uint64_t delcount=0; string prefix(match); @@ -172,7 +172,7 @@ template uint64_t purgeLockedCollectionsVector(vector& maps, con DNSName dprefix(prefix); for(auto& mc : maps) { WriteLock wl(&mc.d_mut); - auto& idx = boost::multi_index::get<1>(mc.d_map); + auto& idx = boost::multi_index::get(mc.d_map); auto iter = idx.lower_bound(dprefix); auto start = iter; @@ -188,11 +188,11 @@ template uint64_t purgeLockedCollectionsVector(vector& maps, con return delcount; } -template uint64_t purgeExactLockedCollection(T& mc, const DNSName& qname) +template uint64_t purgeExactLockedCollection(T& mc, const DNSName& qname) { uint64_t delcount=0; WriteLock wl(&mc.d_mut); - auto& idx = boost::multi_index::get<1>(mc.d_map); + auto& idx = boost::multi_index::get(mc.d_map); auto range = idx.equal_range(qname); if(range.first != range.second) { delcount += distance(range.first, range.second); @@ -202,13 +202,13 @@ template uint64_t purgeExactLockedCollection(T& mc, const DNSName& return delcount; } -template +template std::pair lruReplacingInsert(Index& i,const typename Index::value_type& x) { std::pair res = i.insert(x); if (!res.second) { - moveCacheItemToBack(i, res.first); + moveCacheItemToBack(i, res.first); res.second = i.replace(res.first, x); } return res; diff --git a/pdns/dbdnsseckeeper.cc b/pdns/dbdnsseckeeper.cc index 2fa9b78bef..1e9eb07d80 100644 --- a/pdns/dbdnsseckeeper.cc +++ b/pdns/dbdnsseckeeper.cc @@ -224,7 +224,7 @@ void DNSSECKeeper::getFromMeta(const DNSName& zname, const std::string& key, std nce.d_value = value; { WriteLock l(&s_metacachelock); - lruReplacingInsert(s_metacache, nce); + lruReplacingInsert(s_metacache, nce); } } } @@ -514,7 +514,7 @@ DNSSECKeeper::keyset_t DNSSECKeeper::getKeys(const DNSName& zone, bool useCache) kce.d_ttd = now + ttl; { WriteLock l(&s_keycachelock); - lruReplacingInsert(s_keycache, kce); + lruReplacingInsert(s_keycache, kce); } } @@ -832,11 +832,11 @@ void DNSSECKeeper::cleanup() if(now.tv_sec - s_last_prune > (time_t)(30)) { { WriteLock l(&s_metacachelock); - pruneCollection(*this, s_metacache, ::arg().asNum("max-cache-entries")); + pruneCollection(*this, s_metacache, ::arg().asNum("max-cache-entries")); } { WriteLock l(&s_keycachelock); - pruneCollection(*this, s_keycache, ::arg().asNum("max-cache-entries")); + pruneCollection(*this, s_keycache, ::arg().asNum("max-cache-entries")); } s_last_prune=time(0); } diff --git a/pdns/dnsdistdist/dnsdist-rules.hh b/pdns/dnsdistdist/dnsdist-rules.hh index e443fd3425..ebcc8aa795 100644 --- a/pdns/dnsdistdist/dnsdist-rules.hh +++ b/pdns/dnsdistdist/dnsdist-rules.hh @@ -103,7 +103,7 @@ public: iter = d_limits.insert(e).first; } - moveCacheItemToBack(d_limits, iter); + moveCacheItemToBack(d_limits, iter); return !iter->d_limiter.check(d_qps, d_burst); } } diff --git a/pdns/dnsseckeeper.hh b/pdns/dnsseckeeper.hh index f4bf04fe70..f280f4de98 100644 --- a/pdns/dnsseckeeper.hh +++ b/pdns/dnsseckeeper.hh @@ -256,24 +256,27 @@ private: }; + struct KeyCacheTag{}; + struct CompositeTag{}; + struct SequencedTag{}; typedef multi_index_container< KeyCacheEntry, indexed_by< - ordered_unique >, - sequenced<> + ordered_unique,member >, + sequenced> > > keycache_t; typedef multi_index_container< METACacheEntry, indexed_by< - ordered_unique< + ordered_unique, composite_key< METACacheEntry, member , member >, composite_key_compare, CIStringCompare> >, - sequenced<> + sequenced> > > metacache_t; diff --git a/pdns/recpacketcache.cc b/pdns/recpacketcache.cc index e474b0d218..1b952ba196 100644 --- a/pdns/recpacketcache.cc +++ b/pdns/recpacketcache.cc @@ -73,7 +73,7 @@ bool RecursorPacketCache::checkResponseMatches(std::pair(d_packetCache, iter); #ifdef HAVE_PROTOBUF if (protobufMessage) { if (iter->d_protobufMessage) { @@ -88,7 +88,7 @@ bool RecursorPacketCache::checkResponseMatches(std::pair(d_packetCache, iter); d_misses++; break; } @@ -161,7 +161,7 @@ void RecursorPacketCache::insertResponsePacket(unsigned int tag, uint32_t qhash, continue; } - moveCacheItemToBack(d_packetCache, iter); + moveCacheItemToBack(d_packetCache, iter); iter->d_packet = std::move(responsePacket); iter->d_query = std::move(query); iter->d_ecsBegin = ecsBegin; @@ -214,7 +214,7 @@ uint64_t RecursorPacketCache::bytes() void RecursorPacketCache::doPruneTo(unsigned int maxCached) { - pruneCollection(*this, d_packetCache, maxCached); + pruneCollection(*this, d_packetCache, maxCached); } uint64_t RecursorPacketCache::doDump(int fd) diff --git a/pdns/recpacketcache.hh b/pdns/recpacketcache.hh index 395d9101c8..c1930cddfa 100644 --- a/pdns/recpacketcache.hh +++ b/pdns/recpacketcache.hh @@ -98,11 +98,12 @@ private: } }; + struct SequencedTag{}; typedef multi_index_container< Entry, indexed_by < hashed_non_unique, composite_key, member > >, - sequenced<> , + sequenced> , ordered_non_unique, member, CanonDNSNameCompare > > > packetCache_t; diff --git a/pdns/recursor_cache.cc b/pdns/recursor_cache.cc index 7e0bf054ce..ca1f8bd1cb 100644 --- a/pdns/recursor_cache.cc +++ b/pdns/recursor_cache.cc @@ -78,7 +78,7 @@ int32_t MemRecursorCache::handleHit(MemRecursorCache::OrderedTagIterator_t& entr *wasAuth = entry->d_auth; } - moveCacheItemToBack(d_cache, entry); + moveCacheItemToBack(d_cache, entry); return ttd; } @@ -116,7 +116,7 @@ MemRecursorCache::cache_t::const_iterator MemRecursorCache::getEntryUsingECSInde } else { /* this netmask-specific entry has expired */ - moveCacheItemToFront(d_cache, entry); + moveCacheItemToFront(d_cache, entry); ecsIndex->removeNetmask(best); if (ecsIndex->isEmpty()) { d_ecsIndex.erase(ecsIndex); @@ -136,7 +136,7 @@ MemRecursorCache::cache_t::const_iterator MemRecursorCache::getEntryUsingECSInde } } else { - moveCacheItemToFront(d_cache, entry); + moveCacheItemToFront(d_cache, entry); } } @@ -217,7 +217,7 @@ int32_t MemRecursorCache::get(time_t now, const DNSName &qname, const QType& qt, auto firstIndexIterator = d_cache.project(i); if (i->d_ttd <= now) { - moveCacheItemToFront(d_cache, firstIndexIterator); + moveCacheItemToFront(d_cache, firstIndexIterator); continue; } @@ -311,7 +311,7 @@ void MemRecursorCache::replace(time_t now, const DNSName &qname, const QType& qt } if (!isNew) { - moveCacheItemToBack(d_cache, stored); + moveCacheItemToBack(d_cache, stored); } d_cache.replace(stored, ce); } @@ -480,6 +480,6 @@ void MemRecursorCache::doPrune(unsigned int keep) { d_cachecachevalid=false; - pruneCollection(*this, d_cache, keep); + pruneCollection(*this, d_cache, keep); } diff --git a/pdns/recursordist/negcache.cc b/pdns/recursordist/negcache.cc index 54399b75c1..a48107e761 100644 --- a/pdns/recursordist/negcache.cc +++ b/pdns/recursordist/negcache.cc @@ -50,10 +50,10 @@ bool NegCache::getRootNXTrust(const DNSName& qname, const struct timeval& now, c // We have something if ((uint32_t)now.tv_sec < ni->d_ttd) { *ne = &(*ni); - moveCacheItemToBack(d_negcache, ni); + moveCacheItemToBack(d_negcache, ni); return true; } - moveCacheItemToFront(d_negcache, ni); + moveCacheItemToFront(d_negcache, ni); ++ni; } return false; @@ -83,11 +83,11 @@ bool NegCache::get(const DNSName& qname, const QType& qtype, const struct timeva if ((uint32_t)now.tv_sec < ni->d_ttd) { // Not expired *ne = &(*ni); - moveCacheItemToBack(d_negcache, firstIndexIterator); + moveCacheItemToBack(d_negcache, firstIndexIterator); return true; } // expired - moveCacheItemToFront(d_negcache, firstIndexIterator); + moveCacheItemToFront(d_negcache, firstIndexIterator); } ++ni; } @@ -101,7 +101,7 @@ bool NegCache::get(const DNSName& qname, const QType& qtype, const struct timeva */ void NegCache::add(const NegCacheEntry& ne) { - lruReplacingInsert(d_negcache, ne); + lruReplacingInsert(d_negcache, ne); } /*! @@ -185,7 +185,7 @@ void NegCache::clear() */ void NegCache::prune(unsigned int maxEntries) { - pruneCollection(*this, d_negcache, maxEntries, 200); + pruneCollection(*this, d_negcache, maxEntries, 200); } /*! @@ -199,7 +199,7 @@ uint64_t NegCache::dumpToFile(FILE* fp) struct timeval now; Utility::gettimeofday(&now, nullptr); - negcache_sequence_t& sidx = d_negcache.get<1>(); + negcache_sequence_t& sidx = d_negcache.get(); for (const NegCacheEntry& ne : sidx) { ret++; fprintf(fp, "%s %" PRId64 " IN %s VIA %s ; (%s)\n", ne.d_name.toString().c_str(), static_cast(ne.d_ttd - now.tv_sec), ne.d_qtype.getName().c_str(), ne.d_auth.toString().c_str(), vStates[ne.d_validationState]); diff --git a/pdns/recursordist/negcache.hh b/pdns/recursordist/negcache.hh index e8d2b49cba..2dbabb0f4b 100644 --- a/pdns/recursordist/negcache.hh +++ b/pdns/recursordist/negcache.hh @@ -83,18 +83,24 @@ public: } private: + struct CompositeKey + { + }; + struct SequenceTag + { + }; typedef boost::multi_index_container< NegCacheEntry, indexed_by< - ordered_unique< + ordered_unique, composite_key< NegCacheEntry, member, member>, composite_key_compare< CanonDNSNameCompare, std::less>>, - sequenced<>, - hashed_non_unique< + sequenced>, + hashed_non_unique, member>>> negcache_t; -- 2.47.2