###############################################################################
# This test verifies that if the recovery endpoints has endpoint with IPv6 and
# IPV4, clone will IPv4 due to lack of support IPv6 and incremental recovery
# will use IPv6.
#
# Test:
#   0. The test requires two servers
#   1. Install clone plugin on server1.
#   2. Configure recovery endpoints with two valid endpoints
#   3. Bootstrap server1 and add some data
#   4. Restart server2 with a monitoring process (mysqld_safe) if needed
#   5. Setup the server so group replication starts on boot
#      Install the Clone plugin
#   6. Ensure clone is used on recovery
#   7. On a empty server2 start group replication
#   8. Clone will fail connecting to first endpoint IPv6 and shall use IPv4
#   9. Distributed recovery shall use first endpoint IPv6
#  10. Clone shall succeed and all data retrieved from server1
#  11. Cleanup
#
###############################################################################

--source include/have_ipv6.inc
--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

--echo
--echo # 1. Install clone plugin on server1.

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

--let $server1_port= `SELECT @@GLOBAL.PORT`

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

--echo
--echo # 2. Configure recovery endpoints with two valid endpoints

--replace_result $server1_port SERVER1_PORT
--eval SET @@GLOBAL.group_replication_advertise_recovery_endpoints = "[::1]:$server1_port,127.0.0.1:$server1_port"


--echo
--echo # 3. 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 # 4. Restart server2 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 $_group_replication_start_on_boot= `SELECT @@GLOBAL.group_replication_start_on_boot`

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

--echo
--echo # 5. Setup the server so group replication starts on boot
--echo #    Install the Clone plugin

--disable_query_log
--eval SET PERSIST group_replication_group_name= "$group_replication_group_name"
--eval SET PERSIST group_replication_local_address= "$_group_replication_local_address"
--eval SET PERSIST group_replication_group_seeds= "$_group_replication_group_seeds"

SET PERSIST group_replication_start_on_boot= ON;
--enable_query_log

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

--echo
--echo # 6. Ensure clone is used on recovery

--let $_group_replication_threshold_save= `SELECT @@GLOBAL.group_replication_clone_threshold`
SET GLOBAL group_replication_clone_threshold= 1;

--echo
--echo # 7. On a empty server2 start group replication

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

START GROUP_REPLICATION;

--source include/wait_until_disconnected.inc

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

--let $group_replication_member_state=ONLINE
--source include/gr_wait_for_member_state.inc

--echo # 8. Clone will fail connecting to first endpoint IPv6 and shall use IPv4

--let $assert_only_after = CURRENT_TEST: group_replication.gr_recovery_endpoints_ipv6_ipv4
--let $assert_file= $MYSQLTEST_VARDIR/log/mysqld.2.err
--let $assert_count = 1
--let $assert_select = .*SET GLOBAL clone_valid_donor_list = '::1:$server1_port' result in error.*
--let $assert_text = clone fail setting IPv6 donor
--source include/assert_grep.inc

--echo
--echo # 9. Distributed recovery shall use first endpoint IPv6

--let $assert_only_after = CURRENT_TEST: group_replication.gr_recovery_endpoints_ipv6_ipv4
--let $assert_file= $MYSQLTEST_VARDIR/log/mysqld.2.err
--let $assert_count = 1
--let $assert_select = .*connected to master 'root@::1:$server1_port',replication started.*
--let $assert_text = recovery channel used recovery endpoints configuration
--source include/assert_grep.inc

--echo
--echo # 10. Clone shall succeed and all data retrieved from server1

--let $assert_text= Clone must be completed
--let $assert_cond= [SELECT state="Completed" FROM performance_schema.clone_status] = 1;
--source include/assert.inc

# See if the data has been properly cloned in server3
--let $diff_tables=server1:test.t1 ,server2:test.t1
--source include/diff_tables.inc

--echo
--echo # 11. Cleanup

--eval SET GLOBAL group_replication_clone_threshold= $_group_replication_threshold_save

RESET PERSIST IF EXISTS group_replication_group_name;
RESET PERSIST IF EXISTS group_replication_local_address;
RESET PERSIST IF EXISTS group_replication_group_seeds;
RESET PERSIST IF EXISTS group_replication_start_on_boot;

--replace_result $_group_replication_start_on_boot START_ON_BOOT_VALUE
--eval SET GLOBAL group_replication_start_on_boot= $_group_replication_start_on_boot

DROP TABLE t1;

--source include/rpl_sync.inc

set session sql_log_bin=0;
call mtr.add_suppression("Clone removing all user data for provisioning: Started");
call mtr.add_suppression("Clone removing all user data for provisioning: Finished");
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(".*SET GLOBAL clone_valid_donor_list = '::1:.*' result in error.*");
call mtr.add_suppression("There was an issue when configuring the remote cloning process: Error while updating the clone donor list. Error number: 3869.*");
set session sql_log_bin=1;

--source include/clean_monitoring_process.inc

UNINSTALL PLUGIN clone;

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

--eval SET @@GLOBAL.group_replication_advertise_recovery_endpoints= "DEFAULT"

UNINSTALL PLUGIN clone;

--source include/group_replication_end.inc
