Skip to content
Permalink
Browse files
MDEV-25616 XA PREPARE event group is not binlogged when..
the only query of the XA transaction is on a non-transactional table
errors out:

XA BEGIN 'x';
--error ER_DUP_ENTRY
INSERT INTO t1 VALUES (1),(1);
XA END 'x';
XA PREPARE 'x';

The binlogging pattern is correctly started as expected with
the errored-out Query or its ROW format events, but there is
no empty XA_prepare_log_event group.
The following

XA COMMIT 'x';

therefore should not be logged either, but it does.

The bug is fixed with proper maintaining of a read-write binlog hton
property and use it to enforce correct binlogging decisions.
Specifically in the bug description case XA COMMIT won't be binlogged
in both when given in the same connection and externally after disconnect.
The same continue to apply to an empty XA that do not change any data in all
transactional engines involved.
  • Loading branch information
bnestere authored and andrelkin committed Oct 25, 2022
1 parent 4b4c2b8 commit 42802ad
Show file tree
Hide file tree
Showing 8 changed files with 1,709 additions and 6 deletions.
@@ -108,3 +108,111 @@ master-bin.000001 # Gtid # # GTID #-#-#
master-bin.000001 # Query # # use `test`; DROP SEQUENCE `s` /* generated by server */
master-bin.000001 # Gtid # # GTID #-#-#
master-bin.000001 # Query # # use `test`; DROP TABLE `t1` /* generated by server */
CREATE TABLE t1 (a INT PRIMARY KEY) ENGINE=InnoDB;
INSERT INTO t1 VALUES (1);
connect con1,localhost,root,,;
XA START '1';
INSERT INTO t1 VALUES (2),(1);
ERROR 23000: Duplicate entry '1' for key 'PRIMARY'
SELECT * FROM t1 WHERE a = 2;
a
XA END '1';
XA PREPARE '1';
disconnect con1;
connection default;
XA RECOVER;
formatID gtrid_length bqual_length data
1 1 0 1
XA COMMIT '1';
ERROR XA100: XA_RBROLLBACK: Transaction branch was rolled back
Must be no XA PREPARE group nor XA completion one:
include/show_binlog_events.inc
Log_name Pos Event_type Server_id End_log_pos Info
master-bin.000001 # Gtid # # GTID #-#-#
master-bin.000001 # Query # # use `test`; CREATE TABLE t1 (a INT PRIMARY KEY) ENGINE=InnoDB
master-bin.000001 # Gtid # # BEGIN GTID #-#-#
master-bin.000001 # Annotate_rows # # INSERT INTO t1 VALUES (1)
master-bin.000001 # Table_map # # table_id: # (test.t1)
master-bin.000001 # Write_rows_v1 # # table_id: # flags: STMT_END_F
master-bin.000001 # Xid # # COMMIT /* XID */
master-bin.000001 # Gtid # # GTID #-#-#
master-bin.000001 # Query # # use `test`; CREATE SEQUENCE s ENGINE=InnoDB
master-bin.000001 # Gtid # # BEGIN GTID #-#-#
master-bin.000001 # Annotate_rows # # SELECT NEXT VALUE FOR s
master-bin.000001 # Table_map # # table_id: # (test.s)
master-bin.000001 # Write_rows_v1 # # table_id: # flags: STMT_END_F
master-bin.000001 # Query # # COMMIT
master-bin.000001 # Gtid # # XA START X'32',X'',1 GTID #-#-#
master-bin.000001 # Query # # XA END X'32',X'',1
master-bin.000001 # XA_prepare # # XA PREPARE X'32',X'',1
master-bin.000001 # Gtid # # GTID #-#-#
master-bin.000001 # Query # # XA ROLLBACK X'32',X'',1
master-bin.000001 # Gtid # # GTID #-#-#
master-bin.000001 # Query # # use `test`; DROP SEQUENCE `s` /* generated by server */
master-bin.000001 # Gtid # # GTID #-#-#
master-bin.000001 # Query # # use `test`; DROP TABLE `t1` /* generated by server */
master-bin.000001 # Gtid # # GTID #-#-#
master-bin.000001 # Query # # use `test`; CREATE TABLE t1 (a INT PRIMARY KEY) ENGINE=InnoDB
master-bin.000001 # Gtid # # BEGIN GTID #-#-#
master-bin.000001 # Annotate_rows # # INSERT INTO t1 VALUES (1)
master-bin.000001 # Table_map # # table_id: # (test.t1)
master-bin.000001 # Write_rows_v1 # # table_id: # flags: STMT_END_F
master-bin.000001 # Xid # # COMMIT /* XID */
DROP TABLE t1;
connect con2,localhost,root,,;
CREATE TABLE tm (a INT PRIMARY KEY) ENGINE=MyISAM;
XA START '1';
INSERT INTO tm VALUES (1),(1);
ERROR 23000: Duplicate entry '1' for key 'PRIMARY'
SELECT * FROM tm WHERE a = 2;
a
XA END '1';
XA PREPARE '1';
disconnect con2;
connection default;
XA RECOVER;
formatID gtrid_length bqual_length data
1 1 0 1
XA ROLLBACK '1';
ERROR XA100: XA_RBROLLBACK: Transaction branch was rolled back
Must be no XA PREPARE group nor XA completion one:
include/show_binlog_events.inc
Log_name Pos Event_type Server_id End_log_pos Info
master-bin.000001 # Gtid # # GTID #-#-#
master-bin.000001 # Query # # use `test`; CREATE TABLE t1 (a INT PRIMARY KEY) ENGINE=InnoDB
master-bin.000001 # Gtid # # BEGIN GTID #-#-#
master-bin.000001 # Annotate_rows # # INSERT INTO t1 VALUES (1)
master-bin.000001 # Table_map # # table_id: # (test.t1)
master-bin.000001 # Write_rows_v1 # # table_id: # flags: STMT_END_F
master-bin.000001 # Xid # # COMMIT /* XID */
master-bin.000001 # Gtid # # GTID #-#-#
master-bin.000001 # Query # # use `test`; CREATE SEQUENCE s ENGINE=InnoDB
master-bin.000001 # Gtid # # BEGIN GTID #-#-#
master-bin.000001 # Annotate_rows # # SELECT NEXT VALUE FOR s
master-bin.000001 # Table_map # # table_id: # (test.s)
master-bin.000001 # Write_rows_v1 # # table_id: # flags: STMT_END_F
master-bin.000001 # Query # # COMMIT
master-bin.000001 # Gtid # # XA START X'32',X'',1 GTID #-#-#
master-bin.000001 # Query # # XA END X'32',X'',1
master-bin.000001 # XA_prepare # # XA PREPARE X'32',X'',1
master-bin.000001 # Gtid # # GTID #-#-#
master-bin.000001 # Query # # XA ROLLBACK X'32',X'',1
master-bin.000001 # Gtid # # GTID #-#-#
master-bin.000001 # Query # # use `test`; DROP SEQUENCE `s` /* generated by server */
master-bin.000001 # Gtid # # GTID #-#-#
master-bin.000001 # Query # # use `test`; DROP TABLE `t1` /* generated by server */
master-bin.000001 # Gtid # # GTID #-#-#
master-bin.000001 # Query # # use `test`; CREATE TABLE t1 (a INT PRIMARY KEY) ENGINE=InnoDB
master-bin.000001 # Gtid # # BEGIN GTID #-#-#
master-bin.000001 # Annotate_rows # # INSERT INTO t1 VALUES (1)
master-bin.000001 # Table_map # # table_id: # (test.t1)
master-bin.000001 # Write_rows_v1 # # table_id: # flags: STMT_END_F
master-bin.000001 # Xid # # COMMIT /* XID */
master-bin.000001 # Gtid # # GTID #-#-#
master-bin.000001 # Query # # use `test`; DROP TABLE `t1` /* generated by server */
master-bin.000001 # Gtid # # GTID #-#-#
master-bin.000001 # Query # # use `test`; CREATE TABLE tm (a INT PRIMARY KEY) ENGINE=MyISAM
master-bin.000001 # Gtid # # BEGIN GTID #-#-#
master-bin.000001 # Query # # use `test`; INSERT INTO tm VALUES (1),(1)
master-bin.000001 # Query # # COMMIT
DROP TABLE tm;
@@ -80,3 +80,55 @@ DROP TABLE t1;

