# ==== Purpose ====
#
# Verify that on slave server, appropriate XA_STATE is reported in Performance
# Schema tables for XA transactions.
#
# ==== Implementation ====
#
# 1) On Master start one XA transaction named 'xatest' and set the state to
# 'PREPARED'.
# 2) On slave wait till the applier thread applies the 'XA PREPARE' statement.
# 3) Verify that the XA_STATE is reported as 'PREPARED' in performance schema
# table.
# 4) On Master COMMIT the XA transaction.
# 5) On slave verify that the XA_STATE is reported as 'COMMITTED'
#
# ==== References ====
#
# Bug#25940184: P_S TRANSACTION INSTRUMENTATION DOES NOT WORK PROPERLY FOR
# XA ON SLAVE
--source include/rpl_connection_slave.inc
TRUNCATE TABLE performance_schema.events_transactions_current;

--source include/rpl_connection_master.inc
--let $master_uuid= query_get_value(SELECT @@SERVER_UUID, @@SERVER_UUID, 1)
CREATE TABLE t ( f INT) ENGINE=INNODB;
XA START 'xatest';
INSERT INTO t VALUES (10);
XA END 'xatest';
XA PREPARE 'xatest';
--let $master_pos= query_get_value(SHOW MASTER STATUS, Position, 1)
--let $master_file= query_get_value(SHOW MASTER STATUS, File, 1)

--source include/rpl_connection_slave.inc
--echo # Wait until SQL thread reaches desired master binlog position
--let $slave_param= Relay_Master_Log_File
--let $slave_param_value= $master_file
--let $slave_param_comparison= =
--source include/wait_for_slave_param.inc

--let $slave_param= Exec_Master_Log_Pos
--let $slave_param_value= $master_pos
--let $slave_param_comparison= >=
--source include/wait_for_slave_param.inc

XA RECOVER;
--echo ####################################################################
--echo # Asserting XA_STATE is PREPARED on Slave
--echo ####################################################################
--let $assert_text= Verify that XA_STATE is set PREPARED.
--let $assert_cond=count(*)=1 FROM performance_schema.events_transactions_current WHERE XID_GTRID=\'xatest\' AND XA_STATE=\'PREPARED\'
--source include/assert.inc

--source include/rpl_connection_master.inc
XA COMMIT 'xatest';
--source include/sync_slave_sql_with_master.inc
XA RECOVER;
--echo ####################################################################
--echo # Asserting XA_STATE is COMMITTED on Slave
--echo ####################################################################
--let $assert_text= Verify that XA_STATE is set COMMITTED.
--let $assert_cond=count(*)=1 FROM performance_schema.events_transactions_current WHERE XID_GTRID=\'xatest\' AND XA_STATE=\'COMMITTED\'
--source include/assert.inc

if ($gtid_mode == ON)
{
  --let $gno_0 = 3
  --let $expected_gtid = $master_uuid:$gno_0
  --let $assert_text= Expected gtid is MASTER_UUID:$gno_0
  --let $assert_cond= "[SELECT GTID FROM performance_schema.events_transactions_current where XID_GTRID=\'xatest\']" = "$expected_gtid"
  --source include/assert.inc
}

--let $diff_tables=master:t,slave:t
--source include/diff_tables.inc

--source include/rpl_connection_master.inc
DROP TABLE t;
--source include/sync_slave_sql_with_master.inc
