Browse Source

some change to the interface

zhanglikun 14 years ago
parent
commit
777f07c955

+ 3 - 0
src/lib/cache/TODO

@@ -2,6 +2,9 @@
     
   "TODO, according RFC2181 section 5.4.1, only the record 
    describing that ailas is necessarily authoritative."
+
 * Implement dump/load/resize interfaces of rrset/message/recursor cache.   
 * Once LRU hash table is implemented, it should be used by message/rrset cache.  
+* Once the hash/lrulist related files in /lib/nsas is moved to seperated
+  folder, the code of recursor cache has to be updated.
 

+ 2 - 1
src/lib/cache/message_cache.h

@@ -75,7 +75,8 @@ protected:
     HashKey getEntryHashKey(const isc::dns::Name& name, 
                             const isc::dns::RRType& type) const;
 
-private:
+    // Make these variants be protected for easy unittest.
+protected:
     uint16_t message_class_; // The class of the message cache.
     boost::shared_ptr<RRsetCache> rrset_cache_;
     isc::nsas::HashTable<MessageEntry> message_table_;

+ 45 - 13
src/lib/cache/message_entry.cc

@@ -31,17 +31,47 @@ static uint32_t MAX_UINT32 = numeric_limits<uint32_t>::max();
 
 MessageEntry::MessageEntry(const isc::dns::Message& msg,
                            boost::shared_ptr<RRsetCache> rrset_cache):
-    rrset_cache_(rrset_cache)
+    rrset_cache_(rrset_cache),
+    headerflag_aa_(false),
+    headerflag_tc_(false),
+    headerflag_ad_(false)
 {
     initMessageEntry(msg);
 }
     
 bool
-MessageEntry::genMessage(const time_t&,
-                         isc::dns::Message&)
+MessageEntry::genMessage(const time_t& time_now,
+                         isc::dns::Message& msg)
 {
-    //TODO, generate message according the query header flags.
-    return true;
+    if (time_now > expire_time_) {
+        // The message entry has expired.
+        return false;
+    } else {
+        ConstEDNSPtr edns(msg.getEDNS());
+        bool dnssec_need = edns;
+        uint16_t index = 0;
+        // Add answer section's rrsets.
+        for(index = 0; index < answer_count_; index++) {
+            msg.addRRset(Message::SECTION_ANSWER, 
+                         rrsets_[index]->genRRset(), dnssec_need);
+        }
+        
+        // Add authority section's rrsets.
+        uint16_t end = answer_count_ + authority_count_;
+        for(index = answer_count_; index < end; index++) {
+            msg.addRRset(Message::SECTION_AUTHORITY, 
+                         rrsets_[index]->genRRset(), dnssec_need);
+        }
+
+        // Add additional section's rrsets.
+        index = end;
+        end = end + additional_count_;
+        for(; index < end; index++) {
+            msg.addRRset(Message::SECTION_ADDITIONAL, 
+                         rrsets_[index]->genRRset(), dnssec_need);
+        }
+        return true;
+    }
 }
 
 RRsetTrustLevel
@@ -86,9 +116,9 @@ MessageEntry::getRRsetTrustLevel(const Message& message,
 }
 
 void
