/
sql_repl.cc
4687 lines (4101 loc) · 146 KB
/
sql_repl.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) 2008, 2020, MariaDB Corporation
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"
#include "unireg.h"
#include "sql_base.h"
#include "sql_parse.h" // check_access
#ifdef HAVE_REPLICATION
#include "rpl_mi.h"
#include "rpl_rli.h"
#include "sql_repl.h"
#include "log_event.h"
#include "rpl_filter.h"
#include <my_dir.h>
#include "debug_sync.h"
#include "semisync_master.h"
#include "semisync_slave.h"
#include "mysys_err.h"
enum enum_gtid_until_state {
GTID_UNTIL_NOT_DONE,
GTID_UNTIL_STOP_AFTER_STANDALONE,
GTID_UNTIL_STOP_AFTER_TRANSACTION
};
int max_binlog_dump_events = 0; // unlimited
my_bool opt_sporadic_binlog_dump_fail = 0;
#ifndef DBUG_OFF
static int binlog_dump_count = 0;
#endif
extern TYPELIB binlog_checksum_typelib;
static int
fake_event_header(String* packet, Log_event_type event_type, ulong extra_len,
my_bool *do_checksum, ha_checksum *crc, const char** errmsg,
enum enum_binlog_checksum_alg checksum_alg_arg, uint32 end_pos)
{
char header[LOG_EVENT_HEADER_LEN];
ulong event_len;
*do_checksum= checksum_alg_arg != BINLOG_CHECKSUM_ALG_OFF &&
checksum_alg_arg != BINLOG_CHECKSUM_ALG_UNDEF;
/*
'when' (the timestamp) is set to 0 so that slave could distinguish between
real and fake Rotate events (if necessary)
*/
memset(header, 0, 4);
header[EVENT_TYPE_OFFSET] = (uchar)event_type;
event_len= LOG_EVENT_HEADER_LEN + extra_len +
(*do_checksum ? BINLOG_CHECKSUM_LEN : 0);
int4store(header + SERVER_ID_OFFSET, global_system_variables.server_id);
int4store(header + EVENT_LEN_OFFSET, event_len);
int2store(header + FLAGS_OFFSET, LOG_EVENT_ARTIFICIAL_F);
// TODO: check what problems this may cause and fix them
int4store(header + LOG_POS_OFFSET, end_pos);
if (packet->append(header, sizeof(header)))
{
*errmsg= "Failed due to out-of-memory writing event";
return -1;
}
if (*do_checksum)
{
*crc= my_checksum(0, (uchar*)header, sizeof(header));
}
return 0;
}
static int
fake_event_footer(String *packet, my_bool do_checksum, ha_checksum crc, const char **errmsg)
{
if (do_checksum)
{
char b[BINLOG_CHECKSUM_LEN];
int4store(b, crc);
if (packet->append(b, sizeof(b)))
{
*errmsg= "Failed due to out-of-memory writing event checksum";
return -1;
}
}
return 0;
}
static int
fake_event_write(NET *net, String *packet, const char **errmsg)
{
if (my_net_write(net, (uchar*) packet->ptr(), packet->length()))
{
*errmsg = "failed on my_net_write()";
return -1;
}
return 0;
}
/*
Helper structure, used to pass miscellaneous info from mysql_binlog_send()
into the helper functions that it calls.
*/
struct binlog_send_info {
rpl_binlog_state until_binlog_state;
slave_connection_state gtid_state;
THD *thd;
NET *net;
String *packet;
char *const log_file_name; // ptr/alias to linfo.log_file_name
slave_connection_state *until_gtid_state;
slave_connection_state until_gtid_state_obj;
Format_description_log_event *fdev;
int mariadb_slave_capability;
enum_gtid_skip_type gtid_skip_group;
enum_gtid_until_state gtid_until_group;
ushort flags;
enum enum_binlog_checksum_alg current_checksum_alg;
bool slave_gtid_strict_mode;
bool send_fake_gtid_list;
bool slave_gtid_ignore_duplicates;
bool using_gtid_state;
int error;
const char *errmsg;
char error_text[MAX_SLAVE_ERRMSG];
rpl_gtid error_gtid;
ulonglong heartbeat_period;
/** start file/pos as requested by slave, for error message */
char start_log_file_name[FN_REFLEN];
my_off_t start_pos;
/** last pos for error message */
my_off_t last_pos;
#ifndef DBUG_OFF
int left_events;
uint dbug_reconnect_counter;
ulong hb_info_counter;
#endif
bool clear_initial_log_pos;
bool should_stop;
size_t dirlen;
binlog_send_info(THD *thd_arg, String *packet_arg, ushort flags_arg,
char *lfn)
: thd(thd_arg), net(&thd_arg->net), packet(packet_arg),
log_file_name(lfn), until_gtid_state(NULL), fdev(NULL),
gtid_skip_group(GTID_SKIP_NOT), gtid_until_group(GTID_UNTIL_NOT_DONE),
flags(flags_arg), current_checksum_alg(BINLOG_CHECKSUM_ALG_UNDEF),
slave_gtid_strict_mode(false), send_fake_gtid_list(false),
slave_gtid_ignore_duplicates(false),
error(0),
errmsg("Unknown error"),
heartbeat_period(0),
#ifndef DBUG_OFF
left_events(max_binlog_dump_events),
dbug_reconnect_counter(0),
hb_info_counter(0),
#endif
clear_initial_log_pos(false),
should_stop(false)
{
error_text[0] = 0;
bzero(&error_gtid, sizeof(error_gtid));
until_binlog_state.init();
}
};
// prototype
static int reset_transmit_packet(struct binlog_send_info *info, ushort flags,
ulong *ev_offset, const char **errmsg);
/*
fake_rotate_event() builds a fake (=which does not exist physically in any
binlog) Rotate event, which contains the name of the binlog we are going to
send to the slave (because the slave may not know it if it just asked for
MASTER_LOG_FILE='', MASTER_LOG_POS=4).
< 4.0.14, fake_rotate_event() was called only if the requested pos was 4.
After this version we always call it, so that a 3.23.58 slave can rely on
it to detect if the master is 4.0 (and stop) (the _fake_ Rotate event has
zeros in the good positions which, by chance, make it possible for the 3.23
slave to detect that this event is unexpected) (this is luck which happens
because the master and slave disagree on the size of the header of
Log_event).
Relying on the event length of the Rotate event instead of these
well-placed zeros was not possible as Rotate events have a variable-length
part.
*/
static int fake_rotate_event(binlog_send_info *info, ulonglong position,
const char** errmsg, enum enum_binlog_checksum_alg checksum_alg_arg)
{
DBUG_ENTER("fake_rotate_event");
ulong ev_offset;
char buf[ROTATE_HEADER_LEN+100];
my_bool do_checksum;
int err;
char* p = info->log_file_name+dirname_length(info->log_file_name);
uint ident_len = (uint) strlen(p);
String *packet= info->packet;
ha_checksum crc;
/* reset transmit packet for the fake rotate event below */
if (reset_transmit_packet(info, info->flags, &ev_offset, &info->errmsg))
DBUG_RETURN(1);
if ((err= fake_event_header(packet, ROTATE_EVENT,
ident_len + ROTATE_HEADER_LEN, &do_checksum,
&crc,
errmsg, checksum_alg_arg, 0)))
{
info->error= ER_UNKNOWN_ERROR;
DBUG_RETURN(err);
}
int8store(buf+R_POS_OFFSET,position);
packet->append(buf, ROTATE_HEADER_LEN);
packet->append(p, ident_len);
if (do_checksum)
{
crc= my_checksum(crc, (uchar*)buf, ROTATE_HEADER_LEN);
crc= my_checksum(crc, (uchar*)p, ident_len);
}
if ((err= fake_event_footer(packet, do_checksum, crc, errmsg)) ||
(err= fake_event_write(info->net, packet, errmsg)))
{
info->error= ER_UNKNOWN_ERROR;
DBUG_RETURN(err);
}
DBUG_RETURN(0);
}
static int fake_gtid_list_event(binlog_send_info *info,
Gtid_list_log_event *glev, const char** errmsg,
uint32 current_pos)
{
my_bool do_checksum;
int err;
ha_checksum crc;
char buf[128];
String str(buf, sizeof(buf), system_charset_info);
String* packet= info->packet;
str.length(0);
if (glev->to_packet(&str))
{
info->error= ER_UNKNOWN_ERROR;
*errmsg= "Failed due to out-of-memory writing Gtid_list event";
return -1;
}
if ((err= fake_event_header(packet, GTID_LIST_EVENT,
str.length(), &do_checksum, &crc,
errmsg, info->current_checksum_alg, current_pos)))
{
info->error= ER_UNKNOWN_ERROR;
return err;
}
packet->append(str);
if (do_checksum)
{
crc= my_checksum(crc, (uchar*)str.ptr(), str.length());
}
if ((err= fake_event_footer(packet, do_checksum, crc, errmsg)) ||
(err= fake_event_write(info->net, packet, errmsg)))
{
info->error= ER_UNKNOWN_ERROR;
return err;
}
return 0;
}
/*
Reset thread transmit packet buffer for event sending
This function allocates header bytes for event transmission, and
should be called before store the event data to the packet buffer.
*/
static int reset_transmit_packet(binlog_send_info *info, ushort flags,
ulong *ev_offset, const char **errmsg)
{
int ret= 0;
String *packet= &info->thd->packet;
/* reserve and set default header */
packet->length(0);
packet->set("\0", 1, &my_charset_bin);
if (info->thd->semi_sync_slave)
{
if (repl_semisync_master.reserve_sync_header(packet))
{
info->error= ER_UNKNOWN_ERROR;
*errmsg= "Failed to run hook 'reserve_header'";
ret= 1;
}
}
*ev_offset= packet->length();
return ret;
}
int get_user_var_int(const char *name,
long long int *value, int *null_value)
{
bool null_val;
user_var_entry *entry=
(user_var_entry*) my_hash_search(¤t_thd->user_vars,
(uchar*) name, strlen(name));
if (!entry)
return 1;
*value= entry->val_int(&null_val);
if (null_value)
*null_value= null_val;
return 0;
}
inline bool is_semi_sync_slave()
{
int null_value;
long long val= 0;
get_user_var_int("rpl_semi_sync_slave", &val, &null_value);
return val;
}
static int send_file(THD *thd)
{
NET* net = &thd->net;
int fd = -1, error = 1;
size_t bytes;
char fname[FN_REFLEN+1];
const char *errmsg = 0;
int old_timeout;
unsigned long packet_len;
uchar buf[IO_SIZE]; // It's safe to alloc this
DBUG_ENTER("send_file");
/*
The client might be slow loading the data, give him wait_timeout to do
the job
*/
old_timeout= net->read_timeout;
my_net_set_read_timeout(net, thd->variables.net_wait_timeout);
/*
We need net_flush here because the client will not know it needs to send
us the file name until it has processed the load event entry
*/
if (unlikely(net_flush(net)))
{
read_error:
errmsg = "while reading file name";
goto err;
}
packet_len= my_net_read(net);
if (unlikely(packet_len == packet_error))
goto read_error;
// terminate with \0 for fn_format
*((char*)net->read_pos + packet_len) = 0;
fn_format(fname, (char*) net->read_pos + 1, "", "", 4);
// this is needed to make replicate-ignore-db
if (!strcmp(fname,"/dev/null"))
goto end;
if ((fd= mysql_file_open(key_file_send_file,
fname, O_RDONLY, MYF(0))) < 0)
{
errmsg = "on open of file";
goto err;
}
while ((long) (bytes= mysql_file_read(fd, buf, IO_SIZE, MYF(0))) > 0)
{
if (my_net_write(net, buf, bytes))
{
errmsg = "while writing data to client";
goto err;
}
}
end:
if (my_net_write(net, (uchar*) "", 0) || net_flush(net) ||
(my_net_read(net) == packet_error))
{
errmsg = "while negotiating file transfer close";
goto err;
}
error = 0;
err:
my_net_set_read_timeout(net, old_timeout);
if (fd >= 0)
mysql_file_close(fd, MYF(0));
if (errmsg)
{
sql_print_error("Failed in send_file() %s", errmsg);
DBUG_PRINT("error", ("%s", errmsg));
}
DBUG_RETURN(error);
}
/**
Internal to mysql_binlog_send() routine that recalculates checksum for
1. FD event (asserted) that needs additional arranment prior sending to slave.
2. Start_encryption_log_event whose Ignored flag is set
TODO DBUG_ASSERT can be removed if this function is used for more general cases
*/
inline void fix_checksum(enum_binlog_checksum_alg checksum_alg, String *packet,
ulong ev_offset)
{
if (checksum_alg == BINLOG_CHECKSUM_ALG_OFF ||
checksum_alg == BINLOG_CHECKSUM_ALG_UNDEF)
return;
/* recalculate the crc for this event */
uint data_len = uint4korr(packet->ptr() + ev_offset + EVENT_LEN_OFFSET);
ha_checksum crc;
DBUG_ASSERT((data_len ==
LOG_EVENT_MINIMAL_HEADER_LEN + FORMAT_DESCRIPTION_HEADER_LEN +
BINLOG_CHECKSUM_ALG_DESC_LEN + BINLOG_CHECKSUM_LEN) ||
(data_len ==
LOG_EVENT_MINIMAL_HEADER_LEN + BINLOG_CRYPTO_SCHEME_LENGTH +
BINLOG_KEY_VERSION_LENGTH + BINLOG_NONCE_LENGTH +
BINLOG_CHECKSUM_LEN));
crc= my_checksum(0, (uchar *)packet->ptr() + ev_offset, data_len -
BINLOG_CHECKSUM_LEN);
int4store(packet->ptr() + ev_offset + data_len - BINLOG_CHECKSUM_LEN, crc);
}
static user_var_entry * get_binlog_checksum_uservar(THD * thd)
{
LEX_CSTRING name= { STRING_WITH_LEN("master_binlog_checksum")};
user_var_entry *entry=
(user_var_entry*) my_hash_search(&thd->user_vars, (uchar*) name.str,
name.length);
return entry;
}
/**
Function for calling in mysql_binlog_send
to check if slave initiated checksum-handshake.
@param[in] thd THD to access a user variable
@return TRUE if handshake took place, FALSE otherwise
*/
static bool is_slave_checksum_aware(THD * thd)
{
DBUG_ENTER("is_slave_checksum_aware");
user_var_entry *entry= get_binlog_checksum_uservar(thd);
DBUG_RETURN(entry? true : false);
}
/**
Function for calling in mysql_binlog_send
to get the value of @@binlog_checksum of the master at
time of checksum-handshake.
The value tells the master whether to compute or not, and the slave
to verify or not the first artificial Rotate event's checksum.
@param[in] thd THD to access a user variable
@return value of @@binlog_checksum alg according to
@c enum enum_binlog_checksum_alg
*/
static enum enum_binlog_checksum_alg get_binlog_checksum_value_at_connect(THD * thd)
{
enum enum_binlog_checksum_alg ret;
DBUG_ENTER("get_binlog_checksum_value_at_connect");
user_var_entry *entry= get_binlog_checksum_uservar(thd);
if (!entry)
{
ret= BINLOG_CHECKSUM_ALG_UNDEF;
}
else
{
DBUG_ASSERT(entry->type == STRING_RESULT);
String str;
uint dummy_errors;
str.copy(entry->value, entry->length, &my_charset_bin, &my_charset_bin,
&dummy_errors);
ret= (enum_binlog_checksum_alg)
(find_type ((char*) str.ptr(), &binlog_checksum_typelib, 1) - 1);
DBUG_ASSERT(ret <= BINLOG_CHECKSUM_ALG_CRC32); // while it's just on CRC32 alg
}
DBUG_RETURN(ret);
}
/**
Set current_linfo
Setting current_linfo needs to be done with LOCK_thd_data to ensure that
adjust_linfo_offsets doesn't use a structure that may be deleted.
*/
void THD::set_current_linfo(LOG_INFO *linfo)
{
mysql_mutex_lock(&LOCK_thd_data);
current_linfo= linfo;
mysql_mutex_unlock(&LOCK_thd_data);
}
/*
Adjust the position pointer in the binary log file for all running slaves
SYNOPSIS
adjust_linfo_offsets()
purge_offset Number of bytes removed from start of log index file
NOTES
- This is called when doing a PURGE when we delete lines from the
index log file
REQUIREMENTS
- Before calling this function, we have to ensure that no threads are
using any binary log file before purge_offset.a
TODO
- Inform the slave threads that they should sync the position
in the binary log file with Relay_log_info::flush().
Now they sync is done for next read.
*/
static my_bool adjust_callback(THD *thd, my_off_t *purge_offset)
{
mysql_mutex_lock(&thd->LOCK_thd_data);
if (auto linfo= thd->current_linfo)
{
/*
Index file offset can be less that purge offset only if
we just started reading the index file. In that case
we have nothing to adjust
*/
if (linfo->index_file_offset < *purge_offset)
linfo->fatal= (linfo->index_file_offset != 0);
else
linfo->index_file_offset-= *purge_offset;
}
mysql_mutex_unlock(&thd->LOCK_thd_data);
return 0;
}
void adjust_linfo_offsets(my_off_t purge_offset)
{
server_threads.iterate(adjust_callback, &purge_offset);
}
static my_bool log_in_use_callback(THD *thd, const char *log_name)
{
my_bool result= 0;
mysql_mutex_lock(&thd->LOCK_thd_data);
if (auto linfo= thd->current_linfo)
result= !strcmp(log_name, linfo->log_file_name);
mysql_mutex_unlock(&thd->LOCK_thd_data);
return result;
}
bool log_in_use(const char* log_name)
{
return server_threads.iterate(log_in_use_callback, log_name);
}
bool purge_error_message(THD* thd, int res)
{
uint errcode;
if ((errcode= purge_log_get_error_code(res)) != 0)
{
my_message(errcode, ER_THD(thd, errcode), MYF(0));
return TRUE;
}
my_ok(thd);
return FALSE;
}
/**
Execute a PURGE BINARY LOGS TO <log> command.
@param thd Pointer to THD object for the client thread executing the
statement.
@param to_log Name of the last log to purge.
@retval FALSE success
@retval TRUE failure
*/
bool purge_master_logs(THD* thd, const char* to_log)
{
char search_file_name[FN_REFLEN];
if (!mysql_bin_log.is_open())
{
my_ok(thd);
return FALSE;
}
mysql_bin_log.make_log_name(search_file_name, to_log);
return purge_error_message(thd,
mysql_bin_log.purge_logs(search_file_name, 0, 1,
1, NULL));
}
/**
Execute a PURGE BINARY LOGS BEFORE <date> command.
@param thd Pointer to THD object for the client thread executing the
statement.
@param purge_time Date before which logs should be purged.
@retval FALSE success
@retval TRUE failure
*/
bool purge_master_logs_before_date(THD* thd, time_t purge_time)
{
if (!mysql_bin_log.is_open())
{
my_ok(thd);
return 0;
}
return purge_error_message(thd,
mysql_bin_log.purge_logs_before_date(purge_time));
}
void set_read_error(binlog_send_info *info, int error)
{
if (error == LOG_READ_EOF)
{
return;
}
info->error= ER_MASTER_FATAL_ERROR_READING_BINLOG;
switch (error) {
case LOG_READ_BOGUS:
info->errmsg= "bogus data in log event";
break;
case LOG_READ_TOO_LARGE:
info->errmsg= "log event entry exceeded max_allowed_packet; "
"Increase max_allowed_packet on master";
break;
case LOG_READ_IO:
info->errmsg= "I/O error reading log event";
break;
case LOG_READ_MEM:
info->errmsg= "memory allocation failed reading log event";
break;
case LOG_READ_TRUNC:
info->errmsg= "binlog truncated in the middle of event; "
"consider out of disk space on master";
break;
case LOG_READ_CHECKSUM_FAILURE:
info->errmsg= "event read from binlog did not pass crc check";
break;
case LOG_READ_DECRYPT:
info->errmsg= "event decryption failure";
break;
default:
info->errmsg= "unknown error reading log event on the master";
break;
}
}
/**
An auxiliary function for calling in mysql_binlog_send
to initialize the heartbeat timeout in waiting for a binlogged event.
@param[in] thd THD to access a user variable
@return heartbeat period an ulonglong of nanoseconds
or zero if heartbeat was not demanded by slave
*/
static ulonglong get_heartbeat_period(THD * thd)
{
bool null_value;
LEX_CSTRING name= { STRING_WITH_LEN("master_heartbeat_period")};
user_var_entry *entry=
(user_var_entry*) my_hash_search(&thd->user_vars, (uchar*) name.str,
name.length);
return entry? entry->val_int(&null_value) : 0;
}
/*
Lookup the capabilities of the slave, which it announces by setting a value
MARIA_SLAVE_CAPABILITY_XXX in @mariadb_slave_capability.
Older MariaDB slaves, and other MySQL slaves, do not set
@mariadb_slave_capability, corresponding to a capability of
MARIA_SLAVE_CAPABILITY_UNKNOWN (0).
*/
static int
get_mariadb_slave_capability(THD *thd)
{
bool null_value;
const LEX_CSTRING name= { STRING_WITH_LEN("mariadb_slave_capability") };
const user_var_entry *entry=
(user_var_entry*) my_hash_search(&thd->user_vars, (uchar*) name.str,
name.length);
return entry ?
(int)(entry->val_int(&null_value)) : MARIA_SLAVE_CAPABILITY_UNKNOWN;
}
/*
Get the value of the @slave_connect_state user variable into the supplied
String (this is the GTID connect state requested by the connecting slave).
Returns false if error (ie. slave did not set the variable and does not
want to use GTID to set start position), true if success.
*/
static bool
get_slave_connect_state(THD *thd, String *out_str)
{
bool null_value;
const LEX_CSTRING name= { STRING_WITH_LEN("slave_connect_state") };
user_var_entry *entry=
(user_var_entry*) my_hash_search(&thd->user_vars, (uchar*) name.str,
name.length);
return entry && entry->val_str(&null_value, out_str, 0) && !null_value;
}
static bool
get_slave_gtid_strict_mode(THD *thd)
{
bool null_value;
const LEX_CSTRING name= { STRING_WITH_LEN("slave_gtid_strict_mode") };
user_var_entry *entry=
(user_var_entry*) my_hash_search(&thd->user_vars, (uchar*) name.str,
name.length);
return entry && entry->val_int(&null_value) && !null_value;
}
static bool
get_slave_gtid_ignore_duplicates(THD *thd)
{
bool null_value;
const LEX_CSTRING name= { STRING_WITH_LEN("slave_gtid_ignore_duplicates") };
user_var_entry *entry=
(user_var_entry*) my_hash_search(&thd->user_vars, (uchar*) name.str,
name.length);
return entry && entry->val_int(&null_value) && !null_value;
}
/*
Get the value of the @slave_until_gtid user variable into the supplied
String (this is the GTID position specified for START SLAVE UNTIL
master_gtid_pos='xxx').
Returns false if error (ie. slave did not set the variable and is not doing
START SLAVE UNTIL mater_gtid_pos='xxx'), true if success.
*/
static bool
get_slave_until_gtid(THD *thd, String *out_str)
{
bool null_value;
const LEX_CSTRING name= { STRING_WITH_LEN("slave_until_gtid") };
user_var_entry *entry=
(user_var_entry*) my_hash_search(&thd->user_vars, (uchar*) name.str,
name.length);
return entry && entry->val_str(&null_value, out_str, 0) && !null_value;
}
/*
Function prepares and sends repliation heartbeat event.
@param net net object of THD
@param packet buffer to store the heartbeat instance
@param event_coordinates binlog file name and position of the last
real event master sent from binlog
@note
Among three essential pieces of heartbeat data Log_event::when
is computed locally.
The error to send is serious and should force terminating
the dump thread.
*/
static int send_heartbeat_event(binlog_send_info *info,
NET* net, String* packet,
const struct event_coordinates *coord,
enum enum_binlog_checksum_alg checksum_alg_arg)
{
DBUG_ENTER("send_heartbeat_event");
ulong ev_offset;
if (reset_transmit_packet(info, info->flags, &ev_offset, &info->errmsg))
DBUG_RETURN(1);
char header[LOG_EVENT_HEADER_LEN];
my_bool do_checksum= checksum_alg_arg != BINLOG_CHECKSUM_ALG_OFF &&
checksum_alg_arg != BINLOG_CHECKSUM_ALG_UNDEF;
/*
'when' (the timestamp) is set to 0 so that slave could distinguish between
real and fake Rotate events (if necessary)
*/
memset(header, 0, 4); // when
header[EVENT_TYPE_OFFSET] = HEARTBEAT_LOG_EVENT;
char* p= coord->file_name + dirname_length(coord->file_name);
size_t ident_len = strlen(p);
size_t event_len = ident_len + LOG_EVENT_HEADER_LEN +
(do_checksum ? BINLOG_CHECKSUM_LEN : 0);
int4store(header + SERVER_ID_OFFSET, global_system_variables.server_id);
int4store(header + EVENT_LEN_OFFSET, event_len);
int2store(header + FLAGS_OFFSET, 0);
int4store(header + LOG_POS_OFFSET, coord->pos); // log_pos
packet->append(header, sizeof(header));
packet->append(p, ident_len); // log_file_name
if (do_checksum)
{
char b[BINLOG_CHECKSUM_LEN];
ha_checksum crc= my_checksum(0, (uchar*) header, sizeof(header));
crc= my_checksum(crc, (uchar*) p, ident_len);
int4store(b, crc);
packet->append(b, sizeof(b));
}
if (my_net_write(net, (uchar*) packet->ptr(), packet->length()) ||
net_flush(net))
{
info->error= ER_UNKNOWN_ERROR;
DBUG_RETURN(-1);
}
DBUG_RETURN(0);
}
struct binlog_file_entry
{
binlog_file_entry *next;
LEX_CSTRING name;
my_off_t size;
};
/**
Read all binary logs and return as a list
@param memroot Use this for mem_root calls
@param reverse If set filenames returned in latest first order (reverse
order than in the index file)
@param already_locked If set, index file is already locked.
@return 0 error
# pointer to list
@notes
index_file is always unlocked at return
*/
static binlog_file_entry *
get_binlog_list(MEM_ROOT *memroot, bool reverse= true,
bool already_locked= false)
{
IO_CACHE *index_file;
char *fname, *buff, *end_pos;
binlog_file_entry *current_list= NULL, *current_link= NULL, *e;
DBUG_ENTER("get_binlog_list");
if (!mysql_bin_log.is_open())
{
if (already_locked)
mysql_bin_log.unlock_index();
my_error(ER_NO_BINARY_LOGGING, MYF(0));
DBUG_RETURN(NULL);
}
if (!already_locked)
mysql_bin_log.lock_index();
index_file=mysql_bin_log.get_index_file();
reinit_io_cache(index_file, READ_CACHE, (my_off_t) 0, 0, 0);
if (!(buff= (char*) alloc_root(memroot,
(size_t) (index_file->end_of_file+1))))
goto err;
if (my_b_read(index_file, (uchar*) buff, (size_t) index_file->end_of_file))
{
my_error(EE_READ, MYF(ME_ERROR_LOG), my_filename(index_file->file),
my_errno);
goto err;
}
buff[index_file->end_of_file]= 0; // For strchr
mysql_bin_log.unlock_index();
/* The file ends with EOF or empty line */
for (fname= buff;
(end_pos= strchr(fname, '\n')) && (end_pos - fname) > 1;
fname= end_pos+1)
{
end_pos[0]= '\0'; // remove the newline
if (!(e= (binlog_file_entry *) alloc_root(memroot, sizeof(*e))))
DBUG_RETURN(NULL);
if (reverse)
{
e->next= current_list;
current_list= e;
}
else
{
e->next= NULL;
if (!current_link)
current_list= e;
else
current_link->next= e;
current_link= e;
}
e->name.str= fname;
e->name.length= (size_t) (end_pos - fname);
}
DBUG_RETURN(current_list);
err:
mysql_bin_log.unlock_index();
DBUG_RETURN(0);
}
/*
Check if every GTID requested by the slave is contained in this (or a later)
binlog file. Return true if so, false if not.
We do the check with a single scan of the list of GTIDs, avoiding the need
to build an in-memory hash or stuff like that.
We need to check that slave did not request GTID D-S-N1, when the
Gtid_list_log_event for this binlog file has D-S-N2 with N2 >= N1.
(Because this means that requested GTID is in an earlier binlog).
However, if the Gtid_list_log_event indicates that D-S-N1 is the very last
GTID for domain D in prior binlog files, then it is ok to start from the
very start of this binlog file. This special case is important, as it
allows to purge old logs even if some domain is unused for long.
In addition, we need to check that we do not have a GTID D-S-N3 in the
Gtid_list_log_event where D is not present in the requested slave state at
all. Since if D is not in requested slave state, it means that slave needs
to start at the very first GTID in domain D.
*/
static bool
contains_all_slave_gtid(slave_connection_state *st, Gtid_list_log_event *glev)
{
uint32 i;
for (i= 0; i < glev->count; ++i)
{
uint32 gl_domain_id= glev->list[i].domain_id;
const rpl_gtid *gtid= st->find(gl_domain_id);
if (!gtid)
{
/*