-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
log_event.cc
14194 lines (12425 loc) · 496 KB
/
log_event.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
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, 2021, 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, 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 */
#define LOG_SUBSYSTEM_TAG "Repl"
#include "sql/log_event.h"
#include "my_config.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <algorithm>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include "base64.h"
#include "decimal.h"
#include "libbinlogevents/export/binary_log_funcs.h" // my_timestamp_binary_length
#include "libbinlogevents/include/debug_vars.h"
#include "libbinlogevents/include/table_id.h"
#include "libbinlogevents/include/wrapper_functions.h"
#include "m_ctype.h"
#include "my_bitmap.h"
#include "my_byteorder.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_io.h"
#include "my_loglevel.h"
#include "my_macros.h"
#include "my_systime.h"
#include "my_table_map.h"
#include "my_time.h" // MAX_DATE_STRING_REP_LENGTH
#include "mysql.h" // MYSQL_OPT_MAX_ALLOWED_PACKET
#include "mysql/components/services/log_builtins.h"
#include "mysql/components/services/log_shared.h"
#include "mysql/components/services/psi_statement_bits.h"
#include "mysql/psi/mysql_mutex.h"
#include "mysql/udf_registration_types.h"
#include "mysql_time.h"
#include "psi_memory_key.h"
#include "query_options.h"
#include "sql/auth/auth_acls.h"
#include "sql/binlog_reader.h"
#include "sql/field_common_properties.h"
#include "sql/my_decimal.h" // my_decimal
#include "sql/rpl_handler.h" // RUN_HOOK
#include "sql/rpl_tblmap.h"
#include "sql/sql_show_processlist.h" // pfs_processlist_enabled
#include "sql/system_variables.h"
#include "sql/tc_log.h"
#include "sql_const.h"
#include "sql_string.h"
#include "template_utils.h"
#ifndef MYSQL_SERVER
#include "client/mysqlbinlog.h"
#include "sql/json_binary.h"
#include "sql/json_diff.h" // enum_json_diff_operation
#include "sql/json_dom.h" // Json_wrapper
#endif
#ifdef MYSQL_SERVER
#include <errno.h>
#include <fcntl.h>
#include <cstdint>
#include <new>
#include "libbinlogevents/include/binary_log.h" // binary_log
#include "my_base.h"
#include "my_command.h"
#include "my_dir.h" // my_dir
#include "my_sqlcommand.h"
#include "mysql/plugin.h"
#include "mysql/psi/mysql_cond.h"
#include "mysql/psi/mysql_file.h"
#include "mysql/psi/mysql_stage.h"
#include "mysql/psi/mysql_statement.h"
#include "mysql/psi/mysql_transaction.h"
#include "mysql/psi/psi_statement.h"
#include "mysqld_error.h"
#include "prealloced_array.h"
#include "sql/auth/auth_common.h"
#include "sql/auth/sql_security_ctx.h"
#include "sql/basic_ostream.h"
#include "sql/binlog.h"
#include "sql/current_thd.h"
#include "sql/dd/types/abstract_table.h" // dd::enum_table_type
#include "sql/debug_sync.h" // debug_sync_set_action
#include "sql/derror.h" // ER_THD
#include "sql/enum_query_type.h"
#include "sql/field.h"
#include "sql/handler.h"
#include "sql/item.h"
#include "sql/item_func.h" // Item_func_set_user_var
#include "sql/key.h"
#include "sql/log.h" // Log_throttle
#include "sql/mdl.h"
#include "sql/mysqld.h" // lower_case_table_names server_uuid ...
#include "sql/protocol.h"
#include "sql/rpl_msr.h" // channel_map
#include "sql/rpl_mta_submode.h" // Mts_submode
#include "sql/rpl_replica.h" // use_slave_mask
#include "sql/rpl_reporting.h"
#include "sql/rpl_rli.h" // Relay_log_info
#include "sql/rpl_rli_pdb.h" // Slave_job_group
#include "sql/sp_head.h" // sp_name
#include "sql/sql_base.h" // close_thread_tables
#include "sql/sql_bitmap.h"
#include "sql/sql_class.h"
#include "sql/sql_cmd.h"
#include "sql/sql_data_change.h"
#include "sql/sql_db.h" // load_db_opt_by_name
#include "sql/sql_digest_stream.h"
#include "sql/sql_error.h"
#include "sql/sql_exchange.h" // sql_exchange
#include "sql/sql_lex.h"
#include "sql/sql_list.h" // I_List
#include "sql/sql_load.h" // Sql_cmd_load_table
#include "sql/sql_locale.h" // my_locale_by_number
#include "sql/sql_parse.h" // mysql_test_parse_for_slave
#include "sql/sql_plugin.h" // plugin_foreach
#include "sql/sql_show.h" // append_identifier
#include "sql/sql_tablespace.h" // Sql_cmd_tablespace
#include "sql/table.h"
#include "sql/transaction.h" // trans_rollback_stmt
#include "sql/transaction_info.h"
#include "sql/tztime.h" // Time_zone
#include "thr_lock.h"
#define window_size Log_throttle::LOG_THROTTLE_WINDOW_SIZE
Error_log_throttle slave_ignored_err_throttle(
window_size, INFORMATION_LEVEL, ER_SERVER_SLAVE_IGNORED_TABLE, "Repl",
"Error log throttle: %lu time(s) Error_code: 1237"
" \"Slave SQL thread ignored the query because of"
" replicate-*-table rules\" got suppressed.");
#endif /* MYSQL_SERVER */
#include "libbinlogevents/include/codecs/binary.h"
#include "libbinlogevents/include/codecs/factory.h"
#include "libbinlogevents/include/compression/iterator.h"
#include "sql/rpl_gtid.h"
#include "sql/rpl_record.h" // enum_row_image_type, Bit_reader
#include "sql/rpl_utility.h"
#include "sql/xa_aux.h"
struct mysql_mutex_t;
PSI_memory_key key_memory_log_event;
PSI_memory_key key_memory_Incident_log_event_message;
PSI_memory_key key_memory_Rows_query_log_event_rows_query;
using std::max;
using std::min;
/**
BINLOG_CHECKSUM variable.
*/
const char *binlog_checksum_type_names[] = {"NONE", "CRC32", NullS};
unsigned int binlog_checksum_type_length[] = {sizeof("NONE") - 1,
sizeof("CRC32") - 1, 0};
TYPELIB binlog_checksum_typelib = {
array_elements(binlog_checksum_type_names) - 1, "",
binlog_checksum_type_names, binlog_checksum_type_length};
#define log_cs &my_charset_latin1
/*
Size of buffer for printing a double in format %.<PREC>g
optional '-' + optional zero + '.' + PREC digits + 'e' + sign +
exponent digits + '\0'
*/
#define FMT_G_BUFSIZE(PREC) (3 + (PREC) + 5 + 1)
#if defined(MYSQL_SERVER)
static int rows_event_stmt_cleanup(Relay_log_info const *rli, THD *thd);
static const char *HA_ERR(int i) {
/*
This function should only be called in case of an error
was detected
*/
assert(i != 0);
switch (i) {
case HA_ERR_KEY_NOT_FOUND:
return "HA_ERR_KEY_NOT_FOUND";
case HA_ERR_FOUND_DUPP_KEY:
return "HA_ERR_FOUND_DUPP_KEY";
case HA_ERR_RECORD_CHANGED:
return "HA_ERR_RECORD_CHANGED";
case HA_ERR_WRONG_INDEX:
return "HA_ERR_WRONG_INDEX";
case HA_ERR_CRASHED:
return "HA_ERR_CRASHED";
case HA_ERR_WRONG_IN_RECORD:
return "HA_ERR_WRONG_IN_RECORD";
case HA_ERR_OUT_OF_MEM:
return "HA_ERR_OUT_OF_MEM";
case HA_ERR_NOT_A_TABLE:
return "HA_ERR_NOT_A_TABLE";
case HA_ERR_WRONG_COMMAND:
return "HA_ERR_WRONG_COMMAND";
case HA_ERR_OLD_FILE:
return "HA_ERR_OLD_FILE";
case HA_ERR_NO_ACTIVE_RECORD:
return "HA_ERR_NO_ACTIVE_RECORD";
case HA_ERR_RECORD_DELETED:
return "HA_ERR_RECORD_DELETED";
case HA_ERR_RECORD_FILE_FULL:
return "HA_ERR_RECORD_FILE_FULL";
case HA_ERR_INDEX_FILE_FULL:
return "HA_ERR_INDEX_FILE_FULL";
case HA_ERR_END_OF_FILE:
return "HA_ERR_END_OF_FILE";
case HA_ERR_UNSUPPORTED:
return "HA_ERR_UNSUPPORTED";
case HA_ERR_TOO_BIG_ROW:
return "HA_ERR_TOO_BIG_ROW";
case HA_WRONG_CREATE_OPTION:
return "HA_WRONG_CREATE_OPTION";
case HA_ERR_FOUND_DUPP_UNIQUE:
return "HA_ERR_FOUND_DUPP_UNIQUE";
case HA_ERR_UNKNOWN_CHARSET:
return "HA_ERR_UNKNOWN_CHARSET";
case HA_ERR_WRONG_MRG_TABLE_DEF:
return "HA_ERR_WRONG_MRG_TABLE_DEF";
case HA_ERR_CRASHED_ON_REPAIR:
return "HA_ERR_CRASHED_ON_REPAIR";
case HA_ERR_CRASHED_ON_USAGE:
return "HA_ERR_CRASHED_ON_USAGE";
case HA_ERR_LOCK_WAIT_TIMEOUT:
return "HA_ERR_LOCK_WAIT_TIMEOUT";
case HA_ERR_LOCK_TABLE_FULL:
return "HA_ERR_LOCK_TABLE_FULL";
case HA_ERR_READ_ONLY_TRANSACTION:
return "HA_ERR_READ_ONLY_TRANSACTION";
case HA_ERR_LOCK_DEADLOCK:
return "HA_ERR_LOCK_DEADLOCK";
case HA_ERR_CANNOT_ADD_FOREIGN:
return "HA_ERR_CANNOT_ADD_FOREIGN";
case HA_ERR_NO_REFERENCED_ROW:
return "HA_ERR_NO_REFERENCED_ROW";
case HA_ERR_ROW_IS_REFERENCED:
return "HA_ERR_ROW_IS_REFERENCED";
case HA_ERR_NO_SAVEPOINT:
return "HA_ERR_NO_SAVEPOINT";
case HA_ERR_NON_UNIQUE_BLOCK_SIZE:
return "HA_ERR_NON_UNIQUE_BLOCK_SIZE";
case HA_ERR_NO_SUCH_TABLE:
return "HA_ERR_NO_SUCH_TABLE";
case HA_ERR_TABLE_EXIST:
return "HA_ERR_TABLE_EXIST";
case HA_ERR_NO_CONNECTION:
return "HA_ERR_NO_CONNECTION";
case HA_ERR_NULL_IN_SPATIAL:
return "HA_ERR_NULL_IN_SPATIAL";
case HA_ERR_TABLE_DEF_CHANGED:
return "HA_ERR_TABLE_DEF_CHANGED";
case HA_ERR_NO_PARTITION_FOUND:
return "HA_ERR_NO_PARTITION_FOUND";
case HA_ERR_RBR_LOGGING_FAILED:
return "HA_ERR_RBR_LOGGING_FAILED";
case HA_ERR_DROP_INDEX_FK:
return "HA_ERR_DROP_INDEX_FK";
case HA_ERR_FOREIGN_DUPLICATE_KEY:
return "HA_ERR_FOREIGN_DUPLICATE_KEY";
case HA_ERR_TABLE_NEEDS_UPGRADE:
return "HA_ERR_TABLE_NEEDS_UPGRADE";
case HA_ERR_TABLE_READONLY:
return "HA_ERR_TABLE_READONLY";
case HA_ERR_AUTOINC_READ_FAILED:
return "HA_ERR_AUTOINC_READ_FAILED";
case HA_ERR_AUTOINC_ERANGE:
return "HA_ERR_AUTOINC_ERANGE";
case HA_ERR_GENERIC:
return "HA_ERR_GENERIC";
case HA_ERR_RECORD_IS_THE_SAME:
return "HA_ERR_RECORD_IS_THE_SAME";
case HA_ERR_LOGGING_IMPOSSIBLE:
return "HA_ERR_LOGGING_IMPOSSIBLE";
case HA_ERR_CORRUPT_EVENT:
return "HA_ERR_CORRUPT_EVENT";
case HA_ERR_ROWS_EVENT_APPLY:
return "HA_ERR_ROWS_EVENT_APPLY";
case HA_ERR_FK_DEPTH_EXCEEDED:
return "HA_ERR_FK_DEPTH_EXCEEDED";
case HA_ERR_INNODB_READ_ONLY:
return "HA_ERR_INNODB_READ_ONLY";
case HA_ERR_COMPUTE_FAILED:
return "HA_ERR_COMPUTE_FAILED";
case HA_ERR_NO_WAIT_LOCK:
return "HA_ERR_NO_WAIT_LOCK";
case HA_ERR_FTS_TOO_MANY_NESTED_EXP:
return "HA_ERR_FTS_TOO_MANY_NESTED_EXP";
}
return "No Error!";
}
/**
Error reporting facility for Rows_log_event::do_apply_event
@param level error, warning or info
@param ha_error HA_ERR_ code
@param rli pointer to the active Relay_log_info instance
@param thd pointer to the slave thread's thd
@param table pointer to the event's table object
@param type the type of the event
@param log_name the master binlog file name
@param pos the master binlog file pos (the next after the event)
*/
static void inline slave_rows_error_report(enum loglevel level, int ha_error,
Relay_log_info const *rli, THD *thd,
TABLE *table, const char *type,
const char *log_name, ulong pos) {
const char *handler_error = (ha_error ? HA_ERR(ha_error) : nullptr);
bool is_group_replication_applier_channel =
channel_map.is_group_replication_channel_name(
(const_cast<Relay_log_info *>(rli))->get_channel(), true);
char buff[MAX_SLAVE_ERRMSG], *slider;
const char *buff_end = buff + sizeof(buff);
size_t len;
Diagnostics_area::Sql_condition_iterator it =
thd->get_stmt_da()->sql_conditions();
const Sql_condition *err;
buff[0] = 0;
for (err = it++, slider = buff; err && slider < buff_end - 1;
slider += len, err = it++) {
len = snprintf(slider, buff_end - slider, " %s, Error_code: %d;",
err->message_text(), err->mysql_errno());
}
if (is_group_replication_applier_channel) {
if (ha_error != 0) {
rli->report(level,
thd->is_error() ? thd->get_stmt_da()->mysql_errno()
: ER_UNKNOWN_ERROR,
"Could not execute %s event on table %s.%s;"
"%s handler error %s",
type, table->s->db.str, table->s->table_name.str, buff,
handler_error == nullptr ? "<unknown>" : handler_error);
} else {
rli->report(level,
thd->is_error() ? thd->get_stmt_da()->mysql_errno()
: ER_UNKNOWN_ERROR,
"Could not execute %s event on table %s.%s;"
"%s",
type, table->s->db.str, table->s->table_name.str, buff);
}
} else {
if (ha_error != 0) {
rli->report(level,
thd->is_error() ? thd->get_stmt_da()->mysql_errno()
: ER_UNKNOWN_ERROR,
"Could not execute %s event on table %s.%s;"
"%s handler error %s; "
"the event's master log %s, end_log_pos %lu",
type, table->s->db.str, table->s->table_name.str, buff,
handler_error == nullptr ? "<unknown>" : handler_error,
log_name, pos);
} else {
rli->report(level,
thd->is_error() ? thd->get_stmt_da()->mysql_errno()
: ER_UNKNOWN_ERROR,
"Could not execute %s event on table %s.%s;"
"%s the event's master log %s, end_log_pos %lu",
type, table->s->db.str, table->s->table_name.str, buff,
log_name, pos);
}
}
}
/**
Set the rewritten database, or current database if it should not be
rewritten, into THD.
@param thd THD handle
@param db database name
@param db_len the length of database name
@retval true if the passed db is rewritten.
@retval false if the passed db is not rewritten.
*/
static bool set_thd_db(THD *thd, const char *db, size_t db_len) {
bool need_increase_counter = false;
char lcase_db_buf[NAME_LEN + 1];
LEX_CSTRING new_db;
new_db.length = db_len;
if (lower_case_table_names) {
my_stpcpy(lcase_db_buf, db);
my_casedn_str(system_charset_info, lcase_db_buf);
new_db.str = lcase_db_buf;
} else
new_db.str = db;
/* This function is called by a slave thread. */
assert(thd->rli_slave);
Rpl_filter *rpl_filter = thd->rli_slave->rpl_filter;
new_db.str = rpl_filter->get_rewrite_db(new_db.str, &new_db.length);
if (lower_case_table_names) {
/* lcase_db_buf != new_db.str means that lcase_db_buf is rewritten. */
if (strcmp(lcase_db_buf, new_db.str)) need_increase_counter = true;
} else {
/* db != new_db.str means that db is rewritten. */
if (strcmp(db, new_db.str)) need_increase_counter = true;
}
thd->set_db(new_db);
return need_increase_counter;
}
#endif
/*
pretty_print_str()
*/
#ifndef MYSQL_SERVER
static inline void pretty_print_str(IO_CACHE *cache, const char *str,
size_t len, bool identifier) {
const char *end = str + len;
my_b_printf(cache, identifier ? "`" : "\'");
while (str < end) {
char c;
switch ((c = *str++)) {
case '\n':
my_b_printf(cache, "\\n");
break;
case '\r':
my_b_printf(cache, "\\r");
break;
case '\\':
my_b_printf(cache, "\\\\");
break;
case '\b':
my_b_printf(cache, "\\b");
break;
case '\t':
my_b_printf(cache, "\\t");
break;
case '\'':
my_b_printf(cache, "\\'");
break;
case 0:
my_b_printf(cache, "\\0");
break;
case '`':
if (identifier)
my_b_printf(cache, "``");
else
my_b_printf(cache, "`");
break;
default:
my_b_printf(cache, "%c", c);
break;
}
}
my_b_printf(cache, identifier ? "`" : "\'");
}
/**
Print src as an string enclosed with "'"
@param[out] cache IO_CACHE where the string will be printed.
@param[in] str the string will be printed.
@param[in] len length of the string.
*/
static inline void pretty_print_str(IO_CACHE *cache, const char *str,
size_t len) {
pretty_print_str(cache, str, len, false);
}
/**
Print src as an identifier enclosed with "`"
@param[out] cache IO_CACHE where the identifier will be printed.
@param[in] str the string will be printed.
@param[in] len length of the string.
*/
static inline void pretty_print_identifier(IO_CACHE *cache, const char *str,
size_t len) {
pretty_print_str(cache, str, len, true);
}
#endif /* !MYSQL_SERVER */
#if defined(MYSQL_SERVER)
static void clear_all_errors(THD *thd, Relay_log_info *rli) {
thd->is_slave_error = false;
thd->clear_error();
rli->clear_error();
if (rli->workers_array_initialized) {
for (size_t i = 0; i < rli->get_worker_count(); i++) {
rli->get_worker(i)->clear_error();
}
}
}
inline int idempotent_error_code(int err_code) {
int ret = 0;
switch (err_code) {
case 0:
ret = 1;
break;
/*
The following list of "idempotent" errors
means that an error from the list might happen
because of idempotent (more than once)
applying of a binlog file.
Notice, that binlog has a ddl operation its
second applying may cause
case HA_ERR_TABLE_DEF_CHANGED:
case HA_ERR_CANNOT_ADD_FOREIGN:
which are not included into to the list.
Note that HA_ERR_RECORD_DELETED is not in the list since
do_exec_row() should not return that error code.
*/
case HA_ERR_RECORD_CHANGED:
case HA_ERR_KEY_NOT_FOUND:
case HA_ERR_END_OF_FILE:
case HA_ERR_FOUND_DUPP_KEY:
case HA_ERR_FOUND_DUPP_UNIQUE:
case HA_ERR_FOREIGN_DUPLICATE_KEY:
case HA_ERR_NO_REFERENCED_ROW:
case HA_ERR_ROW_IS_REFERENCED:
ret = 1;
break;
default:
ret = 0;
break;
}
return (ret);
}
/**
Ignore error code specified on command line.
*/
int ignored_error_code(int err_code) {
return ((err_code == ER_SLAVE_IGNORED_TABLE) ||
(use_slave_mask && bitmap_is_set(&slave_error_mask, err_code)));
}
/*
This function converts an engine's error to a server error.
If the thread does not have an error already reported, it tries to
define it by calling the engine's method print_error. However, if a
mapping is not found, it uses the ER_UNKNOWN_ERROR and prints out a
warning message.
*/
static int convert_handler_error(int error, THD *thd, TABLE *table) {
uint actual_error = (thd->is_error() ? thd->get_stmt_da()->mysql_errno() : 0);
if (actual_error == 0) {
table->file->print_error(error, MYF(0));
actual_error = (thd->is_error() ? thd->get_stmt_da()->mysql_errno()
: ER_UNKNOWN_ERROR);
if (actual_error == ER_UNKNOWN_ERROR)
LogErr(WARNING_LEVEL, ER_UNKNOWN_ERROR_DETECTED_IN_SE, error);
}
return (actual_error);
}
inline bool concurrency_error_code(int error) {
switch (error) {
case ER_LOCK_WAIT_TIMEOUT:
case ER_LOCK_DEADLOCK:
case ER_XA_RBDEADLOCK:
return true;
default:
return (false);
}
}
inline bool unexpected_error_code(int unexpected_error) {
switch (unexpected_error) {
case ER_NET_READ_ERROR:
case ER_NET_ERROR_ON_WRITE:
case ER_QUERY_INTERRUPTED:
case ER_SERVER_SHUTDOWN:
case ER_NEW_ABORTING_CONNECTION:
return (true);
default:
return (false);
}
}
/*
pretty_print_str()
*/
static void pretty_print_str(String *packet, const char *str, size_t len) {
packet->append('\'');
for (size_t i = 0; i < len; i++) {
switch (str[i]) {
case '\n':
packet->append("\\n");
break;
case '\r':
packet->append("\\r");
break;
case '\\':
packet->append("\\\\");
break;
case '\b':
packet->append("\\b");
break;
case '\t':
packet->append("\\t");
break;
case '\'':
packet->append("\\'");
break;
case 0:
packet->append("\\0");
break;
default:
packet->append(str[i]);
break;
}
}
packet->append('\'');
}
static inline void pretty_print_str(String *packet, const String *str) {
pretty_print_str(packet, str->ptr(), str->length());
}
/**
Creates a temporary name for load data infile:.
@param buf Store new filename here
@param file_id File_id (part of file name)
@param event_server_id Event_id (part of file name)
@param ext Extension for file name
@return
Pointer to start of extension
*/
static char *slave_load_file_stem(char *buf, uint file_id, int event_server_id,
const char *ext) {
char *res;
fn_format(buf, PREFIX_SQL_LOAD, replica_load_tmpdir, "", MY_UNPACK_FILENAME);
to_unix_path(buf);
buf = strend(buf);
int appended_length = sprintf(buf, "%s-%d-", server_uuid, event_server_id);
buf += appended_length;
res = longlong10_to_str(file_id, buf, 10);
my_stpcpy(res, ext); // Add extension last
return res; // Pointer to extension
}
/**
Delete all temporary files used for SQL_LOAD.
*/
static void cleanup_load_tmpdir() {
MY_DIR *dirp;
FILEINFO *file;
uint i;
char fname[FN_REFLEN], prefbuf[TEMP_FILE_MAX_LEN], *p;
if (!(dirp = my_dir(replica_load_tmpdir, MYF(0)))) return;
/*
When we are deleting temporary files, we should only remove
the files associated with the server id of our server.
We don't use event_server_id here because since we've disabled
direct binlogging of Create_file/Append_file/Exec_load events
we cannot meet Start_log event in the middle of events from one
LOAD DATA.
*/
p = strmake(prefbuf, STRING_WITH_LEN(PREFIX_SQL_LOAD));
sprintf(p, "%s-", server_uuid);
for (i = 0; i < dirp->number_off_files; i++) {
file = dirp->dir_entry + i;
if (is_prefix(file->name, prefbuf)) {
fn_format(fname, file->name, replica_load_tmpdir, "", MY_UNPACK_FILENAME);
mysql_file_delete(key_file_misc, fname, MYF(0));
}
}
my_dirend(dirp);
}
#endif
template <typename T>
bool net_field_length_checked(const uchar **packet, size_t *max_length,
T *out) {
if (*max_length < 1) return true;
const uchar *pos = *packet;
if (*pos < 251) {
(*packet)++;
(*max_length)--;
*out = (T)*pos;
} else if (*pos == 251) {
(*packet)++;
(*max_length)--;
*out = (T)NULL_LENGTH;
} else if (*pos == 252) {
if (*max_length < 3) return true;
(*packet) += 3;
(*max_length) -= 3;
*out = (T)uint2korr(pos + 1);
} else if (*pos == 253) {
if (*max_length < 4) return true;
(*packet) += 4;
(*max_length) -= 4;
*out = (T)uint3korr(pos + 1);
} else {
if (*max_length < 9) return true;
(*packet) += 9;
(*max_length) -= 9;
*out = (T)uint8korr(pos + 1);
}
return false;
}
template bool net_field_length_checked<size_t>(const uchar **packet,
size_t *max_length, size_t *out);
template bool net_field_length_checked<ulonglong>(const uchar **packet,
size_t *max_length,
ulonglong *out);
/**
Transforms a string into "" or its expression in 0x... form.
*/
char *str_to_hex(char *to, const char *from, size_t len) {
if (len) {
*to++ = '0';
*to++ = 'x';
to = octet2hex(to, from, len);
} else
to = my_stpcpy(to, "\"\"");
return to; // pointer to end 0 of 'to'
}
#ifdef MYSQL_SERVER
/**
Append a version of the 'from' string suitable for use in a query to
the 'to' string. To generate a correct escaping, the character set
information in 'csinfo' is used.
*/
int append_query_string(const THD *thd, const CHARSET_INFO *csinfo,
String const *from, String *to) {
char *beg, *ptr;
size_t const orig_len = to->length();
if (to->reserve(orig_len + from->length() * 2 + 3)) return 1;
beg = to->c_ptr_quick() + to->length();
ptr = beg;
if (csinfo->escape_with_backslash_is_dangerous)
ptr = str_to_hex(ptr, from->ptr(), from->length());
else {
*ptr++ = '\'';
if (!(thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES)) {
ptr +=
escape_string_for_mysql(csinfo, ptr, 0, from->ptr(), from->length());
} else {
const char *frm_str = from->ptr();
for (; frm_str < (from->ptr() + from->length()); frm_str++) {
/* Using '' way to represent "'" */
if (*frm_str == '\'') *ptr++ = *frm_str;
*ptr++ = *frm_str;
}
}
*ptr++ = '\'';
}
to->length(orig_len + ptr - beg);
return 0;
}
#endif
/**
Prints a "session_var=value" string. Used by mysqlbinlog to print some SET
commands just before it prints a query.
*/
#ifndef MYSQL_SERVER
static void print_set_option(IO_CACHE *file, uint32 bits_changed, uint32 option,
uint32 flags, const char *name, bool *need_comma) {
if (bits_changed & option) {
if (*need_comma) my_b_printf(file, ", ");
my_b_printf(file, "%s=%d", name, static_cast<bool>(flags & option));
*need_comma = true;
}
}
#endif
#ifdef MYSQL_SERVER
Replicated_columns_view::Replicated_columns_view(
Replicated_columns_view::enum_replication_flow direction, THD const *thd)
: Replicated_columns_view{nullptr, direction, thd} {}
Replicated_columns_view::Replicated_columns_view(
TABLE const *target,
Replicated_columns_view::enum_replication_flow direction, THD const *thd)
: Table_columns_view{} {
filter_fn_type filter{nullptr};
if (direction == Replicated_columns_view::OUTBOUND)
filter = [this](TABLE const *table, size_t column_index) -> bool {
return this->outbound_filtering(table, column_index);
};
else
filter = [this](TABLE const *table, size_t column_index) -> bool {
return this->inbound_filtering(table, column_index);
};
this->set_thd(thd) //
.set_filter(filter) //
.set_table(target);
}
Replicated_columns_view &Replicated_columns_view::set_thd(THD const *thd) {
this->m_thd = thd;
this->init_fields_bitmaps();
return (*this);
}
bool Replicated_columns_view::is_inbound_filtering_enabled() {
return (this->m_thd == nullptr ||
this->m_thd->variables.immediate_server_version ==
UNDEFINED_SERVER_VERSION ||
this->m_thd->variables.immediate_server_version >= 80018);
}
bool Replicated_columns_view::inbound_filtering(TABLE const *table,
size_t column_index) {
if (!this->is_inbound_filtering_enabled()) return false;
// If the set of filtered columns is changed, we need to replicate the change
// in other blocks that reproduce the behavior - Rapid binlog parser, for
// instance.
return bitmap_is_set(&table->fields_for_functional_indexes, column_index);
}
bool Replicated_columns_view::outbound_filtering(TABLE const *table,
size_t column_index) {
// If the set of filtered columns is changed, we need to replicate the change
// in other blocks that reproduce the behavior - Rapid binlog parser, for
// instance.
return bitmap_is_set(&table->fields_for_functional_indexes, column_index);
}
#endif
/**************************************************************************
Log_event methods (= the parent class of all events)
**************************************************************************/
#ifdef MYSQL_SERVER
time_t Log_event::get_time() {
/* Not previously initialized */
if (!common_header->when.tv_sec && !common_header->when.tv_usec) {
THD *tmp_thd = thd ? thd : current_thd;
if (tmp_thd)
common_header->when = tmp_thd->start_time;
else
my_micro_time_to_timeval(my_micro_time(), &(common_header->when));
}
return (time_t)common_header->when.tv_sec;
}
#endif
/**
@return
returns the human readable name of the event's type
*/
const char *Log_event::get_type_str(Log_event_type type) {
switch (type) {
case binary_log::STOP_EVENT:
return "Stop";
case binary_log::QUERY_EVENT:
return "Query";
case binary_log::ROTATE_EVENT:
return "Rotate";
case binary_log::INTVAR_EVENT:
return "Intvar";
case binary_log::APPEND_BLOCK_EVENT:
return "Append_block";
case binary_log::DELETE_FILE_EVENT:
return "Delete_file";
case binary_log::RAND_EVENT:
return "RAND";
case binary_log::XID_EVENT:
return "Xid";
case binary_log::USER_VAR_EVENT:
return "User var";
case binary_log::FORMAT_DESCRIPTION_EVENT:
return "Format_desc";
case binary_log::TABLE_MAP_EVENT:
return "Table_map";
case binary_log::WRITE_ROWS_EVENT_V1:
return "Write_rows_v1";
case binary_log::UPDATE_ROWS_EVENT_V1:
return "Update_rows_v1";
case binary_log::DELETE_ROWS_EVENT_V1:
return "Delete_rows_v1";
case binary_log::BEGIN_LOAD_QUERY_EVENT:
return "Begin_load_query";
case binary_log::EXECUTE_LOAD_QUERY_EVENT:
return "Execute_load_query";
case binary_log::INCIDENT_EVENT:
return "Incident";
case binary_log::IGNORABLE_LOG_EVENT:
return "Ignorable";
case binary_log::ROWS_QUERY_LOG_EVENT:
return "Rows_query";
case binary_log::WRITE_ROWS_EVENT:
return "Write_rows";
case binary_log::UPDATE_ROWS_EVENT:
return "Update_rows";
case binary_log::DELETE_ROWS_EVENT:
return "Delete_rows";
case binary_log::GTID_LOG_EVENT:
return "Gtid";
case binary_log::ANONYMOUS_GTID_LOG_EVENT:
return "Anonymous_Gtid";
case binary_log::PREVIOUS_GTIDS_LOG_EVENT:
return "Previous_gtids";
case binary_log::HEARTBEAT_LOG_EVENT:
case binary_log::HEARTBEAT_LOG_EVENT_V2:
return "Heartbeat";
case binary_log::TRANSACTION_CONTEXT_EVENT:
return "Transaction_context";
case binary_log::VIEW_CHANGE_EVENT:
return "View_change";
case binary_log::XA_PREPARE_LOG_EVENT:
return "XA_prepare";
case binary_log::PARTIAL_UPDATE_ROWS_EVENT:
return "Update_rows_partial";
case binary_log::TRANSACTION_PAYLOAD_EVENT:
return "Transaction_payload";
default:
return "Unknown"; /* impossible */
}
}
const char *Log_event::get_type_str() const {
return get_type_str(get_type_code());
}
/*
Log_event::Log_event()
*/
#ifdef MYSQL_SERVER
Log_event::Log_event(THD *thd_arg, uint16 flags_arg,
enum_event_cache_type cache_type_arg,
enum_event_logging_type logging_type_arg,
Log_event_header *header, Log_event_footer *footer)