mariadb/mysql-test/suite/rpl/include/rpl_xa_mixed_engines.inc
Andrei Elkin c8ae357341 MDEV-742 XA PREPAREd transaction survive disconnect/server restart
Lifted long standing limitation to the XA of rolling it back at the
transaction's
connection close even if the XA is prepared.

Prepared XA-transaction is made to sustain connection close or server
restart.
The patch consists of

    - binary logging extension to write prepared XA part of
      transaction signified with
      its XID in a new XA_prepare_log_event. The concusion part -
      with Commit or Rollback decision - is logged separately as
      Query_log_event.
      That is in the binlog the XA consists of two separate group of
      events.

      That makes the whole XA possibly interweaving in binlog with
      other XA:s or regular transaction but with no harm to
      replication and data consistency.

      Gtid_log_event receives two more flags to identify which of the
      two XA phases of the transaction it represents. With either flag
      set also XID info is added to the event.

      When binlog is ON on the server XID::formatID is
      constrained to 4 bytes.

    - engines are made aware of the server policy to keep up user
      prepared XA:s so they (Innodb, rocksdb) don't roll them back
      anymore at their disconnect methods.

    - slave applier is refined to cope with two phase logged XA:s
      including parallel modes of execution.

This patch does not address crash-safe logging of the new events which
is being addressed by MDEV-21469.

CORNER CASES: read-only, pure myisam, binlog-*, @@skip_log_bin, etc

Are addressed along the following policies.
1. The read-only at reconnect marks XID to fail for future
   completion with ER_XA_RBROLLBACK.

2. binlog-* filtered XA when it changes engine data is regarded as
   loggable even when nothing got cached for binlog.  An empty
   XA-prepare group is recorded. Consequent Commit-or-Rollback
   succeeds in the Engine(s) as well as recorded into binlog.

3. The same applies to the non-transactional engine XA.

4. @@skip_log_bin=OFF does not record anything at XA-prepare
   (obviously), but the completion event is recorded into binlog to
   admit inconsistency with slave.

The following actions are taken by the patch.

At XA-prepare:
   when empty binlog cache - don't do anything to binlog if RO,
   otherwise write empty XA_prepare (assert(binlog-filter case)).

At Disconnect:
   when Prepared && RO (=> no binlogging was done)
     set Xid_cache_element::error := ER_XA_RBROLLBACK
     *keep* XID in the cache, and rollback the transaction.

At XA-"complete":
   Discover the error, if any don't binlog the "complete",
   return the error to the user.

Kudos
-----
Alexey Botchkov took to drive this work initially.
Sergei Golubchik, Sergei Petrunja, Marko Mäkelä provided a number of
good recommendations.
Sergei Voitovich made a magnificent review and improvements to the code.
They all deserve a bunch of thanks for making this work done!
2020-03-14 22:45:48 +02:00

183 lines
4.6 KiB
PHP

#
# The test file is invoked from rpl.rpl_xa_survive_disconnect_mixed_engines
#
# The test file is orginized as three sections: setup, run and cleanup.
# The main logics is resided in the run section which generates
# three types of XA transaction: two kinds of mixed and one on non-transactional
# table.
#
# param $command one of three of: 'setup', 'run' or 'cleanup'
# param $xa_terminate how to conclude: 'XA COMMIT' or 'XA ROLLBACK'
# param $one_phase 'one_phase' can be opted with XA COMMIT above
# param $xa_prepare_opt '1' or empty can be opted to test with and without XA PREPARE
# param $xid arbitrary name for xa trx, defaults to 'xa_trx'
# Note '' is merely to underline, not a part of the value.
#
if ($command == setup)
{
# Test randomizes the following variable's value:
SET @@session.binlog_direct_non_transactional_updates := if(floor(rand()*10)%2,'ON','OFF');
CREATE TABLE t (a INT) ENGINE=innodb;
CREATE TABLE tm (a INT) ENGINE=myisam;
}
if (!$xid)
{
--let $xid=xa_trx
}
if ($command == run)
{
## Non-temporary table cases
# Non transactional table goes first
--eval XA START '$xid'
--disable_warnings
INSERT INTO tm VALUES (1);
INSERT INTO t VALUES (1);
--enable_warnings
--eval XA END '$xid'
if ($xa_prepare_opt)
{
--eval XA PREPARE '$xid'
}
--eval $xa_terminate '$xid' $one_phase
# Transactional table goes first
--eval XA START '$xid'
--disable_warnings
INSERT INTO t VALUES (2);
INSERT INTO tm VALUES (2);
--enable_warnings
--eval XA END '$xid'
if ($xa_prepare_opt)
{
--eval XA PREPARE '$xid'
}
--eval $xa_terminate '$xid' $one_phase
# The pure non-transactional table
--eval XA START '$xid'
--disable_warnings
INSERT INTO tm VALUES (3);
--enable_warnings
--eval XA END '$xid'
if ($xa_prepare_opt)
{
--eval XA PREPARE '$xid'
}
--eval $xa_terminate '$xid' $one_phase
## Temporary tables
# create outside xa use at the tail
CREATE TEMPORARY TABLE tmp_i LIKE t;
CREATE TEMPORARY TABLE tmp_m LIKE tm;
--eval XA START '$xid'
--disable_warnings
INSERT INTO t VALUES (4);
INSERT INTO tm VALUES (4);
INSERT INTO tmp_i VALUES (4);
INSERT INTO tmp_m VALUES (4);
INSERT INTO t SELECT * FROM tmp_i;
INSERT INTO tm SELECT * FROM tmp_m;
--enable_warnings
--eval XA END '$xid'
if ($xa_prepare_opt)
{
--eval XA PREPARE '$xid'
}
--eval $xa_terminate '$xid' $one_phase
# temporary tables at the head
--eval XA START '$xid'
--disable_warnings
INSERT INTO tmp_i VALUES (5);
INSERT INTO tmp_m VALUES (5);
INSERT INTO t SELECT * FROM tmp_i;
INSERT INTO tm SELECT * FROM tmp_m;
INSERT INTO t VALUES (5);
INSERT INTO tm VALUES (5);
--enable_warnings
--eval XA END '$xid'
if ($xa_prepare_opt)
{
--eval XA PREPARE '$xid'
}
--eval $xa_terminate '$xid' $one_phase
# create inside xa use at the tail
DROP TEMPORARY TABLE tmp_i;
DROP TEMPORARY TABLE tmp_m;
--eval XA START '$xid'
--disable_warnings
INSERT INTO t VALUES (6);
INSERT INTO tm VALUES (6);
CREATE TEMPORARY TABLE tmp_i LIKE t;
CREATE TEMPORARY TABLE tmp_m LIKE tm;
INSERT INTO tmp_i VALUES (6);
INSERT INTO tmp_m VALUES (6);
INSERT INTO t SELECT * FROM tmp_i;
INSERT INTO tm SELECT * FROM tmp_m;
--enable_warnings
--eval XA END '$xid'
if ($xa_prepare_opt)
{
--eval XA PREPARE '$xid'
}
--eval $xa_terminate '$xid' $one_phase
# use at the head
DROP TEMPORARY TABLE tmp_i;
DROP TEMPORARY TABLE tmp_m;
--eval XA START '$xid'
--disable_warnings
CREATE TEMPORARY TABLE tmp_i LIKE t;
CREATE TEMPORARY TABLE tmp_m LIKE tm;
INSERT INTO tmp_i VALUES (7);
INSERT INTO tmp_m VALUES (7);
INSERT INTO t SELECT * FROM tmp_i;
INSERT INTO tm SELECT * FROM tmp_m;
INSERT INTO t VALUES (7);
INSERT INTO tm VALUES (7);
--enable_warnings
--eval XA END '$xid'
if ($xa_prepare_opt)
{
--eval XA PREPARE '$xid'
}
--eval $xa_terminate '$xid' $one_phase
# use at the tail and drop
--eval XA START '$xid'
--disable_warnings
INSERT INTO t VALUES (8);
INSERT INTO tm VALUES (8);
INSERT INTO tmp_i VALUES (8);
INSERT INTO tmp_m VALUES (8);
INSERT INTO t SELECT * FROM tmp_i;
INSERT INTO tm SELECT * FROM tmp_m;
DROP TEMPORARY TABLE tmp_i;
DROP TEMPORARY TABLE tmp_m;
--enable_warnings
--eval XA END '$xid'
if ($xa_prepare_opt)
{
--eval XA PREPARE '$xid'
}
--eval $xa_terminate '$xid' $one_phase
## Ineffective transactional table operation case
--eval XA START '$xid'
UPDATE t SET a = 99 where a = -1;
--eval XA END '$xid'
if ($xa_prepare_opt)
{
--eval XA PREPARE '$xid'
}
--eval $xa_terminate '$xid' $one_phase
}
if ($command == cleanup)
{
DROP TABLE t, tm;
}