-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathtest_session_attach.cc
337 lines (258 loc) · 10.5 KB
/
test_session_attach.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* Copyright (c) 2017, 2025, 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 designed to work 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 either included with
the program or referenced in the documentation.
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, version 2.0, 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 <array>
#include <utility>
#define LOG_COMPONENT_TAG "test_session_attach"
#include <mysql/components/my_service.h>
#include <mysql/components/services/log_builtins.h>
#include <mysqld_error.h>
#include <fcntl.h>
#include <mysql/plugin.h>
#include <sys/types.h>
#include <cstdlib>
#include "m_string.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "mysql_com.h"
#include "plugin/test_service_sql_api/helper/test_context.h"
static SERVICE_TYPE(registry) *reg_srv = nullptr;
SERVICE_TYPE(log_builtins) *log_bi = nullptr;
SERVICE_TYPE(log_builtins_string) *log_bs = nullptr;
static Test_context *test_context{nullptr};
static MYSQL_THDVAR_INT(var_int, PLUGIN_VAR_OPCMDARG, "Test variable", nullptr,
nullptr, 0, 0, 2147483, 0);
static int expected_session_variable_value(const int session_index) {
return (session_index + 1) * 10;
}
static void handle_log_error(void *, uint sql_errno, const char *err_msg,
const char *) {
test_context->log_test_line("SQL execution failed with ", sql_errno,
" error and message: ", err_msg);
}
const struct st_command_service_cbs sql_cbs = {
nullptr, // sql_start_result_metadata,
nullptr, // sql_field_metadata,
nullptr, // sql_end_result_metadata,
nullptr, // sql_start_row,
nullptr, // sql_end_row,
nullptr, // sql_abort_row,
nullptr, // sql_get_client_capabilities,
nullptr, // sql_get_null,
nullptr, // sql_get_integer,
nullptr, // sql_get_longlong,
nullptr, // sql_get_decimal,
nullptr, // sql_get_double,
nullptr, // sql_get_date,
nullptr, // sql_get_time,
nullptr, // sql_get_datetime,
nullptr, // sql_get_string,
nullptr, // sql_handle_ok,
handle_log_error, // sql_handle_error,
nullptr, // sql_shutdown,
nullptr // sql_alive
};
static void exec_test_cmd(MYSQL_SESSION session, const char *test_cmd) {
COM_DATA cmd;
test_context->log_test_line(test_cmd);
memset(&cmd, 0, sizeof(cmd));
cmd.com_query.query = test_cmd;
cmd.com_query.length = strlen(cmd.com_query.query);
const bool failed =
0 != command_service_run_command(session, COM_QUERY, &cmd,
&my_charset_utf8mb3_general_ci, &sql_cbs,
CS_BINARY_REPRESENTATION, nullptr);
if (failed) {
test_context->log_error("exec_test_cmd: ret code: ", failed);
}
}
static void test_sql() {
DBUG_TRACE;
constexpr int number_of_sessions = 10;
std::array<MYSQL_SESSION, number_of_sessions> sessions;
/* Open Sessions */
for (int i = 0; i < number_of_sessions; ++i) {
sessions[i] = srv_session_open(nullptr, nullptr);
test_context->log_test_line("Opening Session ", i + 1);
if (!sessions[i]) {
test_context->log_test_line("Opening Session ", i + 1, " failed.");
test_context->log_error("Open Session failed.");
}
}
test_context->separator();
for (uint i = 0; i < number_of_sessions; i++) {
const int buffer_size = 256;
std::string buffer(buffer_size, '\0');
auto session_id_text = std::to_string(i + 1);
if (1 == session_id_text.length()) session_id_text.insert(0, "0");
test_context->log_test("\nQuery ", session_id_text, ": ");
snprintf(buffer.data(), buffer.length(),
"SET SESSION test_session_attach_var_int = %i;",
expected_session_variable_value(i));
exec_test_cmd(sessions[i], buffer.c_str());
}
test_context->separator();
/* Verify Sessions variables in another
sequence than "set" was done */
for (int i = 0; i < number_of_sessions; ++i) {
const int session_offset = 5;
/* Go in reverse direction, starting from offset */
const int session_index =
number_of_sessions - ((i + session_offset) % number_of_sessions) - 1;
test_context->log_test_line("Attach Session ", i + 1);
if (srv_session_attach(sessions[session_index], nullptr)) {
test_context->log_test_line("Attach Session ", i + 1, " failed.");
continue;
}
test_context->log_test_line("Verify Session ", i + 1, " variable");
auto *session_thd = srv_session_info_get_thd(sessions[session_index]);
if (expected_session_variable_value(session_index) !=
THDVAR(session_thd, var_int)) {
test_context->log_test_line("Verify Session ", i + 1,
" variable failed, actual value is ",
THDVAR(session_thd, var_int));
test_context->log_error("Verify Session variable failed.");
}
test_context->log_test_line("Detach Session ", i + 1);
if (srv_session_detach(sessions[session_index])) {
test_context->log_test_line("Detach Session ", i + 1, " failed.");
test_context->log_error("Detach Session failed.");
continue;
}
}
test_context->separator();
/* Close Sessions */
for (int i = 0; i < number_of_sessions; ++i) {
auto result = srv_session_close(sessions[i]);
test_context->log_test_line("Close Session ", i + 1);
if (result) {
test_context->log_test_line("Close Session ", i + 1, " failed.");
test_context->log_error("Close Session failed.");
}
}
test_context->log_test_line("Closed all sessions");
}
struct test_thread_context {
my_thread_handle thread;
bool thread_finished;
void (*test_function)();
};
static void *test_sql_threaded_wrapper(void *param) {
auto *thread_context = (struct test_thread_context *)param;
test_context->separator();
test_context->log_test_line("init thread");
if (srv_session_init_thread(test_context->get_plugin_handler()))
test_context->log_error("srv_session_init_thread failed.");
thread_context->test_function();
test_context->log_test_line("deinit thread");
srv_session_deinit_thread();
thread_context->thread_finished = true;
return nullptr;
}
static void test_in_spawned_thread(void (*test_function)()) {
my_thread_attr_t attr; /* Thread attributes */
my_thread_attr_init(&attr);
(void)my_thread_attr_setdetachstate(&attr, MY_THREAD_CREATE_JOINABLE);
struct test_thread_context thread_context;
thread_context.thread_finished = false;
thread_context.test_function = test_function;
/* now create the thread and call test_session within the thread. */
if (my_thread_create(&(thread_context.thread), &attr,
test_sql_threaded_wrapper, &thread_context) != 0) {
test_context->log_error("Could not create test session thread");
} else {
my_thread_join(&thread_context.thread, nullptr);
}
}
extern "C" {
bool execute_test_init(UDF_INIT *, UDF_ARGS *, char *);
long long execute_test(UDF_INIT *, UDF_ARGS *, unsigned char *,
unsigned char *);
}
/*
UDF responsible for starting the test-case
Test case must be run after plugin was installed.
Plugin API while installing plugin leaves system variables half
initialized , which can't be used in test_sql_service_plugin_init.
*/
long long execute_test(UDF_INIT *, UDF_ARGS *, unsigned char *,
unsigned char *) {
test_context->separator();
test_context->log_test_line(
"Test in a server thread. Attach must fail on non srv_session thread.");
test_sql();
/* Test in a new thread */
test_context->log_test_line("Follows threaded run. Successful scenario.");
test_in_spawned_thread(test_sql);
return 0;
}
/*
UDF initialization function
*/
bool execute_test_init(UDF_INIT *, UDF_ARGS *, char *error_message_buffer) {
if (nullptr == test_context) {
snprintf(error_message_buffer, MYSQL_ERRMSG_SIZE,
"Daemon plugin was not installed.");
return true;
}
return false;
}
/*
Plugin initialization function
Required for session variable registration.
*/
static int test_sql_service_plugin_init(void *p) {
DBUG_TRACE;
if (init_logging_service_for_plugin(®_srv, &log_bi, &log_bs)) return 1;
LogPluginErr(INFORMATION_LEVEL, ER_LOG_PRINTF_MSG, "Installation.");
test_context = new Test_context("test_session_attach", p);
return 0;
}
/*
Plugin deinitialization function
*/
static int test_sql_service_plugin_deinit(void *p [[maybe_unused]]) {
DBUG_TRACE;
LogPluginErr(INFORMATION_LEVEL, ER_LOG_PRINTF_MSG, "Uninstallation.");
delete test_context;
test_context = nullptr;
deinit_logging_service_for_plugin(®_srv, &log_bi, &log_bs);
return 0;
}
static SYS_VAR *plugin_system_variables[] = {MYSQL_SYSVAR(var_int), nullptr};
struct st_mysql_daemon test_sql_service_plugin = {
MYSQL_DAEMON_INTERFACE_VERSION};
/*
Plugin library descriptor
*/
mysql_declare_plugin(test_daemon){
MYSQL_DAEMON_PLUGIN,
&test_sql_service_plugin,
"test_session_attach",
PLUGIN_AUTHOR_ORACLE,
"Test session THDVAR",
PLUGIN_LICENSE_GPL,
test_sql_service_plugin_init, /* Plugin Init */
nullptr, /* Plugin Check uninstall */
test_sql_service_plugin_deinit, /* Plugin Deinit */
0x0100, /* 1.0 */
nullptr, /* status variables */
plugin_system_variables, /* system variables */
nullptr, /* config options */
0, /* flags */
} mysql_declare_plugin_end;