#
# WL#1697: Multisource replication
#
# MSR works only with slave repository type= TABLE.
#
# This tests test the following.
# 1. Start three servers. By default, slave repository is of type FILE
# 2. setup a master-slave replication between server 1 and server 2
#     (a default channel exists between server 1 and server 2)
# 3. Execute CHANGE MASTER to create a new channel.
#    - the command shall fail as we have wrong repository type
# 4. Convert only master info repository to TABLE
#    -  CHANGE MASTER shall fail
# 5. Convert only relay log info repository to TABLE
#   - CHANGE MASTER shall fail
# 6. Convert both master info and relay log info repositories to TABLE
#   -  CHANGE MASTER shall succeed
# 7. Restart the slave server
#   - There shall be errors in server log and slave should not be setup.
#   - subsequent commands shall fail unless the slave is properly restarted.
# 8. Restart the slave server with type=TABLE
#   - success
# 9. Set repositories to FILE
#   - failure with an error message.
# 10. Set repositories to TABLE // doing it again
#    - nothing to do.
#

##Skip on group replication runs
--source include/not_group_replication_plugin.inc
--source include/have_slave_repository_type_file.inc
--source include/have_binlog_format_mixed.inc
--source include/master-slave.inc

--echo #
--echo # Create a new channel from the slave to the third server.
--echo #  There shall be an error
--let $rpl_connection_name= server_2
--source include/rpl_connection.inc

--replace_result $SERVER_MYPORT_3 MASTER_PORT
--error ER_SLAVE_NEW_CHANNEL_WRONG_REPOSITORY
--eval CHANGE MASTER TO MASTER_HOST = "127.0.0.1", MASTER_PORT = $SERVER_MYPORT_3 FOR CHANNEL "channel_3";

--echo #  Convert only master_info_repository to table.
--echo #  The same error shall persist
--source include/stop_slave.inc
SET @@GLOBAL.master_info_repository ="TABLE";

call mtr.add_suppression("Slave: Cannot create new master info structure when repositories are of type FILE. Convert slave repositories  to TABLE to replicate from multiple sources");

--replace_result $SERVER_MYPORT_3 MASTER_PORT
--error ER_SLAVE_NEW_CHANNEL_WRONG_REPOSITORY
--eval CHANGE MASTER TO MASTER_HOST = "127.0.0.1", MASTER_USER = "root", MASTER_PORT = $SERVER_MYPORT_3 FOR CHANNEL "channel_3";

--echo #  Convert only relay log info repository to TABLE
--echo #  The same error shall remain
SET @@GLOBAL.master_info_repository = "FILE";
SET @@GLOBAL.relay_log_info_repository = "TABLE";


--replace_result $SERVER_MYPORT_3 MASTER_PORT
--error ER_SLAVE_NEW_CHANNEL_WRONG_REPOSITORY
--eval CHANGE MASTER TO MASTER_HOST = "127.0.0.1", MASTER_USER = "root", MASTER_PORT = $SERVER_MYPORT_3 FOR CHANNEL "channel_3";

--echo #
--echo # Convert both repositories to TABLE. CHANGE MASTER command passes
--echo #
#As relay_log_info_repository is already set to TABLE, set master_info_repository to TABLE
SET @@GLOBAL.master_info_repository = "TABLE";

--replace_result $SERVER_MYPORT_3 MASTER_PORT
--replace_column 2 #
--eval CHANGE MASTER TO MASTER_HOST ="127.0.0.1", MASTER_USER ="root", MASTER_PORT=$SERVER_MYPORT_3 FOR CHANNEL "channel_3"

--echo # Slave now is a multisourced slave.
--echo # Restart the slave and check that slave threads are not started because
--echo # the repositories are of type FILE
call mtr.add_suppression("Slave: This slave was a multisourced slave previously*");
call mtr.add_suppression("Failed to create or recover replication info repositories");

--let $rpl_server_number=2
--source include/rpl_restart_server.inc
# SHOW SLAVE STATUS will be empty
--query_vertical SHOW SLAVE STATUS
# Assert that pfs tables are empty
--let $assert_text= there shall be only 0 rows as the slave is not configured.
--let $assert_cond= [SELECT COUNT(*) FROM performance_schema.replication_applier_configuration] =0;
--source include/assert.inc

# subsequent commands shall fail with ER_SLAVE_CONFIGURATION i.e slave not setup
# but due to bug19344559 is setup. The error currently fails with
#  ER_SLAVE_NEW_CHANNEL_WRONG_REPOSITORY
# --error ER_SLAVE_CONFIGURATION
# --eval CHANGE MASTER TO MASTER_HOST = "127.0.0.1", MASTER_PORT = $SERVER_MYPORT_3 FOR CHANNEL "channel_3"

--echo #
--echo #   Test that if slave server is started with TABLE, replication proceeds
--echo #   as usual.
--let $rpl_server_number=2
--let $rpl_server_parameters=--master-info-repository=TABLE --relay-log-info-repository=TABLE
--source include/rpl_restart_server.inc
#assert that two rows are present
--let $assert_text= there shall be only 2 rows as slave is restarted properly
--let $assert_cond= [SELECT COUNT(*) FROM performance_schema.replication_applier_configuration] = 2;
--source include/assert.inc

--echo #
--echo #   Set slave repostiories to FILE. An error is generated.
--echo #

--error ER_CHANGE_RPL_INFO_REPOSITORY_FAILURE
SET @@GLOBAL.master_info_repository = "FILE";

--error ER_CHANGE_RPL_INFO_REPOSITORY_FAILURE
SET @@GLOBAL.relay_log_info_repository = "FILE";

SHOW VARIABLES LIKE '%info_repository%';

--echo #
--echo #  Currently slave repositores are of type TABLE. Try again
--echo #  again setting to TABLE. Returns success doing nothing.
--echo #

SET @@GLOBAL.master_info_repository = "TABLE";
SET @@GLOBAL.relay_log_info_repository = "TABLE";

# Clean up.
# Delete channel_3 and restart server with FILE
#
# Stop slave for channel_3
--let $rpl_channel_name= channel_3
--source include/stop_slave.inc

# Reset slave for channel_3
RESET SLAVE ALL FOR CHANNEL 'channel_3';

--let $rpl_channel_name=

--let $rpl_server_number= 2
--let $rpl_server_parameters=--master-info-repository=FILE --relay-log-info-repository=FILE
--source include/rpl_restart_server.inc

#End MSR setup
--let $rpl_only_running_threads= 1
--source include/rpl_end.inc


