| 1 |
--source include/have_binlog_format_statement.inc |
| 2 |
--source include/have_innodb.inc |
| 3 |
--source include/master-slave.inc |
| 4 |
|
| 5 |
--echo MDEV-31482: Lock wait timeout with INSERT-SELECT, autoinc, and statement-based replication |
| 6 |
|
| 7 |
# The scenario is transactions T1, T2, T3: |
| 8 |
# |
| 9 |
# T1 is waiting for T3 on an autoinc lock |
| 10 |
# T2 is waiting for T1 to commit |
| 11 |
# T3 is waiting on a normal row lock held by T2 |
| 12 |
# |
| 13 |
# This caused a hang until innodb_lock_wait_timeout, because autoinc |
| 14 |
# locks were not reported to the in-order parallel replication, so T3 |
| 15 |
# was not deadlock killed. |
| 16 |
|
| 17 |
--let $lock_wait_timeout=20 |
| 18 |
|
| 19 |
--let $rpl_connection_name= slave2 |
| 20 |
--let $rpl_server_number= 2 |
| 21 |
--source include/rpl_connect.inc |
| 22 |
|
| 23 |
--let $rpl_connection_name= slave3 |
| 24 |
--let $rpl_server_number= 2 |
| 25 |
--source include/rpl_connect.inc |
| 26 |
|
| 27 |
--connection master |
| 28 |
ALTER TABLE mysql.gtid_slave_pos ENGINE=InnoDB; |
| 29 |
|
| 30 |
# A table as destination for INSERT-SELECT |
| 31 |
CREATE TABLE t1 (a INT PRIMARY KEY AUTO_INCREMENT, b INT, c INT, INDEX (c)) ENGINE=InnoDB; |
| 32 |
INSERT INTO t1 (b,c) VALUES (0, 1), (0, 1), (0, 2), (0,3), (0, 5), (0, 7), (0, 8); |
| 33 |
|
| 34 |
# A table as source for INSERT-SELECT. |
| 35 |
CREATE TABLE t2 (a INT PRIMARY KEY, b INT) ENGINE=InnoDB; |
| 36 |
INSERT INTO t2 VALUES (10,1), (20,2), (30,3), (40,4), (50,5); |
| 37 |
|
| 38 |
# A table to help order slave worker threads to setup the desired scenario. |
| 39 |
CREATE TABLE t3 (a VARCHAR(20) PRIMARY KEY, b INT) ENGINE=InnoDB; |
| 40 |
INSERT INTO t3 VALUES ('row for T1', 0), ('row for T2', 0), ('row for T3', 0); |
| 41 |
--source include/save_master_gtid.inc |
| 42 |
|
| 43 |
--connection slave |
| 44 |
--source include/sync_with_master_gtid.inc |
| 45 |
--source include/stop_slave.inc |
| 46 |
--let $save_innodb_lock_wait_timeout= `SELECT @@global.innodb_lock_wait_timeout` |
| 47 |
--let $save_slave_parallel_threads= `SELECT @@global.slave_parallel_threads` |
| 48 |
--let $save_slave_parallel_mode= `SELECT @@global.slave_parallel_mode` |
| 49 |
set @@global.slave_parallel_threads= 3; |
| 50 |
set @@global.slave_parallel_mode= OPTIMISTIC; |
| 51 |
eval set @@global.innodb_lock_wait_timeout= $lock_wait_timeout; |
| 52 |
|
| 53 |
--connection master |
| 54 |
# Transaction T1. |
| 55 |
BEGIN; |
| 56 |
UPDATE t3 SET b=b+1 where a="row for T1"; |
| 57 |
INSERT INTO t1(b, c) SELECT 1, t2.b FROM t2 WHERE a=10; |
| 58 |
COMMIT; |
| 59 |
|
| 60 |
# Transaction T2. |
| 61 |
DELETE FROM t1 WHERE c >= 4 and c < 6; |
| 62 |
|
| 63 |
# Transaction T3. |
| 64 |
BEGIN; |
| 65 |
UPDATE t3 SET b=b+1 where a="row for T3"; |
| 66 |
INSERT INTO t1(b, c) SELECT 3, t2.b FROM t2 WHERE a >= 20 AND a <= 40; |
| 67 |
COMMIT; |
| 68 |
|
| 69 |
--source include/save_master_gtid.inc |
| 70 |
|
| 71 |
--connection slave1 |
| 72 |
# Temporarily block T1 to create the scheduling that triggers the bug. |
| 73 |
BEGIN; |
| 74 |
SELECT * FROM t3 WHERE a="row for T1" FOR UPDATE; |
| 75 |
|
| 76 |
--connection slave2 |
| 77 |
# Temporarily block T3 from starting (so T2 can reach commit). |
| 78 |
BEGIN; |
| 79 |
SELECT * FROM t3 WHERE a="row for T3" FOR UPDATE; |
| 80 |
|
| 81 |
--connection slave3 |
| 82 |
# This critical step blocks T3 after it has inserted its first row, |
| 83 |
# and thus taken the auto-increment lock, but before it has reached |
| 84 |
# the point where it gets a row lock wait on T2. Even though |
| 85 |
# auto-increment lock waits were not reported due to the bug, |
| 86 |
# transitive lock waits (T1 waits on autoinc of T3 which waits on row |
| 87 |
# on T2) _were_ reported as T1 waiting on T2, and thus a deadlock kill |
| 88 |
# happened and the bug was not triggered. |
| 89 |
BEGIN; |
| 90 |
DELETE FROM t2 WHERE a=30; |
| 91 |
|
| 92 |
--connection slave |
| 93 |
--source include/start_slave.inc |
| 94 |
|
| 95 |
# First let T2 complete until it is waiting for T1 to commit. |
| 96 |
--let $wait_condition= SELECT count(*)=1 FROM information_schema.processlist WHERE state='Waiting for prior transaction to commit' and command LIKE 'Slave_worker'; |
| 97 |
--source include/wait_condition.inc |
| 98 |
|
| 99 |
# Then let T3 reach the point where it has obtained the autoinc lock, |
| 100 |
# but it is not yet waiting for a row lock held by T2. |
| 101 |
--connection slave2 |
| 102 |
ROLLBACK; |
| 103 |
--let $wait_condition= SELECT count(*)=1 FROM information_schema.processlist WHERE state='Sending data' and info LIKE 'INSERT INTO t1(b, c) SELECT 3, t2.b%' and time_ms > 500 and command LIKE 'Slave_worker'; |
| 104 |
--source include/wait_condition.inc |
| 105 |
|
| 106 |
# Now let T1 continue, while T3 is holding the autoinc lock but before |
| 107 |
# it is waiting for T2. Wait a short while to give the hang a chance to |
| 108 |
# happen; T1 needs to get to request the autoinc lock before we let T3 |
| 109 |
# continue. (There's a small chance the sleep will be too small, which will |
| 110 |
# let the test occasionally pass on non-fixed server). |
| 111 |
--connection slave1 |
| 112 |
ROLLBACK; |
| 113 |
--sleep 0.5 |
| 114 |
|
| 115 |
# Now let T3 continue; the bug was that this lead to an undetected |
| 116 |
# deadlock that remained until innodb lock wait timeout. |
| 117 |
--connection slave3 |
| 118 |
ROLLBACK; |
| 119 |
|
| 120 |
--connection slave |
| 121 |
--let $slave_timeout= `SELECT $lock_wait_timeout/2` |
| 122 |
--source include/sync_with_master_gtid.inc |
| 123 |
--let $slave_timeout= |
| 124 |
SELECT * FROM t1 ORDER BY a; |
| 125 |
SELECT * FROM t2 ORDER BY a; |
| 126 |
SELECT * FROM t3 ORDER BY a; |
| 127 |
|
| 128 |
# Cleanup. |
| 129 |
--connection master |
| 130 |
CALL mtr.add_suppression("Unsafe statement written to the binary log using statement format"); |
| 131 |
DROP TABLE t1, t2, t3; |
| 132 |
|
| 133 |
--connection slave |
| 134 |
--source include/stop_slave.inc |
| 135 |
eval SET @@global.slave_parallel_threads= $save_slave_parallel_threads; |
| 136 |
eval SET @@global.slave_parallel_mode= $save_slave_parallel_mode; |
| 137 |
eval SET @@global.innodb_lock_wait_timeout= $save_innodb_lock_wait_timeout; |
| 138 |
--source include/start_slave.inc |
| 139 |
SELECT @@GLOBAL.innodb_autoinc_lock_mode; |
| 140 |
--source include/rpl_end.inc |