Commit 5d49a2ad authored by Iaroslav Babanin's avatar Iaroslav Babanin Committed by Marko Mäkelä

MDEV-33935 fix deadlock counter

- The deadlock counter was moved from
Deadlock::find_cycle into Deadlock::report, because
the find_cycle method is called multiple times during deadlock
detection flow, which means it shouldn't have such side effects.
But report() can, which called only once for
a victim transaction.
- Also the deadlock_detect.test and *.result test case
has been extended to handle the fix.
parent ee974ca5
...@@ -3,7 +3,12 @@ CREATE TABLE t1( ...@@ -3,7 +3,12 @@ CREATE TABLE t1(
id INT, id INT,
PRIMARY KEY(id) PRIMARY KEY(id)
) ENGINE=InnoDB; ) ENGINE=InnoDB;
CREATE TABLE dl(
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
cnt INT UNSIGNED
) ENGINE=InnoDB;
INSERT INTO t1 VALUES(1), (2), (3); INSERT INTO t1 VALUES(1), (2), (3);
INSERT INTO dl(cnt) SELECT variable_value FROM information_schema.global_status WHERE variable_name LIKE 'Innodb_deadlocks';
BEGIN; BEGIN;
SELECT * FROM t1 WHERE id = 1 LOCK IN SHARE MODE; SELECT * FROM t1 WHERE id = 1 LOCK IN SHARE MODE;
connect con1,localhost,root,,; connect con1,localhost,root,,;
...@@ -20,5 +25,8 @@ disconnect con1; ...@@ -20,5 +25,8 @@ disconnect con1;
ROLLBACK; ROLLBACK;
disconnect con2; disconnect con2;
connection default; connection default;
'Deadlock counter is valid';
1
ROLLBACK; ROLLBACK;
DROP TABLE t1; DROP TABLE t1;
DROP TABLE dl;
...@@ -14,7 +14,14 @@ CREATE TABLE t1( ...@@ -14,7 +14,14 @@ CREATE TABLE t1(
PRIMARY KEY(id) PRIMARY KEY(id)
) ENGINE=InnoDB; ) ENGINE=InnoDB;
CREATE TABLE dl(
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
cnt INT UNSIGNED
) ENGINE=InnoDB;
INSERT INTO t1 VALUES(1), (2), (3); INSERT INTO t1 VALUES(1), (2), (3);
# Preserve the initial value of the deadlock counter
INSERT INTO dl(cnt) SELECT variable_value FROM information_schema.global_status WHERE variable_name LIKE 'Innodb_deadlocks';
# We are not interested query results, only errors # We are not interested query results, only errors
--disable_result_log --disable_result_log
...@@ -61,6 +68,7 @@ disconnect con2; ...@@ -61,6 +68,7 @@ disconnect con2;
# and does the update. # and does the update.
# #
connection default; connection default;
--let $valid_deadlock_cnt= 1
if (!$have_deadlock) { if (!$have_deadlock) {
--error 0,ER_LOCK_WAIT_TIMEOUT --error 0,ER_LOCK_WAIT_TIMEOUT
reap; reap;
...@@ -68,12 +76,21 @@ reap; ...@@ -68,12 +76,21 @@ reap;
if ($have_deadlock) { if ($have_deadlock) {
--error 0,ER_LOCK_DEADLOCK --error 0,ER_LOCK_DEADLOCK
reap; reap;
--disable_query_log
INSERT INTO dl(cnt) SELECT variable_value FROM information_schema.global_status WHERE variable_name LIKE 'Innodb_deadlocks';
set @init_deadlock_cnt = (SELECT min(k.cnt) FROM dl k);
--let $valid_deadlock_cnt= `SELECT (max(t.cnt-@init_deadlock_cnt) = 1) FROM dl t`
--enable_query_log
} }
# Indicates that the deadlock counter works well.
# Use the default =1 where is no deadlock detection,
# to enable unconditional check.
--echo 'Deadlock counter is valid';
--echo $valid_deadlock_cnt
ROLLBACK; ROLLBACK;
--enable_result_log --enable_result_log
DROP TABLE t1; DROP TABLE t1;
DROP TABLE dl;
--source include/wait_until_count_sessions.inc --source include/wait_until_count_sessions.inc
...@@ -668,7 +668,6 @@ struct export_var_t{ ...@@ -668,7 +668,6 @@ struct export_var_t{
ulint innodb_data_reads; /*!< I/O read requests */ ulint innodb_data_reads; /*!< I/O read requests */
ulint innodb_dblwr_pages_written; /*!< srv_dblwr_pages_written */ ulint innodb_dblwr_pages_written; /*!< srv_dblwr_pages_written */
ulint innodb_dblwr_writes; /*!< srv_dblwr_writes */ ulint innodb_dblwr_writes; /*!< srv_dblwr_writes */
ulint innodb_deadlocks;
ulint innodb_history_list_length; ulint innodb_history_list_length;
ulint innodb_log_waits; /*!< srv_log_waits */ ulint innodb_log_waits; /*!< srv_log_waits */
ulint innodb_log_write_requests; /*!< srv_log_write_requests */ ulint innodb_log_write_requests; /*!< srv_log_write_requests */
......
...@@ -296,7 +296,6 @@ namespace Deadlock ...@@ -296,7 +296,6 @@ namespace Deadlock
if (tortoise == hare) if (tortoise == hare)
{ {
ut_ad(l > 1); ut_ad(l > 1);
lock_sys.deadlocks++;
/* Note: Normally, trx should be part of any deadlock cycle /* Note: Normally, trx should be part of any deadlock cycle
that is found. However, if innodb_deadlock_detect=OFF had been that is found. However, if innodb_deadlock_detect=OFF had been
in effect in the past, it is possible that trx will be waiting in effect in the past, it is possible that trx will be waiting
...@@ -6825,6 +6824,7 @@ and less modified rows. Bit 0 is used to prefer orig_trx in case of a tie. ...@@ -6825,6 +6824,7 @@ and less modified rows. Bit 0 is used to prefer orig_trx in case of a tie.
if (!cycle) if (!cycle)
goto func_exit; /* One of the transactions was already aborted. */ goto func_exit; /* One of the transactions was already aborted. */
lock_sys.deadlocks++;
victim= cycle; victim= cycle;
undo_no_t victim_weight= calc_victim_weight(victim, trx); undo_no_t victim_weight= calc_victim_weight(victim, trx);
unsigned victim_pos= l; unsigned victim_pos= l;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment