From a444484310386a21625c2e557072e1427e704fab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Thu, 26 Sep 2019 22:59:31 +0300 Subject: [PATCH] MXS-2702: Update m_current_master on session command When lazy_connect is enabled and the first query is `SET autocommit=0`, a master connection can be created. If it is, then the m_current_master pointer must also be updated. Also fixed the case where a failure to connect to one slave would cause the connection attempts to stop too early. --- .../readwritesplit/rwsplit_route_stmt.cc | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/server/modules/routing/readwritesplit/rwsplit_route_stmt.cc b/server/modules/routing/readwritesplit/rwsplit_route_stmt.cc index 9271c424c..e92046535 100644 --- a/server/modules/routing/readwritesplit/rwsplit_route_stmt.cc +++ b/server/modules/routing/readwritesplit/rwsplit_route_stmt.cc @@ -105,21 +105,32 @@ bool RWSplitSession::prepare_target(RWBackend* target, route_target_t route_targ bool RWSplitSession::create_one_connection() { + mxb_assert(m_config.lazy_connect); + // Try to first find a master for (auto backend : m_raw_backends) { if (backend->can_connect() && backend->is_master()) { - return prepare_target(backend, TARGET_MASTER); + if (prepare_target(backend, TARGET_MASTER)) + { + if (!m_current_master) + { + MXS_INFO("Chose '%s' as master due to session write", backend->name()); + m_current_master = backend; + } + + return true; + } } } // If no master was found, find a slave for (auto backend : m_raw_backends) { - if (backend->can_connect() && backend->is_slave()) + if (backend->can_connect() && backend->is_slave() && prepare_target(backend, TARGET_SLAVE)) { - return prepare_target(backend, TARGET_SLAVE); + return true; } }