mirror of
https://github.com/MariaDB/server.git
synced 2025-01-20 14:02:32 +01:00
afd15c43a9
Add a wait-for graph based deadlock detector to the MDL subsystem. Fixes bug #46272 "MySQL 5.4.4, new MDL: unnecessary deadlock" and bug #37346 "innodb does not detect deadlock between update and alter table". The first bug manifested itself as an unwarranted abort of a transaction with ER_LOCK_DEADLOCK error by a concurrent ALTER statement, when this transaction tried to repeat use of a table, which it has already used in a similar fashion before ALTER started. The second bug showed up as a deadlock between table-level locks and InnoDB row locks, which was "detected" only after innodb_lock_wait_timeout timeout. A transaction would start using the table and modify a few rows. Then ALTER TABLE would come in, and start copying rows into a temporary table. Eventually it would stumble on the modified records and get blocked on a row lock. The first transaction would try to do more updates, and get blocked on thr_lock.c lock. This situation of circular wait would only get resolved by a timeout. Both these bugs stemmed from inadequate solutions to the problem of deadlocks occurring between different locking subsystems. In the first case we tried to avoid deadlocks between metadata locking and table-level locking subsystems, when upgrading shared metadata lock to exclusive one. Transactions holding the shared lock on the table and waiting for some table-level lock used to be aborted too aggressively. We also allowed ALTER TABLE to start in presence of transactions that modify the subject table. ALTER TABLE acquires TL_WRITE_ALLOW_READ lock at start, and that block all writes against the table (naturally, we don't want any writes to be lost when switching the old and the new table). TL_WRITE_ALLOW_READ lock, in turn, would block the started transaction on thr_lock.c lock, should they do more updates. This, again, lead to the need to abort such transactions. The second bug occurred simply because we didn't have any mechanism to detect deadlocks between the table-level locks in thr_lock.c and row-level locks in InnoDB, other than innodb_lock_wait_timeout. This patch solves both these problems by moving lock conflicts which are causing these deadlocks into the metadata locking subsystem, thus making it possible to avoid or detect such deadlocks inside MDL. To do this we introduce new type-of-operation-aware metadata locks, which allow MDL subsystem to know not only the fact that transaction has used or is going to use some object but also what kind of operation it has carried out or going to carry out on the object. This, along with the addition of a special kind of upgradable metadata lock, allows ALTER TABLE to wait until all transactions which has updated the table to go away. This solves the second issue. Another special type of upgradable metadata lock is acquired by LOCK TABLE WRITE. This second lock type allows to solve the first issue, since abortion of table-level locks in event of DDL under LOCK TABLES becomes also unnecessary. Below follows the list of incompatible changes introduced by this patch: - From now on, ALTER TABLE and CREATE/DROP TRIGGER SQL (i.e. those statements that acquire TL_WRITE_ALLOW_READ lock) wait for all transactions which has *updated* the table to complete. - From now on, LOCK TABLES ... WRITE, REPAIR/OPTIMIZE TABLE (i.e. all statements which acquire TL_WRITE table-level lock) wait for all transaction which *updated or read* from the table to complete. As a consequence, innodb_table_locks=0 option no longer applies to LOCK TABLES ... WRITE. - DROP DATABASE, DROP TABLE, RENAME TABLE no longer abort statements or transactions which use tables being dropped or renamed, and instead wait for these transactions to complete. - Since LOCK TABLES WRITE now takes a special metadata lock, not compatible with with reads or writes against the subject table and transaction-wide, thr_lock.c deadlock avoidance algorithm that used to ensure absence of deadlocks between LOCK TABLES WRITE and other statements is no longer sufficient, even for MyISAM. The wait-for graph based deadlock detector of MDL subsystem may sometimes be necessary and is involved. This may lead to ER_LOCK_DEADLOCK error produced for multi-statement transactions even if these only use MyISAM: session 1: session 2: begin; update t1 ... lock table t2 write, t1 write; -- gets a lock on t2, blocks on t1 update t2 ... (ER_LOCK_DEADLOCK) - Finally, support of LOW_PRIORITY option for LOCK TABLES ... WRITE was abandoned. LOCK TABLE ... LOW_PRIORITY WRITE from now on has the same priority as the usual LOCK TABLE ... WRITE. SELECT HIGH PRIORITY no longer trumps LOCK TABLE ... WRITE in the wait queue. - We do not take upgradable metadata locks on implicitly locked tables. So if one has, say, a view v1 that uses table t1, and issues: LOCK TABLE v1 WRITE; FLUSH TABLE t1; -- (or just 'FLUSH TABLES'), an error is produced. In order to be able to perform DDL on a table under LOCK TABLES, the table must be locked explicitly in the LOCK TABLES list.
164 lines
4 KiB
Text
164 lines
4 KiB
Text
#
|
|
# Code coverage testing of TRUNCATE TABLE.
|
|
#
|
|
# Ingo Struewing, 2009-07-20
|
|
#
|
|
|
|
--source include/have_debug_sync.inc
|
|
SET DEBUG_SYNC='RESET';
|
|
|
|
--let $MYSQLD_DATADIR= `SELECT @@datadir`
|
|
|
|
--disable_warnings
|
|
DROP TABLE IF EXISTS t1;
|
|
--enable_warnings
|
|
|
|
--echo #
|
|
--echo # Bug#20667 - Truncate table fails for a write locked table
|
|
--echo #
|
|
########
|
|
# Attack wait_while_table_is_used(). Kill query while trying to
|
|
# upgrade MDL.
|
|
#
|
|
CREATE TABLE t1 (c1 INT);
|
|
INSERT INTO t1 VALUES (1);
|
|
#
|
|
# Acquire a shared metadata lock on table by opening HANDLER for it and wait.
|
|
# TRUNCATE shall block on this metadata lock.
|
|
# We can't use normal DML as such statements would also block LOCK TABLES.
|
|
#
|
|
--echo #
|
|
--echo # connection con1
|
|
--connect (con1, localhost, root,,)
|
|
HANDLER t1 OPEN;
|
|
#
|
|
# Get connection id of default connection.
|
|
# Lock the table and start TRUNCATE, which will block on MDL upgrade.
|
|
#
|
|
--echo #
|
|
--echo # connection default
|
|
--connection default
|
|
let $ID= `SELECT @id := CONNECTION_ID()`;
|
|
LOCK TABLE t1 WRITE;
|
|
SET DEBUG_SYNC='mdl_upgrade_shared_lock_to_exclusive SIGNAL waiting';
|
|
send TRUNCATE TABLE t1;
|
|
#
|
|
# Get the default connection ID into a variable in an invisible statement.
|
|
# Kill the TRUNCATE query. This shall result in an error return
|
|
# from wait_while_table_is_used().
|
|
#
|
|
--echo #
|
|
--echo # connection con2
|
|
--connect (con2, localhost, root,,)
|
|
SET DEBUG_SYNC='now WAIT_FOR waiting';
|
|
let $invisible_assignment_in_select = `SELECT @id := $ID`;
|
|
KILL QUERY @id;
|
|
--disconnect con2
|
|
--echo #
|
|
--echo # connection con1
|
|
--connection con1
|
|
--echo # Release shared metadata lock by closing HANDLER.
|
|
HANDLER t1 CLOSE;
|
|
--disconnect con1
|
|
--echo #
|
|
--echo # connection default
|
|
--connection default
|
|
--error ER_QUERY_INTERRUPTED
|
|
reap;
|
|
UNLOCK TABLES;
|
|
DROP TABLE t1;
|
|
SET DEBUG_SYNC='RESET';
|
|
########
|
|
# Attack reopen_tables(). Remove form file.
|
|
#
|
|
CREATE TABLE t1 (c1 INT);
|
|
INSERT INTO t1 VALUES (1);
|
|
#
|
|
# Acquire a shared metadata lock on table by opening HANDLER for it and wait.
|
|
# TRUNCATE shall block on this metadata lock.
|
|
# We can't use normal DML as such statements would also block LOCK TABLES.
|
|
#
|
|
--echo #
|
|
--echo # connection con1
|
|
--connect (con1, localhost, root,,)
|
|
HANDLER t1 OPEN;
|
|
#
|
|
# Lock the table and start TRUNCATE, which will block on MDL upgrade.
|
|
#
|
|
--echo #
|
|
--echo # connection default
|
|
--connection default
|
|
LOCK TABLE t1 WRITE;
|
|
SET DEBUG_SYNC='mdl_upgrade_shared_lock_to_exclusive SIGNAL waiting';
|
|
send TRUNCATE TABLE t1;
|
|
#
|
|
# Remove datafile.
|
|
# Commit to let TRUNCATE continue.
|
|
#
|
|
--echo #
|
|
--echo # connection con2
|
|
--connect (con2, localhost, root,,)
|
|
SET DEBUG_SYNC='now WAIT_FOR waiting';
|
|
--remove_file $MYSQLD_DATADIR/test/t1.frm
|
|
--disconnect con2
|
|
--echo #
|
|
--echo # connection con1
|
|
--connection con1
|
|
HANDLER t1 CLOSE;
|
|
--disconnect con1
|
|
--echo #
|
|
--echo # connection default
|
|
--connection default
|
|
--error ER_NO_SUCH_TABLE
|
|
reap;
|
|
UNLOCK TABLES;
|
|
--error ER_BAD_TABLE_ERROR
|
|
DROP TABLE t1;
|
|
SET DEBUG_SYNC='RESET';
|
|
########
|
|
# Attack acquire_exclusive_locks(). Hold a global read lock.
|
|
# Non-LOCK TABLE case.
|
|
#
|
|
CREATE TABLE t1 (c1 INT);
|
|
INSERT INTO t1 VALUES (1);
|
|
#
|
|
# Start a transaction and execute a DML in it. Since 5.4.4 this leaves
|
|
# a shared meta data lock (MDL) behind. TRUNCATE shall block on it.
|
|
#
|
|
--echo #
|
|
--echo # connection con1
|
|
--connect (con1, localhost, root,,)
|
|
START TRANSACTION;
|
|
INSERT INTO t1 VALUES (2);
|
|
#
|
|
# Get connection id of default connection.
|
|
# Start TRUNCATE, which will block on acquire_exclusive_locks().
|
|
#
|
|
--echo #
|
|
--echo # connection default
|
|
--connection default
|
|
let $ID= `SELECT @id := CONNECTION_ID()`;
|
|
SET DEBUG_SYNC='mdl_acquire_lock_wait SIGNAL waiting';
|
|
send TRUNCATE TABLE t1;
|
|
#
|
|
# Get the default connection ID into a variable in an invisible statement.
|
|
# Kill the TRUNCATE query. This shall result in an error return
|
|
# from wait_while_table_is_used().
|
|
#
|
|
--echo #
|
|
--echo # connection con1
|
|
--connection con1
|
|
SET DEBUG_SYNC='now WAIT_FOR waiting';
|
|
let $invisible_assignment_in_select = `SELECT @id := $ID`;
|
|
KILL QUERY @id;
|
|
COMMIT;
|
|
--disconnect con1
|
|
--echo #
|
|
--echo # connection default
|
|
--connection default
|
|
--error ER_QUERY_INTERRUPTED
|
|
reap;
|
|
UNLOCK TABLES;
|
|
DROP TABLE t1;
|
|
SET DEBUG_SYNC='RESET';
|
|
|