1
0
mirror of https://github.com/MariaDB/server.git synced 2025-12-24 11:21:21 +03:00

Fix for bug #50913 "Deadlock between open_and_lock_tables_derived

and MDL".

Concurrent execution of a multi-DELETE statement and ALTER
TABLE statement which affected one of the tables used in
the multi-DELETE sometimes led to deadlock.
Similar deadlocks might have occured when one performed
INSERT/UPDATE/DELETE on a view and concurrently executed
ALTER TABLE for the view's underlying table, or when one
concurrently executed TRUNCATE TABLE for InnoDB table and
ALTER TABLE for the same table.

These deadlocks were caused by a discrepancy between types of
metadata and thr_lock.cc locks acquired by those statements.

What happened was that multi-DELETE/TRUNCATE/DML-through-the-
view statement in the first connection acquired SR lock on a
table, then ALTER TABLE would come in in the second connection
and acquire SNW metadata lock and TL_WRITE_ALLOW_READ
thr_lock.c lock and then would start waiting for the first
connection during lock upgrade. After that the statement in
the first connection would try to acquire TL_WRITE lock on
table and would start waiting for the second connection,
creating a deadlock.

This patch solves this problem by ensuring that we acquire
SW metadata lock in all cases in which we acquiring write
thr_lock.c lock. This guarantees that deadlocks like the
one described above won't occur since all lock conflicts
in such situation are resolved within MDL subsystem.

This patch also adds assert which should guarantee that
such situations won't arise in future.
This commit is contained in:
Dmitry Lenev
2010-02-08 23:19:55 +03:00
parent f750b5f160
commit c7e7a7d20c
13 changed files with 256 additions and 3 deletions

View File

@@ -800,5 +800,82 @@ DROP TABLE t1;
DROP VIEW v1;
--echo #
--echo # Test for bug #50913 "Deadlock between open_and_lock_tables_derived
--echo # and MDL". Also see additional coverage in mdl_sync.test.
--echo #
--disable_warnings
drop table if exists t1;
drop view if exists v1;
--enable_warnings
connect (con50913,localhost,root);
connection default;
create table t1 (i int);
create view v1 as select i from t1;
begin;
select * from t1;
--echo # Switching to connection 'con50913'.
connection con50913;
--echo # Sending:
--send alter table t1 add column j int
--echo # Switching to connection 'default'.
connection default;
--echo # Wait until ALTER TABLE gets blocked.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "alter table t1 add column j int";
--source include/wait_condition.inc
--echo # The below statement should try to acquire SW lock on 't1'
--echo # and therefore should get ER_LOCK_DEADLOCK error. Before
--echo # bug fix it acquired SR lock and hung on thr_lock.c lock.
--error ER_LOCK_DEADLOCK
delete a from t1 as a where i = 1;
--echo # Unblock ALTER TABLE.
commit;
--echo # Switching to connection 'con50913'.
connection con50913;
--echo # Reaping ALTER TABLE;
--reap
--echo # Switching to connection 'default'.
connection default;
begin;
select * from v1;
--echo # Switching to connection 'con50913'.
connection con50913;
--echo # Sending:
--send alter table t1 drop column j
--echo # Switching to connection 'default'.
connection default;
--echo # Wait until ALTER TABLE gets blocked.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "alter table t1 drop column j";
--source include/wait_condition.inc
--echo # The below statement should try to acquire SW lock on 't1'
--echo # and therefore should get ER_LOCK_DEADLOCK error. Before
--echo # bug fix it acquired SR lock and hung on thr_lock.c lock.
--error ER_LOCK_DEADLOCK
insert into v1 values (1);
--echo # Unblock ALTER TABLE.
commit;
--echo # Switching to connection 'con50913'.
connection con50913;
--echo # Reaping ALTER TABLE;
--reap
--echo # Switching to connection 'default'.
connection default;
disconnect con50913;
drop view v1;
drop table t1;
# Wait till all disconnects are completed
--source include/wait_until_count_sessions.inc