mirror of
https://github.com/MariaDB/server.git
synced 2025-01-22 14:54:20 +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.
304 lines
7 KiB
Text
304 lines
7 KiB
Text
drop table if exists t1,t2;
|
|
create table t1(n int);
|
|
insert into t1 values (1);
|
|
select get_lock("mysqltest_lock", 100);
|
|
get_lock("mysqltest_lock", 100)
|
|
1
|
|
update t1 set n = 2 and get_lock('mysqltest_lock', 100);
|
|
update low_priority t1 set n = 4;
|
|
select n from t1;
|
|
select release_lock("mysqltest_lock");
|
|
release_lock("mysqltest_lock")
|
|
1
|
|
select release_lock("mysqltest_lock");
|
|
release_lock("mysqltest_lock")
|
|
1
|
|
n
|
|
4
|
|
drop table t1;
|
|
create table t1(n int);
|
|
insert into t1 values (1);
|
|
select get_lock("mysqltest_lock", 100);
|
|
get_lock("mysqltest_lock", 100)
|
|
1
|
|
select n from t1 where get_lock('mysqltest_lock', 100);
|
|
update low_priority t1 set n = 4;
|
|
select n from t1;
|
|
n
|
|
1
|
|
select release_lock("mysqltest_lock");
|
|
release_lock("mysqltest_lock")
|
|
1
|
|
n
|
|
1
|
|
select release_lock("mysqltest_lock");
|
|
release_lock("mysqltest_lock")
|
|
1
|
|
drop table t1;
|
|
create table t1 (a int, b int);
|
|
create table t2 (c int, d int);
|
|
insert into t1 values(1,1);
|
|
insert into t1 values(2,2);
|
|
insert into t2 values(1,2);
|
|
lock table t1 read;
|
|
update t1,t2 set c=a where b=d;
|
|
select c from t2;
|
|
c
|
|
2
|
|
unlock tables;
|
|
drop table t1;
|
|
drop table t2;
|
|
create table t1 (a int);
|
|
create table t2 (a int);
|
|
lock table t1 write, t2 write;
|
|
insert t1 select * from t2;
|
|
drop table t2;
|
|
unlock tables;
|
|
ERROR 42S02: Table 'test.t2' doesn't exist
|
|
drop table t1;
|
|
create table t1 (a int);
|
|
create table t2 (a int);
|
|
lock table t1 write, t2 write, t1 as t1_2 write, t2 as t2_2 write;
|
|
insert t1 select * from t2;
|
|
drop table t2;
|
|
unlock tables;
|
|
ERROR 42S02: Table 'test.t2' doesn't exist
|
|
drop table t1;
|
|
End of 4.1 tests
|
|
create table t1(a int);
|
|
lock tables t1 write;
|
|
show columns from t1;
|
|
Field Type Null Key Default Extra
|
|
a int(11) YES NULL
|
|
unlock tables;
|
|
drop table t1;
|
|
USE mysql;
|
|
LOCK TABLES columns_priv WRITE, db WRITE, host WRITE, user WRITE;
|
|
FLUSH TABLES;
|
|
USE mysql;
|
|
SELECT user.Select_priv FROM user, db WHERE user.user = db.user LIMIT 1;
|
|
OPTIMIZE TABLES columns_priv, db, host, user;
|
|
Table Op Msg_type Msg_text
|
|
mysql.columns_priv optimize status OK
|
|
mysql.db optimize status OK
|
|
mysql.host optimize status OK
|
|
mysql.user optimize status OK
|
|
UNLOCK TABLES;
|
|
Select_priv
|
|
N
|
|
USE test;
|
|
use test;
|
|
CREATE TABLE t1 (c1 int);
|
|
LOCK TABLE t1 WRITE;
|
|
FLUSH TABLES WITH READ LOCK;
|
|
CREATE TABLE t2 (c1 int);
|
|
ERROR HY000: Table 't2' was not locked with LOCK TABLES
|
|
UNLOCK TABLES;
|
|
UNLOCK TABLES;
|
|
DROP TABLE t1;
|
|
CREATE TABLE t1 (c1 int);
|
|
LOCK TABLE t1 WRITE;
|
|
FLUSH TABLES WITH READ LOCK;
|
|
CREATE TABLE t2 AS SELECT * FROM t1;
|
|
ERROR HY000: Table 't2' was not locked with LOCK TABLES
|
|
UNLOCK TABLES;
|
|
UNLOCK TABLES;
|
|
DROP TABLE t1;
|
|
CREATE DATABASE mysqltest_1;
|
|
FLUSH TABLES WITH READ LOCK;
|
|
DROP DATABASE mysqltest_1;
|
|
DROP DATABASE mysqltest_1;
|
|
ERROR HY000: Can't execute the query because you have a conflicting read lock
|
|
UNLOCK TABLES;
|
|
DROP DATABASE mysqltest_1;
|
|
ERROR HY000: Can't drop database 'mysqltest_1'; database doesn't exist
|
|
create table t1 (f1 int(12) unsigned not null auto_increment, primary key(f1)) engine=innodb;
|
|
lock tables t1 write;
|
|
alter table t1 auto_increment=0;
|
|
alter table t1 auto_increment=0;
|
|
unlock tables;
|
|
drop table t1;
|
|
create table t1 (a int);
|
|
create table t2 like t1;
|
|
# con1
|
|
lock tables t1 write;
|
|
# con2
|
|
flush tables with read lock;
|
|
# con5
|
|
# global read lock is taken
|
|
# con3
|
|
select * from t2 for update;
|
|
# waiting for release of read lock
|
|
# con4
|
|
# would hang and later cause a deadlock
|
|
flush tables t2;
|
|
# clean up
|
|
unlock tables;
|
|
unlock tables;
|
|
a
|
|
drop table t1,t2;
|
|
#
|
|
# Lightweight version:
|
|
# Ensure that the wait for a GRL is done before opening tables.
|
|
#
|
|
create table t1 (a int);
|
|
create table t2 like t1;
|
|
#
|
|
# UPDATE
|
|
#
|
|
# default
|
|
flush tables with read lock;
|
|
# con1
|
|
update t2 set a = 1;
|
|
# default
|
|
# statement is waiting for release of read lock
|
|
# con2
|
|
flush table t2;
|
|
# default
|
|
unlock tables;
|
|
# con1
|
|
#
|
|
# LOCK TABLES .. WRITE
|
|
#
|
|
# default
|
|
flush tables with read lock;
|
|
# con1
|
|
lock tables t2 write;
|
|
# default
|
|
# statement is waiting for release of read lock
|
|
# con2
|
|
flush table t2;
|
|
# default
|
|
unlock tables;
|
|
# con1
|
|
unlock tables;
|
|
drop table t1,t2;
|
|
End of 5.0 tests
|
|
create table t1 (i int);
|
|
lock table t1 read;
|
|
update t1 set i= 10;
|
|
select * from t1;
|
|
kill query ID;
|
|
i
|
|
ERROR 70100: Query execution was interrupted
|
|
unlock tables;
|
|
drop table t1;
|
|
drop table if exists t1;
|
|
create table t1 (a int) ENGINE=MEMORY;
|
|
--> client 2
|
|
handler t1 open;
|
|
ERROR HY000: Table storage engine for 't1' doesn't have this option
|
|
--> client 1
|
|
drop table t1;
|
|
drop table if exists t1;
|
|
create table t1 (i int);
|
|
connection: default
|
|
lock tables t1 write;
|
|
connection: flush
|
|
flush tables with read lock;;
|
|
connection: default
|
|
alter table t1 add column j int;
|
|
connection: insert
|
|
insert into t1 values (1,2);;
|
|
connection: default
|
|
unlock tables;
|
|
connection: flush
|
|
select * from t1;
|
|
i j
|
|
unlock tables;
|
|
select * from t1;
|
|
i j
|
|
1 2
|
|
drop table t1;
|
|
drop table if exists t1;
|
|
create table t1 (i int);
|
|
connection: default
|
|
lock tables t1 write;
|
|
connection: flush
|
|
flush tables with read lock;;
|
|
connection: default
|
|
flush tables;
|
|
unlock tables;
|
|
drop table t1;
|
|
drop table if exists t1,t2;
|
|
create table t1 (a int);
|
|
flush status;
|
|
lock tables t1 read;
|
|
insert into t1 values(1);
|
|
unlock tables;
|
|
drop table t1;
|
|
select @tlwa < @tlwb;
|
|
@tlwa < @tlwb
|
|
1
|
|
End of 5.1 tests
|
|
drop table if exists t1;
|
|
create table t1 (i int);
|
|
connection: default
|
|
lock tables t1 write;
|
|
connection: flush
|
|
flush tables with read lock;;
|
|
connection: default
|
|
flush tables;
|
|
drop table t1;
|
|
#
|
|
# Test for bug #46272 "MySQL 5.4.4, new MDL: unnecessary deadlock".
|
|
#
|
|
drop table if exists t1;
|
|
create table t1 (c1 int primary key, c2 int, c3 int);
|
|
insert into t1 values (1,1,0),(2,2,0),(3,3,0),(4,4,0),(5,5,0);
|
|
begin;
|
|
update t1 set c3=c3+1 where c2=3;
|
|
#
|
|
# Switching to connection 'con46272'.
|
|
# The below ALTER TABLE statement should wait till transaction
|
|
# in connection 'default' is complete and then succeed.
|
|
# It should not deadlock or fail with ER_LOCK_DEADLOCK error.
|
|
# Sending:
|
|
alter table t1 add column c4 int;;
|
|
#
|
|
# Switching to connection 'default'.
|
|
# Wait until the above ALTER TABLE gets blocked because this
|
|
# connection holds SW metadata lock on table to be altered.
|
|
# The below statement should succeed. It should not
|
|
# deadlock or end with ER_LOCK_DEADLOCK error.
|
|
update t1 set c3=c3+1 where c2=4;
|
|
# Unblock ALTER TABLE by committing transaction.
|
|
commit;
|
|
#
|
|
# Switching to connection 'con46272'.
|
|
# Reaping ALTER TABLE.
|
|
#
|
|
# Switching to connection 'default'.
|
|
drop table t1;
|
|
#
|
|
# Bug#47249 assert in MDL_global_lock::is_lock_type_compatible
|
|
#
|
|
DROP TABLE IF EXISTS t1;
|
|
DROP VIEW IF EXISTS v1;
|
|
#
|
|
# Test 1: LOCK TABLES v1 WRITE, t1 READ;
|
|
#
|
|
# Thanks to the fact that we no longer allow DDL on tables
|
|
# which are locked for write implicitly, the exact scenario
|
|
# in which assert was failing is no longer repeatable.
|
|
CREATE TABLE t1 ( f1 integer );
|
|
CREATE VIEW v1 AS SELECT f1 FROM t1 ;
|
|
LOCK TABLES v1 WRITE, t1 READ;
|
|
FLUSH TABLE t1;
|
|
ERROR HY000: Table 't1' was locked with a READ lock and can't be updated
|
|
UNLOCK TABLES;
|
|
DROP TABLE t1;
|
|
DROP VIEW v1;
|
|
#
|
|
# Test 2: LOCK TABLES t1 WRITE, v1 READ;
|
|
#
|
|
CREATE TABLE t1 ( f1 integer );
|
|
CREATE VIEW v1 AS SELECT f1 FROM t1 ;
|
|
# Connection 2
|
|
LOCK TABLES t1 WRITE, v1 READ;
|
|
FLUSH TABLE t1;
|
|
# Connection 1
|
|
LOCK TABLES t1 WRITE;
|
|
FLUSH TABLE t1;
|
|
DROP TABLE t1;
|
|
DROP VIEW v1;
|