Debian Codegraph @

mysql-test/suite/innodb/t/deadlock_victim_race.test

raw
1 --source include/have_innodb.inc
2 --source include/have_debug_sync.inc
3 --source include/count_sessions.inc
4
5 --disable_query_log
6 call mtr.add_suppression("InnoDB: Transaction was aborted due to ");
7 --enable_query_log
8
9 --connect(cancel_purge,localhost,root,,)
10 # Purge can cause deadlock in the test, requesting page's RW_X_LATCH for trx
11 # ids reseting, after trx 2 acqured RW_S_LATCH and suspended in debug sync point
12 # lock_trx_handle_wait_enter, waiting for upd_cont signal, which must be
13 # emitted after the last SELECT in this test. The last SELECT will hang waiting
14 # for purge RW_X_LATCH releasing, and trx 2 will be rolled back by timeout.
15 # The last SELECT will then be successfully executed instead of finishing by
16 # lock wait timeout.
17 START TRANSACTION WITH CONSISTENT SNAPSHOT;
18
19 --connection default
20 CREATE TABLE t (a int PRIMARY KEY, b int) engine = InnoDB;
21 CREATE TABLE t2 (a int PRIMARY KEY) engine = InnoDB;
22
23 INSERT INTO t VALUES (10, 10), (20, 20), (30, 30);
24 INSERT INTO t2 VALUES (10), (20), (30);
25
26 BEGIN; # trx 1
27 SELECT * FROM t WHERE a = 20 FOR UPDATE;
28 # Locking order:
29 # (10,10) (20,20) (30,30)
30 # ^
31 # trx 1
32
33 --connect(con_2,localhost,root,,)
34 SET innodb_snapshot_isolation=OFF;
35 # RC is neccessary to do semi-consistent read
36 SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
37 BEGIN; # trx 2
38 # The first time it will be hit on trying to lock (20,20), the second hit
39 # will be on (30,30).
40 SET DEBUG_SYNC = 'lock_trx_handle_wait_enter SIGNAL upd_locked WAIT_FOR upd_cont EXECUTE 2';
41 # We must not modify primary key fields to cause rr_sequential() read record
42 # function choosing in mysql_update(), i.e. both query_plan.using_filesort and
43 # query_plan.using_io_buffer must be false during init_read_record() call.
44 --send UPDATE t SET b = 100
45
46 --connect(con_3,localhost,root,,)
47 BEGIN; # trx 3
48 # The following update is necessary to increase the transaction weight, which is
49 # calculated as the number of locks + the number of undo records during deadlock
50 # report. Victim's transaction should have minimum weight. We need trx 2 to be
51 # choosen as victim, that's why we need to increase the current transaction
52 # weight.
53 UPDATE t2 SET a = a + 100;
54 SELECT * FROM t WHERE a = 30 FOR UPDATE;
55 SET DEBUG_SYNC='now WAIT_FOR upd_locked';
56 # Locking queue:
57 # (10,10) (20,20) (30,30)
58 # ^ ^ ^
59 # trx 2 trx 1 trx 3
60 # trx 2 (waiting for 1)
61
62 SET DEBUG_SYNC = 'lock_wait_start SIGNAL sel_locked';
63 --send SELECT * FROM t WHERE a = 20 FOR UPDATE
64 --connection default
65 SET DEBUG_SYNC='now WAIT_FOR sel_locked';
66 # Locking queue:
67 # (10,10) (20,20) (30,30)
68 # ^ ^ ^
69 # trx 2 trx 1 trx 3
70 # trx 2 (waiting for 1)
71 # trx 3 (waiting for 1)
72 #
73 # Note trx 1 must grant lock to trx2 before trx 2 checks the lock state in
74 # lock_trx_handle_wait(), i.e. the function must return DB_SUCCESS, that's why
75 # the following ROLLBACK must be executed before sending upd_cont signal.
76 ROLLBACK;
77 SET DEBUG_SYNC='now SIGNAL upd_cont';
78
79 SET DEBUG_SYNC="now WAIT_FOR upd_locked";
80 # Locking queue:
81 # (10,10) (20,20) (30,30)
82 # ^ ^ ^
83 # trx 2 trx 2 trx 3
84 # trx 3 (waiting for 2) trx 2 (waiting for 3)
85 #
86 # Deadlock happened after trx 1 granted lock to trx 2, and trx2 continued
87 # sequential read (with rr_sequential() read record function), and requested
88 # lock on (30,30). But the deadlock has not been determined yet.
89
90 SET SESSION innodb_lock_wait_timeout=1;
91 --error ER_LOCK_WAIT_TIMEOUT
92 # The deadlock will be determined in lock_wait() after lock wait timeout
93 # expired.
94 SELECT * FROM t WHERE a = 10 FOR UPDATE;
95 SET DEBUG_SYNC="now SIGNAL upd_cont";
96
97 --connection con_3
98 --reap
99
100 --connection con_2
101 # As lock_trx_handle_wait() wrongly returned DB_SUCCESS instead of
102 # DB_DEADLOCK, row_search_mvcc() of trx 2 behaves so as if (30,30) was locked.
103 # But the waiting(for trx 3) lock was cancelled by deadlock checker after
104 # trx 2 was choosen as a victim (see lock_cancel_waiting_and_release() call
105 # from Deadlock::report() for details). The try to update non-locked record
106 # will cause assertion if the bug is not fixed.
107 --error ER_LOCK_DEADLOCK
108 --reap
109
110 --disconnect con_3
111 --disconnect con_2
112
113 --connection default
114 SET DEBUG_SYNC = 'RESET';
115 DROP TABLE t;
116 DROP TABLE t2;
117 --disconnect cancel_purge
118 --source include/wait_until_count_sessions.inc