Skip to content

Commit

Permalink
MDEV-26137 Improve import tablespace workflow.
Browse files Browse the repository at this point in the history
Allow ALTER TABLE ... IMPORT TABLESPACE without creating the table
followed by discarding the tablespace.

That is, assuming we want to import table t1 to t2, instead of

CREATE TABLE t2 LIKE t1;
ALTER TABLE t2 DISCARD TABLESPACE;
FLUSH TABLES t1 FOR EXPORT;
--copy_file $MYSQLD_DATADIR/test/t1.cfg $MYSQLD_DATADIR/test/t2.cfg
--copy_file $MYSQLD_DATADIR/test/t1.ibd $MYSQLD_DATADIR/test/t2.ibd
UNLOCK TABLES;
ALTER TABLE t2 IMPORT TABLESPACE;

We can simply do

FLUSH TABLES t1 FOR EXPORT;
--copy_file $MYSQLD_DATADIR/test/t1.cfg $MYSQLD_DATADIR/test/t2.cfg
--copy_file $MYSQLD_DATADIR/test/t1.frm $MYSQLD_DATADIR/test/t2.frm
--copy_file $MYSQLD_DATADIR/test/t1.ibd $MYSQLD_DATADIR/test/t2.ibd
UNLOCK TABLES;
ALTER TABLE t2 IMPORT TABLESPACE;

We achieve this by creating a "stub" table in the second scenario
while opening the table, where t2 does not exist but needs to import
from t1. The "stub" table is similar to a table that is created but
then instructed to discard its tablespace.

We include tests with various row formats, encryption, with indexes
and auto-increment.
  • Loading branch information
