使用多个线程同时插入时出现死锁

时间:2019-06-22 10:19:55

标签: mysql laravel

我有一个Laravel应用程序,它接收来自Webhook的响应。数据是来自支付网关的交易信息。在一天中的某些时候,我可以在几秒钟内收到数百个回复。

我将所有响应放入队列,然后进行处理。 Laravel让我可以选择指定可以同时处理给定队列的最大工人数。我将此设置为10,但是出现很多死锁错误。

我要做的就是将数据插入表中。我唯一的主键/唯一键是“ id”,它随每次插入而自动递增。

我不明白为什么会发生这些死锁,因为这是一个简单的插入,其中表只有1个唯一键。

Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction

以下是插入内容:

DB::transaction(function() use ($decode, $status, $c_decode, $m_decode, $d_decode) {

    if($decode->type == 'REGISTRATION' && $decode->action == 'CREATED' && !isset($decode->payload->referencedId)) {
        $paymentType = 'RG';
    }
    else if($decode->type == 'REGISTRATION' && $decode->action == 'CREATED' && isset($decode->payload->referencedId)) {
        $paymentType = 'CF';
    }
    else if($decode->type == 'REGISTRATION' && $decode->action == 'UPDATED') {
        $paymentType = 'RR';
    }
    else if($decode->type == 'REGISTRATION' && $decode->action == 'DELETED') {
        $paymentType = 'DR';
    }
    else {
        $paymentType = $decode->payload->paymentType;
    }

    try {
        DB::connection('mysql2')->statement('INSERT INTO transactions
        (`json`,
            `uuid`,
            `type`,
            `status`,
            `holder`,
            `bin`,
            `last4Digits`,
            `expiryMonth`,
            `expiryYear`,
            `amount`,
            `currency`,
            `resultCode`,
            `processingTime`,
            `paymentType`,
            `threeDSecure`,
            `paymentBrand`,
            `recurringType`,
            `clearingInstitute`,
            `merchantTransactionId`,
            `referencedId`,
            `registrationId`,
            `divisionName`,
            `divisionUuid`,
            `merchantName`,
            `merchantUuid`,
            `channelName`,
            `channelUuid`,
            `channelLogin`,
            `channelPwd`)
        VALUES
        (:json,
            :uuid,
            :type,
            :status,
            :holder,
            :bin,
            :last4Digits,
            :expiryMonth,
            :expiryYear,
            :amount,
            :currency,
            :resultCode,
            :processingTime,
            :paymentType,
            :threeDSecure,
            :paymentBrand,
            :recurringType,
            :clearingInstitute,
            :merchantTransactionId,
            :referencedId,
            :registrationId,
            :divisionName,
            :divisionUuid,
            :merchantName,
            :merchantUuid,
            :channelName,
            :channelUuid,
            :channelLogin,
            :channelPwd)',
            array('json' => $this->transaction,
                'uuid' => $decode->payload->id,
                'type' => $decode->type,
                'status' => $status->status,
                'holder' => $decode->payload->card->holder ?? null,
                'bin' => $decode->payload->card->bin ?? null,
                'last4Digits' => $decode->payload->card->last4Digits ?? null,
                'expiryMonth' => $decode->payload->card->expiryMonth ?? null,
                'expiryYear' => $decode->payload->card->expiryYear ?? null,
                'amount' => isset($decode->payload->presentationAmount) ? $decode->payload->presentationAmount : $decode->payload->amount ?? null,
                'currency' => isset($decode->payload->presentationCurrency) ? $decode->payload->presentationCurrency : $decode->payload->currency ?? null,
                'resultCode' => $decode->payload->result->code,
                'processingTime' => $decode->payload->timestamp,
                'paymentType' => $paymentType,
                'threeDSecure' => isset($decode->payload->threeDSecure) ? 1 : 0,
                'paymentBrand' => $decode->payload->paymentBrand ?? null,
                'recurringType' => $decode->payload->recurringType ?? null,
                'clearingInstitute' => $decode->payload->resultDetails->clearingInstituteName ?? null,
                'merchantTransactionId' => $decode->payload->merchantTransactionId ?? null,
                'referencedId' => $decode->payload->referencedId ?? null,
                'registrationId' => $decode->payload->registrationId ?? null,
                'divisionName' => $d_decode->divisionInfo->name,
                'divisionUuid' => $d_decode->divisionInfo->id,
                'merchantName' => $m_decode->merchantInfo->name,
                'merchantUuid' => $m_decode->merchantInfo->id,
                'channelName' => $c_decode->channelInfo->name,
                'channelUuid' => $c_decode->channelInfo->channel,
                'channelLogin' => $c_decode->channelInfo->login,
                'channelPwd' => $c_decode->channelInfo->pwd));
    }
    catch(Exception $e) {
        Storage::prepend('transactions_insert_errors.txt', Carbon::now('UTC')->toDateTimeString()."\nUUID:".$decode->payload->id."\n".$e->getMessage()."\n\n");
        throw new Exception($e->getMessage());
    }
});

