# Verify that after CHANGE MASTER, replication (I/O thread and SQL
# thread) restart from where SQL thread left, not from where
# I/O thread left (some old bug fixed in 4.0.17)

call mtr.add_suppression("Slave I/O: The slave I/O thread stops because a fatal error is encountered when it tried to SET @master_binlog_checksum");

connection slave;
let $save_master_host= query_get_value(SHOW SLAVE STATUS, Master_Host, 1);

connection master;
# Make SQL slave thread advance a bit
create table t1(n int);
--sync_slave_with_master
select * from t1;
# Now stop it and make I/O slave thread be ahead
stop slave sql_thread;
connection master;
insert into t1 values(1);
insert into t1 values(2);
save_master_pos;
let $slave_param= Read_Master_Log_Pos;
let $slave_param_value= query_get_value(SHOW MASTER STATUS, Position, 1);
connection slave;
source include/wait_for_slave_param.inc;
source include/stop_slave.inc;

let $read_pos= query_get_value(SHOW SLAVE STATUS, Read_Master_Log_Pos, 1);
let $exec_pos= query_get_value(SHOW SLAVE STATUS, Exec_Master_Log_Pos, 1);
if ($read_pos == $exec_pos)
{
  source include/show_rpl_debug_info.inc;
  echo 'Read_Master_Log_Pos: $read_pos' == 'Exec_Master_Log_Pos: $exec_pos';
  die Failed because Read_Master_Log_Pos is equal to Exec_Master_Log_Pos;
} 
--replace_column 2 ####
change master to master_user='root';
let $read_pos= query_get_value(SHOW SLAVE STATUS, Read_Master_Log_Pos, 1);
let $exec_pos= query_get_value(SHOW SLAVE STATUS, Exec_Master_Log_Pos, 1);
if ($read_pos != $exec_pos)
{
  source include/show_rpl_debug_info.inc;
  echo 'Read_Master_Log_Pos: $read_pos' <> 'Exec_Master_Log_Pos: $exec_pos';
  die Failed because Read_Master_Log_Pos is not equal to Exec_Master_Log_Pos;
}

start slave;
sync_with_master;
select * from t1;
connection master;
drop table t1;
--sync_slave_with_master

# End of 4.1 tests

#
# BUG#12190 CHANGE MASTER has differ path requiremts on MASTER_LOG_FILE and RELAY_LOG_FILE
#

--source include/rpl_reset.inc

connection master;
create table t1 (a int);
insert into t1 values (1);
flush logs;
insert into t1 values (2);

# Note: the master positon saved by this will also be used by the
# 'sync_with_master' below.
--sync_slave_with_master

# Check if the table t1 and t2 are identical on master and slave;
--let $diff_tables= master:t1, slave:t1
--source include/diff_tables.inc

connection slave;
source include/stop_slave.inc;
delete from t1 where a=2;

# start replication from the second insert, after fix of BUG#12190,
# relay_log_file does not use absolute path, only the filename is
# required
#
# Note: the follow change master will automatically reset
# relay_log_purge to false, save the old value to restore
let $relay_log_purge= `select @@global.relay_log_purge`;
CHANGE MASTER TO relay_log_file='slave-relay-bin.000005', relay_log_pos=4;
start slave sql_thread;
source include/wait_for_slave_sql_to_start.inc;

# Sync to the same position saved by the 'sync_slave_with_master' above.
sync_with_master;

# Check if the table t1 and t2 are identical on master and slave;
--let $diff_tables= master:t1, slave:t1
--source include/diff_tables.inc

# clean up
connection slave;
start slave io_thread;
source include/wait_for_slave_io_to_start.inc;
eval set global relay_log_purge=$relay_log_purge;
connection master;
drop table t1;
--sync_slave_with_master

#
# BUG#44209: MASTER_CONNECT_RETRY and --master-retry-count disconnected from each other
#

-- connection master
-- source include/rpl_reset.inc

-- connection slave
-- let $retry_count0= query_get_value(SHOW SLAVE STATUS, Master_Retry_Count, 1)

