MySQL 26.10.0
Source Code Documentation
x_connection.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2021, 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 ROUTING_X_CONNECTION_INCLUDED
27#define ROUTING_X_CONNECTION_INCLUDED
28
29#include <memory>
30#include <optional>
31#include <set>
32#include <string>
33
34// #include <mysqlx.pb.h>
35// #include <mysqlx_connection.pb.h>
36
38#include "connection.h" // MySQLRoutingConnectionBase
40
42 public:
43 using Capabilities = std::set<std::string>;
44 struct FrameInfo {
45 size_t frame_size_; //!< size of the whole frame
46 size_t forwarded_frame_size_; //!< size of the forwarded part of frame
47 };
48
49 std::optional<FrameInfo> &current_frame() { return current_frame_; }
50 std::optional<uint8_t> &current_msg_type() { return msg_type_; }
51
52 const Capabilities &caps() { return caps_; }
53 void caps(Capabilities &&caps) { caps_ = std::move(caps); }
54
55 private:
56 std::optional<FrameInfo> current_frame_{};
57 std::optional<uint8_t> msg_type_{};
58
60};
61
64 public std::enable_shared_from_this<MysqlRoutingXConnection> {
65 private:
68
69 // constructor
70 //
71 // use ::create() instead.
73 MySQLRoutingContext &context, DestinationManager *destination_manager,
74 std::unique_ptr<ConnectionBase> client_connection,
75 std::unique_ptr<RoutingConnectionBase> client_routing_connection,
76 std::function<void(MySQLRoutingConnectionBase *)> remove_callback)
77 : MySQLRoutingConnectionBase{context, std::move(remove_callback)},
78 connector_{client_connection->io_ctx(), context, destination_manager},
79 client_conn_{std::move(client_connection),
80 std::move(client_routing_connection),
82 ClientSideConnection::protocol_state_type{}},
84 ServerSideConnection::protocol_state_type{}} {
86
87 if (destination_manager->routing_guidelines_session_rand_used()) {
89 }
90 }
91
92 public:
94
95 // create a shared_ptr<ThisClass>
96 template <typename... Args>
97 [[nodiscard]] static std::shared_ptr<MysqlRoutingXConnection> create(
98 Args &&...args) {
99 // can't use make_unique<> here as the constructor is private.
100 return std::shared_ptr<MysqlRoutingXConnection>(
101 new MysqlRoutingXConnection(std::forward<Args>(args)...));
102 }
103
104 // get a shared-ptr that refers the same 'this'
105 std::shared_ptr<MysqlRoutingXConnection> getptr() {
106 return shared_from_this();
107 }
108
110 std::vector<uint8_t> &error_frame, uint16_t error_code,
111 const std::string &msg, const std::string &sql_state = "HY000",
112 bool is_severity_fatal = false);
113
115
117
119 return client_conn().native_handle();
120 }
121
122 void disconnect() override;
123
124 enum class Function {
126
127 // tls-accept
131
134
137
141
144
145 // cap-get
154
155 // cap-set
160
161 // sess-auth
168
169 // stmt-exec
174
175 // crud::find
180
181 // crud::delete
186
187 // crud::insert
192
193 // crud::update
198
199 // prepare::prepare
204
205 // prepare::deallocate
210
211 // prepare::execute
216
217 // expect::open
222
223 // expect::close
228
229 // crud::create_view
234
235 // crud::modify_view
240
241 // crud::drop_view
246
247 // cursor::open
252
253 // cursor::fetch
258
259 // cursor::close
264
265 // session::close
270
271 // session::reset
276
277 kConnect,
279 kFinish,
280 };
281
283 switch (next) {
285 return client_recv_cmd();
286
288 return tls_accept_init();
290 return tls_accept();
292 return tls_accept_finalize();
293
295 return server_init_tls();
298
300 return tls_connect_init();
302 return tls_connect();
303
305 return server_send_check_caps();
308
310 return client_cap_get();
317
319 return client_cap_set();
334
336 return forward_tls_init();
341
343 return client_sess_auth_start();
354
356 return client_stmt_execute();
363
365 return client_crud_find();
372
374 return client_crud_delete();
381
383 return client_crud_insert();
390
392 return client_crud_update();
399
401 return client_prepare_prepare();
408
417
419 return client_prepare_execute();
426
428 return client_expect_open();
435
437 return client_expect_close();
444
453
462
464 return client_crud_drop_view();
471
473 return client_cursor_open();
480
482 return client_cursor_fetch();
489
491 return client_cursor_close();
498
500 return client_session_close();
507
509 return client_session_reset();
516
518 return connect();
520 return wait_client_close();
522 return finish();
523 }
524 }
525
526 void send_server_failed(std::error_code ec);
527
528 void recv_server_failed(std::error_code ec);
529
530 void send_client_failed(std::error_code ec);
531
532 void recv_client_failed(std::error_code ec);
533
534 void client_socket_failed(std::error_code ec);
535
536 void server_socket_failed(std::error_code ec);
537
538 void async_send_client(Function next);
539
540 void async_recv_client(Function next);
541
542 void async_send_server(Function next);
543
544 void async_recv_server(Function next);
545
546 void async_run();
547
548 void wait_client_close();
549 void finish();
550
551 void server_tls_shutdown();
552 void client_tls_shutdown();
553
554 void done();
555
557 Channel &dst_channel);
558
560
562
563 void forward_tls_init();
564
565 enum class ForwardResult {
570 kFinished,
571 };
572
575
578
579 void forward_client_to_server(Function this_func, Function next_func);
580
581 void forward_server_to_client(Function this_func, Function next_func);
582
585
588 void connect();
589
593
594 void tls_accept_init();
595 void tls_accept();
596 void tls_accept_finalize();
597
598 void server_init_tls();
600 void tls_connect_init();
601 void tls_connect();
602
603 void client_con_close();
604
605 void client_recv_cmd();
606
607 void client_cap_get();
611
612 void client_cap_set();
620
622
629
630 void client_stmt_execute();
634
635 void client_crud_find();
639
640 void client_crud_delete();
644
645 void client_crud_insert();
649
650 void client_crud_update();
654
659
664
669
670 void client_expect_open();
674
675 void client_expect_close();
679
684
689
694
695 void client_cursor_open();
699
700 void client_cursor_fetch();
704
705 void client_cursor_close();
709
714
719
721 return client_conn().protocol();
722 }
724 return client_conn().protocol();
725 }
726
728 return server_conn().protocol();
729 }
731 return server_conn().protocol();
732 }
733
736
739
740 std::optional<mysql_harness::Destination> get_destination_id()
741 const override {
742 return connector().destination_id();
743 }
744
745 std::optional<mysql_harness::DestinationEndpoint> destination_endpoint()
746 const override {
747 return std::nullopt;
748 }
749
750 std::string get_routing_source() const override {
751 return connector().routing_source();
752 }
753
754 void set_routing_source(std::string name) override {
755 return connector().set_routing_source(std::move(name));
756 }
757
758 void wait_until_completed() override {
759 is_completed_.wait([](auto ready) { return ready == true; });
760 }
761
762 void completed() override {
763 is_completed_.serialize_with_cv([](auto &ready, auto &cv) {
764 ready = true;
765 cv.notify_all();
766 });
767 }
768
770 return connector().server_info();
771 }
772
773 private:
775
776 using clock_type = std::chrono::steady_clock;
777
778 clock_type::time_point started_{clock_type::now()};
779 clock_type::time_point last_trace_{clock_type::now()};
780
782
784 const connector_type &connector() const { return connector_; }
785
787
790
792};
793
794#endif
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:247
SSL aware socket buffers.
Definition: channel.h:66
void set_routing_source(std::string name)
Definition: connection.h:267
const routing_guidelines::Server_info & server_info() const
Definition: connection.h:271
void destination_id(std::optional< mysql_harness::Destination > id)
Definition: connection.h:259
std::string routing_source() const
Definition: connection.h:266
Manage destinations for a Connection Routing.
Definition: destination.h:84
bool routing_guidelines_session_rand_used() const
Check if routing guidelines uses $.session.rand as a match criterion.
Definition: destination.h:166
Definition: connection.h:47
std::chrono::system_clock clock_type
Definition: connection.h:132
MySQLRoutingContext & context()
Definition: connection.h:56
void client_address(const std::string &dest)
Definition: connection.h:120
void set_routing_guidelines_session_rand()
Definition: connection.cc:342
MySQLRoutingContext holds data used by MySQLRouting (1 per plugin instances) and MySQLRoutingConnecti...
Definition: context.h:54
Definition: x_connection.h:64
void client_cap_get()
client wants to get the capabilities of the server.
Definition: x_connection.cc:862
void recv_client_failed(std::error_code ec)
Definition: x_connection.cc:354
const ServerSideConnection & server_conn() const
Definition: x_connection.h:738
void server_recv_switch_tls_response()
Definition: x_connection.cc:922
void async_send_server(Function next)
Definition: x_connection.cc:427
void server_recv_prepare_deallocate_response_forward()
Definition: x_connection.cc:2411
void server_recv_expect_close_response_forward_last()
Definition: x_connection.cc:2616
void server_recv_crud_delete_response()
Definition: x_connection.cc:2110
void client_crud_modify_view()
Definition: x_connection.cc:2688
void client_cursor_open()
Definition: x_connection.cc:2817
void server_recv_cap_get_response_forward()
Definition: x_connection.cc:1335
void server_recv_stmt_execute_response_forward()
Definition: x_connection.cc:2024
void server_recv_prepare_execute_response_forward()
Definition: x_connection.cc:2483
void client_cursor_fetch()
Definition: x_connection.cc:2884
void client_stmt_execute()
Definition: x_connection.cc:1967
void server_recv_crud_delete_response_forward()
Definition: x_connection.cc:2156
void server_recv_cap_get_response()
Definition: x_connection.cc:1291
stdx::expected< ForwardResult, std::error_code > forward_frame_from_server_to_client()
Definition: x_connection.cc:710
void client_prepare_execute()
Definition: x_connection.cc:2426
void server_recv_cap_set_response_forward_last()
Definition: x_connection.cc:1867
ServerSideConnection & server_conn()
Definition: x_connection.h:737
void server_recv_switch_tls_response_passthrough_forward_last()
Definition: x_connection.cc:1081
ClientSideConnection client_conn_
Definition: x_connection.h:788
void server_recv_crud_modify_view_response_forward()
Definition: x_connection.cc:2739
void async_run()
Definition: x_connection.cc:314
void server_recv_cursor_close_response()
Definition: x_connection.cc:2958
void server_tls_shutdown()
Definition: x_connection.cc:3191
void client_sess_auth_start()
Definition: x_connection.cc:1874
stdx::expected< ForwardResult, std::error_code > forward_frame_from_client_to_server()
Definition: x_connection.cc:676
void client_crud_drop_view()
Definition: x_connection.cc:2753
void connect()
Definition: x_connection.cc:743
void server_recv_crud_find_response()
Definition: x_connection.cc:2041
void client_crud_create_view()
Definition: x_connection.cc:2623
void server_recv_cap_get_response_forward_last()
Definition: x_connection.cc:1340
ServerSideConnection server_conn_
Definition: x_connection.h:789
static std::shared_ptr< MysqlRoutingXConnection > create(Args &&...args)
Definition: x_connection.h:97
void server_recv_expect_close_response_forward()
Definition: x_connection.cc:2611
void call_next_function(Function next)
Definition: x_connection.h:282
stdx::expected< void, std::error_code > forward_tls(Channel &src_channel, Channel &dst_channel)
Definition: x_connection.cc:1094
int active_work_
Definition: x_connection.h:774
void client_tls_shutdown()
Definition: x_connection.cc:3198
SslMode source_ssl_mode() const
Definition: x_connection.h:114
SslMode dest_ssl_mode() const
Definition: x_connection.h:116
connector_type connector_
Definition: x_connection.h:786
void client_crud_update()
Definition: x_connection.cc:2231
void server_recv_stmt_execute_response_forward_last()
Definition: x_connection.cc:2029
void server_recv_session_close_response_forward()
Definition: x_connection.cc:3067
void forward_tls_server_to_client()
Definition: x_connection.cc:1163
void server_recv_crud_drop_view_response_forward_last()
Definition: x_connection.cc:2810
std::optional< mysql_harness::Destination > get_destination_id() const override
Definition: x_connection.h:740
void client_recv_auth_continue()
Definition: x_connection.cc:1955
Function
Definition: x_connection.h:124
void server_recv_expect_close_response()
Definition: x_connection.cc:2565
void server_recv_prepare_deallocate_response_forward_last()
Definition: x_connection.cc:2418
void server_recv_prepare_execute_response_forward_last()
Definition: x_connection.cc:2489
void server_recv_session_reset_response_forward()
Definition: x_connection.cc:3131
void tls_connect_init()
Definition: x_connection.cc:1205
bool greeting_from_router_
Definition: x_connection.h:781
void client_session_reset()
Definition: x_connection.cc:3080
void server_recv_session_close_response_forward_last()
Definition: x_connection.cc:3073
routing_guidelines::Server_info get_server_info() const override
Definition: x_connection.h:769
void server_recv_prepare_prepare_response_forward_last()
Definition: x_connection.cc:2351
void client_expect_close()
Definition: x_connection.cc:2560
void server_recv_cap_set_response_forward()
Definition: x_connection.cc:1862
void server_recv_cursor_open_response_forward_last()
Definition: x_connection.cc:2877
void forward_client_to_server(Function this_func, Function next_func)
Definition: x_connection.cc:688
void disconnect() override
Definition: x_connection.cc:247
void server_recv_cursor_fetch_response()
Definition: x_connection.cc:2889
void server_socket_failed(std::error_code ec)
Definition: x_connection.cc:362
const ClientSideConnection & client_conn() const
Definition: x_connection.h:735
ServerSideConnection::protocol_state_type & server_protocol()
Definition: x_connection.h:727
void server_recv_session_close_response()
Definition: x_connection.cc:3021
void async_send_client(Function next)
Definition: x_connection.cc:393
void server_recv_cursor_open_response_forward()
Definition: x_connection.cc:2872
std::string get_routing_source() const override
Definition: x_connection.h:750
void async_send_server_buffer(net::const_buffer send_buf, Function next)
Definition: x_connection.cc:1363
void send_server_failed(std::error_code ec)
Definition: x_connection.cc:330
void server_recv_server_greeting_from_server()
Definition: x_connection.cc:3144
void send_client_failed(std::error_code ec)
Definition: x_connection.cc:346
void async_send_client_buffer(net::const_buffer send_buf, Function next)
Definition: x_connection.cc:1345
void client_con_close()
Definition: x_connection.cc:304
void server_recv_crud_insert_response_forward_last()
Definition: x_connection.cc:2224
ClientSideConnection & client_conn()
Definition: x_connection.h:734
void server_recv_switch_tls_response_passthrough_forward()
Definition: x_connection.cc:1074
void server_recv_cursor_close_response_forward()
Definition: x_connection.cc:3004
void forward_tls_client_to_server()
Definition: x_connection.cc:1148
void client_session_close()
Definition: x_connection.cc:3016
ForwardResult
Definition: x_connection.h:565
void client_cursor_close()
Definition: x_connection.cc:2953
void server_recv_cursor_fetch_response_forward_last()
Definition: x_connection.cc:2946
void client_socket_failed(std::error_code ec)
Definition: x_connection.cc:377
void server_recv_prepare_prepare_response()
Definition: x_connection.cc:2299
void server_recv_auth_response_forward()
Definition: x_connection.cc:1945
void server_recv_auth_response_continue()
Definition: x_connection.cc:1950
std::optional< mysql_harness::DestinationEndpoint > destination_endpoint() const override
Definition: x_connection.h:745
void tls_accept_finalize()
Definition: x_connection.cc:1635
void server_recv_session_reset_response()
Definition: x_connection.cc:3085
void server_recv_crud_find_response_forward_last()
Definition: x_connection.cc:2098
void server_send_switch_to_tls()
Definition: x_connection.cc:1712
void client_send_server_greeting_from_router()
Definition: x_connection.cc:461
void client_recv_cmd()
Definition: x_connection.cc:465
void forward_tls_init()
Definition: x_connection.cc:1178
void client_expect_open()
Definition: x_connection.cc:2497
void server_recv_check_caps_response()
Definition: x_connection.cc:1740
void server_recv_crud_create_view_response_forward()
Definition: x_connection.cc:2674
void server_recv_crud_drop_view_response()
Definition: x_connection.cc:2758
void server_send_check_caps()
Definition: x_connection.cc:1727
void forward_server_to_client(Function this_func, Function next_func)
Definition: x_connection.cc:722
void recv_server_failed(std::error_code ec)
Definition: x_connection.cc:338
void server_recv_crud_insert_response_forward()
Definition: x_connection.cc:2219
void server_recv_crud_find_response_forward()
Definition: x_connection.cc:2093
void server_recv_expect_open_response_forward_last()
Definition: x_connection.cc:2553
void server_recv_auth_response()
Definition: x_connection.cc:1896
void server_recv_expect_open_response()
Definition: x_connection.cc:2502
void done()
Definition: x_connection.cc:3189
void tls_connect()
connect server_channel to a TLS server.
Definition: x_connection.cc:1233
void client_crud_insert()
Definition: x_connection.cc:2168
void set_routing_source(std::string name) override
Definition: x_connection.h:754
void server_recv_cursor_open_response()
Definition: x_connection.cc:2822
void server_recv_crud_drop_view_response_forward()
Definition: x_connection.cc:2804
void server_recv_cursor_fetch_response_forward()
Definition: x_connection.cc:2941
void server_recv_cap_set_response()
Definition: x_connection.cc:1816
void server_recv_crud_update_response()
Definition: x_connection.cc:2236
const ServerSideConnection::protocol_state_type & server_protocol() const
Definition: x_connection.h:730
void tls_accept()
accept a TLS handshake.
Definition: x_connection.cc:1585
void server_recv_crud_update_response_forward_last()
Definition: x_connection.cc:2287
void tls_accept_init()
Definition: x_connection.cc:1564
void server_recv_switch_tls_response_passthrough()
Definition: x_connection.cc:1024
void server_recv_crud_modify_view_response_forward_last()
Definition: x_connection.cc:2745
void client_prepare_prepare()
Definition: x_connection.cc:2294
void server_recv_auth_response_forward_last()
Definition: x_connection.cc:1960
MysqlRoutingXConnection(MySQLRoutingContext &context, DestinationManager *destination_manager, std::unique_ptr< ConnectionBase > client_connection, std::unique_ptr< RoutingConnectionBase > client_routing_connection, std::function< void(MySQLRoutingConnectionBase *)> remove_callback)
Definition: x_connection.h:72
void server_recv_crud_insert_response()
Definition: x_connection.cc:2173
void server_recv_session_reset_response_forward_last()
Definition: x_connection.cc:3137
ClientSideConnection::protocol_state_type & client_protocol()
Definition: x_connection.h:720
clock_type::time_point last_trace_
Definition: x_connection.h:779
const connector_type & connector() const
Definition: x_connection.h:784
void server_recv_expect_open_response_forward()
Definition: x_connection.cc:2548
void server_recv_switch_tls_response_passthrough_forward_ok()
Definition: x_connection.cc:1088
void client_cap_set()
client wants to set the capabilities.
Definition: x_connection.cc:1386
void server_recv_cursor_close_response_forward_last()
Definition: x_connection.cc:3009
static stdx::expected< size_t, std::error_code > encode_error_packet(std::vector< uint8_t > &error_frame, uint16_t error_code, const std::string &msg, const std::string &sql_state="HY000", bool is_severity_fatal=false)
Definition: x_connection.cc:288
void server_recv_crud_update_response_forward()
Definition: x_connection.cc:2282
void wait_client_close()
Definition: x_connection.cc:3184
void client_crud_find()
Definition: x_connection.cc:2036
net::impl::socket::native_handle_type get_client_fd() const override
Definition: x_connection.h:118
const ClientSideConnection::protocol_state_type & client_protocol() const
Definition: x_connection.h:723
WaitableMonitor< bool > is_completed_
Definition: x_connection.h:791
void server_init_tls()
Definition: x_connection.cc:1657
void server_recv_prepare_deallocate_response()
Definition: x_connection.cc:2364
void finish()
Definition: x_connection.cc:3148
void server_recv_crud_delete_response_forward_last()
Definition: x_connection.cc:2161
void completed() override
Definition: x_connection.h:762
void async_recv_server(Function next)
Definition: x_connection.cc:448
void server_recv_crud_create_view_response_forward_last()
Definition: x_connection.cc:2680
void server_recv_crud_create_view_response()
Definition: x_connection.cc:2628
std::shared_ptr< MysqlRoutingXConnection > getptr()
Definition: x_connection.h:105
void wait_until_completed() override
Definition: x_connection.h:758
void server_recv_prepare_prepare_response_forward()
Definition: x_connection.cc:2345
void server_recv_prepare_execute_response()
Definition: x_connection.cc:2431
void server_recv_crud_modify_view_response()
Definition: x_connection.cc:2693
void client_prepare_deallocate()
Definition: x_connection.cc:2359
connector_type & connector()
Definition: x_connection.h:783
void async_recv_client(Function next)
Definition: x_connection.cc:414
void server_recv_stmt_execute_response()
Definition: x_connection.cc:1972
void client_crud_delete()
Definition: x_connection.cc:2105
clock_type::time_point started_
Definition: x_connection.h:778
typename base_class_::protocol_state_type protocol_state_type
Definition: basic_protocol_splicer.h:91
protocol_state_type & protocol()
Definition: connection_base.h:399
std::unique_ptr< ConnectionBase > & connection()
Definition: connection_base.h:405
net::impl::socket::native_handle_type native_handle() const
Definition: connection_base.h:368
SslMode ssl_mode() const
Definition: connection_base.h:364
Monitor can be waited for.
Definition: monitor.h:62
Definition: x_connection.h:41
std::optional< uint8_t > msg_type_
Definition: x_connection.h:57
std::optional< FrameInfo > & current_frame()
Definition: x_connection.h:49
std::set< std::string > Capabilities
Definition: x_connection.h:43
const Capabilities & caps()
Definition: x_connection.h:52
void caps(Capabilities &&caps)
Definition: x_connection.h:53
std::optional< FrameInfo > current_frame_
Definition: x_connection.h:56
Capabilities caps_
Definition: x_connection.h:59
std::optional< uint8_t > & current_msg_type()
Definition: x_connection.h:50
Definition: buffer.h:135
int native_handle_type
Definition: socket_constants.h:51
Define std::hash<Gtid>.
Definition: gtid.h:355
SslMode
Definition: ssl_mode.h:29
case opt name
Definition: sslopt-case.h:29
Definition: x_connection.h:44
size_t frame_size_
size of the whole frame
Definition: x_connection.h:45
size_t forwarded_frame_size_
size of the forwarded part of frame
Definition: x_connection.h:46
Information about one server destination.
Definition: routing_guidelines.h:80