MySQL 8.4.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, 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_from_csv (const std::string &csv)
 Sets the destinations from URI. 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...
 
routing::RoutingStrategy get_routing_strategy () const override
 
std::vector< mysql_harness::TCPAddressget_destinations () const override
 
std::vector< MySQLRoutingAPI::ConnDataget_connections () override
 
MySQLRoutingConnectionBaseget_connection (const std::string &) override
 
RouteDestinationdestinations ()
 
void disconnect_all ()
 
void stop_socket_acceptors () 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...
 
MySQLRoutingContextget_context () override
 
bool is_running () const override
 
- 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< RouteDestinationdestination_
 Destination object to use when getting next connection. More...
 
bool is_destination_standalone_ {false}
 
routing::RoutingStrategy routing_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.

Connection routing will not analyze or parse any MySQL package (except from those in the handshake phase to be able to discover invalid connection error) nor will it do any authentication. It will not handle errors from the MySQL server and not automatically recover. The client communicate through MySQL Router just like it would directly connecting.

The MySQL Server is chosen from a given list of hosts or IP addresses (with or without TCP port) based on the the mode. For example, mode read-only will go through the list of servers in a round-robin way. The mode read-write will always go through the list from the beginning and failover to the next available.

Example usage: bind to all IP addresses and use TCP Port 7001

MySQLRouting r(conf, ioctx);
r.destination_connect_timeout = std::chrono::seconds(1);
r.set_destinations_from_csv("10.0.10.5;10.0.11.6");
r.run();
Manage Connections from clients to MySQL servers.
Definition: mysql_routing.h:196
const mysql_service_registry_t * r
Definition: pfs_example_plugin_employee.cc:86
double seconds()
Definition: task.cc:310

The above example will, when MySQL running on 10.0.10.5 is not available, use 10.0.11.6 to setup the connection routing.

Constructor & Destructor Documentation

◆ MySQLRouting()

MySQLRouting::MySQLRouting ( const RoutingConfig routing_config,
net::io_context io_ctx,
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
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

◆ destinations()

RouteDestination * MySQLRouting::destinations ( )
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_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_destinations()

std::vector< mysql_harness::TCPAddress > MySQLRouting::get_destinations ( ) const
overridevirtual

Implements MySQLRoutingBase.

◆ get_max_connections()

int MySQLRouting::get_max_connections ( ) const
inlineoverridevirtualnoexcept

Returns maximum active connections.

Returns
Maximum as int

Implements MySQLRoutingBase.

◆ get_routing_strategy()

routing::RoutingStrategy MySQLRouting::get_routing_strategy ( ) const
overridevirtual

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.

◆ 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_from_csv()

void MySQLRouting::set_destinations_from_csv ( const std::string &  csv)

Sets the destinations from URI.

Sets destinations using the given string and the given mode. The string should be a comma separated list of MySQL servers.

The mode is one of MySQLRouting::Mode, for example MySQLRouting::Mode::kReadOnly.

Example of destinations: "10.0.10.5,10.0.11.6:3307"

Parameters
csvdestinations as comma-separated-values

◆ 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 ( )
overridevirtual

Stop accepting new connections on a listening socket.

Implements MySQLRoutingBase.

◆ 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_

std::unique_ptr<RouteDestination> MySQLRouting::destination_
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_

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: