[svn] commit: r3998 - in /trunk: ./ src/bin/auth/ src/bin/auth/benchmarks/ src/bin/auth/tests/ src/bin/bind10/ src/bin/stats/tests/isc/util/ src/lib/datasrc/ src/lib/datasrc/tests/

BIND 10 source code commits bind10-changes at lists.isc.org
Thu Dec 23 23:43:27 UTC 2010


Author: jinmei
Date: Thu Dec 23 23:43:27 2010
New Revision: 3998

Log:
merged trac #446: config knob for in memory data source.

Added:
    trunk/src/bin/auth/config.cc
      - copied unchanged from r3997, branches/trac446/src/bin/auth/config.cc
    trunk/src/bin/auth/config.h
      - copied unchanged from r3997, branches/trac446/src/bin/auth/config.h
    trunk/src/bin/auth/tests/config_unittest.cc
      - copied unchanged from r3997, branches/trac446/src/bin/auth/tests/config_unittest.cc
Modified:
    trunk/   (props changed)
    trunk/src/bin/auth/Makefile.am
    trunk/src/bin/auth/auth.spec.pre.in
    trunk/src/bin/auth/auth_srv.cc
    trunk/src/bin/auth/auth_srv.h
    trunk/src/bin/auth/benchmarks/Makefile.am
    trunk/src/bin/auth/change_user.cc
    trunk/src/bin/auth/common.h
    trunk/src/bin/auth/main.cc
    trunk/src/bin/auth/tests/Makefile.am
    trunk/src/bin/auth/tests/auth_srv_unittest.cc
    trunk/src/bin/bind10/bind10.py.in   (props changed)
    trunk/src/bin/stats/tests/isc/util/   (props changed)
    trunk/src/lib/datasrc/memory_datasrc.cc
    trunk/src/lib/datasrc/memory_datasrc.h
    trunk/src/lib/datasrc/tests/memory_datasrc_unittest.cc

Modified: trunk/src/bin/auth/Makefile.am
==============================================================================
--- trunk/src/bin/auth/Makefile.am (original)
+++ trunk/src/bin/auth/Makefile.am Thu Dec 23 23:43:27 2010
@@ -39,6 +39,7 @@
 b10_auth_SOURCES = auth_srv.cc auth_srv.h
 b10_auth_SOURCES += query.cc query.h
 b10_auth_SOURCES += change_user.cc change_user.h
+b10_auth_SOURCES += config.cc config.h
 b10_auth_SOURCES += common.h
 b10_auth_SOURCES += main.cc
 b10_auth_LDADD =  $(top_builddir)/src/lib/datasrc/libdatasrc.la

Modified: trunk/src/bin/auth/auth.spec.pre.in
==============================================================================
--- trunk/src/bin/auth/auth.spec.pre.in (original)
+++ trunk/src/bin/auth/auth.spec.pre.in Thu Dec 23 23:43:27 2010
@@ -7,6 +7,51 @@
         "item_type": "string",
         "item_optional": true,
         "item_default": "@@LOCALSTATEDIR@@/@PACKAGE@/zone.sqlite3"
