/
rpl_slave.cc
9003 lines (8071 loc) · 311 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, 2015, 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 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,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
/**
@addtogroup Replication
@{
@file
@brief Code to run the io thread and the sql thread on the
replication slave.
*/
#include "sql_priv.h"
#include "my_global.h"
#include "rpl_slave.h"
#include "sql_parse.h" // execute_init_command
#include "sql_table.h" // mysql_rm_table
#include "rpl_mi.h"
#include "rpl_rli.h"
#include "rpl_filter.h"
#include "rpl_info_factory.h"
#include "transaction.h"
#include <thr_alarm.h>
#include <my_dir.h>
#include <sql_common.h>
#include <errmsg.h>
#include <mysqld_error.h>
#include <mysys_err.h>
#include "rpl_handler.h"
#include "rpl_info_dummy.h"
#include <signal.h>
#include <mysql.h>
#include <myisam.h>
#include "sql_base.h" // close_thread_tables
#include "tztime.h" // struct Time_zone
#include "log_event.h" // Rotate_log_event,
// Create_file_log_event,
// Format_description_log_event
#include "dynamic_ids.h"
#include "rpl_rli_pdb.h"
#include "global_threads.h"
#ifdef HAVE_REPLICATION
#include "rpl_tblmap.h"
#include "debug_sync.h"
using std::min;
using std::max;
#define FLAGSTR(V,F) ((V)&(F)?#F" ":"")
#define MAX_SLAVE_RETRY_PAUSE 5
/*
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];
static unsigned long stop_wait_timeout;
char* slave_load_tmpdir = 0;
Master_info *active_mi= 0;
my_bool replicate_same_server_id;
ulonglong relay_log_space_limit = 0;
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-warnings > 1
*/
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;
Slave_job_item * de_queue(Slave_jobs_queue *jobs, Slave_job_item *ret);
bool append_item_to_jobs(slave_job_item *job_item,
Slave_worker *w, Relay_log_info *rli);
/*
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 pthread_key(Master_info*, RPL_MASTER_INFO);
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 waitnig 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 int process_io_create_file(Master_info* mi, Create_file_log_event* cev);
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 sql_slave_killed(THD* thd,Relay_log_info* rli);
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 Log_event* next_event(Relay_log_info* rli);
static int queue_event(Master_info* mi,const char* buf,ulong event_len);
static void set_stop_slave_wait_timeout(unsigned long wait_timeout);
static int terminate_slave_thread(THD *thd,
mysql_mutex_t *term_lock,
mysql_cond_t *term_cond,
volatile uint *slave_running,
bool need_lock_term);
static bool check_io_slave_killed(THD *thd, Master_info *mi, const char *info);
int slave_worker_exec_job(Slave_worker * w, Relay_log_info *rli);
static int mts_event_coord_cmp(LOG_POS_COORD *id1, LOG_POS_COORD *id2);
/*
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;
thd->net.max_packet_size= 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->net.max_packet_size+= 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;
register 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_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_GLOBAL},
{ &key_thread_slave_sql, "slave_sql", PSI_FLAG_GLOBAL},
{ &key_thread_slave_worker, "slave_worker", PSI_FLAG_GLOBAL}
};
static void init_slave_psi_keys(void)
{
const char* category= "sql";
int count;
count= array_elements(all_slave_threads);
mysql_thread_register(category, all_slave_threads, count);
}
#endif /* HAVE_PSI_INTERFACE */
/* Initialize slave structures */
int init_slave()
{
DBUG_ENTER("init_slave");
int error= 0;
int thread_mask= SLAVE_SQL | SLAVE_IO;
Relay_log_info* rli= 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.
*/
mysql_mutex_lock(&LOCK_active_mi);
if (pthread_key_create(&RPL_MASTER_INFO, NULL))
DBUG_RETURN(1);
if ((error= Rpl_info_factory::create_coordinators(opt_mi_repository_id, &active_mi,
opt_rli_repository_id, &rli)))
{
sql_print_error("Failed to create or recover replication info repository.");
error= 1;
goto err;
}
/*
This is the startup routine and as such we try to
configure both the SLAVE_SQL and SLAVE_IO.
*/
if (global_init_info(active_mi, true, thread_mask))
{
sql_print_error("Failed to initialize the master info structure");
error= 1;
goto err;
}
DBUG_PRINT("info", ("init group master %s %lu group relay %s %lu event %s %lu\n",
rli->get_group_master_log_name(),
(ulong) rli->get_group_master_log_pos(),
rli->get_group_relay_log_name(),
(ulong) rli->get_group_relay_log_pos(),
rli->get_event_relay_log_name(),
(ulong) rli->get_event_relay_log_pos()));
/* If server id is not set, start_slave_thread() will say it */
if (active_mi->host[0] && !opt_skip_slave_start)
{
/* same as in start_slave() cache the global var values into rli's members */
active_mi->rli->opt_slave_parallel_workers= opt_mts_slave_parallel_workers;
active_mi->rli->checkpoint_group= opt_mts_checkpoint_group;
if (start_slave_threads(true/*need_lock_slave=true*/,
false/*wait_for_start=false*/,
active_mi,
thread_mask))
{
sql_print_error("Failed to create slave threads");
error= 1;
goto err;
}
}
err:
mysql_mutex_unlock(&LOCK_active_mi);
if (error)
sql_print_information("Check error log for additional messages. "
"You will not be able to start replication until "
"the issue is resolved and the server restarted.");
DBUG_RETURN(error);
}
/**
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");
/*
Create a Format_description_log_event that is used to read the
first event of the log.
*/
Format_description_log_event fd_ev(BINLOG_VERSION), *fd_ev_p= &fd_ev;
DBUG_ASSERT(fd_ev.is_valid());
IO_CACHE log;
const char *errmsg= NULL;
File file= open_binlog_file(&log, filename, &errmsg);
if (file < 0)
{
sql_print_error("Error during --relay-log-recovery: %s", errmsg);
DBUG_RETURN(ERROR);
}
my_b_seek(&log, BIN_LOG_HEADER_SIZE);
Log_event *ev= NULL;
bool done= false;
enum_read_rotate_from_relay_log_status ret= NOT_FOUND_ROTATE;
while (!done &&
(ev= Log_event::read_log_event(&log, 0, fd_ev_p, opt_slave_sql_verify_checksum)) !=
NULL)
{
DBUG_PRINT("info", ("Read event of type %s", ev->get_type_str()));
switch (ev->get_type_code())
{
case FORMAT_DESCRIPTION_EVENT:
if (fd_ev_p != &fd_ev)
delete fd_ev_p;
fd_ev_p= (Format_description_log_event *)ev;
break;
case ROTATE_EVENT:
/*
Check for rotate event from the master. Ignore the ROTATE event if it
is a fake rotate event with server_id=0.
*/
if (ev->server_id && ev->server_id != ::server_id)
{
Rotate_log_event *rotate_ev= (Rotate_log_event *)ev;
DBUG_ASSERT(FN_REFLEN >= rotate_ev->ident_len + 1);
memcpy(master_log_file, rotate_ev->new_log_ident, rotate_ev->ident_len + 1);
*master_log_pos= rotate_ev->pos;
ret= FOUND_ROTATE;
done= true;
}
break;
case PREVIOUS_GTIDS_LOG_EVENT:
break;
case IGNORABLE_LOG_EVENT:
break;
default:
sql_print_error("Error during --relay-log-recovery: Could not locate "
"rotate event from the master.");
ret= ERROR;
done= true;
break;
}
if (ev != fd_ev_p)
delete ev;
}
if (log.error < 0)
{
sql_print_error("Error during --relay-log-recovery: Error reading events from relay log: %d",
log.error);
DBUG_RETURN(ERROR);
}
if (fd_ev_p != &fd_ev)
{
delete fd_ev_p;
fd_ev_p= &fd_ev;
}
if (mysql_file_close(file, MYF(MY_WME)))
DBUG_RETURN(ERROR);
if (end_io_cache(&log))
{
sql_print_error("Error during --relay-log-recovery: Error while freeing "
"IO_CACHE object");
DBUG_RETURN(ERROR);
}
DBUG_RETURN(ret);
}
/**
Reads relay logs one by one starting from the first relay log. Looks for
the first rotate event from the master. If rotate is not found in the relay
log search continues to next relay log. If rotate event from master is
found then the extracted master_log_file and master_log_pos are used to set
rli->group_master_log_name and rli->group_master_log_pos. If an error has
occurred the error code is retuned back.
@param rli
Relay_log_info object to read relay log files and to set
group_master_log_name and group_master_log_pos.
@retval 0 On success
@retval 1 On failure
*/
static int
find_first_relay_log_with_rotate_from_master(Relay_log_info* rli)
{
DBUG_ENTER("find_first_relay_log_with_rotate_from_master");
int error= 0;
LOG_INFO linfo;
bool got_rotate_from_master= false;
int pos;
char master_log_file[FN_REFLEN];
my_off_t master_log_pos= 0;
for (pos= rli->relay_log.find_log_pos(&linfo, NULL, true);
!pos;
pos= rli->relay_log.find_next_log(&linfo, true))
{
switch (read_rotate_from_relay_log(linfo.log_file_name, master_log_file,
&master_log_pos))
{
case ERROR:
error= 1;
break;
case FOUND_ROTATE:
got_rotate_from_master= true;
break;
case NOT_FOUND_ROTATE:
break;
}
if (error || got_rotate_from_master)
break;
}
if (pos== LOG_INFO_IO)
{
error= 1;
sql_print_error("Error during --relay-log-recovery: Could not read "
"relay log index file due to an IO error.");
goto err;
}
if (pos== LOG_INFO_EOF)
{
error= 1;
sql_print_error("Error during --relay-log-recovery: Could not locate "
"rotate event from master in relay log file.");
goto err;
}
if (!error && got_rotate_from_master)
{
rli->set_group_master_log_name(master_log_file);
rli->set_group_master_log_pos(master_log_pos);
}
err:
DBUG_RETURN(error);
}
/*
Updates the master info based on the information stored in the
relay info and ignores relay logs previously retrieved by the IO
thread, which thus starts fetching again based on to the
master_log_pos and master_log_name. Eventually, the old
relay logs will be purged by the normal purge mechanism.
There can be a special case where rli->group_master_log_name and
rli->group_master_log_pos are not intialized, as the sql thread was never
started at all. In those cases all the existing relay logs are parsed
starting from the first one and the initial rotate event that was received
from the master is identified. From the rotate event master_log_name and
master_log_pos are extracted and they are set to rli->group_master_log_name
and rli->group_master_log_pos.
In the feature, we should improve this routine in order to avoid throwing
away logs that are safely stored in the disk. Note also that this recovery
routine relies on the correctness of the relay-log.info and only tolerates
coordinate problems in master.info.
In this function, there is no need for a mutex as the caller
(i.e. init_slave) already has one acquired.
Specifically, the following structures are updated:
1 - mi->master_log_pos <-- rli->group_master_log_pos
2 - mi->master_log_name <-- rli->group_master_log_name
3 - It moves the relay log to the new relay log file, by
rli->group_relay_log_pos <-- BIN_LOG_HEADER_SIZE;
rli->event_relay_log_pos <-- BIN_LOG_HEADER_SIZE;
rli->group_relay_log_name <-- rli->relay_log.get_log_fname();
rli->event_relay_log_name <-- rli->relay_log.get_log_fname();
If there is an error, it returns (1), otherwise returns (0).
*/
int init_recovery(Master_info* mi, const char** errmsg)
{
DBUG_ENTER("init_recovery");
int error= 0;
Relay_log_info *rli= mi->rli;
char *group_master_log_name= NULL;
if (rli->recovery_parallel_workers)
{
/*
This is not idempotent and a crash after this function and before
the recovery is actually done may lead the system to an inconsistent
state.
This may happen because the gap is not persitent stored anywhere
and eventually old relay log files will be removed and further
calculations on the gaps will be impossible.
We need to improve this. /Alfranio.
*/
error= mts_recovery_groups(rli);
if (rli->mts_recovery_group_cnt)
{
if (gtid_mode == GTID_MODE_ON)
{
rli->recovery_parallel_workers= 0;
rli->clear_mts_recovery_groups();
}
else
{
error= 1;
sql_print_error("--relay-log-recovery cannot be executed when the slave "
"was stopped with an error or killed in MTS mode; "
"consider using RESET SLAVE or restart the server "
"with --relay-log-recovery = 0 followed by "
"START SLAVE UNTIL SQL_AFTER_MTS_GAPS");
}
}
}
group_master_log_name= const_cast<char *>(rli->get_group_master_log_name());
if (!error)
{
if (!group_master_log_name[0])
{
if (rli->replicate_same_server_id)
{
error= 1;
sql_print_error("Error during --relay-log-recovery: "
"replicate_same_server_id is in use and sql thread's "
"positions are not initialized, hence relay log "
"recovery cannot happen.");
DBUG_RETURN(error);
}
error= find_first_relay_log_with_rotate_from_master(rli);
if (error)
DBUG_RETURN(error);
}
mi->set_master_log_pos(max<ulonglong>(BIN_LOG_HEADER_SIZE,
rli->get_group_master_log_pos()));
mi->set_master_log_name(rli->get_group_master_log_name());
sql_print_warning("Recovery from master pos %ld and file %s. "
"Previous relay log pos and relay log file had "
"been set to %lld, %s respectively.",
(ulong) mi->get_master_log_pos(), mi->get_master_log_name(),
rli->get_group_relay_log_pos(), rli->get_group_relay_log_name());
rli->set_group_relay_log_name(rli->relay_log.get_log_fname());
rli->set_event_relay_log_name(rli->relay_log.get_log_fname());
rli->set_group_relay_log_pos(BIN_LOG_HEADER_SIZE);
rli->set_event_relay_log_pos(BIN_LOG_HEADER_SIZE);
}
/*
Clear the retrieved GTID set so that events that are written partially
will be fetched again.
*/
global_sid_lock->wrlock();
(const_cast<Gtid_set *>(rli->get_gtid_set()))->clear();
global_sid_lock->unlock();
DBUG_RETURN(error);
}
int global_init_info(Master_info* mi, bool ignore_if_no_info, int thread_mask)
{
DBUG_ENTER("init_info");
DBUG_ASSERT(mi != NULL && mi->rli != NULL);
int init_error= 0;
enum_return_check check_return= ERROR_CHECKING_REPOSITORY;
THD *thd= current_thd;
/*
We need a mutex while we are changing master info parameters to
keep other threads from reading bogus info
*/
mysql_mutex_lock(&mi->data_lock);
mysql_mutex_lock(&mi->rli->data_lock);
/*
When info tables are used and autocommit= 0 we force a new
transaction start to avoid table access deadlocks when START SLAVE
is executed after RESET SLAVE.
*/
if (thd && thd->in_multi_stmt_transaction_mode() &&
(opt_mi_repository_id == INFO_REPOSITORY_TABLE ||
opt_rli_repository_id == INFO_REPOSITORY_TABLE))
if (trans_begin(thd))
{
init_error= 1;
goto end;
}
/*
This takes care of the startup dependency between the master_info
and relay_info. It initializes the master info if the SLAVE_IO
thread is being started and the relay log info if either the
SLAVE_SQL thread is being started or was not initialized as it is
required by the SLAVE_IO thread.
*/
check_return= mi->check_info();
if (check_return == ERROR_CHECKING_REPOSITORY)
goto end;
if (!(ignore_if_no_info && check_return == REPOSITORY_DOES_NOT_EXIST))
{
if ((thread_mask & SLAVE_IO) != 0 && mi->mi_init_info())
init_error= 1;
}
check_return= mi->rli->check_info();
if (check_return == ERROR_CHECKING_REPOSITORY)
goto end;
if (!(ignore_if_no_info && check_return == REPOSITORY_DOES_NOT_EXIST))
{
if (((thread_mask & SLAVE_SQL) != 0 || !(mi->rli->inited))
&& mi->rli->rli_init_info())
init_error= 1;
}
end:
/*
When info tables are used and autocommit= 0 we force transaction
commit to avoid table access deadlocks when START SLAVE is executed
after RESET SLAVE.
*/
if (thd && thd->in_multi_stmt_transaction_mode() &&
(opt_mi_repository_id == INFO_REPOSITORY_TABLE ||
opt_rli_repository_id == INFO_REPOSITORY_TABLE))
if (trans_commit(thd))
init_error= 1;
mysql_mutex_unlock(&mi->rli->data_lock);
mysql_mutex_unlock(&mi->data_lock);
DBUG_RETURN(check_return == ERROR_CHECKING_REPOSITORY || init_error);
}
void end_info(Master_info* mi)
{
DBUG_ENTER("end_info");
DBUG_ASSERT(mi != NULL && mi->rli != NULL);
/*
The previous implementation was not acquiring locks. We do the same here.
However, this is quite strange.
*/
mi->end_info();
mi->rli->end_info();
DBUG_VOID_RETURN;
}
int remove_info(Master_info* mi)
{
int error= 1;
DBUG_ENTER("remove_info");
DBUG_ASSERT(mi != NULL && mi->rli != NULL);
/*
The previous implementation was not acquiring locks.
We do the same here. However, this is quite strange.
*/
/*
Reset errors (the idea is that we forget about the
old master).
*/
mi->clear_error();
mi->rli->clear_error();
mi->rli->clear_until_condition();
mi->rli->clear_sql_delay();
mi->end_info();
mi->rli->end_info();
if (mi->remove_info() || Rpl_info_factory::reset_workers(mi->rli) ||
mi->rli->remove_info())
goto err;
error= 0;
err:
DBUG_RETURN(error);
}
int flush_master_info(Master_info* mi, bool force)
{
DBUG_ENTER("flush_master_info");
DBUG_ASSERT(mi != NULL && mi->rli != NULL);
/*
The previous implementation was not acquiring locks.
We do the same here. However, this is quite strange.
*/
/*
With the appropriate recovery process, we will not need to flush
the content of the current log.
For now, we flush the relay log BEFORE the master.info file, because
if we crash, we will get a duplicate event in the relay log at restart.
If we change the order, there might be missing events.
If we don't do this and the slave server dies when the relay log has
some parts (its last kilobytes) in memory only, with, say, from master's
position 100 to 150 in memory only (not on disk), and with position 150
in master.info, there will be missing information. When the slave restarts,
the I/O thread will fetch binlogs from 150, so in the relay log we will
have "[0, 100] U [150, infinity[" and nobody will notice it, so the SQL
thread will jump from 100 to 150, and replication will silently break.
*/
mysql_mutex_t *log_lock= mi->rli->relay_log.get_log_lock();
mysql_mutex_lock(log_lock);
int err= (mi->rli->flush_current_log() ||
mi->flush_info(force));
mysql_mutex_unlock(log_lock);
DBUG_RETURN (err);
}
/**
Convert slave skip errors bitmap into a printable string.
*/
static void print_slave_skip_errors(void)
{
/*
To be safe, we want 10 characters of room in the buffer for a number
plus terminators. Also, we need some space for constant strings.
10 characters must be sufficient for a number plus {',' | '...'}
plus a NUL terminator. That is a max 6 digit number.
*/
const size_t MIN_ROOM= 10;
DBUG_ENTER("print_slave_skip_errors");
DBUG_ASSERT(sizeof(slave_skip_error_names) > MIN_ROOM);
DBUG_ASSERT(MAX_SLAVE_ERROR <= 999999); // 6 digits
if (!use_slave_mask || bitmap_is_clear_all(&slave_error_mask))
{
/* purecov: begin tested */
memcpy(slave_skip_error_names, STRING_WITH_LEN("OFF"));
/* purecov: end */
}
else if (bitmap_is_set_all(&slave_error_mask))
{
/* purecov: begin tested */
memcpy(slave_skip_error_names, STRING_WITH_LEN("ALL"));
/* purecov: end */
}
else
{
char *buff= slave_skip_error_names;
char *bend= buff + sizeof(slave_skip_error_names);
int errnum;
for (errnum= 0; errnum < MAX_SLAVE_ERROR; errnum++)
{
if (bitmap_is_set(&slave_error_mask, errnum))
{
if (buff + MIN_ROOM >= bend)
break; /* purecov: tested */
buff= int10_to_str(errnum, buff, 10);
*buff++= ',';
}
}
if (buff != slave_skip_error_names)
buff--; // Remove last ','
if (errnum < MAX_SLAVE_ERROR)
{
/* Couldn't show all errors */
buff= strmov(buff, "..."); /* purecov: tested */
}
*buff=0;
}
DBUG_PRINT("init", ("error_names: '%s'", slave_skip_error_names));
DBUG_VOID_RETURN;
}
static void set_stop_slave_wait_timeout(unsigned long wait_timeout) {
stop_wait_timeout = wait_timeout;
}
/**
Change arg to the string with the nice, human-readable skip error values.
@param slave_skip_errors_ptr
The pointer to be changed
*/
void set_slave_skip_errors(char** slave_skip_errors_ptr)
{
DBUG_ENTER("set_slave_skip_errors");
print_slave_skip_errors();
*slave_skip_errors_ptr= slave_skip_error_names;
DBUG_VOID_RETURN;
}
/**
Init function to set up array for errors that should be skipped for slave
*/
static void init_slave_skip_errors()
{
DBUG_ENTER("init_slave_skip_errors");
DBUG_ASSERT(!use_slave_mask); // not already initialized
if (bitmap_init(&slave_error_mask,0,MAX_SLAVE_ERROR,0))
{
fprintf(stderr, "Badly out of memory, please check your system status\n");
exit(1);
}
use_slave_mask = 1;
DBUG_VOID_RETURN;
}
static void add_slave_skip_errors(const uint* errors, uint n_errors)
{
DBUG_ENTER("add_slave_skip_errors");
DBUG_ASSERT(errors);
DBUG_ASSERT(use_slave_mask);
for (uint i = 0; i < n_errors; i++)
{
const uint err_code = errors[i];
if (err_code < MAX_SLAVE_ERROR)
bitmap_set_bit(&slave_error_mask, err_code);
}
DBUG_VOID_RETURN;
}
/*
Add errors that should be skipped for slave
SYNOPSIS
add_slave_skip_errors()
arg List of errors numbers to be added to skip, separated with ','
NOTES
Called from get_options() in mysqld.cc on start-up
*/
void add_slave_skip_errors(const char* arg)
{
const char *p= NULL;
/*
ALL is only valid when nothing else is provided.