Commit 236539b4 authored by Dmitry Lenev's avatar Dmitry Lenev

Implementation of simple deadlock detection for metadata locks.

This change is supposed to reduce number of ER_LOCK_DEADLOCK
errors which occur when multi-statement transaction encounters
conflicting metadata lock in cases when waiting is possible.

The idea is not to fail ER_LOCK_DEADLOCK error immediately when
we encounter conflicting metadata lock. Instead we release all
metadata locks acquired by current statement and start to wait
until conflicting lock go away. To avoid deadlocks we use simple
empiric which aborts waiting with ER_LOCK_DEADLOCK error if it
turns out that somebody is waiting for metadata locks owned by
this transaction.

This patch also fixes bug #46273 "MySQL 5.4.4 new MDL: Bug#989
is not fully fixed in case of ALTER".

The bug was that concurrent execution of UPDATE or MULTI-UPDATE
statement as a part of multi-statement transaction that already
has used table being updated and ALTER TABLE statement might have
resulted of loss of isolation between this transaction and ALTER
TABLE statement, which manifested itself as changes performed by
ALTER TABLE becoming visible in transaction and wrong binary log
order as a consequence.

This problem occurred when UPDATE or MULTI-UPDATE's wait in
mysql_lock_tables() call was aborted due to metadata lock
upgrade performed by concurrent ALTER TABLE. After such abort all
metadata locks held by transaction were released but transaction
silently continued to be executed as if nothing has happened.