+      },
+      { "item_name": "datasources",
+        "item_type": "list",
+        "item_optional": true,
+        "item_default": [],
+	"list_item_spec": {
+          "item_name": "list_element",
+          "item_type": "map",
+          "item_optional": false,
+          "item_default": {},
+	  "map_item_spec": [
+	    { "item_name": "type",
+	      "item_type": "string",
+	      "item_optional": false,
+	      "item_default": ""
+	    },
+	    { "item_name": "class",
+	      "item_type": "string",
+	      "item_optional": false,
+	      "item_default": "IN"
+	    },
+	    { "item_name": "zones",
+	      "item_type": "list",
+	      "item_optional": false,
+	      "item_default": [],
+	      "list_item_spec": {
+	        "item_name": "list_element",
+	        "item_type": "map",
+	        "item_optional": true,
+	        "map_item_spec": [
+		  { "item_name": "origin",
+		    "item_type": "string",
+		    "item_optional": false,
+		    "item_default": ""
+		  },
+		  { "item_name": "file",
+		    "item_type": "string",
+		    "item_optional": false,
+		    "item_default": ""
+		  }
+		]
+	      }
+	    }
+	  ]
+        }
       }
     ],
     "commands": [

Modified: trunk/src/bin/auth/auth_srv.cc
==============================================================================
--- trunk/src/bin/auth/auth_srv.cc (original)
+++ trunk/src/bin/auth/auth_srv.cc Thu Dec 23 23:43:27 2010
@@ -45,12 +45,14 @@
 
 #include <datasrc/query.h>
 #include <datasrc/data_source.h>
+#include <datasrc/memory_datasrc.h>
 #include <datasrc/static_datasrc.h>
 #include <datasrc/sqlite3_datasrc.h>
 
 #include <xfr/xfrout_client.h>
 
 #include <auth/common.h>
+#include <auth/config.h>
 #include <auth/auth_srv.h>
 
 using namespace std;
@@ -89,6 +91,9 @@
     ModuleCCSession* config_session_;
     bool verbose_mode_;
     AbstractSession* xfrin_session_;
+
+    /// In-memory data source.  Currently class IN only for simplicity.
+    AuthSrv::MemoryDataSrcPtr memory_datasrc_;
 
     /// Hot spot cache
     isc::datasrc::HotCache cache_;
@@ -288,6 +293,39 @@
 ModuleCCSession*
 AuthSrv::getConfigSession() const {
     return (impl_->config_session_);
+}
+
+AuthSrv::ConstMemoryDataSrcPtr
+AuthSrv::getMemoryDataSrc(const RRClass& rrclass) const {
+    // XXX: for simplicity, we only support the IN class right now.
+    if (rrclass != RRClass::IN()) {
+        isc_throw(InvalidParameter,
+                  "Memory data source is not supported for RR class "
+                  << rrclass);
+    }
+    return (impl_->memory_datasrc_);
+}
+
+void
+AuthSrv::setMemoryDataSrc(const isc::dns::RRClass& rrclass,
+                          MemoryDataSrcPtr memory_datasrc)
+{
+    // XXX: see above
+    if (rrclass != RRClass::IN()) {
+        isc_throw(InvalidParameter,
+                  "Memory data source is not supported for RR class "
+                  << rrclass);
+    }
+    if (impl_->verbose_mode_) {
+        if (!impl_->memory_datasrc_ && memory_datasrc) {
+            cerr << "[b10-auth] Memory data source is enabled for class "
+                 << rrclass << endl;
+        } else if (impl_->memory_datasrc_ && !memory_datasrc) {
+            cerr << "[b10-auth] Memory data source is disabled for class "
+                 << rrclass << endl;
+        }
+    }
+    impl_->memory_datasrc_ = memory_datasrc;
 }
 
 void
@@ -609,6 +647,9 @@
     try {
         // the ModuleCCSession has already checked if we have
         // the correct ElementPtr type as specified in our .spec file
+        if (new_config) {
+            configureAuthServer(*this, new_config);
+        }
         return (impl_->setDbFile(new_config));
     } catch (const isc::Exception& error) {
         if (impl_->verbose_mode_) {

Modified: trunk/src/bin/auth/auth_srv.h
==============================================================================
--- trunk/src/bin/auth/auth_srv.h (original)
+++ trunk/src/bin/auth/auth_srv.h Thu Dec 23 23:43:27 2010
@@ -19,12 +19,19 @@
 
 #include <string>
 
+// For MemoryDataSrcPtr below.  This should be a temporary definition until
+// we reorganize the data source framework.
+#include <boost/shared_ptr.hpp>
+
 #include <cc/data.h>
 #include <config/ccsession.h>
 
 #include <asiolink/asiolink.h>
 
 namespace isc {
+namespace datasrc {
+class MemoryDataSrc;
+}
 namespace xfr {
 class AbstractXfroutClient;
 }
@@ -224,6 +231,53 @@
     ///
     void setXfrinSession(isc::cc::AbstractSession* xfrin_session);
 
+    /// A shared pointer type for \c MemoryDataSrc.
+    ///
+    /// This is defined inside the \c AuthSrv class as it's supposed to be
+    /// a short term interface until we integrate the in-memory and other
+    /// data source frameworks.
+    typedef boost::shared_ptr<isc::datasrc::MemoryDataSrc> MemoryDataSrcPtr;
+
+    /// An immutable shared pointer type for \c MemoryDataSrc.
+    typedef boost::shared_ptr<const isc::datasrc::MemoryDataSrc>
+    ConstMemoryDataSrcPtr;
+
+    /// Returns the in-memory data source configured for the \c AuthSrv,
+    /// if any.
+    ///
+    /// The in-memory data source is configured per RR class.  However,
+    /// the data source may not be available for all RR classes.
+    /// If it is not available for the specified RR class, an exception of
+    /// class \c InvalidParameter will be thrown.
+    /// This method never throws an exception otherwise.
+    ///
+    /// Even for supported RR classes, the in-memory data source is not
+    /// configured by default.  In that case a NULL (shared) pointer will
+    /// be returned.
+    ///
+    /// \param rrclass The RR class of the requested in-memory data source.
+    /// \return A pointer to the in-memory data source, if configured;
+    /// otherwise NULL.
+    ConstMemoryDataSrcPtr
+    getMemoryDataSrc(const isc::dns::RRClass& rrclass) const;
+
+    /// Sets or replaces the in-memory data source of the specified RR class.
+    ///
+    /// As noted in \c getMemoryDataSrc(), some RR classes may not be
+    /// supported, in which case an exception of class \c InvalidParameter
+    /// will be thrown.
+    /// This method never throws an exception otherwise.
+    ///
+    /// If there is already an in memory data source configured, it will be
+    /// replaced with the newly specified one.
+    /// \c memory_datasrc can be NULL, in which case it will (re)disable the
+    /// in-memory data source.
+    ///
+    /// \param rrclass The RR class of the in-memory data source to be set.
+    /// \param memory_datasrc A (shared) pointer to \c MemoryDataSrc to be set.
+    void setMemoryDataSrc(const isc::dns::RRClass& rrclass,
+                          MemoryDataSrcPtr memory_datasrc);
+
 private:
     AuthSrvImpl* impl_;
     asiolink::IOService* io_service_;

Modified: trunk/src/bin/auth/benchmarks/Makefile.am
==============================================================================
--- trunk/src/bin/auth/benchmarks/Makefile.am (original)
+++ trunk/src/bin/auth/benchmarks/Makefile.am Thu Dec 23 23:43:27 2010
@@ -9,6 +9,7 @@
 noinst_PROGRAMS = query_bench
 query_bench_SOURCES = query_bench.cc
 query_bench_SOURCES += ../auth_srv.h ../auth_srv.cc
+query_bench_SOURCES += ../config.h ../config.cc
 
 query_bench_LDADD = $(top_builddir)/src/lib/dns/libdns++.la
 query_bench_LDADD += $(top_builddir)/src/lib/exceptions/libexceptions.la

Modified: trunk/src/bin/auth/change_user.cc
==============================================================================
--- trunk/src/bin/auth/change_user.cc (original)
+++ trunk/src/bin/auth/change_user.cc Thu Dec 23 23:43:27 2010
@@ -26,6 +26,7 @@
 #include <auth/common.h>
 
 using namespace boost;
+using namespace std;
 
 void
 changeUser(const char* const username) {
@@ -42,14 +43,14 @@
         }
     }
     if (runas_pw == NULL) {
-        isc_throw(FatalError, "Unknown user name or UID:" << username);
+        throw FatalError("Unknown user name or UID:" + string(username));
     }
 
     if (setgid(runas_pw->pw_gid) < 0) {
-        isc_throw(FatalError, "setgid() failed: " << strerror(errno));
+        throw FatalError("setgid() failed: " + string(strerror(errno)));
     }
 
     if (setuid(runas_pw->pw_uid) < 0) {
-        isc_throw(FatalError, "setuid() failed: " << strerror(errno));
+        throw FatalError("setuid() failed: " + string(strerror(errno)));
     }
 }

Modified: trunk/src/bin/auth/common.h
==============================================================================
--- trunk/src/bin/auth/common.h (original)
+++ trunk/src/bin/auth/common.h Thu Dec 23 23:43:27 2010
@@ -17,12 +17,18 @@
 #ifndef __COMMON_H
 #define __COMMON_H 1
 
-#include <exceptions/exceptions.h>
+#include <stdexcept>
+#include <string>
 
-class FatalError : public isc::Exception {
+/// An exception class that is thrown in an unrecoverable error condition.
+///
+/// This exception should not be caught except at the highest level of
+/// the application only for terminating the program gracefully, and so
+/// it cannot be a derived class of \c isc::Exception.
+class FatalError : public std::runtime_error {
 public:
-    FatalError(const char* file, size_t line, const char* what) :
-        isc::Exception(file, line, what) {}
+    FatalError(const std::string& what) : std::runtime_error(what)
+    {}
 };
 
 #endif // __COMMON_H

Modified: trunk/src/bin/auth/main.cc
==============================================================================
--- trunk/src/bin/auth/main.cc (original)
+++ trunk/src/bin/auth/main.cc Thu Dec 23 23:43:27 2010
@@ -41,6 +41,7 @@
 
 #include <auth/spec_config.h>
 #include <auth/common.h>
+#include <auth/config.h>
 #include <auth/change_user.h>
 #include <auth/auth_srv.h>
 #include <asiolink/asiolink.h>
@@ -235,7 +236,13 @@
         // from auth_server, and create io_service, auth_server, and
         // sessions in that order.
         auth_server->setXfrinSession(xfrin_session);
+
+        // Configure the server.  configureAuthServer() is expected to install
+        // all initial configurations, but as a short term workaround we
+        // handle the traditional "database_file" setup by directly calling
+        // updateConfig().
         auth_server->setConfigSession(config_session);
+        configureAuthServer(*auth_server, config_session->getFullConfig());
         auth_server->updateConfig(ElementPtr());
 
         cout << "[b10-auth] Server started." << endl;

Modified: trunk/src/bin/auth/tests/Makefile.am
==============================================================================
--- trunk/src/bin/auth/tests/Makefile.am (original)
+++ trunk/src/bin/auth/tests/Makefile.am Thu Dec 23 23:43:27 2010
@@ -21,7 +21,9 @@
 run_unittests_SOURCES += ../auth_srv.h ../auth_srv.cc
 run_unittests_SOURCES += ../query.h ../query.cc
 run_unittests_SOURCES += ../change_user.h ../change_user.cc
+run_unittests_SOURCES += ../config.h ../config.cc
 run_unittests_SOURCES += auth_srv_unittest.cc
+run_unittests_SOURCES += config_unittest.cc
 run_unittests_SOURCES += query_unittest.cc
 run_unittests_SOURCES += change_user_unittest.cc
 run_unittests_SOURCES += run_unittests.cc

Modified: trunk/src/bin/auth/tests/auth_srv_unittest.cc
==============================================================================
--- trunk/src/bin/auth/tests/auth_srv_unittest.cc (original)
+++ trunk/src/bin/auth/tests/auth_srv_unittest.cc Thu Dec 23 23:43:27 2010
@@ -15,6 +15,7 @@
 // $Id$
 
 #include <config.h>
+#include <datasrc/memory_datasrc.h>
 #include <auth/auth_srv.h>
 #include <testutils/srv_unittest.h>
 
@@ -36,11 +37,12 @@
 
 class AuthSrvTest : public SrvTestBase {
 protected:
-    AuthSrvTest() : server(true, xfrout) {
+    AuthSrvTest() : server(true, xfrout), rrclass(RRClass::IN()) {
         server.setXfrinSession(&notify_session);
     }
     MockXfroutClient xfrout;
     AuthSrv server;
+    const RRClass rrclass;
 };
 
 // Unsupported requests.  Should result in NOTIMP.
@@ -325,11 +327,11 @@
 }
 
 void
-updateConfig(AuthSrv* server, const char* const dbfile,
+updateConfig(AuthSrv* server, const char* const config_data,
              const bool expect_success)
 {
     ConstElementPtr config_answer =
-        server->updateConfig(Element::fromJSON(dbfile));
+        server->updateConfig(Element::fromJSON(config_data));
     EXPECT_EQ(Element::map, config_answer->getType());
     EXPECT_TRUE(config_answer->contains("result"));
 
@@ -381,6 +383,19 @@
                 QR_FLAG | AA_FLAG, 1, 1, 1, 0);
 }
 
+TEST_F(AuthSrvTest, updateWithMemoryDataSrc) {
+    // Test configuring memory data source.  Detailed test cases are covered
+    // in the configuration tests.  We only check the AuthSrv interface here.
+
+    // By default memory data source isn't enabled
+    EXPECT_EQ(AuthSrv::MemoryDataSrcPtr(), server.getMemoryDataSrc(rrclass));
+    updateConfig(&server,
+                 "{\"datasources\": [{\"type\": \"memory\"}]}", true);
+    // after successful configuration, we should have one (with empty zoneset).
+    ASSERT_NE(AuthSrv::MemoryDataSrcPtr(), server.getMemoryDataSrc(rrclass));
+    EXPECT_EQ(0, server.getMemoryDataSrc(rrclass)->getZoneCount());
+}
+
 TEST_F(AuthSrvTest, cacheSlots) {
     // simple check for the get/set operations
     server.setCacheSlots(10);    // 10 = arbitrary choice

Modified: trunk/src/lib/datasrc/memory_datasrc.cc
==============================================================================
--- trunk/src/lib/datasrc/memory_datasrc.cc (original)
+++ trunk/src/lib/datasrc/memory_datasrc.cc Thu Dec 23 23:43:27 2010
@@ -90,7 +90,10 @@
 /// For now, \c MemoryDataSrc only contains a \c ZoneTable object, which
 /// consists of (pointers to) \c MemoryZone objects, we may add more
 /// member variables later for new features.
-struct MemoryDataSrc::MemoryDataSrcImpl {
+class MemoryDataSrc::MemoryDataSrcImpl {
+public:
+    MemoryDataSrcImpl() : zone_count(0) {}
+    unsigned int zone_count;
     ZoneTable zone_table;
 };
 
@@ -101,13 +104,23 @@
     delete impl_;
 }
 
+unsigned int
+MemoryDataSrc::getZoneCount() const {
+    return (impl_->zone_count);
+}
+
 result::Result
 MemoryDataSrc::addZone(ZonePtr zone) {
     if (!zone) {
         isc_throw(InvalidParameter,
                   "Null pointer is passed to MemoryDataSrc::addZone()");
     }
-    return (impl_->zone_table.addZone(zone));
+
+    const result::Result result = impl_->zone_table.addZone(zone);
+    if (result == result::SUCCESS) {
+        ++impl_->zone_count;
+    }
+    return (result);
 }
 
 MemoryDataSrc::FindResult

Modified: trunk/src/lib/datasrc/memory_datasrc.h
==============================================================================
--- trunk/src/lib/datasrc/memory_datasrc.h (original)
+++ trunk/src/lib/datasrc/memory_datasrc.h Thu Dec 23 23:43:27 2010
@@ -149,6 +149,13 @@
     ~MemoryDataSrc();
     //@}
 
+    /// Return the number of zones stored in the data source.
+    ///
+    /// This method never throws an exception.
+    ///
+    /// \return The number of zones stored in the data source.
+    unsigned int getZoneCount() const;
+
     /// Add a \c Zone to the \c MemoryDataSrc.
     ///
     /// \c Zone must not be associated with a NULL pointer; otherwise
@@ -185,7 +192,7 @@
     FindResult findZone(const isc::dns::Name& name) const;
 
 private:
-    struct MemoryDataSrcImpl;
+    class MemoryDataSrcImpl;
     MemoryDataSrcImpl* impl_;
 };
 }

Modified: trunk/src/lib/datasrc/tests/memory_datasrc_unittest.cc
==============================================================================
--- trunk/src/lib/datasrc/tests/memory_datasrc_unittest.cc (original)
+++ trunk/src/lib/datasrc/tests/memory_datasrc_unittest.cc Thu Dec 23 23:43:27 2010
@@ -28,8 +28,9 @@
 
 class MemoryDataSrcTest : public ::testing::Test {
 protected:
-    MemoryDataSrcTest()
+    MemoryDataSrcTest() : rrclass(RRClass::IN())
     {}
+    RRClass rrclass;
     MemoryDataSrc memory_datasrc;
 };
 
@@ -112,6 +113,23 @@
               memory_datasrc.findZone(Name("z.i.g.h")).zone->getOrigin());
 }
 
+TEST_F(MemoryDataSrcTest, getZoneCount) {
+    EXPECT_EQ(0, memory_datasrc.getZoneCount());
+    memory_datasrc.addZone(
+                  ZonePtr(new MemoryZone(rrclass, Name("example.com"))));
+    EXPECT_EQ(1, memory_datasrc.getZoneCount());
+
+    // duplicate add.  counter shouldn't change
+    memory_datasrc.addZone(
+                  ZonePtr(new MemoryZone(rrclass, Name("example.com"))));
+    EXPECT_EQ(1, memory_datasrc.getZoneCount());
+
+    // add one more
+    memory_datasrc.addZone(
+                  ZonePtr(new MemoryZone(rrclass, Name("example.org"))));
+    EXPECT_EQ(2, memory_datasrc.getZoneCount());
+}
+
 /// \brief Test fixture for the MemoryZone class
 class MemoryZoneTest : public ::testing::Test {
 public:
@@ -137,5 +155,4 @@
     ASSERT_EQ(class_, zone_.getClass());
     ASSERT_EQ(origin_, zone_.getOrigin());
 }
-
 }




More information about the bind10-changes mailing list