Skip to content

Commit c985ed4

Browse files
shlomi-noachpiki
authored andcommitted
Support new Vitess internal table name format (mysql#105) (mysql#129)
* Support new Vitess internal table name format * Update sql/table.cc * Update sql/table.cc * Update sql/table.cc * optimize regexp search * match start of name --------- Signed-off-by: Shlomi Noach <[email protected]> Co-authored-by: Patrick Reynolds <[email protected]>
1 parent e6e75d2 commit c985ed4

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

mysql-test/r/foreign_key_ignored_tables.result

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
drop table if exists t0,t1,t2,t3,t4;
22
drop table if exists t0,t5,t6,t7,t8,t9,t1_1,t1_2,t9_1,t9_2;
33
drop table if exists _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl, _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410, child_table, child_table_old, child_table_new;
4+
drop table if exists _vt_vrp_6ace8bcef73211ea87e9f875a4d24e90_20200915120410_, _vt_hld_6ace8bcef73211ea87e9f875a4d24e90_20200915120410_;
45
drop table if exists parent_table, parent_table_old, parent_table_new;
56
SET FOREIGN_KEY_CHECKS=1;
67
CREATE TABLE parent_table (
@@ -35,10 +36,28 @@ KEY parent_id_idx (parent_id),
3536
CONSTRAINT `hold_child_parent_fk` FOREIGN KEY (parent_id) REFERENCES parent_table(id) ON DELETE NO ACTION
3637
);
3738
insert into _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410 select * from child_table;
39+
CREATE TABLE _vt_hld_6ace8bcef73211ea87e9f875a4d24e90_20200915120410_ (
40+
id INT NOT NULL,
41+
parent_id INT,
42+
PRIMARY KEY (id),
43+
KEY parent_id_idx (parent_id),
44+
CONSTRAINT `hld_child_parent_fk` FOREIGN KEY (parent_id) REFERENCES parent_table(id) ON DELETE NO ACTION
45+
);
46+
insert into _vt_hld_6ace8bcef73211ea87e9f875a4d24e90_20200915120410_ select * from child_table;
47+
CREATE TABLE _vt_vrp_6ace8bcef73211ea87e9f875a4d24e90_20200915120410_ (
48+
id INT NOT NULL,
49+
parent_id INT,
50+
PRIMARY KEY (id),
51+
KEY parent_id_idx (parent_id),
52+
CONSTRAINT `vrp_child_parent_fk` FOREIGN KEY (parent_id) REFERENCES parent_table(id) ON DELETE NO ACTION
53+
);
54+
insert into _vt_vrp_6ace8bcef73211ea87e9f875a4d24e90_20200915120410_ select * from child_table;
3855
insert into child_table values (100, 100);
3956
ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test`.`child_table`, CONSTRAINT `child_parent_fk` FOREIGN KEY (`parent_id`) REFERENCES `parent_table` (`id`))
4057
insert into _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl values (101, 101);
4158
insert into _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410 values (102, 102);
59+
insert into _vt_hld_6ace8bcef73211ea87e9f875a4d24e90_20200915120410_ values (103, 103);
60+
insert into _vt_vrp_6ace8bcef73211ea87e9f875a4d24e90_20200915120410_ values (103, 103);
4261
delete from parent_table where id=1;
4362
ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`child_table`, CONSTRAINT `child_parent_fk` FOREIGN KEY (`parent_id`) REFERENCES `parent_table` (`id`))
4463
delete from child_table;
@@ -199,5 +218,6 @@ id parent_id
199218
13 2
200219
400 400
201220
drop table _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl;
221+
drop table if exists _vt_vrp_6ace8bcef73211ea87e9f875a4d24e90_20200915120410_, _vt_hld_6ace8bcef73211ea87e9f875a4d24e90_20200915120410_;
202222
drop table if exists _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl, _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410, child_table, child_table_old, child_table_new;
203223
drop table if exists parent_table, parent_table_old, parent_table_new;

mysql-test/t/foreign_key_ignored_tables.test

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ drop table if exists t0,t1,t2,t3,t4;
1010
# Clear up from other tests (to ensure that SHOW TABLES below is right)
1111
drop table if exists t0,t5,t6,t7,t8,t9,t1_1,t1_2,t9_1,t9_2;
1212
drop table if exists _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl, _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410, child_table, child_table_old, child_table_new;
13+
drop table if exists _vt_vrp_6ace8bcef73211ea87e9f875a4d24e90_20200915120410_, _vt_hld_6ace8bcef73211ea87e9f875a4d24e90_20200915120410_;
1314
drop table if exists parent_table, parent_table_old, parent_table_new;
1415
--enable_warnings
1516

@@ -50,11 +51,31 @@ CREATE TABLE _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410 (
5051
);
5152
insert into _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410 select * from child_table;
5253

53-
# For normal tables this should be invalid, but for this table it will work:
54+
CREATE TABLE _vt_hld_6ace8bcef73211ea87e9f875a4d24e90_20200915120410_ (
55+
id INT NOT NULL,
56+
parent_id INT,
57+
PRIMARY KEY (id),
58+
KEY parent_id_idx (parent_id),
59+
CONSTRAINT `hld_child_parent_fk` FOREIGN KEY (parent_id) REFERENCES parent_table(id) ON DELETE NO ACTION
60+
);
61+
insert into _vt_hld_6ace8bcef73211ea87e9f875a4d24e90_20200915120410_ select * from child_table;
62+
63+
CREATE TABLE _vt_vrp_6ace8bcef73211ea87e9f875a4d24e90_20200915120410_ (
64+
id INT NOT NULL,
65+
parent_id INT,
66+
PRIMARY KEY (id),
67+
KEY parent_id_idx (parent_id),
68+
CONSTRAINT `vrp_child_parent_fk` FOREIGN KEY (parent_id) REFERENCES parent_table(id) ON DELETE NO ACTION
69+
);
70+
insert into _vt_vrp_6ace8bcef73211ea87e9f875a4d24e90_20200915120410_ select * from child_table;
71+
72+
# For normal tables this should be invalid, but for internal vitess tables it will work:
5473
--error ER_NO_REFERENCED_ROW_2
5574
insert into child_table values (100, 100);
5675
insert into _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl values (101, 101);
5776
insert into _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410 values (102, 102);
77+
insert into _vt_hld_6ace8bcef73211ea87e9f875a4d24e90_20200915120410_ values (103, 103);
78+
insert into _vt_vrp_6ace8bcef73211ea87e9f875a4d24e90_20200915120410_ values (103, 103);
5879

5980
# This should not work:
6081
--error ER_ROW_IS_REFERENCED_2
@@ -214,6 +235,7 @@ select * from child_table;
214235
drop table _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl;
215236
# Cleanup
216237
--disable_warnings
238+
drop table if exists _vt_vrp_6ace8bcef73211ea87e9f875a4d24e90_20200915120410_, _vt_hld_6ace8bcef73211ea87e9f875a4d24e90_20200915120410_;
217239
drop table if exists _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl, _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410, child_table, child_table_old, child_table_new;
218240
drop table if exists parent_table, parent_table_old, parent_table_new;
219241
--enable_warnings

sql/table.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,17 @@ const std::regex vitess_vrepl_table_name_regex = std::regex(".*_[0-f]{8}_[0-f]{4
175175
// - "test/_vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410"
176176
const std::regex vitess_gc_table_name_regex = std::regex(".*_vt_(HOLD|PURGE|EVAC|DROP)_[0-f]{32}_[0-9]{14}$");
177177

178+
// New vitess table names have a unified format, e.g.:
179+
// - _vt_vrp_6ace8bcef73211ea87e9f875a4d24e90_20200915120410_ for a vreplication table
180+
// - _vt_hld_6ace8bcef73211ea87e9f875a4d24e90_20200915120410_ for a lifecycle table
181+
// The new name format will eventually replace the old format. `vitess_vrepl_table_name_regex` and
182+
// `vitess_gc_table_name_regex` will eventually be deprecated and removed.
183+
const std::regex vitess_internal_table_name_regex = std::regex("^_vt_[a-zA-Z0-9]{3}_[0-f]{32}_[0-9]{14}_$");
184+
// Table name if fixed.
185+
const int vitess_internal_table_name_length = 56;
186+
/* Vitess lifecycle table name indicator */
187+
LEX_CSTRING VITESS_INTERNAL_TABLE_PREFIX = {STRING_WITH_LEN("_vt_")};
188+
178189
/* Functions defined in this file */
179190

180191
static Item *create_view_field(THD *thd, Table_ref *view, Item **field_ref,
@@ -8018,6 +8029,21 @@ bool Table_name_inspector::skip_fk_checks() {
80188029
return true;
80198030
}
80208031
}
8032+
// New vitess naming format, e.g. `_vt_drp_6ace8bcef73211ea87e9f875a4d24e90_20200915120410_`, can be identified via:
8033+
// - Expected fixed table length 56
8034+
// - Ends with "_"
8035+
// - Table name (ignore possible InnoDB schema encoding) starts with "_vt_"
8036+
// - Matches regexp
8037+
int table_name_length = strlen(this->table_name);
8038+
if (table_name_length >= vitess_internal_table_name_length) {
8039+
if (this->table_name[table_name_length - 1] == '_') {
8040+
if (strncmp(&this->table_name[table_name_length - vitess_internal_table_name_length], VITESS_INTERNAL_TABLE_PREFIX.str, VITESS_INTERNAL_TABLE_PREFIX.length) == 0) {
8041+
if (std::regex_match(&this->table_name[table_name_length - vitess_internal_table_name_length], vitess_internal_table_name_regex)) {
8042+
return true;
8043+
}
8044+
}
8045+
}
8046+
}
80218047
return false;
80228048
}
80238049

0 commit comments

Comments
 (0)