###############################################################################
# Check that replication will fail safely if we inappropriately change
# replication tables. Besides we check if some maintenance commands
# can run without a problem.
###############################################################################

--source include/not_gtid_enabled.inc
--source include/master-slave.inc
--source include/have_innodb.inc
--source include/not_mts_slave_parallel_workers.inc
--source include/not_relay_log_info_table.inc
--source include/not_master_info_table.inc
--connection slave
--source include/stop_slave.inc
SHOW CREATE TABLE mysql.slave_relay_log_info;
ALTER TABLE mysql.slave_relay_log_info ENGINE= Innodb;
SHOW CREATE TABLE mysql.slave_relay_log_info;
--let $saved_relay_repo= `SELECT @@global.relay_log_info_repository`
--let $saved_master_repo= `SELECT @@global.master_info_repository`
--let $saved_sync_master= `SELECT @@global.sync_master_info`
SET @@global.relay_log_info_repository="TABLE";
SET @@global.master_info_repository="TABLE";
SET @@global.sync_master_info= 1;
--source include/start_slave.inc

--connection master
CREATE TABLE t_innodb (id INTEGER) engine= Innodb;

--connection slave
call mtr.add_suppression("Error writing relay log configuration.");
call mtr.add_suppression("Column count of mysql.slave_relay_log_info is wrong.");
call mtr.add_suppression("Error running query, slave SQL thread aborted.");
call mtr.add_suppression("Info table is not ready to be used.");
call mtr.add_suppression("Error writing master configuration.");
call mtr.add_suppression("Failed to flush master info.");
call mtr.add_suppression(" Error while checking replication metadata.");
call mtr.add_suppression("Failed to create or recover replication info repository.");
call mtr.add_suppression("Error in checking.* repository info type of");
call mtr.add_suppression("Error creating.*: Error checking repositories");

###############################################################################
#           Checking if replication can fail safely (SQL Thread)
###############################################################################
#
# The test executes the following steps:
#  1 - Changes mysql.slave_relay_log_info and SQL Thread stops gracefully.
#  2 - Shows that it is possible to restart the server although replication
#      is not properly configured and working.
#  3 - Shows that after fixing the problem the server can be properly
#      restared.
#  4 - Shows that trying to fix the problem with START SLAVE, RESET SLAVE may
#      not work and requires to manually back up positions to restart
#      replication.
#

#
#  1 - Changes mysql.slave_relay_log_info and SQL Thread stops gracefully.
#
--connection master
INSERT INTO t_innodb VALUES (1), (2), (3);
--source include/sync_slave_sql_with_master.inc
ALTER TABLE mysql.slave_relay_log_info DROP COLUMN Number_of_workers;
--connection master
INSERT INTO t_innodb VALUES (1), (2), (3);
--connection slave
# ER_COL_COUNT_DOESNT_MATCH_CORRUPTED_V2 1805
--let $slave_sql_errno= 1805
--let $show_slave_sql_error= 1
--source include/wait_for_slave_sql_error.inc
--source include/stop_slave_io.inc

#
#  2 - Shows that it is possible to restart the server although replication
#      is not properly configured and working.
#
--let $rpl_server_number= 2
--let $rpl_server_parameters= --sync-master-info=1 --master-info-repository=TABLE --relay-log-info-repository=TABLE
--source include/rpl_restart_server.inc
--connection slave
--error ER_SLAVE_CONFIGURATION
START SLAVE SQL_THREAD;
ALTER TABLE mysql.slave_relay_log_info ADD COLUMN Number_of_workers INTEGER UNSIGNED NOT NULL AFTER Sql_delay;
UPDATE mysql.slave_relay_log_info SET Number_of_workers= 0;

#
#  3 - Shows that after fixing the problem the server can be properly
#      restared.
#
--let $rpl_server_number= 2
--let $rpl_server_parameters= --sync-master-info=1 --master-info-repository=TABLE --relay-log-info-repository=TABLE
--source include/rpl_restart_server.inc
--connection slave
--source include/start_slave.inc

#  4 - Shows that trying to fix the problem with START SLAVE, RESET SLAVE may
#      not work and requires to manually back up positions to restart
#      replication.
#
--connection master
INSERT INTO t_innodb VALUES (1), (2), (3);
--source include/sync_slave_sql_with_master.inc
ALTER TABLE mysql.slave_relay_log_info DROP COLUMN Number_of_workers;
--connection master
INSERT INTO t_innodb VALUES (1), (2), (3);
--connection slave
# ER_COL_COUNT_DOESNT_MATCH_CORRUPTED_V2 1805
--let $slave_sql_errno= 1805
--let $show_slave_sql_error= 1
--source include/wait_for_slave_sql_error.inc
--source include/stop_slave_io.inc
--error ER_COL_COUNT_DOESNT_MATCH_CORRUPTED_V2
START SLAVE SQL_THREAD;

--let $_slave_master_host= query_get_value(SHOW SLAVE STATUS, Master_Host, 1)
--let $_slave_master_user= query_get_value(SHOW SLAVE STATUS, Master_User, 1)
--let $_slave_master_port= query_get_value(SHOW SLAVE STATUS, Master_Port, 1)
--let $_slave_master_log_file= query_get_value(SHOW SLAVE STATUS, Relay_Master_Log_File, 1)
--let $_slave_master_log_pos= query_get_value(SHOW SLAVE STATUS, Exec_Master_Log_Pos, 1)
--error ER_COL_COUNT_DOESNT_MATCH_CORRUPTED_V2
RESET SLAVE ALL;
--replace_result $_slave_master_host MASTER_HOST $_slave_master_user MASTER_USER $_slave_master_port MASTER_PORT $_slave_master_log_file MASTER_LOG_FILE $_slave_master_log_pos MASTER_LOG_POS
--replace_column 2 ####
--error ER_COL_COUNT_DOESNT_MATCH_CORRUPTED_V2
--eval CHANGE MASTER TO MASTER_HOST= '$_slave_master_host', MASTER_USER= '$_slave_master_user', MASTER_PORT= $_slave_master_port, MASTER_LOG_FILE= '$_slave_master_log_file', MASTER_LOG_POS= $_slave_master_log_pos
ALTER TABLE mysql.slave_relay_log_info ADD COLUMN Number_of_workers INTEGER UNSIGNED NOT NULL AFTER Sql_delay;
UPDATE mysql.slave_relay_log_info SET Number_of_workers= 0;
RESET SLAVE ALL;
--replace_result $_slave_master_host MASTER_HOST $_slave_master_user MASTER_USER $_slave_master_port MASTER_PORT $_slave_master_log_file MASTER_LOG_FILE $_slave_master_log_pos MASTER_LOG_POS
--replace_column 2 ####
--eval CHANGE MASTER TO MASTER_HOST= '$_slave_master_host', MASTER_USER= '$_slave_master_user', MASTER_PORT= $_slave_master_port, MASTER_LOG_FILE= '$_slave_master_log_file', MASTER_LOG_POS= $_slave_master_log_pos
--source include/start_slave.inc
--connection master
--source include/sync_slave_sql_with_master.inc

###############################################################################
#          Checking if replication can fail safely (I/O Thread)
###############################################################################
#
# The test executes the following steps:
#  1 - Changes mysql.slave_master_info and SQL Thread stops gracefully.
#  2 - Shows that it is possible to restart the server although replication
#      is not properly configured and working.
#  3 - Shows that after fixing the problem the server can be properly
#      restared.
#  4 - Shows that trying to fix the problem with START SLAVE, RESET SLAVE may
#      not work and requires to manually back up positions to restart
#      replication.
#

#
#  1 - Changes mysql.slave_master_info and SQL Thread stops gracefully.
#
--connection slave
ALTER TABLE mysql.slave_master_info DROP COLUMN Enabled_auto_position;
--connection master
INSERT INTO t_innodb VALUES (1), (2), (3);
--connection slave
# ER_SLAVE_FATAL_ERROR
--let $slave_io_errno= 1593
--let $show_slave_io_error= 1
--source include/wait_for_slave_io_error.inc
--source include/stop_slave_sql.inc

#
#  2 - Shows that it is possible to restart the server although replication
#      is not properly configured and working.
#
--let $rpl_server_number= 2
--let $rpl_server_parameters= --sync-master-info=1 --master-info-repository=TABLE --relay-log-info-repository=TABLE
--source include/rpl_restart_server.inc
--connection slave
--error ER_SLAVE_CONFIGURATION
START SLAVE SQL_THREAD;
ALTER TABLE mysql.slave_master_info ADD COLUMN (Enabled_auto_position BOOLEAN NOT NULL COMMENT 'Indicates whether GTIDs will be used to retrieve events from the master.');
UPDATE mysql.slave_master_info SET Enabled_auto_position= 0;

#
#  3 - Shows that after fixing the problem the server can be properly
#      restared.
#
--let $rpl_server_number= 2
--let $rpl_server_parameters= --sync-master-info=1 --master-info-repository=TABLE --relay-log-info-repository=TABLE
--source include/rpl_restart_server.inc
--connection slave

--source include/start_slave.inc

#  4 - Shows that trying to fix the problem with START SLAVE, RESET SLAVE may
#      not work and requires to manually back up positions to restart
#      replication.
#
--connection slave
ALTER TABLE mysql.slave_master_info DROP COLUMN Enabled_auto_position;
--connection master
INSERT INTO t_innodb VALUES (1), (2), (3);
--connection slave
# ER_SLAVE_FATAL_ERROR
--let $slave_io_errno= 1593
--let $show_slave_io_error= 1
--source include/wait_for_slave_io_error.inc
--source include/stop_slave_sql.inc
--error ER_COL_COUNT_DOESNT_MATCH_CORRUPTED_V2
START SLAVE SQL_THREAD;

