/
log_event.cc
15077 lines (13261 loc) · 471 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, 2018, Oracle and/or its affiliates.
Copyright (c) 2009, 2019, 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-1301 USA */
#include "mariadb.h"
#include "sql_priv.h"
#ifndef MYSQL_CLIENT
#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 <mysql/psi/mysql_statement.h>
#include <strfunc.h>
#include "compat56.h"
#include "wsrep_mysqld.h"
#include "sql_insert.h"
#else
#include "mysqld_error.h"
#endif /* MYSQL_CLIENT */
#include <my_bitmap.h>
#include "rpl_utility.h"
#include "rpl_constants.h"
#include "sql_digest.h"
#include "zlib.h"
#define my_b_write_string(A, B) my_b_write((A), (uchar*)(B), (uint) (sizeof(B) - 1))
/**
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
#define FLAGSTR(V,F) ((V)&(F)?#F" ":"")
/*
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)
/*
replication event checksum is introduced in the following "checksum-home" version.
The checksum-aware servers extract FD's version to decide whether the FD event
carries checksum info.
TODO: correct the constant when it has been determined
(which main tree to push and when)
*/
const Version checksum_version_split_mysql(5, 6, 1);
const Version checksum_version_split_mariadb(5, 3, 0);
// First MySQL version with fraction seconds
const Version fsp_version_split_mysql(5, 6, 0);
#if !defined(MYSQL_CLIENT) && 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";
}
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);
}
/**
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], *slider;
const char *buff_end= buff + sizeof(buff);
size_t len;
Diagnostics_area::Sql_condition_iterator it=
thd->get_stmt_da()->sql_conditions();
Relay_log_info const *rli= rgi->rli;
const Sql_condition *err;
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;
for (err= it++, slider= buff; err && slider < buff_end - 1;
slider += len, err= it++)
{
len= my_snprintf(slider, buff_end - slider,
" %s, Error_code: %d;", err->get_message_text(),
err->get_sql_errno());
}
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) && !defined(MYSQL_CLIENT)
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
/*
Cache that will automatically be written to a dedicated file on
destruction.
DESCRIPTION
*/
class Write_on_release_cache
{
public:
enum flag
{
FLUSH_F
};
typedef unsigned short flag_set;
/*
Constructor.
SYNOPSIS
Write_on_release_cache
cache Pointer to cache to use
file File to write cache to upon destruction
flags Flags for the cache
DESCRIPTION
Cache common parameters and ensure common flush_data() code
on successful copy of the cache, the cache will be reinited as a
WRITE_CACHE.
Currently, a pointer to the cache is provided in the
constructor, but it would be possible to create a subclass
holding the IO_CACHE itself.
*/
Write_on_release_cache(IO_CACHE *cache, FILE *file, flag_set flags = 0, Log_event *ev = NULL)
: m_cache(cache), m_file(file), m_flags(flags), m_ev(ev)
{
reinit_io_cache(m_cache, WRITE_CACHE, 0L, FALSE, TRUE);
}
~Write_on_release_cache() {}
bool flush_data()
{
#ifdef MYSQL_CLIENT
if (m_ev == NULL)
{
if (copy_event_cache_to_file_and_reinit(m_cache, m_file))
return 1;
if ((m_flags & FLUSH_F) && fflush(m_file))
return 1;
}
else // if m_ev<>NULL, then storing the output in output_buf
{
LEX_STRING tmp_str;
bool res;
if (copy_event_cache_to_string_and_reinit(m_cache, &tmp_str))
return 1;
/* use 2 argument append as tmp_str is not \0 terminated */
res= m_ev->output_buf.append(tmp_str.str, tmp_str.length);
my_free(tmp_str.str);
return res ? res : 0;
}
#else /* MySQL_SERVER */
if (copy_event_cache_to_file_and_reinit(m_cache, m_file))
return 1;
if ((m_flags & FLUSH_F) && fflush(m_file))
return 1;
#endif
return 0;
}
/*
Return a pointer to the internal IO_CACHE.
SYNOPSIS
operator&()
DESCRIPTION
Function to return a pointer to the internal cache, so that the
object can be treated as a IO_CACHE and used with the my_b_*
IO_CACHE functions
RETURN VALUE
A pointer to the internal IO_CACHE.
*/
IO_CACHE *operator&()
{
return m_cache;
}
private:
// Hidden, to prevent usage.
Write_on_release_cache(Write_on_release_cache const&);
IO_CACHE *m_cache;
FILE *m_file;
flag_set m_flags;
Log_event *m_ev; // Used for Flashback
};
/*
pretty_print_str()
*/
#ifdef MYSQL_CLIENT
static bool pretty_print_str(IO_CACHE* cache, const char* str, int len)
{
const char* end = str + len;
if (my_b_write_byte(cache, '\''))
goto err;
while (str < end)
{
char c;
int error;
switch ((c=*str++)) {
case '\n': error= my_b_write(cache, (uchar*)"\\n", 2); break;
case '\r': error= my_b_write(cache, (uchar*)"\\r", 2); break;
case '\\': error= my_b_write(cache, (uchar*)"\\\\", 2); break;
case '\b': error= my_b_write(cache, (uchar*)"\\b", 2); break;
case '\t': error= my_b_write(cache, (uchar*)"\\t", 2); break;
case '\'': error= my_b_write(cache, (uchar*)"\\'", 2); break;
case 0 : error= my_b_write(cache, (uchar*)"\\0", 2); break;
default:
error= my_b_write_byte(cache, c);
break;
}
if (unlikely(error))
goto err;
}
return my_b_write_byte(cache, '\'');
err:
return 1;
}
#endif /* MYSQL_CLIENT */
#if defined(HAVE_REPLICATION) && !defined(MYSQL_CLIENT)
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 /* !MYSQL_CLIENT */
#ifndef DBUG_OFF
#define DBUG_DUMP_EVENT_BUF(B,L) \
do { \
const uchar *_buf=(uchar*)(B); \
size_t _len=(L); \
if (_len >= LOG_EVENT_MINIMAL_HEADER_LEN) \
{ \
DBUG_PRINT("data", ("header: timestamp:%u type:%u server_id:%u len:%u log_pos:%u flags:%u", \
uint4korr(_buf), _buf[EVENT_TYPE_OFFSET], \
uint4korr(_buf+SERVER_ID_OFFSET), \
uint4korr(_buf+EVENT_LEN_OFFSET), \
uint4korr(_buf+LOG_POS_OFFSET), \
uint4korr(_buf+FLAGS_OFFSET))); \
DBUG_DUMP("data", _buf+LOG_EVENT_MINIMAL_HEADER_LEN, \
_len-LOG_EVENT_MINIMAL_HEADER_LEN); \
} \
else \
DBUG_DUMP("data", _buf, _len); \
} while(0)
#else
#define DBUG_DUMP_EVENT_BUF(B,L) do { } while(0)
#endif
#if defined(HAVE_REPLICATION) && !defined(MYSQL_CLIENT)
/**
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) && !defined(MYSQL_CLIENT)
/**
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
/*
read_str()
*/
static inline int read_str(const char **buf, const char *buf_end,
const char **str, uint8 *len)
{
if (*buf + ((uint) (uchar) **buf) >= buf_end)
return 1;
*len= (uint8) **buf;
*str= (*buf)+1;
(*buf)+= (uint) *len+1;
return 0;
}
/**
Transforms a string into "" or its expression in X'HHHH' form.
*/
char *str_to_hex(char *to, const char *from, size_t len)
{
if (len)
{
*to++= 'X';
*to++= '\'';
to= octet2hex(to, from, len);
*to++= '\'';
*to= '\0';
}
else
to= strmov(to, "\"\"");
return to; // pointer to end 0 of 'to'
}
#define BINLOG_COMPRESSED_HEADER_LEN 1
#define BINLOG_COMPRESSED_ORIGINAL_LENGTH_MAX_BYTES 4
/**
Compressed Record
Record Header: 1 Byte
7 Bit: Always 1, mean compressed;
4-6 Bit: Compressed algorithm - Always 0, means zlib
It maybe support other compression algorithm in the future.
0-3 Bit: Bytes of "Record Original Length"
Record Original Length: 1-4 Bytes
Compressed Buf:
*/
/**
Get the length of compress content.
*/
uint32 binlog_get_compress_len(uint32 len)
{
/* 5 for the begin content, 1 reserved for a '\0'*/
return ALIGN_SIZE((BINLOG_COMPRESSED_HEADER_LEN + BINLOG_COMPRESSED_ORIGINAL_LENGTH_MAX_BYTES)
+ compressBound(len) + 1);
}
/**
Compress buf from 'src' to 'dst'.
Note: 1) Then the caller should guarantee the length of 'dst', which
can be got by binlog_get_uncompress_len, is enough to hold
the content uncompressed.
2) The 'comlen' should stored the length of 'dst', and it will
be set as the size of compressed content after return.
return zero if successful, others otherwise.
*/
int binlog_buf_compress(const char *src, char *dst, uint32 len, uint32 *comlen)
{
uchar lenlen;
if (len & 0xFF000000)
{
dst[1] = uchar(len >> 24);
dst[2] = uchar(len >> 16);
dst[3] = uchar(len >> 8);
dst[4] = uchar(len);
lenlen = 4;
}
else if (len & 0x00FF0000)
{
dst[1] = uchar(len >> 16);
dst[2] = uchar(len >> 8);
dst[3] = uchar(len);
lenlen = 3;
}
else if (len & 0x0000FF00)
{
dst[1] = uchar(len >> 8);
dst[2] = uchar(len);
lenlen = 2;
}
else
{
dst[1] = uchar(len);
lenlen = 1;
}
dst[0] = 0x80 | (lenlen & 0x07);
uLongf tmplen = (uLongf)*comlen - BINLOG_COMPRESSED_HEADER_LEN - lenlen - 1;
if (compress((Bytef *)dst + BINLOG_COMPRESSED_HEADER_LEN + lenlen, &tmplen,
(const Bytef *)src, (uLongf)len) != Z_OK)
{
return 1;
}
*comlen = (uint32)tmplen + BINLOG_COMPRESSED_HEADER_LEN + lenlen;
return 0;
}
/**
Convert a query_compressed_log_event to query_log_event
from 'src' to 'dst', the size after compression stored in 'newlen'.
@Note:
1) The caller should call my_free to release 'dst' if *is_malloc is
returned as true.
2) If *is_malloc is retuened as false, then 'dst' reuses the passed-in
'buf'.
return zero if successful, non-zero otherwise.
*/
int
query_event_uncompress(const Format_description_log_event *description_event,
bool contain_checksum, const char *src, ulong src_len,
char* buf, ulong buf_size, bool* is_malloc, char **dst,
ulong *newlen)
{
ulong len = uint4korr(src + EVENT_LEN_OFFSET);
const char *tmp = src;
const char *end = src + len;
// bad event
if (src_len < len )
return 1;
DBUG_ASSERT((uchar)src[EVENT_TYPE_OFFSET] == QUERY_COMPRESSED_EVENT);
uint8 common_header_len= description_event->common_header_len;
uint8 post_header_len=
description_event->post_header_len[QUERY_COMPRESSED_EVENT-1];
*is_malloc = false;
tmp += common_header_len;
// bad event
if (end <= tmp)
return 1;
uint db_len = (uint)tmp[Q_DB_LEN_OFFSET];
uint16 status_vars_len= uint2korr(tmp + Q_STATUS_VARS_LEN_OFFSET);
tmp += post_header_len + status_vars_len + db_len + 1;
// bad event
if (end <= tmp)
return 1;
int32 comp_len = (int32)(len - (tmp - src) -
(contain_checksum ? BINLOG_CHECKSUM_LEN : 0));
uint32 un_len = binlog_get_uncompress_len(tmp);
// bad event
if (comp_len < 0 || un_len == 0)
return 1;
*newlen = (ulong)(tmp - src) + un_len;
if(contain_checksum)
*newlen += BINLOG_CHECKSUM_LEN;
uint32 alloc_size = (uint32)ALIGN_SIZE(*newlen);
char *new_dst = NULL;
if (alloc_size <= buf_size)
{
new_dst = buf;
}
else
{
new_dst = (char *)my_malloc(alloc_size, MYF(MY_WME));
if (!new_dst)
return 1;
*is_malloc = true;
}
/* copy the head*/
memcpy(new_dst, src , tmp - src);
if (binlog_buf_uncompress(tmp, new_dst + (tmp - src),
comp_len, &un_len))
{
if (*is_malloc)
my_free(new_dst);
*is_malloc = false;
return 1;
}
new_dst[EVENT_TYPE_OFFSET] = QUERY_EVENT;
int4store(new_dst + EVENT_LEN_OFFSET, *newlen);
if(contain_checksum)
{
ulong clear_len = *newlen - BINLOG_CHECKSUM_LEN;
int4store(new_dst + clear_len,
my_checksum(0L, (uchar *)new_dst, clear_len));
}
*dst = new_dst;
return 0;
}
int
row_log_event_uncompress(const Format_description_log_event *description_event,
bool contain_checksum, const char *src, ulong src_len,
char* buf, ulong buf_size, bool* is_malloc, char **dst,
ulong *newlen)
{
Log_event_type type = (Log_event_type)(uchar)src[EVENT_TYPE_OFFSET];
ulong len = uint4korr(src + EVENT_LEN_OFFSET);
const char *tmp = src;
char *new_dst = NULL;
const char *end = tmp + len;
// bad event
if (src_len < len)
return 1;
DBUG_ASSERT(LOG_EVENT_IS_ROW_COMPRESSED(type));
uint8 common_header_len= description_event->common_header_len;
uint8 post_header_len= description_event->post_header_len[type-1];
tmp += common_header_len + ROWS_HEADER_LEN_V1;
if (post_header_len == ROWS_HEADER_LEN_V2)
{
/*
Have variable length header, check length,
which includes length bytes
*/
// bad event
if (end - tmp <= 2)
return 1;
uint16 var_header_len= uint2korr(tmp);
DBUG_ASSERT(var_header_len >= 2);
/* skip over var-len header, extracting 'chunks' */
tmp += var_header_len;
/* get the uncompressed event type */
type=
(Log_event_type)(type - WRITE_ROWS_COMPRESSED_EVENT + WRITE_ROWS_EVENT);
}
else
{
/* get the uncompressed event type */
type= (Log_event_type)
(type - WRITE_ROWS_COMPRESSED_EVENT_V1 + WRITE_ROWS_EVENT_V1);
}
//bad event
if (end <= tmp)
return 1;
ulong m_width = net_field_length((uchar **)&tmp);
tmp += (m_width + 7) / 8;
if (type == UPDATE_ROWS_EVENT_V1 || type == UPDATE_ROWS_EVENT)
{
tmp += (m_width + 7) / 8;
}
//bad event
if (end <= tmp)
return 1;
uint32 un_len = binlog_get_uncompress_len(tmp);
//bad event
if (un_len == 0)
return 1;
int32 comp_len = (int32)(len - (tmp - src) -
(contain_checksum ? BINLOG_CHECKSUM_LEN : 0));
//bad event