#
# Test mysqldump SST on slave if SR transaction is in progress
#

--source include/big_test.inc
--source include/galera_cluster.inc

--source suite/galera/include/galera_sst_set_mysqldump.inc

# Save original auto_increment_offset values.
--let $node_1=node_1
--let $node_2=node_2
--source ../../galera/include/auto_increment_offset_save.inc

--connection node_1
CREATE TABLE ten (f1 INTEGER);
INSERT INTO ten VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10);

CREATE TABLE t1 (f1 INTEGER PRIMARY KEY AUTO_INCREMENT, f2 CHAR(255)) ENGINE=InnoDB;

SET AUTOCOMMIT=OFF;
SET SESSION wsrep_trx_fragment_size = 1000;
START TRANSACTION;
# Insert 1000 rows
INSERT INTO t1 (f2) SELECT REPEAT('x', 255) FROM ten AS a1, ten AS a2, ten AS a3;

# Update 1000 rows
UPDATE t1 SET f2 = REPEAT('y', 255);

# Wait for SR replication to kick in
--connection node_2
--let $wait_condition = SELECT COUNT(*) > 0 FROM mysql.wsrep_streaming_log;
--source include/wait_condition.inc

# Restart node #2

--connection node_2
--let $MYSQLD2_DATADIR = `SELECT @@datadir`

--echo Shutting down server ...
--source include/shutdown_mysqld.inc

# Force SST
--remove_file $MYSQLD2_DATADIR/grastate.dat

--connection node_1
--let $wait_condition = SELECT VARIABLE_VALUE = 1 FROM INFORMATION_SCHEMA.GLOBAL_STATUS WHERE VARIABLE_NAME = 'wsrep_cluster_size'
--source include/wait_condition.inc

--connection node_2
--let $restart_noprint = 1
--let $restart_parameters = --wsrep_sst_auth=sst:sst --wsrep_sst_method=mysqldump --wsrep-sst-receive-address=127.0.0.1:$NODE_MYPORT_2
--source include/start_mysqld.inc

--connection node_1
--let $wait_condition = SELECT VARIABLE_VALUE = 2 FROM INFORMATION_SCHEMA.GLOBAL_STATUS WHERE VARIABLE_NAME = 'wsrep_cluster_size'
--source include/wait_condition.inc

# Check that node #2 is caught up with the SR transaction that is still in progress
--connection node_2
SELECT COUNT(*) > 0 FROM mysql.wsrep_streaming_log;
SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
--let $wait_condition = SELECT COUNT(*) > 0 FROM t1;
--source include/wait_condition.inc

# Finalize transaction
--connection node_1
UPDATE t1 SET f2 = REPEAT('z', 255);
COMMIT;

# Confirm proper replication of entire transaction to node #2
--connection node_2
SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
SELECT COUNT(*) = 1000 FROM t1;
SELECT COUNT(*) = 1000 FROM t1 WHERE f2 = REPEAT('z', 255);

DROP TABLE t1;
DROP TABLE ten;

--connection node_1
# galera_sst_restore.inc uses DROP USER internally which is incompatible
# with SR, need to disable SR before that.
SET SESSION wsrep_trx_fragment_size=0;
--source suite/galera/include/galera_sst_restore.inc

# Restore original auto_increment_offset values.
--source ../galera/include/auto_increment_offset_restore.inc