--echo # Proof of correct logging incl empty XA-PREPARE
--source include/show_binlog_events.inc


# MDEV-25616 Binlog event for XA COMMIT is generated without matching XA START

CREATE TABLE t1 (a INT PRIMARY KEY) ENGINE=InnoDB;
INSERT INTO t1 VALUES (1);

--source include/count_sessions.inc
--connect(con1,localhost,root,,)

XA START '1';
--error ER_DUP_ENTRY
INSERT INTO t1 VALUES (2),(1);
SELECT * FROM t1 WHERE a = 2;
XA END '1';
XA PREPARE '1';

--disconnect con1

--connection default
--source include/wait_until_count_sessions.inc
XA RECOVER;

--error ER_XA_RBROLLBACK
XA COMMIT '1';
--echo Must be no XA PREPARE group nor XA completion one:
--source include/show_binlog_events.inc
DROP TABLE t1;

--source include/count_sessions.inc

--connect(con2,localhost,root,,)
CREATE TABLE tm (a INT PRIMARY KEY) ENGINE=MyISAM;
XA START '1';
--error ER_DUP_ENTRY
INSERT INTO tm VALUES (1),(1);
SELECT * FROM tm WHERE a = 2;
XA END '1';
XA PREPARE '1';

--disconnect con2

--connection default
--source include/wait_until_count_sessions.inc
XA RECOVER;

