MySQL 26.10.0
Source Code Documentation
routing_component.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2019, 2026, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQLROUTER_ROUTING_COMPONENT_INCLUDED
27#define MYSQLROUTER_ROUTING_COMPONENT_INCLUDED
28
29#include "mysqlrouter/routing_export.h" // ROUTING_EXPORT
30
31#include <chrono>
32#include <map>
33#include <memory>
34#include <mutex>
35#include <string>
36#include <vector>
37
38#include "connection.h"
42
44class BaseProtocol;
45
47 public:
48 MySQLRoutingAPI() = default;
49
50 MySQLRoutingAPI(std::shared_ptr<MySQLRoutingBase> r) : r_{std::move(r)} {}
51
52 // config
53 std::string get_bind_address() const;
54 uint16_t get_bind_port() const;
55
56 std::chrono::milliseconds get_client_connect_timeout() const;
57 std::chrono::milliseconds get_destination_connect_timeout() const;
58 std::string get_destination_cluster_name() const;
59 std::string get_destination_replicaset_name() const;
60
61 DestinationNodesStateNotifier *get_destinations_state_notifier() const;
62 int get_max_connections() const;
63 uint64_t get_max_connect_errors() const;
64
65 std::string get_name() const;
66
67 std::string get_protocol_name() const;
68 std::string get_routing_strategy() const;
69 std::string get_socket() const;
70
71 explicit operator bool() const noexcept { return r_.operator bool(); }
72
73 std::vector<std::string> get_blocked_client_hosts() const;
74
75 struct ConnData {
76 using time_point_type = std::chrono::time_point<std::chrono::system_clock>;
77
78 ConnData() = default;
79
80 ConnData(std::string src, std::string dst, std::size_t bytes_up,
81 std::size_t bytes_down, time_point_type started,
82 time_point_type connected_to_server,
83 time_point_type last_sent_to_server,
84 time_point_type last_received_from_server)
85 : src(std::move(src)),
86 dst(std::move(dst)),
87 bytes_up(bytes_up),
88 bytes_down(bytes_down),
90 connected_to_server(connected_to_server),
91 last_sent_to_server(last_sent_to_server),
92 last_received_from_server(last_received_from_server) {}
93
94 std::string src;
95 std::string dst;
96
97 std::size_t bytes_up;
98 std::size_t bytes_down;
99
104 };
105
106 struct SslOptions {
108 std::string tls_version;
109 std::string ssl_cipher;
110 std::string ca;
111 std::string capath;
112 std::string crl;
113 std::string crlpath;
114 std::string cert;
115 std::string key;
116 std::string curves;
117 };
118
119 std::vector<ConnData> get_connections() const;
120
121 // status
122 int get_active_connections() const;
123 int get_total_connections() const;
124
125 std::vector<mysql_harness::Destination> get_destination_candidates() const;
126 SslOptions get_destination_ssl_options() const;
127
128 stdx::expected<void, std::string> refresh_socket_acceptor_state();
129
130 bool is_accepting_connections() const;
131
132 void stop_socket_acceptors();
133
134 bool is_running() const;
135
136 private:
137 std::shared_ptr<MySQLRoutingBase> r_;
138};
139
141 public:
142 static MySQLRoutingComponent &get_instance();
143
144 void deinit();
145
146 void init(const mysql_harness::Config &config);
147
148 void register_route(const std::string &name,
149 std::shared_ptr<MySQLRoutingBase> srv);
150
151 void erase(const std::string &name);
152
153 MySQLRoutingAPI api(const std::string &name);
154
155 uint64_t current_total_connections();
156 uint64_t max_total_connections() const { return max_total_connections_; }
157
158 std::optional<std::reference_wrapper<const rapidjson::Document>>
159 routing_guidelines_document() const;
160
161 std::optional<rapidjson::Document> routing_guidelines_document_schema() const;
162
163 std::vector<std::string> route_names() const;
164
165 void set_routing_guidelines(const std::string &routing_guidelines_document);
166
167 bool routing_guidelines_initialized() const;
168
169 std::shared_ptr<routing_guidelines::Routing_guidelines_engine>
171 std::lock_guard<std::mutex> lock{routing_guidelines_mtx_};
172 return routing_guidelines_;
173 }
174
176 const std::string &client_endpoint);
177
178 private:
179 // disable copy, as we are a single-instance
181 void operator=(MySQLRoutingComponent const &) = delete;
182
183 std::mutex routes_mu_;
184 std::map<std::string, std::weak_ptr<MySQLRoutingBase>> routes_;
185
186 uint64_t max_total_connections_{0};
187
189
190 mutable std::mutex routing_guidelines_mtx_;
191 std::shared_ptr<routing_guidelines::Routing_guidelines_engine>
192 routing_guidelines_{nullptr};
193};
194
195#endif
static mysql_service_status_t deinit()
Component deinitialization.
Definition: audit_api_message_emit.cc:584
static mysql_service_status_t init()
Component initialization.
Definition: audit_api_message_emit.cc:575
Definition: base_protocol.h:29
Allows the obervers to register for notifications on the change in the state of the destination nodes...
Definition: destination_nodes_state_notifier.h:70
Definition: routing_component.h:46
std::shared_ptr< MySQLRoutingBase > r_
Definition: routing_component.h:137
MySQLRoutingAPI(std::shared_ptr< MySQLRoutingBase > r)
Definition: routing_component.h:50
MySQLRoutingAPI()=default
Facade to avoid a tight coupling between Routing component and actual routing endpoint implementation...
Definition: mysql_routing_base.h:41
Definition: routing_component.h:140
MySQLRoutingComponent(MySQLRoutingComponent const &)=delete
std::map< std::string, std::weak_ptr< MySQLRoutingBase > > routes_
Definition: routing_component.h:184
uint64_t max_total_connections() const
Definition: routing_component.h:156
MySQLRoutingComponent()=default
void operator=(MySQLRoutingComponent const &)=delete
std::mutex routing_guidelines_mtx_
Definition: routing_component.h:190
std::mutex routes_mu_
Definition: routing_component.h:183
std::shared_ptr< routing_guidelines::Routing_guidelines_engine > get_routing_guidelines()
Definition: routing_component.h:170
Definition: connection.h:47
Configuration.
Definition: config_parser.h:253
Definition: expected.h:286
static int get_connection(MEM_ROOT *mem_root, FEDERATED_SHARE *share)
Definition: ha_federated.cc:608
mysql_ssl_mode
Definition: mysql.h:275
net::ip::tcp::socket * get_socket(net::ip::tcp::socket *s)
Definition: connection.h:68
Provides atomic access in shared-exclusive modes.
Definition: shared_spin_lock.h:79
std::chrono::milliseconds milliseconds
Definition: authorize_manager.cc:70
noexcept
The return type for any call_and_catch(f, args...) call where f(args...) returns Type.
Definition: call_and_catch.h:76
bool is_running(const PluginFuncEnv *env) noexcept
Definition: loader.cc:243
stdx::expected< RoutingStrategy, std::error_code > get_routing_strategy(std::string_view value)
Returns RoutingStrategy for its literal representation.
Definition: routing.cc:71
Definition: srv0dynamic_procedures.h:48
Define std::hash<Gtid>.
Definition: gtid.h:355
const mysql_service_registry_t * r
Definition: pfs_example_plugin_employee.cc:86
#define ROUTING_EXPORT
Definition: routing_export.h:15
case opt name
Definition: sslopt-case.h:29
Definition: routing_component.h:75
std::size_t bytes_up
Definition: routing_component.h:97
time_point_type last_sent_to_server
Definition: routing_component.h:102
time_point_type connected_to_server
Definition: routing_component.h:101
std::string dst
Definition: routing_component.h:95
time_point_type started
Definition: routing_component.h:100
std::string src
Definition: routing_component.h:94
std::chrono::time_point< std::chrono::system_clock > time_point_type
Definition: routing_component.h:76
std::size_t bytes_down
Definition: routing_component.h:98
time_point_type last_received_from_server
Definition: routing_component.h:103
ConnData(std::string src, std::string dst, std::size_t bytes_up, std::size_t bytes_down, time_point_type started, time_point_type connected_to_server, time_point_type last_sent_to_server, time_point_type last_received_from_server)
Definition: routing_component.h:80
Definition: routing_component.h:106
mysql_ssl_mode ssl_mode
Definition: routing_component.h:107
std::string ca
Definition: routing_component.h:110
std::string crlpath
Definition: routing_component.h:113
std::string tls_version
Definition: routing_component.h:108
std::string key
Definition: routing_component.h:115
std::string curves
Definition: routing_component.h:116
std::string cert
Definition: routing_component.h:114
std::string crl
Definition: routing_component.h:112
std::string capath
Definition: routing_component.h:111
std::string ssl_cipher
Definition: routing_component.h:109
ulong get_max_connections(void)
Get max number of connections.
Definition: sql_thd_api.cc:314
static int started(pax_machine *p)
Definition: xcom_base.cc:791