From 2bd614ce8e45d520b32bfd0a59fbc5ea4762dd3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Tue, 18 Jun 2019 16:14:59 +0300 Subject: [PATCH] MXS-2566: Use connection counts more often The connection counts are now always used to pick the best servers where the initial connections are created. This covers both master and slaves selection. Reconnections done while routing queries still pick the "best" server according to the slave selection criteria. This allows better servers to be taken into use when `lazy_connect` is enabled. --- .../routing/readwritesplit/readwritesplit.hh | 5 ++--- .../routing/readwritesplit/rwsplit_route_stmt.cc | 2 +- .../readwritesplit/rwsplit_select_backends.cc | 13 +++++++------ 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/server/modules/routing/readwritesplit/readwritesplit.hh b/server/modules/routing/readwritesplit/readwritesplit.hh index 43471594d..5232c8c8c 100644 --- a/server/modules/routing/readwritesplit/readwritesplit.hh +++ b/server/modules/routing/readwritesplit/readwritesplit.hh @@ -127,7 +127,7 @@ static const char gtid_wait_stmt[] = /** Function that returns a "score" for a server to enable comparison. * Smaller numbers are better. */ -using BackendSelectFunction = std::function; +using BackendSelectFunction = mxs::PRWBackends::iterator (*)(mxs::PRWBackends& sBackends); BackendSelectFunction get_backend_select_function(select_criteria_t); using std::chrono::seconds; @@ -409,8 +409,7 @@ inline bool can_continue_using_master(const mxs::RWBackend* current_master) && session_trx_is_active(current_master->dcb()->session)); } -mxs::RWBackend* get_root_master(const mxs::PRWBackends& backends, mxs::RWBackend* current_master, - const BackendSelectFunction& func); +mxs::RWBackend* get_root_master(const mxs::PRWBackends& backends, mxs::RWBackend* current_master); /** * Get total slave count and connected slave count diff --git a/server/modules/routing/readwritesplit/rwsplit_route_stmt.cc b/server/modules/routing/readwritesplit/rwsplit_route_stmt.cc index ca64986fa..f420b9916 100644 --- a/server/modules/routing/readwritesplit/rwsplit_route_stmt.cc +++ b/server/modules/routing/readwritesplit/rwsplit_route_stmt.cc @@ -637,7 +637,7 @@ RWBackend* RWSplitSession::get_master_backend() { RWBackend* rval = nullptr; /** get root master from available servers */ - RWBackend* master = get_root_master(m_raw_backends, m_current_master, m_config.backend_select_fct); + RWBackend* master = get_root_master(m_raw_backends, m_current_master); if (master) { diff --git a/server/modules/routing/readwritesplit/rwsplit_select_backends.cc b/server/modules/routing/readwritesplit/rwsplit_select_backends.cc index ff1a4093c..80152cf56 100644 --- a/server/modules/routing/readwritesplit/rwsplit_select_backends.cc +++ b/server/modules/routing/readwritesplit/rwsplit_select_backends.cc @@ -396,8 +396,7 @@ static void log_server_connections(select_criteria_t criteria, const PRWBackends } } -RWBackend* get_root_master(const PRWBackends& backends, RWBackend* current_master, - const BackendSelectFunction& func) +RWBackend* get_root_master(const PRWBackends& backends, RWBackend* current_master) { if (current_master && current_master->in_use() && can_continue_using_master(current_master)) { @@ -427,7 +426,7 @@ RWBackend* get_root_master(const PRWBackends& backends, RWBackend* current_maste } } - auto it = func(candidates); + auto it = backend_cmp_global_conn(candidates); return it != candidates.end() ? *it : nullptr; } @@ -467,7 +466,7 @@ bool RWSplitSession::open_connections() return true; // No need to create connections } - RWBackend* master = get_root_master(m_raw_backends, m_current_master, m_config.backend_select_fct); + RWBackend* master = get_root_master(m_raw_backends, m_current_master); if ((!master || !master->can_connect()) && m_config.master_failure_mode == RW_FAIL_INSTANTLY) { @@ -514,9 +513,11 @@ bool RWSplitSession::open_connections() } } - for (auto ite = m_config.backend_select_fct(candidates); + auto func = backend_cmp_global_conn; + + for (auto ite = func(candidates); n_slaves < max_nslaves && !candidates.empty() && ite != candidates.end(); - ite = m_config.backend_select_fct(candidates)) + ite = func(candidates)) { if (prepare_connection(*ite)) {