MySQL 9.7.1
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 <string>
32
33#include <mysqlx.pb.h>
34#include <mysqlx_connection.pb.h>
35
37#include "connection.h" // MySQLRoutingConnectionBase
39
41 public:
42 struct FrameInfo {
43 size_t frame_size_; //!< size of the whole frame
44 size_t forwarded_frame_size_; //!< size of the forwarded part of frame
45 };
46
47 std::optional<FrameInfo> &current_frame() { return current_frame_; }
48 std::optional<uint8_t> &current_msg_type() { return msg_type_; }
49
51 void caps(std::unique_ptr<Mysqlx::Connection::Capabilities> caps) {
52 caps_ = std::move(caps);
53 }
54
55 private:
56 std::optional<FrameInfo> current_frame_{};
57 std::optional<uint8_t> msg_type_{};
58
59 std::unique_ptr<Mysqlx::Connection::Capabilities> caps_;
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",
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:65
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:861
void recv_client_failed(std::error_code ec)
Definition: x_connection.cc:353
const ServerSideConnection & server_conn() const
Definition: x_connection.h:738
void server_recv_switch_tls_response()
Definition: x_connection.cc:921
void async_send_server(Function next)
Definition: x_connection.cc:426
void server_recv_prepare_deallocate_response_forward()
Definition: x_connection.cc:2405
void server_recv_expect_close_response_forward_last()
Definition: x_connection.cc:2610
void server_recv_crud_delete_response()
Definition: x_connection.cc:2104
void client_crud_modify_view()
Definition: x_connection.cc:2682
void client_cursor_open()
Definition: x_connection.cc:2811
void server_recv_cap_get_response_forward()
Definition: x_connection.cc:1335
void server_recv_stmt_execute_response_forward()
Definition: x_connection.cc:2018
void server_recv_prepare_execute_response_forward()
Definition: x_connection.cc:2477
void client_cursor_fetch()
Definition: x_connection.cc:2878
void client_stmt_execute()
Definition: x_connection.cc:1961
void server_recv_crud_delete_response_forward()
Definition: x_connection.cc:2150
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:709
void client_prepare_execute()
Definition: x_connection.cc:2420
void server_recv_cap_set_response_forward_last()
Definition: x_connection.cc:1861
ServerSideConnection & server_conn()
Definition: x_connection.h:737
void server_recv_switch_tls_response_passthrough_forward_last()
Definition: x_connection.cc:1080
ClientSideConnection client_conn_
Definition: x_connection.h:788
void server_recv_crud_modify_view_response_forward()
Definition: x_connection.cc:2733
void async_run()
Definition: x_connection.cc:313
void server_recv_cursor_close_response()
Definition: x_connection.cc:2952
void server_tls_shutdown()
Definition: x_connection.cc:3185
void client_sess_auth_start()
Definition: x_connection.cc:1868
stdx::expected< ForwardResult, std::error_code > forward_frame_from_client_to_server()
Definition: x_connection.cc:675
void client_crud_drop_view()
Definition: x_connection.cc:2747
void connect()
Definition: x_connection.cc:742
void server_recv_crud_find_response()
Definition: x_connection.cc:2035
void client_crud_create_view()
Definition: x_connection.cc:2617
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:2605
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:1093
int active_work_
Definition: x_connection.h:774
void client_tls_shutdown()
Definition: x_connection.cc:3192
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:2225
void server_recv_stmt_execute_response_forward_last()
Definition: x_connection.cc:2023
void server_recv_session_close_response_forward()
Definition: x_connection.cc:3061
void forward_tls_server_to_client()
Definition: x_connection.cc:1162
void server_recv_crud_drop_view_response_forward_last()
Definition: x_connection.cc:2804
std::optional< mysql_harness::Destination > get_destination_id() const override
Definition: x_connection.h:740
void client_recv_auth_continue()
Definition: x_connection.cc:1949
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", Mysqlx::Error::Severity severity=Mysqlx::Error::ERROR)
Definition: x_connection.cc:288
Function
Definition: x_connection.h:124
void server_recv_expect_close_response()
Definition: x_connection.cc:2559
void server_recv_prepare_deallocate_response_forward_last()
Definition: x_connection.cc:2412
void server_recv_prepare_execute_response_forward_last()
Definition: x_connection.cc:2483
void server_recv_session_reset_response_forward()
Definition: x_connection.cc:3125
void tls_connect_init()
Definition: x_connection.cc:1204
bool greeting_from_router_
Definition: x_connection.h:781
void client_session_reset()
Definition: x_connection.cc:3074
void server_recv_session_close_response_forward_last()
Definition: x_connection.cc:3067
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:2345
void client_expect_close()
Definition: x_connection.cc:2554
void server_recv_cap_set_response_forward()
Definition: x_connection.cc:1856
void server_recv_cursor_open_response_forward_last()
Definition: x_connection.cc:2871
void forward_client_to_server(Function this_func, Function next_func)
Definition: x_connection.cc:687
void disconnect() override
Definition: x_connection.cc:247
void server_recv_cursor_fetch_response()
Definition: x_connection.cc:2883
void server_socket_failed(std::error_code ec)
Definition: x_connection.cc:361
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:3015
void async_send_client(Function next)
Definition: x_connection.cc:392
void server_recv_cursor_open_response_forward()
Definition: x_connection.cc:2866
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:329
void server_recv_server_greeting_from_server()
Definition: x_connection.cc:3138
void send_client_failed(std::error_code ec)
Definition: x_connection.cc:345
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:303
void server_recv_crud_insert_response_forward_last()
Definition: x_connection.cc:2218
ClientSideConnection & client_conn()
Definition: x_connection.h:734
void server_recv_switch_tls_response_passthrough_forward()
Definition: x_connection.cc:1073
void server_recv_cursor_close_response_forward()
Definition: x_connection.cc:2998
void forward_tls_client_to_server()
Definition: x_connection.cc:1147
void client_session_close()
Definition: x_connection.cc:3010
ForwardResult
Definition: x_connection.h:565
void client_cursor_close()
Definition: x_connection.cc:2947
void server_recv_cursor_fetch_response_forward_last()
Definition: x_connection.cc:2940
void client_socket_failed(std::error_code ec)
Definition: x_connection.cc:376
void server_recv_prepare_prepare_response()
Definition: x_connection.cc:2293
void server_recv_auth_response_forward()
Definition: x_connection.cc:1939
void server_recv_auth_response_continue()
Definition: x_connection.cc:1944
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:3079
void server_recv_crud_find_response_forward_last()
Definition: x_connection.cc:2092
void server_send_switch_to_tls()
Definition: x_connection.cc:1712
void client_send_server_greeting_from_router()
Definition: x_connection.cc:460
void client_recv_cmd()
Definition: x_connection.cc:464
void forward_tls_init()
Definition: x_connection.cc:1177
void client_expect_open()
Definition: x_connection.cc:2491
void server_recv_check_caps_response()
Definition: x_connection.cc:1740
void server_recv_crud_create_view_response_forward()
Definition: x_connection.cc:2668
void server_recv_crud_drop_view_response()
Definition: x_connection.cc:2752
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:721
void recv_server_failed(std::error_code ec)
Definition: x_connection.cc:337
void server_recv_crud_insert_response_forward()
Definition: x_connection.cc:2213
void server_recv_crud_find_response_forward()
Definition: x_connection.cc:2087
void server_recv_expect_open_response_forward_last()
Definition: x_connection.cc:2547
void server_recv_auth_response()
Definition: x_connection.cc:1890
void server_recv_expect_open_response()
Definition: x_connection.cc:2496
void done()
Definition: x_connection.cc:3183
void tls_connect()
connect server_channel to a TLS server.
Definition: x_connection.cc:1233
void client_crud_insert()
Definition: x_connection.cc:2162
void set_routing_source(std::string name) override
Definition: x_connection.h:754
void server_recv_cursor_open_response()
Definition: x_connection.cc:2816
void server_recv_crud_drop_view_response_forward()
Definition: x_connection.cc:2798
void server_recv_cursor_fetch_response_forward()
Definition: x_connection.cc:2935
void server_recv_cap_set_response()
Definition: x_connection.cc:1810
void server_recv_crud_update_response()
Definition: x_connection.cc:2230
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:2281
void tls_accept_init()
Definition: x_connection.cc:1565
void server_recv_switch_tls_response_passthrough()
Definition: x_connection.cc:1023
void server_recv_crud_modify_view_response_forward_last()
Definition: x_connection.cc:2739
void client_prepare_prepare()
Definition: x_connection.cc:2288
void server_recv_auth_response_forward_last()
Definition: x_connection.cc:1954
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:2167
void server_recv_session_reset_response_forward_last()
Definition: x_connection.cc:3131
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:2542
void server_recv_switch_tls_response_passthrough_forward_ok()
Definition: x_connection.cc:1087
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:3003
void server_recv_crud_update_response_forward()
Definition: x_connection.cc:2276
void wait_client_close()
Definition: x_connection.cc:3178
void client_crud_find()
Definition: x_connection.cc:2030
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:2358
void finish()
Definition: x_connection.cc:3142
void server_recv_crud_delete_response_forward_last()
Definition: x_connection.cc:2155
void completed() override
Definition: x_connection.h:762
void async_recv_server(Function next)
Definition: x_connection.cc:447
void server_recv_crud_create_view_response_forward_last()
Definition: x_connection.cc:2674
void server_recv_crud_create_view_response()
Definition: x_connection.cc:2622
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:2339
void server_recv_prepare_execute_response()
Definition: x_connection.cc:2425
void server_recv_crud_modify_view_response()
Definition: x_connection.cc:2687
void client_prepare_deallocate()
Definition: x_connection.cc:2353
connector_type & connector()
Definition: x_connection.h:783
void async_recv_client(Function next)
Definition: x_connection.cc:413
void server_recv_stmt_execute_response()
Definition: x_connection.cc:1966
void client_crud_delete()
Definition: x_connection.cc:2099
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:40
std::optional< uint8_t > msg_type_
Definition: x_connection.h:57
std::optional< FrameInfo > & current_frame()
Definition: x_connection.h:47
Mysqlx::Connection::Capabilities * caps()
Definition: x_connection.h:50
std::optional< FrameInfo > current_frame_
Definition: x_connection.h:56
void caps(std::unique_ptr< Mysqlx::Connection::Capabilities > caps)
Definition: x_connection.h:51
std::unique_ptr< Mysqlx::Connection::Capabilities > caps_
Definition: x_connection.h:59
std::optional< uint8_t > & current_msg_type()
Definition: x_connection.h:48
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
Capabilities.
Definition: mysqlx_connection.proto:53
Severity
Definition: mysqlx.proto:279
@ ERROR
Definition: mysqlx.proto:280
Definition: x_connection.h:42
size_t frame_size_
size of the whole frame
Definition: x_connection.h:43
size_t forwarded_frame_size_
size of the forwarded part of frame
Definition: x_connection.h:44
Information about one server destination.
Definition: routing_guidelines.h:80