Percona pt-deadlock-logger没有检测到死锁

时间:2013-09-25 01:11:50

标签: mysql deadlock percona

在出现性能问题的服务器上,我正试图用Percona的pt-deadlock-logger来检测死锁

我在crontab文件中有这一行

0 * * * * root pt-deadlock-logger --daemonize --run-time=1h --dest D=test,t=deadlocks u=root,h=127.0.0.1

每当我登录服务器时,我都可以确认这是运行ps-ef | grep deadlock

设置数据库和表。我的理解是我根据/root/.my.cnf

中设置的密码使用root访问权限

我试图用(从这里:http://forums.mysql.com/read.php?10,193770,193913#msg-193913

模拟死锁
create table test.innodb_deadlock_maker(a int primary key) engine=innodb; 
insert into test.innodb_deadlock_maker(a) values(0), (1); 

-- connection 0 
set transaction isolation level serializable; 
start transaction; 
select * from test.innodb_deadlock_maker where a = 0; 
update test.innodb_deadlock_maker set a = 0 where a <> 0; 

-- connection 1 
set transaction isolation level serializable; 
start transaction; 
select * from test.innodb_deadlock_maker where a = 1; 
update test.innodb_deadlock_maker set a = 1 where a <> 1;

这显示了mysql控制台中的deadloc,但它没有记录在数据库表中。任何想法为什么不呢?

1 个答案:

答案 0 :(得分:2)

尝试模拟指示的僵局导致了这一点:

-- connection 0 
set transaction isolation level serializable; 
start transaction; 
select * from test.innodb_deadlock_maker where a = 0;
-- does not block, fails immediately with 
-- ERROR 1062 (23000): Duplicate entry '0' for key 'PRIMARY'
update test.innodb_deadlock_maker set a = 0 where a <> 0; 

-- connection 1 
set transaction isolation level serializable; 
start transaction; 
select * from test.innodb_deadlock_maker where a = 1; 
-- does block, fails after a timeout with
-- ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
update test.innodb_deadlock_maker set a = 1 where a <> 1;

因此,连接1等待连接0,但连接0不等待连接1,仅在应用程序的COMMITROLLBACK上等待。

这不是死锁,SHOW ENGINE INNODB STATUS也没有报告死锁。

将序列更改为:

-- connection 0 
set transaction isolation level serializable; 
start transaction; 
select * from test.innodb_deadlock_maker where a = 0;

-- connection 1 
set transaction isolation level serializable; 
start transaction; 
select * from test.innodb_deadlock_maker where a = 1; 

-- connection 0 
-- does block, waiting on connection 1
update test.innodb_deadlock_maker set a = 0 where a <> 0; 

-- connection 1 
-- would deadlock, fails immediately with
-- ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting
update test.innodb_deadlock_maker set a = 1 where a <> 1;

并取消阻止连接0:

-- connection 0 
update test.innodb_deadlock_maker set a = 0 where a <> 0; 
-- now unblocked, fails with
-- ERROR 1062 (23000): Duplicate entry '0' for key 'PRIMARY'

在这种情况下,SHOW ENGINE INNODB STATUS报告:

LATEST DETECTED DEADLOCK
------------------------
2013-10-03 11:57:27 0x7f6b60308700
*** (1) TRANSACTION:
TRANSACTION 1297, ACTIVE 47 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 5 lock struct(s), heap size 1160, 3 row lock(s)
MySQL thread id 2, OS thread handle 140099372304128, query id 13 localhost root Searching rows for update
update test.innodb_deadlock_maker set a = 0 where a <> 0
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 8 page no 3 n bits 72 index `PRIMARY` of table `test`.`innodb_deadlock_maker` trx id 1297 lock_mode X waiting
Record lock, heap no 3 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
 0: len 4; hex 80000001; asc     ;;
 1: len 6; hex 000000000508; asc       ;;
 2: len 7; hex a80000011a011c; asc        ;;

*** (2) TRANSACTION:
TRANSACTION 1298, ACTIVE 15 sec starting index read
mysql tables in use 1, locked 1
4 lock struct(s), heap size 1160, 2 row lock(s)
MySQL thread id 3, OS thread handle 140099152021248, query id 14 localhost root Searching rows for update
update test.innodb_deadlock_maker set a = 1 where a <> 1
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 8 page no 3 n bits 72 index `PRIMARY` of table `test`.`innodb_deadlock_maker` trx id 1298 lock mode S locks rec but not gap
Record lock, heap no 3 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
 0: len 4; hex 80000001; asc     ;;
 1: len 6; hex 000000000508; asc       ;;
 2: len 7; hex a80000011a011c; asc        ;;

*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 8 page no 3 n bits 72 index `PRIMARY` of table `test`.`innodb_deadlock_maker` trx id 1298 lock_mode X waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
 0: len 4; hex 80000000; asc     ;;
 1: len 6; hex 000000000508; asc       ;;
 2: len 7; hex a80000011a0110; asc        ;;

*** WE ROLL BACK TRANSACTION (2)

我无法与pt-deadlock-logger通话,但我认为它依赖于LATEST DETECTED DEADLOCK中的SHOW ENGINE INNODB STATUS部分,因此请首先确保此命令实际报告死锁。