dead lock

Backgrouod:

the error log is:

Before going to deploy [SPPA-17071] create and update record for campaign daily balance, i use automation test in liveish to mock deduct data. in the result, there is dead lock error for getx new campaign daily balance.

the error log is:

  1. getx campaign daily balance first time by id error Error
    {"lvl":"error","@t":"2022-05-11T18:09:55.811","caller":"handler/service.go:460","msg":"deduct money err: deduct err: getx new campaign daily balance error getx campaign daily balance first time by id error Error 1213: Deadlock found when trying to get lock; try restarting transaction, rollback ok","trackUserID":4333484,"adsUserID":533376269,"campaign":61066482,"adsID":93083853,"status":"StatusFail","itemID":12636280604,"shopID":533376269,"country":"XX"}
  1. insert init campaign daily balance Error 1213:
{"lvl":"error","@t":"2022-05-11T18:07:59.871","caller":"handler/service.go:460","msg":"deduct money err: deduct err: getx new campaign daily balance error insert init campaign daily balance Error 1213: Deadlock found when trying to get lock; try restarting transaction, rollback ok","status":"StatusFail","itemID":6541232741,"shopID":212108559,"country":"XX","trackUserID":99291123,"adsUserID":212108559,"campaign":9894084,"adsID":16675986}

it will return statusFail and affect ads revenue, so have to fix the bug first.

New feature Logic:

first of all, the table use (campaignid,ctime) as unique key:

