MySQL 9.2.0
Source Code Documentation
x_connection.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2021, 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_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:251
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:163
bool routing_guidelines_session_rand_used() const
Check if routing guidelines uses $.session.rand as a match criterion.
Definition: destination.h:245
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:2399
void server_recv_expect_close_response_forward_last()
Definition: x_connection.cc:2604
void server_recv_crud_delete_response()
Definition: x_connection.cc:2098
void client_crud_modify_view()
Definition: x_connection.cc:2676
void client_cursor_open()
Definition: x_connection.cc:2805
void server_recv_cap_get_response_forward()
Definition: x_connection.cc:1335
void server_recv_stmt_execute_response_forward()
Definition: x_connection.cc:2012
void server_recv_prepare_execute_response_forward()
Definition: x_connection.cc:2471
void client_cursor_fetch()
Definition: x_connection.cc:2872
void client_stmt_execute()
Definition: x_connection.cc:1955
void server_recv_crud_delete_response_forward()
Definition: x_connection.cc:2144
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:2414
void server_recv_cap_set_response_forward_last()
Definition: x_connection.cc:1855
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:2727
void async_run()
Definition: x_connection.cc:313
void server_recv_cursor_close_response()
Definition: x_connection.cc:2946
void server_tls_shutdown()
Definition: x_connection.cc:3179
void client_sess_auth_start()
Definition: x_connection.cc:1862
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:2741
void connect()
Definition: x_connection.cc:742
void server_recv_crud_find_response()
Definition: x_connection.cc:2029
void client_crud_create_view()
Definition: x_connection.cc:2611
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:2599
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:3186
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:2219
void server_recv_stmt_execute_response_forward_last()
Definition: x_connection.cc:2017
void server_recv_session_close_response_forward()
Definition: x_connection.cc:3055
void forward_tls_server_to_client()
Definition: x_connection.cc:1162
void server_recv_crud_drop_view_response_forward_last()
Definition: x_connection.cc:2798
std::optional< mysql_harness::Destination > get_destination_id() const override
Definition: x_connection.h:740
void client_recv_auth_continue()
Definition: x_connection.cc:1943
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:2553
void server_recv_prepare_deallocate_response_forward_last()
Definition: x_connection.cc:2406
void server_recv_prepare_execute_response_forward_last()
Definition: x_connection.cc:2477
void server_recv_session_reset_response_forward()
Definition: x_connection.cc:3119
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:3068
void server_recv_session_close_response_forward_last()
Definition: x_connection.cc:3061
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:2339
void client_expect_close()
Definition: x_connection.cc:2548
void server_recv_cap_set_response_forward()
Definition: x_connection.cc:1850
void server_recv_cursor_open_response_forward_last()
Definition: x_connection.cc:2865
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:2877
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:3009
void async_send_client(Function next)
Definition: x_connection.cc:392
void server_recv_cursor_open_response_forward()
Definition: x_connection.cc:2860
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:3132
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:2212
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:2992
void forward_tls_client_to_server()
Definition: x_connection.cc:1147
void client_session_close()
Definition: x_connection.cc:3004
ForwardResult
Definition: x_connection.h:565
void client_cursor_close()
Definition: x_connection.cc:2941
void server_recv_cursor_fetch_response_forward_last()
Definition: x_connection.cc:2934
void client_socket_failed(std::error_code ec)
Definition: x_connection.cc:376
void server_recv_prepare_prepare_response()
Definition: x_connection.cc:2287
void server_recv_auth_response_forward()
Definition: x_connection.cc:1933
void server_recv_auth_response_continue()
Definition: x_connection.cc:1938
std::optional< mysql_harness::DestinationEndpoint > destination_endpoint() const override
Definition: x_connection.h:745
void tls_accept_finalize()
Definition: x_connection.cc:1629
void server_recv_session_reset_response()
Definition: x_connection.cc:3073
void server_recv_crud_find_response_forward_last()
Definition: x_connection.cc:2086
void server_send_switch_to_tls()
Definition: x_connection.cc:1706
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:2485
void server_recv_check_caps_response()
Definition: x_connection.cc:1734
void server_recv_crud_create_view_response_forward()
Definition: x_connection.cc:2662
void server_recv_crud_drop_view_response()
Definition: x_connection.cc:2746
void server_send_check_caps()
Definition: x_connection.cc:1721
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:2207
void server_recv_crud_find_response_forward()
Definition: x_connection.cc:2081
void server_recv_expect_open_response_forward_last()
Definition: x_connection.cc:2541
void server_recv_auth_response()
Definition: x_connection.cc:1884
void server_recv_expect_open_response()
Definition: x_connection.cc:2490
void done()
Definition: x_connection.cc:3177
void tls_connect()
connect server_channel to a TLS server.
Definition: x_connection.cc:1233
void client_crud_insert()
Definition: x_connection.cc:2156
void set_routing_source(std::string name) override
Definition: x_connection.h:754
void server_recv_cursor_open_response()
Definition: x_connection.cc:2810
void server_recv_crud_drop_view_response_forward()
Definition: x_connection.cc:2792
void server_recv_cursor_fetch_response_forward()
Definition: x_connection.cc:2929
void server_recv_cap_set_response()
Definition: x_connection.cc:1804
void server_recv_crud_update_response()
Definition: x_connection.cc:2224
const ServerSideConnection::protocol_state_type & server_protocol() const
Definition: x_connection.h:730
void tls_accept()
accept a TLS handshake.
Definition: x_connection.cc:1579
void server_recv_crud_update_response_forward_last()
Definition: x_connection.cc:2275
void tls_accept_init()
Definition: x_connection.cc:1559
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:2733
void client_prepare_prepare()
Definition: x_connection.cc:2282
void server_recv_auth_response_forward_last()
Definition: x_connection.cc:1948
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:2161
void server_recv_session_reset_response_forward_last()
Definition: x_connection.cc:3125
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:2536
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:2997
void server_recv_crud_update_response_forward()
Definition: x_connection.cc:2270
void wait_client_close()
Definition: x_connection.cc:3172
void client_crud_find()
Definition: x_connection.cc:2024
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:1651
void server_recv_prepare_deallocate_response()
Definition: x_connection.cc:2352
void finish()
Definition: x_connection.cc:3136
void server_recv_crud_delete_response_forward_last()
Definition: x_connection.cc:2149
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:2668
void server_recv_crud_create_view_response()
Definition: x_connection.cc:2616
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:2333
void server_recv_prepare_execute_response()
Definition: x_connection.cc:2419
void server_recv_crud_modify_view_response()
Definition: x_connection.cc:2681
void client_prepare_deallocate()
Definition: x_connection.cc:2347
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:1960
void client_crud_delete()
Definition: x_connection.cc:2093
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:398
std::unique_ptr< ConnectionBase > & connection()
Definition: connection_base.h:404
net::impl::socket::native_handle_type native_handle() const
Definition: connection_base.h:367
SslMode ssl_mode() const
Definition: connection_base.h:363
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
Definition: gcs_xcom_synode.h:64
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:78