MySQL 9.3.0
Source Code Documentation
routing_component.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2019, 2025, 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"
41
43class BaseProtocol;
44
46 public:
47 MySQLRoutingAPI() = default;
48
49 MySQLRoutingAPI(std::shared_ptr<MySQLRoutingBase> r) : r_{std::move(r)} {}
50
51 // config
52 std::string get_bind_address() const;
53 uint16_t get_bind_port() const;
54
55 std::chrono::milliseconds get_client_connect_timeout() const;
56 std::chrono::milliseconds get_destination_connect_timeout() const;
57 std::string get_destination_cluster_name() const;
58 std::string get_destination_replicaset_name() const;
59
60 DestinationNodesStateNotifier *get_destinations_state_notifier() const;
61 int get_max_connections() const;
62 uint64_t get_max_connect_errors() const;
63
64 std::string get_name() const;
65
66 std::string get_protocol_name() const;
67 std::string get_routing_strategy() const;
68 std::string get_socket() const;
69
70 explicit operator bool() const noexcept { return r_.operator bool(); }
71
72 std::vector<std::string> get_blocked_client_hosts() const;
73
74 struct ConnData {
75 using time_point_type = std::chrono::time_point<std::chrono::system_clock>;
76
77 ConnData() = default;
78
79 ConnData(std::string src, std::string dst, std::size_t bytes_up,
80 std::size_t bytes_down, time_point_type started,
81 time_point_type connected_to_server,
82 time_point_type last_sent_to_server,
83 time_point_type last_received_from_server)
84 : src(std::move(src)),
85 dst(std::move(dst)),
86 bytes_up(bytes_up),
87 bytes_down(bytes_down),
89 connected_to_server(connected_to_server),
90 last_sent_to_server(last_sent_to_server),
91 last_received_from_server(last_received_from_server) {}
92
93 std::string src;
94 std::string dst;
95
96 std::size_t bytes_up;
97 std::size_t bytes_down;
98
103 };
104
105 struct SslOptions {
107 std::string tls_version;
108 std::string ssl_cipher;
109 std::string ca;
110 std::string capath;
111 std::string crl;
112 std::string crlpath;
113 std::string cert;
114 std::string key;
115 std::string curves;
116 };
117
118 std::vector<ConnData> get_connections() const;
119
120 // status
121 int get_active_connections() const;
122 int get_total_connections() const;
123
124 std::vector<mysql_harness::Destination> get_destination_candidates() const;
125 SslOptions get_destination_ssl_options() const;
126
127 void start_accepting_connections();
128 void restart_accepting_connections();
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 const rapidjson::Document &routing_guidelines_document() const;
159
160 rapidjson::Document routing_guidelines_document_schema() const;
161
162 std::vector<std::string> route_names() const;
163
164 void set_routing_guidelines(const std::string &routing_guidelines_document);
165
166 bool routing_guidelines_initialized() const;
167
168 std::shared_ptr<routing_guidelines::Routing_guidelines_engine>
170 std::lock_guard<std::mutex> lock{routing_guidelines_mtx_};
171 return routing_guidelines_;
172 }
173
175 const std::string &client_endpoint);
176
177 private:
178 // disable copy, as we are a single-instance
180 void operator=(MySQLRoutingComponent const &) = delete;
181
182 std::mutex routes_mu_;
183 std::map<std::string, std::weak_ptr<MySQLRoutingBase>> routes_;
184
185 uint64_t max_total_connections_{0};
186
188
189 mutable std::mutex routing_guidelines_mtx_;
190 std::shared_ptr<routing_guidelines::Routing_guidelines_engine>
191 routing_guidelines_{nullptr};
192};
193
194#endif
static mysql_service_status_t deinit()
Component deinitialization.
Definition: audit_api_message_emit.cc:575
static mysql_service_status_t init()
Component initialization.
Definition: audit_api_message_emit.cc:566
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:71
Definition: routing_component.h:45
std::shared_ptr< MySQLRoutingBase > r_
Definition: routing_component.h:137
MySQLRoutingAPI(std::shared_ptr< MySQLRoutingBase > r)
Definition: routing_component.h:49
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:183
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:189
std::mutex routes_mu_
Definition: routing_component.h:182
std::shared_ptr< routing_guidelines::Routing_guidelines_engine > get_routing_guidelines()
Definition: routing_component.h:169
Definition: connection.h:47
Configuration.
Definition: config_parser.h:253
static int get_connection(MEM_ROOT *mem_root, FEDERATED_SHARE *share)
Definition: ha_federated.cc:608
mysql_ssl_mode
Definition: mysql.h:272
net::ip::tcp::socket * get_socket(net::ip::tcp::socket *s)
Definition: connection.h:65
Provides atomic access in shared-exclusive modes.
Definition: shared_spin_lock.h:79
std::chrono::milliseconds milliseconds
Definition: authorize_manager.cc:68
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
Definition: gcs_xcom_synode.h:64
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:74
std::size_t bytes_up
Definition: routing_component.h:96
time_point_type last_sent_to_server
Definition: routing_component.h:101
time_point_type connected_to_server
Definition: routing_component.h:100
std::string dst
Definition: routing_component.h:94
time_point_type started
Definition: routing_component.h:99
std::string src
Definition: routing_component.h:93
std::chrono::time_point< std::chrono::system_clock > time_point_type
Definition: routing_component.h:75
std::size_t bytes_down
Definition: routing_component.h:97
time_point_type last_received_from_server
Definition: routing_component.h:102
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:79
Definition: routing_component.h:105
mysql_ssl_mode ssl_mode
Definition: routing_component.h:106
std::string ca
Definition: routing_component.h:109
std::string crlpath
Definition: routing_component.h:112
std::string tls_version
Definition: routing_component.h:107
std::string key
Definition: routing_component.h:114
std::string curves
Definition: routing_component.h:115
std::string cert
Definition: routing_component.h:113
std::string crl
Definition: routing_component.h:111
std::string capath
Definition: routing_component.h:110
std::string ssl_cipher
Definition: routing_component.h:108
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:759