We solve this problem by changing our code not to release all
locks in such case. Instead we release only locks which were
acquired by current statement and then try to reacquire them
by restarting open/lock tables process. We piggyback on simple
deadlock detector implementation since this change has to be
done anyway for it.
parent cd6fbffc
......@@ -732,7 +732,7 @@ connection default;
--disable_warnings
drop table if exists t1;
--enable_warnings
create table t1 (a int);
create table t1 (a int, key a (a));
insert into t1 values (1);
handler t1 open;
connection con1;
......@@ -743,7 +743,6 @@ let $wait_condition=
where state = "Waiting for table" and info = "alter table t1 engine=memory";
--source include/wait_condition.inc
connection default;
--error ER_ILLEGAL_HA
handler t1 read a next;
handler t1 close;
connection con1;
......@@ -983,11 +982,12 @@ lock table t2 read;
--echo # --> connection con2
connection con2;
--echo # Sending:
--send drop table t2
send rename table t2 to t3, t1 to t2, t3 to t1;
--echo # --> connection con1
connection con1;
--echo # Waiting for 'drop table t2' to get blocked...
let $wait_condition=select count(*)=1 from information_schema.processlist where state='Waiting for table' and info='drop table t2';
--echo # Waiting for 'rename table ...' to get blocked...
let $wait_condition=select count(*)=1 from information_schema.processlist
where state='Waiting for table' and info='rename table t2 to t3, t1 to t2, t3 to t1';
--source include/wait_condition.inc
--echo # --> connection default
connection default;
......@@ -997,25 +997,26 @@ handler t2 open;
select * from t2;
handler t1 open;
commit;
--error ER_LOCK_DEADLOCK
handler t2 open;
handler t1 close;
--echo # --> connection con1
connection con1;
unlock tables;
--echo # --> connection con2
connection con2;
--echo # Reaping 'drop table t2'...
--echo # Reaping 'rename table ...'...
--reap
--echo # --> connection default
connection default;
handler t1 open;
handler t1 read a prev;
handler t1 close;
drop table t2;
--echo #
--echo # Likewise, this doesn't require a multi-statement transaction.
--echo # ER_LOCK_DEADLOCK is also produced when we have an open
--echo # HANDLER and try to acquire locks for a single statement.
--echo # Originally there was a deadlock error in this test.
--echo # With implementation of deadlock detector
--echo # we no longer deadlock, but block and wait on a lock.
--echo # The HANDLER is auto-closed as soon as the connection
--echo # sees a pending conflicting lock against it.
--echo #
create table t2 (a int, key a (a));
handler t1 open;
......@@ -1033,10 +1034,12 @@ let $wait_condition=select count(*)=1 from information_schema.processlist where
--source include/wait_condition.inc
--echo # --> connection default
connection default;
--error ER_LOCK_DEADLOCK
select * from t2;
--echo # Sending 'select * from t2'
send select * from t2;
--echo # --> connection con1
connection con1;
--echo # Waiting for 'select * from t2' to get blocked...
let $wait_condition=select count(*)=1 from information_schema.processlist where state='Waiting for table' and info='select * from t2';
unlock tables;
--echo # --> connection con2
connection con2;
......@@ -1044,6 +1047,9 @@ connection con2;
--reap
--echo # --> connection default
connection default;
--echo # Reaping 'select * from t2'
--error ER_NO_SUCH_TABLE
reap;
handler t1 close;
--echo #
......
......@@ -745,12 +745,13 @@ drop table t1;
handler t1 read a next;
ERROR 42S02: Unknown table 't1' in HANDLER
drop table if exists t1;
create table t1 (a int);
create table t1 (a int, key a (a));
insert into t1 values (1);
handler t1 open;
alter table t1 engine=memory;
handler t1 read a next;
ERROR HY000: Table storage engine for 't1' doesn't have this option
a
1
handler t1 close;
drop table t1;
USE information_schema;
......@@ -1002,9 +1003,9 @@ a
lock table t2 read;
# --> connection con2
# Sending:
drop table t2;
rename table t2 to t3, t1 to t2, t3 to t1;
# --> connection con1
# Waiting for 'drop table t2' to get blocked...
# Waiting for 'rename table ...' to get blocked...
# --> connection default
handler t2 open;
ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
......@@ -1012,23 +1013,24 @@ select * from t2;
ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
handler t1 open;
commit;
handler t2 open;
ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
handler t1 close;
# --> connection con1
unlock tables;
# --> connection con2
# Reaping 'drop table t2'...
# Reaping 'rename table ...'...
# --> connection default
handler t1 open;
handler t1 read a prev;
a
5
handler t1 close;
drop table t2;
#
# Likewise, this doesn't require a multi-statement transaction.
# ER_LOCK_DEADLOCK is also produced when we have an open
# HANDLER and try to acquire locks for a single statement.
# Originally there was a deadlock error in this test.
# With implementation of deadlock detector
# we no longer deadlock, but block and wait on a lock.
# The HANDLER is auto-closed as soon as the connection
# sees a pending conflicting lock against it.
#
create table t2 (a int, key a (a));
handler t1 open;
......@@ -1040,13 +1042,16 @@ drop table t2;
# --> connection con1
# Waiting for 'drop table t2' to get blocked...
# --> connection default
# Sending 'select * from t2'
select * from t2;
ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
# --> connection con1
# Waiting for 'select * from t2' to get blocked...
unlock tables;
# --> connection con2
# Reaping 'drop table t2'...
# --> connection default
# Reaping 'select * from t2'
ERROR 42S02: Table 'test.t2' doesn't exist
handler t1 close;
#
# ROLLBACK TO SAVEPOINT releases transactional locks,
......
......@@ -743,12 +743,13 @@ drop table t1;
handler t1 read a next;
ERROR 42S02: Unknown table 't1' in HANDLER
drop table if exists t1;
create table t1 (a int);
create table t1 (a int, key a (a));
insert into t1 values (1);
handler t1 open;
alter table t1 engine=memory;
handler t1 read a next;
ERROR HY000: Table storage engine for 't1' doesn't have this option
a
1
handler t1 close;
drop table t1;
USE information_schema;
......@@ -999,9 +1000,9 @@ a
lock table t2 read;
# --> connection con2
# Sending:
drop table t2;
rename table t2 to t3, t1 to t2, t3 to t1;
# --> connection con1
# Waiting for 'drop table t2' to get blocked...
# Waiting for 'rename table ...' to get blocked...
# --> connection default
handler t2 open;
ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
......@@ -1009,23 +1010,24 @@ select * from t2;
ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
handler t1 open;
commit;
handler t2 open;
ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
handler t1 close;
# --> connection con1
unlock tables;
# --> connection con2
# Reaping 'drop table t2'...
# Reaping 'rename table ...'...
# --> connection default
handler t1 open;
handler t1 read a prev;
a
5
handler t1 close;
drop table t2;
#
# Likewise, this doesn't require a multi-statement transaction.
# ER_LOCK_DEADLOCK is also produced when we have an open
# HANDLER and try to acquire locks for a single statement.
# Originally there was a deadlock error in this test.
# With implementation of deadlock detector
# we no longer deadlock, but block and wait on a lock.
# The HANDLER is auto-closed as soon as the connection
# sees a pending conflicting lock against it.
#
create table t2 (a int, key a (a));
handler t1 open;
......@@ -1037,13 +1039,16 @@ drop table t2;
# --> connection con1
# Waiting for 'drop table t2' to get blocked...
# --> connection default
# Sending 'select * from t2'
select * from t2;
ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
# --> connection con1
# Waiting for 'select * from t2' to get blocked...
unlock tables;
# --> connection con2
# Reaping 'drop table t2'...
# --> connection default
# Reaping 'select * from t2'
ERROR 42S02: Table 'test.t2' doesn't exist
handler t1 close;
#
# ROLLBACK TO SAVEPOINT releases transactional locks,
......
......@@ -20,6 +20,220 @@ ERROR 42S02: Unknown table 't1'
drop table t3;
SET DEBUG_SYNC= 'RESET';
#
# Test coverage for basic deadlock detection in metadata
# locking subsystem.
#
drop tables if exists t1, t2, t3, t4;
create table t1 (i int);
create table t2 (j int);
create table t3 (k int);
create table t4 (k int);
#
# Test for the case in which no deadlock occurs.
#
#
# Switching to connection 'deadlock_con1'.
begin;
insert into t1 values (1);
#
# Switching to connection 'deadlock_con2'.
begin;
insert into t2 values (1);
#
# Switching to connection 'default'.
# Send:
rename table t2 to t0, t3 to t2, t0 to t3;;
#
# Switching to connection 'deadlock_con1'.
# Wait until the above RENAME TABLE is blocked because it has to wait
# for 'deadlock_con2' which holds shared metadata lock on 't2'.
# The below statement should wait for exclusive metadata lock
# on 't2' to go away and should not produce ER_LOCK_DEADLOCK
# as no deadlock is possible in this situation.
# Send:
select * from t2;;
#
# Switching to connection 'deadlock_con2'.
# Wait until the above SELECT * FROM t2 is starts waiting
# for an exclusive metadata lock to go away.
#
# Unblock RENAME TABLE by releasing shared metadata lock on t2.
commit;
#
# Switching to connection 'default'.
# Reap RENAME TABLE.
#
# Switching to connection 'deadlock_con1'.
# Reap SELECT.
k
#
# Switching to connection 'default'.
#
# Let us check that in the process of waiting for conflicting lock
# on table 't2' to go away transaction in connection 'deadlock_con1'
# has not released metadata lock on table 't1'.
# Send:
rename table t1 to t0, t3 to t1, t0 to t3;;
#
# Switching to connection 'deadlock_con1'.
# Wait until the above RENAME TABLE is blocked because it has to wait
# for 'deadlock_con1' which should still hold shared metadata lock on
# table 't1'.
# Commit transaction to unblock RENAME TABLE.
commit;
#
# Switching to connection 'default'.
# Reap RENAME TABLE.
#
# Test for case when deadlock occurs and should be detected immediately.
#
#
# Switching to connection 'deadlock_con1'.
begin;
insert into t1 values (2);
#
# Switching to connection 'default'.
# Send:
rename table t2 to t0, t1 to t2, t0 to t1;;
#
# Switching to connection 'deadlock_con1'.
# Wait until the above RENAME TABLE is blocked because it has to wait
# for 'deadlock_con1' which holds shared metadata lock on 't1'.
#
# The below statement should not wait as doing so will cause deadlock.
# Instead it should fail and emit ER_LOCK_DEADLOCK statement.
select * from t2;
ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
#
# Let us check that failure of the above statement has not released
# metadata lock on table 't1', i.e. that RENAME TABLE is still blocked.
# Commit transaction to unblock RENAME TABLE.
commit;
#
# Switching to connection 'default'.
# Reap RENAME TABLE.
#
# Test for the case in which deadlock also occurs but not immediately.
#
#
# Switching to connection 'deadlock_con1'.
begin;
insert into t1 values (1);
#
# Switching to connection 'deadlock_con2'.
begin;
insert into t3 values (1);
#
# Switching to connection 'default'.
# Send:
rename table t2 to t0, t3 to t2, t0 to t3;;
#
# Switching to connection 'deadlock_con1'.
# Wait until the above RENAME TABLE is blocked because it has to wait
# for 'deadlock_con2' which holds shared metadata lock on 't3'.
# The below SELECT statement should wait for metadata lock
# on table 't2' and should not produce ER_LOCK_DEADLOCK
# immediately as no deadlock is possible at the moment.
select * from t2;;
#
# Switching to connection 'deadlock_con3'.
# Wait until the above SELECT * FROM t2 is starts waiting
# for an exclusive metadata lock to go away.
# Send RENAME TABLE statement that will deadlock with the
# SELECT statement and thus should abort the latter.
rename table t1 to t0, t2 to t1, t0 to t2;;
#
# Switching to connection 'deadlock_con1'.
# Since the latest RENAME TABLE entered in deadlock with SELECT
# statement the latter should be aborted and emit ER_LOCK_DEADLOCK
# error.
# Reap SELECT * FROM t2.
ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
#
# Again let us check that failure of the SELECT statement has not
# released metadata lock on table 't1', i.e. that the latest RENAME
# is blocked.
# Commit transaction to unblock this RENAME TABLE.
commit;
#
# Switching to connection 'deadlock_con3'.
# Reap RENAME TABLE t1 TO t0 ... .
#
# Switching to connection 'deadlock_con2'.
# Commit transaction to unblock the first RENAME TABLE.
commit;
#
# Switching to connection 'default'.
# Reap RENAME TABLE t2 TO t0 ... .
drop tables t1, t2, t3, t4;
#
# Now, test case which shows that deadlock detection empiric
# also takes into account requests for metadata lock upgrade.
#
create table t1 (i int);
#
# Switching to connection 'deadlock_con1'.
begin;
insert into t1 values (1);
#
# Switching to connection 'default'.
# Send:
alter table t1 add column j int, rename to t2;;
#
# Switching to connection 'deadlock_con1'.
# Wait until the above ALTER TABLE ... RENAME acquires exclusive
# metadata lock on 't2' and starts waiting for connection
# 'deadlock_con1' which holds shared lock on 't1'.
# The below statement should not wait as it will cause deadlock.
# An appropriate error should be reported instead.
select * from t2;
ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
# Again let us check that failure of the above statement has not
# released all metadata locks in connection 'deadlock_con1' and
# so ALTER TABLE ... RENAME is still blocked.
# Commit transaction to unblock ALTER TABLE ... RENAME.
commit;
#
# Switching to connection 'default'.
# Reap ALTER TABLE ... RENAME.
drop table t2;
#
# Finally, test case in which deadlock (or potentially livelock) occurs
# between metadata locking subsystem and table definition cache/table
# locks, but which should still be detected by our empiric.
#
create table t1 (i int);
#
# Switching to connection 'deadlock_con1'.
begin;
insert into t1 values (1);
#
# Switching to connection 'default'.
lock tables t1 write;
#
# Switching to connection 'deadlock_con1'.
# Send:
insert into t1 values (2);;
#
# Switching to connection 'default'.
# Wait until INSERT in connection 'deadlock_con1' is blocked on
# table-level lock.
# Send:
alter table t1 add column j int;;
#
# Switching to connection 'deadlock_con1'.
# The above ALTER TABLE statement should cause INSERT statement in
# this connection to be aborted and emit ER_LOCK_DEADLOCK error.
# Reap INSERT
ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
# Commit transaction to unblock ALTER TABLE.
commit;
#
# Switching to connection 'default'.
# Reap ALTER TABLE.
unlock tables;
drop table t1;
#
# Test for bug #46748 "Assertion in MDL_context::wait_for_locks()
# on INSERT + CREATE TRIGGER".
#
......@@ -234,6 +448,43 @@ drop table t2;
# Clean-up.
drop table t1;
#
# Test for bug #46273 "MySQL 5.4.4 new MDL: Bug#989 is not fully fixed
# in case of ALTER".
#
drop table if exists t1;
set debug_sync= 'RESET';
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 'con46273'.
set debug_sync='after_lock_tables_takes_lock SIGNAL alter_table_locked WAIT_FOR alter_go';
alter table t1 add column e int, rename to t2;;
#
# Switching to connection 'default'.
set debug_sync='now WAIT_FOR alter_table_locked';
set debug_sync='wait_for_lock SIGNAL alter_go';
# The below statement should get ER_LOCK_DEADLOCK error
# (i.e. it should not allow ALTER to proceed, and then
# fail due to 't1' changing its name to 't2').
update t1 set c3=c3+1 where c2=4;
ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
#
# Let us check that failure of the above statement has not released
# metadata lock on table 't1', i.e. that ALTER TABLE is still blocked.
# Unblock ALTER TABLE by commiting transaction and thus releasing
# metadata lock on 't1'.
commit;
#
# Switching to connection 'con46273'.
# Reap ALTER TABLE.
#
# Switching to connection 'default'.
# Clean-up.
set debug_sync= 'RESET';
drop table t2;
#
# Test for bug #46673 "Deadlock between FLUSH TABLES WITH READ LOCK
# and DML".
#
......
......@@ -125,16 +125,15 @@ drop temporary table t1;
#
# For that, start a transaction, use a routine. In a concurrent
# connection, try to drop or alter the routine. It should place
# a pending or exlusive lock and block. In a concurrnet
# connection, try to use the routine under LOCK TABLES.
# That should yield ER_LOCK_DEADLOCK.
# a pending or exclusive lock and block. In another concurrnet
# connection, try to use the routine.
# That should block on the pending exclusive lock.
#
# Establish helper connections.
#
# Test DROP PROCEDURE.
#
# --> connection default
create table t1 (a int);
create procedure p1() begin end;
create function f1() returns int
begin
......@@ -151,14 +150,17 @@ drop procedure p1;
# --> connection con2
# Waitng for 'drop procedure t1' to get blocked on MDL lock...
# Demonstrate that there is a pending exclusive lock.
lock table t1 read;
# Sending 'select f1()'...
select f1();
ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
unlock tables;
# --> connection con3
# Waitng for 'select f1()' to get blocked by a pending MDL lock...
# --> connection default
commit;
# --> connection con1
# Reaping 'drop procedure p1'...
# --> connection con2
# Reaping 'select f1()'
ERROR 42000: PROCEDURE test.p1 does not exist
# --> connection default
#
# Test CREATE PROCEDURE.
......@@ -174,17 +176,22 @@ create procedure p1() begin end;
# --> connection con2
# Waitng for 'create procedure t1' to get blocked on MDL lock...
# Demonstrate that there is a pending exclusive lock.
lock table t1 read;
# Sending 'select f1()'...
select f1();
ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
unlock tables;
# --> connection con3
# Waitng for 'select f1()' to get blocked by a pending MDL lock...
# --> connection default
commit;
# --> connection con1
# Reaping 'create procedure p1'...
ERROR 42000: PROCEDURE p1 already exists
# --> connection con2
# Reaping 'select f1()'
f1()
1
#
# Test ALTER PROCEDURE.
#
begin;
select f1();
f1()
......@@ -195,14 +202,18 @@ alter procedure p1 contains sql;
# --> connection con2
# Waitng for 'alter procedure t1' to get blocked on MDL lock...
# Demonstrate that there is a pending exclusive lock.
lock table t1 read;
# Sending 'select f1()'...
select f1();
ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
unlock tables;
# --> connection con3
# Waitng for 'select f1()' to get blocked by a pending MDL lock...
# --> connection default
commit;
# --> connection con1
# Reaping 'alter procedure p1'...
# --> connection con2
# Reaping 'select f1()'
f1()
1
# --> connection default
#
# Test DROP FUNCTION.
......@@ -217,14 +228,17 @@ drop function f1;
# --> connection con2
# Waitng for 'drop function f1' to get blocked on MDL lock...
# Demonstrate that there is a pending exclusive lock.
lock table t1 read;
# Sending 'select f1()'...
select f1();
ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
unlock tables;
# --> connection con3
# Waitng for 'select f1()' to get blocked by a pending MDL lock...
# --> connection default
commit;
# --> connection con1
# Reaping 'drop function f1'...
# --> connection con2
# Reaping 'select f1()'
ERROR 42000: FUNCTION test.f1 does not exist
# --> connection default
#
# Test CREATE FUNCTION.
......@@ -240,18 +254,23 @@ create function f1() returns int return 2;
# --> connection con2
# Waitng for 'create function f1' to get blocked on MDL lock...
# Demonstrate that there is a pending exclusive lock.
lock table t1 read;
# Sending 'select f1()'...
select f1();
ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
unlock tables;
# --> connection con3
# Waitng for 'select f1()' to get blocked by a pending MDL lock...
# --> connection default
commit;
# --> connection con1
# Reaping 'create function f1'...
ERROR 42000: FUNCTION f1 already exists
# --> connection con2
# Reaping 'select f1()'
f1()
1
# --> connection default
#
# Test ALTER FUNCTION.
#
begin;
select f1();
f1()
......@@ -262,14 +281,18 @@ alter function f1 contains sql;
# --> connection con2
# Waitng for 'alter function f1' to get blocked on MDL lock...
# Demonstrate that there is a pending exclusive lock.
lock table t1 read;
# Sending 'select f1()'...
select f1();
ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
unlock tables;
# --> connection con3
# Waitng for 'select f1()' to get blocked by a pending MDL lock...
# --> connection default
commit;
# --> connection con1
# Reaping 'alter function f1'...
# --> connection con2
# Reaping 'select f1()'
f1()
1
# --> connection default
drop function f1;
drop procedure p1;
......@@ -283,6 +306,7 @@ drop procedure p1;
#
create procedure p1() begin end;
create procedure p2() begin end;
create table t1 (a int);
create procedure p3()
begin
call p1();
......@@ -415,36 +439,11 @@ drop table t1, t2;
# acquisition of a shared lock fails during a transaction or
# we need to back off to flush the sp cache.
#
# a) A back off due to a lock conflict.
#
create table t1 (a int);
create function f1() returns int return 6;
begin;
select f1();
f1()
6
# --> connection con1
# Sending 'drop function f1'...
drop function f1;
# --> connection con2
# Waitng for 'drop function f1' to get blocked on MDL lock...
begin;
select * from t1;
a
select f1();
ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
commit;
# --> connection default
commit;
# --> connection con1
# Reaping 'drop function f1'...
# --> connection default
#
# b) A back off to flush the cache.
# Sic: now this situation does not require a back off since we
# flush the cache on the fly.
#
create function f1() returns int return 7;
create table t1 (a int);
begin;
select * from t1;
a
......@@ -691,6 +690,7 @@ drop function f1;
set @@session.max_sp_recursion_depth=default;
# --> connection con1
# --> connection con2
# --> connection con3
# --> connection default
#
# End of 5.5 tests
......
This diff is collapsed.
This diff is collapsed.
......@@ -1506,7 +1506,7 @@ int Old_rows_log_event::do_apply_event(Relay_log_info const *rli)
*/
thd->binlog_flush_pending_rows_event(false);
TABLE_LIST *tables= rli->tables_to_lock;
close_tables_for_reopen(thd, &tables);
close_tables_for_reopen(thd, &tables, NULL);
uint tables_count= rli->tables_to_lock_count;
if ((error= open_tables(thd, &tables, &tables_count, 0)))
......
......@@ -196,6 +196,7 @@ void MDL_context::init(THD *thd_arg)
to empty the list.
*/
m_tickets.empty();
m_is_waiting_in_mdl= FALSE;
}
......@@ -803,14 +804,28 @@ MDL_context::clone_ticket(MDL_request *mdl_request)
@retval FALSE Lock is not a shared one or no thread was woken up
*/
static bool notify_shared_lock(THD *thd, MDL_ticket *conflicting_ticket)
bool notify_shared_lock(THD *thd, MDL_ticket *conflicting_ticket)
{
bool woke= FALSE;
if (conflicting_ticket->is_shared())
{
THD *conflicting_thd= conflicting_ticket->get_ctx()->get_thd();
DBUG_ASSERT(thd != conflicting_thd); /* Self-deadlock */
woke= mysql_notify_thread_having_shared_lock(thd, conflicting_thd);
/*
If the thread that holds the conflicting lock is waiting
on an MDL lock, wake it up by broadcasting on COND_mdl.
Otherwise it must be waiting on a table-level lock
or some other non-MDL resource, so delegate its waking up
to an external call.
*/
if (conflicting_ticket->get_ctx()->is_waiting_in_mdl())
{
pthread_cond_broadcast(&COND_mdl);
woke= TRUE;
}
else
woke= mysql_notify_thread_having_shared_lock(thd, conflicting_thd);
}
return woke;
}
......@@ -957,7 +972,7 @@ bool MDL_context::acquire_exclusive_locks(MDL_request_list *mdl_requests)
to abort this thread once again.
*/
struct timespec abstime;
set_timespec(abstime, 10);
set_timespec(abstime, 1);
pthread_cond_timedwait(&COND_mdl, &LOCK_mdl, &abstime);
}
if (mysys_var->abort)
......@@ -1032,6 +1047,7 @@ MDL_ticket::upgrade_shared_lock_to_exclusive()
const char *old_msg;
st_my_thread_var *mysys_var= my_thread_var;
THD *thd= m_ctx->get_thd();
MDL_ticket *pending_ticket;
DBUG_ENTER("MDL_ticket::upgrade_shared_lock_to_exclusive");
DEBUG_SYNC(thd, "mdl_upgrade_shared_lock_to_exclusive");
......@@ -1045,8 +1061,22 @@ MDL_ticket::upgrade_shared_lock_to_exclusive()
/* Only allow upgrades from MDL_SHARED_UPGRADABLE */
DBUG_ASSERT(m_type == MDL_SHARED_UPGRADABLE);
/*
Create an auxiliary ticket to represent a pending exclusive
lock and add it to the 'waiting' queue for the duration
of upgrade. During upgrade we abort waits of connections
that own conflicting locks. A pending request is used
to signal such connections that upon waking up they
must back off, rather than fall into sleep again.
*/
if (! (pending_ticket= MDL_ticket::create(m_ctx, MDL_EXCLUSIVE)))
DBUG_RETURN(TRUE);
pthread_mutex_lock(&LOCK_mdl);
pending_ticket->m_lock= m_lock;
m_lock->waiting.push_front(pending_ticket);
old_msg= MDL_ENTER_COND(thd, mysys_var);
/*
......@@ -1088,6 +1118,30 @@ MDL_ticket::upgrade_shared_lock_to_exclusive()
MDL_ticket *conflicting_ticket;
MDL_lock::Ticket_iterator it(m_lock->granted);
/*
A temporary work-around to avoid deadlocks/livelocks in
a situation when in one connection ALTER TABLE tries to
upgrade its metadata lock and in another connection
the active transaction already got this lock in some
of its earlier statements.
In such case this transaction always succeeds with getting
a metadata lock on the table -- it already has one.
But later on it may block on the table level lock, since ALTER
got TL_WRITE_ALLOW_READ, and subsequently get aborted
by notify_shared_lock().
An abort will lead to a back off, and a second attempt to
get an MDL lock (successful), and a table lock (-> livelock).
The call below breaks this loop by forcing transactions to call
tdc_wait_for_old_versions() (even if the transaction doesn't need
any new metadata locks), which in turn will check if someone
is waiting on the owned MDL lock, and produce ER_LOCK_DEADLOCK.
TODO: Long-term such deadlocks/livelock will be resolved within
MDL subsystem and thus this call will become unnecessary.
*/
mysql_abort_transactions_with_shared_lock(&m_lock->key);
while ((conflicting_ticket= it++))
{
if (conflicting_ticket->m_ctx != m_ctx)
......@@ -1108,12 +1162,15 @@ MDL_ticket::upgrade_shared_lock_to_exclusive()
to abort this thread once again.
*/
struct timespec abstime;
set_timespec(abstime, 10);
set_timespec(abstime, 1);
DBUG_PRINT("info", ("Failed to wake-up from table-level lock ... sleeping"));
pthread_cond_timedwait(&COND_mdl, &LOCK_mdl, &abstime);
}
if (mysys_var->abort)
{
/* Remove and destroy the auxiliary pending ticket. */
m_lock->waiting.remove(pending_ticket);
MDL_ticket::destroy(pending_ticket);
/* Pending requests for shared locks can be satisfied now. */
pthread_cond_broadcast(&COND_mdl);
MDL_EXIT_COND(thd, mysys_var, old_msg);
......@@ -1124,6 +1181,11 @@ MDL_ticket::upgrade_shared_lock_to_exclusive()
m_lock->type= MDL_lock::MDL_LOCK_EXCLUSIVE;
/* Set the new type of lock in the ticket. */
m_type= MDL_EXCLUSIVE;
/* Remove and destroy the auxiliary pending ticket. */
m_lock->waiting.remove(pending_ticket);
MDL_ticket::destroy(pending_ticket);
if (m_lock->cached_object)
(*m_lock->cached_object_release_hook)(m_lock->cached_object);
m_lock->cached_object= 0;
......@@ -1239,6 +1301,59 @@ bool MDL_context::acquire_global_shared_lock()
}
/**
Check if there are any pending exclusive locks which conflict
with shared locks held by this thread.
@pre The caller already has acquired LOCK_mdl.
@return TRUE If there are any pending conflicting locks.
FALSE Otherwise.
*/
bool MDL_context::can_wait_lead_to_deadlock_impl() const
{
Ticket_iterator ticket_it(m_tickets);
MDL_ticket *ticket;
while ((ticket= ticket_it++))
{
/*
In MySQL we never call this method while holding exclusive or
upgradeable shared metadata locks.
Otherwise we would also have to check for the presence of pending
requests for conflicting types of global lock.
In addition MDL_ticket::has_pending_conflicting_lock_impl()
won't work properly for exclusive type of lock.
*/
DBUG_ASSERT(! ticket->is_upgradable_or_exclusive());
if (ticket->has_pending_conflicting_lock_impl())
return TRUE;
}
return FALSE;
}
/**
Implement a simple deadlock detection heuristic: check if there
are any pending exclusive locks which conflict with shared locks
held by this thread. In that case waiting can be circular,
i.e. lead to a deadlock.
@return TRUE if there are any conflicting locks, FALSE otherwise.
*/
bool MDL_context::can_wait_lead_to_deadlock() const
{
bool result;
pthread_mutex_lock(&LOCK_mdl);
result= can_wait_lead_to_deadlock_impl();
pthread_mutex_unlock(&LOCK_mdl);
return result;
}
/**
Wait until there will be no locks that conflict with lock requests
in the given list.
......@@ -1249,7 +1364,7 @@ bool MDL_context::acquire_global_shared_lock()
Does not acquire the locks!
@retval FALSE Success. One can try to obtain metadata locks.
@retval TRUE Failure (thread was killed)
@retval TRUE Failure (thread was killed or deadlock is possible).
*/
bool
......@@ -1278,6 +1393,26 @@ MDL_context::wait_for_locks(MDL_request_list *mdl_requests)
mysql_ha_flush(m_thd);
pthread_mutex_lock(&LOCK_mdl);
old_msg= MDL_ENTER_COND(m_thd, mysys_var);
/*
In cases when we wait while still holding some metadata
locks deadlocks are possible.
To avoid them we use the following simple empiric - don't
wait for new lock request to be satisfied if for one of the
locks which are already held by this connection there is
a conflicting request (i.e. this connection should not wait
if someone waits for it).
This empiric should work well (e.g. give low number of false
negatives) in situations when conflicts are rare (in our
case this is true since DDL statements should be rare).
*/
if (can_wait_lead_to_deadlock_impl())
{
MDL_EXIT_COND(m_thd, mysys_var, old_msg);
my_error(ER_LOCK_DEADLOCK, MYF(0));
return TRUE;
}
it.rewind();
while ((mdl_request= it++))
{
......@@ -1301,7 +1436,9 @@ MDL_context::wait_for_locks(MDL_request_list *mdl_requests)
MDL_EXIT_COND(m_thd, mysys_var, old_msg);
break;
}
m_is_waiting_in_mdl= TRUE;
pthread_cond_wait(&COND_mdl, &LOCK_mdl);
m_is_waiting_in_mdl= FALSE;
/* As a side-effect MDL_EXIT_COND() unlocks LOCK_mdl. */
MDL_EXIT_COND(m_thd, mysys_var, old_msg);
}
......@@ -1550,21 +1687,38 @@ MDL_context::is_lock_owner(MDL_key::enum_mdl_namespace mdl_namespace,
existing shared lock.
@pre The ticket must match an acquired lock.
@pre The caller already has acquired LOCK_mdl.
@param ticket Shared lock against which check should be performed.
@return TRUE if there is a conflicting lock request, FALSE otherwise.
*/
@return TRUE if there are any conflicting locks, FALSE otherwise.
bool MDL_ticket::has_pending_conflicting_lock_impl() const
{
DBUG_ASSERT(is_shared());
safe_mutex_assert_owner(&LOCK_mdl);
return !m_lock->waiting.is_empty();
}
/**
Check if we have any pending exclusive locks which conflict with
existing shared lock.
@pre The ticket must match an acquired lock.
@return TRUE if there is a pending conflicting lock request,
FALSE otherwise.
*/
bool MDL_ticket::has_pending_conflicting_lock() const
{
bool result;
DBUG_ASSERT(is_shared());
safe_mutex_assert_not_owner(&LOCK_open);
pthread_mutex_lock(&LOCK_mdl);
result= !m_lock->waiting.is_empty();
result= has_pending_conflicting_lock_impl();
pthread_mutex_unlock(&LOCK_mdl);
return result;
}
......
......@@ -300,6 +300,8 @@ class MDL_ticket
private:
MDL_ticket(const MDL_ticket &); /* not implemented */
MDL_ticket &operator=(const MDL_ticket &); /* not implemented */
bool has_pending_conflicting_lock_impl() const;
};
......@@ -380,10 +382,19 @@ class MDL_context
void release_transactional_locks();
void rollback_to_savepoint(MDL_ticket *mdl_savepoint);
bool can_wait_lead_to_deadlock() const;
inline THD *get_thd() const { return m_thd; }
bool is_waiting_in_mdl() const { return m_is_waiting_in_mdl; }
private:
Ticket_list m_tickets;
bool m_has_global_shared_lock;
/**
Indicates that the owner of this context is waiting in
wait_for_locks() method.
*/
bool m_is_waiting_in_mdl;
/**
This member has two uses:
1) When entering LOCK TABLES mode, remember the last taken
......@@ -397,6 +408,7 @@ class MDL_context
THD *m_thd;
private:
void release_ticket(MDL_ticket *ticket);
bool can_wait_lead_to_deadlock_impl() const;
MDL_ticket *find_ticket(MDL_request *mdl_req,
bool *is_lt_or_ha);
void release_locks_stored_before(MDL_ticket *sentinel);
......@@ -413,6 +425,7 @@ void mdl_destroy();
extern bool mysql_notify_thread_having_shared_lock(THD *thd, THD *in_use);
extern void mysql_ha_flush(THD *thd);
extern void mysql_abort_transactions_with_shared_lock(const MDL_key *mdl_key);
extern "C" const char *set_thd_proc_info(THD *thd, const char *info,
const char *calling_function,
const char *calling_file,
......
......@@ -1522,7 +1522,8 @@ void free_io_cache(TABLE *entry);
void intern_close_table(TABLE *entry);
bool close_thread_table(THD *thd, TABLE **table_ptr);
void close_temporary_tables(THD *thd);
void close_tables_for_reopen(THD *thd, TABLE_LIST **tables);
void close_tables_for_reopen(THD *thd, TABLE_LIST **tables,
MDL_ticket *mdl_savepoint);
TABLE_LIST *find_table_in_list(TABLE_LIST *table,
TABLE_LIST *TABLE_LIST::*link,
const char *db_name,
......
This diff is collapsed.
......@@ -1297,18 +1297,29 @@ class Open_table_context
bool can_recover_from_failed_open() const
{ return m_action != OT_NO_ACTION; }
bool can_deadlock() const { return m_can_deadlock; }
/**
When doing a back-off, we close all tables acquired by this
statement. Return an MDL savepoint taken at the beginning of
the statement, so that we can rollback to it before waiting on
locks.
*/
MDL_ticket *start_of_statement_svp() const
{
return m_start_of_statement_svp;
}
private:
/** List of requests for all locks taken so far. Used for waiting on locks. */
MDL_request_list m_mdl_requests;
/** Back off action. */
enum enum_open_table_action m_action;
MDL_ticket *m_start_of_statement_svp;
/**
Whether we had any locks when this context was created.
If we did, they are from the previous statement of a transaction,
and we can't safely do back-off (and release them).
*/
bool m_can_deadlock;
bool m_has_locks;
};
......
......@@ -2530,7 +2530,7 @@ pthread_handler_t handle_delayed_insert(void *arg)
aborted. Try to reopen table and if it fails die.
*/
TABLE_LIST *tl_ptr = &di->table_list;
close_tables_for_reopen(thd, &tl_ptr);
close_tables_for_reopen(thd, &tl_ptr, NULL);
di->table= 0;
if (di->open_and_lock_table())
{
......
......@@ -132,11 +132,11 @@ class I_P_List
template <typename T, typename B>
class I_P_List_iterator
{
I_P_List<T, B> *list;
const I_P_List<T, B> *list;
T *current;
public:
I_P_List_iterator(I_P_List<T, B> &a) : list(&a), current(a.first) {}
I_P_List_iterator(I_P_List<T, B> &a, T* current_arg) : list(&a), current(current_arg) {}
I_P_List_iterator(const I_P_List<T, B> &a) : list(&a), current(a.first) {}
I_P_List_iterator(const I_P_List<T, B> &a, T* current_arg) : list(&a), current(current_arg) {}
inline void init(I_P_List<T, B> &a)
{
list= &a;
......
......@@ -2941,7 +2941,7 @@ fill_schema_show_cols_or_idxs(THD *thd, TABLE_LIST *tables,
table, res, db_name,
table_name));
thd->temporary_tables= 0;
close_tables_for_reopen(thd, &show_table_list);
close_tables_for_reopen(thd, &show_table_list, NULL);
DBUG_RETURN(error);
}
......@@ -3500,7 +3500,7 @@ int get_all_tables(THD *thd, TABLE_LIST *tables, COND *cond)
res= schema_table->process_table(thd, show_table_list, table,
res, &orig_db_name,
&tmp_lex_string);
close_tables_for_reopen(thd, &show_table_list);
close_tables_for_reopen(thd, &show_table_list, NULL);
}
DBUG_ASSERT(!lex->query_tables_own_last);
if (res)
......
......@@ -206,6 +206,7 @@ int mysql_update(THD *thd,
ulonglong id;
List<Item> all_fields;
THD::killed_state killed_status= THD::NOT_KILLED;
MDL_ticket *start_of_statement_svp= thd->mdl_context.mdl_savepoint();
DBUG_ENTER("mysql_update");
for ( ; ; )
......@@ -226,7 +227,7 @@ int mysql_update(THD *thd,
break;
if (!need_reopen)
DBUG_RETURN(1);
close_tables_for_reopen(thd, &table_list);
close_tables_for_reopen(thd, &table_list, start_of_statement_svp);
}
if (mysql_handle_derived(thd->lex, &mysql_derived_prepare) ||
......@@ -981,6 +982,7 @@ int mysql_multi_update_prepare(THD *thd)
const bool using_lock_tables= thd->locked_tables_mode != LTM_NONE;
bool original_multiupdate= (thd->lex->sql_command == SQLCOM_UPDATE_MULTI);
bool need_reopen= FALSE;
MDL_ticket *start_of_statement_svp= thd->mdl_context.mdl_savepoint();
DBUG_ENTER("mysql_multi_update_prepare");
/* following need for prepared statements, to run next time multi-update */
......@@ -1145,7 +1147,7 @@ int mysql_multi_update_prepare(THD *thd)
*/
cleanup_items(thd->free_list);
close_tables_for_reopen(thd, &table_list);
close_tables_for_reopen(thd, &table_list, start_of_statement_svp);
goto reopen_tables;
}
......
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