From acdc3b2396d284186610e6020a148b722c88d630 Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Mon, 10 Jun 2019 19:07:12 +0300 Subject: [PATCH] MXS-2556 Add master configuration handling to SmartRouter --- .../routing/smartrouter/smartrouter.cc | 76 ++++++++++++++++++- .../routing/smartrouter/smartrouter.hh | 33 ++++++++ .../routing/smartrouter/smartsession.cc | 3 + 3 files changed, 110 insertions(+), 2 deletions(-) diff --git a/server/modules/routing/smartrouter/smartrouter.cc b/server/modules/routing/smartrouter/smartrouter.cc index 4e9885eb7..ed9598222 100644 --- a/server/modules/routing/smartrouter/smartrouter.cc +++ b/server/modules/routing/smartrouter/smartrouter.cc @@ -16,6 +16,23 @@ #include +namespace +{ + +namespace smartquery +{ + +config::Specification specification(MXS_MODULE_NAME, config::Specification::ROUTER); + +config::ParamServer +master(&specification, + "master", + "The server/cluster to be treated as master, that is, the one where updates are sent."); + +} + +} + /** * The module entry point. * @@ -43,13 +60,68 @@ extern "C" MXS_MODULE* MXS_CREATE_MODULE() } }; + SmartRouter::Config::populate(info); + return &info; } +SmartRouter::Config::Config() + : config::Configuration(&smartquery::specification) + , m_master(this, &smartquery::master) +{ +} + +void SmartRouter::Config::populate(MXS_MODULE& module) +{ + smartquery::specification.populate(module); +} + +bool SmartRouter::Config::configure(const MXS_CONFIG_PARAMETER& params) +{ + return smartquery::specification.configure(*this, params); +} + +bool SmartRouter::Config::post_configure(const MXS_CONFIG_PARAMETER& params) +{ + bool rv = true; + + auto servers = params.get_server_list(CN_SERVERS); + // TODO: Check that the servers are local ones. + + auto it = std::find(servers.begin(), servers.end(), m_master.get()); + + if (it == servers.end()) + { + rv = false; + + std::string s; + + for (auto server : servers) + { + if (!s.empty()) + { + s+= ", "; + } + + s += server->name(); + } + + MXS_ERROR("The master server %s, is not one of the servers (%s) of the service.", + m_master.get()->name(), s.c_str()); + } + + return rv; +} + bool SmartRouter::configure(MXS_CONFIG_PARAMETER* pParams) { - // TODO ensure Servers are internal ones. Later TODO call routers directly. - return true; + if (!smartquery::specification.validate(*pParams)) + { + return false; + } + + // Since post_configure() has been overriden, this may fail. + return m_config.configure(*pParams); } SERVICE* SmartRouter::service() const diff --git a/server/modules/routing/smartrouter/smartrouter.hh b/server/modules/routing/smartrouter/smartrouter.hh index 526a5a9ed..e582c4e48 100644 --- a/server/modules/routing/smartrouter/smartrouter.hh +++ b/server/modules/routing/smartrouter/smartrouter.hh @@ -19,6 +19,7 @@ */ #include +#include #include class SmartRouterSession; @@ -28,6 +29,30 @@ class SmartRouterSession; class SmartRouter : public mxs::Router { public: + class Config : public config::Configuration + { + public: + Config(); + + Config(const Config&) = delete; + Config& operator=(const Config&) = delete; + + static void populate(MXS_MODULE& module); + + bool configure(const MXS_CONFIG_PARAMETER& params); + + SERVER* master() const + { + return m_master.get(); + } + + private: + bool post_configure(const MXS_CONFIG_PARAMETER& params) override; + + private: + config::Server m_master; + }; + static SmartRouter* create(SERVICE* pService, MXS_CONFIG_PARAMETER* pParams); SmartRouterSession* newSession(MXS_SESSION* pSession); @@ -38,6 +63,14 @@ public: bool configure(MXS_CONFIG_PARAMETER* pParams); SERVICE* service() const; + + const Config& config() const + { + return m_config; + } + private: SmartRouter(SERVICE* service); + + Config m_config; }; diff --git a/server/modules/routing/smartrouter/smartsession.cc b/server/modules/routing/smartrouter/smartsession.cc index 4518814b9..0425c8d09 100644 --- a/server/modules/routing/smartrouter/smartsession.cc +++ b/server/modules/routing/smartrouter/smartsession.cc @@ -121,6 +121,9 @@ SmartRouterSession* SmartRouterSession::create(SmartRouter* pRouter, MXS_SESSION { Clusters clusters; + SERVER* pMaster = pRouter->config().master(); + // TODO: Use pMaster below. + bool is_master = true; // TODO this will be read from config int master_pos = 0; // and this will be initialized to the position of the master