mariadb/mysql-test/suite/innodb/t/xa_prepare_unlock_unmodified.test
Vlad Lesin 8c7786e7d5 MDEV-34690 lock_rec_unlock_unmodified() causes deadlock
lock_rec_unlock_unmodified() is executed either under lock_sys.wr_lock()
or under a combination of lock_sys.rd_lock() + record locks hash table
cell latch. It also requests page latch to check if locked records were
changed by the current transaction or not.

Usually InnoDB requests page latch to find the certain record on the
page, and then requests lock_sys and/or record lock hash cell latch to
request record lock. lock_rec_unlock_unmodified() requests the latches
in the opposite order, what causes deadlocks. One of the possible
scenario for the deadlock is the following:

thread 1 - lock_rec_unlock_unmodified() is invoked under locks hash table
           cell latch, the latch is acquired;
thread 2 - purge thread acquires page latch and tries to remove
           delete-marked record, it invokes lock_update_delete(), which
           requests locks hash table cell latch, held by thread 1;
thread 1 - requests page latch, held by thread 2.

To fix it we need to release lock_sys.latch and/or lock hash cell latch,
acquire page latch and re-acquire lock_sys related latches.

When lock_sys.latch and/or lock hash cell latch are released in
lock_release_on_prepare() and lock_release_on_prepare_try(), the page on
which the current lock is held, can be merged. In this case the bitmap
of the current lock must be cleared, and the new lock must be added to
the end of trx->lock.trx_locks list, or bitmap of already existing lock
must be changed.

The new field trx_lock_t::set_nth_bit_calls indicates if new locks
(bits in existing lock bitmaps or new lock objects) were created during
the period when lock_sys was released in trx->lock.trx_locks list
iteration loop in lock_release_on_prepare() or
lock_release_on_prepare_try(). And, if so, we traverse the list again.

The block can be freed during pages merging, what causes assertion
failure in buf_page_get_gen(), as btr_block_get() passes BUF_GET as page
get mode to it. That's why page_get_mode parameter was added to
btr_block_get() to pass BUF_GET_POSSIBLY_FREED from
lock_release_on_prepare() and lock_release_on_prepare_try() to
buf_page_get_gen().

As searching for id of trx, which modified secondary index record, is
quite expensive operation, restrict its usage for master. System variable
was added to remove the restriction for testing simplifying. The
variable exists only either for debug build or for build with
-DINNODB_ENABLE_XAP_UNLOCK_UNMODIFIED_FOR_PRIMARY option to increase the
probability of catching bugs for release build with RQG.

Note that the code, which does primary index lookup to find out what
transaction modified secondary index record, is necessary only when
there is no primary key and no unique secondary key on replica with row
based replication, because only in this case extra X locks on unmodified
records can be set during scan phase.

Reviewed by Marko Mäkelä.
2024-10-23 12:36:17 +03:00

98 lines
3.3 KiB
Text

--source include/have_innodb.inc
--source include/count_sessions.inc
--source include/have_debug.inc
--source include/have_sequence.inc
SET @old_innodb_enable_xap_unlock_unmodified_for_primary_debug=
@@GLOBAL.innodb_enable_xap_unlock_unmodified_for_primary_debug;
SET GLOBAL innodb_enable_xap_unlock_unmodified_for_primary_debug= 1;
# Make sure there is no dynamic memory allocation during getting offsets for
# 32 columns of secondary index with 32 columns of primary index in
# lock_rec_unlock_unmodified().
DELIMITER $$;
BEGIN NOT ATOMIC
DECLARE c TEXT DEFAULT(
SELECT CONCAT(
'CREATE TABLE t1(pk',
GROUP_CONCAT(seq SEPARATOR ' INT, pk'), ' INT, a',
GROUP_CONCAT(seq SEPARATOR ' INT, a'), ' INT, b',
GROUP_CONCAT(seq SEPARATOR ' INT, b'), ' INT, c INT, PRIMARY KEY(pk',
GROUP_CONCAT(seq SEPARATOR ', pk'), '), INDEX i1(a',
GROUP_CONCAT(seq SEPARATOR ', a'), '), INDEX i2(b',
GROUP_CONCAT(seq SEPARATOR ', b'), ')) ENGINE=InnoDB;')
FROM seq_1_to_32);
EXECUTE IMMEDIATE c;
END;
$$
BEGIN NOT ATOMIC
DECLARE c TEXT DEFAULT(
SELECT CONCAT(
'INSERT INTO t1 VALUES (',
GROUP_CONCAT('1' SEPARATOR ','), ',',
GROUP_CONCAT('1' SEPARATOR ','), ',',
GROUP_CONCAT('1' SEPARATOR ','), ',0), (',
GROUP_CONCAT('2' SEPARATOR ','), ',',
GROUP_CONCAT('1' SEPARATOR ','), ',',
GROUP_CONCAT('2' SEPARATOR ','), ',0), (',
GROUP_CONCAT('3' SEPARATOR ','), ',',
GROUP_CONCAT('2' SEPARATOR ','), ',',
GROUP_CONCAT('1' SEPARATOR ','), ',0), (',
GROUP_CONCAT('4' SEPARATOR ','), ',',
GROUP_CONCAT('2' SEPARATOR ','), ',',
GROUP_CONCAT('2' SEPARATOR ','), ',0);')
FROM seq_1_to_32);
EXECUTE IMMEDIATE c;
END;
$$
DELIMITER ;$$
SET @old_timeout= @@GLOBAL.innodb_lock_wait_timeout;
SET GLOBAL innodb_lock_wait_timeout= 1;
XA START "t1";
UPDATE t1 FORCE INDEX (i2) SET c=c+1 WHERE a1=1 AND b1=1;
XA END "t1";
# If there is dynamic memory allocation during getting offsets for
# 32 columns of secondary index with 32 columns of primary index in
# lock_rec_unlock_unmodified(), the following statement will cause assertion
# failure in debug build.
XA PREPARE "t1";
--connect(con1,localhost,root,,)
# (pk, 2, 1, 0) record is X-locked but not modified in clustered index with the
# above XA transaction, if the bug is not fixed, the following SELECT will be
# blocked by the XA transaction if XA PREPARE does not release the unmodified
# record.
SELECT a1, b1, c FROM t1 FORCE INDEX (i1) WHERE a1=2 AND b1=1 FOR UPDATE;
--disconnect con1
--connection default
XA COMMIT "t1";
SET GLOBAL innodb_enable_xap_unlock_unmodified_for_primary_debug=
@old_innodb_enable_xap_unlock_unmodified_for_primary_debug;
SET GLOBAL innodb_lock_wait_timeout= @old_timeout;
# Check that we can't create secondary index with more than 32 columns.
DELIMITER $$;
--error ER_TOO_MANY_KEY_PARTS
BEGIN NOT ATOMIC
DECLARE c TEXT DEFAULT(
SELECT CONCAT(
'ALTER TABLE t1 ADD COLUMN d',
GROUP_CONCAT(seq SEPARATOR ' INT, ADD COLUMN d'),
' INT, ADD INDEX i3(d',
GROUP_CONCAT(seq SEPARATOR ', d'), ');')
FROM seq_1_to_33);
EXECUTE IMMEDIATE c;
END;
$$
DELIMITER ;$$
DROP TABLE t1;
--source include/wait_until_count_sessions.inc