forked from mysql/mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassic_reset_connection_forwarder.cc
203 lines (165 loc) · 6.25 KB
/
classic_reset_connection_forwarder.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
Copyright (c) 2023, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is also distributed with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have included with MySQL.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "classic_reset_connection_forwarder.h"
#include "classic_connection_base.h"
#include "classic_frame.h"
#include "classic_lazy_connect.h"
#include "classic_query_sender.h"
#include "mysql/harness/logging/logging.h"
#include "mysql/harness/stdx/expected.h"
#include "mysql/harness/tls_error.h"
IMPORT_LOG_FUNCTIONS()
/**
* forward the reset-connection message flow.
*
* Expected overall flow:
*
* @code
* c->s: COM_RESET_CONNECTION
* c<-s: Ok
* @endcode
*
* If there is no server connection, it is created on demand.
*/
stdx::expected<Processor::Result, std::error_code>
ResetConnectionForwarder::process() {
switch (stage()) {
case Stage::Command:
return command();
case Stage::Connect:
return connect();
case Stage::Connected:
return connected();
case Stage::Response:
return response();
case Stage::Ok:
return ok();
case Stage::Done:
return Result::Done;
}
harness_assert_this_should_not_execute();
}
stdx::expected<Processor::Result, std::error_code>
ResetConnectionForwarder::command() {
if (auto &tr = tracer()) {
tr.trace(Tracer::Event().stage("reset_connection::command"));
}
auto &server_conn = connection()->socket_splicer()->server_conn();
if (!server_conn.is_open()) {
stage(Stage::Connect);
return Result::Again;
} else {
stage(Stage::Response);
return forward_client_to_server();
}
}
stdx::expected<Processor::Result, std::error_code>
ResetConnectionForwarder::connect() {
if (auto &tr = tracer()) {
tr.trace(Tracer::Event().stage("reset_connection::connect"));
}
stage(Stage::Connected);
return mysql_reconnect_start();
}
stdx::expected<Processor::Result, std::error_code>
ResetConnectionForwarder::connected() {
auto &server_conn = connection()->socket_splicer()->server_conn();
if (!server_conn.is_open()) {
auto *socket_splicer = connection()->socket_splicer();
auto *src_channel = socket_splicer->client_channel();
auto *src_protocol = connection()->client_protocol();
// take the client::command from the connection.
auto recv_res =
ClassicFrame::ensure_has_full_frame(src_channel, src_protocol);
if (!recv_res) return recv_client_failed(recv_res.error());
discard_current_msg(src_channel, src_protocol);
if (auto &tr = tracer()) {
tr.trace(Tracer::Event().stage("reset_connection::connect::error"));
}
stage(Stage::Done);
return reconnect_send_error_msg(src_channel, src_protocol);
}
if (auto &tr = tracer()) {
tr.trace(Tracer::Event().stage("reset_connection::connected"));
}
stage(Stage::Response);
return forward_client_to_server();
}
stdx::expected<Processor::Result, std::error_code>
ResetConnectionForwarder::response() {
auto *socket_splicer = connection()->socket_splicer();
auto src_channel = socket_splicer->server_channel();
auto src_protocol = connection()->server_protocol();
auto read_res =
ClassicFrame::ensure_has_msg_prefix(src_channel, src_protocol);
if (!read_res) return recv_server_failed(read_res.error());
const uint8_t msg_type = src_protocol->current_msg_type().value();
enum class Msg {
Ok = ClassicFrame::cmd_byte<classic_protocol::message::server::Ok>(),
};
// reset-connection is not expected to fail.
switch (Msg{msg_type}) {
case Msg::Ok:
stage(Stage::Ok);
return Result::Again;
}
log_debug("reset_connection::response: unexpected msg-type '%02x'", msg_type);
return stdx::make_unexpected(make_error_code(std::errc::bad_message));
}
stdx::expected<Processor::Result, std::error_code>
ResetConnectionForwarder::ok() {
auto *socket_splicer = connection()->socket_splicer();
auto *src_channel = socket_splicer->server_channel();
auto *src_protocol = connection()->server_protocol();
auto msg_res =
ClassicFrame::recv_msg<classic_protocol::borrowed::message::server::Ok>(
src_channel, src_protocol);
if (!msg_res) return recv_server_failed(msg_res.error());
if (auto &tr = tracer()) {
tr.trace(Tracer::Event().stage("reset_connection::ok"));
}
auto msg = *msg_res;
if (!msg.session_changes().empty()) {
auto track_res = connection()->track_session_changes(
net::buffer(msg.session_changes()),
src_protocol->shared_capabilities());
}
// allow connection sharing again.
connection()->connection_sharing_allowed_reset();
// clear the warnings
connection()->execution_context().diagnostics_area().warnings().clear();
// clear the prepared statements.
connection()->client_protocol()->prepared_statements().clear();
if (connection()->context().connection_sharing() &&
connection()->greeting_from_router()) {
// if connection sharing is enabled in the config, enable the
// session-tracker.
connection()->push_processor(std::make_unique<QuerySender>(connection(), R"(
SET @@SESSION.session_track_schema = 'ON',
@@SESSION.session_track_system_variables = '*',
@@SESSION.session_track_transaction_info = 'CHARACTERISTICS',
@@SESSION.session_track_gtids = 'OWN_GTID',
@@SESSION.session_track_state_change = 'ON')"));
stage(Stage::Done);
} else {
stage(Stage::Done);
}
return forward_server_to_client();
}