Skip to content

Commit

Permalink
MyRocks: implement attachable transaction support (#1314)
Browse files Browse the repository at this point in the history
Upstream commit ID: facebook/mysql-5.6@12c1f02
PS-8951: Merge percona-202305 (https://jira.percona.com/browse/PS-8951)

Summary:
The storage engine requirement for supporting attachable transaction is being
able to handle vanished THD::ha_data field in the middle of transaction. The
server layer swaps its existing data with a nullptr before calling the SE, and
expects the SE to allocate a new transaction object and to invalidate any cached
info depending on the previous one. Once the server is done with the attachable
transaction, it swaps back the original SE data pointer.

MyRocks already happens to work correctly in this case, so the main change is to
advertise the attachable transaction support by adding
HA_ATTACHABLE_TRX_COMPATIBLE to ha_rocksdb::table_flags return value.

Since the attachable transactions are
AC-NL-RO-RC (AutoCommit-NonLocking-ReadOnly-ReadCommitted) at this point, the
rest of code patch introduces invariants asserting these properties for
Rdb_transcation and child class objects and minor changes such as not setting
lock timeouts as no locks can be taken. Specifically, such transaction is
indicated by m_max_row_locks == 0. This is decided in set_params which now
handles both regular and attachable transactions.

Make m_ddl_transaction a private field with getters and setters so that the
invariants could be checked.

Factor out is_autocommit and get_ha_data_or_null helper functions.

At the same time make cleanups in the touched code:
- make field modified_tables private (trivially)
- remove unused write-only field m_mysql_gtid.
- simplify can_acquire_snapshot_without_conflicts, make it `[[nodiscard]]`.
- make ingest_bulk_load_files take const reference argument.
- remove redundant "virtual" keyword from
  Rdb_transaction_impl::is_writebatch_trx.
- remove unused method Rdb_transaction_impl::has_snapshot

For testing, convert the existing InnoDB attachable_trx.test to an include file
that is shared by both engine tests.

At a later point the attachable transactions may become read-write (if i.e.
mysql.gtid_executed is converted to MyRocks) and read-uncommitted, if this
isolation level will be implemented for the DDSE. In this case the invariant
will have to be split.

Reference:
- Server attachable transaction support:
  https://dev.mysql.com/worklog/task/?id=8003
- InnoDB attachable transaction support:
  https://dev.mysql.com/worklog/task/?id=7828
- The initial InnoDB implementation: 5e9eedb

Pull Request resolved: facebook/mysql-5.6#1314

Differential Revision: D46470657

fbshipit-source-id: fb06be1a565cbdfa285cd376a92521cc56e44c21
  • Loading branch information
laurynas-biveinis authored and inikep committed Jan 17, 2024
1 parent 486e280 commit 6114c20
Show file tree
Hide file tree
Showing 7 changed files with 448 additions and 211 deletions.
164 changes: 164 additions & 0 deletions mysql-test/include/attachable_trx.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
--source include/have_debug.inc

# Save the initial number of concurrent sessions
--source include/count_sessions.inc

SET @debug_saved = @@global.debug;

--let $DEFAULT_SE = `SELECT @@default_storage_engine`

--echo
START TRANSACTION;

--echo
CREATE TABLE t1(a INT PRIMARY KEY AUTO_INCREMENT, b VARCHAR(255));

--echo
INSERT INTO t1(b) VALUES ('aaa');
INSERT INTO t1(b) VALUES ('bbb');
INSERT INTO t1(b) VALUES ('ccc');

--echo
COMMIT;

if ($DEFAULT_SE == "InnoDB") {

###########################################################################

--echo
--echo # 1. Start outer transaction with SERIALIZABLE isolation level.
--echo # Check that SELECT in inner transaction will not take locks.

--echo
--echo # Start transaction.
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
START TRANSACTION;

--echo # Insert a new row into t1.
INSERT INTO t1(b) VALUES ('ddd');
SELECT * FROM t1;

--echo
--echo # [another connection]
--connect (con1,localhost,root,,)

--echo
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
START TRANSACTION;
SET @@global.debug = '+d,use_attachable_trx';
SELECT * FROM t1;
SET @@global.debug = '-d,use_attachable_trx';
--disconnect con1

--echo
--echo # [default connection]
--connection default

--echo
ROLLBACK;

} # if ($DEFAULT_SE == "InnoDB")

###########################################################################

--echo
--echo # 2. Check that inner transaction has different visibility scope than
--echo # the outer transaction.

--echo # Start READ ONLY transaction.
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
START TRANSACTION READ ONLY;

--echo # SELECT to actually start a transaction.
SELECT * FROM t1;

--echo
--echo # [another connection]
--connect (con1,localhost,root,,)

--echo
START TRANSACTION;
UPDATE t1 SET b = 'zzz' WHERE a = 2;
COMMIT;
--disconnect con1

--echo
--echo # [default connection]
--connection default

--echo
--echo # SELECT in the outer transaction doesn't see the changes.
SELECT * FROM t1;

--echo
--echo # SELECT in the inner transaction sees the changes.
SET @@global.debug = '+d,use_attachable_trx';
SELECT * FROM t1;
SET @@global.debug = '-d,use_attachable_trx';

--echo
--echo # COMMIT the outer transaction.
COMMIT;

--echo # SELECT in the outer transaction now sees the changes.
SELECT * FROM t1;
COMMIT;

if ($DEFAULT_SE == "InnoDB") {

###########################################################################

--echo
--echo # 3. Check that the inner transaction does not reset a save point set in
--echo # the outer transaction.

--echo
--echo # Start transaction.
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
START TRANSACTION;
SELECT * FROM t1;

--echo
--echo # Set save point.
SAVEPOINT sp1;

--echo
--echo # Do some changes.
UPDATE t1 SET b = 'xxx' WHERE a = 2;
SELECT * FROM t1;

--echo
--echo # Do anything in the inner transaction.
SET @@global.debug = '+d,use_attachable_trx';
SELECT * FROM t1;
SET @@global.debug = '-d,use_attachable_trx';

--echo
--echo # Just make sure the changes are still there.
SELECT * FROM t1;

--echo
--echo # Rollback to the save point to make sure it was not reset.
ROLLBACK TO SAVEPOINT sp1;

--echo
--echo # Check that the changes have been reverted.
SELECT * FROM t1;

--echo
--echo # Commit.
COMMIT;

} # if ($DEFAULT_SE == "InnoDB")

###########################################################################

--echo
--echo # Cleanup.
DROP TABLE t1;

--echo
SET @@global.debug = @debug_saved;

# Wait till we reached the initial number of concurrent sessions
--source include/wait_until_count_sessions.inc
2 changes: 1 addition & 1 deletion mysql-test/suite/innodb/r/attachable_trx.result
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ SET @debug_saved = @@global.debug;

START TRANSACTION;

CREATE TABLE t1(a INT PRIMARY KEY AUTO_INCREMENT, b VARCHAR(255)) engine = innodb;
CREATE TABLE t1(a INT PRIMARY KEY AUTO_INCREMENT, b VARCHAR(255));

INSERT INTO t1(b) VALUES ('aaa');
INSERT INTO t1(b) VALUES ('bbb');
Expand Down
153 changes: 1 addition & 152 deletions mysql-test/suite/innodb/t/attachable_trx.test
Original file line number Diff line number Diff line change
@@ -1,154 +1,3 @@
--source include/have_debug.inc

# Save the initial number of concurrent sessions
--source include/count_sessions.inc

SET @debug_saved = @@global.debug;

--echo
START TRANSACTION;

--echo
CREATE TABLE t1(a INT PRIMARY KEY AUTO_INCREMENT, b VARCHAR(255)) engine = innodb;

--echo
INSERT INTO t1(b) VALUES ('aaa');
INSERT INTO t1(b) VALUES ('bbb');
INSERT INTO t1(b) VALUES ('ccc');

--echo
COMMIT;

###########################################################################

--echo
--echo # 1. Start outer transaction with SERIALIZABLE isolation level.
--echo # Check that SELECT in inner transaction will not take locks.

--echo
--echo # Start transaction.
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
START TRANSACTION;

--echo # Insert a new row into t1.
INSERT INTO t1(b) VALUES ('ddd');
SELECT * FROM t1;

--echo
--echo # [another connection]
--connect (con1,localhost,root,,)

--echo
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
START TRANSACTION;
SET @@global.debug = '+d,use_attachable_trx';
SELECT * FROM t1;
SET @@global.debug = '-d,use_attachable_trx';
--disconnect con1

--echo
--echo # [default connection]
--connection default

--echo
ROLLBACK;

###########################################################################

--echo
--echo # 2. Check that inner transaction has different visibility scope than
--echo # the outer transaction.

--echo # Start READ ONLY transaction.
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
START TRANSACTION READ ONLY;

--echo # SELECT to actually start a transaction.
SELECT * FROM t1;

--echo
--echo # [another connection]
--connect (con1,localhost,root,,)

--echo
START TRANSACTION;
UPDATE t1 SET b = 'zzz' WHERE a = 2;
COMMIT;
--disconnect con1

--echo
--echo # [default connection]
--connection default

--echo
--echo # SELECT in the outer transaction doesn't see the changes.
SELECT * FROM t1;

--echo
--echo # SELECT in the inner transaction sees the changes.
SET @@global.debug = '+d,use_attachable_trx';
SELECT * FROM t1;
SET @@global.debug = '-d,use_attachable_trx';

--echo
--echo # COMMIT the outer transaction.
COMMIT;

--echo # SELECT in the outer transaction now sees the changes.
SELECT * FROM t1;
COMMIT;

###########################################################################

--echo
--echo # 3. Check that the inner transaction does not reset a save point set in
--echo # the outer transaction.

--echo
--echo # Start transaction.
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
START TRANSACTION;
SELECT * FROM t1;

--echo
--echo # Set save point.
SAVEPOINT sp1;

--echo
--echo # Do some changes.
UPDATE t1 SET b = 'xxx' WHERE a = 2;
SELECT * FROM t1;

--echo
--echo # Do anything in the inner transaction.
SET @@global.debug = '+d,use_attachable_trx';
SELECT * FROM t1;
SET @@global.debug = '-d,use_attachable_trx';

--echo
--echo # Just make sure the changes are still there.
SELECT * FROM t1;

--echo
--echo # Rollback to the save point to make sure it was not reset.
ROLLBACK TO SAVEPOINT sp1;

--echo
--echo # Check that the changes have been reverted.
SELECT * FROM t1;

--echo
--echo # Commit.
COMMIT;

###########################################################################

--echo
--echo # Cleanup.
DROP TABLE t1;

--echo
SET @@global.debug = @debug_saved;

# Wait till we reached the initial number of concurrent sessions
--source include/wait_until_count_sessions.inc
--source include/attachable_trx.inc
63 changes: 63 additions & 0 deletions mysql-test/suite/rocksdb/r/attachable_trx.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
set @@default_storage_engine=rocksdb;
SET @debug_saved = @@global.debug;

START TRANSACTION;

CREATE TABLE t1(a INT PRIMARY KEY AUTO_INCREMENT, b VARCHAR(255));

INSERT INTO t1(b) VALUES ('aaa');
INSERT INTO t1(b) VALUES ('bbb');
INSERT INTO t1(b) VALUES ('ccc');

COMMIT;

# 2. Check that inner transaction has different visibility scope than
# the outer transaction.
# Start READ ONLY transaction.
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
START TRANSACTION READ ONLY;
# SELECT to actually start a transaction.
SELECT * FROM t1;
a b
1 aaa
2 bbb
3 ccc

# [another connection]

START TRANSACTION;
UPDATE t1 SET b = 'zzz' WHERE a = 2;
COMMIT;

# [default connection]

# SELECT in the outer transaction doesn't see the changes.
SELECT * FROM t1;
a b
1 aaa
2 bbb
3 ccc

# SELECT in the inner transaction sees the changes.
SET @@global.debug = '+d,use_attachable_trx';
SELECT * FROM t1;
a b
1 aaa
2 zzz
3 ccc
SET @@global.debug = '-d,use_attachable_trx';

# COMMIT the outer transaction.
COMMIT;
# SELECT in the outer transaction now sees the changes.
SELECT * FROM t1;
a b
1 aaa
2 zzz
3 ccc
COMMIT;

# Cleanup.
DROP TABLE t1;

SET @@global.debug = @debug_saved;
5 changes: 5 additions & 0 deletions mysql-test/suite/rocksdb/t/attachable_trx.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
--source include/have_rocksdb.inc

set @@default_storage_engine=rocksdb;

--source include/attachable_trx.inc
Loading

0 comments on commit 6114c20

Please sign in to comment.