--source include/have_debug.inc
--source include/have_innodb.inc
--source include/master-slave.inc

connection master;
SET @@session.gtid_domain_id= 1;
SELECT @@session.gtid_domain_id;
CREATE TABLE t1(i INT) ENGINE=INNODB;
INSERT INTO t1 VALUES(1);
SELECT * FROM t1;
sync_slave_with_master;
--source include/save_master_gtid.inc

connection slave;

call mtr.add_suppression("Slave I/O: Relay log write failure: could not queue event from master.*");

--echo # Case 0 : Start slave with IGNORE_DOMAIN_IDS=(), then restart
--echo #          replication with IGNORE_DOMAIN_IDS=() after IO thread is
--echo #          killed due to DBUG_EXECUTE_IF("+d,kill_slave_io_before_commit").

SELECT * FROM t1;

--source include/stop_slave.inc
let $do_domain_ids_before= query_get_value(SHOW SLAVE STATUS, Replicate_Do_Domain_Ids, 1);
let $ignore_domain_ids_before= query_get_value(SHOW SLAVE STATUS, Replicate_Ignore_Domain_Ids, 1);
--echo DO_DOMAIN_IDS (BEFORE)     : $do_domain_ids_before
--echo IGNORE_DOMAIN_IDS (BEFORE) : $ignore_domain_ids_before

CHANGE MASTER TO IGNORE_DOMAIN_IDS=(), MASTER_USE_GTID=slave_pos;
--source include/start_slave.inc
--source include/sync_with_master_gtid.inc

let $do_domain_ids_after= query_get_value(SHOW SLAVE STATUS, Replicate_Do_Domain_Ids, 1);
let $ignore_domain_ids_after= query_get_value(SHOW SLAVE STATUS, Replicate_Ignore_Domain_Ids, 1);
--echo DO_DOMAIN_IDS (AFTER)     : $do_domain_ids_after
--echo IGNORE_DOMAIN_IDS (AFTER) : $ignore_domain_ids_after

SET @saved_dbug = @@GLOBAL.debug_dbug;
SET @@global.debug_dbug="d,kill_slave_io_before_commit";

connection master;

START TRANSACTION;
INSERT INTO t1 VALUES(2);
INSERT INTO t1 VALUES(3);
COMMIT;
--source include/save_master_gtid.inc
SELECT * FROM t1;

connection slave;
--let $slave_io_errno= 1595
--source include/wait_for_slave_io_error.inc
SELECT * FROM t1;
SET @@global.debug_dbug=@saved_dbug;

START SLAVE io_thread;
--source include/wait_for_slave_io_to_start.inc
--source include/sync_with_master_gtid.inc
SELECT * FROM t1;

--echo # Case 1 : Start slave with IGNORE_DOMAIN_IDS=(1), then restart
--echo #          replication with IGNORE_DOMAIN_IDS=(1) after IO thread is
--echo #          killed due to DBUG_EXECUTE_IF("+d,kill_slave_io_before_commit").

--source include/stop_slave.inc
let $do_domain_ids_before= query_get_value(SHOW SLAVE STATUS, Replicate_Do_Domain_Ids, 1);
let $ignore_domain_ids_before= query_get_value(SHOW SLAVE STATUS, Replicate_Ignore_Domain_Ids, 1);
--echo DO_DOMAIN_IDS (BEFORE)     : $do_domain_ids_before
--echo IGNORE_DOMAIN_IDS (BEFORE) : $ignore_domain_ids_before

CHANGE MASTER TO IGNORE_DOMAIN_IDS=(1), MASTER_USE_GTID=slave_pos;
--source include/start_slave.inc
--source include/sync_with_master_gtid.inc

let $do_domain_ids_after= query_get_value(SHOW SLAVE STATUS, Replicate_Do_Domain_Ids, 1);
let $ignore_domain_ids_after= query_get_value(SHOW SLAVE STATUS, Replicate_Ignore_Domain_Ids, 1);
--echo DO_DOMAIN_IDS (AFTER)     : $do_domain_ids_after
--echo IGNORE_DOMAIN_IDS (AFTER) : $ignore_domain_ids_after

