V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
weishao666
V2EX  ›  问与答

来分析 mysql 死锁了

  •  1
     
  •   weishao666 · 2023-03-19 23:55:48 +08:00 · 865 次点击
    这是一个创建于 650 天前的主题,其中的信息可能已经有所发展或是发生改变。
    ------------------------
    LATEST DETECTED DEADLOCK
    ------------------------
    2023-03-15 15:32:15 7f1948331700
    *** (1) TRANSACTION:
    TRANSACTION 45939504, ACTIVE 0 sec inserting
    mysql tables in use 1, locked 1
    LOCK WAIT 3 lock struct(s), heap size 1184, 2 row lock(s), undo log entries 1
    MySQL thread id 6117, OS thread handle 0x7f1948435700, query id 135246315 10.24.94.10 root update
    INSERT INTO run_pod ( cluster, NAME, image_name, cpu_limit, memory_limit, cpu_request, memory_request, create_by, run_pod_type, priority, expire_time, create_time, update_time )
    VALUES
    	(
    		'local',
    		'vnc-261',
    		'vivado2017-novnc-largeimage',
    		1.0,
    		1024,
    		0.1,
    		10,
    		'10.24.94.10',
    		3,
    		0,
    		'2023-03-15 16:02:15.427',
    		'2023-03-15 15:32:15.427',
    	'2023-03-15 15:32:15.427' 
    	)
    *** (1) WAITING FOR THIS LOCK TO BE GRANTED:
    RECORD LOCKS space id 796 page no 4 n bits 136 index `UNI_NAME` of table `localbridge`.`run_pod` trx id 45939504 lock_mode X insert intention waiting
    Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
     0: len 8; hex 73757072656d756d; asc supremum;;
    
    *** (2) TRANSACTION:
    TRANSACTION 45938917, ACTIVE 2 sec inserting
    mysql tables in use 1, locked 1
    4 lock struct(s), heap size 1184, 3 row lock(s), undo log entries 1
    MySQL thread id 5550, OS thread handle 0x7f1948331700, query id 135245596 10.24.94.10 root update
    INSERT INTO run_pod ( cluster, NAME, image_name, cpu_limit, memory_limit, cpu_request, memory_request, create_by, run_pod_type, priority, expire_time, create_time, update_time )
    VALUES
    	(
    		'local',
    		'vnc-260',
    		'vivado2017-novnc-largeimage',
    		1.0,
    		1024,
    		0.1,
    		10,
    		'10.24.94.10',
    		3,
    		0,
    		'2023-03-15 16:02:13.957',
    		'2023-03-15 15:32:13.957',
    	'2023-03-15 15:32:13.957' 
    	)
    *** (2) HOLDS THE LOCK(S):
    RECORD LOCKS space id 796 page no 4 n bits 136 index `UNI_NAME` of table `localbridge`.`run_pod` trx id 45938917 lock_mode X
    Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
     0: len 8; hex 73757072656d756d; asc supremum;;
    
    *** (2) WAITING FOR THIS LOCK TO BE GRANTED:
    RECORD LOCKS space id 796 page no 4 n bits 136 index `UNI_NAME` of table `localbridge`.`run_pod` trx id 45938917 lock_mode X insert intention waiting
    Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
     0: len 8; hex 73757072656d756d; asc supremum;;
    
    *** WE ROLL BACK TRANSACTION (1)
    ------------
    TRANSACTIONS
    ------------
    Trx id counter 48671067
    Purge done for trx's n:o < 48671067 undo n:o < 0 state: running but idle
    History list length 3428
    LIST OF TRANSACTIONS FOR EACH SESSION:
    ---TRANSACTION 0, not started
    MySQL thread id 7366, OS thread handle 0x7f194b78f700, query id 142556188 172.17.0.1 root init
    show engine innodb status
    ---TRANSACTION 48670419, not started
    MySQL thread id 7363, OS thread handle 0x7f19481ab700, query id 142554235 10.24.94.10 root cleaning up
    ---TRANSACTION 48671065, not started
    MySQL thread id 7293, OS thread handle 0x7f19483f4700, query id 142556186 10.24.94.10 root cleaning up
    --------
    FILE I/O
    --------
    I/O thread 0 state: waiting for completed aio requests (insert buffer thread)
    I/O thread 1 state: waiting for completed aio requests (log thread)
    I/O thread 2 state: waiting for completed aio requests (read thread)
    I/O thread 3 state: waiting for completed aio requests (read thread)
    I/O thread 4 state: waiting for completed aio requests (read thread)
    I/O thread 5 state: waiting for completed aio requests (read thread)
    I/O thread 6 state: waiting for completed aio requests (write thread)
    I/O thread 7 state: waiting for completed aio requests (write thread)
    I/O thread 8 state: waiting for completed aio requests (write thread)
    I/O thread 9 state: waiting for completed aio requests (write thread)
    Pending normal aio reads: 0 [0, 0, 0, 0] , aio writes: 0 [0, 0, 0, 0] ,
     ibuf aio reads: 0, log i/o's: 0, sync i/o's: 0
    Pending flushes (fsync) log: 0; buffer pool: 0
    5341 OS file reads, 18997801 OS file writes, 18824565 OS fsyncs
    0.00 reads/s, 0 avg bytes/read, 6.99 writes/s, 6.99 fsyncs/s
    -------------------------------------
    INSERT BUFFER AND ADAPTIVE HASH INDEX
    -------------------------------------
    Ibuf: size 1, free list len 0, seg size 2, 0 merges
    merged operations:
     insert 0, delete mark 0, delete 0
    discarded operations:
     insert 0, delete mark 0, delete 0
    Hash table size 276671, node heap has 57 buffer(s)
    20.98 hash searches/s, 0.00 non-hash searches/s
    

    表有唯一索引 name ,主键自增,业务场景是有多个线程同时接受多个用户的请求,创建容器。

    每个线程的操作为:查询该表,如果有 name 对应的记录,则更新,否则插入记录 由于不同的线程对应不同的用户,name 与用户一一对应,所以不同的线程处理的 name 都不会相同,不会存在多个线程操作同一条记录

    mysql5.6 ,RR 的隔离级别 mysql 锁掌握得不深,是因为有索引,mysql 在索引前后加了间隙锁,导致我两条相邻的记录插入会死锁么,那这样的话应该很容易死锁,可我们也运行了很久,才出现死锁

    3 条回复    2023-03-20 10:35:07 +08:00
    weishao666
        1
    weishao666  
    OP
       2023-03-19 23:58:49 +08:00
    补充一下,兴许真和隔离级别有关,运行了很久的场景是另外一套环境,隔离级别是 RC ,这个是新部署的环境,隔离级别是 RR
    echo1937
        2
    echo1937  
       2023-03-20 07:57:30 +08:00 via iPhone
    *** (1) WAITING FOR THIS LOCK TO BE GRANTED:
    RECORD LOCKS space id 796 page no 4 n bits 136 index `UNI_NAME` of table `localbridge`.`run_pod` trx id 45939504 lock_mode X insert intention waiting
    Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
    0: len 8; hex 73757072656d756d; asc supremum;;

    昨天刚看过,插入意向锁。
    个人觉得 2023 年了,确实应该上 8.0 ,然后用 RC 代替 RR 。
    Richared
        3
    Richared  
       2023-03-20 10:35:07 +08:00
    没用同步直接换成 rc 吧,有同步还是上 8.0 在开 rc
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   934 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 20:49 · PVG 04:49 · LAX 12:49 · JFK 15:49
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.