MySQL 9.2.0
Source Code Documentation
MySQLRouting Class Reference

Manage Connections from clients to MySQL servers. More...

#include <mysql_routing.h>

Inheritance diagram for MySQLRouting:
[legend]

Public Member Functions

 MySQLRouting (const RoutingConfig &routing_config, net::io_context &io_ctx, std::shared_ptr< routing_guidelines::Routing_guidelines_engine > guidelines, const std::string &route_name={}, TlsServerContext *client_ssl_ctx=nullptr, DestinationTlsContext *dest_ssl_ctx=nullptr)
 Default constructor. More...
 
void run (mysql_harness::PluginFuncEnv *env)
 Runs the service and accept incoming connections. More...
 
void set_destinations (const std::string &dests)
 
void set_destinations_from_dests (const std::vector< mysql_harness::Destination > &dests)
 Sets the destinations. More...
 
void set_destinations_from_uri (const mysqlrouter::URI &uri)
 
std::chrono::milliseconds get_destination_connect_timeout () const noexcept
 Returns timeout when connecting to destination. More...
 
void validate_destination_connect_timeout (std::chrono::milliseconds timeout)
 Sets timeout when connecting to destination. More...
 
int set_max_connections (int maximum)
 Sets maximum active connections. More...
 
int get_max_connections () const noexcept override
 Returns maximum active connections. More...
 
template<class ClientProtocol >
void create_connection (typename ClientProtocol::socket client_socket, const typename ClientProtocol::endpoint &client_endpoint)
 create new connection to MySQL Server than can handle client's traffic and adds it to connection container. More...
 
std::optional< routing::RoutingStrategyget_routing_strategy () const override
 
std::vector< mysql_harness::Destinationget_destination_candidates () const override
 
std::vector< MySQLRoutingAPI::ConnDataget_connections () override
 
MySQLRoutingConnectionBaseget_connection (const std::string &) override
 
DestinationManagerdestination_manager ()
 
void disconnect_all ()
 
void stop_socket_acceptors (const bool shutting_down) override
 Stop accepting new connections on a listening socket. More...
 
bool is_accepting_connections () const override
 Check if we are accepting connections on a routing socket. More...
 
stdx::expected< void, std::string > start_accepting_connections () override
 Start accepting new connections on a listening socket. More...
 
stdx::expected< void, std::string > restart_accepting_connections () override
 Start accepting new connections on a listening socket after it has been quarantined for lack of valid destinations. More...
 
void on_routing_guidelines_update (const routing_guidelines::Routing_guidelines_engine::RouteChanges &affected_routing_sources)
 In case when routing guideline was updated go through each established connection and verify if it is allowed according to the new guideline. More...
 
routing_guidelines::Routing_guidelines_engine::RouteChanges update_routing_guidelines (const std::string &routing_guidelines_document)
 Try to update routing guideline with a new guideline. More...
 
void on_router_info_update (const routing_guidelines::Router_info &router_info)
 If the router info was updated then register this info in routing context. More...
 
bool is_standalone () const override
 
MySQLRoutingContextget_context () override
 
bool is_running () const override
 
mysqlrouter::ServerMode purpose () const override
 get the purpose of connections to this route. More...
 
- Public Member Functions inherited from MySQLRoutingBase
 MySQLRoutingBase ()=default
 
 MySQLRoutingBase (const MySQLRoutingBase &)=default
 
 MySQLRoutingBase (MySQLRoutingBase &&)=default
 
MySQLRoutingBaseoperator= (const MySQLRoutingBase &)=default
 
MySQLRoutingBaseoperator= (MySQLRoutingBase &&)=default
 
virtual ~MySQLRoutingBase ()=default
 

Private Member Functions

stdx::expected< void, std::string > run_acceptor (mysql_harness::PluginFuncEnv *env)
 

Static Private Member Functions

static void set_unix_socket_permissions (const char *socket_file)
 Sets unix socket permissions so that the socket is accessible to all users (no-op on Windows) More...
 

Private Attributes

WaitableMonitor< Nothingacceptor_waitable_ {Nothing{}}
 Monitor for notifying socket acceptor. More...
 
MySQLRoutingContext context_
 wrapper for data used by all connections More...
 
net::io_contextio_ctx_
 
std::unique_ptr< DestinationManagerdestination_manager_ {nullptr}
 Destination object to use when getting next connection. More...
 
bool is_destination_standalone_ {false}
 
std::optional< routing::RoutingStrategyrouting_strategy_
 Routing strategy to use when getting next destination. More...
 
routing::AccessMode access_mode_
 access_mode of the servers in the routing More...
 
int max_connections_
 Maximum active connections. More...
 
AllowedNodesChangeCallbacksListIterator allowed_nodes_list_iterator_
 used to unregister from subscription on allowed nodes changes More...
 
ConnectionContainer connection_container_
 container for connections More...
 
std::atomic< bool > is_running_ {true}
 Information if the routing plugin is still running. More...
 
