forked from alibaba/AliSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysqltest.cc
More file actions
11495 lines (9922 loc) · 365 KB
/
mysqltest.cc
File metadata and controls
11495 lines (9922 loc) · 365 KB
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2000, 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.
/// @file
///
/// mysqltest client - Tool used for executing a .test file.
///
/// See @ref PAGE_MYSQL_TEST_RUN "The MySQL Test Framework" for more
/// information.
#include "client/client_query_attributes.h"
#include "client/mysqltest/error_names.h"
#include "client/mysqltest/expected_errors.h"
#include "client/mysqltest/expected_warnings.h"
#include "client/mysqltest/logfile.h"
#include "client/mysqltest/regular_expressions.h"
#include "client/mysqltest/secondary_engine.h"
#include "client/mysqltest/utils.h"
#include "compression.h"
#include <algorithm>
#include <chrono>
#include <cmath> // std::isinf
#include <limits>
#include <new>
#include <sstream>
#ifdef _WIN32
#include <thread> // std::thread
#endif
#include <assert.h>
#if defined MY_MSCRT_DEBUG || defined _WIN32
#include <crtdbg.h>
#endif
#ifdef _WIN32
#include <direct.h>
#endif
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <mysql_async.h>
#include <mysql_version.h>
#include <mysqld_error.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#ifndef _WIN32
#include <poll.h>
#include <sys/time.h>
#include <sys/wait.h>
#endif
#ifdef _WIN32
#include <windows.h>
#endif
#include "caching_sha2_passwordopt-vars.h"
#include "client/client_priv.h"
#include "m_ctype.h"
#include "map_helpers.h"
#include "mf_wcomp.h" // wild_compare
#include "my_compiler.h"
#include "my_config.h"
#include "my_dbug.h"
#include "my_default.h"
#include "my_dir.h"
#include "my_inttypes.h"
#include "my_macros.h"
#include "my_openssl_fips.h"
#include "my_pointer_arithmetic.h"
#include "my_stacktrace.h"
#include "my_systime.h" // my_sleep()
#include "my_thread_local.h"
#include "prealloced_array.h"
#include "print_version.h"
#include "sql_common.h"
#include "template_utils.h"
#include "typelib.h"
#include "violite.h"
#include "welcome_copyright_notice.h" // ORACLE_WELCOME_COPYRIGHT_NOTICE
#ifdef _WIN32
#define SIGNAL_FMT "exception 0x%x"
#else
#define SIGNAL_FMT "signal %d"
#endif
#ifdef _WIN32
#define setenv(a, b, c) _putenv_s(a, b)
#define popen _popen
#define pclose _pclose
#endif
#define MAX_VAR_NAME_LENGTH 256
#define MAX_COLUMNS 256
#define MAX_DELIMITER_LENGTH 16
#define DEFAULT_MAX_CONN 512
#define REPLACE_ROUND_MAX 16
/* Flags controlling send and reap */
#define QUERY_SEND_FLAG 1
#define QUERY_REAP_FLAG 2
#define APPEND_TYPE(type) \
{ \
dynstr_append(ds, "-- "); \
switch (type) { \
case SESSION_TRACK_SYSTEM_VARIABLES: \
dynstr_append(ds, "Tracker : SESSION_TRACK_SYSTEM_VARIABLES\n"); \
break; \
case SESSION_TRACK_SCHEMA: \
dynstr_append(ds, "Tracker : SESSION_TRACK_SCHEMA\n"); \
break; \
case SESSION_TRACK_STATE_CHANGE: \
dynstr_append(ds, "Tracker : SESSION_TRACK_STATE_CHANGE\n"); \
break; \
case SESSION_TRACK_GTIDS: \
dynstr_append(ds, "Tracker : SESSION_TRACK_GTIDS\n"); \
break; \
case SESSION_TRACK_TRANSACTION_CHARACTERISTICS: \
dynstr_append( \
ds, "Tracker : SESSION_TRACK_TRANSACTION_CHARACTERISTICS\n"); \
break; \
case SESSION_TRACK_TRANSACTION_STATE: \
dynstr_append(ds, "Tracker : SESSION_TRACK_TRANSACTION_STATE\n"); \
break; \
default: \
dynstr_append(ds, "\n"); \
} \
}
extern CHARSET_INFO my_charset_utf16le_bin;
// List of error codes specified with 'error' command.
Expected_errors *expected_errors = new Expected_errors();
// List of warnings disabled with 'disable_warnings' command.
Expected_warnings *disabled_warnings = new Expected_warnings();
// List of warnings enabled with 'enable_warnings' command.
Expected_warnings *enabled_warnings = new Expected_warnings();
enum {
OPT_COLORED_DIFF = OPT_MAX_CLIENT_OPTION,
OPT_CURSOR_PROTOCOL,
OPT_EXPLAIN_PROTOCOL,
OPT_HYPERGRAPH,
OPT_JSON_EXPLAIN_PROTOCOL,
OPT_LOG_DIR,
OPT_MARK_PROGRESS,
OPT_MAX_CONNECT_RETRIES,
OPT_MAX_CONNECTIONS,
OPT_NO_SKIP,
OPT_OFFLOAD_COUNT_FILE,
OPT_PS_PROTOCOL,
OPT_RESULT_FORMAT_VERSION,
#ifdef _WIN32
OPT_SAFEPROCESS_PID,
#endif
OPT_SP_PROTOCOL,
OPT_TAIL_LINES,
OPT_TRACE_EXEC,
OPT_TRACE_PROTOCOL,
OPT_VIEW_PROTOCOL,
};
static int record = 0;
static char *opt_db = nullptr, *opt_pass = nullptr;
const char *opt_user = nullptr, *opt_host = nullptr, *unix_sock = nullptr,
*opt_basedir = "./";
const char *excluded_string = nullptr;
static char *shared_memory_base_name = nullptr;
const char *opt_logdir = "";
const char *opt_include = nullptr, *opt_charsets_dir;
static int opt_port = 0;
static int opt_max_connect_retries;
static int opt_result_format_version;
static int opt_max_connections = DEFAULT_MAX_CONN;
static bool backtick_lhs = false;
static char *opt_init_command = nullptr;
static bool opt_colored_diff = false;
static bool opt_compress = false, silent = false, verbose = false,
trace_exec = false;
static bool debug_info_flag = false, debug_check_flag = false;
static bool tty_password = false;
static bool opt_mark_progress = false;
static bool ps_protocol = false, ps_protocol_enabled = false;
static bool sp_protocol = false, sp_protocol_enabled = false;
static bool no_skip = false;
static bool skip_ignored = false;
static bool view_protocol = false, view_protocol_enabled = false;
static bool opt_trace_protocol = false, opt_trace_protocol_enabled = false;
static bool explain_protocol = false, explain_protocol_enabled = false;
static bool json_explain_protocol = false,
json_explain_protocol_enabled = false;
static bool cursor_protocol = false, cursor_protocol_enabled = false;
static bool testcase_disabled = false;
static bool display_result_vertically = false, display_result_lower = false,
display_metadata = false, display_result_sorted = false,
display_session_track_info = false;
static bool skip_if_hypergraph = false;
static int start_sort_column = 0;
static bool disable_query_log = false, disable_result_log = false;
static bool disable_connect_log = true;
static bool disable_warnings = false;
static bool disable_info = true;
static bool abort_on_error = true;
static bool server_initialized = false;
static bool is_windows = false;
static MEM_ROOT argv_alloc{PSI_NOT_INSTRUMENTED, 512};
static const char *load_default_groups[] = {"mysqltest", "client", nullptr};
static char line_buffer[MAX_DELIMITER_LENGTH], *line_buffer_pos = line_buffer;
static bool can_handle_expired_passwords = true;
static bool opt_hypergraph = false;
/*
These variables control the behavior of the asynchronous operations for
mysqltest client. If --async-client is specified, use_async_client is true.
Each command checks enable_async_client (which can be forced off or
disabled for a single command) to decide the mode it uses to run.
*/
static bool use_async_client = false;
static bool enable_async_client = false;
// Secondary engine options
static const char *opt_offload_count_file;
static Secondary_engine *secondary_engine = nullptr;
static uint opt_zstd_compress_level = default_zstd_compression_level;
static char *opt_compress_algorithm = nullptr;
static uint opt_test_ssl_fips_mode = 0;
#ifdef _WIN32
static DWORD opt_safe_process_pid;
static HANDLE mysqltest_thread;
// Event handle for stacktrace request event
static HANDLE stacktrace_request_event = nullptr;
static std::thread wait_for_stacktrace_request_event_thread;
#endif
Logfile log_file;
// File to store the progress
Logfile progress_file;
/// Info on properties that can be set with '--disable_X' and
/// '--disable_X' commands.
struct Property {
bool *var; // Actual variable
bool set; // Has been set for ONCE command
bool old; // If set, thus is the old value
bool reverse; // Variable is true if disabled
const char *env_name; // Environment variable name
};
static struct Property prop_list[] = {
{&abort_on_error, false, true, false, "$ENABLE_ABORT_ON_ERROR"},
{&disable_connect_log, false, true, true, "$ENABLE_CONNECT_LOG"},
{&disable_info, false, true, true, "$ENABLE_INFO"},
{&display_session_track_info, false, true, true,
"$ENABLE_STATE_CHANGE_INFO"},
{&display_metadata, false, false, false, "$ENABLE_METADATA"},
{&ps_protocol_enabled, false, false, false, "$ENABLE_PS_PROTOCOL"},
{&disable_query_log, false, false, true, "$ENABLE_QUERY_LOG"},
{&disable_result_log, false, false, true, "$ENABLE_RESULT_LOG"},
{&disable_warnings, false, false, true, "$ENABLE_WARNINGS"},
{&enable_async_client, false, false, false, "$ENABLE_ASYNC_CLIENT"}};
static bool once_property = false;
enum enum_prop {
P_ABORT = 0,
P_CONNECT,
P_INFO,
P_SESSION_TRACK,
P_META,
P_PS,
P_QUERY,
P_RESULT,
P_WARN,
P_ASYNC,
P_MAX
};
static uint start_lineno = 0; /* Start line of current command */
static uint my_end_arg = 0;
/* Number of lines of the result to include in failure report */
static uint opt_tail_lines = 0;
static uint opt_connect_timeout = 0;
static char delimiter[MAX_DELIMITER_LENGTH] = ";";
static size_t delimiter_length = 1;
static char TMPDIR[FN_REFLEN];
/* Block stack */
enum block_cmd { cmd_none, cmd_if, cmd_while, cmd_assert };
struct st_block {
int line; /* Start line of block */
bool ok; /* Should block be executed */
enum block_cmd cmd; /* Command owning the block */
char delim[MAX_DELIMITER_LENGTH]; /* Delimiter before block */
};
static struct st_block block_stack[32];
static struct st_block *cur_block, *block_stack_end;
/* Open file stack */
struct st_test_file {
FILE *file;
char *file_name;
uint lineno; /* Current line in file */
};
static struct st_test_file file_stack[16];
static struct st_test_file *cur_file;
static struct st_test_file *file_stack_end;
static const char *default_charset = MYSQL_DEFAULT_CHARSET_NAME;
CHARSET_INFO *charset_info =
&my_charset_utf8mb4_0900_ai_ci; /* Default charset */
/*
Timer related variables
See the timer_output() definition for details
*/
static char *timer_file = nullptr;
static ulonglong timer_start;
static void timer_output(void);
static ulonglong timer_now(void);
static ulong connection_retry_sleep = 100000; /* Microseconds */
static char *opt_plugin_dir = nullptr;
/* To retrieve a filename from a filepath */
const char *get_filename_from_path(const char *path) {
const char *fname = nullptr;
if (is_windows)
fname = strrchr(path, '\\');
else
fname = strrchr(path, '/');
if (fname == nullptr)
return path;
else
return ++fname;
}
static uint opt_protocol = 0;
#if defined(_WIN32)
static uint opt_protocol_for_default_connection = MYSQL_PROTOCOL_PIPE;
#endif
struct st_command;
typedef Prealloced_array<st_command *, 1024> Q_lines;
Q_lines *q_lines;
#include "sslopt-vars.h"
struct Parser {
int read_lines, current_line;
} parser;
struct MasterPos {
char file[FN_REFLEN];
ulong pos;
} master_pos;
/* if set, all results are concated and compared against this file */
const char *result_file_name = nullptr;
typedef struct {
char *name;
size_t name_len;
char *str_val;
size_t str_val_len;
int int_val;
size_t alloced_len;
bool int_dirty; /* do not update string if int is updated until first read */
bool is_int;
bool alloced;
} VAR;
/*Perl/shell-like variable registers */
VAR var_reg[10];
struct var_free {
void operator()(VAR *var) const;
};
collation_unordered_map<std::string, std::unique_ptr<VAR, var_free>> *var_hash;
struct st_connection {
MYSQL mysql;
/* Used when creating views and sp, to avoid implicit commit */
MYSQL *util_mysql;
char *name;
size_t name_len;
MYSQL_STMT *stmt;
/* Set after send to disallow other queries before reap */
bool pending;
};
struct st_connection *connections = nullptr;
struct st_connection *cur_con = nullptr, *next_con, *connections_end;
/*
List of commands in mysqltest
Must match the "command_names" array
Add new commands before Q_UNKNOWN!
*/
enum enum_commands {
Q_CONNECTION = 1,
Q_QUERY,
Q_CONNECT,
Q_SLEEP,
Q_INC,
Q_DEC,
Q_SOURCE,
Q_DISCONNECT,
Q_LET,
Q_ECHO,
Q_EXPR,
Q_WHILE,
Q_END_BLOCK,
Q_SAVE_MASTER_POS,
Q_SYNC_WITH_MASTER,
Q_SYNC_SLAVE_WITH_MASTER,
Q_ERROR,
Q_SEND,
Q_REAP,
Q_DIRTY_CLOSE,
Q_REPLACE,
Q_REPLACE_COLUMN,
Q_PING,
Q_EVAL,
Q_ENABLE_QUERY_LOG,
Q_DISABLE_QUERY_LOG,
Q_ENABLE_RESULT_LOG,
Q_DISABLE_RESULT_LOG,
Q_ENABLE_CONNECT_LOG,
Q_DISABLE_CONNECT_LOG,
Q_WAIT_FOR_SLAVE_TO_STOP,
Q_ENABLE_WARNINGS,
Q_DISABLE_WARNINGS,
Q_ENABLE_INFO,
Q_DISABLE_INFO,
Q_ENABLE_SESSION_TRACK_INFO,
Q_DISABLE_SESSION_TRACK_INFO,
Q_ENABLE_METADATA,
Q_DISABLE_METADATA,
Q_ENABLE_ASYNC_CLIENT,
Q_DISABLE_ASYNC_CLIENT,
Q_EXEC,
Q_EXECW,
Q_EXEC_BACKGROUND,
Q_DELIMITER,
Q_DISABLE_ABORT_ON_ERROR,
Q_ENABLE_ABORT_ON_ERROR,
Q_DISPLAY_VERTICAL_RESULTS,
Q_DISPLAY_HORIZONTAL_RESULTS,
Q_QUERY_VERTICAL,
Q_QUERY_HORIZONTAL,
Q_SORTED_RESULT,
Q_PARTIALLY_SORTED_RESULT,
Q_LOWERCASE,
Q_SKIP_IF_HYPERGRAPH,
Q_START_TIMER,
Q_END_TIMER,
Q_CHARACTER_SET,
Q_DISABLE_PS_PROTOCOL,
Q_ENABLE_PS_PROTOCOL,
Q_DISABLE_RECONNECT,
Q_ENABLE_RECONNECT,
Q_IF,
Q_DISABLE_TESTCASE,
Q_ENABLE_TESTCASE,
Q_REPLACE_REGEX,
Q_REPLACE_NUMERIC_ROUND,
Q_REMOVE_FILE,
Q_FILE_EXIST,
Q_WRITE_FILE,
Q_COPY_FILE,
Q_PERL,
Q_DIE,
Q_ASSERT,
Q_EXIT,
Q_SKIP,
Q_CHMOD_FILE,
Q_APPEND_FILE,
Q_CAT_FILE,
Q_DIFF_FILES,
Q_SEND_QUIT,
Q_CHANGE_USER,
Q_MKDIR,
Q_RMDIR,
Q_FORCE_RMDIR,
Q_FORCE_CPDIR,
Q_LIST_FILES,
Q_LIST_FILES_WRITE_FILE,
Q_LIST_FILES_APPEND_FILE,
Q_SEND_SHUTDOWN,
Q_SHUTDOWN_SERVER,
Q_RESULT_FORMAT_VERSION,
Q_MOVE_FILE,
Q_REMOVE_FILES_WILDCARD,
Q_COPY_FILES_WILDCARD,
Q_SEND_EVAL,
Q_OUTPUT, /* redirect output to a file */
Q_RESET_CONNECTION,
Q_QUERY_ATTRIBUTES,
Q_UNKNOWN, /* Unknown command. */
Q_COMMENT, /* Comments, ignored. */
Q_COMMENT_WITH_COMMAND,
Q_EMPTY_LINE
};
const char *command_names[] = {
"connection", "query", "connect", "sleep", "inc", "dec", "source",
"disconnect", "let", "echo", "expr", "while", "end", "save_master_pos",
"sync_with_master", "sync_slave_with_master", "error", "send", "reap",
"dirty_close", "replace_result", "replace_column", "ping", "eval",
/* Enable/disable that the _query_ is logged to result file */
"enable_query_log", "disable_query_log",
/* Enable/disable that the _result_ from a query is logged to result file */
"enable_result_log", "disable_result_log", "enable_connect_log",
"disable_connect_log", "wait_for_slave_to_stop", "enable_warnings",
"disable_warnings", "enable_info", "disable_info",
"enable_session_track_info", "disable_session_track_info",
"enable_metadata", "disable_metadata", "enable_async_client",
"disable_async_client", "exec", "execw", "exec_in_background", "delimiter",
"disable_abort_on_error", "enable_abort_on_error", "vertical_results",
"horizontal_results", "query_vertical", "query_horizontal", "sorted_result",
"partially_sorted_result", "lowercase_result", "skip_if_hypergraph",
"start_timer", "end_timer", "character_set", "disable_ps_protocol",
"enable_ps_protocol", "disable_reconnect", "enable_reconnect", "if",
"disable_testcase", "enable_testcase", "replace_regex",
"replace_numeric_round", "remove_file", "file_exists", "write_file",
"copy_file", "perl", "die", "assert",
/* Don't execute any more commands, compare result */
"exit", "skip", "chmod", "append_file", "cat_file", "diff_files",
"send_quit", "change_user", "mkdir", "rmdir", "force-rmdir", "force-cpdir",
"list_files", "list_files_write_file", "list_files_append_file",
"send_shutdown", "shutdown_server", "result_format", "move_file",
"remove_files_wildcard", "copy_files_wildcard", "send_eval", "output",
"reset_connection", "query_attributes",
nullptr};
struct st_command {
char *query, *query_buf, *first_argument, *last_argument, *end;
DYNAMIC_STRING content;
size_t first_word_len, query_len;
bool abort_on_error, used_replace;
char output_file[FN_REFLEN];
enum enum_commands type;
// Line number of the command
uint lineno;
};
TYPELIB command_typelib = {array_elements(command_names), "", command_names,
nullptr};
DYNAMIC_STRING ds_res;
DYNAMIC_STRING ds_result;
/* Points to ds_warning in run_query, so it can be freed */
DYNAMIC_STRING *ds_warn = nullptr;
struct st_command *curr_command = nullptr;
char builtin_echo[FN_REFLEN];
/* Stores regex substitutions */
struct st_replace_regex *glob_replace_regex = nullptr;
struct REPLACE;
REPLACE *glob_replace = nullptr;
void replace_strings_append(REPLACE *rep, DYNAMIC_STRING *ds, const char *from,
size_t len);
[[noreturn]] static void cleanup_and_exit(int exit_code);
[[noreturn]] void die(const char *fmt, ...)
MY_ATTRIBUTE((format(printf, 1, 2)));
[[noreturn]] void abort_not_supported_test(const char *fmt, ...)
MY_ATTRIBUTE((format(printf, 1, 2)));
void verbose_msg(const char *fmt, ...) MY_ATTRIBUTE((format(printf, 1, 2)));
void log_msg(const char *fmt, ...) MY_ATTRIBUTE((format(printf, 1, 2)));
void flush_ds_res();
VAR *var_from_env(const char *, const char *);
VAR *var_init(VAR *v, const char *name, size_t name_len, const char *val,
size_t val_len);
VAR *var_get(const char *var_name, const char **var_name_end, bool raw,
bool ignore_not_existing);
void eval_expr(VAR *v, const char *p, const char **p_end, bool open_end = false,
bool do_eval = true);
bool match_delimiter(int c, const char *delim, size_t length);
void do_eval(DYNAMIC_STRING *query_eval, const char *query,
const char *query_end, bool pass_through_escape_chars);
void str_to_file(const char *fname, char *str, size_t size);
void str_to_file2(const char *fname, char *str, size_t size, bool append);
void fix_win_paths(const char *val, size_t len);
#ifdef _WIN32
void free_win_path_patterns();
#endif
/* For replace_column */
static char *replace_column[MAX_COLUMNS];
static uint max_replace_column = 0;
void do_get_replace_column(struct st_command *);
void free_replace_column();
static void do_query_attributes(struct st_command *command);
/* For replace */
void do_get_replace(struct st_command *command);
void free_replace();
/* For replace_regex */
void do_get_replace_regex(struct st_command *command);
void free_replace_regex();
/* For replace numeric round */
static int glob_replace_numeric_round = -1;
void do_get_replace_numeric_round(struct st_command *command);
void free_replace_numeric_round();
void replace_numeric_round_append(int round, DYNAMIC_STRING *ds,
const char *from, size_t len);
/* Used by sleep */
void check_eol_junk_line(const char *eol);
static void var_set(const char *var_name, const char *var_name_end,
const char *var_val, const char *var_val_end);
static void free_all_replace() {
free_replace();
free_replace_regex();
free_replace_column();
free_replace_numeric_round();
global_attrs->clear();
}
/*
To run tests via the async API, invoke mysqltest with --async-client.
*/
class AsyncTimer {
public:
explicit AsyncTimer(std::string label)
: label_(label), time_(std::chrono::system_clock::now()), start_(time_) {}
~AsyncTimer() {
auto now = std::chrono::system_clock::now();
auto delta = now - start_;
[[maybe_unused]] ulonglong micros =
std::chrono::duration_cast<std::chrono::microseconds>(delta).count();
DBUG_PRINT("async_timing",
("%s total micros: %llu", label_.c_str(), micros));
}
void check() {
auto now = std::chrono::system_clock::now();
auto delta = now - time_;
time_ = now;
[[maybe_unused]] ulonglong micros =
std::chrono::duration_cast<std::chrono::microseconds>(delta).count();
DBUG_PRINT("async_timing", ("%s op micros: %llu", label_.c_str(), micros));
}
private:
std::string label_;
std::chrono::system_clock::time_point time_;
std::chrono::system_clock::time_point start_;
};
#ifdef _WIN32
/*
Check if any data is available in the socket to be read or written.
*/
static int socket_event_listen(my_socket fd) {
int result;
fd_set readfds, writefds, exceptfds;
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_ZERO(&exceptfds);
FD_SET(fd, &exceptfds);
FD_SET(fd, &readfds);
FD_SET(fd, &writefds);
result = select((int)(fd + 1), &readfds, &writefds, &exceptfds, nullptr);
if (result < 0) {
DWORD error_code = WSAGetLastError();
verbose_msg("Cannot determine the status due to error :%lu\n", error_code);
}
return result;
}
#else
static int socket_event_listen(my_socket fd) {
int result;
pollfd pfd;
pfd.fd = fd;
/*
Listen to both in/out because SSL can perform reads during writes (and
vice versa).
*/
pfd.events = POLLIN | POLLOUT;
result = poll(&pfd, 1, -1);
if (result < 0) {
perror("poll");
}
return result;
}
#endif
/*
Below async_mysql_*_wrapper functions are used to measure how much time
each nonblocking call spends before completing the operations.
i*/
static MYSQL_ROW async_mysql_fetch_row_wrapper(MYSQL_RES *res) {
MYSQL_ROW row;
MYSQL *mysql = res->handle;
AsyncTimer t(__func__);
while (mysql_fetch_row_nonblocking(res, &row) == NET_ASYNC_NOT_READY) {
t.check();
int result = socket_event_listen(mysql_get_socket_descriptor(mysql));
if (result == -1) return nullptr;
}
return row;
}
static MYSQL_RES *async_mysql_store_result_wrapper(MYSQL *mysql) {
MYSQL_RES *mysql_result;
AsyncTimer t(__func__);
while (mysql_store_result_nonblocking(mysql, &mysql_result) ==
NET_ASYNC_NOT_READY) {
t.check();
int result = socket_event_listen(mysql_get_socket_descriptor(mysql));
if (result == -1) return nullptr;
}
return mysql_result;
}
static int async_mysql_real_query_wrapper(MYSQL *mysql, const char *query,
ulong length) {
net_async_status status;
AsyncTimer t(__func__);
while ((status = mysql_real_query_nonblocking(mysql, query, length)) ==
NET_ASYNC_NOT_READY) {
t.check();
int result = socket_event_listen(mysql_get_socket_descriptor(mysql));
if (result == -1) return 1;
}
if (status == NET_ASYNC_ERROR) {
return 1;
}
return 0;
}
static int async_mysql_send_query_wrapper(MYSQL *mysql, const char *query,
ulong length) {
net_async_status status;
AsyncTimer t(__func__);
while ((status = mysql_send_query_nonblocking(mysql, query, length)) ==
NET_ASYNC_NOT_READY) {
t.check();
int result = socket_event_listen(mysql_get_socket_descriptor(mysql));
if (result == -1) return 1;
}
if (status == NET_ASYNC_ERROR) {
return 1;
}
return 0;
}
static bool async_mysql_read_query_result_wrapper(MYSQL *mysql) {
net_async_status status;
AsyncTimer t(__func__);
while ((status = (*mysql->methods->read_query_result_nonblocking)(mysql)) ==
NET_ASYNC_NOT_READY) {
t.check();
int result = socket_event_listen(mysql_get_socket_descriptor(mysql));
if (result == -1) return true;
}
if (status == NET_ASYNC_ERROR) {
return true;
}
return false;
}
static int async_mysql_next_result_wrapper(MYSQL *mysql) {
net_async_status status;
AsyncTimer t(__func__);
while ((status = mysql_next_result_nonblocking(mysql)) ==
NET_ASYNC_NOT_READY) {
t.check();
int result = socket_event_listen(mysql_get_socket_descriptor(mysql));
if (result == -1) return 1;
}
if (status == NET_ASYNC_ERROR)
return 1;
else if (status == NET_ASYNC_COMPLETE_NO_MORE_RESULTS)
return -1;
else
return 0;
}
static MYSQL *async_mysql_real_connect_wrapper(
MYSQL *mysql, const char *host, const char *user, const char *passwd,
const char *db, uint port, const char *unix_socket, ulong client_flag) {
net_async_status status;
AsyncTimer t(__func__);
while ((status = mysql_real_connect_nonblocking(
mysql, host, user, passwd, db, port, unix_socket, client_flag)) ==
NET_ASYNC_NOT_READY) {
t.check();
}
if (status == NET_ASYNC_ERROR)
return nullptr;
else
return mysql;
}
static int async_mysql_query_wrapper(MYSQL *mysql, const char *query) {
net_async_status status;
AsyncTimer t(__func__);
while ((status = mysql_real_query_nonblocking(mysql, query, strlen(query))) ==
NET_ASYNC_NOT_READY) {
t.check();
int result = socket_event_listen(mysql_get_socket_descriptor(mysql));
if (result == -1) return 1;
}
if (status == NET_ASYNC_ERROR) {
return 1;
}
return 0;
}
static void async_mysql_free_result_wrapper(MYSQL_RES *result) {
AsyncTimer t(__func__);
while (mysql_free_result_nonblocking(result) == NET_ASYNC_NOT_READY) {
t.check();
MYSQL *mysql = result->handle;
int listen_result = socket_event_listen(mysql_get_socket_descriptor(mysql));
if (listen_result == -1) return;
}
return;
}
/*
Below are the wrapper functions which are defined on top of standard C APIs
to make a decision on whether to call blocking or non blocking API based on
--async-client option is set or not.
*/
static MYSQL_ROW mysql_fetch_row_wrapper(MYSQL_RES *res) {
if (enable_async_client)
return async_mysql_fetch_row_wrapper(res);
else
return mysql_fetch_row(res);
}
static MYSQL_RES *mysql_store_result_wrapper(MYSQL *mysql) {
if (enable_async_client)
return async_mysql_store_result_wrapper(mysql);
else
return mysql_store_result(mysql);
}
static int mysql_real_query_wrapper(MYSQL *mysql, const char *query,
ulong length) {
int rc;
if (0 != (rc = global_attrs->set_params(mysql))) return rc;
if (enable_async_client)
return async_mysql_real_query_wrapper(mysql, query, length);
else
return mysql_real_query(mysql, query, length);
}
static int mysql_send_query_wrapper(MYSQL *mysql, const char *query,
ulong length) {
int rc;
if (0 != (rc = global_attrs->set_params(mysql))) return rc;
if (enable_async_client)
return async_mysql_send_query_wrapper(mysql, query, length);
else
return mysql_send_query(mysql, query, length);
}
static bool mysql_read_query_result_wrapper(MYSQL *mysql) {
bool ret;
if (enable_async_client)
ret = async_mysql_read_query_result_wrapper(mysql);
else
ret = mysql_read_query_result(mysql);
return ret;
}
static int mysql_query_wrapper(MYSQL *mysql, const char *query) {
int rc;
if (0 != (rc = global_attrs->set_params(mysql))) return rc;
if (enable_async_client)
return async_mysql_query_wrapper(mysql, query);
else
return mysql_query(mysql, query);
}
static int mysql_next_result_wrapper(MYSQL *mysql) {
if (enable_async_client)
return async_mysql_next_result_wrapper(mysql);
else
return mysql_next_result(mysql);
}
static MYSQL *mysql_real_connect_wrapper(MYSQL *mysql, const char *host,
const char *user, const char *passwd,
const char *db, uint port,
const char *unix_socket,
ulong client_flag) {
if (enable_async_client)
return async_mysql_real_connect_wrapper(mysql, host, user, passwd, db, port,
unix_socket, client_flag);
else
return mysql_real_connect(mysql, host, user, passwd, db, port, unix_socket,
client_flag);
}
static void mysql_free_result_wrapper(MYSQL_RES *result) {
if (enable_async_client)
return async_mysql_free_result_wrapper(result);
else
return mysql_free_result(result);
}
/* async client test code (end) */
void replace_dynstr_append_mem(DYNAMIC_STRING *ds, const char *val, size_t len);
void replace_dynstr_append(DYNAMIC_STRING *ds, const char *val);
void replace_dynstr_append_uint(DYNAMIC_STRING *ds, uint val);
void dynstr_append_sorted(DYNAMIC_STRING *ds, DYNAMIC_STRING *ds_input,
int start_sort_column);
void revert_properties();
void do_eval(DYNAMIC_STRING *query_eval, const char *query,
const char *query_end, bool pass_through_escape_chars) {
const char *p;
char c, next_c;
int escaped = 0;
VAR *v;
DBUG_TRACE;
for (p = query; (c = *p) && p < query_end; ++p) {
next_c = *(p + 1);
switch (c) {
case '$':
if (escaped ||
// a JSON path expression
next_c == '.' || next_c == '[' || next_c == '\'' || next_c == '"') {
escaped = 0;
dynstr_append_mem(query_eval, p, 1);
} else {
if (!(v = var_get(p, &p, false, false))) die("Bad variable in eval");
dynstr_append_mem(query_eval, v->str_val, v->str_val_len);
}
break;