1

在有性能问题的服务器上,我正在尝试使用 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 访问权限,基于 /root/.my.cnf 中设置的密码

我试图用(从这里: 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 控制台中显示了一个死锁,但它没有记录在数据库表中。有什么想法为什么不呢?

4

2 回答 2

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,所以首先请确保该命令确实报告了死锁。

于 2013-10-03T08:24:41.667 回答
1

我不直接回答你的问题。但是 pt-deadlock-logger 根本不需要添加到 crontab 中。

运行 pt-deadlock-logger 后,如果未指定 --run-time,则 pt-deadlock-logger 将永远运行,并在每个时间间隔检查死锁。您还可以通过选项 -interval 指定间隔。欲了解更多信息,请查看以下链接。

https://www.percona.com/doc/percona-toolkit/2.1/pt-deadlock-logger.html#cmdoption-pt-deadlock-logger--interval

于 2015-11-24T02:12:50.587 回答