SET @@global.debug_dbug="d,kill_slave_io_before_commit";

connection master;

START TRANSACTION;
INSERT INTO t1 VALUES(4);
INSERT INTO t1 VALUES(5);
COMMIT;

--source include/save_master_gtid.inc
SELECT * FROM t1;

connection slave;
--let $slave_io_errno= 1595
--source include/wait_for_slave_io_error.inc
SELECT * FROM t1;
SET @@global.debug_dbug=@saved_dbug;

START SLAVE io_thread;
--source include/wait_for_slave_io_to_start.inc
--source include/sync_with_master_gtid.inc
SELECT * FROM t1;

--echo # Case 2 : Start slave with IGNORE_DOMAIN_IDS=(), then restart
--echo #          replication with IGNORE_DOMAIN_IDS=(1) after IO thread is
--echo #          killed due to DBUG_EXECUTE_IF("+d,kill_slave_io_before_commit").

--source include/stop_slave.inc
let $do_domain_ids_before= query_get_value(SHOW SLAVE STATUS, Replicate_Do_Domain_Ids, 1);
let $ignore_domain_ids_before= query_get_value(SHOW SLAVE STATUS, Replicate_Ignore_Domain_Ids, 1);
--echo DO_DOMAIN_IDS (BEFORE)     : $do_domain_ids_before
--echo IGNORE_DOMAIN_IDS (BEFORE) : $ignore_domain_ids_before

CHANGE MASTER TO IGNORE_DOMAIN_IDS=(), MASTER_USE_GTID=slave_pos;
--source include/start_slave.inc
--source include/sync_with_master_gtid.inc

let $do_domain_ids_after= query_get_value(SHOW SLAVE STATUS, Replicate_Do_Domain_Ids, 1);
let $ignore_domain_ids_after= query_get_value(SHOW SLAVE STATUS, Replicate_Ignore_Domain_Ids, 1);
--echo DO_DOMAIN_IDS (AFTER)     : $do_domain_ids_after
--echo IGNORE_DOMAIN_IDS (AFTER) : $ignore_domain_ids_after

SET @@global.debug_dbug="d,kill_slave_io_before_commit";

connection master;

START TRANSACTION;
INSERT INTO t1 VALUES(6);
INSERT INTO t1 VALUES(7);
COMMIT; # IO thread gets killed here.

START TRANSACTION;
INSERT INTO t1 VALUES(8);
INSERT INTO t1 VALUES(9);
COMMIT;

SET @@session.gtid_domain_id= 2;

START TRANSACTION;
INSERT INTO t1 VALUES(10);
INSERT INTO t1 VALUES(11);
COMMIT;

--source include/save_master_gtid.inc
SELECT * FROM t1;

connection slave;
--let $slave_io_errno= 1595
--source include/wait_for_slave_io_error.inc
SELECT * FROM t1;

SET @@global.debug_dbug=@saved_dbug;
--source include/stop_slave_sql.inc
let $do_domain_ids_before= query_get_value(SHOW SLAVE STATUS, Replicate_Do_Domain_Ids, 1);
let $ignore_domain_ids_before= query_get_value(SHOW SLAVE STATUS, Replicate_Ignore_Domain_Ids, 1);
--echo DO_DOMAIN_IDS (BEFORE)     : $do_domain_ids_before
--echo IGNORE_DOMAIN_IDS (BEFORE) : $ignore_domain_ids_before

CHANGE MASTER TO IGNORE_DOMAIN_IDS=(1), MASTER_USE_GTID=slave_pos;
--source include/start_slave.inc
--source include/sync_with_master_gtid.inc

let $do_domain_ids_after= query_get_value(SHOW SLAVE STATUS, Replicate_Do_Domain_Ids, 1);
let $ignore_domain_ids_after= query_get_value(SHOW SLAVE STATUS, Replicate_Ignore_Domain_Ids, 1);
--echo DO_DOMAIN_IDS (AFTER)     : $do_domain_ids_after
--echo IGNORE_DOMAIN_IDS (AFTER) : $ignore_domain_ids_after

