cache.cc 10.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright (C) 2010  Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

// $Id$

#include <stdint.h>

#include <map>

#include <dns/question.h>
#include <dns/rrclass.h>
#include <dns/rrset.h>
#include <dns/rrtype.h>

#include <list>

#include <datasrc/cache.h>

using namespace std;
using namespace isc::dns;

namespace isc {
namespace datasrc {

/// \brief A \c CacheEntry object contains the data stored with
/// each \c CacheNode: a pointer to the cached RRset (empty in
/// the case of a negative cache entry), and a copy of the
/// query-response flags that were returned when the RRset
/// was originally looked up in the low-level data source.
class CacheEntry {
private:
    /// The copy constructor and the assignment operator are intentionally
    /// defined as private.
    CacheEntry(const CacheEntry& source);
    CacheEntry& operator=(const CacheEntry& source);

public:
    CacheEntry(RRsetPtr r, uint32_t f) : rrset(r), flags(f) {};

    RRsetPtr rrset;
    uint32_t flags;
};

typedef boost::shared_ptr<CacheEntry> CacheEntryPtr;

/// \brief A \c CacheNode is a node in the \c HotCache LRU queue.  It
/// contains a pointer to a \c CacheEntry, a reference to the \c Question
/// that we are answering, a lifespan during which this entry remains
/// valid, and pointers to the next and previous entries in the list.
class CacheNode {
private:
    /// \name Constructors and Assignment Operator
    ///
    /// Note: The copy constructor and the assignment operator are intentionally
    /// defined as private.
    //@{
    CacheNode(const CacheNode& source);
    CacheNode& operator=(const CacheNode& source);

public:
    /// \brief Constructor for positive cache entry.
    ///
    /// \param rrset The \c RRset to cache.
    /// \param flags The query response flags returned from the low-level
    /// data source when this \c RRset was looked up.
    /// \param lifespan How long the cache node is to be considered valid.
    CacheNode(const RRsetPtr rrset, uint32_t flags, time_t lifespan);

    /// \brief Constructor for negative cache entry.
    ///
    /// \param name Query name
    /// \param rrclass Query class
    /// \param rrtype Query type
    /// \param flags Query response flags returned from the low-level
    /// data source, indicating why this lookup failed (name not found,
    /// type not found, etc).
    /// \param lifespan How long the cache node is to be considered valid.
    CacheNode(const Name& name,
              const RRClass& rrclass,
              const RRType& rrtype,
              uint32_t flags,
              time_t lifespan);
    //@}

    /// \name Getter and Setter Methods
    //@{
    /// \brief Returns a pointer to the cached RRset (or an empty
    /// RRsetPtr for negative cache entries).

    /// \return \c RRsetPtr
    RRsetPtr getRRset() const { return (entry->rrset); }

    /// \brief Returns the query response flags associated with the data.
    ///
    /// \return \c uint32_t
    uint32_t getFlags() const { return (entry->flags); }

    /// \brief Is this record still valid?
    ///
    /// \return True if the expiration time has not yet passed,
    /// or false if it has.
    bool isValid() const;
    //@}

    // An iterator referencing this entry in the LRU list. This
    // permits unit-time removal using list::erase().
    list<CacheNodePtr>::iterator lru_entry_;

    /// The \c Question (name/rrclass/rrtype) answered by this cache node
    const isc::dns::Question question;

private:
    // The cached RRset data
    CacheEntryPtr entry;

    // When this record should be discarded
    time_t expiry;
};

// CacheNode constructor for a positive cache entry
CacheNode::CacheNode(const RRsetPtr rrset, const uint32_t flags,
                     const time_t lifespan) :
    question(Question(rrset->getName(), rrset->getClass(), rrset->getType()))
{
    const time_t now = time(NULL);
    expiry = now + lifespan;

    entry = CacheEntryPtr(new CacheEntry(rrset, flags));
}

// CacheNode constructor for a negative cache entry
CacheNode::CacheNode(const Name& name,
                     const RRClass& rrclass,
                     const RRType& rrtype,
                     const uint32_t flags,
                     const time_t lifespan) :
    question(Question(name, rrclass, rrtype))
{
    const time_t now = time(NULL);
    expiry = now + lifespan;

    entry = CacheEntryPtr(new CacheEntry(RRsetPtr(), flags));
}
// Returns true if the node has not yet expired.
bool
CacheNode::isValid() const {
    const time_t now = time(NULL);
    return (now < expiry);
}

/// This class abstracts the implementation details for \c HotCache.
///
/// Each node inserted into the cache is placed at the head of a
/// doubly-linked list.  Whenever that node is retrieved from the cache,
/// it is again moved to the head of the list.  When the configured
/// number of slots in the cache has been exceeded, the least recently
/// used nodes will be removed from the tail of the list.
///
/// A pointer to each cache node is also placed in a \c std::map object,
/// keyed by \c isc::dns::Question.  This allows retrieval of data in
/// (usually) logarithmic time.  (Possible TODO item: replace this with a
/// hash table instead.)
class HotCacheImpl {
public:
    HotCacheImpl(int slots, bool enabled);

