###############################################################################
# This test evaluates that when donor and joiner have a different server
# character set, recovery through clone will fail and will fall back to
# incremental recovery.
#
# 1. Install clone and keyring plugins on server1.
# 2. Bootstrap server1 and add some data
# 3. Restart server 2 with a monitoring process (mysqld_safe) if needed
# 4. Install the clone plugin on server2.
# 5. Set server character set to latin1 (non-default)
# 6. On a empty server2 start group replication
#    Clone will fail since the donor server character set utf8mb4 (default) is
#    different from joiner character set. Recovery will fallback to incremental.
# 7. Cleanup
###############################################################################
--source include/have_mysqld_monitoring_process.inc
--source include/have_clone_plugin.inc
--source include/have_group_replication_plugin.inc
--let $rpl_skip_group_replication_start= 1
--source include/group_replication.inc

--let plugins = CLONE_PLUGIN,GROUP_REPLICATION,KEYRING_PLUGIN
--source include/check_plugin_dir.inc

--echo
--echo ############################################################
--echo # 1. Install clone and keyring plugins on server1.

--let $rpl_connection_name= server1
--source include/rpl_connection.inc

--replace_result $CLONE_PLUGIN CLONE_PLUGIN
--eval INSTALL PLUGIN clone SONAME '$CLONE_PLUGIN'

--replace_result $KEYRING_PLUGIN KEYRING_PLUGIN
--eval INSTALL PLUGIN keyring_file SONAME '$KEYRING_PLUGIN'

--echo
--echo ############################################################
--echo # 2. Bootstrap server1 and add some data

--source include/start_and_bootstrap_group_replication.inc

CREATE TABLE t1 (c1 INT NOT NULL PRIMARY KEY) ENGINE=InnoDB;
INSERT INTO t1 VALUES (1);
INSERT INTO t1 VALUES (2);

--echo
--echo #######################################################################
--echo # 3. Restart server 2 with a monitoring process (mysqld_safe) if needed

--let $rpl_connection_name= server2
--source include/rpl_connection.inc

--let $_group_replication_local_address= `SELECT @@GLOBAL.group_replication_local_address`
--let $_group_replication_group_seeds= `SELECT @@GLOBAL.group_replication_group_seeds`

--let $plugin_list= $GROUP_REPLICATION
--source include/spawn_monitoring_process.inc

--echo
--echo ############################################################
--echo # 4. Install the clone plugin on server2.

--replace_result $CLONE_PLUGIN CLONE_PLUGIN
--eval INSTALL PLUGIN clone SONAME '$CLONE_PLUGIN'

--echo
--echo ############################################################
--echo # 5. Set server character set to latin1 (non-default)

SET @@global.character_set_server = latin5;

--echo
--echo ############################################################
--echo # 6. On a empty server2 start group replication
--echo #    Clone will fail since the donor server character set utf8mb4 (default) is
--echo #    different from joiner character set. Recovery will fallback to incremental.

SET @group_replication_clone_threshold_saved = @@GLOBAL.group_replication_clone_threshold;
SET GLOBAL group_replication_clone_threshold= 1;

--disable_query_log
--eval SET GLOBAL group_replication_local_address= "$_group_replication_local_address"
--eval SET GLOBAL group_replication_group_seeds= "$_group_replication_group_seeds"
--enable_query_log

--source include/start_group_replication.inc

--let $diff_tables=server1:test.t1 ,server2:test.t1
--source include/diff_tables.inc

--let $assert_text= Clone should have failed
--let $assert_cond= [SELECT state="Failed" FROM performance_schema.clone_status] = 1;
--source include/assert.inc

--let $assert_file= $MYSQLTEST_VARDIR/log/mysqld.2.err
--let $assert_only_after = CURRENT_TEST: gr_clone_integration_error_fallback
--let $assert_text = Clone failed
--let $assert_select = There was an issue when cloning from another server: Error number: 3868 Error message: Clone Configuration character_set_server: Donor value: utf8mb4 is different from Recipient value: latin5.'
--let $assert_count = 1
--source include/assert_grep.inc

--echo
--echo ############################################################
--echo # 7. Cleanup

SET GLOBAL group_replication_clone_threshold= @group_replication_clone_threshold_saved;

DROP TABLE t1;
--source include/rpl_sync.inc

--let $rpl_connection_name= server2
--source include/rpl_connection.inc

--source include/clean_monitoring_process.inc

set session sql_log_bin=0;
call mtr.add_suppression("This member will start distributed recovery using clone. It is due to the number of missing transactions being higher than the configured threshold of 1.");
call mtr.add_suppression("Internal query: CLONE INSTANCE FROM \'root\'@\'127.0.0.1\':[0-9]+ IDENTIFIED BY \'\\*\\*\\*\\*\\*\' REQUIRE NO SSL; result in error. Error number: 3868");
call mtr.add_suppression("There was an issue when cloning from another server: Error number: 3868 Error message: Clone Configuration character_set_server: Donor value: utf8mb4 is different from Recipient value: latin5.");
call mtr.add_suppression("Due to some issue on the previous step distributed recovery is now executing: Incremental Recovery.");
set session sql_log_bin=1;

--let $rpl_connection_name= server1
--source include/rpl_connection.inc

UNINSTALL PLUGIN clone;
UNINSTALL PLUGIN keyring_file;

--remove_file $MYSQL_TMP_DIR/gr_clone_integration_error_fallback

--let $rpl_connection_name= server2
--source include/rpl_connection.inc

UNINSTALL PLUGIN clone;
SET @@global.character_set_server = default;

--source include/group_replication_end.inc
