MySQL 9.2.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
connection.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018, 2024, 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 ROUTING_CONNECTION_INCLUDED
27#define ROUTING_CONNECTION_INCLUDED
28
29#include <chrono>
30#include <cstdint> // size_t
31#include <functional>
32#include <memory>
33#include <optional>
34
36#include "context.h"
37#include "destination.h" // DestinationManager
38#include "destination_error.h"
46
48 public:
51 std::function<void(MySQLRoutingConnectionBase *)> remove_callback)
52 : context_(context), remove_callback_(std::move(remove_callback)) {}
53
54 virtual ~MySQLRoutingConnectionBase() = default;
55
57 const MySQLRoutingContext &context() const { return context_; }
58
59 virtual std::optional<mysql_harness::Destination> get_destination_id()
60 const = 0;
61
62 virtual std::optional<mysql_harness::Destination> read_only_destination_id()
63 const {
64 return get_destination_id();
65 }
66
67 virtual std::optional<mysql_harness::Destination> read_write_destination_id()
68 const {
69 return get_destination_id();
70 }
71
72 virtual std::optional<mysql_harness::DestinationEndpoint>
74
75 virtual std::optional<mysql_harness::DestinationEndpoint>
77 return destination_endpoint();
78 }
79
80 virtual std::optional<mysql_harness::DestinationEndpoint>
82 return destination_endpoint();
83 }
84
86
87 virtual std::string get_routing_source() const = 0;
88
89 virtual void set_routing_source(std::string name) = 0;
90
91 virtual void wait_until_completed() = 0;
92 virtual void completed() = 0;
93
95
96 /**
97 * @brief Returns address of server to which connection is established.
98 *
99 * @return address of server
100 */
101 std::string get_server_address() const {
102 return stats_([](const Stats &stats) { return stats.server_address; });
103 }
104
105 void server_address(const std::string &dest) {
106 return stats_([&dest](Stats &stats) { stats.server_address = dest; });
107 }
108
109 virtual void disconnect() = 0;
110
111 /**
112 * @brief Returns address of client which connected to router
113 *
114 * @return address of client
115 */
116 std::string get_client_address() const {
117 return stats_([](const Stats &stats) { return stats.client_address; });
118 }
119
120 void client_address(const std::string &dest) {
121 return stats_([&dest](Stats &stats) { stats.client_address = dest; });
122 }
123
124 std::size_t get_bytes_up() const {
125 return stats_([](const Stats &stats) { return stats.bytes_up; });
126 }
127
128 std::size_t get_bytes_down() const {
129 return stats_([](const Stats &stats) { return stats.bytes_down; });
130 }
131
132 using clock_type = std::chrono::system_clock;
133 using time_point_type = clock_type::time_point;
134
136 return stats_([](const Stats &stats) { return stats.started; });
137 }
138
140 return stats_([](const Stats &stats) { return stats.connected_to_server; });
141 }
142
144 return stats_([](const Stats &stats) { return stats.last_sent_to_server; });
145 }
146
148 return stats_(
149 [](const Stats &stats) { return stats.last_received_from_server; });
150 }
151
152 struct Stats {
153 Stats() = default;
154
155 Stats(std::string client_address, std::string server_address,
156 std::size_t bytes_up, std::size_t bytes_down, time_point_type started,
168
169 std::string client_address;
170 std::string server_address;
171
172 std::size_t bytes_up{0};
173 std::size_t bytes_down{0};
174
175 time_point_type started{clock_type::now()};
179 };
180
181 Stats get_stats() const {
182 return stats_([](const Stats &stats) { return stats; });
183 }
184
185 void transfered_to_server(size_t bytes) {
186 const auto now = clock_type::now();
187 stats_([bytes, now](Stats &stats) {
188 stats.last_sent_to_server = now;
189 stats.bytes_down += bytes;
190 });
191 }
192
193 void transfered_to_client(size_t bytes) {
194 const auto now = clock_type::now();
195 stats_([bytes, now](Stats &stats) {
196 stats.last_received_from_server = now;
197 stats.bytes_up += bytes;
198 });
199 }
200
202
203 void accepted();
204
205 virtual void connected();
206
207 template <class F>
208 auto disconnect_request(F &&f) {
209 return disconnect_(std::forward<F>(f));
210 }
211
212 bool disconnect_requested() const {
213 return disconnect_([](auto requested) { return requested; });
214 }
215
217
219
220 protected:
221 /** @brief wrapper for common data used by all routing threads */
223 /** @brief callback that is called when thread of execution completes */
225
227
229
231
232 private:
234 std::optional<double> routing_guidelines_session_rand_;
235};
236
238 public:
240 DestinationManager *destination_manager)
241 : io_ctx_{io_ctx},
242 context_{context},
243 destination_manager_{destination_manager} {}
244
245 enum class Function {
248 };
249
252
254
256
257 bool connect_timed_out() const { return connect_timed_out_; }
258
259 void destination_id(std::optional<mysql_harness::Destination> id) {
260 destination_id_ = std::move(id);
261 }
262 std::optional<mysql_harness::Destination> destination_id() const {
263 return destination_id_;
264 }
265
266 std::string routing_source() const { return destination_->route_name(); }
267 void set_routing_source(std::string name) {
268 destination_->set_route_name(std::move(name));
269 }
270
272 return destination_->get_server_info();
273 }
274
276 std::function<void(const mysql_harness::Destination &, std::error_code)>
277 func) {
278 on_connect_failure_ = std::move(func);
279 }
280
282 std::function<void(const mysql_harness::Destination &)> func) {
283 on_connect_success_ = std::move(func);
284 }
285
287 std::function<bool(const mysql_harness::Destination &dest)> func) {
288 on_is_destination_good_ = std::move(func);
289 }
290
293
294 return true;
295 }
296
297 protected:
309
312
317
319
321 std::unique_ptr<Destination> destination_{nullptr};
322 std::vector<mysql_harness::DestinationEndpoint> endpoints_;
323 std::vector<mysql_harness::DestinationEndpoint>::iterator endpoints_it_;
324
326
328
330
332 std::optional<mysql_harness::Destination> destination_id_;
333
334 std::function<void(const mysql_harness::Destination &, std::error_code)>
337 std::function<bool(const mysql_harness::Destination &)>
339};
340
341template <class ConnectionType>
342class Connector : public ConnectorBase {
343 public:
345
348 switch (func_) {
350 auto init_res = init_destination(std::move(session_info));
351 if (!init_res) return stdx::unexpected(init_res.error());
352
353 } break;
355 auto connect_res = connect_finish();
356 if (!connect_res) return stdx::unexpected(connect_res.error());
357
358 } break;
359 }
360
361 if (!destination_id().has_value()) {
362 // stops at 'connect_init()
363 {
364 auto connect_res = try_connect();
365 if (!connect_res) return stdx::unexpected(connect_res.error());
366 }
367 }
369
370 if (socket().is_local()) {
371 return ret_type{std::in_place, std::make_unique<UnixDomainConnection>(
372 std::move(socket().as_local()),
373 std::move(endpoint().as_local()))};
374 }
375
376 return ret_type{std::in_place, std::make_unique<TcpConnection>(
377 std::move(socket().as_tcp()),
378 std::move(endpoint().as_tcp()))};
379 }
380};
381
382#endif /* ROUTING_CONNECTION_INCLUDED */
Definition: connection.h:237
void set_routing_source(std::string name)
Definition: connection.h:267
std::function< bool(const mysql_harness::Destination &)> on_is_destination_good_
Definition: connection.h:338
void connect_timed_out(bool v)
Definition: connection.h:255
net::steady_timer & timer()
Definition: connection.h:253
stdx::expected< void, std::error_code > connect_init()
Definition: connection.cc:94
const routing_guidelines::Server_info & server_info() const
Definition: connection.h:271
std::unique_ptr< Destination > destination_
Definition: connection.h:321
routing_guidelines::Session_info session_info_
Definition: connection.h:318
std::optional< mysql_harness::Destination > destination_id_
Definition: connection.h:332
std::error_code last_ec_
Definition: connection.h:325
net::steady_timer connect_timer_
Definition: connection.h:329
stdx::expected< void, std::error_code > resolve()
Definition: connection.cc:57
Function
Definition: connection.h:245
stdx::expected< void, std::error_code > init_endpoint()
Definition: connection.cc:88
mysql_harness::DestinationEndpoint server_endpoint_
Definition: connection.h:316
stdx::expected< void, std::error_code > connected()
Definition: connection.cc:222
stdx::expected< void, std::error_code > connect_failed(std::error_code ec)
bool connect_timed_out_
Definition: connection.h:331
MySQLRoutingContext & context_
Definition: connection.h:311
void on_connect_failure(std::function< void(const mysql_harness::Destination &, std::error_code)> func)
Definition: connection.h:275
void destination_id(std::optional< mysql_harness::Destination > id)
Definition: connection.h:259
bool is_destination_good(const mysql_harness::Destination &dest) const
Definition: connection.h:291
DestinationManager * destination_manager_
Definition: connection.h:320
std::function< void(const mysql_harness::Destination &)> on_connect_success_
Definition: connection.h:336
mysql_harness::DestinationEndpoint & endpoint()
Definition: connection.h:251
bool connect_timed_out() const
Definition: connection.h:257
Function func_
Definition: connection.h:327
ConnectorBase(net::io_context &io_ctx, MySQLRoutingContext &context, DestinationManager *destination_manager)
Definition: connection.h:239
net::ip::tcp::resolver resolver_
Definition: connection.h:313
std::optional< mysql_harness::Destination > destination_id() const
Definition: connection.h:262
mysql_harness::DestinationSocket & socket()
Definition: connection.h:250
std::vector< mysql_harness::DestinationEndpoint > endpoints_
Definition: connection.h:322
stdx::expected< void, std::error_code > next_destination()
Definition: connection.cc:248
net::io_context & io_ctx_
Definition: connection.h:310
std::string routing_source() const
Definition: connection.h:266
stdx::expected< void, std::error_code > try_connect()
Definition: connection.cc:105
std::vector< mysql_harness::DestinationEndpoint >::iterator endpoints_it_
Definition: connection.h:323
std::function< void(const mysql_harness::Destination &, std::error_code)> on_connect_failure_
Definition: connection.h:335
stdx::expected< void, std::error_code > init_destination(routing_guidelines::Session_info session_info)
Definition: connection.cc:40
stdx::expected< void, std::error_code > connect_finish()
Definition: connection.cc:189
void on_connect_success(std::function< void(const mysql_harness::Destination &)> func)
Definition: connection.h:281
stdx::expected< void, std::error_code > next_endpoint()
Definition: connection.cc:231
void on_is_destination_good(std::function< bool(const mysql_harness::Destination &dest)> func)
Definition: connection.h:286
mysql_harness::DestinationSocket server_sock_
Definition: connection.h:314
Definition: connection.h:342
stdx::expected< ConnectionType, std::error_code > connect(routing_guidelines::Session_info session_info)
Definition: connection.h:346
Manage destinations for a Connection Routing.
Definition: destination.h:163
Monitor pattern.
Definition: monitor.h:39
Definition: connection.h:47
virtual std::optional< mysql_harness::Destination > read_write_destination_id() const
Definition: connection.h:67
void accepted()
Definition: connection.cc:284
MySQLRoutingConnectionBase(MySQLRoutingContext &context, std::function< void(MySQLRoutingConnectionBase *)> remove_callback)
Definition: connection.h:49
clock_type::time_point time_point_type
Definition: connection.h:133
virtual void connected()
Definition: connection.cc:291
virtual void set_routing_source(std::string name)=0
std::optional< double > routing_guidelines_session_rand_
Definition: connection.h:234
const MySQLRoutingContext & context() const
Definition: connection.h:57
time_point_type get_started() const
Definition: connection.h:135
Stats get_stats() const
Definition: connection.h:181
std::size_t get_bytes_down() const
Definition: connection.h:128
std::chrono::system_clock clock_type
Definition: connection.h:132
void transfered_to_server(size_t bytes)
Definition: connection.h:185
virtual std::optional< mysql_harness::DestinationEndpoint > read_write_destination_endpoint() const
Definition: connection.h:81
std::string get_client_address() const
Returns address of client which connected to router.
Definition: connection.h:116
std::size_t get_bytes_up() const
Definition: connection.h:124
Monitor< bool > disconnect_
Definition: connection.h:228
net::impl::socket::native_handle_type client_fd_
Definition: connection.h:233
virtual std::optional< mysql_harness::DestinationEndpoint > destination_endpoint() const =0
std::string get_server_address() const
Returns address of server to which connection is established.
Definition: connection.h:101
void disassociate()
Definition: connection.h:201
void log_connection_summary()
Definition: connection.cc:305
virtual std::string get_routing_source() const =0
Monitor< Stats > stats_
Definition: connection.h:226
virtual void disconnect()=0
time_point_type get_connected_to_server() const
Definition: connection.h:139
virtual std::optional< mysql_harness::Destination > read_only_destination_id() const
Definition: connection.h:62
routing_guidelines::Session_info get_session_info() const
Definition: connection.cc:320
std::function< void(MySQLRoutingConnectionBase *)> remove_callback_
callback that is called when thread of execution completes
Definition: connection.h:224
virtual ~MySQLRoutingConnectionBase()=default
MySQLRoutingContext & context()
Definition: connection.h:56
void client_address(const std::string &dest)
Definition: connection.h:120
void transfered_to_client(size_t bytes)
Definition: connection.h:193
virtual void wait_until_completed()=0
virtual void completed()=0
time_point_type get_last_received_from_server() const
Definition: connection.h:147
time_point_type get_last_sent_to_server() const
Definition: connection.h:143
void set_routing_guidelines_session_rand()
Definition: connection.cc:342
virtual std::optional< mysql_harness::DestinationEndpoint > read_only_destination_endpoint() const
Definition: connection.h:76
auto disconnect_request(F &&f)
Definition: connection.h:208
virtual std::optional< mysql_harness::Destination > get_destination_id() const =0
virtual net::impl::socket::native_handle_type get_client_fd() const =0
void server_address(const std::string &dest)
Definition: connection.h:105
virtual routing_guidelines::Server_info get_server_info() const =0
bool disconnect_requested() const
Definition: connection.h:212
MySQLRoutingContext & context_
wrapper for common data used by all routing threads
Definition: connection.h:222
MySQLRoutingContext holds data used by MySQLRouting (1 per plugin instances) and MySQLRoutingConnecti...
Definition: context.h:54
Definition: destination_endpoint.h:38
Definition: destination_socket.h:40
Definition: destination.h:95
Definition: socket.h:1090
Definition: timer.h:57
Definition: io_context.h:61
Definition: internet.h:608
Definition: expected.h:286
struct stats stats
Definition: mysqlslap.cc:240
std::error_code make_error_code(DynamicLoaderErrc ec)
make error_code from a DynamicLoaderErrc.
Definition: dynamic_loader.cc:79
int native_handle_type
Definition: socket_constants.h:51
Definition: gcs_xcom_synode.h:64
unexpected(E) -> unexpected< E >
case opt name
Definition: sslopt-case.h:29
Definition: connection.h:152
time_point_type started
Definition: connection.h:175
time_point_type last_sent_to_server
Definition: connection.h:177
time_point_type connected_to_server
Definition: connection.h:176
std::size_t bytes_down
Definition: connection.h:173
Stats(std::string client_address, std::string server_address, 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: connection.h:155
std::string client_address
Definition: connection.h:169
std::string server_address
Definition: connection.h:170
time_point_type last_received_from_server
Definition: connection.h:178
std::size_t bytes_up
Definition: connection.h:172
Information about one server destination.
Definition: routing_guidelines.h:78
Information about incoming session.
Definition: routing_guidelines.h:101
Definition: mysqlslap.cc:242