# BUG#13810456: GTID: UNTIL_SQL_BEFORE_GTID DOES NOT WORK
#
# This test performs multiple invocations of
# START SLAVE UNTIL SQL_BEFORE_GTIDS and START SLAVE UNTIL SQL_AFTER_GTIDS
# to demonstrate its correct operation.
#
# Create a set of transactions on the master and applied them individually
# by using START SLAVE UNTIL SQL_BEFORE_GTIDS and
# START SLAVE UNTIL SQL_AFTER_GTIDS with different sets of missing and/or
# already applied GTIDs.
#
--source include/master-slave.inc
--source include/rpl_set_gtid_mode.inc

--connection master
--let $master_uuid= query_get_value(SELECT @@SERVER_UUID, @@SERVER_UUID, 1)
CREATE TABLE t1(c1 INTEGER) ENGINE= Innodb;
--source include/sync_slave_sql_with_master.inc
--echo [connection slave]
--source include/stop_slave.inc

# Generate a set of transactions on the master.
# INSERT value is the equal to sequence number.
--echo [connection master]
--connection master
--let $i=2
while ($i != 11)
{
  --eval INSERT INTO t1 VALUES($i)
  --inc $i
}

--connection slave
--echo [connection slave]

# Incorrect START SLAVE UNTIL SQL_BEFORE_GTIDS
--replace_result $master_uuid MASTER_UUID
--error ER_BAD_SLAVE_UNTIL_COND
--eval START SLAVE UNTIL SQL_BEFORE_GTIDS= "$master_uuid:4-5", MASTER_LOG_FILE= 'foo.bin', MASTER_LOG_POS= 100

# Incorrect START SLAVE UNTIL SQL_BEFORE_GTIDS
--replace_result $master_uuid MASTER_UUID
--error ER_BAD_SLAVE_UNTIL_COND
--eval START SLAVE SQL_THREAD UNTIL SQL_BEFORE_GTIDS= "$master_uuid:4-5", MASTER_LOG_FILE= 'foo.bin', MASTER_LOG_POS= 100

# Incorrect START SLAVE UNTIL SQL_BEFORE_GTIDS
--replace_result $master_uuid MASTER_UUID
--error ER_BAD_SLAVE_UNTIL_COND
--eval START SLAVE IO_THREAD UNTIL SQL_BEFORE_GTIDS= "$master_uuid:4-5", MASTER_LOG_FILE= 'foo.bin', MASTER_LOG_POS= 100

# Incorrect START SLAVE UNTIL SQL_BEFORE_GTIDS
--replace_result $master_uuid MASTER_UUID
--error ER_PARSE_ERROR
--eval START SLAVE FOO UNTIL SQL_BEFORE_GTIDS= "$master_uuid:4-5"

# UNTIL GTID
--echo Check that START SLAVE IO_THREAD UNTIL ... maintains old behaviour, condition is ignored.
--replace_result $master_uuid MASTER_UUID
--eval START SLAVE IO_THREAD UNTIL SQL_BEFORE_GTIDS= "$master_uuid:4-5"

--echo Wait until IO Thread reads all master binlog and stop slave.
--let $slave_param= Retrieved_Gtid_Set
--let $slave_param_value= $master_uuid:1-10
--source include/wait_for_slave_param.inc
--source include/stop_slave_io.inc

--echo SQL thread must stop *before* INSERT 4
--replace_result $master_uuid MASTER_UUID
--eval START SLAVE SQL_THREAD UNTIL SQL_BEFORE_GTIDS= "$master_uuid:4-5"
--source include/wait_for_slave_sql_to_stop.inc
--let $assert_cond= MAX(c1)=3 FROM t1
--let $assert_text= Rows until 3 in t1 must be replicated now
--source include/assert.inc

--echo SQL thread must stop *after* INSERT 5
# SQL_AFTER_GTIDS is not working fine with MTS which is 'TODO'.  Hence post
# BUG14767986 fix 'ER_MTS_FEATURE_IS_NOT_SUPPORTED' warning will be thrown
# when MTS is enabled and server will be started in single threaded mode.
--disable_warnings
--replace_result $master_uuid MASTER_UUID
--eval START SLAVE SQL_THREAD UNTIL SQL_AFTER_GTIDS= "$master_uuid:4-5"
--enable_warnings
--source include/wait_for_slave_sql_to_stop.inc
--let $assert_cond= MAX(c1)=5 FROM t1
--let $assert_text= Rows until 5 in t1 must be replicated now
--source include/assert.inc

--echo SQL thread must stop *before* INSERT 6
--replace_result $master_uuid MASTER_UUID
--eval START SLAVE SQL_THREAD, IO_THREAD UNTIL SQL_BEFORE_GTIDS= "$master_uuid:2-7"
--source include/wait_for_slave_sql_to_stop.inc
--let $assert_cond= MAX(c1)=5 FROM t1
--let $assert_text= Rows until 5 in t1 must be replicated now
--source include/assert.inc

--echo SQL thread must stop *after* INSERT 5
--echo 1-5 are already applied so it will stop immediately
# SQL_AFTER_GTIDS is not working fine with MTS which is 'TODO'.  Hence post
# BUG14767986 fix 'ER_MTS_FEATURE_IS_NOT_SUPPORTED' warning will be thrown
# when MTS is enabled and server will be started in single threaded mode.
--disable_warnings
--replace_result $master_uuid MASTER_UUID
--eval START SLAVE SQL_THREAD, IO_THREAD UNTIL SQL_AFTER_GTIDS= "$master_uuid:1-5"
--enable_warnings
--source include/wait_for_slave_sql_to_stop.inc
--let $assert_cond= MAX(c1)=5 FROM t1
--let $assert_text= Rows until 5 in t1 must be replicated now
--source include/assert.inc

--echo SQL thread must stop *before* INSERT 10
--replace_result $master_uuid MASTER_UUID
--enable_query_log
--eval START SLAVE UNTIL SQL_BEFORE_GTIDS= "$master_uuid:10"
--source include/wait_for_slave_sql_to_stop.inc
--let $assert_cond= MAX(c1)=9 FROM t1
--let $assert_text= Rows until 9 in t1 must be replicated now
--source include/assert.inc

--echo SQL thread must stop *before* INSERT 3-6
--echo 3-6 is already applied so it will stop immediately
--replace_result $master_uuid MASTER_UUID
--eval START SLAVE UNTIL SQL_BEFORE_GTIDS= "$master_uuid:3-6"
--source include/wait_for_slave_sql_to_stop.inc
--let $assert_cond= MAX(c1)=9 FROM t1
--let $assert_text= Rows until 9 in t1 must be replicated now
--source include/assert.inc

--echo SQL thread must stop *before* INSERT 9
--echo 9 is already applied so it will stop immediately
--replace_result $master_uuid MASTER_UUID
--eval START SLAVE UNTIL SQL_BEFORE_GTIDS= "$master_uuid:9"
--source include/wait_for_slave_sql_to_stop.inc
--let $assert_cond= MAX(c1)=9 FROM t1
--let $assert_text= Rows until 9 in t1 must be replicated now
--source include/assert.inc

--echo Sync slave with master
--source include/start_slave.inc
--replace_result $master_uuid MASTER_UUID
--disable_result_log
--eval SELECT WAIT_UNTIL_SQL_THREAD_AFTER_GTIDS('$master_uuid:10')
--enable_result_log
--let $assert_cond= MAX(c1)=10 FROM t1
--let $assert_text= Rows until 10 in t1 must be replicated now
--source include/assert.inc

# Cleanup
--connection master
DROP TABLE t1;
--source include/rpl_end.inc
