-
Notifications
You must be signed in to change notification settings - Fork 713
/
Copy pathrpl_slave.cc
9981 lines (8748 loc) · 358 KB
/
rpl_slave.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. All rights reserved.
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 */
/**
@addtogroup Replication
@{
@file sql/rpl_slave.cc
@brief Code to run the io thread and the sql thread on the
replication slave.
*/
#include "sql/rpl_slave.h"
#include "my_config.h"
#include <errno.h>
#include <fcntl.h>
#include <math.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mysql/components/services/log_builtins.h"
#include "mysql/components/services/psi_memory_bits.h"
#include "mysql/components/services/psi_stage_bits.h"
#include "mysql/plugin.h"
#include "mysql/psi/mysql_cond.h"
#include "mysql/psi/mysql_mutex.h"
#include "mysql/psi/psi_base.h"
#include "mysql/status_var.h"
#include "sql/rpl_channel_service_interface.h"
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <time.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <algorithm>
#include <atomic>
#include <chrono>
#include <map>
#include <string>
#include <utility>
#include <vector>
#include "binary_log_types.h"
#include "binlog_event.h"
#include "control_events.h"
#include "debug_vars.h"
#include "errmsg.h" // CR_*
#include "lex_string.h"
#include "m_ctype.h"
#include "m_string.h"
#include "my_bitmap.h" // MY_BITMAP
#include "my_byteorder.h"
#include "my_command.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_dir.h"
#include "my_io.h"
#include "my_loglevel.h"
#include "my_macros.h"
#include "my_sys.h"
#include "my_systime.h"
#include "my_thread_local.h" // thread_local_key_t
#include "mysql.h" // MYSQL
#include "mysql/psi/mysql_file.h"
#include "mysql/psi/mysql_memory.h"
#include "mysql/psi/mysql_thread.h"
#include "mysql/service_mysql_alloc.h"
#include "mysql/thread_type.h"
#include "mysql_com.h"
#include "mysqld_error.h"
#include "mysys_err.h"
#include "pfs_thread_provider.h"
#include "prealloced_array.h"
#include "sql/auth/auth_acls.h"
#include "sql/auth/sql_security_ctx.h"
#include "sql/binlog.h"
#include "sql/binlog_reader.h"
#include "sql/current_thd.h"
#include "sql/debug_sync.h" // DEBUG_SYNC
#include "sql/derror.h" // ER_THD
#include "sql/dynamic_ids.h" // Server_ids
#include "sql/handler.h"
#include "sql/item.h"
#include "sql/log.h"
#include "sql/log_event.h" // Rotate_log_event
#include "sql/mdl.h"
#include "sql/mysqld.h" // ER
#include "sql/mysqld_thd_manager.h" // Global_THD_manager
#include "sql/protocol.h"
#include "sql/protocol_classic.h"
#include "sql/psi_memory_key.h"
#include "sql/query_options.h"
#include "sql/rpl_applier_reader.h"
#include "sql/rpl_filter.h"
#include "sql/rpl_group_replication.h"
#include "sql/rpl_gtid.h"
#include "sql/rpl_handler.h" // RUN_HOOK
#include "sql/rpl_info.h"
#include "sql/rpl_info_factory.h" // Rpl_info_factory
#include "sql/rpl_info_handler.h"
#include "sql/rpl_mi.h"
#include "sql/rpl_msr.h" // Multisource_info
#include "sql/rpl_mts_submode.h"
#include "sql/rpl_reporting.h"
#include "sql/rpl_rli.h" // Relay_log_info
#include "sql/rpl_rli_pdb.h" // Slave_worker
#include "sql/rpl_slave_commit_order_manager.h" // Commit_order_manager
#include "sql/rpl_slave_until_options.h"
#include "sql/rpl_trx_boundary_parser.h"
#include "sql/rpl_utility.h"
#include "sql/sql_backup_lock.h" // is_instance_backup_locked
#include "sql/sql_class.h" // THD
#include "sql/sql_const.h"
#include "sql/sql_error.h"
#include "sql/sql_lex.h"
#include "sql/sql_list.h"
#include "sql/sql_parse.h" // execute_init_command
#include "sql/sql_plugin.h" // opt_plugin_dir_ptr
#include "sql/system_variables.h"
#include "sql/table.h"
#include "sql/transaction.h" // trans_begin
#include "sql/transaction_info.h"
#include "sql_common.h" // end_server
#include "sql_string.h"
#include "typelib.h"
struct mysql_cond_t;
struct mysql_mutex_t;
using binary_log::Log_event_header;
using binary_log::checksum_crc32;
using std::max;
using std::min;
using namespace std::chrono;
#define FLAGSTR(V, F) ((V) & (F) ? #F " " : "")
/*
a parameter of sql_slave_killed() to defer the killed status
*/
#define SLAVE_WAIT_GROUP_DONE 60
bool use_slave_mask = 0;
MY_BITMAP slave_error_mask;
char slave_skip_error_names[SHOW_VAR_FUNC_BUFF_SIZE];
char *slave_load_tmpdir = 0;
bool replicate_same_server_id;
ulonglong relay_log_space_limit = 0;
uint rpl_receive_buffer_size = 0;
bool reset_seconds_behind_master = true;
const char *relay_log_index = 0;
const char *relay_log_basename = 0;
/*
MTS load-ballancing parameter.
Max length of one MTS Worker queue. The value also determines the size
of Relay_log_info::gaq (see @c slave_start_workers()).
It can be set to any value in [1, ULONG_MAX - 1] range.
*/
const ulong mts_slave_worker_queue_len_max = 16384;
/*
Statistics go to the error log every # of seconds when
--log_error_verbosity > 2
*/
const long mts_online_stat_period = 60 * 2;
/*
MTS load-ballancing parameter.
Time unit in microsecs to sleep by MTS Coordinator to avoid extra thread
signalling in the case of Worker queues are close to be filled up.
*/
const ulong mts_coordinator_basic_nap = 5;
/*
MTS load-ballancing parameter.
Percent of Worker queue size at which Worker is considered to become
hungry.
C enqueues --+ . underrun level
V "
+----------+-+------------------+--------------+
| empty |.|::::::::::::::::::|xxxxxxxxxxxxxx| ---> Worker dequeues
+----------+-+------------------+--------------+
Like in the above diagram enqueuing to the x-d area would indicate
actual underrruning by Worker.
*/
const ulong mts_worker_underrun_level = 10;
/*
When slave thread exits, we need to remember the temporary tables so we
can re-use them on slave start.
TODO: move the vars below under Master_info
*/
int disconnect_slave_event_count = 0, abort_slave_event_count = 0;
static thread_local Master_info *RPL_MASTER_INFO = nullptr;
enum enum_slave_reconnect_actions {
SLAVE_RECON_ACT_REG = 0,
SLAVE_RECON_ACT_DUMP = 1,
SLAVE_RECON_ACT_EVENT = 2,
SLAVE_RECON_ACT_MAX
};
enum enum_slave_reconnect_messages {
SLAVE_RECON_MSG_WAIT = 0,
SLAVE_RECON_MSG_KILLED_WAITING = 1,
SLAVE_RECON_MSG_AFTER = 2,
SLAVE_RECON_MSG_FAILED = 3,
SLAVE_RECON_MSG_COMMAND = 4,
SLAVE_RECON_MSG_KILLED_AFTER = 5,
SLAVE_RECON_MSG_MAX
};
static const char *reconnect_messages[SLAVE_RECON_ACT_MAX][SLAVE_RECON_MSG_MAX] =
{{"Waiting to reconnect after a failed registration on master",
"Slave I/O thread killed while waiting to reconnect after a failed \
registration on master",
"Reconnecting after a failed registration on master",
"failed registering on master, reconnecting to try again, \
log '%s' at position %s",
"COM_REGISTER_SLAVE",
"Slave I/O thread killed during or after reconnect"},
{"Waiting to reconnect after a failed binlog dump request",
"Slave I/O thread killed while retrying master dump",
"Reconnecting after a failed binlog dump request",
"failed dump request, reconnecting to try again, log '%s' at position %s",
"COM_BINLOG_DUMP", "Slave I/O thread killed during or after reconnect"},
{"Waiting to reconnect after a failed master event read",
"Slave I/O thread killed while waiting to reconnect after a failed read",
"Reconnecting after a failed master event read",
"Slave I/O thread: Failed reading log event, reconnecting to retry, \
log '%s' at position %s",
"",
"Slave I/O thread killed during or after a reconnect done to recover from \
failed read"}};
enum enum_slave_apply_event_and_update_pos_retval {
SLAVE_APPLY_EVENT_AND_UPDATE_POS_OK = 0,
SLAVE_APPLY_EVENT_AND_UPDATE_POS_APPLY_ERROR = 1,
SLAVE_APPLY_EVENT_AND_UPDATE_POS_UPDATE_POS_ERROR = 2,
SLAVE_APPLY_EVENT_AND_UPDATE_POS_APPEND_JOB_ERROR = 3,
SLAVE_APPLY_EVENT_AND_UPDATE_POS_MAX
};
static int process_io_rotate(Master_info *mi, Rotate_log_event *rev);
static bool wait_for_relay_log_space(Relay_log_info *rli);
static inline bool io_slave_killed(THD *thd, Master_info *mi);
static inline bool is_autocommit_off_and_infotables(THD *thd);
static int init_slave_thread(THD *thd, SLAVE_THD_TYPE thd_type);
static void print_slave_skip_errors(void);
static int safe_connect(THD *thd, MYSQL *mysql, Master_info *mi);
static int safe_reconnect(THD *thd, MYSQL *mysql, Master_info *mi,
bool suppress_warnings);
static int connect_to_master(THD *thd, MYSQL *mysql, Master_info *mi,
bool reconnect, bool suppress_warnings);
static int get_master_version_and_clock(MYSQL *mysql, Master_info *mi);
static int get_master_uuid(MYSQL *mysql, Master_info *mi);
int io_thread_init_commands(MYSQL *mysql, Master_info *mi);
static int terminate_slave_thread(THD *thd, mysql_mutex_t *term_lock,
mysql_cond_t *term_cond,
std::atomic<uint> *slave_running,
ulong *stop_wait_timeout, bool need_lock_term,
bool force = false);
static bool check_io_slave_killed(THD *thd, Master_info *mi, const char *info);
static int mts_event_coord_cmp(LOG_POS_COORD *id1, LOG_POS_COORD *id2);
static int check_slave_sql_config_conflict(const Relay_log_info *rli);
/*
Applier thread InnoDB priority.
When two transactions conflict inside InnoDB, the one with
greater priority wins.
@param thd Thread handler for slave
@param priority Thread priority
*/
static void set_thd_tx_priority(THD *thd, int priority) {
DBUG_ENTER("set_thd_tx_priority");
DBUG_ASSERT(thd->system_thread == SYSTEM_THREAD_SLAVE_SQL ||
thd->system_thread == SYSTEM_THREAD_SLAVE_WORKER);
thd->thd_tx_priority = priority;
DBUG_EXECUTE_IF("dbug_set_high_prio_sql_thread",
{ thd->thd_tx_priority = 1; });
DBUG_VOID_RETURN;
}
/*
Function to set the slave's max_allowed_packet based on the value
of slave_max_allowed_packet.
@in_param thd Thread handler for slave
@in_param mysql MySQL connection handle
*/
static void set_slave_max_allowed_packet(THD *thd, MYSQL *mysql) {
DBUG_ENTER("set_slave_max_allowed_packet");
// thd and mysql must be valid
DBUG_ASSERT(thd && mysql);
thd->variables.max_allowed_packet = slave_max_allowed_packet;
/*
Adding MAX_LOG_EVENT_HEADER_LEN to the max_packet_size on the I/O
thread and the mysql->option max_allowed_packet, since a
replication event can become this much larger than
the corresponding packet (query) sent from client to master.
*/
thd->get_protocol_classic()->set_max_packet_size(slave_max_allowed_packet +
MAX_LOG_EVENT_HEADER);
/*
Skipping the setting of mysql->net.max_packet size to slave
max_allowed_packet since this is done during mysql_real_connect.
*/
mysql->options.max_allowed_packet =
slave_max_allowed_packet + MAX_LOG_EVENT_HEADER;
DBUG_VOID_RETURN;
}
/*
Find out which replications threads are running
SYNOPSIS
init_thread_mask()
mask Return value here
mi master_info for slave
inverse If set, returns which threads are not running
IMPLEMENTATION
Get a bit mask for which threads are running so that we can later restart
these threads.
RETURN
mask If inverse == 0, running threads
If inverse == 1, stopped threads
*/
void init_thread_mask(int *mask, Master_info *mi, bool inverse) {
bool set_io = mi->slave_running, set_sql = mi->rli->slave_running;
int tmp_mask = 0;
DBUG_ENTER("init_thread_mask");
if (set_io) tmp_mask |= SLAVE_IO;
if (set_sql) tmp_mask |= SLAVE_SQL;
if (inverse) tmp_mask ^= (SLAVE_IO | SLAVE_SQL);
*mask = tmp_mask;
DBUG_VOID_RETURN;
}
/*
lock_slave_threads()
*/
void lock_slave_threads(Master_info *mi) {
DBUG_ENTER("lock_slave_threads");
// TODO: see if we can do this without dual mutex
mysql_mutex_lock(&mi->run_lock);
mysql_mutex_lock(&mi->rli->run_lock);
DBUG_VOID_RETURN;
}
/*
unlock_slave_threads()
*/
void unlock_slave_threads(Master_info *mi) {
DBUG_ENTER("unlock_slave_threads");
// TODO: see if we can do this without dual mutex
mysql_mutex_unlock(&mi->rli->run_lock);
mysql_mutex_unlock(&mi->run_lock);
DBUG_VOID_RETURN;
}
#ifdef HAVE_PSI_INTERFACE
static PSI_memory_key key_memory_rli_mts_coor;
static PSI_thread_key key_thread_slave_io, key_thread_slave_sql,
key_thread_slave_worker;
static PSI_thread_info all_slave_threads[] = {
{&key_thread_slave_io, "slave_io", PSI_FLAG_SINGLETON, 0, PSI_DOCUMENT_ME},
{&key_thread_slave_sql, "slave_sql", PSI_FLAG_SINGLETON, 0,
PSI_DOCUMENT_ME},
{&key_thread_slave_worker, "slave_worker", PSI_FLAG_SINGLETON, 0,
PSI_DOCUMENT_ME}};
static PSI_memory_info all_slave_memory[] = {{&key_memory_rli_mts_coor,
"Relay_log_info::mts_coor", 0, 0,
PSI_DOCUMENT_ME}};
static void init_slave_psi_keys(void) {
const char *category = "sql";
int count;
count = static_cast<int>(array_elements(all_slave_threads));
mysql_thread_register(category, all_slave_threads, count);
count = static_cast<int>(array_elements(all_slave_memory));
mysql_memory_register(category, all_slave_memory, count);
}
#endif /* HAVE_PSI_INTERFACE */
static bool configured_as_slave() {
channel_map.assert_some_lock();
for (mi_map::iterator it = channel_map.begin(); it != channel_map.end();
it++) {
if (Master_info::is_configured(it->second)) {
return true;
}
}
return false;
}
/* Initialize slave structures */
int init_slave() {
DBUG_ENTER("init_slave");
int error = 0;
int thread_mask = SLAVE_SQL | SLAVE_IO;
Master_info *mi = NULL;
#ifdef HAVE_PSI_INTERFACE
init_slave_psi_keys();
#endif
/*
This is called when mysqld starts. Before client connections are
accepted. However bootstrap may conflict with us if it does START SLAVE.
So it's safer to take the lock.
*/
channel_map.wrlock();
RPL_MASTER_INFO = nullptr;
/*
Create slave info objects by reading repositories of individual
channels and add them into channel_map
*/
if ((error = Rpl_info_factory::create_slave_info_objects(
opt_mi_repository_id, opt_rli_repository_id, thread_mask,
&channel_map)))
LogErr(ERROR_LEVEL,
ER_RPL_SLAVE_FAILED_TO_CREATE_OR_RECOVER_INFO_REPOSITORIES);
#ifndef DBUG_OFF
/* @todo: Print it for all the channels */
{
Master_info *default_mi;
default_mi = channel_map.get_default_channel_mi();
if (default_mi && default_mi->rli) {
DBUG_PRINT("info",
("init group master %s %lu group relay %s %lu event %s %lu\n",
default_mi->rli->get_group_master_log_name(),
(ulong)default_mi->rli->get_group_master_log_pos(),
default_mi->rli->get_group_relay_log_name(),
(ulong)default_mi->rli->get_group_relay_log_pos(),
default_mi->rli->get_event_relay_log_name(),
(ulong)default_mi->rli->get_event_relay_log_pos()));
}
}
#endif
is_slave = configured_as_slave();
/**
If engine binlog max gtid is set, then update recovery_max_engine_gtid.
recovery_max_engine_gtid is used later during slave's idempotent
recovery/apply of binnlog events.
engine_binlog_max_gtid is set during storage engine recovery using
global_sid_map. However, idempotent recovery/apply uses
rli->recovery_sid_map. Hence rli->recovery_max_engine_gtid needs to be
initialized by hashing into rli->recovery_sid_map
NOTE: idempotent recovery requires tables have unique key to work correctly
*/
if (!mysql_bin_log.engine_binlog_max_gtid.is_empty()) {
char buf[Gtid::MAX_TEXT_LENGTH + 1];
/* Extract engine_binlog_max_gtid using global_sid_map */
mysql_bin_log.engine_binlog_max_gtid.to_string(global_sid_map, buf,
/* need_lock */ true);
/* Now set rli->recovery_max_engine_gtid (and optionally add
it into rli->recovery_sid_map */
for (const auto &channel : channel_map) {
channel.second->rli->recovery_sid_lock.rdlock();
channel.second->rli->recovery_max_engine_gtid.parse(
&channel.second->rli->recovery_sid_map, buf);
channel.second->rli->recovery_sid_lock.unlock();
}
}
if (is_slave && mysql_bin_log.engine_binlog_pos != ULLONG_MAX &&
mysql_bin_log.engine_binlog_file[0] &&
get_gtid_mode(GTID_MODE_LOCK_CHANNEL_MAP) != GTID_MODE_OFF) {
/*
With less durable settins (sync_binlog !=1 and
innodb_flush_log_at_trx_commit !=1), a slave with GTIDs/MTS
may be inconsistent due to the two possible scenarios below
1) slave's binary log is behind innodb transaction log.
2) slave's binlary log is ahead of innodb transaction log.
The engine_binlog_max_gtid in innodb trx log stores
max gtid executed in engine and handles scenario 1. But in case
of scenario 2, slave will skip executing some transactions if it's
GTID is logged in the binlog even though it is not commiited in
innodb.
Scenario 2 is handled by changing gtid_executed when a
slave is initialized based on the binlog file and binlog position
which are logged inside innodb trx log. When gtid_executed is set
to an old value which is consistent with innodb, slave doesn't
miss any transactions.
*/
mysql_mutex_t *log_lock = mysql_bin_log.get_log_lock();
mysql_mutex_lock(log_lock);
global_sid_lock->wrlock();
char file_name[FN_REFLEN + 1];
mysql_bin_log.make_log_name(file_name, mysql_bin_log.engine_binlog_file);
char *binlog_gtid_set_buffer = nullptr;
gtid_state->get_executed_gtids()->to_string(&binlog_gtid_set_buffer);
const_cast<Gtid_set *>(gtid_state->get_executed_gtids())->clear();
MYSQL_BIN_LOG::enum_read_gtids_from_binlog_status ret =
mysql_bin_log.read_gtids_from_binlog(
file_name, const_cast<Gtid_set *>(gtid_state->get_executed_gtids()),
NULL, NULL, global_sid_map, opt_master_verify_checksum, false,
mysql_bin_log.engine_binlog_pos);
if (ret == MYSQL_BIN_LOG::ERROR || ret == MYSQL_BIN_LOG::TRUNCATED) {
global_sid_lock->unlock();
mysql_mutex_unlock(log_lock);
my_free(binlog_gtid_set_buffer);
sql_print_error("Fail to read binlog");
error = 1;
goto err;
}
char *trx_gtid_set_buffer = nullptr;
gtid_state->get_executed_gtids()->to_string(&trx_gtid_set_buffer);
sql_print_information("Resetting GTID_EXECUTED: old : %s, new: %s",
binlog_gtid_set_buffer, trx_gtid_set_buffer);
my_free(binlog_gtid_set_buffer);
my_free(trx_gtid_set_buffer);
global_sid_lock->unlock();
// rotate writes the consistent gtid_executed as previous_gtid_log_event
// in next binlog. This is done to avoid situations where there is a
// slave crash immediately after executing some relay log events.
// Those slave crashes are not safe if binlog is not rotated since the
// gtid_executed set after crash recovery will be inconsistent with InnoDB.
// A crash before this rotate is safe because of valid binlog file and
// position values inside innodb trx header which will not be updated
// until sql_thread is ready.
bool check_purge;
mysql_bin_log.rotate(true, &check_purge);
mysql_mutex_unlock(log_lock);
if (ret == MYSQL_BIN_LOG::ERROR || ret == MYSQL_BIN_LOG::TRUNCATED) {
sql_print_error(
"Failed to read log %s up to pos %llu "
"to find out crash safe gtid_executed "
"Replication will not be setup due to "
"possible data inconsistency with master. ",
mysql_bin_log.engine_binlog_file, mysql_bin_log.engine_binlog_pos);
error = 1;
goto err;
}
}
if (get_gtid_mode(GTID_MODE_LOCK_CHANNEL_MAP) == GTID_MODE_OFF) {
for (mi_map::iterator it = channel_map.begin(); it != channel_map.end();
it++) {
Master_info *mi = it->second;
if (mi != NULL && mi->is_auto_position()) {
LogErr(WARNING_LEVEL,
ER_RPL_SLAVE_AUTO_POSITION_IS_1_AND_GTID_MODE_IS_OFF,
mi->get_channel(), mi->get_channel());
}
}
}
if (check_slave_sql_config_conflict(NULL)) {
error = 1;
goto err;
}
/*
Loop through the channel_map and start slave threads for each channel.
*/
if (!opt_skip_slave_start) {
for (mi_map::iterator it = channel_map.begin(); it != channel_map.end();
it++) {
mi = it->second;
/* If server id is not set, start_slave_thread() will say it */
if (Master_info::is_configured(mi) && mi->rli->inited) {
/* same as in start_slave() cache the global var values into rli's
* members */
mi->rli->opt_slave_parallel_workers = opt_mts_slave_parallel_workers;
mi->rli->checkpoint_group = opt_mts_checkpoint_group;
if (mts_parallel_option == MTS_PARALLEL_TYPE_DB_NAME)
mi->rli->channel_mts_submode = MTS_PARALLEL_TYPE_DB_NAME;
else
mi->rli->channel_mts_submode = MTS_PARALLEL_TYPE_LOGICAL_CLOCK;
if (start_slave_threads(true /*need_lock_slave=true*/,
false /*wait_for_start=false*/, mi,
thread_mask)) {
LogErr(ERROR_LEVEL, ER_FAILED_TO_START_SLAVE_THREAD,
mi->get_channel());
}
} else {
LogErr(INFORMATION_LEVEL, ER_FAILED_TO_START_SLAVE_THREAD,
mi->get_channel());
}
}
}
err:
channel_map.unlock();
if (error) LogErr(INFORMATION_LEVEL, ER_SLAVE_NOT_STARTED_ON_SOME_CHANNELS);
DBUG_RETURN(error);
}
/**
Function to start a slave for all channels.
Used in Multisource replication.
@param[in] thd THD object of the client.
@retval false success
@retval true error
@todo It is good to continue to start other channels
when a slave start failed for other channels.
@todo The problem with the below code is if the slave is already
stared, we would multple warnings called
"Slave was already running" for each channel.
A nice warning message would be to add
"Slave for channel '%s" was already running"
but error messages are in different languages and cannot be tampered
with so, we have to handle it case by case basis, whether
only default channel exists or not and properly continue with
starting other channels if one channel fails clearly giving
an error message by displaying failed channels.
*/
bool start_slave(THD *thd) {
DBUG_ENTER("start_slave(THD)");
Master_info *mi;
bool channel_configured, error = false;
if (channel_map.get_num_instances() == 1) {
mi = channel_map.get_default_channel_mi();
DBUG_ASSERT(mi);
if (start_slave(thd, &thd->lex->slave_connection, &thd->lex->mi,
thd->lex->slave_thd_opt, mi, true))
DBUG_RETURN(true);
} else {
/*
Users cannot start more than one channel's applier thread
if sql_slave_skip_counter > 0. It throws an error to the session.
*/
mysql_mutex_lock(&LOCK_sql_slave_skip_counter);
/* sql_slave_skip_counter > 0 && !(START SLAVE IO_THREAD) */
if (sql_slave_skip_counter > 0 && !(thd->lex->slave_thd_opt & SLAVE_IO)) {
my_error(ER_SLAVE_CHANNEL_SQL_SKIP_COUNTER, MYF(0));
mysql_mutex_unlock(&LOCK_sql_slave_skip_counter);
DBUG_RETURN(true);
}
mysql_mutex_unlock(&LOCK_sql_slave_skip_counter);
for (mi_map::iterator it = channel_map.begin(); it != channel_map.end();
it++) {
mi = it->second;
channel_configured =
mi && // Master_info exists
(mi->inited || mi->reset) // It is inited or was reset
&& mi->host[0]; // host is set
if (channel_configured) {
if (start_slave(thd, &thd->lex->slave_connection, &thd->lex->mi,
thd->lex->slave_thd_opt, mi, true)) {
LogErr(ERROR_LEVEL, ER_RPL_SLAVE_CANT_START_SLAVE_FOR_CHANNEL,
mi->get_channel());
error = true;
}
}
}
}
if (!error) {
/* no error */
my_ok(thd);
}
DBUG_RETURN(error);
}
/**
Function to stop a slave for all channels.
Used in Multisource replication.
@param[in] thd THD object of the client.
@return
@retval 0 success
@retval 1 error
@todo It is good to continue to stop other channels
when a slave start failed for other channels.
*/
int stop_slave(THD *thd) {
DBUG_ENTER("stop_slave(THD)");
bool push_temp_table_warning = true;
Master_info *mi = 0;
int error = 0;
if (channel_map.get_num_instances() == 1) {
mi = channel_map.get_default_channel_mi();
DBUG_ASSERT(!strcmp(mi->get_channel(), channel_map.get_default_channel()));
error = stop_slave(thd, mi, 1, false /*for_one_channel*/,
&push_temp_table_warning);
} else {
for (mi_map::iterator it = channel_map.begin(); it != channel_map.end();
it++) {
mi = it->second;
if (Master_info::is_configured(mi)) {
if (stop_slave(thd, mi, 1, false /*for_one_channel*/,
&push_temp_table_warning)) {
LogErr(ERROR_LEVEL, ER_RPL_SLAVE_CANT_STOP_SLAVE_FOR_CHANNEL,
mi->get_channel());
error = 1;
}
}
}
}
if (!error) {
/* no error */
my_ok(thd);
}
DBUG_RETURN(error);
}
void get_master_sidno(THD *thd, rpl_sidno &master_sidno) {
// We should not purge gtids received from current active master.
channel_map.wrlock();
const auto lex = thd->lex;
const auto active_mi = channel_map.get_mi(
lex->mi.channel ? lex->mi.channel : channel_map.get_default_channel());
if (active_mi && active_mi->master_uuid[0]) {
global_sid_lock->rdlock();
rpl_sid master_sid;
if (master_sid.parse(active_mi->master_uuid,
strlen(active_mi->master_uuid)))
DBUG_ASSERT(false);
master_sidno = global_sid_map->sid_to_sidno(master_sid);
global_sid_lock->unlock();
}
channel_map.unlock();
}
/**
Entry point to the START SLAVE command. The function
decides to start replication threads on several channels
or a single given channel.
@param[in] thd the client thread carrying the command.
@return
@retval false ok
@retval true not ok.
*/
bool start_slave_cmd(THD *thd) {
DBUG_ENTER("start_slave_cmd");
Master_info *mi;
LEX *lex = thd->lex;
bool res = true; /* default, an error */
channel_map.wrlock();
if (!is_slave_configured()) {
my_error(ER_SLAVE_CONFIGURATION, MYF(0));
goto err;
}
if (!lex->mi.for_channel) {
/*
If slave_until options are provided when multiple channels exist
without explicitly providing FOR CHANNEL clause, error out.
*/
if (lex->mi.slave_until && channel_map.get_num_instances() > 1) {
my_error(ER_SLAVE_MULTIPLE_CHANNELS_CMD, MYF(0));
goto err;
}
res = start_slave(thd);
} else {
mi = channel_map.get_mi(lex->mi.channel);
/*
If the channel being used is a group replication channel we need to
disable this command here as, in some cases, group replication does not
support them.
For channel group_replication_applier we disable START SLAVE [IO_THREAD]
command.
For channel group_replication_recovery we disable START SLAVE command
and its two thread variants.
*/
if (mi &&
channel_map.is_group_replication_channel_name(mi->get_channel()) &&
((!thd->lex->slave_thd_opt || (thd->lex->slave_thd_opt & SLAVE_IO)) ||
(!(channel_map.is_group_replication_channel_name(mi->get_channel(),
true)) &&
(thd->lex->slave_thd_opt & SLAVE_SQL)))) {
const char *command = "START SLAVE FOR CHANNEL";
if (thd->lex->slave_thd_opt & SLAVE_IO)
command = "START SLAVE IO_THREAD FOR CHANNEL";
else if (thd->lex->slave_thd_opt & SLAVE_SQL)
command = "START SLAVE SQL_THREAD FOR CHANNEL";
my_error(ER_SLAVE_CHANNEL_OPERATION_NOT_ALLOWED, MYF(0), command,
mi->get_channel(), command);
goto err;
}
if (mi)
res = start_slave(thd, &thd->lex->slave_connection, &thd->lex->mi,
thd->lex->slave_thd_opt, mi, true);
else if (strcmp(channel_map.get_default_channel(), lex->mi.channel))
my_error(ER_SLAVE_CHANNEL_DOES_NOT_EXIST, MYF(0), lex->mi.channel);
if (!res) my_ok(thd);
}
err:
channel_map.unlock();
DBUG_RETURN(res);
}
/**
Entry point for the STOP SLAVE command. This function stops replication
threads for all channels or a single channel based on the command
options supplied.
@param[in] thd the client thread.
@return
@retval false ok
@retval true not ok.
*/
bool stop_slave_cmd(THD *thd) {
DBUG_ENTER("stop_slave_cmd");
Master_info *mi;
bool push_temp_table_warning = true;
LEX *lex = thd->lex;
bool res = true; /*default, an error */
channel_map.rdlock();
if (!is_slave_configured()) {
my_error(ER_SLAVE_CONFIGURATION, MYF(0));
channel_map.unlock();
DBUG_RETURN(res = true);
}
if (!lex->mi.for_channel)
res = stop_slave(thd);
else {
mi = channel_map.get_mi(lex->mi.channel);
/*
If the channel being used is a group replication channel we need to
disable this command here as, in some cases, group replication does not
support them.
For channel group_replication_applier we disable STOP SLAVE [IO_THREAD]
command.
For channel group_replication_recovery we disable STOP SLAVE command
and its two thread variants.
*/
if (mi &&
channel_map.is_group_replication_channel_name(mi->get_channel()) &&
((!thd->lex->slave_thd_opt || (thd->lex->slave_thd_opt & SLAVE_IO)) ||
(!(channel_map.is_group_replication_channel_name(mi->get_channel(),
true)) &&
(thd->lex->slave_thd_opt & SLAVE_SQL)))) {
const char *command = "STOP SLAVE FOR CHANNEL";
if (thd->lex->slave_thd_opt & SLAVE_IO)
command = "STOP SLAVE IO_THREAD FOR CHANNEL";
else if (thd->lex->slave_thd_opt & SLAVE_SQL)
command = "STOP SLAVE SQL_THREAD FOR CHANNEL";
my_error(ER_SLAVE_CHANNEL_OPERATION_NOT_ALLOWED, MYF(0), command,
mi->get_channel(), command);
channel_map.unlock();
DBUG_RETURN(true);
}
if (mi)
res = stop_slave(thd, mi, 1 /*net report */, true /*for_one_channel*/,
&push_temp_table_warning);
else if (strcmp(channel_map.get_default_channel(), lex->mi.channel))
my_error(ER_SLAVE_CHANNEL_DOES_NOT_EXIST, MYF(0), lex->mi.channel);
}
channel_map.unlock();
DBUG_RETURN(res);
}
/**
Parse the given relay log and identify the rotate event from the master.
Ignore the Format description event, Previous_gtid log event and ignorable
events within the relay log. When a rotate event is found check if it is a
rotate that is originated from the master or not based on the server_id. If
the rotate is from slave or if it is a fake rotate event ignore the event.
If any other events are encountered apart from the above events generate an
error. From the rotate event extract the master's binary log name and
position.
@param filename
Relay log name which needs to be parsed.
@param[out] master_log_file
Set the master_log_file to the log file name that is extracted from
rotate event. The master_log_file should contain string of len
FN_REFLEN.
@param[out] master_log_pos
Set the master_log_pos to the log position extracted from rotate
event.
@retval FOUND_ROTATE: When rotate event is found in the relay log
@retval NOT_FOUND_ROTATE: When rotate event is not found in the relay log
@retval ERROR: On error
*/
enum enum_read_rotate_from_relay_log_status {
FOUND_ROTATE,
NOT_FOUND_ROTATE,
ERROR
};
static enum_read_rotate_from_relay_log_status read_rotate_from_relay_log(
char *filename, char *master_log_file, my_off_t *master_log_pos) {
DBUG_ENTER("read_rotate_from_relay_log");
Relaylog_file_reader relaylog_file_reader(opt_slave_sql_verify_checksum);
if (relaylog_file_reader.open(filename)) {
LogErr(ERROR_LEVEL, ER_RPL_RECOVERY_ERROR,
relaylog_file_reader.get_error_str());
DBUG_RETURN(ERROR);
}
Log_event *ev = NULL;
bool done = false;