SELECT * FROM t1;

--echo # Case 3 : Start slave with IGNORE_DOMAIN_IDS=(1), then restart
--echo #          replication with IGNORE_DOMAIN_IDS=() after IO thread is
--echo #          killed due to DBUG_EXECUTE_IF("+d,kill_slave_io_before_commit").

--source include/stop_slave.inc
let $do_domain_ids_before= query_get_value(SHOW SLAVE STATUS, Replicate_Do_Domain_Ids, 1);
let $ignore_domain_ids_before= query_get_value(SHOW SLAVE STATUS, Replicate_Ignore_Domain_Ids, 1);
--echo DO_DOMAIN_IDS (BEFORE)     : $do_domain_ids_before
--echo IGNORE_DOMAIN_IDS (BEFORE) : $ignore_domain_ids_before

CHANGE MASTER TO IGNORE_DOMAIN_IDS=(1), MASTER_USE_GTID=slave_pos;
--source include/start_slave.inc
--source include/sync_with_master_gtid.inc

let $do_domain_ids_after= query_get_value(SHOW SLAVE STATUS, Replicate_Do_Domain_Ids, 1);
let $ignore_domain_ids_after= query_get_value(SHOW SLAVE STATUS, Replicate_Ignore_Domain_Ids, 1);
--echo DO_DOMAIN_IDS (AFTER)     : $do_domain_ids_after
--echo IGNORE_DOMAIN_IDS (AFTER) : $ignore_domain_ids_after

SET @@global.debug_dbug="d,kill_slave_io_before_commit";

connection master;

SET @@session.gtid_domain_id= 1;

START TRANSACTION;
INSERT INTO t1 VALUES(12);
INSERT INTO t1 VALUES(13);
COMMIT; # IO thread gets killed here.

# MDEV-14357
# As the prior transaction will be ignored on slave because its domain id is
# ignored, the replica's gtid_slave_pos will be updated to have seen it,
# despite its eventual failure to queue the whole transaction to the relay log.
# So for test consistency, we need to synchronize the SQL thread with this
# position; otherwise, when restarting the server after resetting
# IGNORE_DOMAIN_IDS, we will re-fetch this event and execute it.
--source include/save_master_gtid.inc

START TRANSACTION;
INSERT INTO t1 VALUES(14);
INSERT INTO t1 VALUES(15);
COMMIT;

SET @@session.gtid_domain_id= 2;
START TRANSACTION;
INSERT INTO t1 VALUES(16);
INSERT INTO t1 VALUES(17);
COMMIT;

SELECT * FROM t1;

connection slave;
--let $slave_io_errno= 1595
--source include/wait_for_slave_io_error.inc
SELECT * FROM t1;

SET @@global.debug_dbug=@saved_dbug;

# MDEV-14357
# Ensure the SQL thread is updated with the GTID of the ignored transaction
# so we don't fetch it and execute it after restarting without any ignored
# domain ids.
--source include/sync_with_master_gtid.inc
--source include/stop_slave_sql.inc
let $do_domain_ids_before= query_get_value(SHOW SLAVE STATUS, Replicate_Do_Domain_Ids, 1);
let $ignore_domain_ids_before= query_get_value(SHOW SLAVE STATUS, Replicate_Ignore_Domain_Ids, 1);
--echo DO_DOMAIN_IDS (BEFORE)     : $do_domain_ids_before
--echo IGNORE_DOMAIN_IDS (BEFORE) : $ignore_domain_ids_before

CHANGE MASTER TO IGNORE_DOMAIN_IDS=(), MASTER_USE_GTID=slave_pos;

--connection master
--source include/save_master_gtid.inc
--connection slave
--source include/start_slave.inc
--source include/sync_with_master_gtid.inc