--error ER_XA_RBROLLBACK
XA ROLLBACK '1';
--echo Must be no XA PREPARE group nor XA completion one:
--source include/show_binlog_events.inc
DROP TABLE tm;

@@ -0,0 +1,10 @@
#
# Helper file to run each empty-due-to-err XA transaction test case both with
# and without detaching from the connection when the transaction is prepared.
#

--let $use_disconnect=0
--source rpl_xa_empty_transaction_test_case.inc

--let $use_disconnect=1
--source rpl_xa_empty_transaction_test_case.inc
@@ -0,0 +1,131 @@
#
# Helper script to create an XA transaction and validate it was not
# binlogged
#
# Parameters
# $xa_completion_action : The action to end the XA transaction, either
# COMMIT or ROLLBACK
# $trx_statements : A comma separated list specifying how to build
# the statements of the transaction. Each item in
# the list is either T (for transactional) or N
# (for non-transactional). An empty list will not
# add any statements to the transaction.
# $use_disconnect : When TRUE, disconnect after preparing the XA
# transaction to test the detach/rollback case
#

#
# Setup
--let $generic_assert_text= should not binlog XA transaction

--connection server_1
--let server_1_datadir=`select @@datadir`

--connection server_2
--let server_2_datadir=`select @@datadir`

--connection server_3
--let server_3_datadir=`select @@datadir`

--let assert_file=$MYSQLTEST_VARDIR/tmp/binlog_decoded.out

--connection server_1
CREATE TABLE tm (a INT PRIMARY KEY) engine=myisam;
CREATE TABLE ti (a INT PRIMARY KEY) engine=innodb;
--source include/save_master_gtid.inc

--connection server_3
--source include/sync_with_master_gtid.inc

--connection server_1

if ($use_disconnect)
{
--source include/count_sessions.inc
--connect(con1,localhost,root,,)
}

XA START 'x';
--let $_stmt_items= $trx_statements
--let $_ctr= 1
while($_stmt_items)
{
--let $_cur_stmt= `SELECT SUBSTRING_INDEX('$_stmt_items', ',', 1)`
--let $_stmt_items= `SELECT LTRIM(SUBSTRING('$_stmt_items', LENGTH('$_cur_stmt') + 2))`

if (`SELECT strcmp("$_cur_stmt","T") = 0`)
{
--let $target_table= ti
}

if (`SELECT strcmp("$_cur_stmt","N") = 0`)
{
--let $target_table= tm
}

--error ER_DUP_ENTRY
--eval INSERT INTO $target_table VALUES ($_ctr),($_ctr);
inc $_ctr;

}
XA END 'x';
XA PREPARE 'x';

if ($use_disconnect)
{
--disconnect con1
--connection server_1
--source include/wait_until_count_sessions.inc
XA RECOVER;

--error ER_XA_RBROLLBACK
--eval XA $xa_completion_action 'x';
}
if (!$use_disconnect)
{
--eval XA $xa_completion_action 'x';
}

--source include/save_master_gtid.inc

--let binlog_filename= query_get_value(SHOW MASTER STATUS, File, 1)
FLUSH LOGS;

--echo # MYSQL_BINLOG server_1_datadir/binlog_filename --result-file=assert_file
--exec $MYSQL_BINLOG $server_1_datadir/$binlog_filename --result-file=$assert_file

--let assert_text= server_1 $generic_assert_text
--let assert_count= 0
--let assert_select= XA START|XA END|XA PREPARE|XA COMMIT|XA ROLLBACK
--source include/assert_grep.inc

--connection server_2
--source include/sync_with_master_gtid.inc
--let binlog_filename= query_get_value(SHOW MASTER STATUS, File, 1)
FLUSH LOGS;

--echo # MYSQL_BINLOG server_2_datadir/binlog_filename --result-file=assert_file
--exec $MYSQL_BINLOG $server_2_datadir/$binlog_filename --result-file=$assert_file

--let assert_text= server_2 $generic_assert_text
--source include/assert_grep.inc

--connection server_3
--source include/sync_with_master_gtid.inc
--let binlog_filename= query_get_value(SHOW MASTER STATUS, File, 1)
FLUSH LOGS;

--echo # MYSQL_BINLOG server_3_datadir/binlog_filename --result-file=assert_file
--exec $MYSQL_BINLOG $server_3_datadir/$binlog_filename --result-file=$assert_file

--let assert_text= server_3 $generic_assert_text
--source include/assert_grep.inc

#
# Cleanup
--connection server_1
DROP TABLE ti,tm;
--source include/save_master_gtid.inc

--connection server_3
--source include/sync_with_master_gtid.inc

0 comments on commit 42802ad

Please sign in to comment.