[svn] commit: r3412 - in /experiments/kambe-auth-stats/src/bin/auth: asio_link.cc auth_srv.cc auth_srv.h main.cc
BIND 10 source code commits
bind10-changes at lists.isc.org
Tue Nov 2 07:22:46 UTC 2010
Author: naokikambe
Date: Tue Nov 2 07:22:46 2010
New Revision: 3412
Log:
change groping codes
-- add simple implement to sending to stats module periodically
Modified:
experiments/kambe-auth-stats/src/bin/auth/asio_link.cc
experiments/kambe-auth-stats/src/bin/auth/auth_srv.cc
experiments/kambe-auth-stats/src/bin/auth/auth_srv.h
experiments/kambe-auth-stats/src/bin/auth/main.cc
Modified: experiments/kambe-auth-stats/src/bin/auth/asio_link.cc
==============================================================================
--- experiments/kambe-auth-stats/src/bin/auth/asio_link.cc (original)
+++ experiments/kambe-auth-stats/src/bin/auth/asio_link.cc Tue Nov 2 07:22:46 2010
@@ -551,13 +551,21 @@
typedef boost::shared_ptr<UDPServer> UDPServerPtr;
typedef boost::shared_ptr<TCPServer> TCPServerPtr;
+ typedef boost::shared_ptr<asio::deadline_timer> DlTimerPtr;
UDPServerPtr udp4_server_;
UDPServerPtr udp6_server_;
TCPServerPtr tcp4_server_;
TCPServerPtr tcp6_server_;
+ DlTimerPtr dltimer_;
// This member is used only for testing at the moment.
IOService::IOCallBack callback_;
+
+ // timeout seconds
+ static const size_t DEFAULT_TIMEOUT = 3;
+
+ // async_wait handler
+ void AsWaitHandler(const asio::error_code& error);
};
IOServiceImpl::IOServiceImpl(AuthSrv* auth_server, const char& port,
@@ -597,6 +605,11 @@
tcp6_server_ = TCPServerPtr(new TCPServer(auth_server, io_service_,
*v6addr, portnum));
}
+ // start deadline_timer
+ dltimer_.reset(new asio::deadline_timer(io_service_));
+ dltimer_->expires_from_now(boost::posix_time::seconds(DEFAULT_TIMEOUT));
+ dltimer_->async_wait(boost::bind(&IOServiceImpl::AsWaitHandler,
+ this, asio::placeholders::error));
} catch (const asio::system_error& err) {
// We need to catch and convert any ASIO level exceptions.
// This can happen for unavailable address, binding a privilege port
@@ -604,6 +617,22 @@
isc_throw(IOError, "Failed to initialize network servers: " <<
err.what());
}
+}
+
+void
+IOServiceImpl::AsWaitHandler(const asio::error_code& error)
+{
+ if (error == asio::error::operation_aborted) {
+ isc_throw(IOError, "wait_handler: Error: " << error.message());
+ return;
+ }
+ if (!auth_server_->sendtoStats()) {
+ isc_throw(IOError, "Failed to send statistics data");
+ }
+ dltimer_->expires_at(dltimer_->expires_at()
+ + boost::posix_time::seconds(DEFAULT_TIMEOUT));
+ dltimer_->async_wait(boost::bind(&IOServiceImpl::AsWaitHandler,
+ this, asio::placeholders::error));
}
IOService::IOService(AuthSrv* auth_server, const char& port,
Modified: experiments/kambe-auth-stats/src/bin/auth/auth_srv.cc
==============================================================================
--- experiments/kambe-auth-stats/src/bin/auth/auth_srv.cc (original)
+++ experiments/kambe-auth-stats/src/bin/auth/auth_srv.cc Tue Nov 2 07:22:46 2010
@@ -90,6 +90,7 @@
bool verbose_mode_;
AbstractSession* xfrin_session_;
+ AbstractSession* stats_session_;
bool xfrout_connected_;
AbstractXfroutClient& xfrout_client_;
@@ -99,6 +100,17 @@
/// Hot spot cache
isc::datasrc::HotCache cache_;
+
+ // increment query counters
+ bool addQryCounts(const IOMessage& io_message);
+
+ // send statistics data to stats daemon
+ bool sendtoStats() const;
+
+private:
+ /// the query counters, just incremental
+ unsigned long int udp_q_count_;
+ unsigned long int tcp_q_count_;
};
AuthSrvImpl::AuthSrvImpl(const bool use_cache,
@@ -106,7 +118,8 @@
config_session_(NULL), verbose_mode_(false),
xfrin_session_(NULL),
xfrout_connected_(false),
- xfrout_client_(xfrout_client)
+ xfrout_client_(xfrout_client),
+ udp_q_count_(0), tcp_q_count_(0)
{
// cur_datasrc_ is automatically initialized by the default constructor,
// effectively being an empty (sqlite) data source. once ccsession is up
@@ -210,6 +223,11 @@
}
void
+AuthSrv::setStatsSession(AbstractSession* stats_session) {
+ impl_->stats_session_ = stats_session;
+}
+
+void
AuthSrv::setConfigSession(ModuleCCSession* config_session) {
impl_->config_session_ = config_session;
}
@@ -265,6 +283,9 @@
if (impl_->verbose_mode_) {
cerr << "[b10-auth] received a message:\n" << message.toText() << endl;
}
+
+ // increment query counters
+ impl_->addQryCounts(io_message);
// Perform further protocol-level validation.
@@ -544,3 +565,92 @@
return (isc::config::createAnswer(1, error.what()));
}
}
+
+bool
+AuthSrvImpl::addQryCounts(const IOMessage& io_message)
+{
+ // get protocol
+ int protocol = io_message.getSocket().getProtocol();
+
+ if (protocol == IPPROTO_TCP) {
+ // in case of TCP
+ tcp_q_count_++;
+ } else if (protocol == IPPROTO_UDP){
+ // in case of UDP
+ udp_q_count_++;
+ } else {
+ // unknown protocol
+ cerr << "[b10-auth] got unknown protocol: " << protocol
+ << endl;
+ return (false);
+ }
+ return (true);
+}
+
+bool
+AuthSrvImpl::sendtoStats() const
+{
+ // This method sends increment counter to the stats module.
+ // TODO:
+ // This implementation is very very ad-hoc style. It may bring
+ // worse performance in this style. It must be more considered and
+ // must be replaced with asynchronous one which is low const.
+ if (stats_session_ == NULL) {
+ if (verbose_mode_) {
+ cerr << "[b10-auth] "
+ "session interface for stats is not available" << endl;
+ }
+ return (false);
+ }
+
+ // construct command string
+ static const string command_head =
+ "{ \"command\": [ \"set\", { \"stats_data\": {";
+ static const string command_udp = "\"auth.queries.udp\"";
+ static const string command_tcp = "\"auth.queries.tcp\"";
+ static const string command_foot = "} } ] }";
+ char udp_q_count_str_[256] = "";
+ char tcp_q_count_str_[256] = "";
+ sprintf(udp_q_count_str_, "%ld", udp_q_count_);
+ sprintf(tcp_q_count_str_, "%ld", tcp_q_count_);
+
+ string command_str = command_head
+ + command_udp + ":" + udp_q_count_str_ + ","
+ + command_tcp + ":" + tcp_q_count_str_
+ + command_foot;
+
+ // send to stats module
+ try {
+ ConstElementPtr stats_command = Element::fromJSON(command_str);
+ const unsigned int seq =
+ stats_session_->group_sendmsg(stats_command,
+ "Stats", "*", "*");
+ if (verbose_mode_) {
+ cout << "[b10-auth] send statistics: " << command_str << endl;
+ }
+ ConstElementPtr env, answer, parsed_answer;
+ stats_session_->group_recvmsg(env, answer, false, seq);
+ int rcode;
+ parsed_answer = parseAnswer(rcode, answer);
+ if (rcode != 0) {
+ if (verbose_mode_) {
+ cerr << "[b10-auth] failed to send statistics: "
+ << parsed_answer->str() << endl;
+ }
+ return (false);
+ }
+ } catch (const Exception& ex) {
+ if (verbose_mode_) {
+ cerr << "[b10-auth] failed to send statistics: "
+ << ex.what() << endl;
+ }
+ return (false);
+ }
+ return (true);
+}
+
+bool
+AuthSrv::sendtoStats() const
+{
+ return (impl_->sendtoStats());
+}
Modified: experiments/kambe-auth-stats/src/bin/auth/auth_srv.h
==============================================================================
--- experiments/kambe-auth-stats/src/bin/auth/auth_srv.h (original)
+++ experiments/kambe-auth-stats/src/bin/auth/auth_srv.h Tue Nov 2 07:22:46 2010
@@ -199,6 +199,13 @@
/// is shutdown.
///
void setXfrinSession(isc::cc::AbstractSession* xfrin_session);
+
+ // some one for stats module
+ void setStatsSession(isc::cc::AbstractSession* stats_session);
+
+ // send statistics data to stats daemon
+ bool sendtoStats() const;
+
private:
AuthSrvImpl* impl_;
};
Modified: experiments/kambe-auth-stats/src/bin/auth/main.cc
==============================================================================
--- experiments/kambe-auth-stats/src/bin/auth/main.cc (original)
+++ experiments/kambe-auth-stats/src/bin/auth/main.cc Tue Nov 2 07:22:46 2010
@@ -154,7 +154,9 @@
// XXX: we should eventually pass io_service here.
Session* cc_session = NULL;
Session* xfrin_session = NULL;
+ Session* stats_session = NULL;
bool xfrin_session_established = false; // XXX (see Trac #287)
+ bool stats_session_established = false;
ModuleCCSession* config_session = NULL;
string xfrout_socket_path;
if (getenv("B10_FROM_BUILD") != NULL) {
@@ -209,12 +211,19 @@
xfrin_session_established = true;
cout << "[b10-auth] Xfrin session channel established." << endl;
+ stats_session = new Session(io_service->get_io_service());
+ cout << "[b10-auth] Stats session channel created." << endl;
+ stats_session->establish(NULL);
+ stats_session_established = true;
+ cout << "[b10-auth] Stats session channel established." << endl;
+
// XXX: with the current interface to asio_link we have to create
// auth_server before io_service while Session needs io_service.
// In a next step of refactoring we should make asio_link independent
// from auth_server, and create io_service, auth_server, and
// sessions in that order.
auth_server->setXfrinSession(xfrin_session);
+ auth_server->setStatsSession(stats_session);
auth_server->setConfigSession(config_session);
auth_server->updateConfig(ElementPtr());
@@ -228,7 +237,11 @@
if (xfrin_session_established) {
xfrin_session->disconnect();
}
-
+ if (stats_session_established) {
+ stats_session->disconnect();
+ }
+
+ delete stats_session;
delete xfrin_session;
delete config_session;
delete cc_session;
More information about the bind10-changes
mailing list