# ==== Purpose ====
#
# Auxiliary test file included by 'binlog_gtid_next_xa.test'.
#
# This executes an XA PREPARE transaction, followed by an XA COMMIT or
# XA ROLLBACK transaction.  It sets specified values of GTID_NEXT
# before each of the two transactions.  After each transaction it
# verifies that GTID ownership is as expected.
#
# ==== Usage ====
#
# --let $gtid1= [<GTID> | ANONYMOUS | AUTOMATIC]
# --let $gtid2= [<GTID> | ANONYMOUS | AUTOMATIC | none]
# --let $commit= [COMMIT | ROLLBACK]
# --let $error= [0 | N]
# --let $one_phase = [one phase]
# --source extra/rpl_tests/rpl_gtid_next_xa.test
#
# Parameters:
#
#   $commit
#     If this is COMMIT, the second transaction will be an XA COMMIT
#     transaction. If this is ROLLBACK, the second transaction will be
#     an XA ROLLBACK transaction.
#
#   $gtid1
#     The value to use for GTID_NEXT for the first transaction
#     (XA PREPARE).
#
#   $gtid2
#     The value to use for GTID_NEXT for the second transaction
#     (XA COMMIT/ROLLBACK).  If this is 'none', no SET GTID_NEXT
#     statement is used and the previous value is reused.
#
#   $error
#     This is GTID setting related error.
#     Normally, this script expects all statements to succeed.  For
#     those cases, the caller must set $error=0. But if $gtid1 is a
#     GTID and $gtid2 is none, ER_GTID_NEXT_TYPE_UNDEFINED_GROUP is
#     expected for the second transaction.  Then the caller must set
#     $error= ER_GTID_NEXT_TYPE_UNDEFINED_GROUP.
#
#   $one_phase
#     Option to commit XA in one phase


--echo ---- XID $xid, $commit: $gtid1 + $gtid2 ----
--source include/rpl_connection_master.inc

if ($gtid1 != none)
{
  eval SET GTID_NEXT= '$gtid1';
}

eval XA START '$xid';
eval INSERT INTO t1 VALUES($xid);
eval XA END '$xid';
if (!$one_phase)
{
  eval XA PREPARE '$xid';


  --let $assert_text= Thread should not own any GTID.
  --let $assert_cond= @@SESSION.GTID_OWNED = ""
  --source include/assert.inc

  --source include/rpl_connection_master1.inc

  --let $assert_text= No thread should own any GTID.
  --let $assert_cond= @@GLOBAL.GTID_OWNED = ""
  --source include/assert.inc

  --let $assert_text= No thread should hold anonymous ownership.
  --let $assert_cond= [SHOW STATUS LIKE "ONGOING_ANONYMOUS_TRANSACTION_COUNT", Value, 1] = 0
  --source include/assert.inc

  --source include/rpl_connection_master.inc

  --echo error=$error
if ($gtid2 != none)
{
  eval SET GTID_NEXT= '$gtid2';
}

if ($error)
{
  # Generate erratic statement, check that GTID ownership is ok, then
  # set GTID_NEXT to valid value to allow the statement to execute
  # correctly.

  --error $error
  eval XA $commit '$xid';

  --source include/rpl_connection_master1.inc

  --let $assert_text= No thread should own any GTID.
  --let $assert_cond= @@GLOBAL.GTID_OWNED = ""
  --source include/assert.inc

  --let $assert_text= No thread should hold anonymous ownership.
  --let $assert_cond= [SHOW STATUS LIKE "ONGOING_ANONYMOUS_TRANSACTION_COUNT", Value, 1] = 0
  --source include/assert.inc

  --source include/rpl_connection_master.inc

  SET GTID_NEXT = 'AUTOMATIC';
}
}

if ($commit == ROLLBACK)
{
   # Reset $one_phase to empty in this case to satisfy syntax
   --let $one_phase=
}
eval XA $commit '$xid' $one_phase;

--let $assert_text= Thread should not own any GTID.
--let $assert_cond= @@SESSION.GTID_OWNED = ""
--source include/assert.inc

--let $assert_text= No thread should own any GTID.
--let $assert_cond= @@GLOBAL.GTID_OWNED = ""
--source include/assert.inc

--let $assert_text= No thread should hold anonymous ownership.
--let $assert_cond= [SHOW STATUS LIKE "ONGOING_ANONYMOUS_TRANSACTION_COUNT", Value, 1] = 0
--source include/assert.inc

ROLLBACK;
--inc $xid
SET GTID_NEXT = 'AUTOMATIC';