--let $_slave_master_host= query_get_value(SHOW SLAVE STATUS, Master_Host, 1)
--let $_slave_master_user= query_get_value(SHOW SLAVE STATUS, Master_User, 1)
--let $_slave_master_port= query_get_value(SHOW SLAVE STATUS, Master_Port, 1)
--let $_slave_master_log_file= query_get_value(SHOW SLAVE STATUS, Relay_Master_Log_File, 1)
--let $_slave_master_log_pos= query_get_value(SHOW SLAVE STATUS, Exec_Master_Log_Pos, 1)
--error ER_COL_COUNT_DOESNT_MATCH_CORRUPTED_V2
RESET SLAVE ALL;
--replace_result $_slave_master_host MASTER_HOST $_slave_master_user MASTER_USER $_slave_master_port MASTER_PORT $_slave_master_log_file MASTER_LOG_FILE $_slave_master_log_pos MASTER_LOG_POS
--replace_column 2 ####
--error ER_COL_COUNT_DOESNT_MATCH_CORRUPTED_V2
--eval CHANGE MASTER TO MASTER_HOST= '$_slave_master_host', MASTER_USER= '$_slave_master_user', MASTER_PORT= $_slave_master_port, MASTER_LOG_FILE= '$_slave_master_log_file', MASTER_LOG_POS= $_slave_master_log_pos
ALTER TABLE mysql.slave_master_info ADD COLUMN Enabled_auto_position BOOLEAN NOT NULL COMMENT 'Indicates whether GTIDs will be used to retrieve events from the master.' AFTER Ssl_crlpath;
UPDATE mysql.slave_master_info SET Enabled_auto_position= 0;
RESET SLAVE ALL;
--replace_result $_slave_master_host MASTER_HOST $_slave_master_user MASTER_USER $_slave_master_port MASTER_PORT $_slave_master_log_file MASTER_LOG_FILE $_slave_master_log_pos MASTER_LOG_POS
--replace_column 2 ####
--eval CHANGE MASTER TO MASTER_HOST= '$_slave_master_host', MASTER_USER= '$_slave_master_user', MASTER_PORT= $_slave_master_port, MASTER_LOG_FILE= '$_slave_master_log_file', MASTER_LOG_POS= $_slave_master_log_pos
--source include/start_slave.inc
--connection master
--source include/sync_slave_sql_with_master.inc

###############################################################################
# Checking ANALYZE TABLE, CHECK TABLE, CHECKSUM TABLE, OPTIMIZE TABLE,
#          REPAIR TABLE, SHOW CREATE TABLE and MYSQLDUMP
###############################################################################

#
# Check ANALYZE TABLE, CHECK TABLE and CHECKSUM TABLE
#
ANALYZE TABLE mysql.slave_master_info, mysql.slave_relay_log_info;
CHECK TABLE mysql.slave_master_info, mysql.slave_relay_log_info EXTENDED;

#
# Check OPTIMIZE TABLE and REPAIR TABLE
#
OPTIMIZE TABLE mysql.slave_master_info, mysql.slave_relay_log_info;
REPAIR TABLE mysql.slave_master_info, mysql.slave_relay_log_info EXTENDED;

#
# Check SHOW CREATE TABLE
#
SHOW CREATE TABLE mysql.slave_master_info;
SHOW CREATE TABLE mysql.slave_relay_log_info;

#
# Check MYSQLDUMP
#
--echo # Search for occurrences of slave_master_info in the output from mysqldump
--let $MYSQLD_DATADIR= `select @@datadir`
--exec $MYSQL_DUMP mysql slave_master_info slave_relay_log_info > $MYSQLTEST_VARDIR/tmp/accessing_repository.sql
--let OUTF=$MYSQLTEST_VARDIR/tmp/accessing_repository.sql
perl;
  use strict;
  my $outf= $ENV{'OUTF'} or die "OUTF not set";
  open(FILE, "$outf") or die("Unable to open $outf: $!\n");
  my $count = () = grep(/CREATE TABLE `slave_master_info`/gi,<FILE>);
  print "- Occurrences: $count\n";
  close(FILE);
EOF
--remove_file $MYSQLTEST_VARDIR/tmp/accessing_repository.sql

###############################################################################
# Cleaning up
###############################################################################
--connection master
DROP TABLE t_innodb;
--source include/sync_slave_sql_with_master.inc

--source include/stop_slave.inc

--disable_query_log
--eval SET @@global.relay_log_info_repository="$saved_relay_repo"
--eval SET @@global.master_info_repository="$saved_master_repo"
--eval SET @@global.sync_master_info=$saved_sync_master
--enable_query_log

--query_vertical SELECT * FROM mysql.slave_master_info
--query_vertical SELECT * FROM mysql.slave_relay_log_info

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

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

--source include/rpl_end.inc