    // The LRU list
    list<CacheNodePtr> lru_;

    // Flag to indicate whether the cache is enabled
    bool enabled_;

    // The number of available slots in the LRU list.  (If zero,
    // then the list is unbounded; otherwise, items are removed
    // from the tail of the list whenever it grows past slots_.)
    int slots_;

    // The number of items currently in the list.
    int count_;

    // Map from query tuple to cache node pointer, allowing fast retrieval
    // of data without a linear search of the LRU list
    std::map<Question, CacheNodePtr> map_;

    // Move a node to the front of the LRU list.
    void promote(CacheNodePtr node);

    // Remove a node from the cache.
    void remove(ConstCacheNodePtr node);

    // Insert a node into the cache (called by both cache() and ncache())
    void insert(CacheNodePtr node);
};

// HotCacheImpl constructor
HotCacheImpl::HotCacheImpl(int slots, bool enabled) :
    enabled_(enabled), slots_(slots), count_(0)
{}

// Insert a cache node into the cache
inline void
HotCacheImpl::insert(const CacheNodePtr node) {
    std::map<Question, CacheNodePtr>::const_iterator iter;
    iter = map_.find(node->question);
    if (iter != map_.end()) {
        CacheNodePtr old = iter->second;
        if (old && old->isValid()) {
            remove(old);
        }
    }

    lru_.push_front(node);
    node->lru_entry_ = lru_.begin();

    map_[node->question] = node;
    ++count_;

    if (slots_ != 0 && count_ > slots_) {
        remove(lru_.back());
    }
}

// Promote a node to the head of the LRU list
void
HotCacheImpl::promote(CacheNodePtr node) {
    if (!node) {
        return;
    }
    if (node->lru_entry_ == lru_.begin()) {
        return;
    }
243
    lru_.splice(lru_.begin(), lru_, node->lru_entry_); // move node to front
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
    node->lru_entry_ = lru_.begin();
}

// Remove a node from the LRU list and the map
void
HotCacheImpl::remove(ConstCacheNodePtr node) {
    lru_.erase(node->lru_entry_);
    map_.erase(node->question);
    --count_;
}

// HotCache constructor
HotCache::HotCache(const int slots) {
    impl_ = new HotCacheImpl(slots, true);
}

// HotCache destructor
HotCache::~HotCache() {
    delete impl_;
}

// Add a positive entry to the cache
void
HotCache::addPositive(RRsetPtr rrset, const uint32_t flags,
                      const time_t lifespan)
{
    if (!impl_->enabled_) {
        return;
    }

    impl_->insert(CacheNodePtr(new CacheNode(rrset, flags, lifespan)));
}

// Add a negative entry to the cache
void
HotCache::addNegative(const Name& name, const RRClass &rrclass,
                      const RRType& rrtype, const uint32_t flags,
                      const time_t lifespan)
{
    if (!impl_->enabled_) {
        return;
    }

    if (rrtype == RRType::ANY() || rrclass == RRClass::ANY()) {
        return;
    }

    impl_->insert(CacheNodePtr(new CacheNode(name, rrclass, rrtype,
                                             flags, lifespan)));
}

// Try to retrieve an entry from the cache, returning true if
// it was found and valid.
bool
HotCache::retrieve(const Name& n, const RRClass& c, const RRType& t,
                   RRsetPtr& rrset, uint32_t& flags)
{
    if (!impl_->enabled_) {
        return (false);
    }

    std::map<Question, CacheNodePtr>::const_iterator iter;
    iter = impl_->map_.find(Question(n, c, t));
    if (iter == impl_->map_.end()) {
        return (false);
    }

    CacheNodePtr node = iter->second;

    if (node->isValid()) {
        impl_->promote(node);
        rrset = node->getRRset();
        flags = node->getFlags();
        return (true);
    }

    impl_->remove(node);
    return (false);
}

// Set the number of slots in the cache.
void
HotCache::setSlots(const int slots) {
    impl_->slots_ = slots;

    if (!impl_->enabled_) {
        return;
    }

    while (impl_->slots_ != 0 && impl_->count_ > impl_->slots_) {
        impl_->remove(impl_->lru_.back());
    }
}

// Return the number of slots in the cache
int
HotCache::getSlots() const {
    return (impl_->slots_);
}

/// Enable or disable the cache
void
HotCache::setEnabled(const bool e) {
    impl_->enabled_ = e;
}

/// Indicate whether the cache is enabled
bool
HotCache::getEnabled() const {
    return (impl_->enabled_);
}

// Return the number of entries in the cache
int
HotCache::getCount() const {
    return (impl_->count_);
}

}
}