mariadb-YuchenPei committed Jul 4, 2023
1 parent b7ee3c7 commit 9b431d7
Show file tree
Hide file tree
Showing 23 changed files with 852 additions and 57 deletions.
19 changes: 19 additions & 0 deletions mysql-test/suite/encryption/r/innodb_import.result
Expand Up @@ -20,3 +20,22 @@ SELECT * FROM t2;
f1 f2
1 InnoDB
DROP TABLE t1, t2;
#
# MDEV-26137 ALTER TABLE IMPORT enhancement
#
# with encryption and page_compressed
CREATE TABLE t1 (a int, b varchar(50)) ENGINE=InnoDB ENCRYPTED=YES ENCRYPTION_KEY_ID=6 PAGE_COMPRESSED=1;
INSERT INTO t1 VALUES(42, "hello");
FLUSH TABLES t1 FOR EXPORT;
UNLOCK TABLES;
ALTER TABLE t2 IMPORT TABLESPACE;
SHOW CREATE TABLE t2;
Table Create Table
t2 CREATE TABLE `t2` (
`a` int(11) DEFAULT NULL,
`b` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci `ENCRYPTED`=YES `ENCRYPTION_KEY_ID`=6 `PAGE_COMPRESSED`=1
SELECT * FROM t2;
a b
42 hello
DROP TABLE t1, t2;
19 changes: 19 additions & 0 deletions mysql-test/suite/encryption/t/innodb_import.test
Expand Up @@ -21,3 +21,22 @@ ALTER TABLE t2 DROP KEY idx;
ALTER TABLE t2 IMPORT TABLESPACE;
SELECT * FROM t2;
DROP TABLE t1, t2;

--echo #
--echo # MDEV-26137 ALTER TABLE IMPORT enhancement
--echo #

--echo # with encryption and page_compressed
CREATE TABLE t1 (a int, b varchar(50)) ENGINE=InnoDB ENCRYPTED=YES ENCRYPTION_KEY_ID=6 PAGE_COMPRESSED=1;
INSERT INTO t1 VALUES(42, "hello");
FLUSH TABLES t1 FOR EXPORT;
--copy_file $MYSQLD_DATADIR/test/t1.cfg $MYSQLD_DATADIR/test/t2.cfg
--copy_file $MYSQLD_DATADIR/test/t1.frm $MYSQLD_DATADIR/test/t2.frm
--copy_file $MYSQLD_DATADIR/test/t1.ibd $MYSQLD_DATADIR/test/t2.ibd
UNLOCK TABLES;
ALTER TABLE t2 IMPORT TABLESPACE;
SHOW CREATE TABLE t2;
SELECT * FROM t2;
DROP TABLE t1, t2;

# Embedded server uses absolute path, causing result mismatch in warning messages when .cfg is not copied over. Given we have tested importing without copying cfg in other tests, we don't do it here.
32 changes: 32 additions & 0 deletions mysql-test/suite/innodb/include/import_begin.inc
@@ -0,0 +1,32 @@
--source include/innodb_row_format.inc
--source include/innodb_row_format_2.inc
--source include/innodb_checksum_algorithm.inc

--echo #
--echo # MDEV-26137 ALTER TABLE IMPORT enhancement
--echo #

let $MYSQLD_DATADIR = `SELECT @@datadir`;

--disable_query_log
let $ROW_FORMAT_OPTION=;
if($MTR_COMBINATION_R_REDUNDANT) {
let $ROW_FORMAT_OPTION= ROW_FORMAT=REDUNDANT;
}
if($MTR_COMBINATION_R_COMPACT) {
let $ROW_FORMAT_OPTION= ROW_FORMAT=COMPACT;
}
if($MTR_COMBINATION_R_DYNAMIC) {
let $ROW_FORMAT_OPTION= ROW_FORMAT=DYNAMIC;
}
if($MTR_COMBINATION_R_COMPRESSED) {
let $ROW_FORMAT_OPTION= ROW_FORMAT=COMPRESSED;
}
let $CREATE_OPTIONS_REGEX= /ENGINE=InnoDB.*$ROW_FORMAT_OPTION/CREATE_OPTIONS/;
--enable_query_log

--replace_regex $CREATE_OPTIONS_REGEX
eval
CREATE TABLE t1(a INT PRIMARY KEY DEFAULT 42) ENGINE=InnoDB $ROW_FORMAT_OPTION;
INSERT INTO t1() VALUES();
FLUSH TABLES t1 FOR EXPORT;
8 changes: 8 additions & 0 deletions mysql-test/suite/innodb/include/import_end.inc
@@ -0,0 +1,8 @@
--copy_file $MYSQLD_DATADIR/test/t1.frm $MYSQLD_DATADIR/test/t2.frm
--copy_file $MYSQLD_DATADIR/test/t1.ibd $MYSQLD_DATADIR/test/t2.ibd
UNLOCK TABLES;
ALTER TABLE t2 IMPORT TABLESPACE;
--replace_regex $CREATE_OPTIONS_REGEX
SHOW CREATE TABLE t2;
SELECT * FROM t2;
DROP TABLE t1, t2;
@@ -0,0 +1,5 @@
[r_default]
[r_redundant]
[r_compact]
[r_dynamic]
[r_compressed]
4 changes: 4 additions & 0 deletions mysql-test/suite/innodb/include/innodb_row_format_2.inc
@@ -0,0 +1,4 @@
# The goal of including this file is to add various row_format options
# combinations (see include/innodb_row_format_2.combinations)

--source include/have_innodb.inc
18 changes: 18 additions & 0 deletions mysql-test/suite/innodb/r/import.result
@@ -0,0 +1,18 @@
#
# MDEV-26137 ALTER TABLE IMPORT enhancement
#
CREATE TABLE t1(a INT PRIMARY KEY DEFAULT 42) CREATE_OPTIONS;
INSERT INTO t1() VALUES();
FLUSH TABLES t1 FOR EXPORT;
UNLOCK TABLES;
ALTER TABLE t2 IMPORT TABLESPACE;
SHOW CREATE TABLE t2;
Table Create Table
t2 CREATE TABLE `t2` (
`a` int(11) NOT NULL DEFAULT 42,
PRIMARY KEY (`a`)
) CREATE_OPTIONS
SELECT * FROM t2;
a
42
DROP TABLE t1, t2;
21 changes: 21 additions & 0 deletions mysql-test/suite/innodb/r/import_no_cfg.result
@@ -0,0 +1,21 @@
#
# MDEV-26137 ALTER TABLE IMPORT enhancement
#
CREATE TABLE t1(a INT PRIMARY KEY DEFAULT 42) CREATE_OPTIONS;
INSERT INTO t1() VALUES();
FLUSH TABLES t1 FOR EXPORT;
UNLOCK TABLES;
ALTER TABLE t2 IMPORT TABLESPACE;
Warnings:
Warning 1810 IO Read error: (2, No such file or directory) Error opening './test/t2.cfg', will attempt to import without schema verification
Warning 1810 IO Read error: (2, No such file or directory) Error opening './test/t2.cfg', will attempt to import without schema verification
SHOW CREATE TABLE t2;
Table Create Table
t2 CREATE TABLE `t2` (
`a` int(11) NOT NULL DEFAULT 42,
PRIMARY KEY (`a`)
) CREATE_OPTIONS
SELECT * FROM t2;
a
42
DROP TABLE t1, t2;
80 changes: 80 additions & 0 deletions mysql-test/suite/innodb/r/import_recovery.result
@@ -0,0 +1,80 @@
#
# MDEV-26137 ALTER TABLE IMPORT enhancement
#
call mtr.add_suppression('InnoDB: Tablespace for table `test`.`t1` is set as discarded.');
call mtr.add_suppression('InnoDB: Tablespace for table `test`.`t2` is set as discarded.');
call mtr.add_suppression('InnoDB: Tablespace for table `test`.`t3` is set as discarded.');
call mtr.add_suppression('InnoDB: ./test/t3.ibd: Page 0 at offset 0 looks corrupted.');
call mtr.add_suppression("mariadbd.*: Index for table 't3' is corrupt; try to repair it");
call mtr.add_suppression("InnoDB: Expected tablespace id \\d+ but found \\d+ in the file ./test/t3.ibd");
call mtr.add_suppression("InnoDB: Corrupted page \\[page id: space=.*, page number=0\\] of datafile './test/t3.ibd' could not be found in the doublewrite buffer.");
call mtr.add_suppression('InnoDB: Tablespace for table `test`.`t4` is set as discarded.');
call mtr.add_suppression('InnoDB: ./test/t4.ibd: Page 0 at offset 0 looks corrupted.');
call mtr.add_suppression("mariadbd.*: Index for table 't4' is corrupt; try to repair it");
call mtr.add_suppression("InnoDB: Corrupted page \\[page id: space=.*, page number=0\\] of datafile './test/t4.ibd' could not be found in the doublewrite buffer.");
# Recovery from crashes
## t1: Creation of stub succeeds; server crashes; second import attempt succeeds
## t2: Creation of stub succeeds; server crashes; drop table
## t3: Creation of stub succeeds; server crashes; ibd corrupted; second import attempt fails; drop table
## t4: Did not copy .cfg; creation of stub succeeds; server crashes; ibd corrupted; second import attempt fails; drop table
CREATE TABLE t (a int) ENGINE=InnoDB;
INSERT INTO t VALUES(42);
FLUSH TABLES t FOR EXPORT;
UNLOCK TABLES;
SET GLOBAL innodb_max_dirty_pages_pct_lwm=0.0;
SET GLOBAL innodb_max_dirty_pages_pct=0.0;
connect hang1,localhost,root;
SET DEBUG_SYNC='ib_after_create_stub_for_import SIGNAL hung WAIT_FOR ever';
ALTER TABLE t1 IMPORT TABLESPACE;
connection default;
SET DEBUG_SYNC='now WAIT_FOR hung';
connect hang2,localhost,root;
SET DEBUG_SYNC='ib_after_create_stub_for_import SIGNAL hung WAIT_FOR ever';
ALTER TABLE t2 IMPORT TABLESPACE;
connection default;
SET DEBUG_SYNC='now WAIT_FOR hung';
connect hang3,localhost,root;
SET DEBUG_SYNC='ib_after_create_stub_for_import SIGNAL hung WAIT_FOR ever';
ALTER TABLE t3 IMPORT TABLESPACE;
connection default;
SET DEBUG_SYNC='now WAIT_FOR hung';
connect hang4,localhost,root;
SET DEBUG_SYNC='ib_after_create_stub_for_import SIGNAL hung WAIT_FOR ever';
ALTER TABLE t4 IMPORT TABLESPACE;
connection default;
SET DEBUG_SYNC='now WAIT_FOR hung';
# corrupting the 0th page
# Restart mysqld after the crash and reconnect.
# restart
ALTER TABLE t1 IMPORT TABLESPACE;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
SELECT * FROM t1;
a
42
ALTER TABLE t3 IMPORT TABLESPACE;
ERROR HY000: Internal error: Error importing tablespace for table `test`.`t3` : Data structure corruption
ALTER TABLE t4 IMPORT TABLESPACE;
ERROR HY000: Schema mismatch (Expected FSP_SPACE_FLAGS=0x15, .ibd file contains 0x1010115.)
DROP TABLE t, t1, t2, t3, t4;
# Recovery from corruption only, no server restart
## t5: Recovery from corruption, with cfg
## t6: Recovery from corruption, without cfg
call mtr.add_suppression('InnoDB: ./test/t5.ibd: Page 0 at offset 0 looks corrupted.');
call mtr.add_suppression("mariadbd.*: Index for table 't5' is corrupt; try to repair it");
call mtr.add_suppression("InnoDB: Corrupted page \\[page id: space=.*, page number=0\\] of datafile './test/t5.ibd' could not be found in the doublewrite buffer.");
call mtr.add_suppression("InnoDB: Corrupted page \\[page id: space=.*, page number=0\\] of datafile './test/t6.ibd' could not be found in the doublewrite buffer.");
call mtr.add_suppression("mariadbd.*: Index for table 't6' is corrupt; try to repair it");
CREATE TABLE t (a int) ENGINE=InnoDB;
INSERT INTO t VALUES(42);
FLUSH TABLES t FOR EXPORT;
UNLOCK TABLES;
# corrupting the 0th page
ALTER TABLE t5 IMPORT TABLESPACE;
ERROR HY000: Internal error: Error importing tablespace for table `test`.`t5` : Data structure corruption
ALTER TABLE t6 IMPORT TABLESPACE;
ERROR HY000: Schema mismatch (Expected FSP_SPACE_FLAGS=0x15, .ibd file contains 0x1010115.)
DROP TABLE t, t5, t6;
104 changes: 104 additions & 0 deletions mysql-test/suite/innodb/r/import_run_once.result
@@ -0,0 +1,104 @@
#
# MDEV-26137 ALTER TABLE IMPORT enhancement
#
# drop t1 before importing t2
CREATE TABLE t1 (a int) ENGINE=InnoDB;
INSERT INTO t1 VALUES(42);
FLUSH TABLES t1 FOR EXPORT;
UNLOCK TABLES;
DROP TABLE t1;
ALTER TABLE t2 IMPORT TABLESPACE;
SHOW CREATE TABLE t2;
Table Create Table
t2 CREATE TABLE `t2` (
`a` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
SELECT * FROM t2;
a
42
DROP TABLE t2;
# created t2 but did not discard tablespace
CREATE TABLE t1 (a int) ENGINE=InnoDB;
INSERT INTO t1 VALUES(42);
CREATE TABLE t2 LIKE t1;
FLUSH TABLES t1 FOR EXPORT;
UNLOCK TABLES;
DROP TABLE t1;
call mtr.add_suppression("InnoDB: Unable to import tablespace");
ALTER TABLE t2 IMPORT TABLESPACE;
ERROR HY000: Tablespace for table 'test/t2' exists. Please DISCARD the tablespace before IMPORT
SHOW CREATE TABLE t2;
Table Create Table
t2 CREATE TABLE `t2` (
`a` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
SELECT * FROM t2;
a
DROP TABLE t2;
# attempt to import when there's no tablespace
ALTER TABLE t2 IMPORT TABLESPACE;
ERROR 42S02: Table 'test.t2' doesn't exist
# with index
CREATE TABLE t1 (a int, b varchar(50)) ENGINE=InnoDB;
CREATE UNIQUE INDEX ai ON t1 (a);
INSERT INTO t1 VALUES(42, "hello");
FLUSH TABLES t1 FOR EXPORT;
UNLOCK TABLES;
ALTER TABLE t2 IMPORT TABLESPACE;
SHOW CREATE TABLE t2;
Table Create Table
t2 CREATE TABLE `t2` (
`a` int(11) DEFAULT NULL,
`b` varchar(50) DEFAULT NULL,
UNIQUE KEY `ai` (`a`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
SELECT * FROM t2;
a b
42 hello
SHOW INDEX FROM t1;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Ignored
t1 0 ai 1 a A 1 NULL NULL YES BTREE NO
SHOW INDEX FROM t2;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Ignored
t2 0 ai 1 a A 1 NULL NULL YES BTREE NO
DROP TABLE t1, t2;
# with virtual column index
CREATE TABLE t1 (a int, b int as (a * a)) ENGINE=InnoDB;
CREATE UNIQUE INDEX ai ON t1 (b);
INSERT INTO t1 VALUES(42, default);
FLUSH TABLES t1 FOR EXPORT;
UNLOCK TABLES;
ALTER TABLE t2 IMPORT TABLESPACE;
SHOW CREATE TABLE t2;
Table Create Table
t2 CREATE TABLE `t2` (
`a` int(11) DEFAULT NULL,
`b` int(11) GENERATED ALWAYS AS (`a` * `a`) VIRTUAL,
UNIQUE KEY `ai` (`b`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
SELECT * FROM t2;
a b
42 1764
SELECT b FROM t2 USE INDEX (ai);
b
1764
SHOW INDEX FROM t1;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Ignored
t1 0 ai 1 b A 1 NULL NULL YES BTREE NO
SHOW INDEX FROM t2;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Ignored
t2 0 ai 1 b A 1 NULL NULL YES BTREE NO
CHECK TABLE t2 EXTENDED;
Table Op Msg_type Msg_text
test.t2 check status OK
DROP TABLE t1, t2;
# with auto_increment
CREATE TABLE t1 (id INT PRIMARY KEY AUTO_INCREMENT, i2 INT, i1 INT)ENGINE=INNODB;
INSERT INTO t1 (i2) SELECT 4 FROM seq_1_to_1024;
FLUSH TABLE t1 FOR EXPORT;
UNLOCK TABLES;
ALTER TABLE t2 IMPORT TABLESPACE;
CHECK TABLE t2 EXTENDED;
Table Op Msg_type Msg_text
test.t2 check status OK
DROP TABLE t2, t1;
3 changes: 3 additions & 0 deletions mysql-test/suite/innodb/t/import.test
@@ -0,0 +1,3 @@
--source include/import_begin.inc
--copy_file $MYSQLD_DATADIR/test/t1.cfg $MYSQLD_DATADIR/test/t2.cfg
--source include/import_end.inc
4 changes: 4 additions & 0 deletions mysql-test/suite/innodb/t/import_no_cfg.test
@@ -0,0 +1,4 @@
# embedded server uses absolute path, causing result mismatch in warning messages
--source include/not_embedded.inc
--source include/import_begin.inc
--source include/import_end.inc

0 comments on commit 9b431d7

Please sign in to comment.