死锁信息(实际上还有更多交易,但是我不得不减少它:

=====================================
2019-06-22 11:07:49 0x7f6c4c40f700 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 6 seconds
-----------------
BACKGROUND THREAD
-----------------
srv_master_thread loops: 3534541 srv_active, 0 srv_shutdown, 1520881 srv_idle
srv_master_thread log flush and writes: 0
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 6627458
OS WAIT ARRAY INFO: signal count 40352394
RW-shared spins 11562334, rounds 11781529, OS waits 220181
RW-excl spins 9367862, rounds 24643360, OS waits 234653
RW-sx spins 51796, rounds 1390242, OS waits 43691
Spin rounds per wait: 1.02 RW-shared, 2.63 RW-excl, 26.84 RW-sx
------------------------
LATEST DETECTED DEADLOCK
------------------------
2019-06-22 11:07:32 0x7f7039fef700
*** (1) TRANSACTION:
TRANSACTION 160572950, ACTIVE 0 sec inserting
mysql tables in use 1, locked 1
LOCK WAIT 3 lock struct(s), heap size 1136, 2 row lock(s), undo log entries 1
MySQL thread id 28914713, OS thread handle 140119973230336, query id 2814121268 172.21.174.147 dbUser update
INSERT INTO transactions
                                                   (`json`,
                                                    `uuid`,
                                                    `type`,
                                                    `status`,
                                                    `holder`,
                                                    `bin`,
                                                    `last4Digits`,
                                                    `expiryMonth`,
                                                    `expiryYear`,
                                                    `amount`,
                                                    `currency`,
                                                    `resultCode`,
                                                    `processingTime`,
                                                  
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 19577 page no 64970 n bits 392 index uuidIndex of table `total_control`.`transactions` trx id 160572950 lock_mode X locks gap before rec insert intention waiting
Record lock, heap no 181 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
0: len 30; hex 386163646134613636623236643864643031366236633562393933343333; asc 8acda4a66b26d8dd016b6c5b993433; (total 32 bytes);
1: len 8; hex 80000000000008b1; asc         ;;

*** (2) TRANSACTION:
TRANSACTION 160572923, ACTIVE 2 sec fetching rows
mysql tables in use 2, locked 2
45451 lock struct(s), heap size 4874448, 819432 row lock(s)
MySQL thread id 29420919, OS thread handle 140119986075392, query id 2814120925 event_scheduler Sending data
DELETE t1 FROM transactions t1
INNER JOIN
transactions t2 
WHERE
t1.id < t2.id AND t1.uuid = t2.uuid
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 19577 page no 64970 n bits 392 index uuidIndex of table `total_control`.`transactions` trx id 160572923 lock mode S
Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
0: len 8; hex 73757072656d756d; asc supremum;;

Record lock, heap no 2 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
0: len 30; hex 386163646134613636623236643864643031366237303336323763343361; asc 8acda4a66b26d8dd016b703627c43a; (total 32 bytes);
1: len 8; hex 8000000000000398; asc         ;;

Record lock, heap no 3 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
0: len 30; hex 386163646134613636623236643864643031366237303336356466303364; asc 8acda4a66b26d8dd016b70365df03d; (total 32 bytes);
1: len 8; hex 80000000000003eb; asc         ;;

Record lock, heap no 4 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
0: len 30; hex 386163646134613636623236643864643031366237303336363463353364; asc 8acda4a66b26d8dd016b703664c53d; (total 32 bytes);
1: len 8; hex 80000000000003f4; asc         ;;

*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 19577 page no 73674 n bits 80 index PRIMARY of table `total_control`.`transactions` trx id 160572923 lock_mode X waiting
Record lock, heap no 10 PHYSICAL RECORD: n_fields 34; compact format; info bits 0
0: len 8; hex 800000000005bc89; asc         ;;
1: len 6; hex 000009922616; asc     & ;;
2: len 7; hex 81000024050110; asc    $   ;;
3: len 30; hex 000200f00512000400160007000c1d00002500747970657061796c6f6164; asc                  % typepayload; (total 1521 bytes);
4: len 4; hex 5d0dfde4; asc ]   ;;
5: SQL NULL;
6: len 30; hex 386163646134613536623236623330613031366237656137343237613130; asc 8acda4a56b26b30a016b7ea7427a10; (total 32 bytes);
7: len 7; hex 5041594d454e54; asc PAYMENT;;
8: len 1; hex 81; asc  ;;
9: len 14; hex d092d196d182d0b0d0bbd196d0b9; asc               ;;
10: len 4; hex 800833c5; asc   3 ;;
11: len 4; hex 80000a22; asc    ";;
12: len 4; hex 00000006; asc     ;;
13: len 4; hex 800007e8; asc     ;;
14: len 5; hex 8000000100; asc      ;;
15: len 3; hex 555344; asc USD;;
16: len 11; hex 3030302e3030302e303030; asc 000.000.000;;
17: len 5; hex 99a36ca1c0; asc   l  ;;
18: len 2; hex 4442; asc DB;;
19: len 1; hex 80; asc  ;;
20: len 6; hex 4d4153544552; asc MASTER;;
21: len 7; hex 494e495449414c; asc INITIAL;;
22: len 9; hex 436f6e636172646973; asc Concardis;;
23: len 7; hex 39373539363832; asc 9759682;;
24: SQL NULL;
25: len 30; hex 386163646134613436623236643864623031366237656137343136343636; asc 8acda4a46b26d8db016b7ea7416466; (total 32 bytes);
26: len 9; hex 427261696e2047796d; asc Brain Gym;;
27: len 30; hex 386163646134636236333339386232363031363333623936643961323230; asc 8acda4cb63398b2601633b96d9a220; (total 32 bytes);
28: len 17; hex 4e6561726c79204e6f726d616c20422e56; asc Nearly Normal B.V;;
29: len 30; hex 386163646134636236333339386232363031363333623961653132353230; asc 8acda4cb63398b2601633b9ae12520; (total 32 bytes);
30: len 18; hex 64617a7a6c696e67636f727465782d636f6d; asc dazzlingcortex-com;;
31: len 30; hex 386163396134636436353337613632303031363533613834646538343134; asc 8ac9a4cd6537a62001653a84de8414; (total 32 bytes);
32: len 30; hex 386163646134636236333339386232363031363333623961653132353230; asc 8acda4cb63398b2601633b9ae12520; (total 32 bytes);
33: len 10; hex 5a62534d347065474a32; asc ZbSM4peGJ2;;

*** WE ROLL BACK TRANSACTION (1)
------------
TRANSACTIONS
------------
Trx id counter 160573112
Purge done for trx's n:o < 160573111 undo n:o < 0 state: running but idle
History list length 5
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 421595998864088, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421595998861328, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421595998865008, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421595998852128, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421595998863168, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421595998862248, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421595998860408, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421595998859488, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421595998858568, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421595998857648, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421595998856728, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421595998855808, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421595998854888, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421595998853968, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421595998853048, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421595998851208, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421595998850288, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421595998846608, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421595998849368, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421595998848448, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421595998847528, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421595998845688, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421595998844768, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
--------
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] , aio writes: [0, 0, 0, 0] ,
ibuf aio reads:, log i/o's:, sync i/o's:
Pending flushes (fsync) log: 0; buffer pool: 84
212407375 OS file reads, 1522769173 OS file writes, 310744172 OS fsyncs
0.00 reads/s, 0 avg bytes/read, 54.99 writes/s, 9.33 fsyncs/s
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 686, seg size 688, 509164 merges
merged operations:
insert 852939, delete mark 130857, delete 23044
discarded operations:
insert 7, delete mark 0, delete 0
Hash table size 53124517, node heap has 18519 buffer(s)
Hash table size 53124517, node heap has 4038 buffer(s)
Hash table size 53124517, node heap has 1084 buffer(s)
Hash table size 53124517, node heap has 1548 buffer(s)
Hash table size 53124517, node heap has 23473 buffer(s)
Hash table size 53124517, node heap has 4386 buffer(s)
Hash table size 53124517, node heap has 27676 buffer(s)
Hash table size 53124517, node heap has 23708 buffer(s)
295.95 hash searches/s, 45.66 non-hash searches/s
---
LOG
---
Log sequence number          529089521147
Log buffer assigned up to    529089521147
Log buffer completed up to   529089521147
Log written up to            529089521147
Log flushed up to            529089521147
Added dirty pages up to      529089521147
Pages flushed up to          529088794969
Last checkpoint at           529088794969
1381131647 log i/o's done, 6.83 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total large memory allocated 219886387200
Dictionary memory allocated 13528764
Buffer pool size   13107200
Free buffers       10793
Database pages     12991975
Old database pages 4795845
Modified db pages  333
Pending reads      0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 339364028, not young 2141628637
0.00 youngs/s, 0.00 non-youngs/s
Pages read 212396969, created 19645989, written 133237163
0.00 reads/s, 0.33 creates/s, 46.66 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: 12991975, unzip_LRU len: 0
I/O sum[1328]:cur[200], unzip sum[0]:cur[0]
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
0 read views open inside InnoDB
Process ID=5732, Main thread ID=140120583948032 , state=sleeping
Number of rows inserted 325000214, updated 29685659, deleted 1989864, read 1203522511402
0.67 inserts/s, 1.33 updates/s, 0.00 deletes/s, 879.52 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT
============================

