From: Marc Cromme Date: Tue, 2 Jan 2007 15:35:36 +0000 (+0000) Subject: added first shot on load balance filter which removes all but one vhost from Z39... X-Git-Tag: METAPROXY.1.0.8~56 X-Git-Url: http://lists.indexdata.dk/?a=commitdiff_plain;h=451ffea184560b0d10f369dfc3fccb0fa86b91a2;p=metaproxy-moved-to-github.git added first shot on load balance filter which removes all but one vhost from Z39.50 init package based on nice load balancing algorithm. Missing is still the gathering of workload data and the implementatin of the nice algorithm, but the framework is up and running --- diff --git a/etc/Makefile.am b/etc/Makefile.am index 2b4198d..f74d429 100644 --- a/etc/Makefile.am +++ b/etc/Makefile.am @@ -1,4 +1,4 @@ -# $Id: Makefile.am,v 1.7 2006-11-29 21:43:37 marc Exp $ +# $Id: Makefile.am,v 1.8 2007-01-02 15:35:36 marc Exp $ # This doesn't actually build anything, it just tests whether the # configuration files here correspond with the schema. @@ -7,6 +7,7 @@ etcdatadir = $(pkgdatadir)/etc xmlconfig = $(srcdir)/config-bytarget.xml \ $(srcdir)/config-local.xml \ + $(srcdir)/config-load-balance.xml \ $(srcdir)/config-record-transform.xml \ $(srcdir)/config-shared1.xml \ $(srcdir)/config-simple-multi.xml \ diff --git a/etc/config-load-balance.xml b/etc/config-load-balance.xml new file mode 100644 index 0000000..87bcb6c --- /dev/null +++ b/etc/config-load-balance.xml @@ -0,0 +1,46 @@ + + + + + + + 10 + @:9000 + + + + db9001 + localhost:9001/Default + + + db9002 + localhost:9002/Default + + + db9003 + localhost:9003/Default + + + Default + localhost:9001/Default + localhost:9002/Default + localhost:9003/Default + + + + VIRT + + + + LOAD + + + + + + + diff --git a/src/Makefile.am b/src/Makefile.am index ba5da16..b3fa8c0 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,4 +1,4 @@ -## $Id: Makefile.am,v 1.60 2006-12-28 14:59:44 marc Exp $ +## $Id: Makefile.am,v 1.61 2007-01-02 15:35:36 marc Exp $ MAINTAINERCLEANFILES = Makefile.in config.in config.hpp @@ -20,6 +20,7 @@ libmetaproxy_la_SOURCES = \ filter_bounce.cpp filter_bounce.hpp \ filter_frontend_net.cpp filter_frontend_net.hpp \ filter_http_file.cpp filter_http_file.hpp \ + filter_load_balance.cpp filter_load_balance.hpp \ filter_log.cpp filter_log.hpp \ filter_multi.cpp filter_multi.hpp \ filter_query_rewrite.cpp filter_query_rewrite.hpp \ diff --git a/src/factory_static.cpp b/src/factory_static.cpp index da415b3..6142d46 100644 --- a/src/factory_static.cpp +++ b/src/factory_static.cpp @@ -1,4 +1,4 @@ -/* $Id: factory_static.cpp,v 1.14 2006-12-28 14:59:44 marc Exp $ +/* $Id: factory_static.cpp,v 1.15 2007-01-02 15:35:36 marc Exp $ Copyright (c) 2005-2006, Index Data. See the LICENSE file for details @@ -20,6 +20,7 @@ #include "filter_bounce.hpp" #include "filter_frontend_net.hpp" #include "filter_http_file.hpp" +#include "filter_load_balance.hpp" #include "filter_log.hpp" #include "filter_multi.hpp" #include "filter_query_rewrite.hpp" @@ -41,6 +42,7 @@ mp::FactoryStatic::FactoryStatic() &metaproxy_1_filter_bounce, &metaproxy_1_filter_frontend_net, &metaproxy_1_filter_http_file, + &metaproxy_1_filter_load_balance, &metaproxy_1_filter_log, &metaproxy_1_filter_multi, &metaproxy_1_filter_query_rewrite, diff --git a/src/filter_load_balance.cpp b/src/filter_load_balance.cpp new file mode 100644 index 0000000..57bd390 --- /dev/null +++ b/src/filter_load_balance.cpp @@ -0,0 +1,172 @@ +/* $Id: filter_load_balance.cpp,v 1.1 2007-01-02 15:35:36 marc Exp $ + Copyright (c) 2005-2006, Index Data. + + See the LICENSE file for details + */ + +#include "config.hpp" +#include "filter.hpp" +#include "filter_load_balance.hpp" +#include "package.hpp" +#include "util.hpp" + +#include +#include + +#include + +#include + +namespace mp = metaproxy_1; +namespace yf = mp::filter; + +namespace metaproxy_1 { + namespace filter { + class LoadBalance::Impl { + public: + Impl(); + ~Impl(); + void process(metaproxy_1::Package & package); + void configure(const xmlNode * ptr); + private: + boost::mutex m_mutex; + }; + } +} + +// define Pimpl wrapper forwarding to Impl + +yf::LoadBalance::LoadBalance() : m_p(new Impl) +{ +} + +yf::LoadBalance::~LoadBalance() +{ // must have a destructor because of boost::scoped_ptr +} + +void yf::LoadBalance::configure(const xmlNode *xmlnode) +{ + m_p->configure(xmlnode); +} + +void yf::LoadBalance::process(mp::Package &package) const +{ + m_p->process(package); +} + + +// define Implementation stuff + + + +yf::LoadBalance::Impl::Impl() +{ +} + +yf::LoadBalance::Impl::~Impl() +{ +} + +void yf::LoadBalance::Impl::configure(const xmlNode *xmlnode) +{ +} + +void yf::LoadBalance::Impl::process(mp::Package &package) +{ + Z_GDU *gdu_req = package.request().get(); + + + // passing anything but z3950 packages + if (!gdu_req + || !(gdu_req->which == Z_GDU_Z3950)) + { + package.move(); + return; + } + + + // target selecting only on Z39.50 init request + if (gdu_req->u.z3950->which == Z_APDU_initRequest){ + + mp::odr odr_en(ODR_ENCODE); + Z_InitRequest *org_init = gdu_req->u.z3950->u.initRequest; + + // extracting virtual hosts + std::list vhosts; + + mp::util::remove_vhost_otherinfo(&(org_init->otherInfo), vhosts); + + //std::cout << "LoadBalance::Impl::process() vhosts: " + // << vhosts.size() << "\n"; + //std::cout << "LoadBalance::Impl::process()" << *gdu_req << "\n"; + + // choosing one target according to load-balancing algorithm + + if (vhosts.size()){ + std::string target; + + // getting timestamp for receiving of package + boost::posix_time::ptime receive_time + = boost::posix_time::microsec_clock::local_time(); + + // //<< receive_time << " " + // //<< to_iso_string(receive_time) << " " + //<< to_iso_extended_string(receive_time) << " " + // package.session().id(); + + { // scope for locking local target database + boost::mutex::scoped_lock scoped_lock(m_mutex); + target = *vhosts.begin(); + } + + + // copying new target into init package + mp::util::set_vhost_otherinfo(&(org_init->otherInfo), odr_en, target); + package.request() = gdu_req; + } + + } + + + // moving all Z39.50 package typess + package.move(); + + + + //boost::posix_time::ptime send_time + // = boost::posix_time::microsec_clock::local_time(); + + //boost::posix_time::time_duration duration = send_time - receive_time; + + + // { // scope for locking local target database + // boost::mutex::scoped_lock scoped_lock(m_mutex); + // target = *vhosts.begin(); + // } + + +} + + +static mp::filter::Base* filter_creator() +{ + return new mp::filter::LoadBalance; +} + +extern "C" { + struct metaproxy_1_filter_struct metaproxy_1_filter_load_balance = { + 0, + "load_balance", + filter_creator + }; +} + + +/* + * Local variables: + * c-basic-offset: 4 + * indent-tabs-mode: nil + * c-file-style: "stroustrup" + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ diff --git a/src/filter_load_balance.hpp b/src/filter_load_balance.hpp new file mode 100644 index 0000000..0ede931 --- /dev/null +++ b/src/filter_load_balance.hpp @@ -0,0 +1,41 @@ +/* $Id: filter_load_balance.hpp,v 1.1 2007-01-02 15:35:36 marc Exp $ + Copyright (c) 2005-2006, Index Data. + + See the LICENSE file for details + */ + +// Filter that does nothing. Use as load_balance for new filters +#ifndef FILTER_LOAD_BALANCE_HPP +#define FILTER_LOAD_BALANCE_HPP + +#include + +#include "filter.hpp" + +namespace metaproxy_1 { + namespace filter { + class LoadBalance : public Base { + class Impl; + boost::scoped_ptr m_p; + public: + LoadBalance(); + ~LoadBalance(); + void process(metaproxy_1::Package & package) const; + void configure(const xmlNode * ptr); + }; + } +} + +extern "C" { + extern struct metaproxy_1_filter_struct metaproxy_1_filter_load_balance; +} + +#endif +/* + * Local variables: + * c-basic-offset: 4 + * indent-tabs-mode: nil + * c-file-style: "stroustrup" + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ diff --git a/src/util.cpp b/src/util.cpp index c92fc0b..58d23f9 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1,4 +1,4 @@ -/* $Id: util.cpp,v 1.23 2006-10-04 14:04:00 marc Exp $ +/* $Id: util.cpp,v 1.24 2007-01-02 15:35:36 marc Exp $ Copyright (c) 2005-2006, Index Data. See the LICENSE file for details @@ -334,6 +334,14 @@ void mp_util::set_vhost_otherinfo( } } +void mp_util::set_vhost_otherinfo( + Z_OtherInformation **otherInformation, ODR odr, + const std::string vhost, const int cat) +{ + yaz_oi_set_string_oidval(otherInformation, odr, + VAL_PROXY, cat, vhost.c_str()); +} + void mp_util::split_zurl(std::string zurl, std::string &host, std::list &db) { diff --git a/src/util.hpp b/src/util.hpp index 7727391..efba787 100644 --- a/src/util.hpp +++ b/src/util.hpp @@ -1,4 +1,4 @@ -/* $Id: util.hpp,v 1.22 2006-10-04 14:04:00 marc Exp $ +/* $Id: util.hpp,v 1.23 2007-01-02 15:35:36 marc Exp $ Copyright (c) 2005-2006, Index Data. See the LICENSE file for details @@ -72,6 +72,11 @@ namespace metaproxy_1 { int remove_vhost_otherinfo(Z_OtherInformation **otherInformation, std::list &vhosts); + void set_vhost_otherinfo(Z_OtherInformation **otherInformation, + ODR odr, + const std::string vhost, + const int cat = 1 ); + void set_vhost_otherinfo(Z_OtherInformation **otherInformation, ODR odr, const std::list &vhosts); diff --git a/xml/schema/metaproxy.rnc b/xml/schema/metaproxy.rnc index c31d760..d6b6057 100644 --- a/xml/schema/metaproxy.rnc +++ b/xml/schema/metaproxy.rnc @@ -1,5 +1,5 @@ # Metaproxy XML config file schemas -# $Id: metaproxy.rnc,v 1.12 2006-12-28 14:59:44 marc Exp $ +# $Id: metaproxy.rnc,v 1.13 2007-01-02 15:35:36 marc Exp $ # # Copyright (c) 2005-2006, Index Data. # @@ -50,6 +50,7 @@ filter = | filter_bounce | filter_frontend_net | filter_http_file + | filter_load_balance | filter_log | filter_multi | filter_query_rewrite @@ -99,6 +100,11 @@ filter_http_file = element mp:prefix { xsd:string } } +filter_load_balance = + attribute type { "load_balance" }, + attribute id { xsd:NCName }?, + attribute name { xsd:NCName }? + filter_log = attribute type { "log" }, attribute id { xsd:NCName }?,