-MessageEntry::parseSection(const isc::dns::Message& msg,
-                           const Message::Section& section,
-                           uint32_t& smaller_ttl)
+MessageEntry::parseRRset(const isc::dns::Message& msg,
+                         const Message::Section& section,
+                         uint32_t& smaller_ttl)
 {
     RRsetIterator iter;
     for (iter = msg.beginSection(section);
@@ -116,8 +146,10 @@ MessageEntry::initMessageEntry(const isc::dns::Message& msg) {
     authority_count_ = msg.getRRCount(Message::SECTION_AUTHORITY);
     additional_count_ = msg.getRRCount(Message::SECTION_ADDITIONAL);
     
-    //TODO how to cache the header?
-    // query_header 
+    //TODO better way to cache the header flags?
+    headerflag_aa_ = msg.getHeaderFlag(Message::HEADERFLAG_AA);
+    headerflag_tc_ = msg.getHeaderFlag(Message::HEADERFLAG_TC);
+    headerflag_ad_ = msg.getHeaderFlag(Message::HEADERFLAG_AD);
 
     // We only cache the first question in question section.
     // TODO, do we need to support muptiple questions?
@@ -127,9 +159,9 @@ MessageEntry::initMessageEntry(const isc::dns::Message& msg) {
     query_class_ = (*iter)->getClass().getCode();
     
     uint32_t min_ttl = MAX_UINT32;
-    parseSection(msg, Message::SECTION_ANSWER, min_ttl);
-    parseSection(msg, Message::SECTION_AUTHORITY, min_ttl);
-    parseSection(msg, Message::SECTION_ADDITIONAL, min_ttl);
+    parseRRset(msg, Message::SECTION_ANSWER, min_ttl);
+    parseRRset(msg, Message::SECTION_AUTHORITY, min_ttl);
+    parseRRset(msg, Message::SECTION_ADDITIONAL, min_ttl);
 
     expire_time_ = time(NULL) + min_ttl;
 }

+ 8 - 4
src/lib/cache/message_entry.h

@@ -75,9 +75,9 @@ protected:
     /// \brief Parse the rrsets in specified section.
     /// \param smaller_ttl Get the smallest ttl of rrsets in 
     /// specified section, if it's smaller than the given value.
-    void parseSection(const isc::dns::Message& msg,
-                      const isc::dns::Message::Section& section,
-                      uint32_t& smaller_ttl);
+    void parseRRset(const isc::dns::Message& msg,
+                    const isc::dns::Message::Section& section,
+                    uint32_t& smaller_ttl);
 
     /// \brief Get RRset Trust worthiness
     /// only the rrset can be updated by the rrsets 
@@ -95,7 +95,6 @@ private:
     uint16_t query_class_; // query class of the message.
     uint16_t query_type_; // query type of message.
 
-    uint16_t query_header; // query header of the message.
     uint16_t query_count_; // query count in query section.
     uint16_t answer_count_; // rrset count in answer section.
     uint16_t authority_count_; // rrset count in authority section.
@@ -103,6 +102,11 @@ private:
 
     std::vector<boost::shared_ptr<RRsetEntry> > rrsets_;
     boost::shared_ptr<RRsetCache> rrset_cache_;
+
+    //TODO, there should be a better way to cache these header flags
+    bool headerflag_aa_; // Whether AA bit is set.
+    bool headerflag_tc_; // Whether TC bit is set.
+    bool headerflag_ad_; // Whether AD bit is set.
 };
     
 typedef boost::shared_ptr<MessageEntry> MessageEntryPtr;

+ 7 - 6
src/lib/cache/recursor_cache.cc

@@ -25,17 +25,18 @@ using namespace std;
 namespace isc {
 namespace cache {
 
-RecursorCache::RecursorCache(std::vector<uint16_t> dns_classes) {
+RecursorCache::RecursorCache(std::vector<CacheSizeInfo> caches_size) {
     uint32_t index = 0;
-    uint32_t size = dns_classes.size();
+    uint32_t size = caches_size.size();
     for (; index < size; index++) {
-        uint16_t klass = dns_classes[index];
+        CacheSizeInfo* infop = &caches_size[index];
+        uint16_t klass = infop->class_;
         rrsets_cache1_[klass] = RRsetCachePtr(new 
-                                RRsetCache(RRSET_CACHE1_DEFAULT_SIZE, klass));
+                                     RRsetCache(infop->rrset_cache_size, klass));
         rrsets_cache2_[klass] = RRsetCachePtr(new 
-                                RRsetCache(RRSET_CACHE2_DEFAULT_SIZE, klass));
+                                     RRsetCache(infop->rrset_cache_size, klass));
         messages_cache_[klass] = MessageCachePtr(new MessageCache(rrsets_cache2_[klass], 
-                                                      MESSAGE_CACHE_DEFAULT_SIZE, 
+                                                      infop->message_cache_size, 
                                                       klass));
     }
 }

+ 21 - 8
src/lib/cache/recursor_cache.h

@@ -34,8 +34,16 @@ typedef std::map<uint16_t, RRsetCachePtr> RRsetCacheMap;
 
 //TODO a better proper default cache size
 #define MESSAGE_CACHE_DEFAULT_SIZE 1000000 
-#define RRSET_CACHE1_DEFAULT_SIZE  1000
-#define RRSET_CACHE2_DEFAULT_SIZE  10000
+#define RRSET_CACHE_DEFAULT_SIZE  10000
+
+/// \brief Cache Size Information.
+/// It is used to initialize the size of rrset/message.
+struct CacheSizeInfo
+{
+    uint16_t class_; // class of the cache.
+    uint32_t message_cache_size; // the size for message cache.
+    uint32_t rrset_cache_size; // The size for rrset cache.
+};
 
 ///    
 /// \brief Recursor Cache
@@ -45,15 +53,17 @@ typedef std::map<uint16_t, RRsetCachePtr> RRsetCacheMap;
 class RecursorCache {
 public:
     /// \brief Construct Function
-    /// \param dns_classes cache the messages/rrsets for these classes.
-    RecursorCache(std::vector<uint16_t> dns_classes);
+    /// \param caches_size cache size information for each 
+    /// messages/rrsets.
+    RecursorCache(std::vector<CacheSizeInfo> caches_size);
 
     /// \name Lookup Interfaces
     //@{
     /// \brief Look up message in cache.
     ///
-    /// \param response generated response message if the message can be found 
-    ///  in cache.
+    /// \param response the query message (must in RENDER mode),
+    /// if the message can be found in cache, rrsets for the message
+    /// will be added to different sections.
     ///
     /// \return return true if the message can be found, or else, return false.
     bool lookup(const isc::dns::Name& qname, 
@@ -75,7 +85,10 @@ public:
 
     /// \brief Update the message in the cache with the new one.
     /// \return return true if the message is updated into the cache,
-    /// or else, return false.
+    /// or else, return false. 
+    ///
+    /// \note, the function doesn't do any message
+    /// validation check, the user should make sure the message is valid.
     bool update(const isc::dns::Message& msg);
 
     /// \brief Update the rrset in the cache with the new one.
@@ -84,7 +97,7 @@ public:
     /// will be added into both of them.
     /// \return return false, if the class of the parameter rrset is
     /// allowed to be cached.
-    ///
+    /// 
     /// \overload 
     ///
     bool update(const isc::dns::RRset& rrset);