let $do_domain_ids_after= query_get_value(SHOW SLAVE STATUS, Replicate_Do_Domain_Ids, 1);
let $ignore_domain_ids_after= query_get_value(SHOW SLAVE STATUS, Replicate_Ignore_Domain_Ids, 1);
--echo DO_DOMAIN_IDS (AFTER)     : $do_domain_ids_after
--echo IGNORE_DOMAIN_IDS (AFTER) : $ignore_domain_ids_after

SELECT * FROM t1;

--echo # Case 4 : Start slave with IGNORE_DOMAIN_IDS=(1), then restart
--echo #          replication with IGNORE_DOMAIN_IDS=() after IO thread is
--echo #          killed due to DBUG_EXECUTE_IF("+d,kill_slave_io_after_2_events").

--source include/stop_slave.inc
let $do_domain_ids_before= query_get_value(SHOW SLAVE STATUS, Replicate_Do_Domain_Ids, 1);
let $ignore_domain_ids_before= query_get_value(SHOW SLAVE STATUS, Replicate_Ignore_Domain_Ids, 1);
--echo DO_DOMAIN_IDS (BEFORE)     : $do_domain_ids_before
--echo IGNORE_DOMAIN_IDS (BEFORE) : $ignore_domain_ids_before

CHANGE MASTER TO IGNORE_DOMAIN_IDS=(1), MASTER_USE_GTID=slave_pos;
--source include/start_slave.inc
--source include/sync_with_master_gtid.inc

let $do_domain_ids_after= query_get_value(SHOW SLAVE STATUS, Replicate_Do_Domain_Ids, 1);
let $ignore_domain_ids_after= query_get_value(SHOW SLAVE STATUS, Replicate_Ignore_Domain_Ids, 1);
--echo DO_DOMAIN_IDS (AFTER)     : $do_domain_ids_after
--echo IGNORE_DOMAIN_IDS (AFTER) : $ignore_domain_ids_after

SET @@global.debug_dbug="d,kill_slave_io_after_2_events";

connection master;

SET @@session.gtid_domain_id= 1;

START TRANSACTION;
INSERT INTO t1 VALUES(18);
INSERT INTO t1 VALUES(19); # IO thread gets killed here.
COMMIT;

# MDEV-14357
# Synchronize gtid_slave_pos with the ignored event. See prior comments about
# MDEV-14357 for details.
--source include/save_master_gtid.inc

START TRANSACTION;
INSERT INTO t1 VALUES(20);
INSERT INTO t1 VALUES(21);
COMMIT;

SET @@session.gtid_domain_id= 2;
START TRANSACTION;
INSERT INTO t1 VALUES(22);
INSERT INTO t1 VALUES(23);
COMMIT;

SELECT * FROM t1;

connection slave;
--let $slave_io_errno= 1595
--source include/wait_for_slave_io_error.inc
SELECT * FROM t1;

SET @@global.debug_dbug=@saved_dbug;

# MDEV-14357
# Synchronize gtid_slave_pos with the ignored event. See prior comments about
# MDEV-14357 for details.
--source include/sync_with_master_gtid.inc
--source include/stop_slave_sql.inc
let $do_domain_ids_before= query_get_value(SHOW SLAVE STATUS, Replicate_Do_Domain_Ids, 1);
let $ignore_domain_ids_before= query_get_value(SHOW SLAVE STATUS, Replicate_Ignore_Domain_Ids, 1);
--echo DO_DOMAIN_IDS (BEFORE)     : $do_domain_ids_before
--echo IGNORE_DOMAIN_IDS (BEFORE) : $ignore_domain_ids_before

CHANGE MASTER TO IGNORE_DOMAIN_IDS=(), MASTER_USE_GTID=slave_pos;

--connection master
--source include/save_master_gtid.inc
--connection slave
--source include/start_slave.inc
--source include/sync_with_master_gtid.inc

let $do_domain_ids_after= query_get_value(SHOW SLAVE STATUS, Replicate_Do_Domain_Ids, 1);
let $ignore_domain_ids_after= query_get_value(SHOW SLAVE STATUS, Replicate_Ignore_Domain_Ids, 1);
--echo DO_DOMAIN_IDS (AFTER)     : $do_domain_ids_after
--echo IGNORE_DOMAIN_IDS (AFTER) : $ignore_domain_ids_after