表架构:

CREATE TABLE transactions (
    id BIGINT(20) AUTO_INCREMENT PRIMARY KEY,
    json JSON NOT NULL,
    createdAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updatedAt TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    uuid VARCHAR(32) NOT NULL,
    type VARCHAR(12),
    status TINYINT(1),
    holder VARCHAR(32),
    bin INT(6),
    last4Digits INT(4),
    expiryMonth INT(2) UNSIGNED ZEROFILL,
    expiryYear INT(4),
    amount DECIMAL(11,2),
    currency VARCHAR(3),
    resultCode VARCHAR(11),
    processingTime DATETIME,
    paymentType VARCHAR(2),
    threeDSecure TINYINT(1),
    paymentBrand VARCHAR(32),
    recurringType VARCHAR(8),
    clearingInstitute VARCHAR(32),
    merchantTransactionId VARCHAR(64),
    referencedId VARCHAR(32),
    registrationId VARCHAR(32),
    divisionName VARCHAR(32),
    divisionUuid VARCHAR(32),
    merchantName VARCHAR(32),
    merchantUuid VARCHAR(32),
    channelName VARCHAR(32),
    channelUuid VARCHAR(32),
    channelLogin VARCHAR(32),
    channelPwd VARCHAR(32),
    INDEX createdAtIndex (createdAt),
    INDEX uuidIndex (uuid),
    INDEX typeIndex (type),
    INDEX statusIndex (status),
    INDEX holderIndex (holder),
    INDEX binIndex (bin),
    INDEX last4DigitsIndex (last4Digits),
    INDEX expiryMonthIndex (expiryMonth),
    INDEX expiryYearIndex (expiryYear),
    INDEX amountIndex (amount),
    INDEX resultCodeIndex (resultCode),
    INDEX processingTimeIndex (processingTime),
    INDEX paymentTypeIndex (paymentType),
    INDEX threeDSecureIndex (threeDSecure),
    INDEX paymentBrandIndex (paymentBrand),
    INDEX recurringTypeIndex (recurringType),
    INDEX clearingInstituteIndex (clearingInstitute),
    INDEX merchantTransactionIdIndex (merchantTransactionId),
    INDEX divisonNameIndex (divisionName),
    INDEX divisionUuidIndex (divisionUuid),
    INDEX merchantNameIndex (merchantName),
    INDEX merchantUuidindex (merchantUuid),
    INDEX channelNameIndex (channelName),
    INDEX channelUuidIndex (channelUuid)
) ENGINE=INNODB;

0 个答案:

没有答案