# assertion: show that the command line is still valid (MTR sets it to 10)
-- echo Master_Retry_Count: $retry_count

-- source include/stop_slave.inc

# action: change the initial value to some other value
-- let $retry_count1= `SELECT $retry_count0+10`
-- eval CHANGE MASTER TO master_retry_count=$retry_count1

-- source include/start_slave.inc

-- let $retry_count1= query_get_value(SHOW SLAVE STATUS, Master_Retry_Count, 1)
# assertion: it shows the new value -- retry_count0 +10
if (`SELECT $retry_count1 <> ($retry_count0+10)`)
{
  -- let $expected= `SELECT $retry_count0+10`
  -- echo Unexpected retry count value! Got: $retry_count1, expected: $expected
  -- die
}

-- source include/stop_slave.inc

# action: change master_retry_count to zero will set it to zero.
-- let $retry_count1= 0
-- eval CHANGE MASTER TO master_retry_count=$retry_count1

-- source include/start_slave.inc

-- let $retry_count2= query_get_value(SHOW SLAVE STATUS, Master_Retry_Count, 1)
# assertion: it shows no changes
if ($retry_count2 != $retry_count1)
{
  -- echo Unexpected retry count value! Got: $retry_count2, expected: $retry_count1
  -- die
}

-- source include/stop_slave.inc

# action: change the value to the smallest value possible
CHANGE MASTER TO master_retry_count=1;

-- source include/start_slave.inc

-- let $retry_count3= query_get_value(SHOW SLAVE STATUS, Master_Retry_Count, 1)
# assertion: it should show 1
if ($retry_count3 != 1)
{
  -- echo Unexpected retry count value! Got: $retry_count3, expected: 1
  -- die
}

-- source include/stop_slave.inc

# action: change the value back to the original one
-- eval CHANGE MASTER TO master_retry_count=$retry_count0

-- source include/start_slave.inc

-- let $retry_count4= query_get_value(SHOW SLAVE STATUS, Master_Retry_Count, 1)
# assertion: it should show 1
if ($retry_count4 != $retry_count0)
{
  -- echo Unexpected retry count value! Got: $retry_count4, expected: $retry_count0
  -- die
}

## Now lets change master_retry_count to the maximum value possible
## and stop and restart the slave to check that (in the middle of it 
## we also check that when issueing CHANGE MASTER TO without specifying
## master_retry_count will not affect its current value):
##   1. master_retry_count persists after restart
##   2. setting a value higher than the max will make the max to be set.
##   3. maximum value is accepted and persists after serialization 
##      and de-serialization
-- source include/stop_slave.inc
-- let $retry_count_set= 18446744073709551616
-- replace_result 18446744073709551616 RETRY_COUNT_SET
-- eval CHANGE MASTER TO master_retry_count=$retry_count_set
-- let $retry_count_expected= query_get_value(SHOW SLAVE STATUS, Master_Retry_Count, 1)
-- replace_result 18446744073709551615 RETRY_COUNT_EXPECTED 4294967295 RETRY_COUNT_EXPECTED 9223372036854775807 RETRY_COUNT_EXPECTED
-- eval SELECT $retry_count_expected AS current_retry_count

## assert that issuing CHANGE MASTER without changing master_retry_count
## does not affect the existing value for master_retry_count
-- let $connect_retry_old= query_get_value(SHOW SLAVE STATUS, Connect_Retry, 1)
-- let $connect_retry_new= `SELECT $connect_retry_old + 1`
-- let $retry_count_before= query_get_value(SHOW SLAVE STATUS, Master_Retry_Count, 1)
-- eval CHANGE MASTER TO master_connect_retry= $connect_retry_new
-- let $retry_count_after= query_get_value(SHOW SLAVE STATUS, Master_Retry_Count, 1)
if ($retry_count_before != $retry_count_after)
{
  -- echo Unexpected retry count value! Got: $retry_count_after, expected: $retry_count_before
  -- die
}
-- eval CHANGE MASTER TO master_connect_retry= $connect_retry_old