SELECT * FROM t1;

--echo # Case 5 : Start slave with IGNORE_DOMAIN_IDS=(), then restart
--echo #          replication with IGNORE_DOMAIN_IDS=(1) after IO thread is
--echo #          killed due to DBUG_EXECUTE_IF("+d,kill_slave_io_after_2_events").

--source include/stop_slave.inc
let $do_domain_ids_before= query_get_value(SHOW SLAVE STATUS, Replicate_Do_Domain_Ids, 1);
let $ignore_domain_ids_before= query_get_value(SHOW SLAVE STATUS, Replicate_Ignore_Domain_Ids, 1);
--echo DO_DOMAIN_IDS (BEFORE)     : $do_domain_ids_before
--echo IGNORE_DOMAIN_IDS (BEFORE) : $ignore_domain_ids_before

CHANGE MASTER TO IGNORE_DOMAIN_IDS=(), MASTER_USE_GTID=slave_pos;
--source include/start_slave.inc
--source include/sync_with_master_gtid.inc

let $do_domain_ids_after= query_get_value(SHOW SLAVE STATUS, Replicate_Do_Domain_Ids, 1);
let $ignore_domain_ids_after= query_get_value(SHOW SLAVE STATUS, Replicate_Ignore_Domain_Ids, 1);
--echo DO_DOMAIN_IDS (AFTER)     : $do_domain_ids_after
--echo IGNORE_DOMAIN_IDS (AFTER) : $ignore_domain_ids_after

SET @@global.debug_dbug="d,kill_slave_io_after_2_events";

connection master;

SET @@session.gtid_domain_id= 1;

START TRANSACTION;
INSERT INTO t1 VALUES(24);
INSERT INTO t1 VALUES(25); # IO thread gets killed here.
COMMIT;

START TRANSACTION;
INSERT INTO t1 VALUES(26);
INSERT INTO t1 VALUES(27);
COMMIT;

SET @@session.gtid_domain_id= 2;
START TRANSACTION;
INSERT INTO t1 VALUES(28);
INSERT INTO t1 VALUES(29);
COMMIT;

--source include/save_master_gtid.inc
SELECT * FROM t1;

connection slave;
--let $slave_io_errno= 1595
--source include/wait_for_slave_io_error.inc
SELECT * FROM t1;

SET @@global.debug_dbug=@saved_dbug;

--source include/stop_slave_sql.inc
let $do_domain_ids_before= query_get_value(SHOW SLAVE STATUS, Replicate_Do_Domain_Ids, 1);
let $ignore_domain_ids_before= query_get_value(SHOW SLAVE STATUS, Replicate_Ignore_Domain_Ids, 1);
--echo DO_DOMAIN_IDS (BEFORE)     : $do_domain_ids_before
--echo IGNORE_DOMAIN_IDS (BEFORE) : $ignore_domain_ids_before

CHANGE MASTER TO IGNORE_DOMAIN_IDS=(1), MASTER_USE_GTID=slave_pos;
--source include/start_slave.inc
--source include/sync_with_master_gtid.inc

let $do_domain_ids_after= query_get_value(SHOW SLAVE STATUS, Replicate_Do_Domain_Ids, 1);
let $ignore_domain_ids_after= query_get_value(SHOW SLAVE STATUS, Replicate_Ignore_Domain_Ids, 1);
--echo DO_DOMAIN_IDS (AFTER)     : $do_domain_ids_after
--echo IGNORE_DOMAIN_IDS (AFTER) : $ignore_domain_ids_after

SELECT * FROM t1;

# Cleanup
connection master;
DROP TABLE t1;
sync_slave_with_master;

connection slave;
--source include/stop_slave.inc
CHANGE MASTER TO DO_DOMAIN_IDS=(), IGNORE_DOMAIN_IDS=();
--source include/start_slave.inc
SET @@GLOBAL.debug_dbug = @saved_dbug;
--source include/rpl_end.inc
