-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathrpl_filter.cc
1583 lines (1366 loc) · 49.6 KB
/
rpl_filter.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, 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 "sql/rpl_filter.h"
#include "my_config.h"
#include <string.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <algorithm>
#include <map>
#include <utility>
#include "m_string.h"
#include "mf_wcomp.h" // wild_one, wild_many
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_psi_config.h"
#include "my_sys.h"
#include "mysql/psi/mysql_mutex.h"
#include "mysql/service_mysql_alloc.h"
#include "mysql/strings/m_ctype.h"
#include "mysql_com.h"
#include "mysqld_error.h"
#include "sql/auth/auth_acls.h"
#include "sql/auth/sql_security_ctx.h"
#include "sql/changestreams/apply/replication_thread_status.h"
#include "sql/current_thd.h"
#include "sql/item.h" // Item
#include "sql/mysqld.h" // table_alias_charset
#include "sql/psi_memory_key.h"
#include "sql/rpl_mi.h" // Master_info
#include "sql/rpl_msr.h" // channel_map
#include "sql/rpl_replica.h" // REPLICA_SQL
#include "sql/rpl_rli.h" // Relay_log_info
#include "sql/sql_class.h"
#include "sql/sql_lex.h"
#include "sql/table.h" // Table_ref
#include "sql/thr_malloc.h"
#include "sql_string.h"
#include "string_with_len.h"
#include "template_utils.h" // my_free_container_pointers
extern PSI_memory_key key_memory_array_buffer;
Rpl_pfs_filter::Rpl_pfs_filter()
: m_channel_name(nullptr),
m_filter_name(nullptr),
m_rpl_filter_statistics(nullptr) {}
Rpl_pfs_filter::Rpl_pfs_filter(const char *channel_name,
const char *filter_name,
const String &filter_rule,
Rpl_filter_statistics *rpl_filter_statistics) {
m_channel_name = channel_name;
m_filter_name = filter_name;
if (!filter_rule.is_empty()) m_filter_rule.copy(filter_rule);
m_rpl_filter_statistics = rpl_filter_statistics;
}
Rpl_pfs_filter::Rpl_pfs_filter(const Rpl_pfs_filter &other) {
m_channel_name = other.m_channel_name;
m_filter_name = other.m_filter_name;
m_rpl_filter_statistics = other.m_rpl_filter_statistics;
/* Deep copy */
if (!other.m_filter_rule.is_empty()) m_filter_rule.copy(other.m_filter_rule);
}
Rpl_pfs_filter::~Rpl_pfs_filter() = default;
Rpl_filter_statistics::Rpl_filter_statistics() { reset(); }
Rpl_filter_statistics::~Rpl_filter_statistics() = default;
void Rpl_filter_statistics::reset() {
m_configured_by = CONFIGURED_BY_STARTUP_OPTIONS;
m_atomic_counter = 0;
m_active_since = 0;
}
void Rpl_filter_statistics::set_all(enum_configured_by configured_by) {
m_configured_by = configured_by;
m_atomic_counter = 0;
/* Set m_active_since to current time. */
THD *thd = current_thd;
if (thd == nullptr)
m_active_since = my_micro_time();
else {
/*
Calculate time stamp up to tenths of milliseconds elapsed
from 1 Jan 1970 00:00:00.
*/
my_timeval stmt_start_time = thd->query_start_timeval_trunc(6);
m_active_since =
stmt_start_time.m_tv_sec * 1000000 + stmt_start_time.m_tv_usec;
}
}
Rpl_filter::Rpl_filter()
: table_rules_on(false),
attached(false),
do_table_array(key_memory_TABLE_RULE_ENT),
ignore_table_array(key_memory_TABLE_RULE_ENT),
wild_do_table(key_memory_TABLE_RULE_ENT),
wild_ignore_table(key_memory_TABLE_RULE_ENT),
do_table_hash_inited(false),
ignore_table_hash_inited(false),
do_table_array_inited(false),
ignore_table_array_inited(false),
wild_do_table_inited(false),
wild_ignore_table_inited(false) {
do_db.clear();
ignore_db.clear();
rewrite_db.clear();
m_rpl_filter_lock = new Checkable_rwlock(
#ifdef HAVE_PSI_INTERFACE
key_rwlock_rpl_filter_lock
#endif
);
}
Rpl_filter::~Rpl_filter() {
reset();
delete m_rpl_filter_lock;
}
void Rpl_filter::reset() {
if (do_table_hash_inited) delete do_table_hash;
if (ignore_table_hash_inited) delete ignore_table_hash;
do_table_hash_inited = false;
ignore_table_hash_inited = false;
do_table_array_inited = false;
ignore_table_array_inited = false;
wild_do_table_inited = false;
wild_ignore_table_inited = false;
table_rules_on = false;
free_string_array(&do_table_array);
free_string_array(&ignore_table_array);
free_string_array(&wild_do_table);
free_string_array(&wild_ignore_table);
free_string_list(&do_db);
free_string_list(&ignore_db);
free_string_pair_list(&rewrite_db);
do_table_statistics.reset();
ignore_table_statistics.reset();
wild_do_table_statistics.reset();
wild_ignore_table_statistics.reset();
do_db_statistics.reset();
ignore_db_statistics.reset();
rewrite_db_statistics.reset();
}
bool Rpl_filter::is_empty() {
rdlock();
bool res = do_table_hash_inited == 0 && ignore_table_hash_inited == 0 &&
do_table_array_inited == 0 && ignore_table_array_inited == 0 &&
wild_do_table_inited == 0 && wild_ignore_table_inited == 0 &&
do_db.is_empty() && ignore_db.is_empty() && rewrite_db.is_empty();
unlock();
return res;
}
int Rpl_filter::copy_global_replication_filters() {
DBUG_TRACE;
int res = 0;
bool need_unlock = false;
/* Assert that it is not self copy. */
assert(this != &rpl_global_filter);
/* Check if the source is empty. */
if (rpl_global_filter.is_empty()) return 0;
THD *thd = current_thd;
if (thd != nullptr &&
thd->lex->sql_command == SQLCOM_CHANGE_REPLICATION_SOURCE) {
/*
Acquire the write lock when copying global replication filter if
a new channel is being created by CHANGE REPLICATION SOURCE TO ... FOR
CHANNEL command after server startup, in case SHOW REPLICA STATUS or
SELECT * FROM performance_schema.replication_applier_filters is
querying the filter in parallel. We do not have the race problem
when creating a new channel from repository during server startup.
Note: we hold a write lock of channel_map when executing
CHANGE REPLICATION SOURCE TO ... FOR CHANNEL <channel_name>, and hold a
read lock of channel_map when executing CHANGE REPLICATION FILTER (the
global replication filters). So we do not need to lock the global
replication filters for read.
*/
wrlock();
need_unlock = true;
}
if (!do_table_hash_inited && rpl_global_filter.do_table_hash_inited) {
/*
Build this->do_table_array from rpl_global_filter.do_table_hash since
rpl_global_filter.do_table_array is freed after building do table hash.
*/
res = table_rule_ent_hash_to_array(&do_table_array,
rpl_global_filter.do_table_hash,
rpl_global_filter.do_table_hash_inited);
if (res != 0) goto err;
do_table_array_inited = true;
table_rules_on = true;
res = build_do_table_hash();
if (res != 0) goto err;
if (do_table_hash_inited && do_table_hash->empty()) {
delete do_table_hash;
do_table_hash = nullptr;
do_table_hash_inited = false;
}
do_table_statistics.set_all(
rpl_global_filter.do_table_statistics.get_configured_by());
}
if (!ignore_table_hash_inited && rpl_global_filter.ignore_table_hash_inited) {
/*
Build this->ignore_table_array from rpl_global_filter.ignore_table_hash
since rpl_global_filter.ignore_table_array is freed after building
ignore table hash.
*/
res = table_rule_ent_hash_to_array(
&ignore_table_array, rpl_global_filter.ignore_table_hash,
rpl_global_filter.ignore_table_hash_inited);
if (res != 0) goto err;
ignore_table_array_inited = true;
table_rules_on = true;
res = build_ignore_table_hash();
DBUG_EXECUTE_IF("simulate_out_of_memory_on_copy_ignore_table", res = 1;);
if (res != 0) goto err;
if (ignore_table_hash_inited && ignore_table_hash->empty()) {
delete ignore_table_hash;
ignore_table_hash = nullptr;
ignore_table_hash_inited = false;
}
ignore_table_statistics.set_all(
rpl_global_filter.ignore_table_statistics.get_configured_by());
}
if (!wild_do_table_inited && rpl_global_filter.wild_do_table_inited) {
res = table_rule_ent_array_to_array(&wild_do_table,
&rpl_global_filter.wild_do_table,
rpl_global_filter.wild_do_table_inited);
if (res != 0) goto err;
assert(!wild_do_table.empty());
wild_do_table_inited = true;
table_rules_on = true;
wild_do_table_statistics.set_all(
rpl_global_filter.wild_do_table_statistics.get_configured_by());
}
if (!wild_ignore_table_inited && rpl_global_filter.wild_ignore_table_inited) {
res = table_rule_ent_array_to_array(
&wild_ignore_table, &rpl_global_filter.wild_ignore_table,
rpl_global_filter.wild_ignore_table_inited);
DBUG_EXECUTE_IF("simulate_out_of_memory_on_copy_wild_ignore_table",
res = 1;);
if (res != 0) goto err;
assert(!wild_ignore_table.empty());
wild_ignore_table_inited = true;
table_rules_on = true;
wild_ignore_table_statistics.set_all(
rpl_global_filter.wild_ignore_table_statistics.get_configured_by());
}
if (do_db.is_empty() && !rpl_global_filter.do_db.is_empty()) {
/* Copy content from rpl_global_filter.do_db to this->do_db */
res = parse_filter_list(&rpl_global_filter.do_db, &Rpl_filter::add_do_db);
if (res != 0) goto err;
do_db_statistics.set_all(
rpl_global_filter.do_db_statistics.get_configured_by());
}
if (ignore_db.is_empty() && !rpl_global_filter.ignore_db.is_empty()) {
/* Copy content from rpl_global_filter.ignore_db to this->ignore_db */
res = parse_filter_list(&rpl_global_filter.ignore_db,
&Rpl_filter::add_ignore_db);
DBUG_EXECUTE_IF("simulate_out_of_memory_on_copy_ignore_db", res = 1;);
if (res != 0) goto err;
ignore_db_statistics.set_all(
rpl_global_filter.ignore_db_statistics.get_configured_by());
}
if (rewrite_db.is_empty() && !rpl_global_filter.rewrite_db.is_empty()) {
/* Copy content from rpl_global_filter.rewrite_db to this->rewrite_db */
I_List_iterator<i_string_pair> it(rpl_global_filter.rewrite_db);
i_string_pair *str_pair;
while ((str_pair = it++)) {
res = add_db_rewrite(str_pair->key, str_pair->val);
DBUG_EXECUTE_IF("simulate_out_of_memory_on_copy_rewrite_db", res = 1;);
if (res) break;
}
if (res != 0) goto err;
rewrite_db_statistics.set_all(
rpl_global_filter.rewrite_db_statistics.get_configured_by());
}
if (need_unlock) unlock();
#ifdef WITH_PERFSCHEMA_STORAGE_ENGINE
rpl_channel_filters.wrlock();
rpl_channel_filters.reset_pfs_view();
rpl_channel_filters.unlock();
#endif /* WITH_PERFSCHEMA_STORAGE_ENGINE */
return 0;
err:
if (need_unlock) unlock();
my_error(ER_OUTOFMEMORY, MYF(ME_FATALERROR), 0);
return 1;
}
/*
Returns true if table should be logged/replicated
SYNOPSIS
tables_ok()
db db to use if db in Table_ref is undefined for a table
tables list of tables to check
NOTES
Changing table order in the list can lead to different results.
Note also order of precedence of do/ignore rules (see code). For
that reason, users should not set conflicting rules because they
may get unpredicted results (precedence order is explained in the
manual).
If no table in the list is marked "updating", then we always
return 0, because there is no reason to execute this statement on
slave if it updates nothing. (Currently, this can only happen if
statement is a multi-delete (SQLCOM_DELETE_MULTI) and "tables" are
the tables in the FROM):
In the case of SQLCOM_DELETE_MULTI, there will be a second call to
tables_ok(), with tables having "updating==TRUE" (those after the
DELETE), so this second call will make the decision (because
all_tables_not_ok() = !tables_ok(1st_list) &&
!tables_ok(2nd_list)).
TODO
"Include all tables like "abc.%" except "%.EFG"". (Can't be done now.)
If we supported Perl regexps, we could do it with pattern: /^abc\.(?!EFG)/
(I could not find an equivalent in the regex library MySQL uses).
RETURN VALUES
0 should not be logged/replicated
1 should be logged/replicated
*/
bool Rpl_filter::tables_ok(const char *db, Table_ref *tables) {
bool some_tables_updating = false;
DBUG_TRACE;
for (; tables; tables = tables->next_global) {
char hash_key[2 * NAME_LEN + 2];
char *end;
uint len;
if (!tables->updating) continue;
some_tables_updating = true;
end = my_stpcpy(hash_key, tables->db ? tables->db : db);
*end++ = '.';
len = (uint)(my_stpcpy(end, tables->table_name) - hash_key);
if (do_table_hash_inited) // if there are any do's
{
if (do_table_hash->count(std::string(hash_key, len)) != 0) {
do_table_statistics.increase_counter();
return true;
}
}
if (ignore_table_hash_inited) // if there are any ignores
{
if (ignore_table_hash->count(std::string(hash_key, len)) != 0) {
ignore_table_statistics.increase_counter();
return false;
}
}
if (wild_do_table_inited && find_wild(&wild_do_table, hash_key, len)) {
wild_do_table_statistics.increase_counter();
return true;
}
if (wild_ignore_table_inited &&
find_wild(&wild_ignore_table, hash_key, len)) {
wild_ignore_table_statistics.increase_counter();
return false;
}
}
/*
If no table was to be updated, ignore statement (no reason we play it on
slave, slave is supposed to replicate _changes_ only).
If no explicit rule found and there was a do list, do not replicate.
If there was no do list, go ahead
*/
return some_tables_updating && !do_table_hash_inited && !wild_do_table_inited;
}
/*
Checks whether a db matches some do_db and ignore_db rules
SYNOPSIS
db_ok()
db name of the db to check
need_increase_counter true if need to increase do_db/ignore_db counter
RETURN VALUES
0 should not be logged/replicated
1 should be logged/replicated
*/
bool Rpl_filter::db_ok(const char *db, bool need_increase_counter) {
DBUG_TRACE;
if (do_db.is_empty() && ignore_db.is_empty())
return true; // Ok to replicate if the user puts no constraints
/*
Previous behaviour "if the user has specified restrictions on which
databases to replicate and db was not selected, do not replicate" has
been replaced with "do replicate".
Since the filtering criteria is not equal to "NULL" the statement should
be logged into binlog.
*/
if (!db) return true;
if (!do_db.is_empty()) // if the do's are not empty
{
I_List_iterator<i_string> it(do_db);
i_string *tmp;
while ((tmp = it++)) {
/*
Filters will follow the setting of lower_case_table_name
to be case sensitive when setting lower_case_table_name=0.
Otherwise they will be case insensitive but accent sensitive.
*/
if (!my_strcasecmp(table_alias_charset, tmp->ptr, db)) {
if (need_increase_counter) do_db_statistics.increase_counter();
return true; // match
}
}
return false;
} else // there are some elements in the don't, otherwise we cannot get here
{
I_List_iterator<i_string> it(ignore_db);
i_string *tmp;
while ((tmp = it++)) {
/*
Filters will follow the setting of lower_case_table_name
to be case sensitive when setting lower_case_table_name=0.
Otherwise they will be case insensitive but accent sensitive.
*/
if (!my_strcasecmp(table_alias_charset, tmp->ptr, db)) {
if (need_increase_counter) ignore_db_statistics.increase_counter();
return false; // match
}
}
return true;
}
}
/*
Checks whether a db matches wild_do_table and wild_ignore_table
rules (for replication)
SYNOPSIS
db_ok_with_wild_table()
db name of the db to check.
Is tested with check_db_name() before calling this function.
NOTES
Here is the reason for this function.
We advise users who want to exclude a database 'db1' safely to do it
with replicate_wild_ignore_table='db1.%' instead of binlog_ignore_db or
replicate_ignore_db because the two lasts only check for the selected db,
which won't work in that case:
USE db2;
UPDATE db1.t SET ... #this will be replicated and should not
whereas replicate_wild_ignore_table will work in all cases.
With replicate_wild_ignore_table, we only check tables. When
one does 'DROP DATABASE db1', tables are not involved and the
statement will be replicated, while users could expect it would not (as it
roughly means 'DROP db1.first_table, DROP db1.second_table...').
In other words, we want to interpret 'db1.%' as "everything touching db1".
That is why we want to match 'db1' against 'db1.%' wild table rules.
RETURN VALUES
0 should not be logged/replicated
1 should be logged/replicated
*/
bool Rpl_filter::db_ok_with_wild_table(const char *db) {
DBUG_TRACE;
char hash_key[NAME_LEN + 2];
char *end;
size_t len;
end = my_stpcpy(hash_key, db);
*end++ = '.';
len = end - hash_key;
if (wild_do_table_inited && find_wild(&wild_do_table, hash_key, len)) {
wild_do_table_statistics.increase_counter();
DBUG_PRINT("return", ("1"));
return true;
}
if (wild_ignore_table_inited &&
find_wild(&wild_ignore_table, hash_key, len)) {
wild_ignore_table_statistics.increase_counter();
DBUG_PRINT("return", ("0"));
return false;
}
/*
If no explicit rule found and there was a do list, do not replicate.
If there was no do list, go ahead
*/
DBUG_PRINT("return", ("db=%s,retval=%d", db, !wild_do_table_inited));
return !wild_do_table_inited;
}
bool Rpl_filter::is_on() { return table_rules_on; }
bool Rpl_filter::is_rewrite_empty() { return rewrite_db.is_empty(); }
int Rpl_filter::add_do_table_array(const char *table_spec) {
DBUG_TRACE;
if (!do_table_array_inited)
init_table_rule_array(&do_table_array, &do_table_array_inited);
table_rules_on = true;
return add_table_rule_to_array(&do_table_array, table_spec);
}
int Rpl_filter::add_ignore_table_array(const char *table_spec) {
DBUG_TRACE;
if (!ignore_table_array_inited)
init_table_rule_array(&ignore_table_array, &ignore_table_array_inited);
table_rules_on = true;
return add_table_rule_to_array(&ignore_table_array, table_spec);
}
int Rpl_filter::add_wild_do_table(const char *table_spec) {
DBUG_TRACE;
if (!wild_do_table_inited)
init_table_rule_array(&wild_do_table, &wild_do_table_inited);
table_rules_on = true;
return add_table_rule_to_array(&wild_do_table, table_spec);
}
int Rpl_filter::add_wild_ignore_table(const char *table_spec) {
DBUG_TRACE;
if (!wild_ignore_table_inited)
init_table_rule_array(&wild_ignore_table, &wild_ignore_table_inited);
table_rules_on = true;
int ret = add_table_rule_to_array(&wild_ignore_table, table_spec);
return ret;
}
int Rpl_filter::add_db_rewrite(const char *from_db, const char *to_db) {
DBUG_TRACE;
int ret = add_string_pair_list(&rewrite_db, from_db, to_db);
return ret;
}
/*
Build do_table rules to hash from dynamic array
for faster filter checking.
@return
0 ok
1 error
*/
int Rpl_filter::build_do_table_hash() {
DBUG_TRACE;
if (build_table_hash_from_array(&do_table_array, &do_table_hash,
do_table_array_inited, &do_table_hash_inited))
return 1;
/* Free do table ARRAY as it is a copy in do table hash */
if (do_table_array_inited) {
free_string_array(&do_table_array);
do_table_array_inited = false;
}
return 0;
}
/*
Build ignore_table rules to hash from dynamic array
for faster filter checking.
@return
0 ok
1 error
*/
int Rpl_filter::build_ignore_table_hash() {
DBUG_TRACE;
if (build_table_hash_from_array(&ignore_table_array, &ignore_table_hash,
ignore_table_array_inited,
&ignore_table_hash_inited))
return 1;
/* Free ignore table ARRAY as it is a copy in ignore table hash */
if (ignore_table_array_inited) {
free_string_array(&ignore_table_array);
ignore_table_array_inited = false;
}
return 0;
}
/**
Table rules are initially added to DYNAMIC_LIST, and then,
when the charset to use for tables has been established,
inserted into a hash for faster filter checking.
@param[in] table_array dynamic array stored table rules
@param[in] table_hash hash for storing table rules
@param[in] array_inited Table rules are added to dynamic array
@param[in] hash_inited Table rules are added to hash
@return
0 ok
1 error
*/
int Rpl_filter::build_table_hash_from_array(Table_rule_array *table_array,
Table_rule_hash **table_hash,
bool array_inited,
bool *hash_inited) {
DBUG_TRACE;
if (array_inited) {
init_table_rule_hash(table_hash, hash_inited);
for (size_t i = 0; i < table_array->size(); i++) {
TABLE_RULE_ENT *e = table_array->at(i);
if (add_table_rule_to_hash(*table_hash, e->db, e->key_len)) return 1;
}
}
return 0;
}
/**
Added one table rule to hash.
@param[in] h hash for storing table rules
@param[in] table_spec Table name with db
@param[in] len The length of table_spec
@return
0 ok
1 error
*/
int Rpl_filter::add_table_rule_to_hash(Table_rule_hash *h,
const char *table_spec, uint len) {
const char *dot = strchr(table_spec, '.');
if (!dot) return 1;
// len is always > 0 because we know the there exists a '.'
TABLE_RULE_ENT *e = (TABLE_RULE_ENT *)my_malloc(
key_memory_TABLE_RULE_ENT, sizeof(TABLE_RULE_ENT) + len, MYF(MY_WME));
if (!e) return 1;
e->db = (char *)e + sizeof(TABLE_RULE_ENT);
e->tbl_name = e->db + (dot - table_spec) + 1;
e->key_len = len;
memcpy(e->db, table_spec, len);
h->emplace(std::string(e->db, e->key_len),
unique_ptr_my_free<TABLE_RULE_ENT>(e));
return 0;
}
/*
Add table expression to dynamic array
*/
int Rpl_filter::add_table_rule_to_array(Table_rule_array *a,
const char *table_spec) {
const char *dot = strchr(table_spec, '.');
if (!dot) return 1;
size_t len = strlen(table_spec);
TABLE_RULE_ENT *e = (TABLE_RULE_ENT *)my_malloc(
key_memory_TABLE_RULE_ENT, sizeof(TABLE_RULE_ENT) + len, MYF(MY_WME));
if (!e) return 1;
e->db = (char *)e + sizeof(TABLE_RULE_ENT);
e->tbl_name = e->db + (dot - table_spec) + 1;
e->key_len = len;
memcpy(e->db, table_spec, len);
if (a->push_back(e)) {
my_free(e);
return 1;
}
return 0;
}
int Rpl_filter::parse_filter_list(mem_root_deque<Item *> *item_list,
Add_filter add) {
DBUG_TRACE;
int status = 0;
for (Item *item : *item_list) {
String buf;
status = (this->*add)(item->val_str(&buf)->c_ptr());
if (status) break;
}
return status;
}
int Rpl_filter::parse_filter_list(I_List<i_string> *list, Add_filter add) {
DBUG_TRACE;
int status = 0;
if (list->is_empty()) /* to support '()' syntax */
return status;
I_List_iterator<i_string> it(*list);
i_string *istr;
while ((istr = it++)) {
status = (this->*add)(istr->ptr);
DBUG_EXECUTE_IF("simulate_out_of_memory_on_copy_do_db", status = 1;);
if (status) break;
}
return status;
}
int Rpl_filter::set_do_db(mem_root_deque<Item *> *do_db_list,
enum_configured_by configured_by) {
DBUG_TRACE;
m_rpl_filter_lock->assert_some_wrlock();
if (!do_db_list) return 0;
free_string_list(&do_db);
int ret = parse_filter_list(do_db_list, &Rpl_filter::add_do_db);
do_db_statistics.set_all(configured_by);
return ret;
}
int Rpl_filter::set_ignore_db(mem_root_deque<Item *> *ignore_db_list,
enum_configured_by configured_by) {
DBUG_TRACE;
m_rpl_filter_lock->assert_some_wrlock();
if (!ignore_db_list) return 0;
free_string_list(&ignore_db);
int ret = parse_filter_list(ignore_db_list, &Rpl_filter::add_ignore_db);
ignore_db_statistics.set_all(configured_by);
return ret;
}
int Rpl_filter::set_do_table(mem_root_deque<Item *> *do_table_list,
enum_configured_by configured_by) {
DBUG_TRACE;
m_rpl_filter_lock->assert_some_wrlock();
if (!do_table_list) return 0;
int status;
if (do_table_hash_inited) {
delete do_table_hash;
do_table_hash = nullptr;
do_table_hash_inited = false;
}
if (do_table_array_inited)
free_string_array(&do_table_array); /* purecov: inspected */
status = parse_filter_list(do_table_list, &Rpl_filter::add_do_table_array);
if (!status) {
status = build_do_table_hash();
if (do_table_hash_inited && do_table_hash->empty()) {
delete do_table_hash;
do_table_hash = nullptr;
do_table_hash_inited = false;
}
}
do_table_statistics.set_all(configured_by);
return status;
}
int Rpl_filter::set_ignore_table(mem_root_deque<Item *> *ignore_table_list,
enum_configured_by configured_by) {
DBUG_TRACE;
m_rpl_filter_lock->assert_some_wrlock();
if (!ignore_table_list) return 0;
int status;
if (ignore_table_hash_inited) {
delete ignore_table_hash;
ignore_table_hash = nullptr;
ignore_table_hash_inited = false;
}
if (ignore_table_array_inited)
free_string_array(&ignore_table_array); /* purecov: inspected */
status =
parse_filter_list(ignore_table_list, &Rpl_filter::add_ignore_table_array);
if (!status) {
status = build_ignore_table_hash();
if (ignore_table_hash_inited && ignore_table_hash->empty()) {
delete ignore_table_hash;
ignore_table_hash = nullptr;
ignore_table_hash_inited = false;
}
}
ignore_table_statistics.set_all(configured_by);
return status;
}
int Rpl_filter::set_wild_do_table(mem_root_deque<Item *> *wild_do_table_list,
enum_configured_by configured_by) {
DBUG_TRACE;
m_rpl_filter_lock->assert_some_wrlock();
if (!wild_do_table_list) return 0;
int status;
if (wild_do_table_inited) free_string_array(&wild_do_table);
status =
parse_filter_list(wild_do_table_list, &Rpl_filter::add_wild_do_table);
if (wild_do_table.empty()) {
wild_do_table.shrink_to_fit();
wild_do_table_inited = false;
}
wild_do_table_statistics.set_all(configured_by);
return status;
}
int Rpl_filter::set_wild_ignore_table(
mem_root_deque<Item *> *wild_ignore_table_list,
enum_configured_by configured_by) {
DBUG_TRACE;
m_rpl_filter_lock->assert_some_wrlock();
if (!wild_ignore_table_list) return 0;
int status;
if (wild_ignore_table_inited) free_string_array(&wild_ignore_table);
status = parse_filter_list(wild_ignore_table_list,
&Rpl_filter::add_wild_ignore_table);
if (wild_ignore_table.empty()) {
wild_ignore_table.shrink_to_fit();
wild_ignore_table_inited = false;
}
wild_ignore_table_statistics.set_all(configured_by);
return status;
}
int Rpl_filter::set_db_rewrite(mem_root_deque<Item *> *rewrite_db_pair_list,
enum_configured_by configured_by) {
DBUG_TRACE;
m_rpl_filter_lock->assert_some_wrlock();
if (!rewrite_db_pair_list) return 0;
int status = 0;
free_string_pair_list(&rewrite_db);
auto it = rewrite_db_pair_list->begin();
/* Please note that grammar itself allows only even number of db values. So
* it is ok to do it++ twice without checking anything. */
while (status == 0 && it != rewrite_db_pair_list->end()) {
Item *db_key = *it++;
Item *db_val = *it++;
String buf1, buf2;
status = add_db_rewrite(db_key->val_str(&buf1)->c_ptr(),
db_val->val_str(&buf2)->c_ptr());
}
rewrite_db_statistics.set_all(configured_by);
return status;
}
int Rpl_filter::add_string_list(I_List<i_string> *list, const char *spec) {
char *str;
i_string *node;
if (!(str = my_strdup(key_memory_rpl_filter, spec, MYF(MY_WME))))
return true; /* purecov: inspected */
if (!(node = new i_string(str))) {
/* purecov: begin inspected */
my_free(str);
return true;
/* purecov: end */
}
list->push_back(node);
return false;
}
int Rpl_filter::add_string_pair_list(I_List<i_string_pair> *list,
const char *key, const char *val) {
char *dup_key, *dup_val;
i_string_pair *node;
if (!(dup_key = my_strdup(key_memory_rpl_filter, key, MYF(MY_WME))))
return true; /* purecov: inspected */
if (!(dup_val = my_strdup(key_memory_rpl_filter, val, MYF(MY_WME)))) {
/* purecov: begin inspected */
my_free(dup_key);
return true;
/* purecov: end */
}
if (!(node = new i_string_pair(dup_key, dup_val))) {
/* purecov: begin inspected */
my_free(dup_key);
my_free(dup_val);
return true;
/* purecov: end */
}
list->push_back(node);
return false;
}
int Rpl_filter::add_do_db(const char *table_spec) {
DBUG_TRACE;
int ret = add_string_list(&do_db, table_spec);
return ret;
}
int Rpl_filter::add_ignore_db(const char *table_spec) {
DBUG_TRACE;
int ret = add_string_list(&ignore_db, table_spec);
return ret;
}
void Rpl_filter::init_table_rule_hash(Table_rule_hash **h, bool *h_inited) {
*h = new Table_rule_hash(table_alias_charset, key_memory_TABLE_RULE_ENT);
*h_inited = true;
}
void Rpl_filter::init_table_rule_array(Table_rule_array *a, bool *a_inited) {
a->clear();
*a_inited = true;
}
TABLE_RULE_ENT *Rpl_filter::find_wild(Table_rule_array *a, const char *key,
size_t len) {
const char *key_end = key + len;
for (size_t i = 0; i < a->size(); i++) {
TABLE_RULE_ENT *e = a->at(i);
/*
Filters will follow the setting of lower_case_table_name
to be case sensitive when setting lower_case_table_name=0.
Otherwise they will be case insensitive but accent sensitive.
*/
if (!my_wildcmp(table_alias_charset, key, key_end, (const char *)e->db,
(const char *)(e->db + e->key_len), '\\', wild_one,
wild_many))
return e;
}
return nullptr;
}
void Rpl_filter::free_string_array(Table_rule_array *a) {
my_free_container_pointers(*a);
a->shrink_to_fit();
}