]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/auth-querycache.cc
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "auth-querycache.hh"
29 #include "cachecleaner.hh"
32 const unsigned int AuthQueryCache::s_mincleaninterval
, AuthQueryCache::s_maxcleaninterval
;
34 AuthQueryCache::AuthQueryCache(size_t mapsCount
): d_lastclean(time(nullptr))
36 d_maps
.resize(mapsCount
);
37 for(auto& mc
: d_maps
) {
38 pthread_rwlock_init(&mc
.d_mut
, 0);
41 S
.declare("query-cache-hit","Number of hits on the query cache");
42 S
.declare("query-cache-miss","Number of misses on the query cache");
43 S
.declare("query-cache-size", "Number of entries in the query cache");
44 S
.declare("deferred-cache-inserts","Amount of cache inserts that were deferred because of maintenance");
45 S
.declare("deferred-cache-lookup","Amount of cache lookups that were deferred because of maintenance");
47 d_statnumhit
=S
.getPointer("query-cache-hit");
48 d_statnummiss
=S
.getPointer("query-cache-miss");
49 d_statnumentries
=S
.getPointer("query-cache-size");
52 AuthQueryCache::~AuthQueryCache()
55 vector
<WriteLock
*> locks
;
56 for(auto& mc
: d_maps
) {
57 locks
.push_back(new WriteLock(&mc
.d_mut
));
59 for(auto wl
: locks
) {
67 // called from ueberbackend
68 bool AuthQueryCache::getEntry(const DNSName
&qname
, const QType
& qtype
, vector
<DNSZoneRecord
>& value
, int zoneID
)
72 time_t now
= time(nullptr);
73 uint16_t qt
= qtype
.getCode();
74 auto& mc
= getMap(qname
);
76 TryReadLock
rl(&mc
.d_mut
);
78 S
.inc("deferred-cache-lookup");
82 return getEntryLocked(mc
.d_map
, qname
, qt
, value
, zoneID
, now
);
86 void AuthQueryCache::insert(const DNSName
&qname
, const QType
& qtype
, const vector
<DNSZoneRecord
>& value
, uint32_t ttl
, int zoneID
)
93 time_t now
= time(nullptr);
98 val
.qtype
= qtype
.getCode();
102 auto& mc
= getMap(val
.qname
);
105 TryWriteLock
l(&mc
.d_mut
);
107 S
.inc("deferred-cache-inserts");
112 cmap_t::iterator place
;
113 tie(place
, inserted
) = mc
.d_map
.insert(val
);
116 mc
.d_map
.replace(place
, val
);
119 (*d_statnumentries
)++;
124 bool AuthQueryCache::getEntryLocked(cmap_t
& map
, const DNSName
&qname
, uint16_t qtype
, vector
<DNSZoneRecord
>& value
, int zoneID
, time_t now
)
126 auto& idx
= boost::multi_index::get
<HashTag
>(map
);
127 auto iter
= idx
.find(tie(qname
, qtype
, zoneID
));
129 if (iter
== idx
.end()) {
134 if (iter
->ttd
< now
) {
144 map
<char,uint64_t> AuthQueryCache::getCounts()
146 uint64_t queryCacheEntries
=0, negQueryCacheEntries
=0;
148 for(auto& mc
: d_maps
) {
149 ReadLock
l(&mc
.d_mut
);
151 for(cmap_t::const_iterator iter
= mc
.d_map
.begin() ; iter
!= mc
.d_map
.end(); ++iter
) {
152 if(iter
->drs
.empty())
153 negQueryCacheEntries
++;
158 map
<char,uint64_t> ret
;
160 ret
['!']=negQueryCacheEntries
;
161 ret
['Q']=queryCacheEntries
;
165 /* clears the entire cache. */
166 uint64_t AuthQueryCache::purge()
168 d_statnumentries
->store(0);
170 return purgeLockedCollectionsVector(d_maps
);
173 uint64_t AuthQueryCache::purgeExact(const DNSName
& qname
)
175 auto& mc
= getMap(qname
);
176 uint64_t delcount
= purgeExactLockedCollection(mc
, qname
);
178 *d_statnumentries
-= delcount
;
183 /* purges entries from the querycache. If match ends on a $, it is treated as a suffix */
184 uint64_t AuthQueryCache::purge(const string
&match
)
186 uint64_t delcount
= 0;
188 if(ends_with(match
, "$")) {
189 delcount
= purgeLockedCollectionsVector(d_maps
, match
);
190 *d_statnumentries
-= delcount
;
193 delcount
= purgeExact(DNSName(match
));
199 void AuthQueryCache::cleanup()
201 uint64_t maxCached
= d_maxEntries
;
202 uint64_t cacheSize
= *d_statnumentries
;
203 uint64_t totErased
= 0;
205 totErased
= pruneLockedCollectionsVector(d_maps
, maxCached
, cacheSize
);
207 *d_statnumentries
-= totErased
;
208 DLOG(g_log
<<"Done with cache clean, cacheSize: "<<*d_statnumentries
<<", totErased"<<totErased
<<endl
);
212 after d_nextclean operations, we clean. We also adjust the cleaninterval
213 a bit so we slowly move it to a value where we clean roughly every 30 seconds.
215 If d_nextclean has reached its maximum value, we also test if we were called
216 within 30 seconds, and if so, we skip cleaning. This means that under high load,
217 we will not clean more often than every 30 seconds anyhow.
220 void AuthQueryCache::cleanupIfNeeded()
222 if (d_ops
++ == d_nextclean
) {
223 time_t now
= time(nullptr);
224 int timediff
= max((int)(now
- d_lastclean
), 1);
226 DLOG(g_log
<<"cleaninterval: "<<d_cleaninterval
<<", timediff: "<<timediff
<<endl
);
228 if (d_cleaninterval
== s_maxcleaninterval
&& timediff
< 30) {
229 d_cleanskipped
= true;
230 d_nextclean
+= d_cleaninterval
;
232 DLOG(g_log
<<"cleaning skipped, timediff: "<<timediff
<<endl
);
237 if(!d_cleanskipped
) {
238 d_cleaninterval
=(int)(0.6*d_cleaninterval
)+(0.4*d_cleaninterval
*(30.0/timediff
));
239 d_cleaninterval
=std::max(d_cleaninterval
, s_mincleaninterval
);
240 d_cleaninterval
=std::min(d_cleaninterval
, s_maxcleaninterval
);
242 DLOG(g_log
<<"new cleaninterval: "<<d_cleaninterval
<<endl
);
244 d_cleanskipped
= false;
247 d_nextclean
+= d_cleaninterval
;