-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathrpl_rli_pdb.cc
2628 lines (2221 loc) · 88.9 KB
/
rpl_rli_pdb.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) 2011, 2025, Oracle and/or its affiliates.
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 designed to work 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 either included with
the program or referenced in the documentation.
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 */
#include "sql/rpl_rli_pdb.h"
#include "my_config.h"
#include <string.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <stdio.h>
#include <algorithm>
#include <atomic>
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include "lex_string.h"
#include "m_string.h"
#include "map_helpers.h"
#include "my_bitmap.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_sys.h"
#include "my_systime.h"
#include "my_thread.h"
#include "mysql/components/services/bits/psi_stage_bits.h"
#include "mysql/components/services/log_builtins.h"
#include "mysql/plugin.h"
#include "mysql/psi/mysql_cond.h"
#include "mysql/psi/mysql_file.h"
#include "mysql/psi/mysql_mutex.h"
#include "mysql/thread_type.h"
#include "mysqld_error.h"
#include "scope_guard.h" // Scope_guard
#include "sql/binlog.h"
#include "sql/binlog_reader.h"
#include "sql/current_thd.h"
#include "sql/debug_sync.h"
#include "sql/log.h"
#include "sql/mdl.h"
#include "sql/mysqld.h" // key_mutex_slave_parallel_worker
#include "sql/psi_memory_key.h"
#include "sql/raii/sentry.h" // raii::Sentry<>
#include "sql/rpl_info_handler.h"
#include "sql/rpl_msr.h" // For channel_map
#include "sql/rpl_replica_commit_order_manager.h" // Commit_order_manager
#include "sql/rpl_reporting.h"
#include "sql/sql_error.h"
#include "sql/sql_lex.h"
#include "sql/table.h"
#include "sql/transaction_info.h"
#include "thr_mutex.h"
#ifndef NDEBUG
ulong w_rr = 0;
uint mta_debug_concurrent_access = 0;
#endif
#define HASH_DYNAMIC_INIT 4
using std::max;
using std::min;
/**
This function is called by both coordinator and workers.
Upon receiving the STOP command, the workers will identify a
maximum group index already executed (or under execution).
All groups whose index are below or equal to the maximum
group index will be applied by the workers before stopping.
The workers with groups above the maximum group index will
exit without applying these groups by setting their running
status to "STOP_ACCEPTED".
@param worker a pointer to the waiting Worker struct
@param job_item a pointer to struct carrying a reference to an event
@return true if STOP command gets accepted otherwise false is returned.
*/
bool handle_slave_worker_stop(Slave_worker *worker, Slave_job_item *job_item) {
ulonglong group_index = 0;
Relay_log_info *rli = worker->c_rli;
mysql_mutex_lock(&rli->exit_count_lock);
/*
First, W calculates a group-"at-hands" index which is
either the currently read ev group index, or the last executed
group's one when the queue is empty.
*/
group_index =
(job_item->data)
? rli->gaq->get_job_group(job_item->data->mts_group_idx)->total_seqno
: worker->last_groups_assigned_index;
/*
The max updated index is being updated as long as
exit_counter permits. That's stopped with the final W's
increment of it.
*/
if (!worker->exit_incremented) {
if (rli->exit_counter < rli->replica_parallel_workers)
rli->max_updated_index = max(rli->max_updated_index, group_index);
++rli->exit_counter;
worker->exit_incremented = true;
assert(!is_mts_worker(current_thd));
}
#ifndef NDEBUG
else
assert(is_mts_worker(current_thd));
#endif
/*
Now let's decide about the deferred exit to consider
the empty queue and the counter value reached
replica_parallel_workers.
*/
if (!job_item->data) {
worker->running_status = Slave_worker::STOP_ACCEPTED;
mysql_cond_signal(&worker->jobs_cond);
mysql_mutex_unlock(&rli->exit_count_lock);
return (true);
} else if (rli->exit_counter == rli->replica_parallel_workers) {
// over steppers should exit with accepting STOP
if (group_index > rli->max_updated_index) {
worker->running_status = Slave_worker::STOP_ACCEPTED;
mysql_cond_signal(&worker->jobs_cond);
mysql_mutex_unlock(&rli->exit_count_lock);
return (true);
}
}
mysql_mutex_unlock(&rli->exit_count_lock);
return (false);
}
/**
This function is called by both coordinator and workers.
Both coordinator and workers contribute to max_updated_index.
@param worker a pointer to the waiting Worker struct
@param job_item a pointer to struct carrying a reference to an event
@return true if STOP command gets accepted otherwise false is returned.
*/
bool set_max_updated_index_on_stop(Slave_worker *worker,
Slave_job_item *job_item) {
const auto head = worker->jobs.head_queue();
if (head != nullptr) {
*job_item = *head;
} else {
job_item->data = nullptr;
}
if (worker->running_status == Slave_worker::STOP) {
if (handle_slave_worker_stop(worker, job_item)) return true;
}
return false;
}
/*
Please every time you add a new field to the worker slave info, update
what follows. For now, this is just used to get the number of fields.
*/
const char *info_slave_worker_fields[] = {
"id",
/*
These positions identify what has been executed. Notice that they are
redundant and only the group_master_log_name and group_master_log_pos
are really necessary. However, the additional information is kept to
ease debugging.
*/
"group_relay_log_name", "group_relay_log_pos", "group_source_log_name",
"group_source_log_pos",
/*
These positions identify what a worker knew about the coordinator at
the time a job was assigned. Notice that they are redundant and are
kept to ease debugging.
*/
"checkpoint_relay_log_name", "checkpoint_relay_log_pos",
"checkpoint_source_log_name", "checkpoint_source_log_pos",
/*
Identify the greatest job, i.e. group, processed by a worker.
*/
"checkpoint_seqno",
/*
Maximum number of jobs that can be assigned to a worker. This
information is necessary to read the next entry.
*/
"checkpoint_group_size",
/*
Bitmap used to identify what jobs were processed by a worker.
*/
"checkpoint_group_bitmap",
/*
Channel on which this workers are acting
*/
"channel_name"};
/*
Number of records in the mts partition hash below which
entries with zero usage are tolerated so could be quickly
recycled.
*/
const ulong mts_partition_hash_soft_max = 16;
/*
index value of some outstanding slots of info_slave_worker_fields
*/
enum {
LINE_FOR_CHANNEL = 12,
};
const uint info_slave_worker_table_pk_field_indexes[] = {
LINE_FOR_CHANNEL,
0,
};
Slave_worker::Slave_worker(Relay_log_info *rli,
#ifdef HAVE_PSI_INTERFACE
PSI_mutex_key *param_key_info_run_lock,
PSI_mutex_key *param_key_info_data_lock,
PSI_mutex_key *param_key_info_sleep_lock,
PSI_mutex_key *param_key_info_thd_lock,
PSI_mutex_key *param_key_info_data_cond,
PSI_mutex_key *param_key_info_start_cond,
PSI_mutex_key *param_key_info_stop_cond,
PSI_mutex_key *param_key_info_sleep_cond,
#endif
uint param_id, const char *param_channel)
: Relay_log_info(false,
#ifdef HAVE_PSI_INTERFACE
param_key_info_run_lock, param_key_info_data_lock,
param_key_info_sleep_lock, param_key_info_thd_lock,
param_key_info_data_cond, param_key_info_start_cond,
param_key_info_stop_cond, param_key_info_sleep_cond,
#endif
param_id + 1, param_channel, true),
c_rli(rli),
curr_group_exec_parts(key_memory_db_worker_hash_entry),
id(param_id),
checkpoint_relay_log_pos(0),
checkpoint_master_log_pos(0),
worker_checkpoint_seqno(0),
running_status(NOT_RUNNING),
exit_incremented(false) {
/*
In the future, it would be great if we use only one identifier.
So when factoring out this code, please, consider this.
*/
assert(internal_id == id + 1);
checkpoint_relay_log_name[0] = 0;
checkpoint_master_log_name[0] = 0;
mysql_mutex_init(key_mutex_slave_parallel_worker, &jobs_lock,
MY_MUTEX_INIT_FAST);
mysql_cond_init(key_cond_slave_parallel_worker, &jobs_cond);
mysql_cond_init(key_cond_mta_gaq, &logical_clock_cond);
}
Slave_worker::~Slave_worker() {
end_info();
if (jobs.inited_queue) {
assert(jobs.m_Q.size() == jobs.capacity);
jobs.m_Q.clear();
}
mysql_mutex_destroy(&jobs_lock);
mysql_cond_destroy(&jobs_cond);
mysql_cond_destroy(&logical_clock_cond);
mysql_mutex_lock(&info_thd_lock);
info_thd = nullptr;
mysql_mutex_unlock(&info_thd_lock);
if (set_rli_description_event(nullptr)) {
#ifndef NDEBUG
bool set_rli_description_event_failed = false;
#endif
assert(set_rli_description_event_failed);
}
}
/**
Method is executed by Coordinator at Worker startup time to initialize
members parly with values supplied by Coordinator through rli.
@param rli Coordinator's Relay_log_info pointer
@param i identifier of the Worker
@return 0 success
non-zero failure
*/
int Slave_worker::init_worker(Relay_log_info *rli, ulong i) {
DBUG_TRACE;
assert(!rli->info_thd->is_error());
Slave_job_item empty = Slave_job_item();
c_rli = rli;
this->set_require_row_format(rli->is_row_format_required());
set_commit_order_manager(c_rli->get_commit_order_manager());
if (rli_init_info(false) ||
DBUG_EVALUATE_IF("inject_init_worker_init_info_fault", true, false))
return 1;
if (!rli->is_privilege_checks_user_null()) {
this->set_privilege_checks_user(
rli->get_privilege_checks_username().c_str(),
rli->get_privilege_checks_hostname().c_str());
}
if (this->m_assign_gtids_to_anonymous_transactions_info.set_info(
rli->m_assign_gtids_to_anonymous_transactions_info.get_type(),
(rli->m_assign_gtids_to_anonymous_transactions_info.get_value()
.c_str())))
return 1;
id = i;
curr_group_exec_parts.clear();
relay_log_change_notified = false; // the 1st group to contain relaylog name
checkpoint_notified = false; // the same as above
master_log_change_notified =
false; // W learns master log during 1st group exec
fd_change_notified = false; // W is to learn master FD version same as above
server_version = version_product(rli->slave_version_split);
bitmap_shifted = 0;
workers = c_rli->workers; // shallow copying is sufficient
wq_empty_waits = wq_size_waits_cnt = groups_done = events_done = curr_jobs =
0;
usage_partition = 0;
end_group_sets_max_dbs = false;
gaq_index = last_group_done_index = c_rli->gaq->capacity; // out of range
last_groups_assigned_index = 0;
assert(!jobs.inited_queue);
jobs.avail = 0;
jobs.entry = 0;
jobs.len = 0;
jobs.overfill = false; // todo: move into Slave_jobs_queue constructor
jobs.waited_overfill = 0;
jobs.capacity = c_rli->mts_slave_worker_queue_len_max;
jobs.inited_queue = true;
curr_group_seen_gtid = false;
#ifndef NDEBUG
curr_group_seen_sequence_number = false;
#endif
jobs.m_Q.resize(jobs.capacity, empty);
assert(jobs.m_Q.size() == jobs.capacity);
wq_overrun_cnt = excess_cnt = 0;
underrun_level =
(ulong)((rli->mts_worker_underrun_level * jobs.capacity) / 100.0);
// overrun level is symmetric to underrun (as underrun to the full queue)
overrun_level = jobs.capacity - underrun_level;
/* create mts submode for each of the the workers. */
current_mts_submode = (rli->channel_mts_submode == MTS_PARALLEL_TYPE_DB_NAME)
? (Mts_submode *)new Mts_submode_database()
: (Mts_submode *)new Mts_submode_logical_clock();
// workers and coordinator must be of the same type
assert(rli->current_mts_submode->get_type() ==
current_mts_submode->get_type());
reset_commit_order_deadlock();
return 0;
}
/**
A part of Slave worker initializer that provides a
minimum context for MTS recovery.
@param is_gaps_collecting_phase
clarifies what state the caller
executes this method from. When it's @c true
that is @c mts_recovery_groups() and Worker should
restore the last session time info which is processed
to collect gaps that is not executed transactions (groups).
Such recovery Slave_worker instance is destroyed at the end of
@c mts_recovery_groups().
When it's @c false Slave_worker is initialized for the run time
and should not read the last session time stale info.
Its info will be ultimately reset once all gaps are executed
to finish off recovery.
@return 0 on success, non-zero for a failure
*/
int Slave_worker::rli_init_info(bool is_gaps_collecting_phase) {
enum_return_check return_check = ERROR_CHECKING_REPOSITORY;
DBUG_TRACE;
if (inited) return 0;
/*
Worker bitmap size depends on recovery mode.
If it is gaps collecting the bitmaps must be capable to accept
up to MTS_MAX_BITS_IN_GROUP of bits.
*/
size_t num_bits = is_gaps_collecting_phase ? MTS_MAX_BITS_IN_GROUP
: c_rli->checkpoint_group;
/*
This checks if the repository was created before and thus there
will be values to be read. Please, do not move this call after
the handler->init_info().
*/
return_check = check_info();
if (return_check == ERROR_CHECKING_REPOSITORY ||
(return_check == REPOSITORY_DOES_NOT_EXIST && is_gaps_collecting_phase))
goto err;
if (handler->init_info()) goto err;
bitmap_init(&group_executed, nullptr, num_bits);
bitmap_init(&group_shifted, nullptr, num_bits);
if (is_gaps_collecting_phase &&
(DBUG_EVALUATE_IF("mta_replica_worker_init_at_gaps_fails", true, false) ||
read_info(handler))) {
bitmap_free(&group_executed);
bitmap_free(&group_shifted);
goto err;
}
inited = true;
return 0;
err:
// todo: handler->end_info(uidx, nidx);
inited = false;
LogErr(ERROR_LEVEL, ER_RPL_ERROR_READING_REPLICA_WORKER_CONFIGURATION);
return 1;
}
void Slave_worker::end_info() {
DBUG_TRACE;
if (!inited) return;
if (handler) handler->end_info();
if (inited) {
bitmap_free(&group_executed);
bitmap_free(&group_shifted);
}
inited = false;
}
int Slave_worker::flush_info(const bool force) {
DBUG_TRACE;
if (!inited) return 0;
if (c_rli->mi->is_gtid_only_mode()) return 0;
/*
We update the sync_period at this point because only here we
now that we are handling a Slave_worker. This needs to be
update every time we call flush because the option may be
dynamically set.
*/
handler->set_sync_period(sync_relayloginfo_period);
/*
This only fails on out-of-memory errors, which are reported (using
the MY_WME flag to my_malloc).
*/
if (write_info(handler)) return 1;
/*
This fails on errors committing the info, or when
replica_preserve_commit_order is enabled and a previous transaction
has failed. In both cases, the error is reported already.
*/
if (handler->flush_info(force)) return 1;
return 0;
}
bool Slave_worker::read_info(Rpl_info_handler *from) {
DBUG_TRACE;
ulong temp_group_relay_log_pos = 0;
ulong temp_group_master_log_pos = 0;
ulong temp_checkpoint_relay_log_pos = 0;
ulong temp_checkpoint_master_log_pos = 0;
ulong temp_checkpoint_seqno = 0;
ulong nbytes = 0;
uchar *buffer = (uchar *)group_executed.bitmap;
int temp_internal_id = 0;
if (from->prepare_info_for_read()) return true;
if (!!from->get_info(&temp_internal_id, 0) ||
!!from->get_info(group_relay_log_name, sizeof(group_relay_log_name),
"") ||
!!from->get_info(&temp_group_relay_log_pos, 0UL) ||
!!from->get_info(group_master_log_name, sizeof(group_master_log_name),
"") ||
!!from->get_info(&temp_group_master_log_pos, 0UL) ||
!!from->get_info(checkpoint_relay_log_name,
sizeof(checkpoint_relay_log_name), "") ||
!!from->get_info(&temp_checkpoint_relay_log_pos, 0UL) ||
!!from->get_info(checkpoint_master_log_name,
sizeof(checkpoint_master_log_name), "") ||
!!from->get_info(&temp_checkpoint_master_log_pos, 0UL) ||
!!from->get_info(&temp_checkpoint_seqno, 0UL) ||
!!from->get_info(&nbytes, 0UL) ||
!!from->get_info(buffer, (size_t)nbytes, (uchar *)nullptr) ||
/* default is empty string */
!!from->get_info(channel, sizeof(channel), ""))
return true;
assert(nbytes <= no_bytes_in_map(&group_executed));
internal_id = (uint)temp_internal_id;
group_relay_log_pos = temp_group_relay_log_pos;
group_master_log_pos = temp_group_master_log_pos;
checkpoint_relay_log_pos = temp_checkpoint_relay_log_pos;
checkpoint_master_log_pos = temp_checkpoint_master_log_pos;
worker_checkpoint_seqno = temp_checkpoint_seqno;
return false;
}
/*
This function is used to make a copy of the worker object before we
destroy it while STOP SLAVE. This new object is then used to report the
worker status until next START SLAVE following which the new worker objects
will be used.
*/
void Slave_worker::copy_values_for_PFS(ulong worker_id,
en_running_state thd_running_status,
THD *worker_thd, const Error &last_error,
Gtid_monitoring_info *monitoring_info) {
id = worker_id;
running_status = thd_running_status;
info_thd = worker_thd;
m_last_error = last_error;
monitoring_info->copy_info_to(get_gtid_monitoring_info());
}
bool Slave_worker::set_info_search_keys(Rpl_info_handler *to) {
DBUG_TRACE;
/* primary keys are Id and channel_name */
if (to->set_info(0, (int)internal_id) ||
to->set_info(LINE_FOR_CHANNEL, channel))
return true;
return false;
}
bool Slave_worker::write_info(Rpl_info_handler *to) {
DBUG_TRACE;
ulong nbytes = (ulong)no_bytes_in_map(&group_executed);
uchar *buffer = (uchar *)group_executed.bitmap;
assert(nbytes <= (c_rli->checkpoint_group + 7) / 8);
if (to->prepare_info_for_write() || to->set_info((int)internal_id) ||
to->set_info(group_relay_log_name) ||
to->set_info((ulong)group_relay_log_pos) ||
to->set_info(group_master_log_name) ||
to->set_info((ulong)group_master_log_pos) ||
to->set_info(checkpoint_relay_log_name) ||
to->set_info((ulong)checkpoint_relay_log_pos) ||
to->set_info(checkpoint_master_log_name) ||
to->set_info((ulong)checkpoint_master_log_pos) ||
to->set_info(worker_checkpoint_seqno) || to->set_info(nbytes) ||
to->set_info(buffer, (size_t)nbytes) || to->set_info(channel))
return true;
return false;
}
/**
Clean up a part of Worker info table that is regarded in
in gaps collecting at recovery.
This worker won't contribute to recovery bitmap at future
slave restart (see @c mts_recovery_groups).
@return false as success true as failure
*/
bool Slave_worker::reset_recovery_info() {
DBUG_TRACE;
set_group_master_log_name("");
set_group_master_log_pos(0);
return flush_info(true);
}
size_t Slave_worker::get_number_worker_fields() {
return sizeof(info_slave_worker_fields) / sizeof(info_slave_worker_fields[0]);
}
void Slave_worker::set_nullable_fields(MY_BITMAP *nullable_fields) {
bitmap_init(nullable_fields, nullptr,
Slave_worker::get_number_worker_fields());
bitmap_clear_all(nullable_fields);
}
const char *Slave_worker::get_master_log_name() {
Slave_job_group *ptr_g = c_rli->gaq->get_job_group(gaq_index);
return (ptr_g->checkpoint_log_name != nullptr) ? ptr_g->checkpoint_log_name
: checkpoint_master_log_name;
}
bool Slave_worker::commit_positions(Log_event *ev, Slave_job_group *ptr_g,
bool force) {
DBUG_TRACE;
/*
Initial value of checkpoint_master_log_name is learned from
group_master_log_name. The latter can be passed to Worker
at rare event of master binlog rotation.
This initialization is needed to provide to Worker info
on physical coordinates during execution of the very first group
after a rotation.
*/
if (ptr_g->group_master_log_name != nullptr) {
my_claim(ptr_g->group_master_log_name, /*claim=*/true);
strmake(group_master_log_name, ptr_g->group_master_log_name,
sizeof(group_master_log_name) - 1);
my_free(ptr_g->group_master_log_name);
ptr_g->group_master_log_name = nullptr;
strmake(checkpoint_master_log_name, group_master_log_name,
sizeof(checkpoint_master_log_name) - 1);
}
if (ptr_g->checkpoint_log_name != nullptr) {
my_claim(ptr_g->checkpoint_log_name, /*claim=*/true);
my_claim(ptr_g->checkpoint_relay_log_name, /*claim=*/true);
strmake(checkpoint_relay_log_name, ptr_g->checkpoint_relay_log_name,
sizeof(checkpoint_relay_log_name) - 1);
checkpoint_relay_log_pos = ptr_g->checkpoint_relay_log_pos;
strmake(checkpoint_master_log_name, ptr_g->checkpoint_log_name,
sizeof(checkpoint_master_log_name) - 1);
checkpoint_master_log_pos = ptr_g->checkpoint_log_pos;
my_free(ptr_g->checkpoint_log_name);
ptr_g->checkpoint_log_name = nullptr;
my_free(ptr_g->checkpoint_relay_log_name);
ptr_g->checkpoint_relay_log_name = nullptr;
bitmap_copy(&group_shifted, &group_executed);
bitmap_clear_all(&group_executed);
for (uint pos = ptr_g->shifted; pos < c_rli->checkpoint_group; pos++) {
if (bitmap_is_set(&group_shifted, pos))
bitmap_set_bit(&group_executed, pos - ptr_g->shifted);
}
}
/*
Extracts an updated relay-log name to store in Worker's rli.
*/
if (ptr_g->group_relay_log_name) {
assert(strlen(ptr_g->group_relay_log_name) + 1 <=
sizeof(group_relay_log_name));
strmake(group_relay_log_name, ptr_g->group_relay_log_name,
sizeof(group_relay_log_name) - 1);
}
assert(ptr_g->checkpoint_seqno <= (c_rli->checkpoint_group - 1));
bitmap_set_bit(&group_executed, ptr_g->checkpoint_seqno);
worker_checkpoint_seqno = ptr_g->checkpoint_seqno;
group_relay_log_pos = ev->future_event_relay_log_pos;
group_master_log_pos = ev->common_header->log_pos;
/*
Directly accessing c_rli->get_group_master_log_name() does not
represent a concurrency issue because the current code places
a synchronization point when master rotates.
*/
strmake(group_master_log_name, c_rli->get_group_master_log_name(),
sizeof(group_master_log_name) - 1);
DBUG_PRINT("mta", ("Committing worker-id %lu group source log pos %llu "
"group source log name %s checkpoint sequence number %lu.",
id, group_master_log_pos, group_master_log_name,
worker_checkpoint_seqno));
DBUG_EXECUTE_IF("mta_debug_concurrent_access",
{ mta_debug_concurrent_access++; };);
m_flag_positions_committed = true;
return flush_info(force);
}
void Slave_worker::rollback_positions(Slave_job_group *ptr_g) {
if (!is_transactional()) {
bitmap_clear_bit(&group_executed, ptr_g->checkpoint_seqno);
flush_info(false);
}
}
static void free_entry(db_worker_hash_entry *entry) {
THD *c_thd = current_thd;
DBUG_TRACE;
DBUG_PRINT("info", ("free_entry %s, %zu", entry->db, strlen(entry->db)));
assert(c_thd->system_thread == SYSTEM_THREAD_SLAVE_SQL);
/*
Although assert is correct valgrind senses entry->worker can be freed.
assert(entry->usage == 0 ||
!entry->worker || // last entry owner could have errored out
entry->worker->running_status != Slave_worker::RUNNING);
*/
mts_move_temp_tables_to_thd(c_thd, entry->temporary_tables);
entry->temporary_tables = nullptr;
my_free(const_cast<char *>(entry->db));
my_free(entry);
}
bool init_hash_workers(Relay_log_info *rli) {
DBUG_TRACE;
rli->inited_hash_workers = true;
mysql_mutex_init(key_mutex_replica_worker_hash, &rli->slave_worker_hash_lock,
MY_MUTEX_INIT_FAST);
mysql_cond_init(key_cond_slave_worker_hash, &rli->slave_worker_hash_cond);
return false;
}
void destroy_hash_workers(Relay_log_info *rli) {
DBUG_TRACE;
if (rli->inited_hash_workers) {
rli->mapping_db_to_worker.clear();
mysql_mutex_destroy(&rli->slave_worker_hash_lock);
mysql_cond_destroy(&rli->slave_worker_hash_cond);
rli->inited_hash_workers = false;
}
}
/**
Relocating temporary table reference into @c entry's table list head.
Sources can be the coordinator's and the Worker's thd->temporary_tables.
@param table TABLE instance pointer
@param thd THD instance pointer of the source of relocation
@param entry db_worker_hash_entry instance pointer
@note thd->temporary_tables can become NULL
@return the pointer to a table following the unlinked
*/
TABLE *mts_move_temp_table_to_entry(TABLE *table, THD *thd,
db_worker_hash_entry *entry) {
TABLE *ret = table->next;
if (table->prev) {
table->prev->next = table->next;
if (table->prev->next) table->next->prev = table->prev;
} else {
/* removing the first item from the list */
assert(table == thd->temporary_tables);
thd->temporary_tables = table->next;
if (thd->temporary_tables) table->next->prev = nullptr;
}
table->next = entry->temporary_tables;
table->prev = nullptr;
if (table->next) table->next->prev = table;
entry->temporary_tables = table;
return ret;
}
/**
Relocation of the list of temporary tables to thd->temporary_tables.
@param thd THD instance pointer of the destination
@param temporary_tables
the source temporary_tables list
@note destroying references to the source list, if necessary,
is left to the caller.
@return the post-merge value of thd->temporary_tables.
*/
TABLE *mts_move_temp_tables_to_thd(THD *thd, TABLE *temporary_tables) {
DBUG_TRACE;
TABLE *table = temporary_tables;
if (!table) return nullptr;
// accept only if this is the start of the list.
assert(!table->prev);
// walk along the source list and associate the tables with thd
do {
table->in_use = thd;
} while (table->next && (table = table->next));
// link the former list against the tail of the source list
if (thd->temporary_tables) thd->temporary_tables->prev = table;
table->next = thd->temporary_tables;
thd->temporary_tables = temporary_tables;
return thd->temporary_tables;
}
/**
Relocating references of temporary tables of a database
of the entry argument from THD into the entry.
@param thd THD pointer of the source temporary_tables list
@param entry a pointer to db_worker_hash_entry record
containing database descriptor and temporary_tables list.
*/
static void move_temp_tables_to_entry(THD *thd, db_worker_hash_entry *entry) {
for (TABLE *table = thd->temporary_tables; table;) {
if (strcmp(table->s->db.str, entry->db) == 0) {
// table pointer is shifted inside the function
table = mts_move_temp_table_to_entry(table, thd, entry);
} else {
table = table->next;
}
}
}
/**
The function produces a reference to the struct of a Worker
that has been or will be engaged to process the @c dbname -keyed partition
(D). It checks a local to Coordinator CGAP list first and returns
@c last_assigned_worker when found (todo: assert).
Otherwise, the partition is appended to the current group list:
CGAP .= D
here .= is concatenate operation,
and a possible D's Worker id is searched in Assigned Partition Hash
(APH) that collects tuples (P, W_id, U, mutex, cond).
In case not found,
W_d := W_c unless W_c is NULL.
When W_c is NULL it is assigned to a least occupied as defined by
@c get_least_occupied_worker().
W_d := W_c := W_{least_occupied}
APH .= a new (D, W_d, 1)
In a case APH contains W_d == W_c, (assert U >= 1)
update APH set U++ where APH.P = D
The case APH contains a W_d != W_c != NULL assigned to D-partition represents
the hashing conflict and is handled as the following:
a. marks the record of APH with a flag requesting to signal in the
cond var when `U' the usage counter drops to zero by the other Worker;
b. waits for the other Worker to finish tasks on that partition and
gets the signal;
c. updates the APH record to point to the first Worker (naturally, U := 1),
scheduled the event, and goes back into the parallel mode
@param dbname pointer to c-string containing database name
It can be empty string to indicate specific locking
to facilitate sequential applying.
@param rli pointer to Coordinators relay-log-info instance
@param ptr_entry reference to a pointer to the resulted entry in
the Assigned Partition Hash where
the entry's pointer is stored at return.
@param need_temp_tables
if false migration of temporary tables not needed
@param last_worker caller opts for this Worker, it must be
rli->last_assigned_worker if one is determined.
@note modifies CGAP, APH and unlinks @c dbname -keyd temporary tables
from C's thd->temporary_tables to move them into the entry record.
@return the pointer to a Worker struct
*/
Slave_worker *map_db_to_worker(const char *dbname, Relay_log_info *rli,
db_worker_hash_entry **ptr_entry,
bool need_temp_tables,
Slave_worker *last_worker) {
Slave_worker_array *workers = &rli->workers;
THD *thd = rli->info_thd;
DBUG_TRACE;
assert(!rli->last_assigned_worker ||
rli->last_assigned_worker == last_worker);
assert(is_mts_db_partitioned(rli));
if (!rli->inited_hash_workers) return nullptr;
db_worker_hash_entry *entry = nullptr;
size_t dblength = strlen(dbname);
// Search in CGAP
for (db_worker_hash_entry **it = rli->curr_group_assigned_parts.begin();
it != rli->curr_group_assigned_parts.end(); ++it) {
entry = *it;
if ((uchar)entry->db_len != dblength)
continue;
else if (strncmp(entry->db, const_cast<char *>(dbname), dblength) == 0) {
*ptr_entry = entry;
return last_worker;
}
}
DBUG_PRINT("info", ("Searching for %s, %zu", dbname, dblength));
mysql_mutex_lock(&rli->slave_worker_hash_lock);
std::string key(dbname, dblength);
entry = find_or_nullptr(rli->mapping_db_to_worker, key);
if (!entry) {
DBUG_PRINT("debug", ("NO ENTRY found for: %s!", dbname));
/*
The database name was not found which means that a worker never
processed events from that database. In such case, we need to
map the database to a worker my inserting an entry into the
hash map.
*/
bool ret;
char *db = nullptr;
mysql_mutex_unlock(&rli->slave_worker_hash_lock);
DBUG_PRINT("info", ("Inserting %s, %zu", dbname, dblength));
/*
Allocate an entry to be inserted and if the operation fails
an error is returned.
*/
if (!(db = (char *)my_malloc(key_memory_db_worker_hash_entry, dblength + 1,
MYF(0))))
goto err;
if (!(entry = (db_worker_hash_entry *)my_malloc(
key_memory_db_worker_hash_entry, sizeof(db_worker_hash_entry),
MYF(0)))) {
my_free(db);
goto err;
}
my_stpcpy(db, dbname);
entry->db = db;
entry->db_len = strlen(db);
entry->usage = 1;
entry->temporary_tables = nullptr;
/*
Unless \exists the last assigned Worker, get a free worker based
on a policy described in the function get_least_occupied_worker().
*/
mysql_mutex_lock(&rli->slave_worker_hash_lock);
entry->worker = (!last_worker)
? get_least_occupied_worker(rli, workers, nullptr)
: last_worker;
entry->worker->usage_partition++;
if (rli->mapping_db_to_worker.size() > mts_partition_hash_soft_max) {
/*
remove zero-usage (todo: rare or long ago scheduled) records.
Free the element if the usage of the hash entry is 0 or not.
*/
for (auto it = rli->mapping_db_to_worker.begin();
it != rli->mapping_db_to_worker.end();) {
assert(!entry->temporary_tables || !entry->temporary_tables->prev);
assert(!thd->temporary_tables || !thd->temporary_tables->prev);
db_worker_hash_entry *zero_entry = it->second.get();
if (zero_entry->usage == 0) {
mts_move_temp_tables_to_thd(thd, zero_entry->temporary_tables);
zero_entry->temporary_tables = nullptr;