diff --git a/server/modules/routing/readwritesplit/rwsplit_route_stmt.cc b/server/modules/routing/readwritesplit/rwsplit_route_stmt.cc index b92d9d96e..7223258f0 100644 --- a/server/modules/routing/readwritesplit/rwsplit_route_stmt.cc +++ b/server/modules/routing/readwritesplit/rwsplit_route_stmt.cc @@ -91,6 +91,30 @@ bool RWSplitSession::prepare_target(RWBackend* target, route_target_t route_targ return rval; } +bool RWSplitSession::create_one_connection() +{ + // 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 no master was found, find a slave + for (auto backend : m_raw_backends) + { + if (backend->can_connect() && backend->is_slave()) + { + return prepare_target(backend, TARGET_SLAVE); + } + } + + // No servers are available + return false; +} + void RWSplitSession::retry_query(GWBUF* querybuf, int delay) { mxb_assert(querybuf); @@ -560,6 +584,15 @@ bool RWSplitSession::route_session_write(GWBUF* querybuf, uint8_t command, uint3 m_sescmd_list.push_back(sescmd); } + if (m_config.lazy_connect && !attempted_write && nsucc == 0) + { + // If no connections are open, create one and execute the session command on it + if (create_one_connection()) + { + nsucc = 1; + } + } + if (nsucc) { m_sent_sescmd = id; diff --git a/server/modules/routing/readwritesplit/rwsplitsession.hh b/server/modules/routing/readwritesplit/rwsplitsession.hh index 5dff2648a..28d81a589 100644 --- a/server/modules/routing/readwritesplit/rwsplitsession.hh +++ b/server/modules/routing/readwritesplit/rwsplitsession.hh @@ -163,6 +163,7 @@ private: bool handle_got_target(GWBUF* querybuf, mxs::RWBackend* target, bool store); void handle_connection_keepalive(mxs::RWBackend* target); bool prepare_target(mxs::RWBackend* target, route_target_t route_target); + bool create_one_connection(); void retry_query(GWBUF* querybuf, int delay = 1); bool should_replace_master(mxs::RWBackend* target);