From 88b87aceb57fae3706066f5c704f8102d4c5761c Mon Sep 17 00:00:00 2001 From: Esa Korhonen Date: Mon, 27 Mar 2017 18:53:30 +0300 Subject: [PATCH] HintRouter: use a subfunction for connecting to backends Also, removed needless runtime capabilities + other fixes according to comments. --- .../modules/routing/hintrouter/hintrouter.cc | 68 ++++++++----------- .../modules/routing/hintrouter/hintrouter.hh | 8 ++- .../routing/hintrouter/hintrouterdefs.hh | 2 +- 3 files changed, 37 insertions(+), 41 deletions(-) diff --git a/server/modules/routing/hintrouter/hintrouter.cc b/server/modules/routing/hintrouter/hintrouter.cc index 265d57cba..2b8fd9831 100644 --- a/server/modules/routing/hintrouter/hintrouter.cc +++ b/server/modules/routing/hintrouter/hintrouter.cc @@ -106,23 +106,7 @@ HintRouterSession* HintRouter::newSession(MXS_SESSION *pSession) if (master_ref) { // Connect to master - HR_DEBUG("Connecting to %s.", master_ref->server->unique_name); - DCB* master_conn = dcb_connect(master_ref->server, pSession, master_ref->server->protocol); - - if (master_conn) - { - HR_DEBUG("Connected."); - atomic_add(&master_ref->connections, 1); - master_conn->service = pSession->service; - - master_Dcb = Dcb(master_conn); - string name(master_conn->server->unique_name); - all_backends.insert(HintRouterSession::MapElement(name, master_Dcb)); - } - else - { - HR_DEBUG("Connection failed."); - } + master_Dcb = connect_to_backend(pSession, master_ref, &all_backends); } /* Different sessions may use different slaves if the 'max_session_slaves'- @@ -142,34 +126,22 @@ HintRouterSession* HintRouter::newSession(MXS_SESSION *pSession) current++) { SERVER_REF* slave_ref = slave_refs.at(current % size); - // Connect to a slave - HR_DEBUG("Connecting to %s.", slave_ref->server->unique_name); - DCB* slave_conn = dcb_connect(slave_ref->server, pSession, slave_ref->server->protocol); - - if (slave_conn) + Dcb slave_conn = connect_to_backend(pSession, slave_ref, &all_backends); + if (slave_conn.get()) { - HR_DEBUG("Connected."); - atomic_add(&slave_ref->connections, 1); - slave_conn->service = pSession->service; - Dcb slave_Dcb(slave_conn); - slave_arr.push_back(slave_Dcb); - - string name(slave_conn->server->unique_name); - all_backends.insert(HintRouterSession::MapElement(name, slave_Dcb)); + slave_arr.push_back(slave_conn); slave_conns++; } - else - { - HR_DEBUG("Connection failed."); - } } m_total_slave_conns += slave_conns; } + + HintRouterSession* rval = NULL; if (all_backends.size() != 0) { - return new HintRouterSession(pSession, this, all_backends); + rval = new HintRouterSession(pSession, this, all_backends); } - return NULL; + return rval; } void HintRouter::diagnostics(DCB* pOut) @@ -191,10 +163,28 @@ void HintRouter::diagnostics(DCB* pOut) dcb_printf(pOut, "\tQueries routed to all servers: %d\n", m_routed_to_all); } -uint64_t HintRouter::getCapabilities() +Dcb HintRouter::connect_to_backend(MXS_SESSION* session, SERVER_REF* sref, + HintRouterSession::BackendMap* all_backends) { - HR_ENTRY(); - return RCAP_TYPE_STMT_INPUT | RCAP_TYPE_RESULTSET_OUTPUT; + Dcb result(NULL); + HR_DEBUG("Connecting to %s.", sref->server->unique_name); + DCB* new_connection = dcb_connect(sref->server, session, sref->server->protocol); + + if (new_connection) + { + HR_DEBUG("Connected."); + atomic_add(&sref->connections, 1); + new_connection->service = session->service; + + result = Dcb(new_connection); + string name(new_connection->server->unique_name); + all_backends->insert(HintRouterSession::MapElement(name, result)); + } + else + { + HR_DEBUG("Connection failed."); + } + return result; } extern "C" MXS_MODULE* MXS_CREATE_MODULE() diff --git a/server/modules/routing/hintrouter/hintrouter.hh b/server/modules/routing/hintrouter/hintrouter.hh index 2048113ce..67f5bffcf 100644 --- a/server/modules/routing/hintrouter/hintrouter.hh +++ b/server/modules/routing/hintrouter/hintrouter.hh @@ -23,7 +23,10 @@ public: static HintRouter* create(SERVICE* pService, char** pzOptions); HintRouterSession* newSession(MXS_SESSION *pSession); void diagnostics(DCB* pOut); - uint64_t getCapabilities(); + uint64_t getCapabilities() const + { + return RCAP_TYPE_NONE; + } HINT_TYPE get_default_action() const { return m_default_action; @@ -48,4 +51,7 @@ private: private: HintRouter(const HintRouter&); HintRouter& operator = (const HintRouter&); + + static Dcb connect_to_backend(MXS_SESSION* session, SERVER_REF* sref, + HintRouterSession::BackendMap* all_backends); }; diff --git a/server/modules/routing/hintrouter/hintrouterdefs.hh b/server/modules/routing/hintrouter/hintrouterdefs.hh index b435ad799..ce8030a02 100644 --- a/server/modules/routing/hintrouter/hintrouterdefs.hh +++ b/server/modules/routing/hintrouter/hintrouterdefs.hh @@ -19,7 +19,7 @@ #if defined(SS_DEBUG) #define DEBUG_HINTROUTER -//#undef DEBUG_HINTROUTER +#undef DEBUG_HINTROUTER #else #undef DEBUG_HINTROUTER #endif