| campaign_daily_xxxx | CREATE TABLE `campaign_daily_xxx```` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `campaignid` bigint(20) NOT NULL DEFAULT '0',
  `userid` bigint(20) NOT NULL DEFAULT '0',
  `daily_balance` bigint(20) NOT NULL DEFAULT '0',
  `mtime` bigint(20) NOT NULL DEFAULT '0',
  `ctime` bigint(20) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `campaignid` (`campaignid`,`ctime`)
) ENGINE=InnoDB AUTO_INCREMENT=216993 DEFAULT CHARSET=utf8mb4 |

in this feature. main logic can extract as:


begin transaction:
    1. campaignDailyBalance := GetXCampaignDailyBalanceDateByID(select * from campaign_daily_balance_tab_20220501 where campaignid=xxx and timestamp >= ctime ORDER BY ctime DESC LIMIT 1 for update) 
    2. if campaignDailyBalance not nil, then return.
       else insert one new campaignDailyBalance(insert into campaign_daily_balance_tab_20220501 xxxx). 
    3. getX camapaignDailyBalance again.
    4. other logic(get account, get campaign, calculate logic etc 
    5. update campaignDailyBalance 

reproduce by sql:

build data in local mysql and test. only focus on campaignid and timestamp so i use table as below.

in our deduction server, our workers read msg by shopid, one shopid cors one campaignid, so there is no case with concurrent handler one

| id | campaignid | ctime |
| 1 | 10 | 1100 |
| 2 | 11 | 1100 |
| 3 | 12 | 1100 |
| 4 | 15 | 1000 |
| 5 | 15 | 1200 |
| 6 | 15 | 1300 |
  1. case1:

session1: 

begin;

select * from campaign_daily_balance_tab_20220101 where campaignid=14 and 900>=ctime ORDER BY ctime DESC LIMIT 1 for update;

INSERT INTO campaign_daily_balance_tab_20220101 (campaignid,ctime) VALUES(14,900);
session2: 

begin;

select * from campaign_daily_balance_tab_20220101 where campaignid=13 and 900>=ctime ORDER BY ctime DESC LIMIT 1 for update; 

ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction

show ENGINE INNODB status:

------------------------
LATEST DETECTED DEADLOCK
------------------------
2022-05-13 00:52:45 0x700007333000
*** (1) TRANSACTION:
TRANSACTION 35260, ACTIVE 11 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 3 lock struct(s), heap size 1136, 2 row lock(s)
MySQL thread id 46, OS thread handle 123145423663104, query id 627 localhost root Sending data
select * from campaign_daily_balance_tab_20220101 where campaignid=13 and 900>=ctime ORDER BY ctime DESC LIMIT 1 for update
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 36 page no 4 n bits 104 index campaignid of table `entry_task`.`campaign_daily_balance_tab_20220101` trx id 35260 lock_mode X waiting
Record lock, heap no 34 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
 0: len 8; hex 800000000000000c; asc         ;;
 1: len 8; hex 800000000000044c; asc        L;;
 2: len 4; hex 8000003c; asc    <;;

*** (2) TRANSACTION:
TRANSACTION 35259, ACTIVE 23 sec inserting
mysql tables in use 1, locked 1
8 lock struct(s), heap size 1136, 7 row lock(s), undo log entries 1
MySQL thread id 44, OS thread handle 123145423106048, query id 629 localhost root update
INSERT INTO campaign_daily_balance_tab_20220101 (campaignid,ctime) VALUES(14,991)
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 36 page no 4 n bits 104 index campaignid of table `entry_task`.`campaign_daily_balance_tab_20220101` trx id 35259 lock_mode X
Record lock, heap no 34 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
 0: len 8; hex 800000000000000c; asc         ;;
 1: len 8; hex 800000000000044c; asc        L;;
 2: len 4; hex 8000003c; asc    <;;

*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 36 page no 4 n bits 104 index campaignid of table `entry_task`.`campaign_daily_balance_tab_20220101` trx id 35259 lock_mode X locks gap before rec insert intention waiting
Record lock, heap no 4 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
 0: len 8; hex 800000000000000e; asc         ;;
 1: len 8; hex 80000000000003e7; asc         ;;
 2: len 4; hex 80000043; asc    C;;

*** WE ROLL BACK TRANSACTION (1)
------------
TRANSACTIONS
------------
Trx id counter 35265
Purge done for trx's n:o < 35265 undo n:o < 0 state: running but idle
History list length 2
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 421984974412472, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 35259, ACTIVE 27 sec
9 lock struct(s), heap size 1136, 9 row lock(s), undo log entries 1
MySQL thread id 44, OS thread handle 123145423106048, query id 630 localhost root starting
show ENGINE INNODB status
--------
FILE I/O
--------
I/O thread 0 state: waiting for i/o request (insert buffer thread)
I/O thread 1 state: waiting for i/o request (log thread)
I/O thread 2 state: waiting for i/o request (read thread)
I/O thread 3 state: waiting for i/o request (read thread)
I/O thread 4 state: waiting for i/o request (read thread)
I/O thread 5 state: waiting for i/o request (read thread)
I/O thread 6 state: waiting for i/o request (write thread)
I/O thread 7 state: waiting for i/o request (write thread)
I/O thread 8 state: waiting for i/o request (write thread)
I/O thread 9 state: waiting for i/o request (write thread)
Pending normal aio reads: [0, 0, 0, 0] , aio writes: [0, 0, 0, 0] ,
 ibuf aio reads:, log i/o's:, sync i/o's:
Pending flushes (fsync) log: 0; buffer pool: 0
385 OS file reads, 1232 OS file writes, 939 OS fsyncs
0.00 reads/s, 0 avg bytes/read, 0.44 writes/s, 0.35 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 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 1 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
0.39 hash searches/s, 0.33 non-hash searches/s
---
LOG
---
Log sequence number 2681687751
Log flushed up to   2681687751
Pages flushed up to 2681687751
Last checkpoint at  2681687742
0 pending log flushes, 0 pending chkp writes
575 log i/o's done, 0.20 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total large memory allocated 137428992
Dictionary memory allocated 118621
Buffer pool size   8192
Free buffers       7838
Database pages     353
Old database pages 0
Modified db pages  0
Pending reads      0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 293, created 60, written 527
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 353, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
0 read views open inside InnoDB
Process ID=89536, Main thread ID=123145417719808, state: sleeping
Number of rows inserted 44, updated 0, deleted 30, read 720
0.02 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.02 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT
============================

analyse:

session1 and session2 should have same gap lock in (12,15]. ** A gap lock does not exclude other gap locks, but it excludes insertion intent locks.(so don't know why block in session2)?**

if session2 block and wait for gap lock. should not have dead lock.

refer to article: http://mysql.taobao.org/monthly/2022/02/01/

should be a Mysql bug.

case2:


session1: 

begin;

select * from campaign_daily_balance_tab_20220101 where campaignid=9  and 900

session2: 

begin;

select * from campaign_daily_balance_tab_20220101 where campaignid=8  and 900

analyse:

both session1 and session2 get same gap lock in (-∞,10]. and share same lock.

when insert new record in the same time, will try to get each other lock and dead lock in the end.


------------------------
LATEST DETECTED DEADLOCK
------------------------
2022-05-13 02:44:08 0x700007333000
*** (1) TRANSACTION:
TRANSACTION 35317, ACTIVE 70 sec inserting
mysql tables in use 1, locked 1
LOCK WAIT 4 lock struct(s), heap size 1136, 4 row lock(s), undo log entries 1
MySQL thread id 64, OS thread handle 123145423384576, query id 791 localhost root update
INSERT INTO campaign_daily_balance_tab_20220101 (campaignid,ctime) VALUES(8,900)
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 36 page no 4 n bits 104 index campaignid of table `entry_task`.`campaign_daily_balance_tab_20220101` trx id 35317 lock_mode X locks gap before rec insert intention waiting
Record lock, heap no 32 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
 0: len 8; hex 800000000000000a; asc         ;;
 1: len 8; hex 800000000000044c; asc        L;;
 2: len 4; hex 8000003a; asc    :;;

*** (2) TRANSACTION:
TRANSACTION 35319, ACTIVE 17 sec inserting
mysql tables in use 1, locked 1
3 lock struct(s), heap size 1136, 2 row lock(s), undo log entries 1
MySQL thread id 62, OS thread handle 123145423106048, query id 792 localhost root update
INSERT INTO campaign_daily_balance_tab_20220101 (campaignid,ctime) VALUES(9,900)
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 36 page no 4 n bits 104 index campaignid of table `entry_task`.`campaign_daily_balance_tab_20220101` trx id 35319 lock_mode X locks gap before rec
Record lock, heap no 32 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
 0: len 8; hex 800000000000000a; asc         ;;
 1: len 8; hex 800000000000044c; asc        L;;
 2: len 4; hex 8000003a; asc    :;;

*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 36 page no 4 n bits 104 index campaignid of table `entry_task`.`campaign_daily_balance_tab_20220101` trx id 35319 lock_mode X locks gap before rec insert intention waiting
Record lock, heap no 32 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
 0: len 8; hex 800000000000000a; asc         ;;
 1: len 8; hex 800000000000044c; asc        L;;
 2: len 4; hex 8000003a; asc    :;;

*** WE ROLL BACK TRANSACTION (2)
------------
TRANSACTIONS
------------
Trx id counter 35324
Purge done for trx's n:o < 35324 undo n:o < 0 state: running but idle
History list length 0
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 421984974411568, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 35317, ACTIVE 289 sec
4 lock struct(s), heap size 1136, 5 row lock(s), undo log entries 1
MySQL thread id 64, OS thread handle 123145423384576, query id 794 localhost root starting
show ENGINE INNODB status
--------
FILE I/O
--------
I/O thread 0 state: waiting for i/o request (insert buffer thread)
I/O thread 1 state: waiting for i/o request (log thread)
I/O thread 2 state: waiting for i/o request (read thread)
I/O thread 3 state: waiting for i/o request (read thread)
I/O thread 4 state: waiting for i/o request (read thread)
I/O thread 5 state: waiting for i/o request (read thread)
I/O thread 6 state: waiting for i/o request (write thread)
I/O thread 7 state: waiting for i/o request (write thread)
I/O thread 8 state: waiting for i/o request (write thread)
I/O thread 9 state: waiting for i/o request (write thread)
Pending normal aio reads: [0, 0, 0, 0] , aio writes: [0, 0, 0, 0] ,
 ibuf aio reads:, log i/o's:, sync i/o's:
Pending flushes (fsync) log: 0; buffer pool: 0
386 OS file reads, 1707 OS file writes, 1315 OS fsyncs
0.00 reads/s, 0 avg bytes/read, 0.00 writes/s, 0.00 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 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 1 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
0.00 hash searches/s, 0.00 non-hash searches/s
---
LOG
---
Log sequence number 2681724594
Log flushed up to   2681724594
Pages flushed up to 2681724594
Last checkpoint at  2681724585
0 pending log flushes, 0 pending chkp writes
804 log i/o's done, 0.00 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total large memory allocated 137428992
Dictionary memory allocated 118621
Buffer pool size   8192
Free buffers       7823
Database pages     368
Old database pages 0
Modified db pages  0
Pending reads      0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 294, created 74, written 725
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 368, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
0 read views open inside InnoDB
Process ID=89536, Main thread ID=123145417719808, state: sleeping
Number of rows inserted 51, updated 0, deleted 33, read 770
0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.00 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT

| 1. getx campaign daily balance first time by id error Error |
{"lvl":"error","@t":"2022-05-11T18:09:55.811","caller":"handler/service.go:460","msg":"deduct money err: deduct err: getx new campaign daily balance error getx campaign daily balance first time by id error Error 1213: Deadlock found when trying to get lock; try restarting transaction, rollback ok","trackUserID":4333484,"adsUserID":533376269,"campaign":61066482,"adsID":93083853,"status":"StatusFail","itemID":12636280604,"shopID":533376269,"country":"XX"}
| 2. insert init campaign daily balance Error 1213: |
"lvl":"error","@t":"2022-05-11T18:07:59.871","caller":"handler/service.go:460","msg":"deduct money err: deduct err: getx new campaign daily balance error insert init campaign daily balance Error 1213: Deadlock found when trying to get lock; try restarting transaction, rollback ok","status":"StatusFail","itemID":6541232741,"shopID":212108559,"country":"XX","trackUserID":99291123,"adsUserID":212108559,"campaign":9894084,"adsID":16675986}
| it will return statusFail and affect ads revenue, so have to fix the bug first. # New feature Logic: first of all, the table use (`campaignid`,`ctime`) as unique key: |
| campaign_daily_balance_tab_20220501 | CREATE TABLE `campaign_daily_balance_tab_20220501` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `campaignid` bigint(20) NOT NULL DEFAULT '0',
  `userid` bigint(20) NOT NULL DEFAULT '0',
  `daily_balance` bigint(20) NOT NULL DEFAULT '0',
  `mtime` bigint(20) NOT NULL DEFAULT '0',
  `ctime` bigint(20) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `campaignid` (`campaignid`,`ctime`)
) ENGINE=InnoDB AUTO_INCREMENT=216993 DEFAULT CHARSET=utf8mb4 |
+-------------------------------------+---------------------------
| in this feature. main logic can extract as: |
begin transaction:
    1\. campaignDailyBalance := GetXCampaignDailyBalanceDateByID(select * from campaign_daily_balance_tab_20220501 where campaignid=xxx and timestamp >= ctime ORDER BY ctime DESC LIMIT 1 for update) 
    2\. if campaignDailyBalance not nil, then return.
       else insert one new campaignDailyBalance(insert into campaign_daily_balance_tab_20220501 xxxx). 
    3\. getX camapaignDailyBalance again.
    4\. other logic(get account, get campaign, calculate logic etc)
    5\. update campaignDailyBalance 
| # reproduce by sql: build data in local mysql and test. only focus on campaignid and timestamp so i use table as below. in our deduction server, our workers read msg by shopid, one shopid cors one campaignid, so there is no case with concurrent handler one | id | campaignid | ctime | | 1 | 10 | 1100 | | 2 | 11 | 1100 | | 3 | 12 | 1100 | | 4 | 15 | 1000 | | 5 | 15 | 1200 | | 6 | 15 | 1300 | 1. case1: | session1: begin; select * from campaign_daily_balance_tab_20220101 where campaignid=14 and 900>=ctime ORDER BY ctime DESC LIMIT 1 for update; INSERT INTO campaign_daily_balance_tab_20220101 (campaignid,ctime) VALUES(14,900); | session2: begin; select * from campaign_daily_balance_tab_20220101 where campaignid=13 and 900>=ctime ORDER BY ctime DESC LIMIT 1 for update; ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction | show ENGINE INNODB status: |
------------------------
LATEST DETECTED DEADLOCK
------------------------
2022-05-13 00:52:45 0x700007333000
*** (1) TRANSACTION:
TRANSACTION 35260, ACTIVE 11 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 3 lock struct(s), heap size 1136, 2 row lock(s)
MySQL thread id 46, OS thread handle 123145423663104, query id 627 localhost root Sending data
select * from campaign_daily_balance_tab_20220101 where campaignid=13 and 900>=ctime ORDER BY ctime DESC LIMIT 1 for update
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 36 page no 4 n bits 104 index campaignid of table `entry_task`.`campaign_daily_balance_tab_20220101` trx id 35260 lock_mode X waiting
Record lock, heap no 34 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
 0: len 8; hex 800000000000000c; asc         ;;
 1: len 8; hex 800000000000044c; asc        L;;
 2: len 4; hex 8000003c; asc    <;;

*** (2) TRANSACTION:
TRANSACTION 35259, ACTIVE 23 sec inserting
mysql tables in use 1, locked 1
8 lock struct(s), heap size 1136, 7 row lock(s), undo log entries 1
MySQL thread id 44, OS thread handle 123145423106048, query id 629 localhost root update
INSERT INTO campaign_daily_balance_tab_20220101 (campaignid,ctime) VALUES(14,991)
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 36 page no 4 n bits 104 index campaignid of table `entry_task`.`campaign_daily_balance_tab_20220101` trx id 35259 lock_mode X
Record lock, heap no 34 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
 0: len 8; hex 800000000000000c; asc         ;;
 1: len 8; hex 800000000000044c; asc        L;;
 2: len 4; hex 8000003c; asc    <;;

*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 36 page no 4 n bits 104 index campaignid of table `entry_task`.`campaign_daily_balance_tab_20220101` trx id 35259 lock_mode X locks gap before rec insert intention waiting
Record lock, heap no 4 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
 0: len 8; hex 800000000000000e; asc         ;;
 1: len 8; hex 80000000000003e7; asc         ;;
 2: len 4; hex 80000043; asc    C;;

*** WE ROLL BACK TRANSACTION (1)
------------
TRANSACTIONS
------------
Trx id counter 35265
Purge done for trx's n:o < 35265 undo n:o < 0 state: running but idle
History list length 2
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 421984974412472, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 35259, ACTIVE 27 sec
9 lock struct(s), heap size 1136, 9 row lock(s), undo log entries 1
MySQL thread id 44, OS thread handle 123145423106048, query id 630 localhost root starting
show ENGINE INNODB status
--------
FILE I/O
--------
I/O thread 0 state: waiting for i/o request (insert buffer thread)
I/O thread 1 state: waiting for i/o request (log thread)
I/O thread 2 state: waiting for i/o request (read thread)
I/O thread 3 state: waiting for i/o request (read thread)
I/O thread 4 state: waiting for i/o request (read thread)
I/O thread 5 state: waiting for i/o request (read thread)
I/O thread 6 state: waiting for i/o request (write thread)
I/O thread 7 state: waiting for i/o request (write thread)
I/O thread 8 state: waiting for i/o request (write thread)
I/O thread 9 state: waiting for i/o request (write thread)
Pending normal aio reads: [0, 0, 0, 0] , aio writes: [0, 0, 0, 0] ,
 ibuf aio reads:, log i/o's:, sync i/o's:
Pending flushes (fsync) log: 0; buffer pool: 0
385 OS file reads, 1232 OS file writes, 939 OS fsyncs
0.00 reads/s, 0 avg bytes/read, 0.44 writes/s, 0.35 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 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 1 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
Hash table size 34679, node heap has 0 buffer(s)
0.39 hash searches/s, 0.33 non-hash searches/s
---
LOG
---
Log sequence number 2681687751
Log flushed up to   2681687751
Pages flushed up to 2681687751
Last checkpoint at  2681687742
0 pending log flushes, 0 pending chkp writes
575 log i/o's done, 0.20 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total large memory allocated 137428992
Dictionary memory allocated 118621
Buffer pool size   8192
Free buffers       7838
Database pages     353
Old database pages 0
Modified db pages  0
Pending reads      0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 293, created 60, written 527
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 353, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
0 read views open inside InnoDB
Process ID=89536, Main thread ID=123145417719808, state: sleeping
Number of rows inserted 44, updated 0, deleted 30, read 720
0.02 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.02 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT
============================
| **analyse:** session1 and session2 should have same gap lock in (12,15]. ** A gap lock does not exclude other gap locks, but it excludes insertion intent locks.(so don't know why block in session2)?** if session2 block and wait for gap lock. should not have dead lock. refer to article: [http://mysql.taobao.org/monthly/2022/02/01/](http://mysql.taobao.org/monthly/2022/02/01/) should be a Mysql bug. case2: | session1: begin; select * from campaign_daily_balance_tab_20220101 where campaignid=9 and 900------------------------ LATEST DETECTED DEADLOCK ------------------------ 2022-05-13 02:44:08 0x700007333000 *** (1) TRANSACTION: TRANSACTION 35317, ACTIVE 70 sec inserting mysql tables in use 1, locked 1 LOCK WAIT 4 lock struct(s), heap size 1136, 4 row lock(s), undo log entries 1 MySQL thread id 64, OS thread handle 123145423384576, query id 791 localhost root update INSERT INTO campaign_daily_balance_tab_20220101 (campaignid,ctime) VALUES(8,900) *** (1) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 36 page no 4 n bits 104 index campaignid of table `entry_task`.`campaign_daily_balance_tab_20220101` trx id 35317 lock_mode X locks gap before rec insert intention waiting Record lock, heap no 32 PHYSICAL RECORD: n_fields 3; compact format; info bits 0 0: len 8; hex 800000000000000a; asc ;; 1: len 8; hex 800000000000044c; asc L;; 2: len 4; hex 8000003a; asc :;; *** (2) TRANSACTION: TRANSACTION 35319, ACTIVE 17 sec inserting mysql tables in use 1, locked 1 3 lock struct(s), heap size 1136, 2 row lock(s), undo log entries 1 MySQL thread id 62, OS thread handle 123145423106048, query id 792 localhost root update INSERT INTO campaign_daily_balance_tab_20220101 (campaignid,ctime) VALUES(9,900) *** (2) HOLDS THE LOCK(S): RECORD LOCKS space id 36 page no 4 n bits 104 index campaignid of table `entry_task`.`campaign_daily_balance_tab_20220101` trx id 35319 lock_mode X locks gap before rec Record lock, heap no 32 PHYSICAL RECORD: n_fields 3; compact format; info bits 0 0: len 8; hex 800000000000000a; asc ;; 1: len 8; hex 800000000000044c; asc L;; 2: len 4; hex 8000003a; asc :;; *** (2) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 36 page no 4 n bits 104 index campaignid of table `entry_task`.`campaign_daily_balance_tab_20220101` trx id 35319 lock_mode X locks gap before rec insert intention waiting Record lock, heap no 32 PHYSICAL RECORD: n_fields 3; compact format; info bits 0 0: len 8; hex 800000000000000a; asc ;; 1: len 8; hex 800000000000044c; asc L;; 2: len 4; hex 8000003a; asc :;; *** WE ROLL BACK TRANSACTION (2) ------------ TRANSACTIONS ------------ Trx id counter 35324 Purge done for trx's n:o < 35324 undo n:o < 0 state: running but idle History list length 0 LIST OF TRANSACTIONS FOR EACH SESSION: ---TRANSACTION 421984974411568, not started 0 lock struct(s), heap size 1136, 0 row lock(s) ---TRANSACTION 35317, ACTIVE 289 sec 4 lock struct(s), heap size 1136, 5 row lock(s), undo log entries 1 MySQL thread id 64, OS thread handle 123145423384576, query id 794 localhost root starting show ENGINE INNODB status -------- FILE I/O -------- I/O thread 0 state: waiting for i/o request (insert buffer thread) I/O thread 1 state: waiting for i/o request (log thread) I/O thread 2 state: waiting for i/o request (read thread) I/O thread 3 state: waiting for i/o request (read thread) I/O thread 4 state: waiting for i/o request (read thread) I/O thread 5 state: waiting for i/o request (read thread) I/O thread 6 state: waiting for i/o request (write thread) I/O thread 7 state: waiting for i/o request (write thread) I/O thread 8 state: waiting for i/o request (write thread) I/O thread 9 state: waiting for i/o request (write thread) Pending normal aio reads: [0, 0, 0, 0] , aio writes: [0, 0, 0, 0] , ibuf aio reads:, log i/o's:, sync i/o's: Pending flushes (fsync) log: 0; buffer pool: 0 386 OS file reads, 1707 OS file writes, 1315 OS fsyncs 0.00 reads/s, 0 avg bytes/read, 0.00 writes/s, 0.00 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 34679, node heap has 0 buffer(s) Hash table size 34679, node heap has 0 buffer(s) Hash table size 34679, node heap has 0 buffer(s) Hash table size 34679, node heap has 0 buffer(s) Hash table size 34679, node heap has 1 buffer(s) Hash table size 34679, node heap has 0 buffer(s) Hash table size 34679, node heap has 0 buffer(s) Hash table size 34679, node heap has 0 buffer(s) 0.00 hash searches/s, 0.00 non-hash searches/s --- LOG --- Log sequence number 2681724594 Log flushed up to 2681724594 Pages flushed up to 2681724594 Last checkpoint at 2681724585 0 pending log flushes, 0 pending chkp writes 804 log i/o's done, 0.00 log i/o's/second ---------------------- BUFFER POOL AND MEMORY ---------------------- Total large memory allocated 137428992 Dictionary memory allocated 118621 Buffer pool size 8192 Free buffers 7823 Database pages 368 Old database pages 0 Modified db pages 0 Pending reads 0 Pending writes: LRU 0, flush list 0, single page 0 Pages made young 0, not young 0 0.00 youngs/s, 0.00 non-youngs/s Pages read 294, created 74, written 725 0.00 reads/s, 0.00 creates/s, 0.00 writes/s No buffer pool page gets since the last printout Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s LRU len: 368, unzip_LRU len: 0 I/O sum[0]:cur[0], unzip sum[0]:cur[0] -------------- ROW OPERATIONS -------------- 0 queries inside InnoDB, 0 queries in queue 0 read views open inside InnoDB Process ID=89536, Main thread ID=123145417719808, state: sleeping Number of rows inserted 51, updated 0, deleted 33, read 770 0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.00 reads/s ---------------------------- END OF INNODB MONITOR OUTPUT

你可能感兴趣的:(dead lock)