-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathrpl_gtid_set.cc
1561 lines (1432 loc) · 50.3 KB
/
rpl_gtid_set.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, 2024, 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 "my_config.h"
#include <inttypes.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include "mysql/components/services/bits/psi_mutex_bits.h"
#include "mysql/components/services/log_builtins.h"
#include "mysql/my_loglevel.h"
#include "mysql/utils/enumeration_utils.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <algorithm>
#include <list>
#include "m_string.h" // my_strtoll
#include "my_byteorder.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_stacktrace.h" // my_safe_printf_stderr
#include "my_sys.h"
#include "mysql/binlog/event/control_events.h"
#include "mysql/psi/mysql_mutex.h"
#include "mysql/service_mysql_alloc.h"
#include "mysql/strings/int2str.h"
#include "prealloced_array.h"
#include "sql/rpl_gtid.h"
#include "sql/sql_const.h"
#include "sql/thr_malloc.h"
#ifdef MYSQL_SERVER
#include "mysql/psi/psi_memory.h"
#include "mysqld_error.h" // ER_*
#include "sql/log.h"
#endif
#ifndef MYSQL_SERVER
#include "client/mysqlbinlog.h"
#endif
PSI_memory_key key_memory_Gtid_set_to_string;
PSI_memory_key key_memory_Gtid_set_Interval_chunk;
using std::list;
using std::max;
using std::min;
#define MAX_NEW_CHUNK_ALLOCATE_TRIES 10
PSI_mutex_key Gtid_set::key_gtid_executed_free_intervals_mutex;
const Gtid_set::String_format Gtid_set::default_string_format = {
"", "", ":", ":", "-", ":", ",\n", "", 0, 0, 1, 1, 1, 1, 2, 0};
const Gtid_set::String_format Gtid_set::sql_string_format = {
"'", "'", ":", ":", "-", ":", "',\n'", "''", 1, 1, 1, 1, 1, 1, 4, 2};
const Gtid_set::String_format Gtid_set::commented_string_format = {
"# ", "", ":", ":", "-", ":", ",\n# ", "# [empty]", 2, 0, 1, 1, 1, 1, 4, 9};
Gtid_set::Gtid_set(Tsid_map *_tsid_map, Checkable_rwlock *_tsid_lock)
: tsid_lock(_tsid_lock),
tsid_map(_tsid_map),
m_intervals(key_memory_Gtid_set_Interval_chunk) {
init();
}
Gtid_set::Gtid_set(Tsid_map *_tsid_map, const char *text,
enum_return_status *status, Checkable_rwlock *_tsid_lock)
: tsid_lock(_tsid_lock),
tsid_map(_tsid_map),
m_intervals(key_memory_Gtid_set_Interval_chunk) {
assert(_tsid_map != nullptr);
init();
*status = add_gtid_text(text);
}
void Gtid_set::claim_memory_ownership(bool claim) {
m_intervals.claim_memory_ownership(claim);
Interval_chunk *chunk = chunks;
while (chunk != nullptr) {
Interval_chunk *next_chunk = chunk->next;
my_claim(chunk, claim);
chunk = next_chunk;
}
}
void Gtid_set::init() {
DBUG_TRACE;
has_cached_string_length = false;
cached_string_length = 0;
cached_string_format = nullptr;
chunks = nullptr;
free_intervals = nullptr;
if (tsid_lock)
mysql_mutex_init(key_gtid_executed_free_intervals_mutex,
&free_intervals_mutex, nullptr);
#ifndef NDEBUG
n_chunks = 0;
#endif
}
Gtid_set::~Gtid_set() {
DBUG_TRACE;
Interval_chunk *chunk = chunks;
while (chunk != nullptr) {
Interval_chunk *next_chunk = chunk->next;
my_free(chunk);
chunk = next_chunk;
#ifndef NDEBUG
n_chunks--;
#endif
}
assert(n_chunks == 0);
if (tsid_lock) mysql_mutex_destroy(&free_intervals_mutex);
}
enum_return_status Gtid_set::ensure_sidno(rpl_sidno sidno) {
DBUG_TRACE;
if (tsid_lock != nullptr) tsid_lock->assert_some_lock();
DBUG_PRINT("info", ("sidno=%d get_max_sidno()=%d tsid_map=%p "
"sid_map->get_max_sidno()=%d",
sidno, get_max_sidno(), tsid_map,
tsid_map != nullptr ? tsid_map->get_max_sidno() : 0));
assert(sidno <= tsid_map->get_max_sidno());
assert(get_max_sidno() <= tsid_map->get_max_sidno());
rpl_sidno max_sidno = get_max_sidno();
if (sidno > max_sidno) {
/*
Not all Gtid_sets are protected by an rwlock. But if this
Gtid_set is, we assume that the read lock has been taken.
Then we temporarily upgrade it to a write lock while resizing
the array, and then we restore it to a read lock at the end.
*/
bool is_wrlock = false;
if (tsid_lock != nullptr) {
is_wrlock = tsid_lock->is_wrlock();
if (!is_wrlock) {
tsid_lock->unlock();
tsid_lock->wrlock();
// maybe a concurrent thread already resized the Gtid_set
// while we released the lock; check the condition again
if (sidno <= max_sidno) {
tsid_lock->unlock();
tsid_lock->rdlock();
RETURN_OK;
}
}
}
Interval *null_p = nullptr;
for (rpl_sidno i = max_sidno; i < sidno; i++)
if (m_intervals.push_back(null_p)) goto error;
if (tsid_lock != nullptr) {
if (!is_wrlock) {
tsid_lock->unlock();
tsid_lock->rdlock();
}
}
}
RETURN_OK;
error:
BINLOG_ERROR(("Out of memory."), (ER_OUT_OF_RESOURCES, MYF(0)));
RETURN_REPORTED_ERROR;
}
void Gtid_set::add_interval_memory_lock_taken(int n_ivs, Interval *ivs) {
DBUG_TRACE;
assert_free_intervals_locked();
// make ivs a linked list
for (int i = 0; i < n_ivs - 1; i++) ivs[i].next = &(ivs[i + 1]);
Interval_iterator ivit(this);
ivs[n_ivs - 1].next = ivit.get();
// add intervals to list of free intervals
ivit.set(&(ivs[0]));
}
void Gtid_set::create_new_chunk(int size) {
DBUG_TRACE;
int i = 0;
Interval_chunk *new_chunk = nullptr;
assert_free_intervals_locked();
/*
Try to allocate the new chunk in MAX_NEW_CHUNK_ALLOCATE_TRIES
tries when encountering 'out of memory' situation.
*/
while (i < MAX_NEW_CHUNK_ALLOCATE_TRIES) {
/*
Allocate the new chunk. one element is already pre-allocated, so
we only add size-1 elements to the size of the struct.
*/
new_chunk = (Interval_chunk *)my_malloc(
key_memory_Gtid_set_Interval_chunk,
sizeof(Interval_chunk) + sizeof(Interval) * (size - 1), MYF(MY_WME));
if (new_chunk != nullptr) {
#ifdef MYSQL_SERVER
if (i > 0)
LogErr(WARNING_LEVEL, ER_RPL_GTID_MEMORY_FINALLY_AVAILABLE, i + 1);
#endif
break;
}
/* Sleep 1 microsecond per try to avoid temporary 'out of memory' */
my_sleep(1);
i++;
}
/*
Terminate the server after failed to allocate the new chunk
in MAX_NEW_CHUNK_ALLOCATE_TRIES tries.
*/
if (MAX_NEW_CHUNK_ALLOCATE_TRIES == i ||
DBUG_EVALUATE_IF("rpl_simulate_new_chunk_allocate_failure", 1, 0)) {
my_safe_print_system_time();
my_safe_printf_stderr("%s",
"[Fatal] Out of memory while allocating "
"a new chunk of intervals for storing GTIDs.\n");
_exit(MYSQLD_FAILURE_EXIT);
}
// store the chunk in the list of chunks
new_chunk->next = chunks;
chunks = new_chunk;
#ifndef NDEBUG
n_chunks++;
#endif
// add the intervals in the chunk to the list of free intervals
add_interval_memory_lock_taken(size, new_chunk->intervals);
}
void Gtid_set::get_free_interval(Interval **out) {
DBUG_TRACE;
assert_free_intervals_locked();
Interval_iterator ivit(this);
bool simulate_failure = DBUG_EVALUATE_IF(
"rpl_gtid_get_free_interval_simulate_out_of_memory", true, false);
if (simulate_failure) DBUG_SET("+d,rpl_simulate_new_chunk_allocate_failure");
if (ivit.get() == nullptr || simulate_failure)
create_new_chunk(CHUNK_GROW_SIZE);
*out = ivit.get();
ivit.set((*out)->next);
}
void Gtid_set::put_free_interval(Interval *iv) {
DBUG_TRACE;
assert_free_intervals_locked();
Interval_iterator ivit(this);
iv->next = ivit.get();
ivit.set(iv);
}
void Gtid_set::clear() {
DBUG_TRACE;
has_cached_string_length = false;
cached_string_length = 0;
rpl_sidno max_sidno = get_max_sidno();
if (max_sidno == 0) return;
Interval_iterator free_ivit(this);
for (rpl_sidno sidno = 1; sidno <= max_sidno; sidno++) {
/*
Link in this list of intervals at the end of the list of
free intervals.
*/
Interval_iterator ivit(this, sidno);
Interval *iv = ivit.get();
if (iv != nullptr) {
// find the end of the list of free intervals
while (free_ivit.get() != nullptr) free_ivit.next();
// append the present list
free_ivit.set(iv);
// clear the pointer to the head of this list
ivit.set(nullptr);
}
}
}
void Gtid_set::clear_set_and_tsid_map() {
DBUG_TRACE;
clear();
/*
Cleaning the TSID map without cleaning up the Gtid_set intervals may lead
to a condition were the Gtid_set->get_max_sidno() will be greater than the
Tsid_map->get_max_sidno().
*/
m_intervals.clear();
tsid_map->clear();
assert(get_max_sidno() == tsid_map->get_max_sidno());
}
void Gtid_set::add_gno_interval(Interval_iterator *ivitp, rpl_gno start,
rpl_gno end, Free_intervals_lock *lock) {
DBUG_TRACE;
assert(start > 0);
assert(start < end);
DBUG_PRINT("info", ("start=%" PRId64 " end=%" PRId64, start, end));
Interval *iv;
Interval_iterator ivit = *ivitp;
has_cached_string_length = false;
cached_string_length = 0;
while ((iv = ivit.get()) != nullptr) {
if (iv->end >= start) {
if (iv->start > end)
// (start, end) is strictly before the current interval
break;
// (start, end) and (iv->start, iv->end) touch or intersect.
// Save the start of the merged interval.
if (iv->start < start) start = iv->start;
// Remove the current interval as long as the new interval
// intersects with the next interval.
while (iv->next && end >= iv->next->start) {
lock->lock_if_not_locked();
ivit.remove(this);
iv = ivit.get();
}
// Store the interval in the current interval.
iv->start = start;
if (iv->end < end) iv->end = end;
*ivitp = ivit;
return;
}
ivit.next();
}
/*
We come here if the interval cannot be combined with any existing
interval: it is after the previous interval (if any) and before
the current interval (if any). So we allocate a new interval and
insert it at the current position.
*/
Interval *new_iv;
lock->lock_if_not_locked();
get_free_interval(&new_iv);
new_iv->start = start;
new_iv->end = end;
ivit.insert(new_iv);
*ivitp = ivit;
}
void Gtid_set::remove_gno_interval(Interval_iterator *ivitp, rpl_gno start,
rpl_gno end, Free_intervals_lock *lock) {
DBUG_TRACE;
assert(start < end);
Interval_iterator ivit = *ivitp;
Interval *iv;
has_cached_string_length = false;
cached_string_length = -1;
// Skip intervals of 'this' that are completely before the removed interval.
while (true) {
iv = ivit.get();
if (iv == nullptr) goto ok;
if (iv->end > start) break;
ivit.next();
}
// Now iv ends after the beginning of the removed interval.
assert(iv != nullptr && iv->end > start);
if (iv->start < start) {
if (iv->end > end) {
// iv cuts also the end of the removed interval: split iv in two
Interval *new_iv;
lock->lock_if_not_locked();
get_free_interval(&new_iv);
new_iv->start = end;
new_iv->end = iv->end;
iv->end = start;
ivit.next();
ivit.insert(new_iv);
goto ok;
}
// iv cuts the beginning but not the end of the removed interval:
// truncate iv, and iterate one step to next interval
iv->end = start;
ivit.next();
iv = ivit.get();
if (iv == nullptr) goto ok;
}
// Now iv starts after the beginning of the removed interval.
assert(iv != nullptr && iv->start >= start);
while (iv->end <= end) {
// iv ends before the end of the removed interval, so it is
// completely covered: remove iv.
lock->lock_if_not_locked();
ivit.remove(this);
iv = ivit.get();
if (iv == nullptr) goto ok;
}
// Now iv ends after the removed interval.
assert(iv != nullptr && iv->end > end);
if (iv->start < end) {
// iv begins before the end of the removed interval: truncate iv
iv->start = end;
}
ok:
*ivitp = ivit;
}
rpl_gno parse_gno(const char **s) {
char *endp;
long long ret = my_strtoll(*s, &endp, 0);
if (ret < 0 || ret >= GNO_END) return -1;
*s = endp;
return static_cast<rpl_gno>(ret);
}
int format_gno(char *s, rpl_gno gno) {
return static_cast<int>(longlong10_to_str(gno, s, 10) - s);
}
enum_return_status Gtid_set::add_gtid(const mysql::gtid::Gtid >id) {
DBUG_TRACE;
assert(tsid_map != nullptr);
if (tsid_lock != nullptr) tsid_lock->assert_some_wrlock();
rpl_sidno sidno = tsid_map->add_tsid(gtid.get_tsid());
if (sidno <= 0) {
RETURN_REPORTED_ERROR;
}
PROPAGATE_REPORTED_ERROR(ensure_sidno(sidno));
_add_gtid(sidno, gtid.get_gno());
return RETURN_STATUS_OK;
}
enum_return_status Gtid_set::add_gtid_text(const char *text, bool *anonymous,
bool *starts_with_plus) {
DBUG_TRACE;
assert(tsid_map != nullptr);
if (tsid_lock != nullptr) tsid_lock->assert_some_wrlock();
const char *s = text;
DBUG_PRINT("info", ("adding '%s'", text));
if (anonymous != nullptr) *anonymous = false;
if (starts_with_plus != nullptr) {
SKIP_WHITESPACE();
if (*s == '+') {
*starts_with_plus = true;
s++;
} else
*starts_with_plus = false;
}
SKIP_WHITESPACE();
if (*s == 0) {
DBUG_PRINT("info", ("'%s' is empty", text));
RETURN_OK;
}
Free_intervals_lock lock(this);
DBUG_PRINT("info", ("'%s' not only whitespace", text));
// Allocate space for all intervals at once, if nothing is allocated.
if (chunks == nullptr) {
// compute number of intervals in text: it is equal to the number of
// colons
int n_intervals = 0;
text = s;
for (; *s; s++)
if (*s == Gtid::gtid_separator) n_intervals++;
// allocate all intervals in one chunk
lock.lock_if_not_locked();
create_new_chunk(n_intervals);
lock.unlock_if_locked();
s = text;
}
while (true) {
// Skip commas (we allow empty SID:GNO specifications).
while (*s == ',') {
s++;
SKIP_WHITESPACE();
}
// We allow empty Gtid_sets containing only commas.
if (*s == 0) {
DBUG_PRINT("info", ("successfully parsed"));
RETURN_OK;
}
if (anonymous != nullptr && strncmp(s, "ANONYMOUS", 9) == 0) {
*anonymous = true;
s += 9;
} else {
// Parse TSID.
mysql::gtid::Tsid tsid;
std::size_t characters_read = tsid.from_cstring(s);
if (characters_read == 0) {
DBUG_PRINT("info",
("expected UUID; found garbage '%.80s' at char %d in '%s'",
s, (int)(s - text), text));
goto parse_error;
}
s += characters_read;
rpl_sidno sidno = tsid_map->add_tsid(tsid);
if (sidno <= 0) {
RETURN_REPORTED_ERROR;
}
PROPAGATE_REPORTED_ERROR(ensure_sidno(sidno));
SKIP_WHITESPACE();
while (*s == Gtid::gtid_separator) {
// Skip gtid_separator.
++s;
// parse Tag if any
mysql::gtid::Tag tag;
auto tag_chars = tag.from_cstring(s);
s += tag_chars;
SKIP_WHITESPACE();
if (tag_chars > 0) {
tsid = mysql::gtid::Tsid(tsid.get_uuid(), tag);
sidno = tsid_map->add_tsid(tsid);
if (sidno <= 0) {
RETURN_REPORTED_ERROR;
}
PROPAGATE_REPORTED_ERROR(ensure_sidno(sidno));
} else {
Interval_iterator ivit(this, sidno);
// Read start of interval.
rpl_gno start = parse_gno(&s);
if (start <= 0) {
if (start == 0)
DBUG_PRINT("info", ("expected positive NUMBER; found zero "
"('%.80s') at char %d in '%s'",
s - 1, (int)(s - text) - 1, text));
else
DBUG_PRINT("info", ("expected positive NUMBER; found zero or "
"garbage '%.80s' at char %d in '%s'",
s, (int)(s - text), text));
goto parse_error;
}
SKIP_WHITESPACE();
// Read end of interval.
rpl_gno end;
if (*s == '-') {
s++;
end = parse_gno(&s);
if (end < 0) {
DBUG_PRINT(
"info",
("expected NUMBER; found garbage '%.80s' at char %d in '%s'",
s, (int)(s - text), text));
goto parse_error;
}
end++;
SKIP_WHITESPACE();
} else
end = start + 1;
if (end > start) {
// Add interval. Use the existing iterator position if the
// current interval does not begin before it. Otherwise iterate
// from the beginning.
Interval *current = ivit.get();
if (current == nullptr || start < current->start)
ivit.init(this, sidno);
add_gno_interval(&ivit, start, end, &lock);
}
}
}
}
// Must be end of string or comma. (Commas are consumed and
// end-of-loop is detected at the beginning of the loop.)
if (*s != ',' && *s != 0) {
DBUG_PRINT("info", ("expected end of string, UUID, or :NUMBER; found "
"garbage '%.80s' at char %d in '%s'",
s, (int)(s - text), text));
goto parse_error;
}
}
assert(0);
parse_error:
BINLOG_ERROR(("Malformed Gtid_set specification '%.200s'.", text),
(ER_MALFORMED_GTID_SET_SPECIFICATION, MYF(0), text));
RETURN_REPORTED_ERROR;
}
bool Gtid_set::is_valid(const char *text) {
DBUG_TRACE;
const char *s = text;
SKIP_WHITESPACE();
if (*s == '+') s++;
SKIP_WHITESPACE();
do {
// Skip commas (we allow empty SID:GNO specifications).
while (*s == ',') {
s++;
SKIP_WHITESPACE();
}
if (*s == 0) return true;
// Parse SID.
mysql::gtid::Uuid uuid;
if (uuid.parse(s, mysql::gtid::Uuid::TEXT_LENGTH) != 0) {
return false;
}
s += mysql::gtid::Uuid::TEXT_LENGTH;
SKIP_WHITESPACE();
mysql::gtid::Tag tag; // empty tag
// Iterate over intervals.
while (*s == Gtid::gtid_separator) {
// Skip gtid_separator.
s++;
// Parse the next "gtid_separator" separated item,
// which may be either a tag or an interval
SKIP_WHITESPACE();
mysql::gtid::Tag tag_read;
auto tag_len = tag_read.from_cstring(s);
s += tag_len;
if (tag_len > 0) {
tag = tag_read;
} else {
// Read start of interval.
if (parse_gno(&s) <= 0) return false;
SKIP_WHITESPACE();
// Read end of interval
if (*s == '-') {
s++;
if (parse_gno(&s) < 0) return false;
SKIP_WHITESPACE();
}
}
}
} while (*s == ',');
if (*s != 0) return false;
return true;
}
void Gtid_set::add_gno_intervals(rpl_sidno sidno,
Const_interval_iterator other_ivit,
Free_intervals_lock *lock) {
DBUG_TRACE;
assert(sidno >= 1 && sidno <= get_max_sidno());
const Interval *other_iv;
Interval_iterator ivit(this, sidno);
while ((other_iv = other_ivit.get()) != nullptr) {
add_gno_interval(&ivit, other_iv->start, other_iv->end, lock);
other_ivit.next();
}
}
void Gtid_set::remove_gno_intervals(rpl_sidno sidno,
Const_interval_iterator other_ivit,
Free_intervals_lock *lock) {
DBUG_TRACE;
assert(sidno >= 1 && sidno <= get_max_sidno());
const Interval *other_iv;
Interval_iterator ivit(this, sidno);
while ((other_iv = other_ivit.get()) != nullptr) {
remove_gno_interval(&ivit, other_iv->start, other_iv->end, lock);
if (ivit.get() == nullptr) break;
other_ivit.next();
}
}
void Gtid_set::remove_intervals_for_sidno(Gtid_set *other, rpl_sidno sidno) {
// Currently only works if this and other use the same Tsid_map.
assert(other->tsid_map == tsid_map);
Const_interval_iterator other_ivit(other, sidno);
Free_intervals_lock lock(this);
remove_gno_intervals(sidno, other_ivit, &lock);
}
enum_return_status Gtid_set::add_gtid_set(const Gtid_set *other) {
/*
@todo refactor this and remove_gtid_set to avoid duplicated code
*/
DBUG_TRACE;
if (tsid_lock != nullptr) tsid_lock->assert_some_wrlock();
rpl_sidno max_other_sidno = other->get_max_sidno();
Free_intervals_lock lock(this);
if (other->tsid_map == tsid_map) {
PROPAGATE_REPORTED_ERROR(ensure_sidno(max_other_sidno));
for (rpl_sidno sidno = 1; sidno <= max_other_sidno; sidno++)
add_gno_intervals(sidno, Const_interval_iterator(other, sidno), &lock);
} else {
Tsid_map *other_tsid_map = other->tsid_map;
Checkable_rwlock *other_tsid_lock = other->tsid_lock;
if (other_tsid_lock != nullptr) other_tsid_lock->assert_some_wrlock();
for (rpl_sidno other_sidno = 1; other_sidno <= max_other_sidno;
other_sidno++) {
Const_interval_iterator other_ivit(other, other_sidno);
if (other_ivit.get() != nullptr) {
const auto &tsid = other_tsid_map->sidno_to_tsid(other_sidno);
rpl_sidno this_sidno = tsid_map->add_tsid(tsid);
if (this_sidno <= 0) RETURN_REPORTED_ERROR;
PROPAGATE_REPORTED_ERROR(ensure_sidno(this_sidno));
add_gno_intervals(this_sidno, other_ivit, &lock);
}
}
}
RETURN_OK;
}
void Gtid_set::remove_gtid_set(const Gtid_set *other) {
DBUG_TRACE;
if (tsid_lock != nullptr) tsid_lock->assert_some_wrlock();
rpl_sidno max_other_sidno = other->get_max_sidno();
Free_intervals_lock lock(this);
if (other->tsid_map == tsid_map) {
rpl_sidno max_sidno = min(max_other_sidno, get_max_sidno());
for (rpl_sidno sidno = 1; sidno <= max_sidno; sidno++)
remove_gno_intervals(sidno, Const_interval_iterator(other, sidno), &lock);
} else {
Tsid_map *other_tsid_map = other->tsid_map;
Checkable_rwlock *other_tsid_lock = other->tsid_lock;
if (other_tsid_lock != nullptr) other_tsid_lock->assert_some_wrlock();
for (rpl_sidno other_sidno = 1; other_sidno <= max_other_sidno;
other_sidno++) {
Const_interval_iterator other_ivit(other, other_sidno);
if (other_ivit.get() != nullptr) {
const auto &tsid = other_tsid_map->sidno_to_tsid(other_sidno);
rpl_sidno this_sidno = tsid_map->tsid_to_sidno(tsid);
if (this_sidno != 0)
remove_gno_intervals(this_sidno, other_ivit, &lock);
}
}
}
}
bool Gtid_set::contains_gtid(rpl_sidno sidno, rpl_gno gno) const {
DBUG_TRACE;
if (tsid_lock != nullptr) tsid_lock->assert_some_lock();
if (sidno > get_max_sidno()) return false;
assert(sidno >= 1);
assert(gno >= 1);
Const_interval_iterator ivit(this, sidno);
const Interval *iv;
while ((iv = ivit.get()) != nullptr) {
if (gno < iv->start)
return false;
else if (gno < iv->end)
return true;
ivit.next();
}
return false;
}
rpl_gno Gtid_set::get_last_gno(rpl_sidno sidno) const {
DBUG_TRACE;
rpl_gno gno = 0;
if (tsid_lock != nullptr) tsid_lock->assert_some_lock();
if (sidno > get_max_sidno()) return gno;
Const_interval_iterator ivit(this, sidno);
const Gtid_set::Interval *iv = ivit.get();
while (iv != nullptr) {
gno = iv->end - 1;
ivit.next();
iv = ivit.get();
}
return gno;
}
long Gtid_set::to_string(char **buf_arg, bool need_lock,
const Gtid_set::String_format *sf_arg) const {
DBUG_TRACE;
if (tsid_lock != nullptr) {
if (need_lock)
tsid_lock->wrlock();
else
tsid_lock->assert_some_wrlock();
}
size_t len = get_string_length(sf_arg);
*buf_arg =
(char *)my_malloc(key_memory_Gtid_set_to_string, len + 1, MYF(MY_WME));
if (*buf_arg == nullptr) return -1;
to_string(*buf_arg, false /*need_lock*/, sf_arg);
if (tsid_lock != nullptr && need_lock) tsid_lock->unlock();
return len;
}
size_t Gtid_set::to_string(char *buf, bool need_lock,
const Gtid_set::String_format *sf) const {
DBUG_TRACE;
assert(tsid_map != nullptr);
if (tsid_lock != nullptr) {
if (need_lock)
tsid_lock->wrlock();
else
tsid_lock->assert_some_wrlock();
}
if (sf == nullptr) sf = &default_string_format;
if (sf->empty_set_string != nullptr && is_empty()) {
memcpy(buf, sf->empty_set_string, sf->empty_set_string_length);
buf[sf->empty_set_string_length] = '\0';
if (tsid_lock != nullptr && need_lock) tsid_lock->unlock();
return sf->empty_set_string_length;
}
assert(get_max_sidno() <= tsid_map->get_max_sidno());
memcpy(buf, sf->begin, sf->begin_length);
char *s = buf + sf->begin_length;
bool first_sidno = true;
mysql::gtid::Uuid prev_uuid;
for (const auto &tsid_it : tsid_map->get_sorted_sidno()) {
rpl_sidno sidno = tsid_it.second;
if (contains_sidno(sidno)) {
Const_interval_iterator ivit(this, sidno);
const Interval *iv = ivit.get();
auto tsid = tsid_map->sidno_to_tsid(sidno);
// save UUID
if (first_sidno || tsid.get_uuid() != prev_uuid) {
if (first_sidno == false) {
memcpy(s, sf->gno_sid_separator, sf->gno_sid_separator_length);
s += sf->gno_sid_separator_length;
}
s += tsid.get_uuid().to_string(s);
prev_uuid = tsid.get_uuid();
first_sidno = false;
}
// save tag and intervals
if (tsid.is_tagged()) {
memcpy(s, sf->tag_sid_separator, strlen(sf->tag_sid_separator));
s += strlen(sf->tag_sid_separator);
s += tsid.get_tag().to_string(s);
}
bool first_gno = true;
do {
if (first_gno) {
memcpy(s, sf->tsid_gno_separator, sf->tsid_gno_separator_length);
s += sf->tsid_gno_separator_length;
} else {
memcpy(s, sf->gno_gno_separator, sf->gno_gno_separator_length);
s += sf->gno_gno_separator_length;
}
s += format_gno(s, iv->start);
if (iv->end > iv->start + 1) {
memcpy(s, sf->gno_start_end_separator,
sf->gno_start_end_separator_length);
s += sf->gno_start_end_separator_length;
s += format_gno(s, iv->end - 1);
}
ivit.next();
iv = ivit.get();
} while (iv != nullptr);
}
}
memcpy(s, sf->end, sf->end_length);
s += sf->end_length;
*s = '\0';
DBUG_PRINT("info", ("ret='%s' strlen(s)=%zu s-buf=%lu get_string_length=%llu",
buf, strlen(buf), (ulong)(s - buf),
static_cast<unsigned long long>(get_string_length(sf))));
assert((ulong)(s - buf) == get_string_length(sf));
if (tsid_lock != nullptr && need_lock) tsid_lock->unlock();
return (int)(s - buf);
}
void Gtid_set::get_gtid_intervals(list<Gtid_interval> *gtid_intervals) const {
DBUG_TRACE;
assert(tsid_map != nullptr);
if (tsid_lock != nullptr) tsid_lock->assert_some_wrlock();
assert(get_max_sidno() <= tsid_map->get_max_sidno());
for (const auto &sid_it : tsid_map->get_sorted_sidno()) {
rpl_sidno sidno = sid_it.second;
if (contains_sidno(sidno)) {
Const_interval_iterator ivit(this, sidno);
const Interval *iv = ivit.get();
while (iv != nullptr) {
Gtid_interval gtid_interval;
gtid_interval.set(sidno, iv->start, iv->end - 1);
gtid_intervals->push_back(gtid_interval);
ivit.next();
iv = ivit.get();
};
}
}
}
/**
Returns the length that the given rpl_sidno (64 bit integer) would
have, if it was encoded as a string.
*/
static size_t get_string_length(rpl_gno gno) {
assert(gno >= 1);
assert(gno < GNO_END);
rpl_gno tmp_gno = gno;
size_t len = 0;
do {
tmp_gno /= 10;
len++;
} while (tmp_gno != 0);
#ifndef NDEBUG
char buf[22];
assert(snprintf(buf, 22, "%" PRId64, gno) == ssize_t(len));
#endif
return len;
}
bool Gtid_set::contains_tags() const {
assert(tsid_map != nullptr);
for (const auto &tsid_it : tsid_map->get_sorted_sidno()) {
rpl_sidno sidno = tsid_it.second;
if (contains_sidno(sidno)) {
Const_interval_iterator ivit(this, sidno);
if (ivit.get() != nullptr) {
auto tsid = tsid_map->sidno_to_tsid(sidno);
if (tsid.is_tagged()) {
return true;
}
}
}
}
return false;
}
size_t Gtid_set::get_string_length(const Gtid_set::String_format *sf) const {
assert(tsid_map != nullptr);
if (tsid_lock != nullptr) tsid_lock->assert_some_wrlock();
if (sf == nullptr) sf = &default_string_format;
if (has_cached_string_length == false || cached_string_format != sf) {
int n_sids = 0, n_sidnos = 0, n_intervals = 0, n_long_intervals = 0;
size_t total_interval_length = 0;
size_t total_tsids_length = 0;
mysql::gtid::Uuid prev_uuid;
bool first_sidno = true;
for (const auto &tsid_it : tsid_map->get_sorted_sidno()) {
rpl_sidno sidno = tsid_it.second;
if (contains_sidno(sidno)) {
Const_interval_iterator ivit(this, sidno);
const Interval *iv = ivit.get();
if (iv != nullptr) {
auto tsid = tsid_map->sidno_to_tsid(sidno);
++n_sidnos;
if (tsid.is_tagged()) {
total_tsids_length +=
tsid.get_tag().get_length() + sf->tag_sid_separator_length;
}
if (first_sidno || tsid.get_uuid() != prev_uuid) {
total_tsids_length += mysql::gtid::Uuid::TEXT_LENGTH;
prev_uuid = tsid.get_uuid();
first_sidno = false;
++n_sids;
}
do {
total_interval_length += ::get_string_length(iv->start);
++n_intervals;
if (iv->end - 1 > iv->start) {
++n_long_intervals;
total_interval_length += ::get_string_length(iv->end - 1);
}
ivit.next();
iv = ivit.get();
} while (iv != nullptr);
}
}
}
if (n_sids == 0 && sf->empty_set_string != nullptr)
cached_string_length = sf->empty_set_string_length;
else {
cached_string_length = sf->begin_length + sf->end_length;
if (n_sids > 0)
cached_string_length +=
total_tsids_length + ((n_sids - 1) * sf->gno_sid_separator_length) +
total_interval_length + n_sidnos * sf->tsid_gno_separator_length +
(n_intervals - n_sidnos) * sf->gno_gno_separator_length +
n_long_intervals * sf->gno_start_end_separator_length;
}
has_cached_string_length = true;
cached_string_format = sf;
}
return cached_string_length;
}
bool Gtid_set::sidno_equals(rpl_sidno sidno, const Gtid_set *other,