Skip to content

Commit

Permalink
MDEV-22979 MDEV-27233 MDEV-28218 Fixing spider init bugs
Browse files Browse the repository at this point in the history
Fix spider init bugs (MDEV-22979, MDEV-27233, MDEV-28218) while
preventing regression on old ones (MDEV-30370, MDEV-29904)

Two things are changed:

First, Spider initialisation is made fully synchronous, i.e. it no
longer happens in a background thread. Adapted from the original fix
by nayuta for MDEV-27233. This change itself would cause failure when
spider is initialised early, by plugin-load-add, due to dependency on
Aria and udf function creation, which are fixed in the second and
third parts below. Requires SQL Service, thus porting earlier versions
requires MDEV-27595

Second, if spider is initialised before udf_init(), create udf by
inserting into `mysql.func`, otherwise do it by `CREATE FUNCTION` as
usual. This change may be generalised in MDEV-31401.

Also factor out some clean-up queries from deinit_spider.inc for use
of spider init tests.

A minor caveat is that early spider initialisation will fail if the
server is bootstrapped for the first time, due to missing `mysql`
database which needs to be created by the bootstrap script.
  • Loading branch information
mariadb-YuchenPei committed Dec 7, 2023
1 parent afe63ec commit d8f5d2b
Show file tree
Hide file tree
Showing 36 changed files with 337 additions and 139 deletions.
5 changes: 5 additions & 0 deletions storage/spider/mysql-test/spider/bugfix/r/mdev_22979.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
MDEV-22979 "mysqld --bootstrap" / mysql_install_db hangs when Spider is installed
# Kill the server
# restart
Warnings:
Note 1305 SONAME ha_spider.so does not exist
11 changes: 11 additions & 0 deletions storage/spider/mysql-test/spider/bugfix/r/mdev_27233.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#
# MDEV-27233 Server hangs when using --init-file which loads Spider and creates a Spider table
#
show create table t;
Table Create Table
t CREATE TABLE `t` (
`c` int(11) DEFAULT NULL
) ENGINE=SPIDER DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
Warnings:
Error 1429 Unable to connect to foreign data source: localhost
Error 1429 Unable to connect to foreign data source: localhost
9 changes: 9 additions & 0 deletions storage/spider/mysql-test/spider/bugfix/r/mdev_28218.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# MDEV-28218 Spider: thread hang/deadlock as result of INSTALL PLUGIN and DROP TABLE
#
INSTALL SONAME 'ha_spider.so';
DROP TABLE IF EXISTS mysql.spider_tables;
show create table mysql.spider_tables;
ERROR 42S02: Table 'mysql.spider_tables' doesn't exist
Warnings:
Note 1051 Unknown table 'mysql.spider_tables'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#
# MDEV-28218 Spider: thread hang/deadlock as result of INSTALL PLUGIN and DROP TABLE
#
show create table mysql.spider_tables;
ERROR 42S02: Table 'mysql.spider_tables' doesn't exist
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#
# MDEV-28218 Spider: thread hang/deadlock as result of INSTALL PLUGIN and DROP TABLE
#
show create table mysql.spider_tables;
ERROR 42S02: Table 'mysql.spider_tables' doesn't exist
4 changes: 4 additions & 0 deletions storage/spider/mysql-test/spider/bugfix/r/mdev_30370.result
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#
# MDEV-30370 mariadbd hangs when running with --wsrep-recover and --plugin-load-add=ha_spider.so
#
# Kill the server
# restart
Warnings:
Note 1305 SONAME ha_spider.so does not exist
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#
# plugin-load-add=ha_spider
#
select * from mysql.plugin;
name dl
create table t (c int) Engine=SPIDER;
drop table t;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#
# plugin-load-add=ha_spider
#
select * from mysql.plugin;
name dl
create table t (c int) Engine=SPIDER;
drop table t;
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#
# Test that udf created by inserting into mysql_func works as expected
#
CREATE SERVER s_1 FOREIGN DATA WRAPPER mysql OPTIONS (
HOST 'localhost',
DATABASE 'auto_test_local',
USER 'root',
PASSWORD '',
SOCKET '$MASTER_1_MYSOCK'
);
CREATE SERVER s_2_1 FOREIGN DATA WRAPPER mysql OPTIONS (
HOST 'localhost',
DATABASE 'auto_test_remote',
USER 'root',
PASSWORD '',
SOCKET '$CHILD2_1_MYSOCK'
);
connect master_1, localhost, root, , , $MASTER_1_MYPORT, $MASTER_1_MYSOCK;
connect child2_1, localhost, root, , , $CHILD2_1_MYPORT, $CHILD2_1_MYSOCK;
connection child2_1;
CREATE DATABASE auto_test_remote;
USE auto_test_remote;
CREATE TABLE tbl_a (
a INT
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into tbl_a values (42);
connection master_1;
CREATE DATABASE auto_test_local;
USE auto_test_local;
CREATE TABLE tbl_a (
a INT
) ENGINE=Spider DEFAULT CHARSET=utf8 COMMENT='table "tbl_a", srv "s_2_1"';
create temporary table results (a int);
SELECT SPIDER_DIRECT_SQL('select * from tbl_a', 'results', 'srv "s_2_1", database "auto_test_remote"');
SPIDER_DIRECT_SQL('select * from tbl_a', 'results', 'srv "s_2_1", database "auto_test_remote"')
1
select * from results;
a
42
connection master_1;
DROP DATABASE IF EXISTS auto_test_local;
connection child2_1;
DROP DATABASE IF EXISTS auto_test_remote;
18 changes: 18 additions & 0 deletions storage/spider/mysql-test/spider/bugfix/t/mdev_22979.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--echo MDEV-22979 "mysqld --bootstrap" / mysql_install_db hangs when Spider is installed

# This test is not the most faithful, as it does not have any
# dependency problems on the existence of the `mysql` database. To
# test MDEV-22979 faithfully, a mysql_install_db invocation with
# --plugin-load-add=ha_spider should be run. We cannot run it in mtr
# because we do not have access to --srcdir.

let $MYSQLD_DATADIR= `select @@datadir`;
let $PLUGIN_DIR=`select @@plugin_dir`;
--source include/kill_mysqld.inc
--write_file $MYSQLTEST_VARDIR/tmp/mdev_22979.sql
drop table if exists foo.bar;
EOF
--exec $MYSQLD_CMD --datadir=$MYSQLD_DATADIR --bootstrap --plugin-dir=$PLUGIN_DIR --plugin-load-add=ha_spider < $MYSQLTEST_VARDIR/tmp/mdev_22979.sql
--source include/start_mysqld.inc
--disable_query_log
--source ../../include/clean_up_spider.inc
1 change: 1 addition & 0 deletions storage/spider/mysql-test/spider/bugfix/t/mdev_27233.opt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--init-file=$MYSQL_TEST_DIR/../storage/spider/mysql-test/spider/bugfix/t/mdev_27233.sql
3 changes: 3 additions & 0 deletions storage/spider/mysql-test/spider/bugfix/t/mdev_27233.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
INSTALL SONAME 'ha_spider.so';
USE test;
CREATE TABLE t (c INT) ENGINE=SPIDER;
7 changes: 7 additions & 0 deletions storage/spider/mysql-test/spider/bugfix/t/mdev_27233.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
--echo #
--echo # MDEV-27233 Server hangs when using --init-file which loads Spider and creates a Spider table
--echo #
# ps protocol eats warnings
--disable_ps_protocol
show create table t;
--enable_ps_protocol
10 changes: 10 additions & 0 deletions storage/spider/mysql-test/spider/bugfix/t/mdev_28218.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--echo #
--echo # MDEV-28218 Spider: thread hang/deadlock as result of INSTALL PLUGIN and DROP TABLE
--echo #
INSTALL SONAME 'ha_spider.so';
DROP TABLE IF EXISTS mysql.spider_tables;
--error ER_NO_SUCH_TABLE
show create table mysql.spider_tables;

--disable_query_log
--source ../../include/clean_up_spider.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--init-file=$MYSQL_TEST_DIR/../storage/spider/mysql-test/spider/bugfix/t/mdev_28218_init_file.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
INSTALL PLUGIN spider SONAME 'ha_spider.so';
DROP TABLE IF EXISTS mysql.spider_tables;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
--echo #
--echo # MDEV-28218 Spider: thread hang/deadlock as result of INSTALL PLUGIN and DROP TABLE
--echo #
# This is a variant of the testcase in MDEV-28218, where we put the
# queries are in an init file
--error ER_NO_SUCH_TABLE
show create table mysql.spider_tables;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--plugin-load-add=ha_spider
--init-file=$MYSQL_TEST_DIR/../storage/spider/mysql-test/spider/bugfix/t/mdev_28218_mixed.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS mysql.spider_tables;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--echo #
--echo # MDEV-28218 Spider: thread hang/deadlock as result of INSTALL PLUGIN and DROP TABLE
--echo #
# this is a variant of the testcase in MDEV-28218, where we load
# spider early with --plugin_load_add, and execute the drop table
# query in an init file
--error ER_NO_SUCH_TABLE
show create table mysql.spider_tables;
8 changes: 7 additions & 1 deletion storage/spider/mysql-test/spider/bugfix/t/mdev_30370.test
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@
--echo # MDEV-30370 mariadbd hangs when running with --wsrep-recover and --plugin-load-add=ha_spider.so
--echo #

--exec $MYSQLD_BOOTSTRAP_CMD --wsrep-recover --plugin-load-add=ha_spider.so
let $MYSQLD_DATADIR= `select @@datadir`;
let $PLUGIN_DIR=`select @@plugin_dir`;
--source include/kill_mysqld.inc
--exec $MYSQLD_CMD --datadir=$MYSQLD_DATADIR --wsrep-recover --plugin-dir=$PLUGIN_DIR --plugin-load-add=ha_spider
--source include/start_mysqld.inc
--disable_query_log
--source ../../include/clean_up_spider.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--plugin-load-add=ha_spider
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
--echo #
--echo # plugin-load-add=ha_spider
--echo #
# A simple test that tests plugin-load-add=ha_spider
select * from mysql.plugin;
create table t (c int) Engine=SPIDER;
drop table t;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--plugin-load-add=SPIDER=ha_spider
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
--echo #
--echo # plugin-load-add=ha_spider
--echo #
# A simple test that tests plugin-load-add=SPIDER=ha_spider
select * from mysql.plugin;
create table t (c int) Engine=SPIDER;
drop table t;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
!include include/default_mysqld.cnf
!include ../my_1_1.cnf
!include ../my_2_1.cnf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--plugin-load-add=ha_spider
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
--echo #
--echo # Test that udf created by inserting into mysql_func works as expected
--echo #

evalp CREATE SERVER s_1 FOREIGN DATA WRAPPER mysql OPTIONS (
HOST 'localhost',
DATABASE 'auto_test_local',
USER 'root',
PASSWORD '',
SOCKET '$MASTER_1_MYSOCK'
);
evalp CREATE SERVER s_2_1 FOREIGN DATA WRAPPER mysql OPTIONS (
HOST 'localhost',
DATABASE 'auto_test_remote',
USER 'root',
PASSWORD '',
SOCKET '$CHILD2_1_MYSOCK'
);

--connect (master_1, localhost, root, , , $MASTER_1_MYPORT, $MASTER_1_MYSOCK)
--connect (child2_1, localhost, root, , , $CHILD2_1_MYPORT, $CHILD2_1_MYSOCK)

--connection child2_1
CREATE DATABASE auto_test_remote;
USE auto_test_remote;
eval CREATE TABLE tbl_a (
a INT
) $CHILD2_1_ENGINE $CHILD2_1_CHARSET;
insert into tbl_a values (42);

--connection master_1
CREATE DATABASE auto_test_local;
USE auto_test_local;
eval CREATE TABLE tbl_a (
a INT
) $MASTER_1_ENGINE $MASTER_1_CHARSET COMMENT='table "tbl_a", srv "s_2_1"';

create temporary table results (a int);
--disable_ps_protocol
SELECT SPIDER_DIRECT_SQL('select * from tbl_a', 'results', 'srv "s_2_1", database "auto_test_remote"');
--enable_ps_protocol
select * from results;

--connection master_1
DROP DATABASE IF EXISTS auto_test_local;

--connection child2_1
DROP DATABASE IF EXISTS auto_test_remote;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
!include include/default_mysqld.cnf
!include ../my_1_1.cnf
!include ../my_2_1.cnf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--init-file=$MYSQL_TEST_DIR/../storage/spider/mysql-test/spider/bugfix/t/udf_mysql_func_early_init_file.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
install soname "ha_spider.so";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Same as udf_mysql_func_early.test, except that we load spider in
# init_file, which is after udf_init() and before
# mysqld_server_started is on.
--source udf_mysql_func_early.test
1 change: 0 additions & 1 deletion storage/spider/spd_include.h
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,6 @@ typedef struct st_spider_thread
volatile bool killed;
volatile bool thd_wait;
volatile bool first_free_wait;
volatile bool init_command;
volatile int error;
pthread_t thread;
pthread_cond_t cond;
Expand Down
86 changes: 41 additions & 45 deletions storage/spider/spd_init_query.h
Original file line number Diff line number Diff line change
Expand Up @@ -585,56 +585,52 @@ static LEX_STRING spider_init_queries[] = {
" engine=Aria transactional=1;"
"end if;"
)},
/*
Install UDFs
*/
{C_STRING_WITH_LEN(
"set @win_plugin := IF(@@version_compile_os like 'Win%', 1, 0);"
)},
/* Install UDFs. If udf is not initialised, then install by
inserting into mysql.func */
{C_STRING_WITH_LEN(
"if @win_plugin = 0 then"
" create function if not exists spider_direct_sql returns int"
" soname 'ha_spider.so';"
"else"
" create function if not exists spider_direct_sql returns int"
" soname 'ha_spider.dll';"
"end if;"
)},
{C_STRING_WITH_LEN(
"if @win_plugin = 0 then"
" create aggregate function if not exists spider_bg_direct_sql returns int"
" soname 'ha_spider.so';"
"else"
" create aggregate function if not exists spider_bg_direct_sql returns int"
" soname 'ha_spider.dll';"
"end if;"
)},
{C_STRING_WITH_LEN(
"if @win_plugin = 0 then"
" create function if not exists spider_ping_table returns int"
" soname 'ha_spider.so';"
" begin not atomic"
" declare exit handler for 1041, 1123"
" insert into mysql.func values"
" ('spider_direct_sql', 2, 'ha_spider.so', 'function'),"
" ('spider_bg_direct_sql', 2, 'ha_spider.so', 'aggregate'),"
" ('spider_ping_table', 2, 'ha_spider.so', 'function'),"
" ('spider_copy_tables', 2, 'ha_spider.so', 'function'),"
" ('spider_flush_table_mon_cache', 2, 'ha_spider.so', 'function');"
" create function if not exists spider_direct_sql returns int"
" soname 'ha_spider.so';"
" create aggregate function if not exists spider_bg_direct_sql returns int"
" soname 'ha_spider.so';"
" create function if not exists spider_ping_table returns int"
" soname 'ha_spider.so';"
" create function if not exists spider_copy_tables returns int"
" soname 'ha_spider.so';"
" create function if not exists spider_flush_table_mon_cache returns int"
" soname 'ha_spider.so';"
" end;"
"else"
" create function if not exists spider_ping_table returns int"
" soname 'ha_spider.dll';"
" begin not atomic"
" declare exit handler for 1041, 1123"
" insert into mysql.func values"
" ('spider_direct_sql', 2, 'ha_spider.dll', 'function'),"
" ('spider_bg_direct_sql', 2, 'ha_spider.dll', 'aggregate'),"
" ('spider_ping_table', 2, 'ha_spider.dll', 'function'),"
" ('spider_copy_tables', 2, 'ha_spider.dll', 'function'),"
" ('spider_flush_table_mon_cache', 2, 'ha_spider.dll', 'function');"
" create function if not exists spider_direct_sql returns int"
" soname 'ha_spider.dll';"
" create aggregate function if not exists spider_bg_direct_sql returns int"
" soname 'ha_spider.dll';"
" create function if not exists spider_ping_table returns int"
" soname 'ha_spider.dll';"
" create function if not exists spider_copy_tables returns int"
" soname 'ha_spider.dll';"
" create function if not exists spider_flush_table_mon_cache returns int"
" soname 'ha_spider.dll';"
" end;"
"end if;"
)},
{C_STRING_WITH_LEN(
"if @win_plugin = 0 then"
" create function if not exists spider_copy_tables returns int"
" soname 'ha_spider.so';"
"else"
" create function if not exists spider_copy_tables returns int"
" soname 'ha_spider.dll';"
"end if;"
)},
{C_STRING_WITH_LEN(
"if @win_plugin = 0 then"
" create function if not exists spider_flush_table_mon_cache returns int"
" soname 'ha_spider.so';"
"else"
" create function if not exists spider_flush_table_mon_cache returns int"
" soname 'ha_spider.dll';"
"end if;"
)},
{C_STRING_WITH_LEN("")}
)}
};

0 comments on commit d8f5d2b

Please sign in to comment.