Commit 2556fe1a authored by Yuchen Pei's avatar Yuchen Pei

MDEV-31996 Create connection on demand in spider_db_delete_all_rows

When spider_db_delete_all_rows() is called, the supplied spider->conns
may have already been freed. The existing mechanism has spider_trx own
the connections in trx_conn_hash and it may free a conn during the
cleanup after a query. When running a delete query and if the table is
in the table cache, ha_spider::open() would not be called which would
recreate the conn. So we recreate the conn when necessary during
delete by calling spider_check_trx_and_get_conn().

We also reduce code duplication as delete_all_rows() and truncate()
has almost identical code, and there's no need to assign
wide_handler->sql_command in these functions because it has already
been correctly assigned.
parent 194e4f2e
...@@ -11223,41 +11223,11 @@ int ha_spider::pre_direct_delete_rows() ...@@ -11223,41 +11223,11 @@ int ha_spider::pre_direct_delete_rows()
int ha_spider::delete_all_rows() int ha_spider::delete_all_rows()
{ {
int error_num, roop_count;
THD *thd = ha_thd(); THD *thd = ha_thd();
backup_error_status();
DBUG_ENTER("ha_spider::delete_all_rows"); DBUG_ENTER("ha_spider::delete_all_rows");
DBUG_PRINT("info",("spider this=%p", this));
if (spider_param_delete_all_rows_type(thd, share->delete_all_rows_type)) if (spider_param_delete_all_rows_type(thd, share->delete_all_rows_type))
DBUG_RETURN(HA_ERR_WRONG_COMMAND); DBUG_RETURN(HA_ERR_WRONG_COMMAND);
if (spider_param_read_only_mode(thd, share->read_only_mode)) DBUG_RETURN(truncate());
{
my_printf_error(ER_SPIDER_READ_ONLY_NUM, ER_SPIDER_READ_ONLY_STR, MYF(0),
table_share->db.str, table_share->table_name.str);
DBUG_RETURN(ER_SPIDER_READ_ONLY_NUM);
}
#ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS
do_direct_update = FALSE;
#endif
sql_kinds = SPIDER_SQL_KIND_SQL;
for (roop_count = 0; roop_count < (int) share->link_count; roop_count++)
sql_kind[roop_count] = SPIDER_SQL_KIND_SQL;
if ((error_num = spider_db_delete_all_rows(this)))
DBUG_RETURN(check_error_mode(error_num));
if (sql_command == SQLCOM_TRUNCATE && table->found_next_number_field)
{
DBUG_PRINT("info",("spider reset auto increment"));
pthread_mutex_lock(&share->lgtm_tblhnd_share->auto_increment_mutex);
share->lgtm_tblhnd_share->auto_increment_lclval = 1;
share->lgtm_tblhnd_share->auto_increment_init = FALSE;
share->lgtm_tblhnd_share->auto_increment_value = 1;
DBUG_PRINT("info",("spider init auto_increment_lclval=%llu",
share->lgtm_tblhnd_share->auto_increment_lclval));
DBUG_PRINT("info",("spider auto_increment_value=%llu",
share->lgtm_tblhnd_share->auto_increment_value));
pthread_mutex_unlock(&share->lgtm_tblhnd_share->auto_increment_mutex);
}
DBUG_RETURN(0);
} }
int ha_spider::truncate() int ha_spider::truncate()
......
for master_1
for child2
for child3
CREATE SERVER srv FOREIGN DATA WRAPPER mysql
OPTIONS (SOCKET "$MASTER_1_MYSOCK", DATABASE 'test',user 'root');
set session spider_delete_all_rows_type=0;
create table t2 (c int);
create table t1 (c int) ENGINE=Spider
COMMENT='WRAPPER "mysql", srv "srv",TABLE "t2", delete_all_rows_type "0"';
delete from t1;
drop table t1, t2;
create table t2 (c int);
create table t1 (c int) ENGINE=Spider
COMMENT='WRAPPER "mysql", srv "srv",TABLE "t2", delete_all_rows_type "0"';
insert ignore into t1 values (42), (378);
select * from t1;
c
42
378
delete from t1;
select * from t1;
c
drop table t1, t2;
create table t2 (c int);
create table t1 (c int) ENGINE=Spider
COMMENT='WRAPPER "mysql", srv "srv",TABLE "t2", delete_all_rows_type "0"';
truncate t1;
drop table t1, t2;
create table t2 (c int);
create table t1 (c int) ENGINE=Spider
COMMENT='WRAPPER "mysql", srv "srv",TABLE "t2", delete_all_rows_type "0"';
insert ignore into t1 values (42), (378);
select * from t1;
c
42
378
truncate t1;
select * from t1;
c
drop table t1, t2;
drop server srv;
for master_1
for child2
for child3
#
# end of test tmp
#
--disable_query_log
--disable_result_log
--source ../../t/test_init.inc
--enable_result_log
--enable_query_log
evalp CREATE SERVER srv FOREIGN DATA WRAPPER mysql
OPTIONS (SOCKET "$MASTER_1_MYSOCK", DATABASE 'test',user 'root');
set session spider_delete_all_rows_type=0;
create table t2 (c int);
create table t1 (c int) ENGINE=Spider
COMMENT='WRAPPER "mysql", srv "srv",TABLE "t2", delete_all_rows_type "0"';
delete from t1;
drop table t1, t2;
create table t2 (c int);
create table t1 (c int) ENGINE=Spider
COMMENT='WRAPPER "mysql", srv "srv",TABLE "t2", delete_all_rows_type "0"';
insert ignore into t1 values (42), (378);
select * from t1;
delete from t1;
select * from t1;
drop table t1, t2;
create table t2 (c int);
create table t1 (c int) ENGINE=Spider
COMMENT='WRAPPER "mysql", srv "srv",TABLE "t2", delete_all_rows_type "0"';
truncate t1;
drop table t1, t2;
create table t2 (c int);
create table t1 (c int) ENGINE=Spider
COMMENT='WRAPPER "mysql", srv "srv",TABLE "t2", delete_all_rows_type "0"';
insert ignore into t1 values (42), (378);
select * from t1;
truncate t1;
select * from t1;
drop table t1, t2;
drop server srv;
--disable_query_log
--disable_result_log
--source ../../t/test_deinit.inc
--enable_result_log
--enable_query_log
--echo #
--echo # end of test tmp
--echo #
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment