revision-id: 2cde654d9948eaf28dff621db34c44ce47c20b5f (mariadb-10.2.26-25-g2cde654d994) parent(s): 65296123d0fcaeb122bc3b9d9e387468052c06b6 author: Sujatha committer: Sujatha timestamp: 2019-08-14 17:31:23 +0530 message: MENT-325: DROP TABLE IF EXISTS killed on master but was replicated Problem: ======= DROP TABLE IF EXISTS was killed. The table still exists on the master but the DDL was still logged. Fix: === When DROP TABLE command execution is interuppted because of kill command the replicated DROP TABLE command should include only the list of tables which were dropped succesfully.MENT-325: DROP TABLE IF EXISTS killed on master but was replicated Problem: ======= DROP TABLE IF EXISTS was killed. The table still exists on the master but the DDL was still logged. Analysis: ========= During the execution of DROP TABLE command "ha_delete_table" call is invoked to delete the table. If the query is killed at this point, the kill command is not handled within the code. This results in two issues. 1) The table which is not dropped also gets written into the binary log. 2) The code continues further upon receiving 'KILL QUERY'. Fix: === Upon receiving the KILL command the query should stop its current execution. Tables which were successfully dropped prior to KILL command should be included in the binary log. --- .../suite/rpl/r/rpl_failed_drop_tbl_binlog.result | 29 ++++++++++ .../suite/rpl/t/rpl_failed_drop_tbl_binlog.test | 61 ++++++++++++++++++++++ sql/sql_table.cc | 53 +++++++++++-------- 3 files changed, 122 insertions(+), 21 deletions(-) diff --git a/mysql-test/suite/rpl/r/rpl_failed_drop_tbl_binlog.result b/mysql-test/suite/rpl/r/rpl_failed_drop_tbl_binlog.result new file mode 100644 index 00000000000..2831cb05dbd --- /dev/null +++ b/mysql-test/suite/rpl/r/rpl_failed_drop_tbl_binlog.result @@ -0,0 +1,29 @@ +include/master-slave.inc +[connection master] +create table t1 (a int) engine=innodb; +create table t2 (b longblob) engine=innodb; +create table t3 (c int) engine=innodb; +insert into t2 values (repeat('b',1024*1024)); +insert into t2 select * from t2; +insert into t2 select * from t2; +insert into t2 select * from t2; +insert into t2 select * from t2; +set debug_sync='rm_table_no_locks_before_delete_table SIGNAL nogo WAIT_FOR go EXECUTE 2'; +drop table t1, t2, t3; +connect foo,localhost,root; +set debug_sync='now SIGNAL go'; +kill query CONNECTION_ID; +connection master; +ERROR 70100: Query execution was interrupted +"Tables t2 and t3 should be listed" +SHOW TABLES; +Tables_in_test +t2 +t3 +include/show_binlog_events.inc +Log_name Pos Event_type Server_id End_log_pos Info +master-bin.000001 # Gtid # # GTID #-#-# +master-bin.000001 # Query # # use `test`; DROP TABLE `t1` /* generated by server */ +set debug_sync='RESET'; +drop table t2, t3; +include/rpl_end.inc diff --git a/mysql-test/suite/rpl/t/rpl_failed_drop_tbl_binlog.test b/mysql-test/suite/rpl/t/rpl_failed_drop_tbl_binlog.test new file mode 100644 index 00000000000..93ac6916777 --- /dev/null +++ b/mysql-test/suite/rpl/t/rpl_failed_drop_tbl_binlog.test @@ -0,0 +1,61 @@ +# ==== Purpose ==== +# +# Check that when the execution of a DROP TABLE command with single table +# fails it should not be written to the binary log. Also test that when the +# execution of DROP TABLE command with multiple tables fails the command +# should be written into the binary log. +# +# ==== Implementation ==== +# +# Steps: +# 0 - Create tables named t1, t2, t3 +# 1 - Execute DROP TABLE t1,t2,t3 command. +# 2 - Kill the DROP TABLE command while it is trying to drop table 't2'. +# 3 - Verify that tables t2,t3 are present after the DROP command execution +# was interrupted. +# 4 - Check that table 't1' is present in binary log as part of DROP +# command. +# +# ==== References ==== +# +# MENT-325: DROP TABLE IF EXISTS killed on master but was replicated. +# + +--source include/have_innodb.inc +--source include/have_debug_sync.inc +--source include/have_binlog_format_statement.inc +--source include/master-slave.inc + +create table t1 (a int) engine=innodb; +create table t2 (b longblob) engine=innodb; +create table t3 (c int) engine=innodb; +insert into t2 values (repeat('b',1024*1024)); +insert into t2 select * from t2; +insert into t2 select * from t2; +insert into t2 select * from t2; +insert into t2 select * from t2; +let $binlog_start= query_get_value(SHOW MASTER STATUS, Position, 1); + +let $id=`select connection_id()`; +set debug_sync='rm_table_no_locks_before_delete_table SIGNAL nogo WAIT_FOR go EXECUTE 2'; +send drop table t1, t2, t3; + +connect foo,localhost,root; +set debug_sync='now SIGNAL go'; +let $wait_condition=select 1 from information_schema.processlist where state like 'debug sync point:%'; +source include/wait_condition.inc; +--replace_result $id CONNECTION_ID +eval kill query $id; + +connection master; +error ER_QUERY_INTERRUPTED; +reap; + +--echo "Tables t2 and t3 should be listed" +SHOW TABLES; +--source include/show_binlog_events.inc + +set debug_sync='RESET'; +drop table t2, t3; + +source include/rpl_end.inc; diff --git a/sql/sql_table.cc b/sql/sql_table.cc index 9d7e03727d3..c78fca28031 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -2185,6 +2185,7 @@ int mysql_rm_table_no_locks(THD *thd, TABLE_LIST *tables, bool if_exists, int error= 0; int non_temp_tables_count= 0; bool non_tmp_error= 0; + bool at_least_one_table_dropped= false; bool trans_tmp_table_deleted= 0, non_trans_tmp_table_deleted= 0; bool non_tmp_table_deleted= 0; bool is_drop_tmp_if_exists_added= 0; @@ -2377,19 +2378,6 @@ int mysql_rm_table_no_locks(THD *thd, TABLE_LIST *tables, bool if_exists, binary log. See further information on this variable in what follows. */ non_tmp_table_deleted= (if_exists ? TRUE : non_tmp_table_deleted); - /* - Don't write the database name if it is the current one (or if - thd->db is NULL). - */ - if (thd->db == NULL || strcmp(db,thd->db) != 0) - { - append_identifier(thd, &built_query, db, db_length); - built_query.append("."); - } - - append_identifier(thd, &built_query, table->table_name, - table->table_name_length); - built_query.append(","); } } DEBUG_SYNC(thd, "rm_table_no_locks_before_delete_table"); @@ -2464,14 +2452,20 @@ int mysql_rm_table_no_locks(THD *thd, TABLE_LIST *tables, bool if_exists, // Remove extension for delete *(end= path + path_length - reg_ext_length)= '\0'; - error= ha_delete_table(thd, table_type, path, db, table->table_name, - !dont_log_query); - - if (!error) + if ((error= ha_delete_table(thd, table_type, path, db, table->table_name, + !dont_log_query))) + { + if (thd->killed) + { + error= -1; + goto err; + } + } + else { int frm_delete_error, trigger_drop_error= 0; - /* Delete the table definition file */ - strmov(end,reg_ext); + /* Delete the table definition file */ + strmov(end,reg_ext); if (table_type && table_type != view_pseudo_hton && table_type->discover_table) { @@ -2505,7 +2499,7 @@ int mysql_rm_table_no_locks(THD *thd, TABLE_LIST *tables, bool if_exists, if (error) { if (wrong_tables.length()) - wrong_tables.append(','); + wrong_tables.append(','); wrong_tables.append(db); wrong_tables.append('.'); wrong_tables.append(table->table_name); @@ -2518,6 +2512,22 @@ int mysql_rm_table_no_locks(THD *thd, TABLE_LIST *tables, bool if_exists, mysql_audit_drop_table(thd, table); } + /* + Add table name to "DROP TABLE command" only after successful deletion. + Don't write the database name if it is the current one (or if + thd->db is NULL). + */ + if (thd->db == NULL || strcmp(db,thd->db) != 0) + { + append_identifier(thd, &built_query, db, db_length); + built_query.append("."); + } + + append_identifier(thd, &built_query, table->table_name, + table->table_name_length); + built_query.append(","); + at_least_one_table_dropped= true; + DBUG_PRINT("table", ("table: %p s: %p", table->table, table->table ? table->table->s : NULL)); } @@ -2586,7 +2596,8 @@ int mysql_rm_table_no_locks(THD *thd, TABLE_LIST *tables, bool if_exists, is_drop_tmp_if_exists_added, 0); } - if (non_tmp_table_deleted) + if (non_tmp_table_deleted && + (at_least_one_table_dropped == true)) { /* Chop of the last comma */ built_query.chop();