/
log_event_server.cc
8577 lines (7478 loc) · 274 KB
/
log_event_server.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, 2019, Oracle and/or its affiliates.
Copyright (c) 2009, 2021, MariaDB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
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-1335 USA */
#include "mariadb.h"
#include "sql_priv.h"
#ifdef MYSQL_CLIENT
#error MYSQL_CLIENT must not be defined here
#endif
#ifndef MYSQL_SERVER
#error MYSQL_SERVER must be defined here
#endif
#include "unireg.h"
#include "log_event.h"
#include "sql_base.h" // close_thread_tables
#include "sql_cache.h" // QUERY_CACHE_FLAGS_SIZE
#include "sql_locale.h" // MY_LOCALE, my_locale_by_number, my_locale_en_US
#include "key.h" // key_copy
#include "lock.h" // mysql_unlock_tables
#include "sql_parse.h" // mysql_test_parse_for_slave
#include "tztime.h" // struct Time_zone
#include "sql_load.h" // mysql_load
#include "sql_db.h" // load_db_opt_by_name
#include "slave.h"
#include "rpl_rli.h"
#include "rpl_mi.h"
#include "rpl_filter.h"
#include "rpl_record.h"
#include "transaction.h"
#include <my_dir.h>
#include "sql_show.h" // append_identifier
#include "debug_sync.h" // debug_sync
#include <mysql/psi/mysql_statement.h>
#include <strfunc.h>
#include "compat56.h"
#include "wsrep_mysqld.h"
#include "sql_insert.h"
#include <my_bitmap.h>
#include "rpl_utility.h"
#include "rpl_constants.h"
#include "sql_digest.h"
#include "zlib.h"
#define log_cs &my_charset_latin1
#if defined(HAVE_REPLICATION)
static int rows_event_stmt_cleanup(rpl_group_info *rgi, THD* thd);
static const char *HA_ERR(int i)
{
/*
This function should only be called in case of an error
was detected
*/
DBUG_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_TO_BIG_ROW: return "HA_ERR_TO_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_PARTITION_LIST : return "HA_ERR_PARTITION_LIST";
}
return "No Error!";
}
/*
Return true if an error caught during event execution is a temporary error
that will cause automatic retry of the event group during parallel
replication, false otherwise.
In parallel replication, conflicting transactions can occasionally cause
deadlocks; such errors are handled automatically by rolling back re-trying
the transactions, so should not pollute the error log.
*/
static bool
is_parallel_retry_error(rpl_group_info *rgi, int err)
{
if (!rgi->is_parallel_exec)
return false;
if (rgi->speculation == rpl_group_info::SPECULATE_OPTIMISTIC)
return true;
if (rgi->killed_for_retry &&
(err == ER_QUERY_INTERRUPTED || err == ER_CONNECTION_KILLED))
return true;
return has_temporary_error(rgi->thd);
}
/**
Accumulate a Diagnostics_area's errors and warnings into an output buffer
@param errbuf The output buffer to write error messages
@param errbuf_size The size of the output buffer
@param da The Diagnostics_area to check for errors
*/
static void inline aggregate_da_errors(char *errbuf, size_t errbuf_size,
Diagnostics_area *da)
{
const char *errbuf_end= errbuf + errbuf_size;
char *slider;
Diagnostics_area::Sql_condition_iterator it= da->sql_conditions();
const Sql_condition *err;
size_t len;
for (err= it++, slider= errbuf; err && slider < errbuf_end - 1;
slider += len, err= it++)
{
len= my_snprintf(slider, errbuf_end - slider,
" %s, Error_code: %d;", err->get_message_text(),
err->get_sql_errno());
}
}
/**
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,
rpl_group_info *rgi, THD *thd,
TABLE *table, const char * type,
const char *log_name, my_off_t pos)
{
const char *handler_error= (ha_error ? HA_ERR(ha_error) : NULL);
char buff[MAX_SLAVE_ERRMSG];
Relay_log_info const *rli= rgi->rli;
buff[0]= 0;
int errcode= thd->is_error() ? thd->get_stmt_da()->sql_errno() : 0;
/*
In parallel replication, deadlocks or other temporary errors can happen
occasionally in normal operation, they will be handled correctly and
automatically by re-trying the transactions. So do not pollute the error
log with messages about them.
*/
if (is_parallel_retry_error(rgi, errcode))
return;
aggregate_da_errors(buff, sizeof(buff), thd->get_stmt_da());
if (ha_error != 0)
rli->report(level, errcode, rgi->gtid_info(),
"Could not execute %s event on table %s.%s;"
"%s handler error %s; "
"the event's master log %s, end_log_pos %llu",
type, table->s->db.str, table->s->table_name.str,
buff, handler_error == NULL ? "<unknown>" : handler_error,
log_name, pos);
else
rli->report(level, errcode, rgi->gtid_info(),
"Could not execute %s event on table %s.%s;"
"%s the event's master log %s, end_log_pos %llu",
type, table->s->db.str, table->s->table_name.str,
buff, log_name, pos);
}
#endif
#if defined(HAVE_REPLICATION)
static void set_thd_db(THD *thd, Rpl_filter *rpl_filter,
const char *db, uint32 db_len)
{
char lcase_db_buf[NAME_LEN +1];
LEX_CSTRING new_db;
new_db.length= db_len;
if (lower_case_table_names == 1)
{
strmov(lcase_db_buf, db);
my_casedn_str(system_charset_info, lcase_db_buf);
new_db.str= lcase_db_buf;
}
else
new_db.str= db;
/* TODO WARNING this makes rewrite_db respect lower_case_table_names values
* for more info look MDEV-17446 */
new_db.str= rpl_filter->get_rewrite_db(new_db.str, &new_db.length);
thd->set_db(&new_db);
}
#endif
#if defined(HAVE_REPLICATION)
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.
*/
inline int ignored_error_code(int err_code)
{
if (use_slave_mask && bitmap_is_set(&slave_error_mask, err_code))
{
statistic_increment(slave_skipped_errors, LOCK_status);
return 1;
}
return err_code == ER_SLAVE_IGNORED_TABLE;
}
/*
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.
*/
int convert_handler_error(int error, THD* thd, TABLE *table)
{
uint actual_error= (thd->is_error() ? thd->get_stmt_da()->sql_errno() :
0);
if (actual_error == 0)
{
table->file->print_error(error, MYF(0));
actual_error= (thd->is_error() ? thd->get_stmt_da()->sql_errno() :
ER_UNKNOWN_ERROR);
if (actual_error == ER_UNKNOWN_ERROR)
if (global_system_variables.log_warnings)
sql_print_warning("Unknown error detected %d in handler", 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_STATEMENT_TIMEOUT:
case ER_CONNECTION_KILLED:
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, int len)
{
const char *end= str + len;
packet->append(STRING_WITH_LEN("'"));
while (str < end)
{
char c;
switch ((c=*str++)) {
case '\n': packet->append(STRING_WITH_LEN("\\n")); break;
case '\r': packet->append(STRING_WITH_LEN("\\r")); break;
case '\\': packet->append(STRING_WITH_LEN("\\\\")); break;
case '\b': packet->append(STRING_WITH_LEN("\\b")); break;
case '\t': packet->append(STRING_WITH_LEN("\\t")); break;
case '\'': packet->append(STRING_WITH_LEN("\\'")); break;
case 0 : packet->append(STRING_WITH_LEN("\\0")); break;
default:
packet->append(&c, 1);
break;
}
}
packet->append(STRING_WITH_LEN("'"));
}
#endif /* HAVE_REPLICATION */
#if defined(HAVE_REPLICATION)
/**
Create a prefix for the temporary files that is to be used for
load data file name for this master
@param name Store prefix of name here
@param connection_name Connection name
@return pointer to end of name
@description
We assume that FN_REFLEN is big enough to hold
MAX_CONNECTION_NAME * MAX_FILENAME_MBWIDTH characters + 2 numbers +
a short extension.
The resulting file name has the following parts, each separated with a '-'
- PREFIX_SQL_LOAD (SQL_LOAD-)
- If a connection name is given (multi-master setup):
- Add an extra '-' to mark that this is a multi-master file
- connection name in lower case, converted to safe file characters.
(see create_logfile_name_with_suffix()).
- server_id
- A last '-' (after server_id).
*/
static char *load_data_tmp_prefix(char *name,
LEX_CSTRING *connection_name)
{
name= strmov(name, PREFIX_SQL_LOAD);
if (connection_name->length)
{
uint buf_length;
uint errors;
/* Add marker that this is a multi-master-file */
*name++='-';
/* Convert connection_name to a safe filename */
buf_length= strconvert(system_charset_info, connection_name->str, FN_REFLEN,
&my_charset_filename, name, FN_REFLEN, &errors);
name+= buf_length;
*name++= '-';
}
name= int10_to_str(global_system_variables.server_id, name, 10);
*name++ = '-';
*name= '\0'; // For testing prefixes
return name;
}
/**
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,
LEX_CSTRING *connection_name)
{
char *res;
res= buf+ unpack_dirname(buf, slave_load_tmpdir);
to_unix_path(buf);
buf= load_data_tmp_prefix(res, connection_name);
buf= int10_to_str(event_server_id, buf, 10);
*buf++ = '-';
res= int10_to_str(file_id, buf, 10);
strmov(res, ext); // Add extension last
return res; // Pointer to extension
}
#endif
#if defined(HAVE_REPLICATION)
/**
Delete all temporary files used for SQL_LOAD.
*/
static void cleanup_load_tmpdir(LEX_CSTRING *connection_name)
{
MY_DIR *dirp;
FILEINFO *file;
uint i;
char dir[FN_REFLEN], fname[FN_REFLEN];
char prefbuf[31 + MAX_CONNECTION_NAME* MAX_FILENAME_MBWIDTH + 1];
DBUG_ENTER("cleanup_load_tmpdir");
unpack_dirname(dir, slave_load_tmpdir);
if (!(dirp=my_dir(dir, MYF(MY_WME))))
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.
*/
load_data_tmp_prefix(prefbuf, connection_name);
DBUG_PRINT("enter", ("dir: '%s' prefix: '%s'", dir, prefbuf));
for (i=0 ; i < (uint)dirp->number_of_files; i++)
{
file=dirp->dir_entry+i;
if (is_prefix(file->name, prefbuf))
{
fn_format(fname,file->name,slave_load_tmpdir,"",MY_UNPACK_FILENAME);
mysql_file_delete(key_file_misc, fname, MYF(0));
}
}
my_dirend(dirp);
DBUG_VOID_RETURN;
}
#endif
/**
Append a version of the 'str' 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(CHARSET_INFO *csinfo, String *to,
const char *str, size_t len, bool no_backslash)
{
char *beg, *ptr;
uint32 const orig_len= to->length();
if (to->reserve(orig_len + len * 2 + 4))
return 1;
beg= (char*) to->ptr() + to->length();
ptr= beg;
if (csinfo->escape_with_backslash_is_dangerous)
ptr= str_to_hex(ptr, str, len);
else
{
*ptr++= '\'';
if (!no_backslash)
{
ptr+= escape_string_for_mysql(csinfo, ptr, 0, str, len);
}
else
{
const char *frm_str= str;
for (; frm_str < (str + len); frm_str++)
{
/* Using '' way to represent "'" */
if (*frm_str == '\'')
*ptr++= *frm_str;
*ptr++= *frm_str;
}
}
*ptr++= '\'';
}
to->length((uint32)(orig_len + ptr - beg));
return 0;
}
/**************************************************************************
Log_event methods (= the parent class of all events)
**************************************************************************/
Log_event::Log_event(THD* thd_arg, uint16 flags_arg, bool using_trans)
:log_pos(0), temp_buf(0), exec_time(0), thd(thd_arg),
checksum_alg(BINLOG_CHECKSUM_ALG_UNDEF)
{
server_id= thd->variables.server_id;
when= thd->start_time;
when_sec_part=thd->start_time_sec_part;
if (using_trans)
cache_type= Log_event::EVENT_TRANSACTIONAL_CACHE;
else
cache_type= Log_event::EVENT_STMT_CACHE;
flags= flags_arg |
(thd->variables.option_bits & OPTION_SKIP_REPLICATION ?
LOG_EVENT_SKIP_REPLICATION_F : 0);
}
/**
This minimal constructor is for when you are not even sure that there
is a valid THD. For example in the server when we are shutting down or
flushing logs after receiving a SIGHUP (then we must write a Rotate to
the binlog but we have no THD, so we need this minimal constructor).
*/
Log_event::Log_event()
:temp_buf(0), exec_time(0), flags(0), cache_type(EVENT_INVALID_CACHE),
thd(0), checksum_alg(BINLOG_CHECKSUM_ALG_UNDEF)
{
server_id= global_system_variables.server_id;
/*
We can't call my_time() here as this would cause a call before
my_init() is called
*/
when= 0;
when_sec_part=0;
log_pos= 0;
}
#ifdef HAVE_REPLICATION
int Log_event::do_update_pos(rpl_group_info *rgi)
{
Relay_log_info *rli= rgi->rli;
DBUG_ENTER("Log_event::do_update_pos");
DBUG_ASSERT(!rli->belongs_to_client());
/*
rli is null when (as far as I (Guilhem) know) the caller is
Load_log_event::do_apply_event *and* that one is called from
Execute_load_log_event::do_apply_event. In this case, we don't
do anything here ; Execute_load_log_event::do_apply_event will
call Log_event::do_apply_event again later with the proper rli.
Strictly speaking, if we were sure that rli is null only in the
case discussed above, 'if (rli)' is useless here. But as we are
not 100% sure, keep it for now.
Matz: I don't think we will need this check with this refactoring.
*/
if (rli)
{
/*
In parallel execution, delay position update for the events that are
not part of event groups (format description, rotate, and such) until
the actual event execution reaches that point.
*/
if (!rgi->is_parallel_exec || is_group_event(get_type_code()))
rli->stmt_done(log_pos, thd, rgi);
}
DBUG_RETURN(0); // Cannot fail currently
}
Log_event::enum_skip_reason
Log_event::do_shall_skip(rpl_group_info *rgi)
{
Relay_log_info *rli= rgi->rli;
DBUG_PRINT("info", ("ev->server_id: %lu, ::server_id: %lu,"
" rli->replicate_same_server_id: %d,"
" rli->slave_skip_counter: %llu",
(ulong) server_id,
(ulong) global_system_variables.server_id,
rli->replicate_same_server_id,
rli->slave_skip_counter));
if ((server_id == global_system_variables.server_id &&
!rli->replicate_same_server_id) ||
(rli->slave_skip_counter == 1 && rli->is_in_group()) ||
(flags & LOG_EVENT_SKIP_REPLICATION_F &&
opt_replicate_events_marked_for_skip != RPL_SKIP_REPLICATE))
return EVENT_SKIP_IGNORE;
if (rli->slave_skip_counter > 0)
return EVENT_SKIP_COUNT;
return EVENT_SKIP_NOT;
}
/*
Log_event::pack_info()
*/
void Log_event::pack_info(Protocol *protocol)
{
protocol->store("", &my_charset_bin);
}
/**
Only called by SHOW BINLOG EVENTS
*/
int Log_event::net_send(Protocol *protocol, const char* log_name, my_off_t pos)
{
const char *p= strrchr(log_name, FN_LIBCHAR);
const char *event_type;
if (p)
log_name = p + 1;
protocol->prepare_for_resend();
protocol->store(log_name, &my_charset_bin);
protocol->store((ulonglong) pos);
event_type = get_type_str();
protocol->store(event_type, strlen(event_type), &my_charset_bin);
protocol->store((uint32) server_id);
protocol->store((ulonglong) log_pos);
pack_info(protocol);
return protocol->write();
}
#endif /* HAVE_REPLICATION */
/**
init_show_field_list() prepares the column names and types for the
output of SHOW BINLOG EVENTS; it is used only by SHOW BINLOG
EVENTS.
*/
void Log_event::init_show_field_list(THD *thd, List<Item>* field_list)
{
MEM_ROOT *mem_root= thd->mem_root;
field_list->push_back(new (mem_root)
Item_empty_string(thd, "Log_name", 20),
mem_root);
field_list->push_back(new (mem_root)
Item_return_int(thd, "Pos",
MY_INT64_NUM_DECIMAL_DIGITS,
MYSQL_TYPE_LONGLONG),
mem_root);
field_list->push_back(new (mem_root)
Item_empty_string(thd, "Event_type", 20),
mem_root);
field_list->push_back(new (mem_root)
Item_return_int(thd, "Server_id", 10,
MYSQL_TYPE_LONG),
mem_root);
field_list->push_back(new (mem_root)
Item_return_int(thd, "End_log_pos",
MY_INT64_NUM_DECIMAL_DIGITS,
MYSQL_TYPE_LONGLONG),
mem_root);
field_list->push_back(new (mem_root) Item_empty_string(thd, "Info", 20),
mem_root);
}
/**
A decider of whether to trigger checksum computation or not.
To be invoked in Log_event::write() stack.
The decision is positive
S,M) if it's been marked for checksumming with @c checksum_alg
M) otherwise, if @@global.binlog_checksum is not NONE and the event is
directly written to the binlog file.
The to-be-cached event decides at @c write_cache() time.
Otherwise the decision is negative.
@note A side effect of the method is altering Log_event::checksum_alg
it the latter was undefined at calling.
@return true Checksum should be used. Log_event::checksum_alg is set.
@return false No checksum
*/
my_bool Log_event::need_checksum()
{
my_bool ret;
DBUG_ENTER("Log_event::need_checksum");
/*
few callers of Log_event::write
(incl FD::write, FD constructing code on the slave side, Rotate relay log
and Stop event)
provides their checksum alg preference through Log_event::checksum_alg.
*/
if (checksum_alg != BINLOG_CHECKSUM_ALG_UNDEF)
ret= checksum_alg != BINLOG_CHECKSUM_ALG_OFF;
else
{
ret= binlog_checksum_options && cache_type == Log_event::EVENT_NO_CACHE;
checksum_alg= ret ? (enum_binlog_checksum_alg)binlog_checksum_options
: BINLOG_CHECKSUM_ALG_OFF;
}
/*
FD calls the methods before data_written has been calculated.
The following invariant claims if the current is not the first
call (and therefore data_written is not zero) then `ret' must be
TRUE. It may not be null because FD is always checksummed.
*/
DBUG_ASSERT(get_type_code() != FORMAT_DESCRIPTION_EVENT || ret ||
data_written == 0);
DBUG_ASSERT(!ret ||
((checksum_alg == binlog_checksum_options ||
/*
Stop event closes the relay-log and its checksum alg
preference is set by the caller can be different
from the server's binlog_checksum_options.
*/
get_type_code() == STOP_EVENT ||
/*
Rotate:s can be checksummed regardless of the server's
binlog_checksum_options. That applies to both
the local RL's Rotate and the master's Rotate
which IO thread instantiates via queue_binlog_ver_3_event.
*/
get_type_code() == ROTATE_EVENT ||
get_type_code() == START_ENCRYPTION_EVENT ||
/* FD is always checksummed */
get_type_code() == FORMAT_DESCRIPTION_EVENT) &&
checksum_alg != BINLOG_CHECKSUM_ALG_OFF));
DBUG_ASSERT(checksum_alg != BINLOG_CHECKSUM_ALG_UNDEF);
DBUG_ASSERT(((get_type_code() != ROTATE_EVENT &&
get_type_code() != STOP_EVENT) ||
get_type_code() != FORMAT_DESCRIPTION_EVENT) ||
cache_type == Log_event::EVENT_NO_CACHE);
DBUG_RETURN(ret);
}
int Log_event_writer::write_internal(const uchar *pos, size_t len)
{
DBUG_ASSERT(!ctx || encrypt_or_write == &Log_event_writer::encrypt_and_write);
if (my_b_safe_write(file, pos, len))
{
DBUG_PRINT("error", ("write to log failed: %d", my_errno));
return 1;
}
bytes_written+= len;
return 0;
}
/*
as soon as encryption produces the first output block, write event_len
where it should be in a valid event header
*/
int Log_event_writer::maybe_write_event_len(uchar *pos, size_t len)
{
if (len && event_len)
{
DBUG_ASSERT(len >= EVENT_LEN_OFFSET);
if (write_internal(pos + EVENT_LEN_OFFSET - 4, 4))
return 1;
int4store(pos + EVENT_LEN_OFFSET - 4, event_len);
event_len= 0;
}
return 0;
}
int Log_event_writer::encrypt_and_write(const uchar *pos, size_t len)
{
uchar *dst;
size_t dstsize;
uint dstlen;
int res; // Safe as res is always set
DBUG_ASSERT(ctx);
if (!len)
return 0;
dstsize= encryption_encrypted_length((uint)len, ENCRYPTION_KEY_SYSTEM_DATA,
crypto->key_version);
if (!(dst= (uchar*)my_safe_alloca(dstsize)))
return 1;
if (encryption_ctx_update(ctx, pos, (uint)len, dst, &dstlen))
{
res= 1;
goto err;
}
if (maybe_write_event_len(dst, dstlen))
{
res= 1;
goto err;
}
res= write_internal(dst, dstlen);
err:
my_safe_afree(dst, dstsize);
return res;
}
int Log_event_writer::write_header(uchar *pos, size_t len)
{
DBUG_ENTER("Log_event_writer::write_header");
/*
recording checksum of FD event computed with dropped
possibly active LOG_EVENT_BINLOG_IN_USE_F flag.
Similar step at verication: the active flag is dropped before
checksum computing.
*/
if (checksum_len)
{
uchar save=pos[FLAGS_OFFSET];
pos[FLAGS_OFFSET]&= ~LOG_EVENT_BINLOG_IN_USE_F;
crc= my_checksum(0, pos, len);
pos[FLAGS_OFFSET]= save;
}
if (ctx)
{
uchar iv[BINLOG_IV_LENGTH];
crypto->set_iv(iv, (uint32)my_b_safe_tell(file));
if (encryption_ctx_init(ctx, crypto->key, crypto->key_length,
iv, sizeof(iv), ENCRYPTION_FLAG_ENCRYPT | ENCRYPTION_FLAG_NOPAD,
ENCRYPTION_KEY_SYSTEM_DATA, crypto->key_version))
DBUG_RETURN(1);
DBUG_ASSERT(len >= LOG_EVENT_HEADER_LEN);
event_len= uint4korr(pos + EVENT_LEN_OFFSET);
DBUG_ASSERT(event_len >= len);
memcpy(pos + EVENT_LEN_OFFSET, pos, 4);
pos+= 4;
len-= 4;
}
DBUG_RETURN((this->*encrypt_or_write)(pos, len));
}
int Log_event_writer::write_data(const uchar *pos, size_t len)
{
DBUG_ENTER("Log_event_writer::write_data");
if (!len)
DBUG_RETURN(0);
if (checksum_len)
crc= my_checksum(crc, pos, len);
DBUG_RETURN((this->*encrypt_or_write)(pos, len));
}
int Log_event_writer::write_footer()
{
DBUG_ENTER("Log_event_writer::write_footer");
if (checksum_len)
{
uchar checksum_buf[BINLOG_CHECKSUM_LEN];
int4store(checksum_buf, crc);
if ((this->*encrypt_or_write)(checksum_buf, BINLOG_CHECKSUM_LEN))
DBUG_RETURN(ER_ERROR_ON_WRITE);
}
if (ctx)
{
uint dstlen;
uchar dst[MY_AES_BLOCK_SIZE*2];
if (encryption_ctx_finish(ctx, dst, &dstlen))
DBUG_RETURN(1);
if (maybe_write_event_len(dst, dstlen) || write_internal(dst, dstlen))
DBUG_RETURN(ER_ERROR_ON_WRITE);
}
DBUG_RETURN(0);
}
/*
Log_event::write_header()
*/
bool Log_event::write_header(size_t event_data_length)
{
uchar header[LOG_EVENT_HEADER_LEN];
ulong now;
DBUG_ENTER("Log_event::write_header");
DBUG_PRINT("enter", ("filepos: %lld length: %zu type: %d",
(longlong) writer->pos(), event_data_length,
(int) get_type_code()));
writer->checksum_len= need_checksum() ? BINLOG_CHECKSUM_LEN : 0;
/* Store number of bytes that will be written by this event */
data_written= event_data_length + sizeof(header) + writer->checksum_len;
/*
log_pos != 0 if this is relay-log event. In this case we should not
change the position
*/
if (is_artificial_event())
{
/*
Artificial events are automatically generated and do not exist
in master's binary log, so log_pos should be set to 0.
*/
log_pos= 0;
}
else if (!log_pos)
{
/*
Calculate the position of where the next event will start
(end of this event, that is).
*/
log_pos= writer->pos() + data_written;
DBUG_EXECUTE_IF("dbug_master_binlog_over_2GB", log_pos += (1ULL <<31););
}
now= get_time(); // Query start time
/*
Header will be of size LOG_EVENT_HEADER_LEN for all events, except for
FORMAT_DESCRIPTION_EVENT and ROTATE_EVENT, where it will be
LOG_EVENT_MINIMAL_HEADER_LEN (remember these 2 have a frozen header,
because we read them before knowing the format).