net::steady_timer accept_port_reopen_retry_timer_ {io_ctx_}
 Used when the accepting port is been reopened and it failed, to schedule another retry for standalone-destination(s) route. More...
 
std::vector< std::unique_ptr< AcceptingEndpoint > > accepting_endpoints_
 

Detailed Description

Manage Connections from clients to MySQL servers.

The class MySQLRouter is used to start a service listening on a particular TCP port for incoming MySQL Client connection and route these to a MySQL Server.

Constructor & Destructor Documentation

◆ MySQLRouting()

MySQLRouting::MySQLRouting ( const RoutingConfig routing_config,
net::io_context io_ctx,
std::shared_ptr< routing_guidelines::Routing_guidelines_engine guidelines,
const std::string &  route_name = {},
TlsServerContext client_ssl_ctx = nullptr,
DestinationTlsContext dest_ssl_ctx = nullptr 
)

Default constructor.

class MySQLRouting

Parameters
routing_configrouting configuration
io_ctxIO context
guidelinesrouting guidelines engine
route_nameName of connection routing (can be empty string)
client_ssl_ctxSSL context of the client side
dest_ssl_ctxSSL contexts of the destinations

Member Function Documentation

◆ create_connection()

template<class ClientProtocol >
void MySQLRouting::create_connection ( typename ClientProtocol::socket  client_socket,
const typename ClientProtocol::endpoint &  client_endpoint 
)

create new connection to MySQL Server than can handle client's traffic and adds it to connection container.

Parameters
client_socketsocket used to transfer data to/from client
client_endpointendpoint of client

◆ destination_manager()

DestinationManager * MySQLRouting::destination_manager ( )
inline

◆ disconnect_all()

void MySQLRouting::disconnect_all ( )

◆ get_connection()

MySQLRoutingConnectionBase * MySQLRouting::get_connection ( const std::string &  client_endpoint)
overridevirtual

Implements MySQLRoutingBase.

◆ get_connections()

std::vector< MySQLRoutingAPI::ConnData > MySQLRouting::get_connections ( )
overridevirtual

Implements MySQLRoutingBase.

◆ get_context()

MySQLRoutingContext & MySQLRouting::get_context ( )
inlineoverridevirtual

Implements MySQLRoutingBase.

◆ get_destination_candidates()

std::vector< mysql_harness::Destination > MySQLRouting::get_destination_candidates ( ) const
overridevirtual

Implements MySQLRoutingBase.

◆ get_destination_connect_timeout()

std::chrono::milliseconds MySQLRouting::get_destination_connect_timeout ( ) const
inlinenoexcept

Returns timeout when connecting to destination.

Returns
Timeout in seconds as int

◆ get_max_connections()

int MySQLRouting::get_max_connections ( ) const
inlineoverridevirtualnoexcept

Returns maximum active connections.

Returns
Maximum as int

Implements MySQLRoutingBase.

◆ get_routing_strategy()

std::optional< routing::RoutingStrategy > MySQLRouting::get_routing_strategy ( ) const
inlineoverridevirtual

Implements MySQLRoutingBase.

◆ is_accepting_connections()

bool MySQLRouting::is_accepting_connections ( ) const
overridevirtual

Check if we are accepting connections on a routing socket.

Return values
trueif we are accepting connections, false otherwise

Implements MySQLRoutingBase.

◆ is_running()

bool MySQLRouting::is_running ( ) const
inlineoverridevirtual

Implements MySQLRoutingBase.

◆ is_standalone()

bool MySQLRouting::is_standalone ( ) const
inlineoverridevirtual

Implements MySQLRoutingBase.

◆ on_router_info_update()

void MySQLRouting::on_router_info_update ( const routing_guidelines::Router_info router_info)

If the router info was updated then register this info in routing context.

Parameters
router_infoupdated router info

◆ on_routing_guidelines_update()

void MySQLRouting::on_routing_guidelines_update ( const routing_guidelines::Routing_guidelines_engine::RouteChanges affected_routing_sources)

In case when routing guideline was updated go through each established connection and verify if it is allowed according to the new guideline.

If not then such connection is dropped.

Parameters
affected_routing_sourceslist of routing guideline route names that were affected by the guideline update

◆ purpose()

mysqlrouter::ServerMode MySQLRouting::purpose ( ) const
overridevirtual

get the purpose of connections to this route.

  • read-write : all statements are treated as "read-write".
  • read-only : all statements are treated as "read-only".
  • unavailable: it is currently unknown where the statement should go to.

"Unavailable" is used for read-write splitting where the purpose is determined per statement, session, ...

A statement over a read-only server connection may end up on a read-write server in case all read-only servers aren't reachable. Even if the server is read-write, the connections purpose is read-only and if the server changes its role from PRIMARY to SECONDARY, these read-only connections will not be abort as a SECONDARY is good enough to serve read-only connections.

