root/trunk/pdns/pdns/packetcache.hh @ 1399

Revision 1399, 4.5 KB (checked in by ahu, 4 years ago)

repair cache cleaning logic, which would never run in certain benchmark situations, plus retune the actual cleaning process.
Thanks to Marcus Goller for reporting this issue.

  • Property svn:eol-style set to native
  • Property svn:keywords set to author date id revision
RevLine 
[2]1/*
2    PowerDNS Versatile Database Driven Nameserver
[1209]3    Copyright (C) 2002 - 2008  PowerDNS.COM BV
[2]4
5    This program is free software; you can redistribute it and/or modify
[681]6    it under the terms of the GNU General Public License version 2
7    as published by the Free Software Foundation
8   
[2]9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
[680]17    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
[2]18*/
19#ifndef PACKETCACHE_HH
20#define PACKETCACHE_HH
21
22#include <string>
23#include <utility>
24#include <map>
[1218]25#include <map>
[1273]26#include "dns.hh"
[1218]27#include <boost/version.hpp>
[2]28using namespace std;
[1218]29using namespace ::boost::multi_index;
[2]30
[1218]31using namespace boost;
[2]32#include "dnspacket.hh"
33#include "lock.hh"
34#include "statbag.hh"
35
36/** This class performs 'whole packet caching'. Feed it a question packet and it will
37    try to find an answer. If you have an answer, insert it to have it cached for later use.
38    Take care not to replace existing cache entries. While this works, it is wasteful. Only
39    insert packets that where not found by get()
40
41    Locking!
42
43    The cache itself is protected by a read/write lock. Because deleting is a two step process, which
44    first marks and then sweeps, a second lock is present to prevent simultaneous inserts and deletes.
[1218]45*/
[2]46
[1220]47struct CIBackwardsStringCompare: public binary_function<string, string, bool> 
48{
[1227]49  bool operator()(const string& str_a, const string& str_b) const
[1220]50  {
[1227]51    string::const_reverse_iterator ra, rb;
52    char a=0, b=0;
53    for(ra = str_a.rbegin(), rb = str_b.rbegin();
54        ra < str_a.rend() && rb < str_b.rend() && (a=dns_tolower(*ra)) == (b=dns_tolower(*rb));
55        ra++, rb++);
[1220]56   
[1227]57    if (ra < str_a.rend() && rb==str_b.rend()) { a=*(ra++); b=0; }
58    if (rb < str_b.rend() && ra==str_a.rend()) { b=*(rb++); a=0; }
[1220]59
[1227]60    return a < b;
[1220]61  }
62};
63
64
[1365]65class PacketCache : public boost::noncopyable
[2]66{
67public:
68  PacketCache();
[1365]69  ~PacketCache();
[1313]70  enum CacheEntryType { PACKETCACHE, QUERYCACHE};
[1218]71
[2]72  void insert(DNSPacket *q, DNSPacket *r);  //!< We copy the contents of *p into our cache. Do not needlessly call this to insert questions already in the cache as it wastes resources
73
[1218]74  void insert(const string &qname, const QType& qtype, CacheEntryType cet, const string& value, unsigned int ttl, int zoneID=-1, bool meritsRecursion=false);
[1209]75
76  int get(DNSPacket *p, DNSPacket *q); //!< We return a dynamically allocated copy out of our cache. You need to delete it. You also need to spoof in the right ID with the DNSPacket.spoofID() method.
[1218]77  bool getEntry(const string &content, const QType& qtype, CacheEntryType cet, string& entry, int zoneID=-1, bool meritsRecursion=false);
78
[2]79  int size(); //!< number of entries in the cache
80  void cleanup(); //!< force the cache to preen itself from expired packets
[1221]81  int purge(const vector<string>&matches= vector<string>());
[1209]82
[2]83  map<char,int> getCounts();
84private:
[1399]85  bool getEntryLocked(const string &content, const QType& qtype, CacheEntryType cet, string& entry, int zoneID=-1, bool meritsRecursion=false);
[1218]86  struct CacheEntry
87  {
88    CacheEntry() { qtype = ctype = 0; zoneID = -1; meritsRecursion=false;}
[2]89
[1218]90    string qname;
91    uint16_t qtype;
92    uint16_t ctype;
93    int zoneID;
[2]94    time_t ttd;
[1218]95    bool meritsRecursion;
[2]96    string value;
97  };
98
99  void getTTLS();
100
[1218]101  typedef multi_index_container<
102    CacheEntry,
103    indexed_by <
104                ordered_unique<
105                      composite_key< 
106                        CacheEntry,
107                        member<CacheEntry,string,&CacheEntry::qname>,
108                        member<CacheEntry,uint16_t,&CacheEntry::qtype>,
109                        member<CacheEntry,uint16_t, &CacheEntry::ctype>,
110                        member<CacheEntry,int, &CacheEntry::zoneID>,
111                        member<CacheEntry,bool, &CacheEntry::meritsRecursion>
112                      >,
[1220]113                  composite_key_compare<CIBackwardsStringCompare, std::less<uint16_t>, std::less<uint16_t>, std::less<int>, std::less<bool> >
[1218]114                >,
115               sequenced<>
116               >
117  > cmap_t;
118
119
[2]120  cmap_t d_map;
121
122  pthread_rwlock_t d_mut;
123
[1399]124  unsigned int d_ops;
[2]125  int d_ttl;
126  int d_recursivettl;
127  bool d_doRecursion;
[1284]128  unsigned int *d_statnumhit;
129  unsigned int *d_statnummiss;
130  unsigned int *d_statnumentries;
[2]131};
132
133
134
135#endif /* PACKETCACHE_HH */
136
Note: See TracBrowser for help on using the browser.