## Server restart persisting test
--let $rpl_server_number= 2
--source include/rpl_restart_server.inc

-- source include/start_slave.inc

-- let $retry_count5= query_get_value(SHOW SLAVE STATUS, Master_Retry_Count, 1)
if ($retry_count5 != $retry_count_expected)
{
  -- echo Unexpected retry count value! Got: $retry_count5, expected: $retry_count_expected
  -- die
}

# action: change the value back to the original one
-- source include/stop_slave.inc
-- eval CHANGE MASTER TO master_retry_count=$retry_count0

## assert that there is a parse error if not using a numerical value
-- error ER_PARSE_ERROR
CHANGE MASTER TO master_retry_count='a';

## assert that there is a parse error if using a negative number
-- error ER_PARSE_ERROR
CHANGE MASTER TO master_retry_count=-1;

-- source include/start_slave.inc

## assert that the test has cleaned itself up
-- let $retry_count6= query_get_value(SHOW SLAVE STATUS, Master_Retry_Count, 1)
if ($retry_count6 != $retry_count0)
{
  -- echo Unexpected retry count value! Got: $retry_count6, expected: $retry_count0
  -- die
}

## Checks if Replicate_Ignore_Server_Ids is correctly written to a repository.

--connection slave
-- source include/stop_slave.inc

-- let $connect_old_value= query_get_value(SHOW SLAVE STATUS, Connect_Retry, 1)
-- let $ignore_old_value= query_get_value(SHOW SLAVE STATUS, Replicate_Ignore_Server_Ids, 1)

-- eval CHANGE MASTER TO master_connect_retry= 200, IGNORE_SERVER_IDS= (100, 200, 300, 400, 500)

-- let $connect_new_value= query_get_value(SHOW SLAVE STATUS, Connect_Retry, 1)
-- let $ignore_new_value= query_get_value(SHOW SLAVE STATUS, Replicate_Ignore_Server_Ids, 1)

--let $rpl_server_number= 2
--source include/rpl_restart_server.inc

-- source include/start_slave.inc

-- let $connect_crash_value= query_get_value(SHOW SLAVE STATUS, Connect_Retry, 1)
-- let $ignore_crash_value= query_get_value(SHOW SLAVE STATUS, Replicate_Ignore_Server_Ids, 1)

-- echo Eexpected values: "$ignore_new_value" == "$ignore_crash_value" or $connect_new_value == $connect_crash_value
if (`SELECT "$ignore_new_value" <> "$ignore_crash_value" || $connect_new_value <> $connect_crash_value`)
{
  -- echo Unexpected values: "$ignore_new_value" <> "$ignore_crash_value" or $connect_new_value <> $connect_crash_value
  -- die
}

# clean up
--source include/stop_slave.inc
CHANGE MASTER TO IGNORE_SERVER_IDS= ();
--source include/start_slave.inc

#
# WL#3127 slave side TCP address binding
# - CHANGE MASTER ... MASTER_BIND='interface'
# - SHOW SLAVE STATUS has new column Master_Bind
#

let $check_ipv6_just_check=1;
source include/check_ipv6.inc;
let $check_ipv6_just_check=0;

# Test valid IPv4 address
let $master_bind='127.0.0.1';
--source extra/rpl_tests/rpl_change_master_bind.inc

# Test invalid IPv4 address
let $master_bind='1.1.1.1';
let $master_bind_error_expected=2003;
--source extra/rpl_tests/rpl_change_master_bind.inc
let $master_bind_error_expected=0;

# Test valid IPv6 address
let $master_bind='::1';
if (!$check_ipv6_supported)
{
  # No IPv6 support, fallback to IPv4
  let $master_bind='127.0.0.1';
}
--source extra/rpl_tests/rpl_change_master_bind.inc

# Test with no bind address(check that reset works)
let $master_bind='';
--source extra/rpl_tests/rpl_change_master_bind.inc

### Revert to original master_host value
--source include/stop_slave.inc
--replace_result $save_master_host SAVE_MASTER_HOST
--eval CHANGE MASTER TO master_host='$save_master_host'
--source include/start_slave.inc