Implements MySQLRoutingBase.

◆ restart_accepting_connections()

stdx::expected< void, std::string > MySQLRouting::restart_accepting_connections ( )
overridevirtual

Start accepting new connections on a listening socket after it has been quarantined for lack of valid destinations.

Returns
std::string on errors.

Implements MySQLRoutingBase.

◆ run()

void MySQLRouting::run ( mysql_harness::PluginFuncEnv env)

Runs the service and accept incoming connections.

Runs the connection routing service and starts accepting incoming MySQL client connections.

Exceptions
std::runtime_erroron errors.

◆ run_acceptor()

stdx::expected< void, std::string > MySQLRouting::run_acceptor ( mysql_harness::PluginFuncEnv env)
private

◆ set_destinations()

void MySQLRouting::set_destinations ( const std::string &  dests)

◆ set_destinations_from_dests()

void MySQLRouting::set_destinations_from_dests ( const std::vector< mysql_harness::Destination > &  dests)

Sets the destinations.

Parameters
destsdestinations

◆ set_destinations_from_uri()

void MySQLRouting::set_destinations_from_uri ( const mysqlrouter::URI uri)

◆ set_max_connections()

int MySQLRouting::set_max_connections ( int  maximum)

Sets maximum active connections.

Sets maximum of active connections. Maximum must be between 1 and 65535.

Exceptions
std::invalid_argumentwhen an invalid value was provided.
Parameters
maximumMax number of connections allowed
Returns
New value as int

◆ set_unix_socket_permissions()

void MySQLRouting::set_unix_socket_permissions ( const char *  socket_file)
staticprivate

Sets unix socket permissions so that the socket is accessible to all users (no-op on Windows)

Parameters
socket_filepath to socket file
Exceptions
std::runtime_errorif chmod() inside fails

◆ start_accepting_connections()

stdx::expected< void, std::string > MySQLRouting::start_accepting_connections ( )
overridevirtual

Start accepting new connections on a listening socket.

Returns
std::string on errors.

Implements MySQLRoutingBase.

◆ stop_socket_acceptors()

void MySQLRouting::stop_socket_acceptors ( const bool  shutting_down)
overridevirtual

Stop accepting new connections on a listening socket.

Parameters
shutting_downis plugin shutting down.

Implements MySQLRoutingBase.

◆ update_routing_guidelines()

routing_guidelines::Routing_guidelines_engine::RouteChanges MySQLRouting::update_routing_guidelines ( const std::string &  routing_guidelines_document)

Try to update routing guideline with a new guideline.

Returns
list of routing guideline route names that were affected by the guideline update

◆ validate_destination_connect_timeout()

void MySQLRouting::validate_destination_connect_timeout ( std::chrono::milliseconds  timeout)

Sets timeout when connecting to destination.

Checks timeout connecting with destination servers.

Exceptions
std::invalid_argumentwhen an invalid value was provided.
Parameters
timeoutTimeout

Member Data Documentation

◆ accept_port_reopen_retry_timer_

net::steady_timer MySQLRouting::accept_port_reopen_retry_timer_ {io_ctx_}
private

Used when the accepting port is been reopened and it failed, to schedule another retry for standalone-destination(s) route.

◆ accepting_endpoints_

std::vector<std::unique_ptr<AcceptingEndpoint> > MySQLRouting::accepting_endpoints_
private

◆ acceptor_waitable_

WaitableMonitor<Nothing> MySQLRouting::acceptor_waitable_ {Nothing{}}
private

Monitor for notifying socket acceptor.

◆ access_mode_

routing::AccessMode MySQLRouting::access_mode_
private

access_mode of the servers in the routing

◆ allowed_nodes_list_iterator_

AllowedNodesChangeCallbacksListIterator MySQLRouting::allowed_nodes_list_iterator_
private

used to unregister from subscription on allowed nodes changes

◆ connection_container_

ConnectionContainer MySQLRouting::connection_container_
private

container for connections

◆ context_

MySQLRoutingContext MySQLRouting::context_
private

wrapper for data used by all connections

◆ destination_manager_

std::unique_ptr<DestinationManager> MySQLRouting::destination_manager_ {nullptr}
private

Destination object to use when getting next connection.

◆ io_ctx_

net::io_context& MySQLRouting::io_ctx_
private

◆ is_destination_standalone_

bool MySQLRouting::is_destination_standalone_ {false}
private

◆ is_running_

std::atomic<bool> MySQLRouting::is_running_ {true}
private

Information if the routing plugin is still running.

◆ max_connections_

int MySQLRouting::max_connections_
private

Maximum active connections.

Maximum number of incoming connections that will be accepted by this MySQLRouter instances. There is no maximum for outgoing connections since it is one-to-one with incoming.

◆ routing_strategy_

std::optional<routing::RoutingStrategy> MySQLRouting::routing_strategy_
private

Routing strategy to use when getting next destination.


The documentation for this class was generated from the following files: