From: bert hubert Date: Fri, 6 Nov 2015 11:23:18 +0000 (+0100) Subject: fix up PacketCache misuse if empty DNSNames by moving it to native DNSName canonical... X-Git-Tag: dnsdist-1.0.0-alpha1~239^2~12 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=478748c30528625c38b3e804fcdf9bf569209f6d;p=pdns fix up PacketCache misuse if empty DNSNames by moving it to native DNSName canonical ordering. Speed up non-range delete. Make it possible to wipe . --- diff --git a/pdns/dnsname.cc b/pdns/dnsname.cc index 4d17129f7..65ca46e79 100644 --- a/pdns/dnsname.cc +++ b/pdns/dnsname.cc @@ -265,7 +265,7 @@ bool DNSName::operator==(const DNSName& rhs) const size_t hash_value(DNSName const& d) { boost::hash hasher; - return hasher(toLower(d.toString())); // FIXME400 HACK + return hasher(toLower(d.toString())); // FIXME400 HACK - we rely on this lowercasing though in packetcache.hh } string DNSName::escapeLabel(const std::string& label) diff --git a/pdns/dynhandler.cc b/pdns/dynhandler.cc index e581ac79d..d8701a2e8 100644 --- a/pdns/dynhandler.cc +++ b/pdns/dynhandler.cc @@ -129,7 +129,10 @@ string DLPurgeHandler(const vector&parts, Utility::pid_t ppid) if(parts.size()>1) { for (vector::const_iterator i=++parts.begin();iqdomain)); + MapCombo& mc=getMap(p->qdomain); TryReadLock l(&mc.d_mut); // take a readlock here if(!l.gotIt()) { S.inc("deferred-cache-lookup"); @@ -175,7 +175,7 @@ void PacketCache::insert(const DNSName &qname, const QType& qtype, CacheEntryTyp CacheEntry val; val.created=time(0); val.ttd=val.created+ttl; - val.qname=pcReverse(qname); + val.qname=qname; val.qtype=qtype.getCode(); val.value=value; val.ctype=cet; @@ -216,36 +216,35 @@ int PacketCache::purge() int PacketCache::purge(const string &match) { int delcount=0; - - BOOST_FOREACH(MapCombo& mc, d_maps) { - WriteLock l(&mc.d_mut); - - if(ends_with(match, "$")) { - string prefix(match); - prefix.resize(prefix.size()-1); - - string zone = pcReverse(DNSName(prefix)); - - cmap_t::const_iterator iter = mc.d_map.lower_bound(tie(zone)); - cmap_t::const_iterator start=iter; + if(ends_with(match, "$")) { + string prefix(match); + prefix.resize(prefix.size()-1); + DNSName dprefix(prefix); + for(MapCombo& mc : d_maps) { + WriteLock l(&mc.d_mut); + cmap_t::const_iterator iter = mc.d_map.lower_bound(tie(dprefix)); + auto start=iter; for(; iter != mc.d_map.end(); ++iter) { - if(iter->qname.compare(0, zone.size(), zone) != 0) { + if(!iter->qname.isPartOf(dprefix)) { break; } delcount++; } mc.d_map.erase(start, iter); } - - else { - string qname = pcReverse(DNSName(match)); - - delcount+=mc.d_map.count(tie(qname)); - pair range = mc.d_map.equal_range(tie(qname)); + } + else { + DNSName dn(match); + auto mc = getMap(dn); + WriteLock l(&mc.d_mut); + auto range = mc.d_map.equal_range(tie(dn)); + if(range.first != range.second) { + delcount+=distance(range.first, range.second) - 1; mc.d_map.erase(range.first, range.second); } } + *d_statnumentries-=delcount; // XXX FIXME NEEDS TO BE ADJUSTED return delcount; } @@ -260,7 +259,7 @@ bool PacketCache::getEntry(const DNSName &qname, const QType& qtype, CacheEntryT cleanup(); } - MapCombo& mc=getMap(pcReverse(qname)); + MapCombo& mc=getMap(qname); TryReadLock l(&mc.d_mut); // take a readlock here if(!l.gotIt()) { @@ -277,9 +276,8 @@ bool PacketCache::getEntryLocked(const DNSName &qname, const QType& qtype, Cache { uint16_t qt = qtype.getCode(); //cerr<<"Lookup for maxReplyLen: "<ttd > now); if(ret) { @@ -292,12 +290,6 @@ bool PacketCache::getEntryLocked(const DNSName &qname, const QType& qtype, Cache } -string PacketCache::pcReverse(DNSName content) -{ - string ret=content.labelReverse().toDNSString(); - return ret.substr(0, ret.size()-1); -} - map PacketCache::getCounts() { int recursivePackets=0, nonRecursivePackets=0, queryCacheEntries=0, negQueryCacheEntries=0; diff --git a/pdns/packetcache.hh b/pdns/packetcache.hh index 7f98c75f8..1a28689be 100644 --- a/pdns/packetcache.hh +++ b/pdns/packetcache.hh @@ -72,12 +72,12 @@ public: private: bool getEntryLocked(const DNSName &content, const QType& qtype, CacheEntryType cet, string& entry, int zoneID=-1, bool meritsRecursion=false, unsigned int maxReplyLen=512, bool dnssecOk=false, bool hasEDNS=false, unsigned int *age=0); - string pcReverse(DNSName content); + struct CacheEntry { CacheEntry() { qtype = ctype = 0; zoneID = -1; meritsRecursion=false; dnssecOk=false; hasEDNS=false; created=0; ttd=0; maxReplyLen=512;} - string qname; + DNSName qname; string value; time_t created; time_t ttd; @@ -100,7 +100,7 @@ private: ordered_unique< composite_key< CacheEntry, - member, + member, member, member, member, @@ -109,7 +109,7 @@ private: member, member >, - composite_key_compare, std::less, std::less, std::less, std::less, + composite_key_compare, std::less, std::less, std::less, std::less, std::less, std::less > >, sequenced<> @@ -124,9 +124,9 @@ private: }; vector d_maps; - MapCombo& getMap(const std::string& qname) + MapCombo& getMap(const DNSName& qname) { - return d_maps[burtle((const unsigned char*)qname.c_str(), qname.length(), 0) % d_maps.size()]; + return d_maps[hash_value(qname) % d_maps.size()]; } AtomicCounter d_ops;