/
slave.cc
8333 lines (7477 loc) · 288 KB
/
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, 2017, Oracle and/or its affiliates.
Copyright (c) 2009, 2022, 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 */
/**
@addtogroup Replication
@{
@file
@brief Code to run the io thread and the sql thread on the
replication slave.
*/
#include "mariadb.h"
#include "sql_priv.h"
#include "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 "sql_repl.h"
#include "rpl_filter.h"
#include "repl_failsafe.h"
#include "transaction.h"
#include <thr_alarm.h>
#include <my_dir.h>
#include <sql_common.h>
#include <errmsg.h>
#include <ssl_compat.h>
#include "unireg.h"
#include <mysys_err.h>
#include <signal.h>
#include <mysql.h>
#include <myisam.h>
#include "debug_sync.h" // debug_sync_set_action
#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 "wsrep_mysqld.h"
#ifdef WITH_WSREP
#include "wsrep_trans_observer.h"
#endif
#ifdef HAVE_REPLICATION
#include "rpl_tblmap.h"
#include "rpl_parallel.h"
#include "sql_show.h"
#include "semisync_slave.h"
#include "sql_manager.h"
#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];
uint *slave_transaction_retry_errors;
uint slave_transaction_retry_error_length= 0;
char slave_transaction_retry_error_names[SHOW_VAR_FUNC_BUFF_SIZE];
char* slave_load_tmpdir = 0;
Master_info *active_mi= 0;
Master_info_index *master_info_index;
my_bool replicate_same_server_id;
ulonglong relay_log_space_limit = 0;
ulonglong opt_read_binlog_speed_limit = 0;
const char *relay_log_index= 0;
const char *relay_log_basename= 0;
LEX_CSTRING default_master_connection_name= { (char*) "", 0 };
/*
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 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 %llu%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 %llu%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 %llu%s",
"",
"Slave I/O thread killed during or after a reconnect done to recover from \
failed read"
}
};
typedef enum { SLAVE_THD_IO, SLAVE_THD_SQL} SLAVE_THD_TYPE;
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 bool io_slave_killed(Master_info* mi);
static bool sql_slave_killed(rpl_group_info *rgi);
static int init_slave_thread(THD*, Master_info *, SLAVE_THD_TYPE);
static void make_slave_skip_errors_printable(void);
static void make_slave_transaction_retry_errors_printable(void);
static int safe_connect(THD* thd, MYSQL* mysql, Master_info* mi);
static int safe_reconnect(THD*, MYSQL*, Master_info*, bool);
static int connect_to_master(THD*, MYSQL*, Master_info*, bool, bool);
static Log_event* next_event(rpl_group_info* rgi, ulonglong *event_size);
static int queue_event(Master_info* mi,const char* buf,ulong event_len);
static int terminate_slave_thread(THD *, mysql_mutex_t *, mysql_cond_t *,
volatile uint *, bool);
static bool check_io_slave_killed(Master_info *mi, const char *info);
static bool send_show_master_info_data(THD *, Master_info *, bool, String *);
/*
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;
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() against other threads doing STOP, START or RESET SLAVE
*/
void Master_info::lock_slave_threads()
{
DBUG_ENTER("lock_slave_threads");
mysql_mutex_lock(&start_stop_lock);
DBUG_VOID_RETURN;
}
/*
unlock_slave_threads()
*/
void Master_info::unlock_slave_threads()
{
DBUG_ENTER("unlock_slave_threads");
mysql_mutex_unlock(&start_stop_lock);
DBUG_VOID_RETURN;
}
#ifdef HAVE_PSI_INTERFACE
static PSI_thread_key key_thread_slave_io, key_thread_slave_sql;
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}
};
static void init_slave_psi_keys(void)
{
const char* category= "sql";
int count;
if (PSI_server == NULL)
return;
count= array_elements(all_slave_threads);
PSI_server->register_thread(category, all_slave_threads, count);
}
#endif /* HAVE_PSI_INTERFACE */
/*
Note: This definition needs to be kept in sync with the one in
mysql_system_tables.sql which is used by mysql_create_db.
*/
static const char gtid_pos_table_definition1[]=
"CREATE TABLE ";
static const char gtid_pos_table_definition2[]=
" (domain_id INT UNSIGNED NOT NULL, "
"sub_id BIGINT UNSIGNED NOT NULL, "
"server_id INT UNSIGNED NOT NULL, "
"seq_no BIGINT UNSIGNED NOT NULL, "
"PRIMARY KEY (domain_id, sub_id)) CHARSET=latin1 "
"COMMENT='Replication slave GTID position' "
"ENGINE=";
/*
Build a query string
CREATE TABLE mysql.gtid_slave_pos_<engine> ... ENGINE=<engine>
*/
static bool
build_gtid_pos_create_query(THD *thd, String *query,
LEX_CSTRING *table_name,
LEX_CSTRING *engine_name)
{
bool err= false;
err|= query->append(gtid_pos_table_definition1);
err|= append_identifier(thd, query, table_name);
err|= query->append(gtid_pos_table_definition2);
err|= append_identifier(thd, query, engine_name);
return err;
}
static int
gtid_pos_table_creation(THD *thd, plugin_ref engine, LEX_CSTRING *table_name)
{
int err;
StringBuffer<sizeof(gtid_pos_table_definition1) +
sizeof(gtid_pos_table_definition1) +
2*FN_REFLEN> query;
if (build_gtid_pos_create_query(thd, &query, table_name, plugin_name(engine)))
{
my_error(ER_OUT_OF_RESOURCES, MYF(0));
return 1;
}
thd->set_db(&MYSQL_SCHEMA_NAME);
thd->clear_error();
ulonglong thd_saved_option= thd->variables.option_bits;
/* This query shuold not be binlogged. */
thd->variables.option_bits&= ~(ulonglong)OPTION_BIN_LOG;
thd->set_query_and_id(query.c_ptr(), query.length(), thd->charset(),
next_query_id());
Parser_state parser_state;
err= parser_state.init(thd, thd->query(), thd->query_length());
if (err)
goto end;
mysql_parse(thd, thd->query(), thd->query_length(), &parser_state,
FALSE, FALSE);
if (unlikely(thd->is_error()))
err= 1;
/* The warning is relevant to 10.3 and earlier. */
sql_print_warning("The automatically created table '%s' name may not be "
"entirely in lowercase. The table name will be converted "
"to lowercase to any future upgrade to 10.4.0 and later "
"version where it will be auto-created at once "
"in lowercase.",
table_name->str);
end:
thd->variables.option_bits= thd_saved_option;
thd->reset_query();
return err;
}
static THD *new_bg_THD()
{
THD *thd= new THD(next_thread_id());
thd->thread_stack= (char*) &thd;
thd->store_globals();
thd->system_thread = SYSTEM_THREAD_SLAVE_BACKGROUND;
thd->security_ctx->skip_grants();
thd->set_command(COM_DAEMON);
thd->variables.wsrep_on= 0;
return thd;
}
static void bg_gtid_delete_pending(void *)
{
THD *thd= new_bg_THD();
rpl_slave_state::list_element *list;
list= rpl_global_gtid_slave_state->gtid_grab_pending_delete_list();
rpl_global_gtid_slave_state->gtid_delete_pending(thd, &list);
if (list)
rpl_global_gtid_slave_state->put_back_list(list);
delete thd;
}
static void bg_gtid_pos_auto_create(void *hton)
{
THD *thd= NULL;
int UNINIT_VAR(err);
plugin_ref engine= NULL, *auto_engines;
rpl_slave_state::gtid_pos_table *entry;
StringBuffer<FN_REFLEN> loc_table_name;
LEX_CSTRING table_name;
/*
Check that the plugin is still in @@gtid_pos_auto_engines, and lock
it.
*/
mysql_mutex_lock(&LOCK_global_system_variables);
for (auto_engines= opt_gtid_pos_auto_plugins;
auto_engines && *auto_engines;
++auto_engines)
{
if (plugin_hton(*auto_engines) == hton)
{
engine= my_plugin_lock(NULL, *auto_engines);
break;
}
}
mysql_mutex_unlock(&LOCK_global_system_variables);
if (!engine)
{
/* The engine is gone from @@gtid_pos_auto_engines, so no action. */
goto end;
}
/* Find the entry for the table to auto-create. */
mysql_mutex_lock(&rpl_global_gtid_slave_state->LOCK_slave_state);
entry= rpl_global_gtid_slave_state->
gtid_pos_tables.load(std::memory_order_relaxed);
while (entry)
{
if (entry->table_hton == hton &&
entry->state == rpl_slave_state::GTID_POS_CREATE_REQUESTED)
break;
entry= entry->next;
}
if (entry)
{
entry->state = rpl_slave_state::GTID_POS_CREATE_IN_PROGRESS;
err= loc_table_name.append(entry->table_name.str, entry->table_name.length);
}
mysql_mutex_unlock(&rpl_global_gtid_slave_state->LOCK_slave_state);
if (!entry)
goto end;
if (err)
{
sql_print_error("Out of memory while trying to auto-create GTID position table");
goto end;
}
table_name.str= loc_table_name.c_ptr_safe();
table_name.length= loc_table_name.length();
thd= new_bg_THD();
err= gtid_pos_table_creation(thd, engine, &table_name);
if (err)
{
sql_print_error("Error auto-creating GTID position table `mysql.%s`: %s Error_code: %d",
table_name.str, thd->get_stmt_da()->message(),
thd->get_stmt_da()->sql_errno());
thd->clear_error();
goto end;
}
/* Now enable the entry for the auto-created table. */
mysql_mutex_lock(&rpl_global_gtid_slave_state->LOCK_slave_state);
entry= rpl_global_gtid_slave_state->
gtid_pos_tables.load(std::memory_order_relaxed);
while (entry)
{
if (entry->table_hton == hton &&
entry->state == rpl_slave_state::GTID_POS_CREATE_IN_PROGRESS)
{
entry->state= rpl_slave_state::GTID_POS_AVAILABLE;
break;
}
entry= entry->next;
}
mysql_mutex_unlock(&rpl_global_gtid_slave_state->LOCK_slave_state);
end:
delete thd;
if (engine)
plugin_unlock(NULL, engine);
}
static bool slave_background_thread_gtid_loaded;
static void bg_rpl_load_gtid_slave_state(void *)
{
THD *thd= new_bg_THD();
thd_proc_info(thd, "Loading slave GTID position from table");
if (rpl_load_gtid_slave_state(thd))
sql_print_warning("Failed to load slave replication state from table "
"%s.%s: %u: %s", "mysql",
rpl_gtid_slave_state_table_name.str,
thd->get_stmt_da()->sql_errno(),
thd->get_stmt_da()->message());
// hijacking global_rpl_thread_pool cond here - it's only once on startup
mysql_mutex_lock(&global_rpl_thread_pool.LOCK_rpl_thread_pool);
slave_background_thread_gtid_loaded= true;
mysql_cond_signal(&global_rpl_thread_pool.COND_rpl_thread_pool);
mysql_mutex_unlock(&global_rpl_thread_pool.LOCK_rpl_thread_pool);
delete thd;
}
static void bg_slave_kill(void *victim)
{
THD *to_kill= (THD *)victim;
DBUG_EXECUTE_IF("rpl_delay_deadlock_kill", my_sleep(1500000););
to_kill->awake(KILL_CONNECTION);
mysql_mutex_lock(&to_kill->LOCK_wakeup_ready);
to_kill->rgi_slave->killed_for_retry= rpl_group_info::RETRY_KILL_KILLED;
mysql_cond_broadcast(&to_kill->COND_wakeup_ready);
mysql_mutex_unlock(&to_kill->LOCK_wakeup_ready);
}
void slave_background_kill_request(THD *to_kill)
{
if (to_kill->rgi_slave->killed_for_retry)
return; // Already deadlock killed.
to_kill->rgi_slave->killed_for_retry= rpl_group_info::RETRY_KILL_PENDING;
mysql_manager_submit(bg_slave_kill, to_kill);
}
/*
This function must only be called from a slave SQL thread (or worker thread),
to ensure that the table_entry will not go away before we can lock the
LOCK_slave_state.
*/
void slave_background_gtid_pos_create_request(
rpl_slave_state::gtid_pos_table *table_entry)
{
if (table_entry->state != rpl_slave_state::GTID_POS_AUTO_CREATE)
return;
mysql_mutex_lock(&rpl_global_gtid_slave_state->LOCK_slave_state);
if (table_entry->state != rpl_slave_state::GTID_POS_AUTO_CREATE)
{
mysql_mutex_unlock(&rpl_global_gtid_slave_state->LOCK_slave_state);
return;
}
table_entry->state= rpl_slave_state::GTID_POS_CREATE_REQUESTED;
mysql_mutex_unlock(&rpl_global_gtid_slave_state->LOCK_slave_state);
mysql_manager_submit(bg_gtid_pos_auto_create, table_entry->table_hton);
}
/*
Request the manager thread to delete no longer used rows from the
mysql.gtid_slave_pos* tables.
*/
void slave_background_gtid_pending_delete_request(void)
{
mysql_manager_submit(bg_gtid_delete_pending, NULL);
}
/* Initialize slave structures */
int init_slave()
{
DBUG_ENTER("init_slave");
int error= 0;
#ifdef HAVE_PSI_INTERFACE
init_slave_psi_keys();
#endif
if (global_rpl_thread_pool.init(opt_slave_parallel_threads))
return 1;
slave_background_thread_gtid_loaded= false;
mysql_manager_submit(bg_rpl_load_gtid_slave_state, NULL);
// hijacking global_rpl_thread_pool cond here - it's only once on startup
mysql_mutex_lock(&global_rpl_thread_pool.LOCK_rpl_thread_pool);
while (!slave_background_thread_gtid_loaded)
mysql_cond_wait(&global_rpl_thread_pool.COND_rpl_thread_pool,
&global_rpl_thread_pool.LOCK_rpl_thread_pool);
mysql_mutex_unlock(&global_rpl_thread_pool.LOCK_rpl_thread_pool);
/*
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.
*/
if (pthread_key_create(&RPL_MASTER_INFO, NULL))
goto err;
master_info_index= new Master_info_index;
if (!master_info_index || master_info_index->init_all_master_info())
{
sql_print_error("Failed to initialize multi master structures");
DBUG_RETURN(1);
}
if (!(active_mi= new Master_info(&default_master_connection_name,
relay_log_recovery)) ||
active_mi->error())
{
delete active_mi;
active_mi= 0;
sql_print_error("Failed to allocate memory for the Master Info structure");
goto err;
}
if (master_info_index->add_master_info(active_mi, FALSE))
{
delete active_mi;
active_mi= 0;
goto err;
}
/*
If master_host is not specified, try to read it from the master_info file.
If master_host is specified, create the master_info file if it doesn't
exists.
*/
if (init_master_info(active_mi,master_info_file,relay_log_info_file,
1, (SLAVE_IO | SLAVE_SQL)))
{
sql_print_error("Failed to initialize the master info structure");
goto err;
}
/* If server id is not set, start_slave_thread() will say it */
if (active_mi->host[0] && !opt_skip_slave_start)
{
int error;
THD *thd= new THD(next_thread_id());
thd->thread_stack= (char*) &thd;
thd->store_globals();
error= start_slave_threads(0, /* No active thd */
1 /* need mutex */,
1 /* wait for start*/,
active_mi,
master_info_file,
relay_log_info_file,
SLAVE_IO | SLAVE_SQL);
thd->reset_globals();
delete thd;
if (unlikely(error))
{
sql_print_error("Failed to create slave threads");
goto err;
}
}
end:
DBUG_RETURN(error);
err:
error= 1;
goto end;
}
/*
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
group_master_log_pos and group_master_log_name. Eventually, the old
relay logs will be purged by the normal purge mechanism.
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");
Relay_log_info *rli= &mi->rli;
if (rli->group_master_log_name[0])
{
mi->master_log_pos= MY_MAX(BIN_LOG_HEADER_SIZE,
rli->group_master_log_pos);
strmake_buf(mi->master_log_name, rli->group_master_log_name);
sql_print_warning("Recovery from master pos %ld and file %s.",
(ulong) mi->master_log_pos, mi->master_log_name);
strmake_buf(rli->group_relay_log_name, rli->relay_log.get_log_fname());
strmake_buf(rli->event_relay_log_name, rli->relay_log.get_log_fname());
rli->group_relay_log_pos= rli->event_relay_log_pos= BIN_LOG_HEADER_SIZE;
}
DBUG_RETURN(0);
}
/**
Convert slave skip errors bitmap into a printable string.
*/
static void make_slave_skip_errors_printable(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("make_slave_skip_errors_printable");
DBUG_ASSERT(sizeof(slave_skip_error_names) > MIN_ROOM);
DBUG_ASSERT(MAX_SLAVE_ERROR <= 999999); // 6 digits
/* Make @@slave_skip_errors show the nice human-readable value. */
opt_slave_skip_errors= slave_skip_error_names;
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) - MIN_ROOM;
int errnum;
for (errnum= 0; errnum < MAX_SLAVE_ERROR; errnum++)
{
if (bitmap_is_set(&slave_error_mask, errnum))
{
if (buff >= 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;
}
/*
Init function to set up array for errors that should be skipped for slave
SYNOPSIS
init_slave_skip_errors()
arg List of errors numbers to skip, separated with ','
NOTES
Called from get_options() in mysqld.cc on start-up
*/
bool init_slave_skip_errors(const char* arg)
{
const char *p;
DBUG_ENTER("init_slave_skip_errors");
if (!arg || !*arg) // No errors defined
goto end;
if (unlikely(my_bitmap_init(&slave_error_mask,0,MAX_SLAVE_ERROR,0)))
DBUG_RETURN(1);
use_slave_mask= 1;
for (;my_isspace(system_charset_info,*arg);++arg)
/* empty */;
if (!my_strnncoll(system_charset_info,(uchar*)arg,4,(const uchar*)"all",4))
{
bitmap_set_all(&slave_error_mask);
goto end;
}
for (p= arg ; *p; )
{
long err_code;
if (!(p= str2int(p, 10, 0, LONG_MAX, &err_code)))
break;
if (err_code < MAX_SLAVE_ERROR)
bitmap_set_bit(&slave_error_mask,(uint)err_code);
while (!my_isdigit(system_charset_info,*p) && *p)
p++;
}
end:
make_slave_skip_errors_printable();
DBUG_RETURN(0);
}
/**
Make printable version if slave_transaction_retry_errors
This is never empty as at least ER_LOCK_DEADLOCK and ER_LOCK_WAIT_TIMEOUT
will be there
*/
static void make_slave_transaction_retry_errors_printable(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;
char *buff= slave_transaction_retry_error_names;
char *bend= buff + sizeof(slave_transaction_retry_error_names) - MIN_ROOM;
uint i;
DBUG_ENTER("make_slave_transaction_retry_errors_printable");
DBUG_ASSERT(sizeof(slave_transaction_retry_error_names) > MIN_ROOM);
/* Make @@slave_transaction_retry_errors show a human-readable value */
opt_slave_transaction_retry_errors= slave_transaction_retry_error_names;
for (i= 0; i < slave_transaction_retry_error_length && buff < bend; i++)
{
buff= int10_to_str(slave_transaction_retry_errors[i], buff, 10);
*buff++= ',';
}
if (buff != slave_transaction_retry_error_names)
buff--; // Remove last ','
if (i < slave_transaction_retry_error_length)
{
/* Couldn't show all errors */
buff= strmov(buff, "..."); /* purecov: tested */
}
*buff=0;
DBUG_PRINT("exit", ("error_names: '%s'",
slave_transaction_retry_error_names));
DBUG_VOID_RETURN;
}
#define DEFAULT_SLAVE_RETRY_ERRORS 9
bool init_slave_transaction_retry_errors(const char* arg)
{
const char *p;
long err_code;
uint i;
DBUG_ENTER("init_slave_transaction_retry_errors");
/* Handle empty strings */
if (!arg)
arg= "";
slave_transaction_retry_error_length= DEFAULT_SLAVE_RETRY_ERRORS;
for (;my_isspace(system_charset_info,*arg);++arg)
/* empty */;
for (p= arg; *p; )
{
if (!(p= str2int(p, 10, 0, LONG_MAX, &err_code)))
break;
slave_transaction_retry_error_length++;
while (!my_isdigit(system_charset_info,*p) && *p)
p++;
}
if (unlikely(!(slave_transaction_retry_errors=
(uint *) my_once_alloc(sizeof(int) *
slave_transaction_retry_error_length,
MYF(MY_WME)))))
DBUG_RETURN(1);
/*
Temporary error codes:
currently, InnoDB deadlock detected by InnoDB or lock
wait timeout (innodb_lock_wait_timeout exceeded
*/
slave_transaction_retry_errors[0]= ER_NET_READ_ERROR;
slave_transaction_retry_errors[1]= ER_NET_READ_INTERRUPTED;
slave_transaction_retry_errors[2]= ER_NET_ERROR_ON_WRITE;
slave_transaction_retry_errors[3]= ER_NET_WRITE_INTERRUPTED;
slave_transaction_retry_errors[4]= ER_LOCK_WAIT_TIMEOUT;
slave_transaction_retry_errors[5]= ER_LOCK_DEADLOCK;
slave_transaction_retry_errors[6]= ER_CONNECT_TO_FOREIGN_DATA_SOURCE;
slave_transaction_retry_errors[7]= 2013; /* CR_SERVER_LOST */
slave_transaction_retry_errors[8]= 12701; /* ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM */
/* Add user codes after this */
for (p= arg, i= DEFAULT_SLAVE_RETRY_ERRORS; *p; )
{
if (!(p= str2int(p, 10, 0, LONG_MAX, &err_code)))
break;
if (err_code > 0)
slave_transaction_retry_errors[i++]= (uint) err_code;
while (!my_isdigit(system_charset_info,*p) && *p)
p++;
}
slave_transaction_retry_error_length= i;
make_slave_transaction_retry_errors_printable();
DBUG_RETURN(0);
}
int terminate_slave_threads(Master_info* mi,int thread_mask,bool skip_lock)
{
DBUG_ENTER("terminate_slave_threads");
if (!mi->inited)
DBUG_RETURN(0); /* successfully do nothing */
int error,force_all = (thread_mask & SLAVE_FORCE_ALL);
int retval= 0;
mysql_mutex_t *sql_lock = &mi->rli.run_lock, *io_lock = &mi->run_lock;
mysql_mutex_t *log_lock= mi->rli.relay_log.get_log_lock();
if (thread_mask & (SLAVE_SQL|SLAVE_FORCE_ALL))
{
DBUG_PRINT("info",("Terminating SQL thread"));
if (mi->using_parallel() && mi->rli.abort_slave && mi->rli.stop_for_until)
{
mi->rli.stop_for_until= false;
mi->rli.parallel.stop_during_until();
}
else
mi->rli.abort_slave=1;
if (unlikely((error= terminate_slave_thread(mi->rli.sql_driver_thd,
sql_lock,
&mi->rli.stop_cond,
&mi->rli.slave_running,
skip_lock))) &&
!force_all)
DBUG_RETURN(error);
retval= error;
mysql_mutex_lock(log_lock);
DBUG_PRINT("info",("Flushing relay-log info file."));
if (current_thd)
THD_STAGE_INFO(current_thd, stage_flushing_relay_log_info_file);
if (mi->rli.flush() || my_sync(mi->rli.info_fd, MYF(MY_WME)))
retval= ER_ERROR_DURING_FLUSH_LOGS;
mysql_mutex_unlock(log_lock);
}
if (thread_mask & (SLAVE_IO|SLAVE_FORCE_ALL))
{
DBUG_PRINT("info",("Terminating IO thread"));
mi->abort_slave=1;
if (unlikely((error= terminate_slave_thread(mi->io_thd, io_lock,
&mi->stop_cond,
&mi->slave_running,
skip_lock))) &&
!force_all)
DBUG_RETURN(error);
if (!retval)
retval= error;
mysql_mutex_lock(log_lock);
DBUG_PRINT("info",("Flushing relay log and master info file."));
if (current_thd)
THD_STAGE_INFO(current_thd, stage_flushing_relay_log_and_master_info_repository);
if (likely(mi->fd >= 0))
{
if (flush_master_info(mi, TRUE, FALSE) || my_sync(mi->fd, MYF(MY_WME)))
retval= ER_ERROR_DURING_FLUSH_LOGS;
}
if (mi->rli.relay_log.is_open() &&
my_sync(mi->rli.relay_log.get_log_file()->file, MYF(MY_WME)))
retval= ER_ERROR_DURING_FLUSH_LOGS;
mysql_mutex_unlock(log_lock);
}
DBUG_RETURN(retval);
}
/**
Wait for a slave thread to terminate.
This function is called after requesting the thread to terminate
(by setting @c abort_slave member of @c Relay_log_info or @c
Master_info structure to 1). Termination of the thread is
controlled with the the predicate <code>*slave_running</code>.