summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSachin <sachinsetia1001@gmail.com>2021-01-29 11:59:14 +0000
committerAndrei <andrei.elkin@mariadb.com>2021-11-16 19:38:35 +0200
commitf9f578c7db1924456938a061b3c6287cd09c3963 (patch)
tree820b9bebd6e9ff52f11d0f3d75eed3f3343fcbf1
parent5566cbadb03856aba9c236b131f544490cd2bee4 (diff)
downloadmariadb-git-bb-10.8-MENT-662-tmp.tar.gz
MDEV-662 Lag Free Alter On Slavebb-10.8-MENT-662-tmp
This commit implements two phase binloggable ALTER. When a new @@session.binlog_alter_two_phase = YES ALTER query gets logged in two parts, the START ALTER and the COMMIT or ROLLBACK ALTER. START Alter is written in binlog as soon as at its handling necessary locks are acquired for the table. The timing is such that any concurrent DML:s that update the same table are either committed, thus logged into binary log having done work on the old version of the table, or will be queued for execution on its new version. The "COMPLETE" COMMIT or ROLLBACK ALTER are written after the most of ALTER work is done. When its result is positive COMMIT ALTER is written, otherwise when there were errors since START ALTER, ROLLBACK ALTER is written. Replication of two-phase binloggable ALTER is cross-version safe. Specifically the OLD slave merely does not recognized the start alter part, still memorizing its gtid. Two phase logged ALTER is read from binlog by mysqlbinlog to produce BINLOG 'string', where 'string' contains base64 encoded Query_log_event containing either the start part of ALTER, or a completion part. The Query details can be displayed with `-v` flag, similarly to ROW format events. Notice, mysqlbinlog output containing parts of two-phase binloggable ALTER is processable correctly only by binlog_alter_two_phase server. Thanks to all people involved into early discussion of the feature including Kristian Nielsen, those who helped to design, implement and test: Sergei Golubchik, Andrei Elkin, Sujatha Sivakumar, Brandon Nesterenko, Alice Sherepa.
-rw-r--r--client/mysqlbinlog.cc1
-rw-r--r--mysql-test/include/show_events.inc2
-rw-r--r--mysql-test/main/mysqld--help.result4
-rw-r--r--mysql-test/suite/binlog/r/binlog_mysqlbinlog_start_alter_verbose.result15
-rw-r--r--mysql-test/suite/binlog/r/start_alter_mysqlbinlog_replay.result42
-rw-r--r--mysql-test/suite/binlog/t/binlog_mysqlbinlog_start_alter_verbose.test58
-rw-r--r--mysql-test/suite/binlog/t/start_alter_mysqlbinlog_replay.test61
-rw-r--r--mysql-test/suite/rpl/include/start_alter_basic.inc55
-rw-r--r--mysql-test/suite/rpl/include/start_alter_concurrent.inc230
-rw-r--r--mysql-test/suite/rpl/include/start_alter_include.inc64
-rw-r--r--mysql-test/suite/rpl/include/start_alter_options.inc237
-rw-r--r--mysql-test/suite/rpl/r/rpl_alter_rollback.result54
-rw-r--r--mysql-test/suite/rpl/r/rpl_start_alter_1.result280
-rw-r--r--mysql-test/suite/rpl/r/rpl_start_alter_2.result293
-rw-r--r--mysql-test/suite/rpl/r/rpl_start_alter_3.result293
-rw-r--r--mysql-test/suite/rpl/r/rpl_start_alter_4.result294
-rw-r--r--mysql-test/suite/rpl/r/rpl_start_alter_5.result294
-rw-r--r--mysql-test/suite/rpl/r/rpl_start_alter_6.result296
-rw-r--r--mysql-test/suite/rpl/r/rpl_start_alter_7.result302
-rw-r--r--mysql-test/suite/rpl/r/rpl_start_alter_8.result296
-rw-r--r--mysql-test/suite/rpl/r/rpl_start_alter_bugs.result33
-rw-r--r--mysql-test/suite/rpl/r/rpl_start_alter_chain_basic.result72
-rw-r--r--mysql-test/suite/rpl/r/rpl_start_alter_mysqlbinlog_1.result139
-rw-r--r--mysql-test/suite/rpl/r/rpl_start_alter_mysqlbinlog_2.result353
-rw-r--r--mysql-test/suite/rpl/r/rpl_start_alter_options.result316
-rw-r--r--mysql-test/suite/rpl/r/rpl_start_alter_restart_master.result85
-rw-r--r--mysql-test/suite/rpl/r/rpl_start_alter_restart_slave.result76
-rw-r--r--mysql-test/suite/rpl/r/rpl_start_alter_unsupported.result38
-rw-r--r--mysql-test/suite/rpl/t/rpl_alter_rollback.test42
-rw-r--r--mysql-test/suite/rpl/t/rpl_start_alter_1.test33
-rw-r--r--mysql-test/suite/rpl/t/rpl_start_alter_2.test53
-rw-r--r--mysql-test/suite/rpl/t/rpl_start_alter_3.test54
-rw-r--r--mysql-test/suite/rpl/t/rpl_start_alter_4.test54
-rw-r--r--mysql-test/suite/rpl/t/rpl_start_alter_5.test54
-rw-r--r--mysql-test/suite/rpl/t/rpl_start_alter_6.test58
-rw-r--r--mysql-test/suite/rpl/t/rpl_start_alter_7.cnf19
-rw-r--r--mysql-test/suite/rpl/t/rpl_start_alter_7.test112
-rw-r--r--mysql-test/suite/rpl/t/rpl_start_alter_8.cnf19
-rw-r--r--mysql-test/suite/rpl/t/rpl_start_alter_8.test109
-rw-r--r--mysql-test/suite/rpl/t/rpl_start_alter_bugs.test47
-rw-r--r--mysql-test/suite/rpl/t/rpl_start_alter_chain_basic.cnf23
-rw-r--r--mysql-test/suite/rpl/t/rpl_start_alter_chain_basic.test54
-rw-r--r--mysql-test/suite/rpl/t/rpl_start_alter_mysqlbinlog_1.test39
-rw-r--r--mysql-test/suite/rpl/t/rpl_start_alter_mysqlbinlog_2.cnf19
-rw-r--r--mysql-test/suite/rpl/t/rpl_start_alter_mysqlbinlog_2.test164
-rw-r--r--mysql-test/suite/rpl/t/rpl_start_alter_options.test33
-rw-r--r--mysql-test/suite/rpl/t/rpl_start_alter_restart_master.test75
-rw-r--r--mysql-test/suite/rpl/t/rpl_start_alter_restart_slave.test86
-rw-r--r--mysql-test/suite/rpl/t/rpl_start_alter_unsupported.test19
-rw-r--r--mysql-test/suite/sys_vars/r/binlog_alter_two_phase.result53
-rw-r--r--mysql-test/suite/sys_vars/r/sysvars_server_notembedded.result10
-rw-r--r--mysql-test/suite/sys_vars/t/binlog_alter_two_phase.test50
-rw-r--r--sql/log.cc98
-rw-r--r--sql/log_event.cc41
-rw-r--r--sql/log_event.h30
-rw-r--r--sql/log_event_client.cc61
-rw-r--r--sql/log_event_server.cc370
-rw-r--r--sql/mysqld.cc4
-rw-r--r--sql/mysqld.h2
-rw-r--r--sql/rpl_gtid.cc34
-rw-r--r--sql/rpl_mi.cc10
-rw-r--r--sql/rpl_mi.h13
-rw-r--r--sql/rpl_parallel.cc179
-rw-r--r--sql/rpl_parallel.h22
-rw-r--r--sql/rpl_rli.cc11
-rw-r--r--sql/rpl_rli.h52
-rw-r--r--sql/share/errmsg-utf8.txt2
-rw-r--r--sql/slave.cc98
-rw-r--r--sql/sql_alter.cc3
-rw-r--r--sql/sql_binlog.cc40
-rw-r--r--sql/sql_class.h32
-rw-r--r--sql/sql_table.cc296
-rw-r--r--sql/sql_table.h5
-rw-r--r--sql/sys_vars.cc8
74 files changed, 6848 insertions, 125 deletions
diff --git a/client/mysqlbinlog.cc b/client/mysqlbinlog.cc
index 3d9b9712ec8..f38ab6ce17a 100644
--- a/client/mysqlbinlog.cc
+++ b/client/mysqlbinlog.cc
@@ -3208,7 +3208,6 @@ int main(int argc, char** argv)
"/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;\n"
"/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;\n"
"/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;\n");
-
fprintf(result_file, "/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;\n");
}
diff --git a/mysql-test/include/show_events.inc b/mysql-test/include/show_events.inc
index 7f35fc6c433..9df12b2304b 100644
--- a/mysql-test/include/show_events.inc
+++ b/mysql-test/include/show_events.inc
@@ -107,6 +107,8 @@ let $script=
s{block_len=[0-9]+}{block_len=#};
s{Server ver:.*DOLLAR}{SERVER_VERSION, BINLOG_VERSION};
s{GTID [0-9]+-[0-9]+-[0-9]+}{GTID #-#-#};
+ s{COMMIT ALTER id=[0-9]+}{COMMIT ALTER id=#};
+ s{ROLLBACK ALTER id=[0-9]+}{ROLLBACK ALTER id=#};
s{\[([0-9]-[0-9]-[0-9]+,?)+\]}{[#-#-#]};
s{cid=[0-9]+}{cid=#};
s{SQL_LOAD-[a-z,0-9,-]*.[a-z]*}{SQL_LOAD-<SERVER UUID>-<MASTER server-id>-<file-id>.<extension>};
diff --git a/mysql-test/main/mysqld--help.result b/mysql-test/main/mysqld--help.result
index a8aa461c863..1bc60dc11a6 100644
--- a/mysql-test/main/mysqld--help.result
+++ b/mysql-test/main/mysqld--help.result
@@ -42,6 +42,9 @@ The following specify which files/extra groups are read (specified before remain
full' errors. No longer needed, as the server now handles
this automatically.
--bind-address=name IP address to bind to.
+ --binlog-alter-two-phase=name
+ If set split the alter into 2 statement START ALTER and
+ COMMIT/ROLLBACKALTER. One of: No, Yes
--binlog-annotate-row-events
Tells the master to annotate RBR events with the
statement that caused these events
@@ -1475,6 +1478,7 @@ automatic-sp-privileges TRUE
back-log 80
big-tables FALSE
bind-address (No default value)
+binlog-alter-two-phase No
binlog-annotate-row-events TRUE
binlog-cache-size 32768
binlog-checksum CRC32
diff --git a/mysql-test/suite/binlog/r/binlog_mysqlbinlog_start_alter_verbose.result b/mysql-test/suite/binlog/r/binlog_mysqlbinlog_start_alter_verbose.result
new file mode 100644
index 00000000000..cabc77ab113
--- /dev/null
+++ b/mysql-test/suite/binlog/r/binlog_mysqlbinlog_start_alter_verbose.result
@@ -0,0 +1,15 @@
+set global binlog_alter_two_phase=YES;
+set binlog_alter_two_phase=YES;
+RESET MASTER;
+create table myt (a int) engine=InnoDB;
+alter table myt add column (b int);
+FLUSH LOGS;
+# Exec MYSQL_BINLOG --base64-output=decode-rows -v MYSQLD_DATADIR/BINLOG_FILENAME > MYSQLTEST_VARDIR/tmp/mysqlbinlog_verbose.sql
+Verbose statements from : master-bin.000001
+select replace(txt,'\r', '') as stmt from raw_binlog_rows where txt like '###%';
+stmt
+### alter table myt add column (b int)
+### alter table myt add column (b int)
+drop table raw_binlog_rows;
+drop table myt;
+set global binlog_alter_two_phase=No;
diff --git a/mysql-test/suite/binlog/r/start_alter_mysqlbinlog_replay.result b/mysql-test/suite/binlog/r/start_alter_mysqlbinlog_replay.result
new file mode 100644
index 00000000000..a4f2ec08b64
--- /dev/null
+++ b/mysql-test/suite/binlog/r/start_alter_mysqlbinlog_replay.result
@@ -0,0 +1,42 @@
+#
+# Test verifies replay of binary logs which contain
+# SA/RA/CA works fine.
+# Generate a binary log with alter events and use mysqlbinlog tool to
+# generate a sql file for replay. Source it on an clean master and
+# verify the correctness. Use the latest binlog and repeat the same
+# process mentioned above and observe replay works fine.
+#
+set global binlog_alter_two_phase=YES;
+set binlog_alter_two_phase=YES;
+create table t1 (f1 int primary key) engine=InnoDB;
+create table t2 (f1 int primary key, constraint c1 foreign key (f1) references t1(f1)) engine=innodb;
+alter table t2 add constraint c1 foreign key (f1) references t1(f1);
+ERROR HY000: Can't create table `test`.`t2` (errno: 121 "Duplicate key on write or update")
+drop table t2, t1;
+select @@gtid_binlog_state;
+@@gtid_binlog_state
+0-1-5
+FLUSH LOGS;
+# reset the binlog
+RESET MASTER;
+# execute the binlog
+SELECT @@gtid_binlog_state;
+@@gtid_binlog_state
+0-1-5
+FLUSH LOGS;
+# Replay 1: One more time to simulate S->S case
+RESET MASTER;
+# execute the binlog
+SELECT @@gtid_binlog_state;
+@@gtid_binlog_state
+0-1-5
+FLUSH LOGS;
+# Replay 2: One more time to simulate S->S case
+RESET MASTER;
+# execute the binlog
+SELECT @@gtid_binlog_state;
+@@gtid_binlog_state
+0-1-5
+# clean up
+RESET MASTER;
+set global binlog_alter_two_phase=No;;
diff --git a/mysql-test/suite/binlog/t/binlog_mysqlbinlog_start_alter_verbose.test b/mysql-test/suite/binlog/t/binlog_mysqlbinlog_start_alter_verbose.test
new file mode 100644
index 00000000000..d532c2ff538
--- /dev/null
+++ b/mysql-test/suite/binlog/t/binlog_mysqlbinlog_start_alter_verbose.test
@@ -0,0 +1,58 @@
+#
+# Purpose:
+#
+# This test ensures that mysqlbinlog prints a comment when two-phase alter
+# is enabled which shows the original alter query issued.
+#
+#
+# References:
+# MENT-662: Finalize MDEV-11675 "Lag Free Alter On Slave"
+#
+
+# Just row format for faster testing
+--source include/have_binlog_format_row.inc
+--source include/have_innodb.inc
+
+#---
+# Setup
+#---
+--let $binlog_alter_two_phase= `select @@binlog_alter_two_phase`
+set global binlog_alter_two_phase=YES;
+set binlog_alter_two_phase=YES;
+
+RESET MASTER;
+create table myt (a int) engine=InnoDB;
+
+
+#---
+# Issue ALTER
+#---
+alter table myt add column (b int);
+
+
+#---
+# Check binlog output
+#---
+FLUSH LOGS;
+
+--disable_query_log
+--let $MYSQLD_DATADIR= `select @@datadir`
+--let $BINLOG_FILENAME= query_get_value(SHOW BINARY LOGS, Log_name, 1)
+
+--echo # Exec MYSQL_BINLOG --base64-output=decode-rows -v MYSQLD_DATADIR/BINLOG_FILENAME > MYSQLTEST_VARDIR/tmp/mysqlbinlog_verbose.sql
+--exec $MYSQL_BINLOG --base64-output=decode-rows -v $MYSQLD_DATADIR/$BINLOG_FILENAME > $MYSQLTEST_VARDIR/tmp/mysqlbinlog_verbose.sql
+
+create table raw_binlog_rows (txt varchar(1000));
+--eval load data local infile '$MYSQLTEST_VARDIR/tmp/mysqlbinlog_verbose.sql' into table raw_binlog_rows columns terminated by '\n'
+--remove_file $MYSQLTEST_VARDIR/tmp/mysqlbinlog_verbose.sql
+--enable_query_log
+--echo Verbose statements from : $BINLOG_FILENAME
+# Output --verbose lines, with extra Windows CR's trimmed
+select replace(txt,'\r', '') as stmt from raw_binlog_rows where txt like '###%';
+
+#---
+# Cleanup
+#---
+drop table raw_binlog_rows;
+drop table myt;
+--eval set global binlog_alter_two_phase=$binlog_alter_two_phase
diff --git a/mysql-test/suite/binlog/t/start_alter_mysqlbinlog_replay.test b/mysql-test/suite/binlog/t/start_alter_mysqlbinlog_replay.test
new file mode 100644
index 00000000000..9c83025fbb2
--- /dev/null
+++ b/mysql-test/suite/binlog/t/start_alter_mysqlbinlog_replay.test
@@ -0,0 +1,61 @@
+#
+# MENT-662: Lag Free Alter On Slave
+#
+
+--echo #
+--echo # Test verifies replay of binary logs which contain
+--echo # SA/RA/CA works fine.
+--echo # Generate a binary log with alter events and use mysqlbinlog tool to
+--echo # generate a sql file for replay. Source it on an clean master and
+--echo # verify the correctness. Use the latest binlog and repeat the same
+--echo # process mentioned above and observe replay works fine.
+--echo #
+--source include/have_log_bin.inc
+--source include/have_innodb.inc
+--source include/have_binlog_format_statement.inc
+
+--let $binlog_alter_two_phase= `select @@binlog_alter_two_phase`
+set global binlog_alter_two_phase=YES;
+set binlog_alter_two_phase=YES;
+
+create table t1 (f1 int primary key) engine=InnoDB;
+create table t2 (f1 int primary key, constraint c1 foreign key (f1) references t1(f1)) engine=innodb;
+
+--error ER_CANT_CREATE_TABLE
+alter table t2 add constraint c1 foreign key (f1) references t1(f1);
+
+drop table t2, t1;
+select @@gtid_binlog_state;
+FLUSH LOGS;
+
+let MYSQLD_DATADIR= `select @@datadir;`;
+--exec $MYSQL_BINLOG $MYSQLD_DATADIR/master-bin.000001 > $MYSQLTEST_VARDIR/tmp/slave_1.sql
+
+--echo # reset the binlog
+RESET MASTER;
+--echo # execute the binlog
+--exec $MYSQL --port=$MASTER_MYPORT --host=127.0.0.1 -e "source $MYSQLTEST_VARDIR/tmp/slave_1.sql"
+SELECT @@gtid_binlog_state;
+FLUSH LOGS;
+--echo # Replay 1: One more time to simulate S->S case
+--exec $MYSQL_BINLOG $MYSQLD_DATADIR/master-bin.000001 > $MYSQLTEST_VARDIR/tmp/slave_2.sql
+
+RESET MASTER;
+--echo # execute the binlog
+--exec $MYSQL --port=$MASTER_MYPORT --host=127.0.0.1 -e "source $MYSQLTEST_VARDIR/tmp/slave_2.sql"
+SELECT @@gtid_binlog_state;
+FLUSH LOGS;
+--echo # Replay 2: One more time to simulate S->S case
+--exec $MYSQL_BINLOG $MYSQLD_DATADIR/master-bin.000001 > $MYSQLTEST_VARDIR/tmp/slave_3.sql
+RESET MASTER;
+--echo # execute the binlog
+--exec $MYSQL --port=$MASTER_MYPORT --host=127.0.0.1 -e "source $MYSQLTEST_VARDIR/tmp/slave_3.sql"
+SELECT @@gtid_binlog_state;
+
+--echo # clean up
+remove_file $MYSQLTEST_VARDIR/tmp/slave_1.sql;
+remove_file $MYSQLTEST_VARDIR/tmp/slave_2.sql;
+remove_file $MYSQLTEST_VARDIR/tmp/slave_3.sql;
+RESET MASTER;
+
+--eval set global binlog_alter_two_phase=$binlog_alter_two_phase;
diff --git a/mysql-test/suite/rpl/include/start_alter_basic.inc b/mysql-test/suite/rpl/include/start_alter_basic.inc
new file mode 100644
index 00000000000..77482ea0992
--- /dev/null
+++ b/mysql-test/suite/rpl/include/start_alter_basic.inc
@@ -0,0 +1,55 @@
+#
+# Run start alter basic tests (CA/RA with given engine)
+# Params:-
+# $engine
+# $sync_slave
+# master_node and slave_node connection should be defined
+
+--echo # $engine
+--connection master_node
+--eval create table t1(a int, b int) engine=$engine;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+--echo # Normal Alter
+alter table t1 add column c int;
+--echo # Failed Alter
+insert into t1 values(1,1, NULL);
+--error ER_DUP_ENTRY
+alter table t1 change a a int unique ;
+show create table t1;
+
+# The following restriction should be removed post MDEV-26005 fix.
+# TODO MDEV-26130 , should be removed after fixing
+#if ($engine != 'aria')
+#{
+#--eval create temporary table tmp_tbl(a int, b int) engine=$engine;
+#insert into tmp_tbl values(1,1);
+#insert into tmp_tbl values(2,2);
+#--echo # Normal Alter
+#alter table tmp_tbl add column c int;
+#--echo # Failed Alter
+#insert into tmp_tbl values(1,1, NULL);
+#--error ER_DUP_ENTRY
+#alter table tmp_tbl change a a int unique ;
+#show create table tmp_tbl;
+#}
+if ($sync_slave)
+{
+ --source include/save_master_gtid.inc
+ --connection slave_node
+ --source include/sync_with_master_gtid.inc
+ show create table t1;
+}
+
+--connection master_node
+drop table t1;
+#if ($engine != 'aria')
+#{
+#drop temporary table tmp_tbl;
+#}
+if ($sync_slave)
+{
+ --source include/save_master_gtid.inc
+ --connection slave_node
+ --source include/sync_with_master_gtid.inc
+}
diff --git a/mysql-test/suite/rpl/include/start_alter_concurrent.inc b/mysql-test/suite/rpl/include/start_alter_concurrent.inc
new file mode 100644
index 00000000000..e5472152807
--- /dev/null
+++ b/mysql-test/suite/rpl/include/start_alter_concurrent.inc
@@ -0,0 +1,230 @@
+#
+# ==== Purpose ====
+#
+# Run concurrent split alter on given storage engine
+# With concurrent 10 connections
+#
+# ==== Usage ====
+#
+# --let $alter_engine= Engine Name (myisam , innodb ...)
+# --let $alter_algorithm= ...
+# --let $alter_online = [][ONLINE]
+# --let $domain_1=
+# --let $domain_2=
+# --let $M_port= //Master port
+# --let $S_port= //Slave port
+# --let $sync_slave= // 0/1 whether to sync slave with master or not
+#
+
+
+--connection master_node
+set global debug_dbug="+d,start_alter_delay_master";
+
+--let $i= 1
+while($i < 11)
+{
+ if($i == 1 && $domain_1)
+ {
+ --eval set gtid_domain_id= $domain_1;
+ }
+ if($i == 6 && $domain_2)
+ {
+ --eval set gtid_domain_id= $domain_2;
+ }
+ --eval create table t$i( a int primary key, b int) engine=$alter_engine
+ --eval insert into t$i values(1,1),(2,2)
+ --inc $i
+}
+
+
+if ($sync_slave)
+{
+ --echo # Sync slave
+ --source include/save_master_gtid.inc
+ --connection slave_node
+ --source include/sync_with_master_gtid.inc
+ --connection master_node
+}
+
+--let $i= 1
+while($i < 21)
+{
+ if($i == 1 || $i == 11)
+ {
+ if($domain_1)
+ {
+ --eval set global gtid_domain_id= $domain_1;
+ }
+ }
+ if($i == 6 || $i == 16)
+ {
+ if($domain_2)
+ {
+ --eval set global gtid_domain_id= $domain_2;
+ }
+ }
+ connect(con$i,127.0.0.1,root,,$db_name, $M_port);
+ --inc $i
+}
+
+--let $i= 1
+while($i < 11)
+{
+ --connection con$i
+ --send_eval alter $alter_online table t$i add column c int, force, algorithm=$alter_algorithm
+ --inc $i
+}
+
+--connection master_node
+set DEBUG_SYNC= "now signal alter_cont";
+
+--let $i= 1
+while($i < 11)
+{
+ --connection con$i
+ --reap
+ --inc $i
+}
+--connection master_node
+set DEBUG_SYNC= 'RESET';
+
+if ($sync_slave)
+{
+ --echo # Sync slave
+ --source include/save_master_gtid.inc
+ --connection slave_node
+ --source include/sync_with_master_gtid.inc
+ --connection master_node
+}
+
+
+--echo # Concurrent DML
+--let $i= 1
+while($i < 11)
+{
+ --connection con$i
+ --send_eval alter table t$i add column d int, force, algorithm=$alter_algorithm
+ --inc $i
+}
+
+if ($alter_algorithm == "inplace")
+{
+ --sleep 1
+ --let $i= 11
+ --let $j= 1
+ while($i < 21)
+ {
+ --connection con$i
+ --send_eval insert into t$j values(5,5,5);
+ --inc $i
+ --inc $j
+ }
+
+ --let $i= 11
+ while($i < 21)
+ {
+ --connection con$i
+ --reap
+ --inc $i
+ }
+
+ if ($sync_slave)
+ {
+ --echo # Sync slave
+ --source include/save_master_gtid.inc
+ --connection slave_node
+ --source include/sync_with_master_gtid.inc
+ --connection master_node
+ }
+}
+--connection master_node
+set DEBUG_SYNC= "now signal alter_cont";
+--let $i= 1
+while($i < 11)
+{
+ --connection con$i
+ --reap
+ --inc $i
+}
+--connection master_node
+set DEBUG_SYNC= 'RESET';
+
+if ($sync_slave)
+{
+ --echo # Sync slave
+ --source include/save_master_gtid.inc
+ --connection slave_node
+ --source include/sync_with_master_gtid.inc
+ --connection master_node
+}
+
+
+--echo # Rollback tests
+--let $i= 1
+while($i < 11)
+{
+ --connection con$i
+ --eval insert into t$i values(3,2,1,1)
+ --send_eval alter table t$i change b b int unique, force, algorithm=$alter_algorithm
+ --inc $i
+}
+--connection master_node
+set DEBUG_SYNC= "now signal alter_cont";
+
+
+--let $i= 1
+while ($i < 11)
+{
+ --connection con$i
+ --error ER_DUP_ENTRY
+ --reap
+ --inc $i
+}
+--connection master_node
+set DEBUG_SYNC= 'RESET';
+
+
+if ($sync_slave)
+{
+ --echo # Sync slave
+ --source include/save_master_gtid.inc
+ --connection slave_node
+ --source include/sync_with_master_gtid.inc
+ --connection master_node
+}
+
+if ($sync_slave)
+{
+ --enable_query_log
+ --echo # diff_table of master and slave , we will do only in the case when
+ --echo # sync_slave is on
+ --let $i= 1
+ while($i < 11)
+ {
+ --let $diff_tables= master_node:t$i, slave_node:t$i
+ source include/diff_tables.inc;
+ --inc $i
+ }
+ --disable_query_log
+}
+
+--connection master_node
+drop table t1,t2,t3,t4,t5,t6,t7,t8,t9,t10;
+
+if ($sync_slave)
+{
+ --echo # Sync slave
+ --source include/save_master_gtid.inc
+ --connection slave_node
+ --source include/sync_with_master_gtid.inc
+ --connection master_node
+}
+
+--let $i= 1
+while ($i < 21)
+{
+ --disconnect con$i
+ --inc $i
+}
+--connection master_node
+SET GLOBAL debug_dbug= "";
diff --git a/mysql-test/suite/rpl/include/start_alter_include.inc b/mysql-test/suite/rpl/include/start_alter_include.inc
new file mode 100644
index 00000000000..c66075c0164
--- /dev/null
+++ b/mysql-test/suite/rpl/include/start_alter_include.inc
@@ -0,0 +1,64 @@
+#
+# ==== Purpose ====
+#
+# Run concurrent split alter on different storage engine and with sync to given
+# master
+#
+# ==== Usage ====
+# --let $domain_1=
+# --let $domain_2=
+# --let $M_port= //Master port
+# --let $S_port= //Slave port
+# --let $sync_slave=
+# --let $db_name=
+#
+
+connect(master_node,127.0.0.1,root,,$db_name, $M_port);
+if (!$sync_slave)
+{
+ --eval set gtid_domain_id= $domain_1;
+}
+connect(slave_node,127.0.0.1,root,,test, $S_port);
+if (!$sync_slave)
+{
+ --eval set gtid_domain_id= $domain_2;
+}
+
+--let $engine=myisam
+--source include/start_alter_basic.inc
+
+--let $engine=innodb
+--source include/start_alter_basic.inc
+
+--let $engine=aria
+--source include/start_alter_basic.inc
+
+--disable_query_log
+--disable_warnings
+--connection master_node
+--echo # concurrent alter Myisam
+--let $alter_engine=myisam
+--let $alter_algorithm=copy
+--source include/start_alter_concurrent.inc
+
+--connection master_node
+--echo # concurrent alter Aria
+--let $alter_engine=aria
+--let $alter_algorithm=copy
+--source include/start_alter_concurrent.inc
+
+--connection master_node
+--echo # concurrent alter Innodb copy
+--let $alter_engine=innodb
+--let $alter_algorithm=copy
+--source include/start_alter_concurrent.inc
+
+--connection master_node
+--echo # concurrent alter Innodb Inplace
+--let $alter_engine=innodb
+--let $alter_algorithm=inplace
+--source include/start_alter_concurrent.inc
+--disconnect master_node
+--disconnect slave_node
+--enable_warnings
+--enable_query_log
diff --git a/mysql-test/suite/rpl/include/start_alter_options.inc b/mysql-test/suite/rpl/include/start_alter_options.inc
new file mode 100644
index 00000000000..2dc662fa43c
--- /dev/null
+++ b/mysql-test/suite/rpl/include/start_alter_options.inc
@@ -0,0 +1,237 @@
+# This test will test all the option related to the Alter Table command
+# NOTE not all alter statements will follow alter_algorithm since for some statements
+# copy is only option
+# parameters
+# $alter_algorithm {DEFAULT|INPLACE|COPY|NOCOPY|INSTANT}
+# $show_binlog
+#
+--let $force_needed= force ,
+
+--connection slave
+stop slave;
+--let $gtid_strict_mode= `select @@gtid_strict_mode`
+--let $slave_parallel_threads= `select @@slave_parallel_threads`
+--let $slave_parallel_mode= `select @@slave_parallel_mode`
+change master to master_use_gtid= current_pos;
+SET GLOBAL slave_parallel_threads=4;
+set global slave_parallel_mode=optimistic;
+set global gtid_strict_mode=1;
+start slave;
+--connection master
+set binlog_alter_two_phase=true;
+create table t1(a int , b int) engine=innodb;
+create table a1(a int , b int) engine=myisam;
+create temporary table tmp_tbl(a int, b int) engine=innodb;
+
+# These are grammer rules for ALTER TABLE we will got through all alter table
+# rules in this test.
+# | ADD [COLUMN] [IF NOT EXISTS] col_name column_definition
+# [FIRST | AFTER col_name ]
+# | ADD [COLUMN] [IF NOT EXISTS] (col_name column_definition,...)
+--eval alter table t1 add column if not exists c int , $force_needed algorithm=$alter_algorithm
+--eval alter table t1 add column d int first, $force_needed algorithm=$alter_algorithm
+--eval alter table t1 add column e int after c, $force_needed algorithm=$alter_algorithm
+--eval alter table t1 add column f int after c, $force_needed add column g int first ,add column h char, algorithm=$alter_algorithm
+--eval alter table t1 drop column c, drop column d, drop column e, drop column f, drop column g , drop column h, $force_needed algorithm=$alter_algorithm
+if ($alter_algorithm == 'copy')
+{
+--eval alter table tmp_tbl add column if not exists c int , $force_needed algorithm=$alter_algorithm
+--eval alter table tmp_tbl add column d int first, $force_needed algorithm=$alter_algorithm
+--eval alter table tmp_tbl add column e int after c, $force_needed algorithm=$alter_algorithm
+--eval alter table tmp_tbl add column f int after c, $force_needed add column g int first ,add column h char, algorithm=$alter_algorithm
+--eval alter table tmp_tbl drop column c, drop column d, drop column e, drop column f, drop column g , drop column h, $force_needed algorithm=$alter_algorithm
+}
+--echo # show binlog and clear status
+--sync_slave_with_master
+if ($show_binlog)
+{
+ --source include/show_binlog_events.inc
+}
+reset master;
+--connection master
+
+
+#
+# | ADD {INDEX|KEY} [IF NOT EXISTS] [index_name]
+# [index_type] (index_col_name,...) [index_option] ...
+# | ADD [CONSTRAINT [symbol]]
+# UNIQUE [INDEX|KEY] [index_name]
+# [index_type] (index_col_name,...) [index_option] ...
+#
+# | ADD FULLTEXT [INDEX|KEY] [index_name]
+# (index_col_name,...) [index_option] ...
+# | DROP [COLUMN] [IF EXISTS] col_name [RESTRICT|CASCADE]
+# | DROP PRIMARY KEY
+# | DROP {INDEX|KEY} [IF EXISTS] index_name
+# | DROP FOREIGN KEY [IF EXISTS] fk_symbol
+# | DROP CONSTRAINT [IF EXISTS] constraint_name
+--eval alter table t1 add column f int after b, $force_needed add column g int first ,add column h varchar(100), algorithm=$alter_algorithm
+--eval alter table t1 add index if not exists index_1(f), $force_needed algorithm=$alter_algorithm
+--eval alter table t1 drop index index_1, $force_needed algorithm=$alter_algorithm
+--eval alter table t1 add unique key unique_1(g), $force_needed algorithm=$alter_algorithm
+--eval alter table t1 drop index unique_1, $force_needed algorithm=$alter_algorithm
+--eval alter table t1 add fulltext key f_1(h), $force_needed algorithm=$alter_algorithm
+--eval alter table t1 drop column f, drop column g , $force_needed algorithm=$alter_algorithm
+if ($alter_algorithm == 'copy')
+{
+--eval alter table tmp_tbl add column f int after b, $force_needed add column g int first ,add column h varchar(100), algorithm=$alter_algorithm
+--eval alter table tmp_tbl add index if not exists index_1(f), $force_needed algorithm=$alter_algorithm
+--eval alter table tmp_tbl drop index index_1, $force_needed algorithm=$alter_algorithm
+--eval alter table tmp_tbl add unique key unique_1(g), $force_needed algorithm=$alter_algorithm
+--eval alter table tmp_tbl drop index unique_1, $force_needed algorithm=$alter_algorithm
+--eval alter table tmp_tbl drop column f, drop column g , $force_needed algorithm=$alter_algorithm
+}
+# | ADD [CONSTRAINT [symbol]] PRIMARY KEY
+# [index_type] (index_col_name,...) [index_option] ...
+# primary key changes cant use inplace algorithm
+--eval alter table t1 add primary key(h), $force_needed algorithm=copy
+--eval alter table t1 drop primary key, $force_needed algorithm=copy
+--eval alter table t1 drop column h, $force_needed algorithm=copy
+if ($alter_algorithm == 'copy')
+{
+--eval alter table tmp_tbl add primary key(h), $force_needed algorithm=copy
+--eval alter table tmp_tbl drop primary key, $force_needed algorithm=copy
+--eval alter table tmp_tbl drop column h, $force_needed algorithm=copy
+}
+--echo # show binlog and clear status
+--sync_slave_with_master
+if ($show_binlog)
+{
+ --source include/show_binlog_events.inc
+}
+reset master;
+--connection master
+
+## NOTE force_needed and algorithm will not be used for system versioning
+# | ADD PERIOD FOR SYSTEM_TIME (start_column_name, end_column_name)
+# | ADD SYSTEM VERSIONING
+# | DROP SYSTEM VERSIONING
+--eval alter table t1 add column f varchar(100) after b, add column g varchar(100) first ,add column h char, $force_needed algorithm=$alter_algorithm
+--eval alter table t1 add period for system_time(f,h)
+--eval alter table t1 add system versioning
+--eval alter table t1 drop system versioning
+--eval alter table t1 drop column f, drop column g , drop column h, $force_needed algorithm=$alter_algorithm
+--echo # show binlog and clear status
+if ($alter_algorithm == 'copy')
+{
+--eval alter table tmp_tbl add column f varchar(100) after b, add column g varchar(100) first ,add column h char, $force_needed algorithm=$alter_algorithm
+--eval alter table tmp_tbl drop column f, drop column g , drop column h, $force_needed algorithm=$alter_algorithm
+}
+--sync_slave_with_master
+if ($show_binlog)
+{
+ --source include/show_binlog_events.inc
+}
+reset master;
+--connection master
+
+
+# | ALTER [COLUMN] col_name SET DEFAULT literal | (expression)
+# | ALTER [COLUMN] col_name DROP DEFAULT
+# | CHANGE [COLUMN] [IF EXISTS] old_col_name new_col_name column_definition
+# [FIRST|AFTER col_name]
+# | MODIFY [COLUMN] [IF EXISTS] col_name column_definition
+# [FIRST | AFTER col_name]
+--eval alter table t1 add column f varchar(100) after b,add column g varchar(100) first ,add column h char, $force_needed algorithm=$alter_algorithm ;
+--eval alter table t1 alter column f set default "****", $force_needed algorithm=$alter_algorithm ;
+--eval alter table t1 alter column f drop default, $force_needed algorithm=$alter_algorithm ;
+--eval alter table t1 change column g new_g char, $force_needed algorithm=copy;
+--eval alter table t1 modify column h varchar(100), $force_needed algorithm=copy;
+--eval alter table t1 drop column new_g ,drop column f, drop column h, $force_needed algorithm=$alter_algorithm ;
+if ($alter_algorithm == 'copy')
+{
+--eval alter table tmp_tbl add column f varchar(100) after b,add column g varchar(100) first ,add column h char, $force_needed algorithm=$alter_algorithm ;
+--eval alter table tmp_tbl alter column f set default "****", $force_needed algorithm=$alter_algorithm ;
+--eval alter table tmp_tbl alter column f drop default, $force_needed algorithm=$alter_algorithm ;
+--eval alter table tmp_tbl change column g new_g char, $force_needed algorithm=copy;
+--eval alter table tmp_tbl modify column h varchar(100), $force_needed algorithm=copy;
+--eval alter table tmp_tbl drop column new_g ,drop column f, drop column h, $force_needed algorithm=$alter_algorithm ;
+}
+--echo # show binlog and clear status
+--sync_slave_with_master
+if ($show_binlog)
+{
+ --source include/show_binlog_events.inc
+}
+reset master;
+--connection master
+
+# | DISABLE KEYS
+# | ENABLE KEYS
+# | RENAME [TO] new_tbl_name
+# | ORDER BY col_name [, col_name] ...
+# | RENAME COLUMN old_col_name TO new_col_name
+# | RENAME {INDEX|KEY} old_index_name TO new_index_name
+# | CONVERT TO CHARACTER SET charset_name [COLLATE collation_name]
+# | [DEFAULT] CHARACTER SET [=] charset_name
+# | [DEFAULT] COLLATE [=] collation_name
+# | DISCARD TABLESPACE
+# | IMPORT TABLESPACE
+--eval alter table t1 add column f varchar(100) after b,add column g varchar(100) first ,add column h char, $force_needed algorithm=$alter_algorithm
+--eval alter table t1 add index if not exists index_1(f), $force_needed algorithm=$alter_algorithm
+--eval alter table t1 disable keys, $force_needed algorithm=copy
+--eval alter table t1 enable keys, $force_needed algorithm=copy
+--eval alter table t1 rename t2, $force_needed algorithm=$alter_algorithm
+--eval alter table t2 rename t1, $force_needed algorithm=$alter_algorithm
+--eval alter table a1 order by a
+--eval alter table t1 rename column f to new_f, $force_needed algorithm=copy
+--eval alter table t1 convert to character set 'utf8', $force_needed algorithm=copy
+--eval alter table t1 default character set 'utf8', $force_needed algorithm=copy
+--eval alter table t1 default collate 'utf8_icelandic_ci', $force_needed algorithm=copy
+--eval alter table t1 drop column new_f ,drop column g, drop column h, $force_needed algorithm=$alter_algorithm
+if ($alter_algorithm == 'copy')
+{
+--eval alter table tmp_tbl add column f varchar(100) after b,add column g varchar(100) first ,add column h char, $force_needed algorithm=$alter_algorithm
+--eval alter table tmp_tbl add index if not exists index_1(f), $force_needed algorithm=$alter_algorithm
+--eval alter table tmp_tbl disable keys, $force_needed algorithm=copy
+--eval alter table tmp_tbl enable keys, $force_needed algorithm=copy
+--eval alter table a1 order by a
+--eval alter table tmp_tbl rename column f to new_f, $force_needed algorithm=copy
+--eval alter table tmp_tbl convert to character set 'utf8', $force_needed algorithm=copy
+--eval alter table tmp_tbl default character set 'utf8', $force_needed algorithm=copy
+--eval alter table tmp_tbl default collate 'utf8_icelandic_ci', $force_needed algorithm=copy
+--eval alter table tmp_tbl drop column new_f ,drop column g, drop column h, $force_needed algorithm=$alter_algorithm
+}
+##--eval alter table t1 discard tablespace;
+######--eval alter table t1 import tablespace;
+
+--echo # show binlog and clear status
+--sync_slave_with_master
+if ($show_binlog)
+{
+ --source include/show_binlog_events.inc
+}
+reset master;
+--connection master
+
+# Only add partition and remove partition is tested
+# | ADD PARTITION (partition_definition)
+# | REMOVE PARTITIONING
+--eval alter table t1 add column f varchar(100) after b,add column g varchar(100) first ,add column h char, $force_needed algorithm=$alter_algorithm
+--eval alter table t1 partition by hash(b) partitions 8
+--eval alter table t1 remove partitioning
+--eval alter table t1 drop column f ,drop column g, drop column h, $force_needed algorithm=$alter_algorithm
+if ($alter_algorithm == 'copy')
+{
+--eval alter table tmp_tbl add column f varchar(100) after b,add column g varchar(100) first ,add column h char, $force_needed algorithm=$alter_algorithm
+--eval alter table tmp_tbl drop column f ,drop column g, drop column h, $force_needed algorithm=$alter_algorithm
+}
+--echo # show binlog and clear status
+--sync_slave_with_master
+if ($show_binlog)
+{
+ --source include/show_binlog_events.inc
+}
+reset master;
+--connection master
+
+# clean master/slave
+--connection master
+drop table t1, a1;
+drop temporary table tmp_tbl;
+--sync_slave_with_master
+--source include/stop_slave.inc
+--eval set global slave_parallel_threads = $slave_parallel_threads;
+--eval set global slave_parallel_mode = $slave_parallel_mode;
+--eval set global gtid_strict_mode = $gtid_strict_mode;
+--source include/start_slave.inc
diff --git a/mysql-test/suite/rpl/r/rpl_alter_rollback.result b/mysql-test/suite/rpl/r/rpl_alter_rollback.result
new file mode 100644
index 00000000000..441d768e584
--- /dev/null
+++ b/mysql-test/suite/rpl/r/rpl_alter_rollback.result
@@ -0,0 +1,54 @@
+#
+# Test verifies that "ROLLBACK ALTER" is written to binary log upon
+#ALTER command execution failure.
+#
+include/master-slave.inc
+[connection master]
+connection master;
+set global binlog_alter_two_phase=YES;
+set binlog_alter_two_phase=YES;
+create table t1 (f1 int primary key) engine=InnoDB;
+create table t2 (f1 int primary key,
+constraint c1 foreign key (f1) references t1(f1),
+constraint c1 foreign key (f1) references t1(f1)) engine=InnoDB;
+ERROR HY000: Can't create table `test`.`t2` (errno: 150 "Foreign key constraint is incorrectly formed")
+create table t2 (f1 int primary key,
+constraint c1 foreign key (f1) references t1(f1)) engine=innodb;
+alter table t2 add constraint c1 foreign key (f1) references t1(f1);
+ERROR HY000: Can't create table `test`.`t2` (errno: 121 "Duplicate key on write or update")
+include/show_binlog_events.inc
+Log_name Pos Event_type Server_id End_log_pos Info
+master-bin.000001 # Gtid # # GTID #-#-#
+master-bin.000001 # Query # # use `test`; create table t1 (f1 int primary key) engine=InnoDB
+master-bin.000001 # Gtid # # GTID #-#-#
+master-bin.000001 # Query # # use `test`; create table t2 (f1 int primary key,
+constraint c1 foreign key (f1) references t1(f1)) engine=innodb
+master-bin.000001 # Gtid # # GTID #-#-# START ALTER
+master-bin.000001 # Query # # use `test`; alter table t2 add constraint c1 foreign key (f1) references t1(f1)
+master-bin.000001 # Gtid # # GTID #-#-# ROLLBACK ALTER id=#
+master-bin.000001 # Query # # use `test`; alter table t2 add constraint c1 foreign key (f1) references t1(f1)
+set foreign_key_checks = 0;
+alter table t2 add constraint c1 foreign key (f1) references t1(f1);
+ERROR HY000: Duplicate FOREIGN KEY constraint name 'test/c1'
+include/show_binlog_events.inc
+Log_name Pos Event_type Server_id End_log_pos Info
+master-bin.000001 # Gtid # # GTID #-#-#
+master-bin.000001 # Query # # use `test`; create table t1 (f1 int primary key) engine=InnoDB
+master-bin.000001 # Gtid # # GTID #-#-#
+master-bin.000001 # Query # # use `test`; create table t2 (f1 int primary key,
+constraint c1 foreign key (f1) references t1(f1)) engine=innodb
+master-bin.000001 # Gtid # # GTID #-#-# START ALTER
+master-bin.000001 # Query # # use `test`; alter table t2 add constraint c1 foreign key (f1) references t1(f1)
+master-bin.000001 # Gtid # # GTID #-#-# ROLLBACK ALTER id=#
+master-bin.000001 # Query # # use `test`; alter table t2 add constraint c1 foreign key (f1) references t1(f1)
+master-bin.000001 # Gtid # # GTID #-#-# START ALTER
+master-bin.000001 # Query # # use `test`; set foreign_key_checks=1; alter table t2 add constraint c1 foreign key (f1) references t1(f1)
+master-bin.000001 # Gtid # # GTID #-#-# ROLLBACK ALTER id=#
+master-bin.000001 # Query # # use `test`; set foreign_key_checks=1; alter table t2 add constraint c1 foreign key (f1) references t1(f1)
+connection slave;
+connection master;
+drop table t2, t1;
+connection slave;
+connection master;
+set global binlog_alter_two_phase=No;;
+include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/r/rpl_start_alter_1.result b/mysql-test/suite/rpl/r/rpl_start_alter_1.result
new file mode 100644
index 00000000000..d99a258ebad
--- /dev/null
+++ b/mysql-test/suite/rpl/r/rpl_start_alter_1.result
@@ -0,0 +1,280 @@
+include/master-slave.inc
+[connection master]
+connection master;
+set global binlog_alter_two_phase=YES;
+set binlog_alter_two_phase=YES;
+connection slave;
+set global gtid_strict_mode=1;
+# Legacy Master Slave
+connect master_node,127.0.0.1,root,,$db_name, $M_port;
+connect slave_node,127.0.0.1,root,,test, $S_port;
+# myisam
+connection master_node;
+create table t1(a int, b int) engine=myisam;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=MyISAM DEFAULT CHARSET=latin1
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=MyISAM DEFAULT CHARSET=latin1
+connection master_node;
+drop table t1;
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+# innodb
+connection master_node;
+create table t1(a int, b int) engine=innodb;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=latin1
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=latin1
+connection master_node;
+drop table t1;
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+# aria
+connection master_node;
+create table t1(a int, b int) engine=aria;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=Aria DEFAULT CHARSET=latin1 PAGE_CHECKSUM=1
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=Aria DEFAULT CHARSET=latin1 PAGE_CHECKSUM=1
+connection master_node;
+drop table t1;
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+# concurrent alter Myisam
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Concurrent DML
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# diff_table of master and slave , we will do only in the case when
+# sync_slave is on
+include/diff_tables.inc [master_node:t1, slave_node:t1]
+include/diff_tables.inc [master_node:t2, slave_node:t2]
+include/diff_tables.inc [master_node:t3, slave_node:t3]
+include/diff_tables.inc [master_node:t4, slave_node:t4]
+include/diff_tables.inc [master_node:t5, slave_node:t5]
+include/diff_tables.inc [master_node:t6, slave_node:t6]
+include/diff_tables.inc [master_node:t7, slave_node:t7]
+include/diff_tables.inc [master_node:t8, slave_node:t8]
+include/diff_tables.inc [master_node:t9, slave_node:t9]
+include/diff_tables.inc [master_node:t10, slave_node:t10]
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# concurrent alter Aria
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Concurrent DML
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# diff_table of master and slave , we will do only in the case when
+# sync_slave is on
+include/diff_tables.inc [master_node:t1, slave_node:t1]
+include/diff_tables.inc [master_node:t2, slave_node:t2]
+include/diff_tables.inc [master_node:t3, slave_node:t3]
+include/diff_tables.inc [master_node:t4, slave_node:t4]
+include/diff_tables.inc [master_node:t5, slave_node:t5]
+include/diff_tables.inc [master_node:t6, slave_node:t6]
+include/diff_tables.inc [master_node:t7, slave_node:t7]
+include/diff_tables.inc [master_node:t8, slave_node:t8]
+include/diff_tables.inc [master_node:t9, slave_node:t9]
+include/diff_tables.inc [master_node:t10, slave_node:t10]
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# concurrent alter Innodb copy
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Concurrent DML
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# diff_table of master and slave , we will do only in the case when
+# sync_slave is on
+include/diff_tables.inc [master_node:t1, slave_node:t1]
+include/diff_tables.inc [master_node:t2, slave_node:t2]
+include/diff_tables.inc [master_node:t3, slave_node:t3]
+include/diff_tables.inc [master_node:t4, slave_node:t4]
+include/diff_tables.inc [master_node:t5, slave_node:t5]
+include/diff_tables.inc [master_node:t6, slave_node:t6]
+include/diff_tables.inc [master_node:t7, slave_node:t7]
+include/diff_tables.inc [master_node:t8, slave_node:t8]
+include/diff_tables.inc [master_node:t9, slave_node:t9]
+include/diff_tables.inc [master_node:t10, slave_node:t10]
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# concurrent alter Innodb Inplace
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Concurrent DML
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# diff_table of master and slave , we will do only in the case when
+# sync_slave is on
+include/diff_tables.inc [master_node:t1, slave_node:t1]
+include/diff_tables.inc [master_node:t2, slave_node:t2]
+include/diff_tables.inc [master_node:t3, slave_node:t3]
+include/diff_tables.inc [master_node:t4, slave_node:t4]
+include/diff_tables.inc [master_node:t5, slave_node:t5]
+include/diff_tables.inc [master_node:t6, slave_node:t6]
+include/diff_tables.inc [master_node:t7, slave_node:t7]
+include/diff_tables.inc [master_node:t8, slave_node:t8]
+include/diff_tables.inc [master_node:t9, slave_node:t9]
+include/diff_tables.inc [master_node:t10, slave_node:t10]
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+connection master;
+include/save_master_gtid.inc
+connection slave;
+include/sync_with_master_gtid.inc
+set global gtid_strict_mode = 0;;
+connection master;
+set global binlog_alter_two_phase=No;;
+include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/r/rpl_start_alter_2.result b/mysql-test/suite/rpl/r/rpl_start_alter_2.result
new file mode 100644
index 00000000000..9171a12fd5e
--- /dev/null
+++ b/mysql-test/suite/rpl/r/rpl_start_alter_2.result
@@ -0,0 +1,293 @@
+include/master-slave.inc
+[connection master]
+connection master;
+set global binlog_alter_two_phase=Yes;
+set binlog_alter_two_phase=Yes;
+connection slave;
+set global gtid_strict_mode=1;
+connection slave;
+include/stop_slave.inc
+SET GLOBAL slave_parallel_threads=10;
+set global slave_parallel_mode=optimistic;
+change master to master_use_gtid=slave_pos;
+include/start_slave.inc
+# Parallel Slave
+connection master;
+connect master_node,127.0.0.1,root,,$db_name, $M_port;
+connect slave_node,127.0.0.1,root,,test, $S_port;
+# myisam
+connection master_node;
+create table t1(a int, b int) engine=myisam;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=MyISAM DEFAULT CHARSET=latin1
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=MyISAM DEFAULT CHARSET=latin1
+connection master_node;
+drop table t1;
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+# innodb
+connection master_node;
+create table t1(a int, b int) engine=innodb;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=latin1
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=latin1
+connection master_node;
+drop table t1;
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+# aria
+connection master_node;
+create table t1(a int, b int) engine=aria;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=Aria DEFAULT CHARSET=latin1 PAGE_CHECKSUM=1
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=Aria DEFAULT CHARSET=latin1 PAGE_CHECKSUM=1
+connection master_node;
+drop table t1;
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+# concurrent alter Myisam
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Concurrent DML
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# diff_table of master and slave , we will do only in the case when
+# sync_slave is on
+include/diff_tables.inc [master_node:t1, slave_node:t1]
+include/diff_tables.inc [master_node:t2, slave_node:t2]
+include/diff_tables.inc [master_node:t3, slave_node:t3]
+include/diff_tables.inc [master_node:t4, slave_node:t4]
+include/diff_tables.inc [master_node:t5, slave_node:t5]
+include/diff_tables.inc [master_node:t6, slave_node:t6]
+include/diff_tables.inc [master_node:t7, slave_node:t7]
+include/diff_tables.inc [master_node:t8, slave_node:t8]
+include/diff_tables.inc [master_node:t9, slave_node:t9]
+include/diff_tables.inc [master_node:t10, slave_node:t10]
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# concurrent alter Aria
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Concurrent DML
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# diff_table of master and slave , we will do only in the case when
+# sync_slave is on
+include/diff_tables.inc [master_node:t1, slave_node:t1]
+include/diff_tables.inc [master_node:t2, slave_node:t2]
+include/diff_tables.inc [master_node:t3, slave_node:t3]
+include/diff_tables.inc [master_node:t4, slave_node:t4]
+include/diff_tables.inc [master_node:t5, slave_node:t5]
+include/diff_tables.inc [master_node:t6, slave_node:t6]
+include/diff_tables.inc [master_node:t7, slave_node:t7]
+include/diff_tables.inc [master_node:t8, slave_node:t8]
+include/diff_tables.inc [master_node:t9, slave_node:t9]
+include/diff_tables.inc [master_node:t10, slave_node:t10]
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# concurrent alter Innodb copy
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Concurrent DML
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# diff_table of master and slave , we will do only in the case when
+# sync_slave is on
+include/diff_tables.inc [master_node:t1, slave_node:t1]
+include/diff_tables.inc [master_node:t2, slave_node:t2]
+include/diff_tables.inc [master_node:t3, slave_node:t3]
+include/diff_tables.inc [master_node:t4, slave_node:t4]
+include/diff_tables.inc [master_node:t5, slave_node:t5]
+include/diff_tables.inc [master_node:t6, slave_node:t6]
+include/diff_tables.inc [master_node:t7, slave_node:t7]
+include/diff_tables.inc [master_node:t8, slave_node:t8]
+include/diff_tables.inc [master_node:t9, slave_node:t9]
+include/diff_tables.inc [master_node:t10, slave_node:t10]
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# concurrent alter Innodb Inplace
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Concurrent DML
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# diff_table of master and slave , we will do only in the case when
+# sync_slave is on
+include/diff_tables.inc [master_node:t1, slave_node:t1]
+include/diff_tables.inc [master_node:t2, slave_node:t2]
+include/diff_tables.inc [master_node:t3, slave_node:t3]
+include/diff_tables.inc [master_node:t4, slave_node:t4]
+include/diff_tables.inc [master_node:t5, slave_node:t5]
+include/diff_tables.inc [master_node:t6, slave_node:t6]
+include/diff_tables.inc [master_node:t7, slave_node:t7]
+include/diff_tables.inc [master_node:t8, slave_node:t8]
+include/diff_tables.inc [master_node:t9, slave_node:t9]
+include/diff_tables.inc [master_node:t10, slave_node:t10]
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+connection master;
+include/save_master_gtid.inc
+connection slave;
+include/sync_with_master_gtid.inc
+# cleanup
+include/stop_slave.inc
+set global slave_parallel_threads = 0;;
+set global slave_parallel_mode = optimistic;;
+set global gtid_strict_mode = 0;;
+set global gtid_domain_id= 0;
+include/start_slave.inc
+connection master;
+set global binlog_alter_two_phase=No;;
+include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/r/rpl_start_alter_3.result b/mysql-test/suite/rpl/r/rpl_start_alter_3.result
new file mode 100644
index 00000000000..845c860de53
--- /dev/null
+++ b/mysql-test/suite/rpl/r/rpl_start_alter_3.result
@@ -0,0 +1,293 @@
+include/master-slave.inc
+[connection master]
+connection master;
+set global binlog_alter_two_phase=Yes;
+set binlog_alter_two_phase=Yes;
+connection slave;
+set global gtid_strict_mode=1;
+connection slave;
+include/stop_slave.inc
+SET GLOBAL slave_parallel_threads=4;
+set global slave_parallel_mode=optimistic;
+change master to master_use_gtid=slave_pos;
+include/start_slave.inc
+# Parallel Slave
+connection master;
+connect master_node,127.0.0.1,root,,$db_name, $M_port;
+connect slave_node,127.0.0.1,root,,test, $S_port;
+# myisam
+connection master_node;
+create table t1(a int, b int) engine=myisam;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=MyISAM DEFAULT CHARSET=latin1
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=MyISAM DEFAULT CHARSET=latin1
+connection master_node;
+drop table t1;
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+# innodb
+connection master_node;
+create table t1(a int, b int) engine=innodb;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=latin1
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=latin1
+connection master_node;
+drop table t1;
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+# aria
+connection master_node;
+create table t1(a int, b int) engine=aria;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=Aria DEFAULT CHARSET=latin1 PAGE_CHECKSUM=1
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=Aria DEFAULT CHARSET=latin1 PAGE_CHECKSUM=1
+connection master_node;
+drop table t1;
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+# concurrent alter Myisam
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Concurrent DML
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# diff_table of master and slave , we will do only in the case when
+# sync_slave is on
+include/diff_tables.inc [master_node:t1, slave_node:t1]
+include/diff_tables.inc [master_node:t2, slave_node:t2]
+include/diff_tables.inc [master_node:t3, slave_node:t3]
+include/diff_tables.inc [master_node:t4, slave_node:t4]
+include/diff_tables.inc [master_node:t5, slave_node:t5]
+include/diff_tables.inc [master_node:t6, slave_node:t6]
+include/diff_tables.inc [master_node:t7, slave_node:t7]
+include/diff_tables.inc [master_node:t8, slave_node:t8]
+include/diff_tables.inc [master_node:t9, slave_node:t9]
+include/diff_tables.inc [master_node:t10, slave_node:t10]
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# concurrent alter Aria
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Concurrent DML
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# diff_table of master and slave , we will do only in the case when
+# sync_slave is on
+include/diff_tables.inc [master_node:t1, slave_node:t1]
+include/diff_tables.inc [master_node:t2, slave_node:t2]
+include/diff_tables.inc [master_node:t3, slave_node:t3]
+include/diff_tables.inc [master_node:t4, slave_node:t4]
+include/diff_tables.inc [master_node:t5, slave_node:t5]
+include/diff_tables.inc [master_node:t6, slave_node:t6]
+include/diff_tables.inc [master_node:t7, slave_node:t7]
+include/diff_tables.inc [master_node:t8, slave_node:t8]
+include/diff_tables.inc [master_node:t9, slave_node:t9]
+include/diff_tables.inc [master_node:t10, slave_node:t10]
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# concurrent alter Innodb copy
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Concurrent DML
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# diff_table of master and slave , we will do only in the case when
+# sync_slave is on
+include/diff_tables.inc [master_node:t1, slave_node:t1]
+include/diff_tables.inc [master_node:t2, slave_node:t2]
+include/diff_tables.inc [master_node:t3, slave_node:t3]
+include/diff_tables.inc [master_node:t4, slave_node:t4]
+include/diff_tables.inc [master_node:t5, slave_node:t5]
+include/diff_tables.inc [master_node:t6, slave_node:t6]
+include/diff_tables.inc [master_node:t7, slave_node:t7]
+include/diff_tables.inc [master_node:t8, slave_node:t8]
+include/diff_tables.inc [master_node:t9, slave_node:t9]
+include/diff_tables.inc [master_node:t10, slave_node:t10]
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# concurrent alter Innodb Inplace
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Concurrent DML
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# diff_table of master and slave , we will do only in the case when
+# sync_slave is on
+include/diff_tables.inc [master_node:t1, slave_node:t1]
+include/diff_tables.inc [master_node:t2, slave_node:t2]
+include/diff_tables.inc [master_node:t3, slave_node:t3]
+include/diff_tables.inc [master_node:t4, slave_node:t4]
+include/diff_tables.inc [master_node:t5, slave_node:t5]
+include/diff_tables.inc [master_node:t6, slave_node:t6]
+include/diff_tables.inc [master_node:t7, slave_node:t7]
+include/diff_tables.inc [master_node:t8, slave_node:t8]
+include/diff_tables.inc [master_node:t9, slave_node:t9]
+include/diff_tables.inc [master_node:t10, slave_node:t10]
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+connection master;
+include/save_master_gtid.inc
+connection slave;
+include/sync_with_master_gtid.inc
+# cleanup
+include/stop_slave.inc
+set global slave_parallel_threads = 0;;
+set global slave_parallel_mode = optimistic;;
+set global gtid_strict_mode = 0;;
+set global gtid_domain_id= 0;
+include/start_slave.inc
+connection master;
+set global binlog_alter_two_phase=No;;
+include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/r/rpl_start_alter_4.result b/mysql-test/suite/rpl/r/rpl_start_alter_4.result
new file mode 100644
index 00000000000..87f00b1694c
--- /dev/null
+++ b/mysql-test/suite/rpl/r/rpl_start_alter_4.result
@@ -0,0 +1,294 @@
+include/master-slave.inc
+[connection master]
+connection master;
+set global binlog_alter_two_phase=Yes;
+set binlog_alter_two_phase=Yes;
+connection slave;
+set global gtid_strict_mode=1;
+connection slave;
+include/stop_slave.inc
+SET GLOBAL slave_parallel_threads=10;
+set global slave_parallel_mode=optimistic;
+change master to master_use_gtid=slave_pos;
+include/start_slave.inc
+# Parallel Slave
+connection master;
+connect master_node,127.0.0.1,root,,$db_name, $M_port;
+connect slave_node,127.0.0.1,root,,test, $S_port;
+# myisam
+connection master_node;
+create table t1(a int, b int) engine=myisam;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=MyISAM DEFAULT CHARSET=latin1
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=MyISAM DEFAULT CHARSET=latin1
+connection master_node;
+drop table t1;
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+# innodb
+connection master_node;
+create table t1(a int, b int) engine=innodb;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=latin1
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=latin1
+connection master_node;
+drop table t1;
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+# aria
+connection master_node;
+create table t1(a int, b int) engine=aria;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=Aria DEFAULT CHARSET=latin1 PAGE_CHECKSUM=1
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=Aria DEFAULT CHARSET=latin1 PAGE_CHECKSUM=1
+connection master_node;
+drop table t1;
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+# concurrent alter Myisam
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Concurrent DML
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# diff_table of master and slave , we will do only in the case when
+# sync_slave is on
+include/diff_tables.inc [master_node:t1, slave_node:t1]
+include/diff_tables.inc [master_node:t2, slave_node:t2]
+include/diff_tables.inc [master_node:t3, slave_node:t3]
+include/diff_tables.inc [master_node:t4, slave_node:t4]
+include/diff_tables.inc [master_node:t5, slave_node:t5]
+include/diff_tables.inc [master_node:t6, slave_node:t6]
+include/diff_tables.inc [master_node:t7, slave_node:t7]
+include/diff_tables.inc [master_node:t8, slave_node:t8]
+include/diff_tables.inc [master_node:t9, slave_node:t9]
+include/diff_tables.inc [master_node:t10, slave_node:t10]
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# concurrent alter Aria
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Concurrent DML
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# diff_table of master and slave , we will do only in the case when
+# sync_slave is on
+include/diff_tables.inc [master_node:t1, slave_node:t1]
+include/diff_tables.inc [master_node:t2, slave_node:t2]
+include/diff_tables.inc [master_node:t3, slave_node:t3]
+include/diff_tables.inc [master_node:t4, slave_node:t4]
+include/diff_tables.inc [master_node:t5, slave_node:t5]
+include/diff_tables.inc [master_node:t6, slave_node:t6]
+include/diff_tables.inc [master_node:t7, slave_node:t7]
+include/diff_tables.inc [master_node:t8, slave_node:t8]
+include/diff_tables.inc [master_node:t9, slave_node:t9]
+include/diff_tables.inc [master_node:t10, slave_node:t10]
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# concurrent alter Innodb copy
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Concurrent DML
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# diff_table of master and slave , we will do only in the case when
+# sync_slave is on
+include/diff_tables.inc [master_node:t1, slave_node:t1]
+include/diff_tables.inc [master_node:t2, slave_node:t2]
+include/diff_tables.inc [master_node:t3, slave_node:t3]
+include/diff_tables.inc [master_node:t4, slave_node:t4]
+include/diff_tables.inc [master_node:t5, slave_node:t5]
+include/diff_tables.inc [master_node:t6, slave_node:t6]
+include/diff_tables.inc [master_node:t7, slave_node:t7]
+include/diff_tables.inc [master_node:t8, slave_node:t8]
+include/diff_tables.inc [master_node:t9, slave_node:t9]
+include/diff_tables.inc [master_node:t10, slave_node:t10]
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# concurrent alter Innodb Inplace
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Concurrent DML
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# diff_table of master and slave , we will do only in the case when
+# sync_slave is on
+include/diff_tables.inc [master_node:t1, slave_node:t1]
+include/diff_tables.inc [master_node:t2, slave_node:t2]
+include/diff_tables.inc [master_node:t3, slave_node:t3]
+include/diff_tables.inc [master_node:t4, slave_node:t4]
+include/diff_tables.inc [master_node:t5, slave_node:t5]
+include/diff_tables.inc [master_node:t6, slave_node:t6]
+include/diff_tables.inc [master_node:t7, slave_node:t7]
+include/diff_tables.inc [master_node:t8, slave_node:t8]
+include/diff_tables.inc [master_node:t9, slave_node:t9]
+include/diff_tables.inc [master_node:t10, slave_node:t10]
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+connection master;
+include/save_master_gtid.inc
+connection slave;
+include/sync_with_master_gtid.inc
+# cleanup
+include/stop_slave.inc
+set global slave_parallel_threads = 0;;
+set global slave_parallel_mode = optimistic;;
+set global gtid_strict_mode = 0;;
+set global gtid_domain_id= 0;
+include/start_slave.inc
+connection master;
+set global binlog_alter_two_phase=No;;
+set global gtid_domain_id= 0;
+include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/r/rpl_start_alter_5.result b/mysql-test/suite/rpl/r/rpl_start_alter_5.result
new file mode 100644
index 00000000000..d50c2854670
--- /dev/null
+++ b/mysql-test/suite/rpl/r/rpl_start_alter_5.result
@@ -0,0 +1,294 @@
+include/master-slave.inc
+[connection master]
+connection master;
+set global binlog_alter_two_phase=Yes;
+set binlog_alter_two_phase=Yes;
+connection slave;
+set global gtid_strict_mode=1;
+connection slave;
+include/stop_slave.inc
+SET GLOBAL slave_parallel_threads=4;
+set global slave_parallel_mode=optimistic;
+change master to master_use_gtid=slave_pos;
+include/start_slave.inc
+# Parallel Slave
+connection master;
+connect master_node,127.0.0.1,root,,$db_name, $M_port;
+connect slave_node,127.0.0.1,root,,test, $S_port;
+# myisam
+connection master_node;
+create table t1(a int, b int) engine=myisam;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=MyISAM DEFAULT CHARSET=latin1
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=MyISAM DEFAULT CHARSET=latin1
+connection master_node;
+drop table t1;
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+# innodb
+connection master_node;
+create table t1(a int, b int) engine=innodb;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=latin1
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=latin1
+connection master_node;
+drop table t1;
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+# aria
+connection master_node;
+create table t1(a int, b int) engine=aria;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=Aria DEFAULT CHARSET=latin1 PAGE_CHECKSUM=1
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=Aria DEFAULT CHARSET=latin1 PAGE_CHECKSUM=1
+connection master_node;
+drop table t1;
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+# concurrent alter Myisam
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Concurrent DML
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# diff_table of master and slave , we will do only in the case when
+# sync_slave is on
+include/diff_tables.inc [master_node:t1, slave_node:t1]
+include/diff_tables.inc [master_node:t2, slave_node:t2]
+include/diff_tables.inc [master_node:t3, slave_node:t3]
+include/diff_tables.inc [master_node:t4, slave_node:t4]
+include/diff_tables.inc [master_node:t5, slave_node:t5]
+include/diff_tables.inc [master_node:t6, slave_node:t6]
+include/diff_tables.inc [master_node:t7, slave_node:t7]
+include/diff_tables.inc [master_node:t8, slave_node:t8]
+include/diff_tables.inc [master_node:t9, slave_node:t9]
+include/diff_tables.inc [master_node:t10, slave_node:t10]
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# concurrent alter Aria
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Concurrent DML
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# diff_table of master and slave , we will do only in the case when
+# sync_slave is on
+include/diff_tables.inc [master_node:t1, slave_node:t1]
+include/diff_tables.inc [master_node:t2, slave_node:t2]
+include/diff_tables.inc [master_node:t3, slave_node:t3]
+include/diff_tables.inc [master_node:t4, slave_node:t4]
+include/diff_tables.inc [master_node:t5, slave_node:t5]
+include/diff_tables.inc [master_node:t6, slave_node:t6]
+include/diff_tables.inc [master_node:t7, slave_node:t7]
+include/diff_tables.inc [master_node:t8, slave_node:t8]
+include/diff_tables.inc [master_node:t9, slave_node:t9]
+include/diff_tables.inc [master_node:t10, slave_node:t10]
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# concurrent alter Innodb copy
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Concurrent DML
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# diff_table of master and slave , we will do only in the case when
+# sync_slave is on
+include/diff_tables.inc [master_node:t1, slave_node:t1]
+include/diff_tables.inc [master_node:t2, slave_node:t2]
+include/diff_tables.inc [master_node:t3, slave_node:t3]
+include/diff_tables.inc [master_node:t4, slave_node:t4]
+include/diff_tables.inc [master_node:t5, slave_node:t5]
+include/diff_tables.inc [master_node:t6, slave_node:t6]
+include/diff_tables.inc [master_node:t7, slave_node:t7]
+include/diff_tables.inc [master_node:t8, slave_node:t8]
+include/diff_tables.inc [master_node:t9, slave_node:t9]
+include/diff_tables.inc [master_node:t10, slave_node:t10]
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# concurrent alter Innodb Inplace
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Concurrent DML
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# diff_table of master and slave , we will do only in the case when
+# sync_slave is on
+include/diff_tables.inc [master_node:t1, slave_node:t1]
+include/diff_tables.inc [master_node:t2, slave_node:t2]
+include/diff_tables.inc [master_node:t3, slave_node:t3]
+include/diff_tables.inc [master_node:t4, slave_node:t4]
+include/diff_tables.inc [master_node:t5, slave_node:t5]
+include/diff_tables.inc [master_node:t6, slave_node:t6]
+include/diff_tables.inc [master_node:t7, slave_node:t7]
+include/diff_tables.inc [master_node:t8, slave_node:t8]
+include/diff_tables.inc [master_node:t9, slave_node:t9]
+include/diff_tables.inc [master_node:t10, slave_node:t10]
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+connection master;
+include/save_master_gtid.inc
+connection slave;
+include/sync_with_master_gtid.inc
+# cleanup
+include/stop_slave.inc
+set global slave_parallel_threads = 0;;
+set global slave_parallel_mode = optimistic;;
+set global gtid_strict_mode = 0;;
+set global gtid_domain_id= 0;
+include/start_slave.inc
+connection master;
+set global binlog_alter_two_phase=No;;
+set global gtid_domain_id= 0;
+include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/r/rpl_start_alter_6.result b/mysql-test/suite/rpl/r/rpl_start_alter_6.result
new file mode 100644
index 00000000000..8d27f47ad0e
--- /dev/null
+++ b/mysql-test/suite/rpl/r/rpl_start_alter_6.result
@@ -0,0 +1,296 @@
+include/master-slave.inc
+[connection master]
+connection master;
+set global binlog_alter_two_phase=Yes;
+set binlog_alter_two_phase=Yes;
+connection slave;
+set global gtid_strict_mode=1;
+connection slave;
+include/stop_slave.inc
+SET GLOBAL slave_parallel_threads=10;
+set global slave_parallel_mode=optimistic;
+set global slave_domain_parallel_threads=3;
+change master to master_use_gtid=slave_pos;
+include/start_slave.inc
+# Parallel Slave
+connection master;
+connect master_node,127.0.0.1,root,,$db_name, $M_port;
+connect slave_node,127.0.0.1,root,,test, $S_port;
+# myisam
+connection master_node;
+create table t1(a int, b int) engine=myisam;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=MyISAM DEFAULT CHARSET=latin1
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=MyISAM DEFAULT CHARSET=latin1
+connection master_node;
+drop table t1;
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+# innodb
+connection master_node;
+create table t1(a int, b int) engine=innodb;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=latin1
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=latin1
+connection master_node;
+drop table t1;
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+# aria
+connection master_node;
+create table t1(a int, b int) engine=aria;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=Aria DEFAULT CHARSET=latin1 PAGE_CHECKSUM=1
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=Aria DEFAULT CHARSET=latin1 PAGE_CHECKSUM=1
+connection master_node;
+drop table t1;
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+# concurrent alter Myisam
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Concurrent DML
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# diff_table of master and slave , we will do only in the case when
+# sync_slave is on
+include/diff_tables.inc [master_node:t1, slave_node:t1]
+include/diff_tables.inc [master_node:t2, slave_node:t2]
+include/diff_tables.inc [master_node:t3, slave_node:t3]
+include/diff_tables.inc [master_node:t4, slave_node:t4]
+include/diff_tables.inc [master_node:t5, slave_node:t5]
+include/diff_tables.inc [master_node:t6, slave_node:t6]
+include/diff_tables.inc [master_node:t7, slave_node:t7]
+include/diff_tables.inc [master_node:t8, slave_node:t8]
+include/diff_tables.inc [master_node:t9, slave_node:t9]
+include/diff_tables.inc [master_node:t10, slave_node:t10]
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# concurrent alter Aria
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Concurrent DML
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# diff_table of master and slave , we will do only in the case when
+# sync_slave is on
+include/diff_tables.inc [master_node:t1, slave_node:t1]
+include/diff_tables.inc [master_node:t2, slave_node:t2]
+include/diff_tables.inc [master_node:t3, slave_node:t3]
+include/diff_tables.inc [master_node:t4, slave_node:t4]
+include/diff_tables.inc [master_node:t5, slave_node:t5]
+include/diff_tables.inc [master_node:t6, slave_node:t6]
+include/diff_tables.inc [master_node:t7, slave_node:t7]
+include/diff_tables.inc [master_node:t8, slave_node:t8]
+include/diff_tables.inc [master_node:t9, slave_node:t9]
+include/diff_tables.inc [master_node:t10, slave_node:t10]
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# concurrent alter Innodb copy
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Concurrent DML
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# diff_table of master and slave , we will do only in the case when
+# sync_slave is on
+include/diff_tables.inc [master_node:t1, slave_node:t1]
+include/diff_tables.inc [master_node:t2, slave_node:t2]
+include/diff_tables.inc [master_node:t3, slave_node:t3]
+include/diff_tables.inc [master_node:t4, slave_node:t4]
+include/diff_tables.inc [master_node:t5, slave_node:t5]
+include/diff_tables.inc [master_node:t6, slave_node:t6]
+include/diff_tables.inc [master_node:t7, slave_node:t7]
+include/diff_tables.inc [master_node:t8, slave_node:t8]
+include/diff_tables.inc [master_node:t9, slave_node:t9]
+include/diff_tables.inc [master_node:t10, slave_node:t10]
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# concurrent alter Innodb Inplace
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Concurrent DML
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+# diff_table of master and slave , we will do only in the case when
+# sync_slave is on
+include/diff_tables.inc [master_node:t1, slave_node:t1]
+include/diff_tables.inc [master_node:t2, slave_node:t2]
+include/diff_tables.inc [master_node:t3, slave_node:t3]
+include/diff_tables.inc [master_node:t4, slave_node:t4]
+include/diff_tables.inc [master_node:t5, slave_node:t5]
+include/diff_tables.inc [master_node:t6, slave_node:t6]
+include/diff_tables.inc [master_node:t7, slave_node:t7]
+include/diff_tables.inc [master_node:t8, slave_node:t8]
+include/diff_tables.inc [master_node:t9, slave_node:t9]
+include/diff_tables.inc [master_node:t10, slave_node:t10]
+# Sync slave
+include/save_master_gtid.inc
+include/sync_with_master_gtid.inc
+connection master;
+include/save_master_gtid.inc
+connection slave;
+include/sync_with_master_gtid.inc
+# cleanup
+include/stop_slave.inc
+set global slave_parallel_threads = 0;;
+set global slave_parallel_mode = optimistic;;
+set global gtid_strict_mode = 0;;
+set global slave_domain_parallel_threads = 0;;
+set global gtid_domain_id= 0;
+include/start_slave.inc
+connection master;
+set global binlog_alter_two_phase=No;;
+set global gtid_domain_id= 0;
+include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/r/rpl_start_alter_7.result b/mysql-test/suite/rpl/r/rpl_start_alter_7.result
new file mode 100644
index 00000000000..90dc582e665
--- /dev/null
+++ b/mysql-test/suite/rpl/r/rpl_start_alter_7.result
@@ -0,0 +1,302 @@
+connect server_1,127.0.0.1,root,,,$SERVER_MYPORT_1;
+connect server_2,127.0.0.1,root,,,$SERVER_MYPORT_2;
+connect server_3,127.0.0.1,root,,,$SERVER_MYPORT_3;
+connection server_1;
+set global binlog_alter_two_phase=Yes;
+set binlog_alter_two_phase=Yes;
+connection server_2;
+stop slave;
+Warnings:
+Note 1255 Slave already has been stopped
+set global binlog_alter_two_phase=true;
+connection server_3;
+SET GLOBAL slave_parallel_threads=8;
+set global slave_parallel_mode=optimistic;
+set global gtid_strict_mode=1;
+connection server_1;
+set gtid_domain_id= 11;
+create database s1;
+use s1;
+connect master_node,127.0.0.1,root,,$db_name, $M_port;
+set gtid_domain_id= 11;;
+connect slave_node,127.0.0.1,root,,test, $S_port;
+set gtid_domain_id= 11;;
+# myisam
+connection master_node;
+create table t1(a int, b int) engine=myisam;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=MyISAM DEFAULT CHARSET=latin1
+connection master_node;
+drop table t1;
+# innodb
+connection master_node;
+create table t1(a int, b int) engine=innodb;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=latin1
+connection master_node;
+drop table t1;
+# aria
+connection master_node;
+create table t1(a int, b int) engine=aria;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=Aria DEFAULT CHARSET=latin1 PAGE_CHECKSUM=1
+connection master_node;
+drop table t1;
+# concurrent alter Myisam
+# Concurrent DML
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# concurrent alter Aria
+# Concurrent DML
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# concurrent alter Innodb copy
+# Concurrent DML
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# concurrent alter Innodb Inplace
+# Concurrent DML
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+connection server_1;
+drop database s1;
+select @@gtid_binlog_pos;
+@@gtid_binlog_pos
+11-1-403
+connection server_2;
+set gtid_domain_id= 12;
+create database s2;
+use s2;
+connect master_node,127.0.0.1,root,,$db_name, $M_port;
+set gtid_domain_id= 12;;
+connect slave_node,127.0.0.1,root,,test, $S_port;
+set gtid_domain_id= 12;;
+# myisam
+connection master_node;
+create table t1(a int, b int) engine=myisam;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=MyISAM DEFAULT CHARSET=latin1
+connection master_node;
+drop table t1;
+# innodb
+connection master_node;
+create table t1(a int, b int) engine=innodb;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=latin1
+connection master_node;
+drop table t1;
+# aria
+connection master_node;
+create table t1(a int, b int) engine=aria;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=Aria DEFAULT CHARSET=latin1 PAGE_CHECKSUM=1
+connection master_node;
+drop table t1;
+# concurrent alter Myisam
+# Concurrent DML
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# concurrent alter Aria
+# Concurrent DML
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# concurrent alter Innodb copy
+# Concurrent DML
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# concurrent alter Innodb Inplace
+# Concurrent DML
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+connection server_2;
+drop database s2;
+select @@gtid_binlog_pos;
+@@gtid_binlog_pos
+12-2-403
+connection server_3;
+start all slaves;
+Warnings:
+Note 1937 SLAVE 'm2' started
+Note 1937 SLAVE 'm1' started
+set default_master_connection = 'm1';
+include/wait_for_slave_to_start.inc
+set default_master_connection = 'm2';
+include/wait_for_slave_to_start.inc
+set default_master_connection = 'm1';
+include/sync_with_master_gtid.inc
+set default_master_connection = 'm2';
+include/sync_with_master_gtid.inc
+# cleanup
+connection server_3;
+set default_master_connection = 'm1';
+include/stop_slave.inc
+set default_master_connection = 'm2';
+include/stop_slave.inc
+set global slave_parallel_threads = 0;;
+set global slave_parallel_mode = optimistic;;
+set global gtid_strict_mode = 0;;
+set global gtid_domain_id= 0;
+reset master;
+RESET SLAVE ALL;
+SET GLOBAL gtid_slave_pos= '';
+connection server_1;
+set global binlog_alter_two_phase=No;;
+set global gtid_domain_id= 0;
+reset master;
+connection server_2;
+set global gtid_domain_id= 0;
+set global binlog_alter_two_phase=No;;
+reset master;
+disconnect server_1;
+disconnect server_2;
+disconnect server_3;
diff --git a/mysql-test/suite/rpl/r/rpl_start_alter_8.result b/mysql-test/suite/rpl/r/rpl_start_alter_8.result
new file mode 100644
index 00000000000..af2ed3e904d
--- /dev/null
+++ b/mysql-test/suite/rpl/r/rpl_start_alter_8.result
@@ -0,0 +1,296 @@
+connect server_1,127.0.0.1,root,,,$SERVER_MYPORT_1;
+connect server_2,127.0.0.1,root,,,$SERVER_MYPORT_2;
+connect server_3,127.0.0.1,root,,,$SERVER_MYPORT_3;
+connection server_1;
+set global binlog_alter_two_phase=Yes;
+set binlog_alter_two_phase=Yes;
+connection server_2;
+stop slave;
+Warnings:
+Note 1255 Slave already has been stopped
+set global binlog_alter_two_phase=true;
+connection server_3;
+SET GLOBAL slave_parallel_threads=20;
+set global slave_parallel_mode=optimistic;
+set global gtid_strict_mode=1;
+connection server_1;
+set gtid_domain_id= 11;
+create database s1;
+use s1;
+connect master_node,127.0.0.1,root,,$db_name, $M_port;
+set gtid_domain_id= 11;;
+connect slave_node,127.0.0.1,root,,test, $S_port;
+set gtid_domain_id= 11;;
+# myisam
+connection master_node;
+create table t1(a int, b int) engine=myisam;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=MyISAM DEFAULT CHARSET=latin1
+connection master_node;
+drop table t1;
+# innodb
+connection master_node;
+create table t1(a int, b int) engine=innodb;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=latin1
+connection master_node;
+drop table t1;
+# aria
+connection master_node;
+create table t1(a int, b int) engine=aria;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=Aria DEFAULT CHARSET=latin1 PAGE_CHECKSUM=1
+connection master_node;
+drop table t1;
+# concurrent alter Myisam
+# Concurrent DML
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# concurrent alter Aria
+# Concurrent DML
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# concurrent alter Innodb copy
+# Concurrent DML
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# concurrent alter Innodb Inplace
+# Concurrent DML
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+connection server_1;
+drop database s1;
+connection server_2;
+set gtid_domain_id= 12;
+create database s2;
+use s2;
+connect master_node,127.0.0.1,root,,$db_name, $M_port;
+set gtid_domain_id= 12;;
+connect slave_node,127.0.0.1,root,,test, $S_port;
+set gtid_domain_id= 12;;
+# myisam
+connection master_node;
+create table t1(a int, b int) engine=myisam;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=MyISAM DEFAULT CHARSET=latin1
+connection master_node;
+drop table t1;
+# innodb
+connection master_node;
+create table t1(a int, b int) engine=innodb;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=latin1
+connection master_node;
+drop table t1;
+# aria
+connection master_node;
+create table t1(a int, b int) engine=aria;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=Aria DEFAULT CHARSET=latin1 PAGE_CHECKSUM=1
+connection master_node;
+drop table t1;
+# concurrent alter Myisam
+# Concurrent DML
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# concurrent alter Aria
+# Concurrent DML
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# concurrent alter Innodb copy
+# Concurrent DML
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# concurrent alter Innodb Inplace
+# Concurrent DML
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+connection server_2;
+drop database s2;
+connection server_3;
+start all slaves;
+Warnings:
+Note 1937 SLAVE 'm2' started
+Note 1937 SLAVE 'm1' started
+set default_master_connection = 'm1';
+include/wait_for_slave_to_start.inc
+set default_master_connection = 'm2';
+include/wait_for_slave_to_start.inc
+set default_master_connection = 'm1';
+include/sync_with_master_gtid.inc
+set default_master_connection = 'm2';
+include/sync_with_master_gtid.inc
+# cleanup
+connection server_3;
+set default_master_connection = 'm1';
+include/stop_slave.inc
+set default_master_connection = 'm2';
+include/stop_slave.inc
+set global slave_parallel_threads = 0;;
+set global slave_parallel_mode = optimistic;;
+set global gtid_strict_mode = 0;;
+set global gtid_domain_id= 0;
+reset master;
+RESET SLAVE ALL;
+SET GLOBAL gtid_slave_pos= '';
+connection server_1;
+set global binlog_alter_two_phase=No;;
+set global gtid_domain_id= 0;
+reset master;
+connection server_2;
+set global gtid_domain_id= 0;
+set global binlog_alter_two_phase=No;;
+reset master;
+disconnect server_1;
+disconnect server_2;
+disconnect server_3;
diff --git a/mysql-test/suite/rpl/r/rpl_start_alter_bugs.result b/mysql-test/suite/rpl/r/rpl_start_alter_bugs.result
new file mode 100644
index 00000000000..3fb3df27afd
--- /dev/null
+++ b/mysql-test/suite/rpl/r/rpl_start_alter_bugs.result
@@ -0,0 +1,33 @@
+include/master-slave.inc
+[connection master]
+connection master;
+set global binlog_alter_two_phase=true;
+connection slave;
+stop slave;
+SET global slave_parallel_threads=2;
+set global slave_parallel_mode=optimistic;
+start slave;
+connection master;
+CREATE TABLE t1 (i int primary key) ENGINE = InnoDB;
+connection master1;
+ALTER TABLE t1 DROP PRIMARY KEY;
+ALTER TABLE t1 ADD UNIQUE KEY ui (i);
+ALTER TABLE t1 ADD PRIMARY KEY (i);
+connection slave;
+connection master;
+drop table t1;
+CREATE TABLE t1 (a int)engine=innodb;
+ALTER TABLE t1 add column b int, LOCK=EXCLUSIVE;
+drop table t1;
+CREATE TABLE t1 (pk int)engine=innodb;
+ALTER TABLE t1 DROP FOREIGN KEY y, LOCK=EXCLUSIVE;
+ERROR 42000: Can't DROP FOREIGN KEY `y`; check that it exists
+drop table t1;
+connection slave;
+connection master;
+set global binlog_alter_two_phase=false;
+connection slave;
+include/stop_slave.inc
+SET global slave_parallel_threads=0;
+include/start_slave.inc
+include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/r/rpl_start_alter_chain_basic.result b/mysql-test/suite/rpl/r/rpl_start_alter_chain_basic.result
new file mode 100644
index 00000000000..b3b5f459943
--- /dev/null
+++ b/mysql-test/suite/rpl/r/rpl_start_alter_chain_basic.result
@@ -0,0 +1,72 @@
+include/rpl_init.inc [topology=1->2->3->4]
+connection server_3;
+set global gtid_strict_mode=1;
+include/stop_slave.inc
+SET GLOBAL slave_parallel_threads=10;
+set global slave_parallel_mode=optimistic;
+change master to master_use_gtid=slave_pos;
+include/start_slave.inc
+connection server_1;
+set global binlog_alter_two_phase=YES;
+set binlog_alter_two_phase=YES;
+connect master_node,127.0.0.1,root,,$db_name, $SERVER_MYPORT_1;
+connect slave_node,127.0.0.1,root,,test, $SERVER_MYPORT_2;
+# innodb
+connection master_node;
+create table t1(a int, b int) engine=innodb;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=latin1
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=latin1
+connection master_node;
+drop table t1;
+include/save_master_gtid.inc
+connection slave_node;
+include/sync_with_master_gtid.inc
+disconnect master_node;
+disconnect slave_node;
+connection server_1;
+set global binlog_alter_two_phase=No;;
+include/rpl_sync.inc
+connection server_2;
+select domain_id, seq_no from mysql.gtid_slave_pos order by seq_no desc limit 1;
+domain_id seq_no
+0 9
+connection server_3;
+select domain_id, seq_no from mysql.gtid_slave_pos order by seq_no desc limit 1;
+domain_id seq_no
+0 9
+include/stop_slave.inc
+set global slave_parallel_threads = 0;;
+set global slave_parallel_mode = optimistic;;
+set global gtid_strict_mode = 0;;
+include/start_slave.inc
+select @@slave_parallel_threads;
+@@slave_parallel_threads
+0
+connection server_4;
+select domain_id, seq_no from mysql.gtid_slave_pos order by seq_no desc limit 1;
+domain_id seq_no
+0 9
+include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/r/rpl_start_alter_mysqlbinlog_1.result b/mysql-test/suite/rpl/r/rpl_start_alter_mysqlbinlog_1.result
new file mode 100644
index 00000000000..fb5f25d0c26
--- /dev/null
+++ b/mysql-test/suite/rpl/r/rpl_start_alter_mysqlbinlog_1.result
@@ -0,0 +1,139 @@
+include/master-slave.inc
+[connection master]
+connection master;
+set global binlog_alter_two_phase=true;
+connection slave;
+include/stop_slave.inc
+change master to master_use_gtid= current_pos;
+set global gtid_strict_mode=1;
+# Legacy Master Slave
+connect master_node,127.0.0.1,root,,$db_name, $M_port;
+set gtid_domain_id= 0;;
+connect slave_node,127.0.0.1,root,,test, $S_port;
+set gtid_domain_id= 0;;
+# myisam
+connection master_node;
+create table t1(a int, b int) engine=myisam;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=MyISAM DEFAULT CHARSET=latin1
+connection master_node;
+drop table t1;
+# innodb
+connection master_node;
+create table t1(a int, b int) engine=innodb;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=latin1
+connection master_node;
+drop table t1;
+# aria
+connection master_node;
+create table t1(a int, b int) engine=aria;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=Aria DEFAULT CHARSET=latin1 PAGE_CHECKSUM=1
+connection master_node;
+drop table t1;
+# concurrent alter Myisam
+# Concurrent DML
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# concurrent alter Aria
+# Concurrent DML
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# concurrent alter Innodb copy
+# Concurrent DML
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# concurrent alter Innodb Inplace
+# Concurrent DML
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+connection master;
+select @@gtid_binlog_state;
+@@gtid_binlog_state
+0-1-401
+RESET master;
+connection slave;
+select @@gtid_binlog_state;
+@@gtid_binlog_state
+0-1-401
+set global gtid_strict_mode=0;
+include/start_slave.inc
+connection master;
+set global binlog_alter_two_phase=false;
+include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/r/rpl_start_alter_mysqlbinlog_2.result b/mysql-test/suite/rpl/r/rpl_start_alter_mysqlbinlog_2.result
new file mode 100644
index 00000000000..e009462bf26
--- /dev/null
+++ b/mysql-test/suite/rpl/r/rpl_start_alter_mysqlbinlog_2.result
@@ -0,0 +1,353 @@
+connect server_1,127.0.0.1,root,,,$SERVER_MYPORT_1;
+connect server_2,127.0.0.1,root,,,$SERVER_MYPORT_2;
+connect server_3,127.0.0.1,root,,,$SERVER_MYPORT_3;
+connection server_1;
+SET @save_binlog_alter_two_phase= @@GLOBAL.binlog_alter_two_phase;
+SET GLOBAL binlog_alter_two_phase=Yes;
+SET binlog_alter_two_phase=Yes;
+# Create table and perform CA and RA
+CREATE TABLE t1( a INT, b INT) ENGINE=InnoDB;
+INSERT INTO t1 VALUES(1,1);
+INSERT INTO t1 VALUES(2,2);
+# Normal Alter
+ALTER TABLE t1 ADD COLUMN c INT;
+# Failed Alter
+INSERT INTO t1 VALUES(1,1, NULL);
+ALTER TABLE t1 CHANGE a a INT UNIQUE;
+ERROR 23000: Duplicate entry '1' for key 'a'
+SHOW CREATE TABLE t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=latin1
+SELECT @@gtid_binlog_state;
+@@gtid_binlog_state
+0-1-8
+# apply the binlog
+DROP TABLE t1;
+# reset the binlog
+RESET MASTER;
+# execute the binlog
+SELECT @@gtid_binlog_state;
+@@gtid_binlog_state
+0-1-8
+# Same as before
+SHOW CREATE TABLE t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=latin1
+DROP TABLE t1;
+# reset the binlog
+RESET MASTER;
+RESET SLAVE;
+connection server_2;
+SET @save_binlog_alter_two_phase= @@GLOBAL.binlog_alter_two_phase;
+SET GLOBAL binlog_alter_two_phase=Yes;
+connection server_3;
+SET @save_gtid_strict_mode= @@GLOBAL.gtid_strict_mode;
+SET @slave_parallel_threads= @@GLOBAL.slave_parallel_threads;
+SET @slave_parallel_mode= @@GLOBAL.slave_parallel_mode;
+SET GLOBAL slave_parallel_threads=20;
+SET GLOBAL slave_parallel_mode=optimistic;
+SET GLOBAL gtid_strict_mode=1;
+connection server_1;
+SET gtid_domain_id= 11;
+CREATE DATABASE s1;
+USE s1;
+connect master_node,127.0.0.1,root,,$db_name, $M_port;
+set gtid_domain_id= 11;;
+connect slave_node,127.0.0.1,root,,test, $S_port;
+set gtid_domain_id= 11;;
+# myisam
+connection master_node;
+create table t1(a int, b int) engine=myisam;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=MyISAM DEFAULT CHARSET=latin1
+connection master_node;
+drop table t1;
+# innodb
+connection master_node;
+create table t1(a int, b int) engine=innodb;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=latin1
+connection master_node;
+drop table t1;
+# aria
+connection master_node;
+create table t1(a int, b int) engine=aria;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=Aria DEFAULT CHARSET=latin1 PAGE_CHECKSUM=1
+connection master_node;
+drop table t1;
+# concurrent alter Myisam
+# Concurrent DML
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# concurrent alter Aria
+# Concurrent DML
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# concurrent alter Innodb copy
+# Concurrent DML
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# concurrent alter Innodb Inplace
+# Concurrent DML
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+connection server_1;
+DROP DATABASE s1;
+connection server_2;
+SET gtid_domain_id= 12;
+CREATE DATABASE s2;
+USE s2;
+connect master_node,127.0.0.1,root,,$db_name, $M_port;
+set gtid_domain_id= 12;;
+connect slave_node,127.0.0.1,root,,test, $S_port;
+set gtid_domain_id= 12;;
+# myisam
+connection master_node;
+create table t1(a int, b int) engine=myisam;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=MyISAM DEFAULT CHARSET=latin1
+connection master_node;
+drop table t1;
+# innodb
+connection master_node;
+create table t1(a int, b int) engine=innodb;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=latin1
+connection master_node;
+drop table t1;
+# aria
+connection master_node;
+create table t1(a int, b int) engine=aria;;
+insert into t1 values(1,1);
+insert into t1 values(2,2);
+# Normal Alter
+alter table t1 add column c int;
+# Failed Alter
+insert into t1 values(1,1, NULL);
+alter table t1 change a a int unique ;
+ERROR 23000: Duplicate entry '1' for key 'a'
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) DEFAULT NULL,
+ `c` int(11) DEFAULT NULL
+) ENGINE=Aria DEFAULT CHARSET=latin1 PAGE_CHECKSUM=1
+connection master_node;
+drop table t1;
+# concurrent alter Myisam
+# Concurrent DML
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# concurrent alter Aria
+# Concurrent DML
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# concurrent alter Innodb copy
+# Concurrent DML
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+# concurrent alter Innodb Inplace
+# Concurrent DML
+# Rollback tests
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+ERROR 23000: Duplicate entry '2' for key 'b'
+connection server_2;
+DROP DATABASE s2;
+connection server_3;
+START ALL SLAVES;
+Warnings:
+Note 1937 SLAVE 'm2' started
+Note 1937 SLAVE 'm1' started
+SET default_master_connection = 'm1';
+include/wait_for_slave_to_start.inc
+SET default_master_connection = 'm2';
+include/wait_for_slave_to_start.inc
+SET default_master_connection = 'm1';
+include/sync_with_master_gtid.inc
+SET default_master_connection = 'm2';
+include/sync_with_master_gtid.inc
+# Stop slaves and apply binlog
+connection server_3;
+SET default_master_connection = 'm1';
+include/stop_slave.inc
+SET default_master_connection = 'm2';
+include/stop_slave.inc
+SET GLOBAL slave_parallel_threads = @slave_parallel_threads;
+SET GLOBAL slave_parallel_mode = @slave_parallel_mode;
+SET GLOBAL gtid_strict_mode = @save_gtid_strict_mode;
+SET GLOBAL gtid_domain_id= 0;
+SELECT @@gtid_binlog_state;
+@@gtid_binlog_state
+11-1-403,12-2-403
+# reset the binlog
+RESET MASTER;
+# execute the binlog
+SELECT @@gtid_binlog_state;
+@@gtid_binlog_state
+11-1-403,12-2-403
+# One more time to simulate S->S case
+RESET MASTER;
+# execute the binlog
+SELECT @@gtid_binlog_state;
+@@gtid_binlog_state
+11-1-403,12-2-403
+RESET MASTER;
+RESET SLAVE ALL;
+SET GLOBAL gtid_slave_pos= '';
+connection server_1;
+SET GLOBAL binlog_alter_two_phase=@save_binlog_alter_two_phase;
+SET GLOBAL gtid_domain_id= 0;
+RESET MASTER;
+connection server_2;
+SET GLOBAL gtid_domain_id= 0;
+SET GLOBAL binlog_alter_two_phase=@save_binlog_alter_two_phase;
+RESET MASTER;
+disconnect server_1;
+disconnect server_2;
+disconnect server_3;
diff --git a/mysql-test/suite/rpl/r/rpl_start_alter_options.result b/mysql-test/suite/rpl/r/rpl_start_alter_options.result
new file mode 100644
index 00000000000..704c88263ca
--- /dev/null
+++ b/mysql-test/suite/rpl/r/rpl_start_alter_options.result
@@ -0,0 +1,316 @@
+include/master-slave.inc
+[connection master]
+connection slave;
+stop slave;
+change master to master_use_gtid= current_pos;
+SET GLOBAL slave_parallel_threads=4;
+set global slave_parallel_mode=optimistic;
+set global gtid_strict_mode=1;
+start slave;
+connection master;
+set binlog_alter_two_phase=true;
+create table t1(a int , b int) engine=innodb;
+create table a1(a int , b int) engine=myisam;
+create temporary table tmp_tbl(a int, b int) engine=innodb;
+alter table t1 add column if not exists c int , force , algorithm=default;
+alter table t1 add column d int first, force , algorithm=default;
+alter table t1 add column e int after c, force , algorithm=default;
+alter table t1 add column f int after c, force , add column g int first ,add column h char, algorithm=default;
+alter table t1 drop column c, drop column d, drop column e, drop column f, drop column g , drop column h, force , algorithm=default;
+# show binlog and clear status
+connection slave;
+reset master;
+connection master;
+alter table t1 add column f int after b, force , add column g int first ,add column h varchar(100), algorithm=default;
+alter table t1 add index if not exists index_1(f), force , algorithm=default;
+alter table t1 drop index index_1, force , algorithm=default;
+alter table t1 add unique key unique_1(g), force , algorithm=default;
+alter table t1 drop index unique_1, force , algorithm=default;
+alter table t1 add fulltext key f_1(h), force , algorithm=default;
+alter table t1 drop column f, drop column g , force , algorithm=default;
+alter table t1 add primary key(h), force , algorithm=copy;
+alter table t1 drop primary key, force , algorithm=copy;
+alter table t1 drop column h, force , algorithm=copy;
+# show binlog and clear status
+connection slave;
+reset master;
+connection master;
+alter table t1 add column f varchar(100) after b, add column g varchar(100) first ,add column h char, force , algorithm=default;
+alter table t1 add period for system_time(f,h);
+alter table t1 add system versioning;
+alter table t1 drop system versioning;
+alter table t1 drop column f, drop column g , drop column h, force , algorithm=default;
+# show binlog and clear status
+connection slave;
+reset master;
+connection master;
+alter table t1 add column f varchar(100) after b,add column g varchar(100) first ,add column h char, force , algorithm=default ;;
+alter table t1 alter column f set default "****", force , algorithm=default ;;
+alter table t1 alter column f drop default, force , algorithm=default ;;
+alter table t1 change column g new_g char, force , algorithm=copy;;
+alter table t1 modify column h varchar(100), force , algorithm=copy;;
+alter table t1 drop column new_g ,drop column f, drop column h, force , algorithm=default ;;
+# show binlog and clear status
+connection slave;
+reset master;
+connection master;
+alter table t1 add column f varchar(100) after b,add column g varchar(100) first ,add column h char, force , algorithm=default;
+alter table t1 add index if not exists index_1(f), force , algorithm=default;
+alter table t1 disable keys, force , algorithm=copy;
+Warnings:
+Note 1031 Storage engine InnoDB of the table `test`.`t1` doesn't have this option
+alter table t1 enable keys, force , algorithm=copy;
+Warnings:
+Note 1031 Storage engine InnoDB of the table `test`.`t1` doesn't have this option
+alter table t1 rename t2, force , algorithm=default;
+alter table t2 rename t1, force , algorithm=default;
+alter table a1 order by a;
+alter table t1 rename column f to new_f, force , algorithm=copy;
+alter table t1 convert to character set 'utf8', force , algorithm=copy;
+alter table t1 default character set 'utf8', force , algorithm=copy;
+alter table t1 default collate 'utf8_icelandic_ci', force , algorithm=copy;
+alter table t1 drop column new_f ,drop column g, drop column h, force , algorithm=default;
+# show binlog and clear status
+connection slave;
+reset master;
+connection master;
+alter table t1 add column f varchar(100) after b,add column g varchar(100) first ,add column h char, force , algorithm=default;
+alter table t1 partition by hash(b) partitions 8;
+alter table t1 remove partitioning;
+alter table t1 drop column f ,drop column g, drop column h, force , algorithm=default;
+# show binlog and clear status
+connection slave;
+reset master;
+connection master;
+connection master;
+drop table t1, a1;
+drop temporary table tmp_tbl;
+connection slave;
+include/stop_slave.inc
+set global slave_parallel_threads = 0;;
+set global slave_parallel_mode = optimistic;;
+set global gtid_strict_mode = 0;;
+include/start_slave.inc
+connection slave;
+stop slave;
+change master to master_use_gtid= current_pos;
+SET GLOBAL slave_parallel_threads=4;
+set global slave_parallel_mode=optimistic;
+set global gtid_strict_mode=1;
+start slave;
+connection master;
+set binlog_alter_two_phase=true;
+create table t1(a int , b int) engine=innodb;
+create table a1(a int , b int) engine=myisam;
+create temporary table tmp_tbl(a int, b int) engine=innodb;
+alter table t1 add column if not exists c int , force , algorithm=inplace;
+alter table t1 add column d int first, force , algorithm=inplace;
+alter table t1 add column e int after c, force , algorithm=inplace;
+alter table t1 add column f int after c, force , add column g int first ,add column h char, algorithm=inplace;
+alter table t1 drop column c, drop column d, drop column e, drop column f, drop column g , drop column h, force , algorithm=inplace;
+# show binlog and clear status
+connection slave;
+reset master;
+connection master;
+alter table t1 add column f int after b, force , add column g int first ,add column h varchar(100), algorithm=inplace;
+alter table t1 add index if not exists index_1(f), force , algorithm=inplace;
+alter table t1 drop index index_1, force , algorithm=inplace;
+alter table t1 add unique key unique_1(g), force , algorithm=inplace;
+alter table t1 drop index unique_1, force , algorithm=inplace;
+alter table t1 add fulltext key f_1(h), force , algorithm=inplace;
+alter table t1 drop column f, drop column g , force , algorithm=inplace;
+alter table t1 add primary key(h), force , algorithm=copy;
+alter table t1 drop primary key, force , algorithm=copy;
+alter table t1 drop column h, force , algorithm=copy;
+# show binlog and clear status
+connection slave;
+reset master;
+connection master;
+alter table t1 add column f varchar(100) after b, add column g varchar(100) first ,add column h char, force , algorithm=inplace;
+alter table t1 add period for system_time(f,h);
+alter table t1 add system versioning;
+alter table t1 drop system versioning;
+alter table t1 drop column f, drop column g , drop column h, force , algorithm=inplace;
+# show binlog and clear status
+connection slave;
+reset master;
+connection master;
+alter table t1 add column f varchar(100) after b,add column g varchar(100) first ,add column h char, force , algorithm=inplace ;;
+alter table t1 alter column f set default "****", force , algorithm=inplace ;;
+alter table t1 alter column f drop default, force , algorithm=inplace ;;
+alter table t1 change column g new_g char, force , algorithm=copy;;
+alter table t1 modify column h varchar(100), force , algorithm=copy;;
+alter table t1 drop column new_g ,drop column f, drop column h, force , algorithm=inplace ;;
+# show binlog and clear status
+connection slave;
+reset master;
+connection master;
+alter table t1 add column f varchar(100) after b,add column g varchar(100) first ,add column h char, force , algorithm=inplace;
+alter table t1 add index if not exists index_1(f), force , algorithm=inplace;
+alter table t1 disable keys, force , algorithm=copy;
+Warnings:
+Note 1031 Storage engine InnoDB of the table `test`.`t1` doesn't have this option
+alter table t1 enable keys, force , algorithm=copy;
+Warnings:
+Note 1031 Storage engine InnoDB of the table `test`.`t1` doesn't have this option
+alter table t1 rename t2, force , algorithm=inplace;
+alter table t2 rename t1, force , algorithm=inplace;
+alter table a1 order by a;
+alter table t1 rename column f to new_f, force , algorithm=copy;
+alter table t1 convert to character set 'utf8', force , algorithm=copy;
+alter table t1 default character set 'utf8', force , algorithm=copy;
+alter table t1 default collate 'utf8_icelandic_ci', force , algorithm=copy;
+alter table t1 drop column new_f ,drop column g, drop column h, force , algorithm=inplace;
+# show binlog and clear status
+connection slave;
+reset master;
+connection master;
+alter table t1 add column f varchar(100) after b,add column g varchar(100) first ,add column h char, force , algorithm=inplace;
+alter table t1 partition by hash(b) partitions 8;
+alter table t1 remove partitioning;
+alter table t1 drop column f ,drop column g, drop column h, force , algorithm=inplace;
+# show binlog and clear status
+connection slave;
+reset master;
+connection master;
+connection master;
+drop table t1, a1;
+drop temporary table tmp_tbl;
+connection slave;
+include/stop_slave.inc
+set global slave_parallel_threads = 0;;
+set global slave_parallel_mode = optimistic;;
+set global gtid_strict_mode = 0;;
+include/start_slave.inc
+connection slave;
+stop slave;
+change master to master_use_gtid= current_pos;
+SET GLOBAL slave_parallel_threads=4;
+set global slave_parallel_mode=optimistic;
+set global gtid_strict_mode=1;
+start slave;
+connection master;
+set binlog_alter_two_phase=true;
+create table t1(a int , b int) engine=innodb;
+create table a1(a int , b int) engine=myisam;
+create temporary table tmp_tbl(a int, b int) engine=innodb;
+alter table t1 add column if not exists c int , force , algorithm=copy;
+alter table t1 add column d int first, force , algorithm=copy;
+alter table t1 add column e int after c, force , algorithm=copy;
+alter table t1 add column f int after c, force , add column g int first ,add column h char, algorithm=copy;
+alter table t1 drop column c, drop column d, drop column e, drop column f, drop column g , drop column h, force , algorithm=copy;
+alter table tmp_tbl add column if not exists c int , force , algorithm=copy;
+alter table tmp_tbl add column d int first, force , algorithm=copy;
+alter table tmp_tbl add column e int after c, force , algorithm=copy;
+alter table tmp_tbl add column f int after c, force , add column g int first ,add column h char, algorithm=copy;
+alter table tmp_tbl drop column c, drop column d, drop column e, drop column f, drop column g , drop column h, force , algorithm=copy;
+# show binlog and clear status
+connection slave;
+reset master;
+connection master;
+alter table t1 add column f int after b, force , add column g int first ,add column h varchar(100), algorithm=copy;
+alter table t1 add index if not exists index_1(f), force , algorithm=copy;
+alter table t1 drop index index_1, force , algorithm=copy;
+alter table t1 add unique key unique_1(g), force , algorithm=copy;
+alter table t1 drop index unique_1, force , algorithm=copy;
+alter table t1 add fulltext key f_1(h), force , algorithm=copy;
+alter table t1 drop column f, drop column g , force , algorithm=copy;
+alter table tmp_tbl add column f int after b, force , add column g int first ,add column h varchar(100), algorithm=copy;
+alter table tmp_tbl add index if not exists index_1(f), force , algorithm=copy;
+alter table tmp_tbl drop index index_1, force , algorithm=copy;
+alter table tmp_tbl add unique key unique_1(g), force , algorithm=copy;
+alter table tmp_tbl drop index unique_1, force , algorithm=copy;
+alter table tmp_tbl drop column f, drop column g , force , algorithm=copy;
+alter table t1 add primary key(h), force , algorithm=copy;
+alter table t1 drop primary key, force , algorithm=copy;
+alter table t1 drop column h, force , algorithm=copy;
+alter table tmp_tbl add primary key(h), force , algorithm=copy;
+alter table tmp_tbl drop primary key, force , algorithm=copy;
+alter table tmp_tbl drop column h, force , algorithm=copy;
+# show binlog and clear status
+connection slave;
+reset master;
+connection master;
+alter table t1 add column f varchar(100) after b, add column g varchar(100) first ,add column h char, force , algorithm=copy;
+alter table t1 add period for system_time(f,h);
+alter table t1 add system versioning;
+alter table t1 drop system versioning;
+alter table t1 drop column f, drop column g , drop column h, force , algorithm=copy;
+# show binlog and clear status
+alter table tmp_tbl add column f varchar(100) after b, add column g varchar(100) first ,add column h char, force , algorithm=copy;
+alter table tmp_tbl drop column f, drop column g , drop column h, force , algorithm=copy;
+connection slave;
+reset master;
+connection master;
+alter table t1 add column f varchar(100) after b,add column g varchar(100) first ,add column h char, force , algorithm=copy ;;
+alter table t1 alter column f set default "****", force , algorithm=copy ;;
+alter table t1 alter column f drop default, force , algorithm=copy ;;
+alter table t1 change column g new_g char, force , algorithm=copy;;
+alter table t1 modify column h varchar(100), force , algorithm=copy;;
+alter table t1 drop column new_g ,drop column f, drop column h, force , algorithm=copy ;;
+alter table tmp_tbl add column f varchar(100) after b,add column g varchar(100) first ,add column h char, force , algorithm=copy ;;
+alter table tmp_tbl alter column f set default "****", force , algorithm=copy ;;
+alter table tmp_tbl alter column f drop default, force , algorithm=copy ;;
+alter table tmp_tbl change column g new_g char, force , algorithm=copy;;
+alter table tmp_tbl modify column h varchar(100), force , algorithm=copy;;
+alter table tmp_tbl drop column new_g ,drop column f, drop column h, force , algorithm=copy ;;
+# show binlog and clear status
+connection slave;
+reset master;
+connection master;
+alter table t1 add column f varchar(100) after b,add column g varchar(100) first ,add column h char, force , algorithm=copy;
+alter table t1 add index if not exists index_1(f), force , algorithm=copy;
+alter table t1 disable keys, force , algorithm=copy;
+Warnings:
+Note 1031 Storage engine InnoDB of the table `test`.`t1` doesn't have this option
+alter table t1 enable keys, force , algorithm=copy;
+Warnings:
+Note 1031 Storage engine InnoDB of the table `test`.`t1` doesn't have this option
+alter table t1 rename t2, force , algorithm=copy;
+alter table t2 rename t1, force , algorithm=copy;
+alter table a1 order by a;
+alter table t1 rename column f to new_f, force , algorithm=copy;
+alter table t1 convert to character set 'utf8', force , algorithm=copy;
+alter table t1 default character set 'utf8', force , algorithm=copy;
+alter table t1 default collate 'utf8_icelandic_ci', force , algorithm=copy;
+alter table t1 drop column new_f ,drop column g, drop column h, force , algorithm=copy;
+alter table tmp_tbl add column f varchar(100) after b,add column g varchar(100) first ,add column h char, force , algorithm=copy;
+alter table tmp_tbl add index if not exists index_1(f), force , algorithm=copy;
+alter table tmp_tbl disable keys, force , algorithm=copy;
+Warnings:
+Note 1031 Storage engine InnoDB of the table `test`.`tmp_tbl` doesn't have this option
+alter table tmp_tbl enable keys, force , algorithm=copy;
+Warnings:
+Note 1031 Storage engine InnoDB of the table `test`.`tmp_tbl` doesn't have this option
+alter table a1 order by a;
+alter table tmp_tbl rename column f to new_f, force , algorithm=copy;
+alter table tmp_tbl convert to character set 'utf8', force , algorithm=copy;
+alter table tmp_tbl default character set 'utf8', force , algorithm=copy;
+alter table tmp_tbl default collate 'utf8_icelandic_ci', force , algorithm=copy;
+alter table tmp_tbl drop column new_f ,drop column g, drop column h, force , algorithm=copy;
+# show binlog and clear status
+connection slave;
+reset master;
+connection master;
+alter table t1 add column f varchar(100) after b,add column g varchar(100) first ,add column h char, force , algorithm=copy;
+alter table t1 partition by hash(b) partitions 8;
+alter table t1 remove partitioning;
+alter table t1 drop column f ,drop column g, drop column h, force , algorithm=copy;
+alter table tmp_tbl add column f varchar(100) after b,add column g varchar(100) first ,add column h char, force , algorithm=copy;
+alter table tmp_tbl drop column f ,drop column g, drop column h, force , algorithm=copy;
+# show binlog and clear status
+connection slave;
+reset master;
+connection master;
+connection master;
+drop table t1, a1;
+drop temporary table tmp_tbl;
+connection slave;
+include/stop_slave.inc
+set global slave_parallel_threads = 0;;
+set global slave_parallel_mode = optimistic;;
+set global gtid_strict_mode = 0;;
+include/start_slave.inc
+# TODO remove support for nocopy and instant
+# and how to verify results
+include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/r/rpl_start_alter_restart_master.result b/mysql-test/suite/rpl/r/rpl_start_alter_restart_master.result
new file mode 100644
index 00000000000..0b608c3a7b5
--- /dev/null
+++ b/mysql-test/suite/rpl/r/rpl_start_alter_restart_master.result
@@ -0,0 +1,85 @@
+include/master-slave.inc
+[connection master]
+connection slave;
+SET @old_debug_slave= @@global.debug;
+stop slave;
+SET GLOBAL slave_parallel_threads=4;
+set global slave_parallel_mode=optimistic;
+set global gtid_strict_mode=1;
+start slave;
+connection master;
+call mtr.add_suppression("ALTER query started at .+ could not be completed");
+SET @old_debug_master= @@global.debug;
+set binlog_alter_two_phase=true;
+create table t3( a int primary key, b int) engine=innodb;
+connection master;
+connection slave;
+include/stop_slave.inc
+connection master;
+SET SESSION debug_dbug="d,start_alter_kill_after_binlog";
+alter table t3 add column d int;
+ERROR HY000: Lost connection to server during query
+include/rpl_reconnect.inc
+set binlog_alter_two_phase= true;
+alter table t3 add column d int;
+show create table t3;
+Table Create Table
+t3 CREATE TABLE `t3` (
+ `a` int(11) NOT NULL,
+ `b` int(11) DEFAULT NULL,
+ `d` int(11) DEFAULT NULL,
+ PRIMARY KEY (`a`)
+) ENGINE=InnoDB DEFAULT CHARSET=latin1
+include/show_binlog_events.inc
+Log_name Pos Event_type Server_id End_log_pos Info
+master-bin.000001 # Gtid # # BEGIN GTID #-#-#
+master-bin.000001 # Query # # use `mtr`; INSERT INTO test_suppressions (pattern) VALUES ( NAME_CONST('pattern',_latin1'ALTER query started at .+ could not be completed' COLLATE 'latin1_swedish_ci'))
+master-bin.000001 # Query # # COMMIT
+master-bin.000001 # Gtid # # GTID #-#-#
+master-bin.000001 # Query # # use `test`; create table t3( a int primary key, b int) engine=innodb
+master-bin.000001 # Gtid # # GTID #-#-# START ALTER
+master-bin.000001 # Query # # use `test`; alter table t3 add column d int
+include/show_binlog_events.inc
+Log_name Pos Event_type Server_id End_log_pos Info
+master-bin.000002 # Gtid # # GTID #-#-# START ALTER
+master-bin.000002 # Query # # use `test`; alter table t3 add column d int
+master-bin.000002 # Gtid # # GTID #-#-# COMMIT ALTER id=#
+master-bin.000002 # Query # # use `test`; alter table t3 add column d int
+connection slave;
+include/start_slave.inc
+connection master;
+connection slave;
+include/show_binlog_events.inc
+Log_name Pos Event_type Server_id End_log_pos Info
+slave-bin.000001 # Gtid # # BEGIN GTID #-#-#
+slave-bin.000001 # Query # # use `mtr`; INSERT INTO test_suppressions (pattern) VALUES ( NAME_CONST('pattern',_latin1'ALTER query started at .+ could not be completed' COLLATE 'latin1_swedish_ci'))
+slave-bin.000001 # Query # # COMMIT
+slave-bin.000001 # Gtid # # GTID #-#-#
+slave-bin.000001 # Query # # use `test`; create table t3( a int primary key, b int) engine=innodb
+slave-bin.000001 # Gtid # # GTID #-#-# START ALTER
+slave-bin.000001 # Query # # use `test`; alter table t3 add column d int
+slave-bin.000001 # Gtid # # GTID #-#-# START ALTER
+slave-bin.000001 # Query # # use `test`; alter table t3 add column d int
+slave-bin.000001 # Gtid # # GTID #-#-# COMMIT ALTER id=#
+slave-bin.000001 # Query # # use `test`; alter table t3 add column d int
+show create table t3;
+Table Create Table
+t3 CREATE TABLE `t3` (
+ `a` int(11) NOT NULL,
+ `b` int(11) DEFAULT NULL,
+ `d` int(11) DEFAULT NULL,
+ PRIMARY KEY (`a`)
+) ENGINE=InnoDB DEFAULT CHARSET=latin1
+connection master;
+SET GLOBAL debug_dbug= @old_debug_master;
+drop table t3;
+set global binlog_alter_two_phase = No;;
+connection slave;
+SET GLOBAL debug_dbug= @old_debug_slave;
+stop slave;
+set global slave_parallel_threads = 0;;
+set global slave_parallel_mode = optimistic;;
+set global gtid_strict_mode = 0;;
+start slave;
+connection master;
+include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/r/rpl_start_alter_restart_slave.result b/mysql-test/suite/rpl/r/rpl_start_alter_restart_slave.result
new file mode 100644
index 00000000000..b6d0a79d081
--- /dev/null
+++ b/mysql-test/suite/rpl/r/rpl_start_alter_restart_slave.result
@@ -0,0 +1,76 @@
+include/master-slave.inc
+[connection master]
+connection slave;
+SET @old_debug_slave= @@global.debug;
+include/stop_slave.inc
+SET GLOBAL slave_parallel_threads=4;
+set global slave_parallel_mode=optimistic;
+set global gtid_strict_mode=1;
+set global debug_dbug="+d,rpl_slave_stop_CA_before_binlog";
+include/start_slave.inc
+connection master;
+SET @old_debug_master= @@global.debug;
+set global debug_dbug="+d,start_alter_delay_master";
+set global binlog_alter_two_phase=true;
+create table t1( a int primary key, b int) engine=myisam;
+create table t2( a int primary key, b int) engine=myisam;
+connect con1,localhost,root,,;
+connect con2,localhost,root,,;
+connection con1;
+alter table t1 add column c int;;
+# Sleep just to make sure that t1 is logged before t2
+connection con2;
+alter table t2 add column c int;;
+connection master;
+set DEBUG_SYNC= "now signal alter_cont";
+connection con1;
+connection con2;
+create table t3( a int primary key, b int) engine=innodb;
+select @@gtid_binlog_state;
+@@gtid_binlog_state
+0-1-7
+# Stop Slave
+# Master binlog SA SA CA CA
+# lets stop at first CA processing (in process_commit_alter)
+connection slave;
+include/show_binlog_events.inc
+Log_name Pos Event_type Server_id End_log_pos Info
+slave-bin.000001 # Gtid # # GTID #-#-#
+slave-bin.000001 # Query # # use `test`; create table t1( a int primary key, b int) engine=myisam
+slave-bin.000001 # Gtid # # GTID #-#-#
+slave-bin.000001 # Query # # use `test`; create table t2( a int primary key, b int) engine=myisam
+slave-bin.000001 # Gtid # # GTID #-#-# START ALTER
+slave-bin.000001 # Query # # use `test`; alter table t1 add column c int
+slave-bin.000001 # Gtid # # GTID #-#-# START ALTER
+slave-bin.000001 # Query # # use `test`; alter table t2 add column c int
+set debug_sync="now wait_for CA_1_processing";
+connect extra_slave,127.0.0.1,root,,test,$SLAVE_MYPORT;
+stop slave;;
+connection slave;
+set debug_sync="now signal proceed_CA_1";
+connection extra_slave;
+SET GLOBAL debug_dbug= @old_debug_slave;
+connection slave;
+select domain_id, seq_no from mysql.gtid_slave_pos order by seq_no desc limit 1;
+domain_id seq_no
+0 5
+include/start_slave.inc
+connection master;
+connection slave;
+# only SA SA CA should be executed
+select domain_id, seq_no from mysql.gtid_slave_pos order by seq_no desc limit 1;
+domain_id seq_no
+0 7
+connection master;
+drop table t1,t2,t3;
+set global binlog_alter_two_phase = No;;
+SET GLOBAL debug_dbug= @old_debug_master;
+set DEBUG_SYNC= 'RESET';
+connection slave;
+stop slave;
+set global slave_parallel_threads = 0;;
+set global slave_parallel_mode = optimistic;;
+set global gtid_strict_mode = 0;;
+set DEBUG_SYNC= 'RESET';
+start slave;
+include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/r/rpl_start_alter_unsupported.result b/mysql-test/suite/rpl/r/rpl_start_alter_unsupported.result
new file mode 100644
index 00000000000..a538cb0eb9f
--- /dev/null
+++ b/mysql-test/suite/rpl/r/rpl_start_alter_unsupported.result
@@ -0,0 +1,38 @@
+include/master-slave.inc
+[connection master]
+connection master;
+set binlog_alter_two_phase=true;
+CREATE OR REPLACE TABLE tab (
+a int PRIMARY KEY,
+b varchar(50),
+c varchar(50)
+) CHARACTER SET=latin1 engine=innodb;
+SET SESSION alter_algorithm='INSTANT';
+ALTER TABLE tab MODIFY COLUMN c varchar(100);
+drop table tab;
+include/show_binlog_events.inc
+Log_name Pos Event_type Server_id End_log_pos Info
+master-bin.000001 # Gtid # # GTID #-#-#
+master-bin.000001 # Query # # use `test`; CREATE OR REPLACE TABLE tab (
+a int PRIMARY KEY,
+b varchar(50),
+c varchar(50)
+) CHARACTER SET=latin1 engine=innodb
+master-bin.000001 # Gtid # # GTID #-#-#
+master-bin.000001 # Query # # use `test`; ALTER TABLE tab MODIFY COLUMN c varchar(100)
+master-bin.000001 # Gtid # # GTID #-#-#
+master-bin.000001 # Query # # use `test`; DROP TABLE `tab` /* generated by server */
+connection slave;
+include/show_binlog_events.inc
+Log_name Pos Event_type Server_id End_log_pos Info
+slave-bin.000001 # Gtid # # GTID #-#-#
+slave-bin.000001 # Query # # use `test`; CREATE OR REPLACE TABLE tab (
+a int PRIMARY KEY,
+b varchar(50),
+c varchar(50)
+) CHARACTER SET=latin1 engine=innodb
+slave-bin.000001 # Gtid # # GTID #-#-#
+slave-bin.000001 # Query # # use `test`; ALTER TABLE tab MODIFY COLUMN c varchar(100)
+slave-bin.000001 # Gtid # # GTID #-#-#
+slave-bin.000001 # Query # # use `test`; DROP TABLE IF EXISTS `tab` /* generated by server */
+include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/t/rpl_alter_rollback.test b/mysql-test/suite/rpl/t/rpl_alter_rollback.test
new file mode 100644
index 00000000000..daffe62db93
--- /dev/null
+++ b/mysql-test/suite/rpl/t/rpl_alter_rollback.test
@@ -0,0 +1,42 @@
+#
+# MENT-662: Lag Free Alter On Slave
+#
+
+--echo #
+--echo # Test verifies that "ROLLBACK ALTER" is written to binary log upon
+--echo #ALTER command execution failure.
+--echo #
+--source include/have_innodb.inc
+--source include/master-slave.inc
+
+--connection master
+--let $binlog_alter_two_phase= `select @@binlog_alter_two_phase`
+set global binlog_alter_two_phase=YES;
+set binlog_alter_two_phase=YES;
+
+create table t1 (f1 int primary key) engine=InnoDB;
+--error ER_CANT_CREATE_TABLE
+create table t2 (f1 int primary key,
+constraint c1 foreign key (f1) references t1(f1),
+constraint c1 foreign key (f1) references t1(f1)) engine=InnoDB;
+create table t2 (f1 int primary key,
+ constraint c1 foreign key (f1) references t1(f1)) engine=innodb;
+
+--error ER_CANT_CREATE_TABLE
+alter table t2 add constraint c1 foreign key (f1) references t1(f1);
+--source include/show_binlog_events.inc
+
+set foreign_key_checks = 0;
+--error ER_DUP_CONSTRAINT_NAME
+alter table t2 add constraint c1 foreign key (f1) references t1(f1);
+--source include/show_binlog_events.inc
+--sync_slave_with_master
+
+--connection master
+drop table t2, t1;
+--sync_slave_with_master
+
+--connection master
+--eval set global binlog_alter_two_phase=$binlog_alter_two_phase;
+
+--source include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/t/rpl_start_alter_1.test b/mysql-test/suite/rpl/t/rpl_start_alter_1.test
new file mode 100644
index 00000000000..c5cdd02ba31
--- /dev/null
+++ b/mysql-test/suite/rpl/t/rpl_start_alter_1.test
@@ -0,0 +1,33 @@
+#
+# Start Alter with Legacy Replication
+#
+--source include/have_innodb.inc
+--source include/have_debug.inc
+--source include/master-slave.inc
+
+--connection master
+--let $binlog_alter_two_phase= `select @@binlog_alter_two_phase`
+set global binlog_alter_two_phase=YES;
+set binlog_alter_two_phase=YES;
+--connection slave
+--let $gtid_strict_mode= `select @@gtid_strict_mode`
+set global gtid_strict_mode=1;
+
+--echo # Legacy Master Slave
+--let $domain_1=0
+--let $domain_2=0
+--let $M_port= $MASTER_MYPORT
+--let $S_port= $SLAVE_MYPORT
+--let $sync_slave=1
+
+--source include/start_alter_include.inc
+--connection master
+--source include/save_master_gtid.inc
+
+--connection slave
+--source include/sync_with_master_gtid.inc
+--eval set global gtid_strict_mode = $gtid_strict_mode;
+
+--connection master
+--eval set global binlog_alter_two_phase=$binlog_alter_two_phase;
+--source include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/t/rpl_start_alter_2.test b/mysql-test/suite/rpl/t/rpl_start_alter_2.test
new file mode 100644
index 00000000000..7bf1421c125
--- /dev/null
+++ b/mysql-test/suite/rpl/t/rpl_start_alter_2.test
@@ -0,0 +1,53 @@
+#
+# Start Alter with Parallel Replication
+# 1 domain id
+# |Concurrent alters| < |Parallel workers on slave|
+# |x| denotes number of entities it encloses
+#
+--source include/have_innodb.inc
+--source include/have_debug.inc
+--source include/master-slave.inc
+--connection master
+--let $binlog_alter_two_phase= `select @@binlog_alter_two_phase`
+set global binlog_alter_two_phase=Yes;
+set binlog_alter_two_phase=Yes;
+--connection slave
+--let $gtid_strict_mode= `select @@gtid_strict_mode`
+--let $slave_parallel_threads= `select @@slave_parallel_threads`
+--let $slave_parallel_mode= `select @@slave_parallel_mode`
+set global gtid_strict_mode=1;
+
+
+--connection slave
+--source include/stop_slave.inc
+SET GLOBAL slave_parallel_threads=10;
+set global slave_parallel_mode=optimistic;
+change master to master_use_gtid=slave_pos;
+--source include/start_slave.inc
+
+--echo # Parallel Slave
+--connection master
+--let $master_server= "master"
+--let $domain_1=0
+--let $domain_2=0
+--let $M_port= $MASTER_MYPORT
+--let $S_port= $SLAVE_MYPORT
+--let $sync_slave=1
+--source include/start_alter_include.inc
+--connection master
+--source include/save_master_gtid.inc
+
+--connection slave
+--source include/sync_with_master_gtid.inc
+
+--echo # cleanup
+--source include/stop_slave.inc
+--eval set global slave_parallel_threads = $slave_parallel_threads;
+--eval set global slave_parallel_mode = $slave_parallel_mode;
+--eval set global gtid_strict_mode = $gtid_strict_mode;
+set global gtid_domain_id= 0;
+--source include/start_slave.inc
+
+--connection master
+--eval set global binlog_alter_two_phase=$binlog_alter_two_phase;
+--source include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/t/rpl_start_alter_3.test b/mysql-test/suite/rpl/t/rpl_start_alter_3.test
new file mode 100644
index 00000000000..d733cfc14b8
--- /dev/null
+++ b/mysql-test/suite/rpl/t/rpl_start_alter_3.test
@@ -0,0 +1,54 @@
+#
+# Start Alter with Parallel Replication
+# 1 domain id
+# |Concurrent alters| >= |Parallel workers on slave|
+# |x| denotes number of entities it encloses
+#
+--source include/have_log_bin.inc
+--source include/have_innodb.inc
+--source include/master-slave.inc
+--source include/have_debug.inc
+--connection master
+--let $binlog_alter_two_phase= `select @@binlog_alter_two_phase`
+set global binlog_alter_two_phase=Yes;
+set binlog_alter_two_phase=Yes;
+--connection slave
+--let $gtid_strict_mode= `select @@gtid_strict_mode`
+--let $slave_parallel_threads= `select @@slave_parallel_threads`
+--let $slave_parallel_mode= `select @@slave_parallel_mode`
+set global gtid_strict_mode=1;
+
+
+--connection slave
+--source include/stop_slave.inc
+SET GLOBAL slave_parallel_threads=4;
+set global slave_parallel_mode=optimistic;
+change master to master_use_gtid=slave_pos;
+--source include/start_slave.inc
+
+--echo # Parallel Slave
+--connection master
+--let $master_server= "master"
+--let $domain_1=0
+--let $domain_2=0
+--let $M_port= $MASTER_MYPORT
+--let $S_port= $SLAVE_MYPORT
+--let $sync_slave=1
+--source include/start_alter_include.inc
+--connection master
+--source include/save_master_gtid.inc
+
+--connection slave
+--source include/sync_with_master_gtid.inc
+
+--echo # cleanup
+--source include/stop_slave.inc
+--eval set global slave_parallel_threads = $slave_parallel_threads;
+--eval set global slave_parallel_mode = $slave_parallel_mode;
+--eval set global gtid_strict_mode = $gtid_strict_mode;
+set global gtid_domain_id= 0;
+--source include/start_slave.inc
+
+--connection master
+--eval set global binlog_alter_two_phase=$binlog_alter_two_phase;
+--source include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/t/rpl_start_alter_4.test b/mysql-test/suite/rpl/t/rpl_start_alter_4.test
new file mode 100644
index 00000000000..7f0146f97c3
--- /dev/null
+++ b/mysql-test/suite/rpl/t/rpl_start_alter_4.test
@@ -0,0 +1,54 @@
+#
+# Start Alter with Parallel Replication
+# 2 domain id
+# |Concurrent alters| < |Parallel workers on slave|
+# |x| denotes number of entities it encloses
+#
+--source include/have_log_bin.inc
+--source include/have_innodb.inc
+--source include/master-slave.inc
+--source include/have_debug.inc
+--connection master
+--let $binlog_alter_two_phase= `select @@binlog_alter_two_phase`
+set global binlog_alter_two_phase=Yes;
+set binlog_alter_two_phase=Yes;
+--connection slave
+--let $gtid_strict_mode= `select @@gtid_strict_mode`
+--let $slave_parallel_threads= `select @@slave_parallel_threads`
+--let $slave_parallel_mode= `select @@slave_parallel_mode`
+set global gtid_strict_mode=1;
+
+--connection slave
+--source include/stop_slave.inc
+SET GLOBAL slave_parallel_threads=10;
+set global slave_parallel_mode=optimistic;
+change master to master_use_gtid=slave_pos;
+--source include/start_slave.inc
+
+--echo # Parallel Slave
+--connection master
+--let $master_server= "master"
+--let $domain_1=11
+--let $domain_2=12
+--let $M_port= $MASTER_MYPORT
+--let $S_port= $SLAVE_MYPORT
+--let $sync_slave=1
+--source include/start_alter_include.inc
+--connection master
+--source include/save_master_gtid.inc
+
+--connection slave
+--source include/sync_with_master_gtid.inc
+
+--echo # cleanup
+--source include/stop_slave.inc
+--eval set global slave_parallel_threads = $slave_parallel_threads;
+--eval set global slave_parallel_mode = $slave_parallel_mode;
+--eval set global gtid_strict_mode = $gtid_strict_mode;
+set global gtid_domain_id= 0;
+--source include/start_slave.inc
+
+--connection master
+--eval set global binlog_alter_two_phase=$binlog_alter_two_phase;
+set global gtid_domain_id= 0;
+--source include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/t/rpl_start_alter_5.test b/mysql-test/suite/rpl/t/rpl_start_alter_5.test
new file mode 100644
index 00000000000..b9d303fbdf5
--- /dev/null
+++ b/mysql-test/suite/rpl/t/rpl_start_alter_5.test
@@ -0,0 +1,54 @@
+#
+# Start Alter with Parallel Replication
+# 2 domain id
+# |Concurrent alters| >= |Parallel workers on slave|
+# |x| denotes number of entities it encloses
+#
+--source include/have_log_bin.inc
+--source include/have_innodb.inc
+--source include/master-slave.inc
+--source include/have_debug.inc
+--connection master
+--let $binlog_alter_two_phase= `select @@binlog_alter_two_phase`
+set global binlog_alter_two_phase=Yes;
+set binlog_alter_two_phase=Yes;
+--connection slave
+--let $gtid_strict_mode= `select @@gtid_strict_mode`
+--let $slave_parallel_threads= `select @@slave_parallel_threads`
+--let $slave_parallel_mode= `select @@slave_parallel_mode`
+set global gtid_strict_mode=1;
+
+--connection slave
+--source include/stop_slave.inc
+SET GLOBAL slave_parallel_threads=4;
+set global slave_parallel_mode=optimistic;
+change master to master_use_gtid=slave_pos;
+--source include/start_slave.inc
+
+--echo # Parallel Slave
+--connection master
+--let $master_server= "master"
+--let $domain_1=11
+--let $domain_2=12
+--let $M_port= $MASTER_MYPORT
+--let $S_port= $SLAVE_MYPORT
+--let $sync_slave=1
+--source include/start_alter_include.inc
+--connection master
+--source include/save_master_gtid.inc
+
+--connection slave
+--source include/sync_with_master_gtid.inc
+
+--echo # cleanup
+--source include/stop_slave.inc
+--eval set global slave_parallel_threads = $slave_parallel_threads;
+--eval set global slave_parallel_mode = $slave_parallel_mode;
+--eval set global gtid_strict_mode = $gtid_strict_mode;
+set global gtid_domain_id= 0;
+--source include/start_slave.inc
+
+--connection master
+--eval set global binlog_alter_two_phase=$binlog_alter_two_phase;
+set global gtid_domain_id= 0;
+--source include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/t/rpl_start_alter_6.test b/mysql-test/suite/rpl/t/rpl_start_alter_6.test
new file mode 100644
index 00000000000..01d98baf4ad
--- /dev/null
+++ b/mysql-test/suite/rpl/t/rpl_start_alter_6.test
@@ -0,0 +1,58 @@
+#
+# Start Alter with Parallel Replication
+# 2 domain id
+# |Concurrent alters| < |Parallel workers on slave|
+# |x| denotes number of entities it encloses
+# slave_domain_parallel_threads < |Concurrent Alters|
+#
+--source include/have_log_bin.inc
+--source include/have_innodb.inc
+--source include/master-slave.inc
+--source include/have_debug.inc
+--connection master
+--let $binlog_alter_two_phase= `select @@binlog_alter_two_phase`
+set global binlog_alter_two_phase=Yes;
+set binlog_alter_two_phase=Yes;
+--connection slave
+--let $gtid_strict_mode= `select @@gtid_strict_mode`
+--let $slave_parallel_threads= `select @@slave_parallel_threads`
+--let $slave_parallel_mode= `select @@slave_parallel_mode`
+--let $slave_domain_parallel_threads= `select @@slave_domain_parallel_threads`
+set global gtid_strict_mode=1;
+
+--connection slave
+--source include/stop_slave.inc
+SET GLOBAL slave_parallel_threads=10;
+set global slave_parallel_mode=optimistic;
+set global slave_domain_parallel_threads=3;
+change master to master_use_gtid=slave_pos;
+--source include/start_slave.inc
+
+--echo # Parallel Slave
+--connection master
+--let $master_server= "master"
+--let $domain_1=11
+--let $domain_2=12
+--let $M_port= $MASTER_MYPORT
+--let $S_port= $SLAVE_MYPORT
+--let $sync_slave=1
+--source include/start_alter_include.inc
+--connection master
+--source include/save_master_gtid.inc
+
+--connection slave
+--source include/sync_with_master_gtid.inc
+
+--echo # cleanup
+--source include/stop_slave.inc
+--eval set global slave_parallel_threads = $slave_parallel_threads;
+--eval set global slave_parallel_mode = $slave_parallel_mode;
+--eval set global gtid_strict_mode = $gtid_strict_mode;
+--eval set global slave_domain_parallel_threads = $slave_domain_parallel_threads;
+set global gtid_domain_id= 0;
+--source include/start_slave.inc
+
+--connection master
+--eval set global binlog_alter_two_phase=$binlog_alter_two_phase;
+set global gtid_domain_id= 0;
+--source include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/t/rpl_start_alter_7.cnf b/mysql-test/suite/rpl/t/rpl_start_alter_7.cnf
new file mode 100644
index 00000000000..a0f6dc5710c
--- /dev/null
+++ b/mysql-test/suite/rpl/t/rpl_start_alter_7.cnf
@@ -0,0 +1,19 @@
+!include ../my.cnf
+
+[mysqld.1]
+log-bin
+log-slave-updates
+
+[mysqld.2]
+log-bin
+log-slave-updates
+
+[mysqld.3]
+log-bin
+log-slave-updates
+
+
+[ENV]
+SERVER_MYPORT_1= @mysqld.1.port
+SERVER_MYPORT_2= @mysqld.2.port
+SERVER_MYPORT_3= @mysqld.3.port
diff --git a/mysql-test/suite/rpl/t/rpl_start_alter_7.test b/mysql-test/suite/rpl/t/rpl_start_alter_7.test
new file mode 100644
index 00000000000..b6e76bb8d1e
--- /dev/null
+++ b/mysql-test/suite/rpl/t/rpl_start_alter_7.test
@@ -0,0 +1,112 @@
+#
+# Start Alter with Parallel Replication, With 2 sources
+# 2 domain id (From 2 sources)
+# |Concurrent alters| >= |Parallel workers on slave|
+# |x| denotes number of entities it encloses
+#
+--source include/have_log_bin.inc
+--source include/have_innodb.inc
+--source include/have_debug.inc
+--connect (server_1,127.0.0.1,root,,,$SERVER_MYPORT_1)
+--connect (server_2,127.0.0.1,root,,,$SERVER_MYPORT_2)
+--connect (server_3,127.0.0.1,root,,,$SERVER_MYPORT_3)
+
+--connection server_1
+--let $binlog_alter_two_phase= `select @@binlog_alter_two_phase`
+set global binlog_alter_two_phase=Yes;
+set binlog_alter_two_phase=Yes;
+
+--connection server_2
+stop slave;
+set global binlog_alter_two_phase=true;
+
+--connection server_3
+--let $gtid_strict_mode= `select @@gtid_strict_mode`
+--let $slave_parallel_threads= `select @@slave_parallel_threads`
+--let $slave_parallel_mode= `select @@slave_parallel_mode`
+SET GLOBAL slave_parallel_threads=8;
+set global slave_parallel_mode=optimistic;
+set global gtid_strict_mode=1;
+
+
+--disable_warnings
+--disable_query_log
+--replace_result $SERVER_MYPORT_1 MYPORT_1
+eval change master 'm1' to master_port=$SERVER_MYPORT_1 , master_host='127.0.0.1', master_user='root', master_use_gtid=slave_pos;
+--replace_result $SERVER_MYPORT_2 MYPORT_2
+eval change master 'm2' to master_port=$SERVER_MYPORT_2 , master_host='127.0.0.1', master_user='root', master_use_gtid=slave_pos;
+--enable_query_log
+--enable_warnings
+
+--connection server_1
+set gtid_domain_id= 11;
+create database s1;
+use s1;
+--let $domain_1=11
+--let $domain_2=11
+--let $M_port= $SERVER_MYPORT_1
+--let $S_port= $SERVER_MYPORT_3
+--let $sync_slave=0
+--let $db_name=s1
+--source include/start_alter_include.inc
+--connection server_1
+drop database s1;
+select @@gtid_binlog_pos;
+--let $master_pos_1= `SELECT @@gtid_binlog_pos`
+
+--connection server_2
+set gtid_domain_id= 12;
+create database s2;
+use s2;
+--let $domain_1=12
+--let $domain_2=12
+--let $M_port= $SERVER_MYPORT_2
+--let $S_port= $SERVER_MYPORT_3
+--let $sync_slave=0
+--let $db_name=s2
+--source include/start_alter_include.inc
+--connection server_2
+drop database s2;
+select @@gtid_binlog_pos;
+--let $master_pos_2= `SELECT @@gtid_binlog_pos`
+
+--connection server_3
+start all slaves;
+set default_master_connection = 'm1';
+--source include/wait_for_slave_to_start.inc
+set default_master_connection = 'm2';
+--source include/wait_for_slave_to_start.inc
+
+set default_master_connection = 'm1';
+--let $master_pos= $master_pos_1
+--source include/sync_with_master_gtid.inc
+set default_master_connection = 'm2';
+--let $master_pos= $master_pos_2
+--source include/sync_with_master_gtid.inc
+
+--echo # cleanup
+--connection server_3
+set default_master_connection = 'm1';
+--source include/stop_slave.inc
+set default_master_connection = 'm2';
+--source include/stop_slave.inc
+--eval set global slave_parallel_threads = $slave_parallel_threads;
+--eval set global slave_parallel_mode = $slave_parallel_mode;
+--eval set global gtid_strict_mode = $gtid_strict_mode;
+set global gtid_domain_id= 0;
+reset master;
+RESET SLAVE ALL;
+SET GLOBAL gtid_slave_pos= '';
+
+--connection server_1
+--eval set global binlog_alter_two_phase=$binlog_alter_two_phase;
+set global gtid_domain_id= 0;
+reset master;
+--connection server_2
+set global gtid_domain_id= 0;
+--eval set global binlog_alter_two_phase=$binlog_alter_two_phase;
+reset master;
+
+--disconnect server_1
+--disconnect server_2
+--disconnect server_3
diff --git a/mysql-test/suite/rpl/t/rpl_start_alter_8.cnf b/mysql-test/suite/rpl/t/rpl_start_alter_8.cnf
new file mode 100644
index 00000000000..a0f6dc5710c
--- /dev/null
+++ b/mysql-test/suite/rpl/t/rpl_start_alter_8.cnf
@@ -0,0 +1,19 @@
+!include ../my.cnf
+
+[mysqld.1]
+log-bin
+log-slave-updates
+
+[mysqld.2]
+log-bin
+log-slave-updates
+
+[mysqld.3]
+log-bin
+log-slave-updates
+
+
+[ENV]
+SERVER_MYPORT_1= @mysqld.1.port
+SERVER_MYPORT_2= @mysqld.2.port
+SERVER_MYPORT_3= @mysqld.3.port
diff --git a/mysql-test/suite/rpl/t/rpl_start_alter_8.test b/mysql-test/suite/rpl/t/rpl_start_alter_8.test
new file mode 100644
index 00000000000..dcdc862992b
--- /dev/null
+++ b/mysql-test/suite/rpl/t/rpl_start_alter_8.test
@@ -0,0 +1,109 @@
+#
+# Start Alter with Parallel Replication, With 2 sources
+# 2 domain id (From 2 sources)
+# |Concurrent alters| < |Parallel workers on slave|
+# |x| denotes number of entities it encloses
+#
+--source include/have_log_bin.inc
+--source include/have_innodb.inc
+--source include/have_debug.inc
+--connect (server_1,127.0.0.1,root,,,$SERVER_MYPORT_1)
+--connect (server_2,127.0.0.1,root,,,$SERVER_MYPORT_2)
+--connect (server_3,127.0.0.1,root,,,$SERVER_MYPORT_3)
+
+--connection server_1
+--let $binlog_alter_two_phase= `select @@binlog_alter_two_phase`
+set global binlog_alter_two_phase=Yes;
+set binlog_alter_two_phase=Yes;
+
+--connection server_2
+stop slave;
+set global binlog_alter_two_phase=true;
+
+--connection server_3
+--let $gtid_strict_mode= `select @@gtid_strict_mode`
+--let $slave_parallel_threads= `select @@slave_parallel_threads`
+--let $slave_parallel_mode= `select @@slave_parallel_mode`
+SET GLOBAL slave_parallel_threads=20;
+set global slave_parallel_mode=optimistic;
+set global gtid_strict_mode=1;
+
+--disable_warnings
+--disable_query_log
+--replace_result $SERVER_MYPORT_1 MYPORT_1
+eval change master 'm1' to master_port=$SERVER_MYPORT_1 , master_host='127.0.0.1', master_user='root', master_use_gtid=slave_pos;
+--replace_result $SERVER_MYPORT_2 MYPORT_2
+eval change master 'm2' to master_port=$SERVER_MYPORT_2 , master_host='127.0.0.1', master_user='root', master_use_gtid=slave_pos;
+--enable_query_log
+--enable_warnings
+
+--connection server_1
+set gtid_domain_id= 11;
+create database s1;
+use s1;
+--let $domain_1=11
+--let $domain_2=11
+--let $M_port= $SERVER_MYPORT_1
+--let $S_port= $SERVER_MYPORT_3
+--let $sync_slave=0
+--let $db_name=s1
+--source include/start_alter_include.inc
+--connection server_1
+drop database s1;
+--let $master_pos_1= `SELECT @@gtid_binlog_pos`
+
+--connection server_2
+set gtid_domain_id= 12;
+create database s2;
+use s2;
+--let $domain_1=12
+--let $domain_2=12
+--let $M_port= $SERVER_MYPORT_2
+--let $S_port= $SERVER_MYPORT_3
+--let $sync_slave=0
+--let $db_name=s2
+--source include/start_alter_include.inc
+--connection server_2
+drop database s2;
+--let $master_pos_2= `SELECT @@gtid_binlog_pos`
+
+--connection server_3
+start all slaves;
+set default_master_connection = 'm1';
+--source include/wait_for_slave_to_start.inc
+set default_master_connection = 'm2';
+--source include/wait_for_slave_to_start.inc
+
+set default_master_connection = 'm1';
+--let $master_pos= $master_pos_1
+--source include/sync_with_master_gtid.inc
+set default_master_connection = 'm2';
+--let $master_pos= $master_pos_2
+--source include/sync_with_master_gtid.inc
+
+--echo # cleanup
+--connection server_3
+set default_master_connection = 'm1';
+--source include/stop_slave.inc
+set default_master_connection = 'm2';
+--source include/stop_slave.inc
+--eval set global slave_parallel_threads = $slave_parallel_threads;
+--eval set global slave_parallel_mode = $slave_parallel_mode;
+--eval set global gtid_strict_mode = $gtid_strict_mode;
+set global gtid_domain_id= 0;
+reset master;
+RESET SLAVE ALL;
+SET GLOBAL gtid_slave_pos= '';
+
+--connection server_1
+--eval set global binlog_alter_two_phase=$binlog_alter_two_phase;
+set global gtid_domain_id= 0;
+reset master;
+--connection server_2
+set global gtid_domain_id= 0;
+--eval set global binlog_alter_two_phase=$binlog_alter_two_phase;
+reset master;
+
+--disconnect server_1
+--disconnect server_2
+--disconnect server_3
diff --git a/mysql-test/suite/rpl/t/rpl_start_alter_bugs.test b/mysql-test/suite/rpl/t/rpl_start_alter_bugs.test
new file mode 100644
index 00000000000..52eef9fbb16
--- /dev/null
+++ b/mysql-test/suite/rpl/t/rpl_start_alter_bugs.test
@@ -0,0 +1,47 @@
+#
+# MDEV-22985 Assertion `!(thd->rgi_slave && thd->rgi_slave->did_mark_start_commit)' failed in ha_rollback_trans#
+#
+#
+
+--source include/have_log_bin.inc
+--source include/have_innodb.inc
+--source include/master-slave.inc
+
+--connection master
+set global binlog_alter_two_phase=true;
+
+--connection slave
+stop slave;
+SET global slave_parallel_threads=2;
+set global slave_parallel_mode=optimistic;
+start slave;
+--connection master
+
+CREATE TABLE t1 (i int primary key) ENGINE = InnoDB;
+--connection master1
+ALTER TABLE t1 DROP PRIMARY KEY;
+ALTER TABLE t1 ADD UNIQUE KEY ui (i);
+ALTER TABLE t1 ADD PRIMARY KEY (i);
+
+--sync_slave_with_master
+
+ #MENT 1274
+--connection master
+drop table t1;
+CREATE TABLE t1 (a int)engine=innodb;
+ALTER TABLE t1 add column b int, LOCK=EXCLUSIVE;
+drop table t1;
+CREATE TABLE t1 (pk int)engine=innodb;
+--error ER_CANT_DROP_FIELD_OR_KEY
+ALTER TABLE t1 DROP FOREIGN KEY y, LOCK=EXCLUSIVE;
+drop table t1;
+--sync_slave_with_master
+--connection master
+set global binlog_alter_two_phase=false;
+
+--connection slave
+--source include/stop_slave.inc
+SET global slave_parallel_threads=0;
+--source include/start_slave.inc
+
+--source include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/t/rpl_start_alter_chain_basic.cnf b/mysql-test/suite/rpl/t/rpl_start_alter_chain_basic.cnf
new file mode 100644
index 00000000000..04393b874bd
--- /dev/null
+++ b/mysql-test/suite/rpl/t/rpl_start_alter_chain_basic.cnf
@@ -0,0 +1,23 @@
+!include ../my.cnf
+
+[mysqld.1]
+log-slave-updates
+loose-innodb
+
+[mysqld.2]
+log-slave-updates
+loose-innodb
+
+[mysqld.3]
+log-slave-updates
+loose-innodb
+
+[mysqld.4]
+loose-innodb
+
+[ENV]
+SERVER_MYPORT_3= @mysqld.3.port
+SERVER_MYSOCK_3= @mysqld.3.socket
+
+SERVER_MYPORT_4= @mysqld.4.port
+SERVER_MYSOCK_4= @mysqld.4.socket
diff --git a/mysql-test/suite/rpl/t/rpl_start_alter_chain_basic.test b/mysql-test/suite/rpl/t/rpl_start_alter_chain_basic.test
new file mode 100644
index 00000000000..1f8979c0382
--- /dev/null
+++ b/mysql-test/suite/rpl/t/rpl_start_alter_chain_basic.test
@@ -0,0 +1,54 @@
+#
+# MENT-662 Lag Free alter for slave
+# In this we will see if chain replication works as
+# M->S(Legacy)->S(Parallel)->S(Legacy, without log-slave-upadates)
+#
+--source include/have_innodb.inc
+--let $rpl_topology=1->2->3->4
+--source include/rpl_init.inc
+
+--connection server_3
+--let $gtid_strict_mode= `select @@gtid_strict_mode`
+--let $slave_parallel_threads= `select @@slave_parallel_threads`
+--let $slave_parallel_mode= `select @@slave_parallel_mode`
+set global gtid_strict_mode=1;
+
+--source include/stop_slave.inc
+SET GLOBAL slave_parallel_threads=10;
+set global slave_parallel_mode=optimistic;
+change master to master_use_gtid=slave_pos;
+--source include/start_slave.inc
+
+
+--connection server_1
+--let $binlog_alter_two_phase= `select @@binlog_alter_two_phase`
+set global binlog_alter_two_phase=YES;
+set binlog_alter_two_phase=YES;
+--let $engine=innodb
+--let $sync_slave= 1
+connect(master_node,127.0.0.1,root,,$db_name, $SERVER_MYPORT_1);
+connect(slave_node,127.0.0.1,root,,test, $SERVER_MYPORT_2);
+--source include/start_alter_basic.inc
+--disconnect master_node
+--disconnect slave_node
+--connection server_1
+--eval set global binlog_alter_two_phase=$binlog_alter_two_phase;
+--source include/rpl_sync.inc
+
+
+--connection server_2
+select domain_id, seq_no from mysql.gtid_slave_pos order by seq_no desc limit 1;
+
+--connection server_3
+select domain_id, seq_no from mysql.gtid_slave_pos order by seq_no desc limit 1;
+--source include/stop_slave.inc
+--eval set global slave_parallel_threads = $slave_parallel_threads;
+--eval set global slave_parallel_mode = $slave_parallel_mode;
+--eval set global gtid_strict_mode = $gtid_strict_mode;
+--source include/start_slave.inc
+select @@slave_parallel_threads;
+
+--connection server_4
+select domain_id, seq_no from mysql.gtid_slave_pos order by seq_no desc limit 1;
+
+--source include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/t/rpl_start_alter_mysqlbinlog_1.test b/mysql-test/suite/rpl/t/rpl_start_alter_mysqlbinlog_1.test
new file mode 100644
index 00000000000..5c78eb290c8
--- /dev/null
+++ b/mysql-test/suite/rpl/t/rpl_start_alter_mysqlbinlog_1.test
@@ -0,0 +1,39 @@
+#
+# Start Alter with binlog applied using mysqlbinlog
+# single maser with only one domain id
+#
+--source include/have_innodb.inc
+--source include/have_debug.inc
+--source include/master-slave.inc
+
+--connection master
+set global binlog_alter_two_phase=true;
+--connection slave
+--source include/stop_slave.inc
+change master to master_use_gtid= current_pos;
+set global gtid_strict_mode=1;
+
+--echo # Legacy Master Slave
+--let $domain_1=0
+--let $domain_2=0
+--let $M_port= $MASTER_MYPORT
+--let $S_port= $SLAVE_MYPORT
+--let $sync_slave=0
+
+--source include/start_alter_include.inc
+--connection master
+--let $MYSQLD_DATADIR= `select @@datadir;`
+--exec $MYSQL_BINLOG $MYSQLD_DATADIR/master-bin.000001 > $MYSQLTEST_VARDIR/tmp/master.sql
+select @@gtid_binlog_state;
+RESET master;
+
+--connection slave
+--exec $MYSQL --host=127.0.0.1 --port=$SLAVE_MYPORT -e "source $MYSQLTEST_VARDIR/tmp/master.sql"
+select @@gtid_binlog_state;
+set global gtid_strict_mode=0;
+--source include/start_slave.inc
+
+--connection master
+set global binlog_alter_two_phase=false;
+remove_file $MYSQLTEST_VARDIR/tmp/master.sql;
+--source include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/t/rpl_start_alter_mysqlbinlog_2.cnf b/mysql-test/suite/rpl/t/rpl_start_alter_mysqlbinlog_2.cnf
new file mode 100644
index 00000000000..a0f6dc5710c
--- /dev/null
+++ b/mysql-test/suite/rpl/t/rpl_start_alter_mysqlbinlog_2.cnf
@@ -0,0 +1,19 @@
+!include ../my.cnf
+
+[mysqld.1]
+log-bin
+log-slave-updates
+
+[mysqld.2]
+log-bin
+log-slave-updates
+
+[mysqld.3]
+log-bin
+log-slave-updates
+
+
+[ENV]
+SERVER_MYPORT_1= @mysqld.1.port
+SERVER_MYPORT_2= @mysqld.2.port
+SERVER_MYPORT_3= @mysqld.3.port
diff --git a/mysql-test/suite/rpl/t/rpl_start_alter_mysqlbinlog_2.test b/mysql-test/suite/rpl/t/rpl_start_alter_mysqlbinlog_2.test
new file mode 100644
index 00000000000..eff80ba7403
--- /dev/null
+++ b/mysql-test/suite/rpl/t/rpl_start_alter_mysqlbinlog_2.test
@@ -0,0 +1,164 @@
+#
+# MENT-662 Lag Free Alter On Slave
+#
+
+# Start Alter with Parallel Replication, With 2 sources
+# 2 domain id (From 2 sources)
+# |Concurrent alters| < |Parallel workers on slave|
+# |x| denotes number of entities it encloses
+# And then binary log from slave is replayed to slave again to check if
+# binlog output is okay.
+#
+
+--source include/have_log_bin.inc
+--source include/have_innodb.inc
+--source include/have_debug.inc
+--connect (server_1,127.0.0.1,root,,,$SERVER_MYPORT_1)
+--connect (server_2,127.0.0.1,root,,,$SERVER_MYPORT_2)
+--connect (server_3,127.0.0.1,root,,,$SERVER_MYPORT_3)
+
+--connection server_1
+SET @save_binlog_alter_two_phase= @@GLOBAL.binlog_alter_two_phase;
+SET GLOBAL binlog_alter_two_phase=Yes;
+SET binlog_alter_two_phase=Yes;
+--echo # Create table and perform CA and RA
+CREATE TABLE t1( a INT, b INT) ENGINE=InnoDB;
+INSERT INTO t1 VALUES(1,1);
+INSERT INTO t1 VALUES(2,2);
+--echo # Normal Alter
+ALTER TABLE t1 ADD COLUMN c INT;
+--echo # Failed Alter
+INSERT INTO t1 VALUES(1,1, NULL);
+--error ER_DUP_ENTRY
+ALTER TABLE t1 CHANGE a a INT UNIQUE;
+SHOW CREATE TABLE t1;
+SELECT @@gtid_binlog_state;
+
+--echo # apply the binlog
+let MYSQLD_DATADIR= `select @@datadir;`;
+--exec $MYSQL_BINLOG $MYSQLD_DATADIR/master-bin.000001 > $MYSQLTEST_VARDIR/tmp/master_1.sql
+DROP TABLE t1;
+--echo # reset the binlog
+RESET MASTER;
+
+--echo # execute the binlog
+--exec $MYSQL --port=$SERVER_MYPORT_1 --host=127.0.0.1 -e "source $MYSQLTEST_VARDIR/tmp/master_1.sql"
+SELECT @@gtid_binlog_state;
+--echo # Same as before
+SHOW CREATE TABLE t1;
+DROP TABLE t1;
+--echo # reset the binlog
+RESET MASTER;
+RESET SLAVE;
+remove_file $MYSQLTEST_VARDIR/tmp/master_1.sql;
+
+--connection server_2
+SET @save_binlog_alter_two_phase= @@GLOBAL.binlog_alter_two_phase;
+SET GLOBAL binlog_alter_two_phase=Yes;
+
+--connection server_3
+SET @save_gtid_strict_mode= @@GLOBAL.gtid_strict_mode;
+SET @slave_parallel_threads= @@GLOBAL.slave_parallel_threads;
+SET @slave_parallel_mode= @@GLOBAL.slave_parallel_mode;
+SET GLOBAL slave_parallel_threads=20;
+SET GLOBAL slave_parallel_mode=optimistic;
+SET GLOBAL gtid_strict_mode=1;
+
+--disable_warnings
+--disable_query_log
+--replace_result $SERVER_MYPORT_1 MYPORT_1
+eval CHANGE MASTER 'm1' TO MASTER_PORT=$SERVER_MYPORT_1, MASTER_HOST='127.0.0.1', MASTER_USER='root', MASTER_USE_GTID=slave_pos;
+--replace_result $SERVER_MYPORT_2 MYPORT_2
+eval CHANGE MASTER 'm2' TO MASTER_PORT=$SERVER_MYPORT_2, MASTER_HOST='127.0.0.1', MASTER_USER='root', MASTER_USE_GTID=slave_pos;
+--enable_query_log
+--enable_warnings
+
+--connection server_1
+SET gtid_domain_id= 11;
+CREATE DATABASE s1;
+USE s1;
+--let $domain_1=11
+--let $domain_2=11
+--let $M_port= $SERVER_MYPORT_1
+--let $S_port= $SERVER_MYPORT_3
+--let $sync_slave=0
+--let $db_name=s1
+--source include/start_alter_include.inc
+
+--connection server_1
+DROP DATABASE s1;
+--let $master_pos_1= `SELECT @@gtid_binlog_pos`
+
+--connection server_2
+SET gtid_domain_id= 12;
+CREATE DATABASE s2;
+USE s2;
+--let $domain_1=12
+--let $domain_2=12
+--let $M_port= $SERVER_MYPORT_2
+--let $S_port= $SERVER_MYPORT_3
+--let $sync_slave=0
+--let $db_name=s2
+--source include/start_alter_include.inc
+--connection server_2
+DROP DATABASE s2;
+--let $master_pos_2= `SELECT @@gtid_binlog_pos`
+
+--connection server_3
+START ALL SLAVES;
+SET default_master_connection = 'm1';
+--source include/wait_for_slave_to_start.inc
+SET default_master_connection = 'm2';
+--source include/wait_for_slave_to_start.inc
+
+SET default_master_connection = 'm1';
+--let $master_pos= $master_pos_1
+--source include/sync_with_master_gtid.inc
+SET default_master_connection = 'm2';
+--let $master_pos= $master_pos_2
+--source include/sync_with_master_gtid.inc
+
+--echo # Stop slaves and apply binlog
+--connection server_3
+SET default_master_connection = 'm1';
+--source include/stop_slave.inc
+SET default_master_connection = 'm2';
+--source include/stop_slave.inc
+SET GLOBAL slave_parallel_threads = @slave_parallel_threads;
+SET GLOBAL slave_parallel_mode = @slave_parallel_mode;
+SET GLOBAL gtid_strict_mode = @save_gtid_strict_mode;
+SET GLOBAL gtid_domain_id= 0;
+SELECT @@gtid_binlog_state;
+
+let MYSQLD_DATADIR= `select @@datadir;`;
+--exec $MYSQL_BINLOG $MYSQLD_DATADIR/master-bin.000001 > $MYSQLTEST_VARDIR/tmp/slave_1.sql
+--echo # reset the binlog
+RESET MASTER;
+--echo # execute the binlog
+--exec $MYSQL --port=$SERVER_MYPORT_3 --host=127.0.0.1 -e "source $MYSQLTEST_VARDIR/tmp/slave_1.sql"
+SELECT @@gtid_binlog_state;
+
+--echo # One more time to simulate S->S case
+--exec $MYSQL_BINLOG $MYSQLD_DATADIR/master-bin.000001 > $MYSQLTEST_VARDIR/tmp/slave_2.sql
+RESET MASTER;
+--echo # execute the binlog
+--exec $MYSQL --port=$SERVER_MYPORT_3 --host=127.0.0.1 -e "source $MYSQLTEST_VARDIR/tmp/slave_2.sql"
+SELECT @@gtid_binlog_state;
+remove_file $MYSQLTEST_VARDIR/tmp/slave_1.sql;
+remove_file $MYSQLTEST_VARDIR/tmp/slave_2.sql;
+RESET MASTER;
+RESET SLAVE ALL;
+SET GLOBAL gtid_slave_pos= '';
+
+--connection server_1
+SET GLOBAL binlog_alter_two_phase=@save_binlog_alter_two_phase;
+SET GLOBAL gtid_domain_id= 0;
+RESET MASTER;
+--connection server_2
+SET GLOBAL gtid_domain_id= 0;
+SET GLOBAL binlog_alter_two_phase=@save_binlog_alter_two_phase;
+RESET MASTER;
+
+--disconnect server_1
+--disconnect server_2
+--disconnect server_3
diff --git a/mysql-test/suite/rpl/t/rpl_start_alter_options.test b/mysql-test/suite/rpl/t/rpl_start_alter_options.test
new file mode 100644
index 00000000000..ecf6829aada
--- /dev/null
+++ b/mysql-test/suite/rpl/t/rpl_start_alter_options.test
@@ -0,0 +1,33 @@
+# This test will test all the option related to the Alter Table command
+# NOTE not all alter statements will follow alter_algorithm since for some statements
+# copy is only option
+# parameters
+# $alter_algorithm {DEFAULT|INPLACE|COPY|NOCOPY|INSTANT}
+# $show_binlog
+#
+
+--source include/have_partition.inc
+--source include/have_innodb.inc
+--source include/master-slave.inc
+
+--let $alter_algorithm= default
+#--let $show_binlog= false
+--source include/start_alter_options.inc
+
+--let $alter_algorithm= inplace
+--source include/start_alter_options.inc
+
+--let $alter_algorithm= copy
+--source include/start_alter_options.inc
+
+--echo # TODO remove support for nocopy and instant
+--echo # and how to verify results
+#--let $alter_algorithm= nocopy
+#--let $show_binlog= true
+#--source include/start_alter_options.inc
+
+#--let $alter_algorithm= instant
+#--let $show_binlog= true
+#--source include/start_alter_options.inc
+
+--source include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/t/rpl_start_alter_restart_master.test b/mysql-test/suite/rpl/t/rpl_start_alter_restart_master.test
new file mode 100644
index 00000000000..4abdc5e3b94
--- /dev/null
+++ b/mysql-test/suite/rpl/t/rpl_start_alter_restart_master.test
@@ -0,0 +1,75 @@
+# Test crashing of master after writing start alter into binary log.
+# And the doing the same alter again, to test on slave if that is successful
+# ====> SA Crash SA CA Case
+#
+--source include/have_log_bin.inc
+--source include/have_binlog_format_mixed.inc
+--source include/have_innodb.inc
+--source include/master-slave.inc
+--source include/have_debug.inc
+
+--connection slave
+SET @old_debug_slave= @@global.debug;
+stop slave;
+--let $gtid_strict_mode= `select @@gtid_strict_mode`
+--let $slave_parallel_threads= `select @@slave_parallel_threads`
+--let $slave_parallel_mode= `select @@slave_parallel_mode`
+SET GLOBAL slave_parallel_threads=4;
+set global slave_parallel_mode=optimistic;
+set global gtid_strict_mode=1;
+start slave;
+
+--connection master
+call mtr.add_suppression("ALTER query started at .+ could not be completed");
+
+SET @old_debug_master= @@global.debug;
+--let $binlog_alter_two_phase= `select @@binlog_alter_two_phase`
+set binlog_alter_two_phase=true;
+create table t3( a int primary key, b int) engine=innodb;
+
+--connection master
+--sync_slave_with_master
+--source include/stop_slave.inc
+
+
+--connection master
+--exec echo "restart" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
+SET SESSION debug_dbug="d,start_alter_kill_after_binlog";
+--error 2013
+alter table t3 add column d int;
+
+--let $rpl_server_number= 1
+--source include/rpl_reconnect.inc
+set binlog_alter_two_phase= true;
+alter table t3 add column d int;
+show create table t3;
+--source include/show_binlog_events.inc
+--let $binlog_file=master-bin.000002
+--source include/show_binlog_events.inc
+--let $binlog_file=
+
+--connection slave
+--source include/start_slave.inc
+
+--connection master
+--sync_slave_with_master
+--source include/show_binlog_events.inc
+show create table t3;
+
+
+--connection master
+SET GLOBAL debug_dbug= @old_debug_master;
+drop table t3;
+--eval set global binlog_alter_two_phase = $binlog_alter_two_phase;
+
+--sync_slave_with_master
+SET GLOBAL debug_dbug= @old_debug_slave;
+stop slave;
+--eval set global slave_parallel_threads = $slave_parallel_threads;
+--eval set global slave_parallel_mode = $slave_parallel_mode;
+--eval set global gtid_strict_mode = $gtid_strict_mode;
+start slave;
+--connection master
+let MYSQLD_DATADIR= `select @@datadir;`;
+--remove_files_wildcard $MYSQLD_DATADIR/test #sql*.frm
+--source include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/t/rpl_start_alter_restart_slave.test b/mysql-test/suite/rpl/t/rpl_start_alter_restart_slave.test
new file mode 100644
index 00000000000..fc995e06cda
--- /dev/null
+++ b/mysql-test/suite/rpl/t/rpl_start_alter_restart_slave.test
@@ -0,0 +1,86 @@
+# This test will restart the slave in middle of start alter commit alter processing
+# slave will be restarted after start alter and before binlogging of commit alter,
+# Then we will recieved commit alter from the master.
+# Commit alter will act like standalone alter
+# =====> SA SA CA(Stop Slave before binlogging) CA
+#
+--source include/have_log_bin.inc
+--source include/have_innodb.inc
+--source include/master-slave.inc
+--source include/have_debug.inc
+
+--connection slave
+SET @old_debug_slave= @@global.debug;
+--source include/stop_slave.inc
+--let $gtid_strict_mode= `select @@gtid_strict_mode`
+--let $slave_parallel_threads= `select @@slave_parallel_threads`
+--let $slave_parallel_mode= `select @@slave_parallel_mode`
+SET GLOBAL slave_parallel_threads=4;
+set global slave_parallel_mode=optimistic;
+set global gtid_strict_mode=1;
+set global debug_dbug="+d,rpl_slave_stop_CA_before_binlog";
+--source include/start_slave.inc
+#
+# SLAVE Shutdown
+--connection master
+SET @old_debug_master= @@global.debug;
+set global debug_dbug="+d,start_alter_delay_master";
+--let $binlog_alter_two_phase= `select @@binlog_alter_two_phase`
+set global binlog_alter_two_phase=true;
+create table t1( a int primary key, b int) engine=myisam;
+create table t2( a int primary key, b int) engine=myisam;
+connect (con1,localhost,root,,);
+connect (con2,localhost,root,,);
+--connection con1
+--send alter table t1 add column c int;
+--echo # Sleep just to make sure that t1 is logged before t2
+--sleep 1
+--connection con2
+--send alter table t2 add column c int;
+--connection master
+--sleep 1
+set DEBUG_SYNC= "now signal alter_cont";
+
+--connection con1
+--reap
+--connection con2
+--reap
+create table t3( a int primary key, b int) engine=innodb;
+select @@gtid_binlog_state;
+
+--echo # Stop Slave
+--echo # Master binlog SA SA CA CA
+--echo # lets stop at first CA processing (in process_commit_alter)
+--connection slave
+--source include/show_binlog_events.inc
+set debug_sync="now wait_for CA_1_processing";
+connect(extra_slave,127.0.0.1,root,,test,$SLAVE_MYPORT);
+--send stop slave;
+--connection slave
+set debug_sync="now signal proceed_CA_1";
+--connection extra_slave
+--reap
+SET GLOBAL debug_dbug= @old_debug_slave;
+
+--connection slave
+select domain_id, seq_no from mysql.gtid_slave_pos order by seq_no desc limit 1;
+--source include/start_slave.inc
+--connection master
+--sync_slave_with_master
+--echo # only SA SA CA should be executed
+select domain_id, seq_no from mysql.gtid_slave_pos order by seq_no desc limit 1;
+
+--connection master
+drop table t1,t2,t3;
+--eval set global binlog_alter_two_phase = $binlog_alter_two_phase;
+SET GLOBAL debug_dbug= @old_debug_master;
+set DEBUG_SYNC= 'RESET';
+
+--sync_slave_with_master
+stop slave;
+--eval set global slave_parallel_threads = $slave_parallel_threads;
+--eval set global slave_parallel_mode = $slave_parallel_mode;
+--eval set global gtid_strict_mode = $gtid_strict_mode;
+set DEBUG_SYNC= 'RESET';
+start slave;
+--source include/rpl_end.inc
diff --git a/mysql-test/suite/rpl/t/rpl_start_alter_unsupported.test b/mysql-test/suite/rpl/t/rpl_start_alter_unsupported.test
new file mode 100644
index 00000000000..078be1face4
--- /dev/null
+++ b/mysql-test/suite/rpl/t/rpl_start_alter_unsupported.test
@@ -0,0 +1,19 @@
+--source include/have_innodb.inc
+--source include/master-slave.inc
+
+--connection master
+set binlog_alter_two_phase=true;
+CREATE OR REPLACE TABLE tab (
+ a int PRIMARY KEY,
+ b varchar(50),
+ c varchar(50)
+) CHARACTER SET=latin1 engine=innodb;
+
+SET SESSION alter_algorithm='INSTANT';
+ALTER TABLE tab MODIFY COLUMN c varchar(100);
+drop table tab;
+--source include/show_binlog_events.inc
+
+--sync_slave_with_master
+--source include/show_binlog_events.inc
+--source include/rpl_end.inc
diff --git a/mysql-test/suite/sys_vars/r/binlog_alter_two_phase.result b/mysql-test/suite/sys_vars/r/binlog_alter_two_phase.result
new file mode 100644
index 00000000000..bfb7103a3bd
--- /dev/null
+++ b/mysql-test/suite/sys_vars/r/binlog_alter_two_phase.result
@@ -0,0 +1,53 @@
+set binlog_alter_two_phase=NO;
+select @@binlog_alter_two_phase;
+@@binlog_alter_two_phase
+No
+set binlog_alter_two_phase=Yes;
+select @@binlog_alter_two_phase;
+@@binlog_alter_two_phase
+Yes
+# wrong value
+set binlog_alter_two_phase=BINLOG_OY;
+ERROR 42000: Variable 'binlog_alter_two_phase' can't be set to the value of 'BINLOG_OY'
+select @@binlog_alter_two_phase;
+@@binlog_alter_two_phase
+Yes
+#true and false
+set binlog_alter_two_phase=false;
+select @@binlog_alter_two_phase;
+@@binlog_alter_two_phase
+No
+set binlog_alter_two_phase=true;
+select @@binlog_alter_two_phase;
+@@binlog_alter_two_phase
+Yes
+set binlog_alter_two_phase=0;
+select @@binlog_alter_two_phase;
+@@binlog_alter_two_phase
+No
+set binlog_alter_two_phase=1;
+select @@binlog_alter_two_phase;
+@@binlog_alter_two_phase
+Yes
+##wrong value
+set binlog_alter_two_phase=2;
+ERROR 42000: Variable 'binlog_alter_two_phase' can't be set to the value of '2'
+select @@binlog_alter_two_phase;
+@@binlog_alter_two_phase
+Yes
+#Global value
+set global binlog_alter_two_phase=NO;
+connect con1,localhost,root,,;
+select @@binlog_alter_two_phase;
+@@binlog_alter_two_phase
+No
+disconnect con1;
+connection default;
+set global binlog_alter_two_phase=Yes;
+connect con1,localhost,root,,;
+select @@binlog_alter_two_phase;
+@@binlog_alter_two_phase
+Yes
+disconnect con1;
+connection default;
+set global binlog_alter_two_phase=No;;
diff --git a/mysql-test/suite/sys_vars/r/sysvars_server_notembedded.result b/mysql-test/suite/sys_vars/r/sysvars_server_notembedded.result
index 7b811a011ff..2e8e7f4168c 100644
--- a/mysql-test/suite/sys_vars/r/sysvars_server_notembedded.result
+++ b/mysql-test/suite/sys_vars/r/sysvars_server_notembedded.result
@@ -322,6 +322,16 @@ NUMERIC_BLOCK_SIZE NULL
ENUM_VALUE_LIST NULL
READ_ONLY YES
COMMAND_LINE_ARGUMENT REQUIRED
+VARIABLE_NAME BINLOG_ALTER_TWO_PHASE
+VARIABLE_SCOPE SESSION
+VARIABLE_TYPE ENUM
+VARIABLE_COMMENT If set split the alter into 2 statement START ALTER and COMMIT/ROLLBACKALTER
+NUMERIC_MIN_VALUE NULL
+NUMERIC_MAX_VALUE NULL
+NUMERIC_BLOCK_SIZE NULL
+ENUM_VALUE_LIST No,Yes
+READ_ONLY NO
+COMMAND_LINE_ARGUMENT REQUIRED
VARIABLE_NAME BINLOG_ANNOTATE_ROW_EVENTS
VARIABLE_SCOPE SESSION
VARIABLE_TYPE BOOLEAN
diff --git a/mysql-test/suite/sys_vars/t/binlog_alter_two_phase.test b/mysql-test/suite/sys_vars/t/binlog_alter_two_phase.test
new file mode 100644
index 00000000000..b7cc26ec1d5
--- /dev/null
+++ b/mysql-test/suite/sys_vars/t/binlog_alter_two_phase.test
@@ -0,0 +1,50 @@
+#
+# binlog_alter_two_phase system variable
+# Session as well as global
+# Allowed values NO_SPLIT, SPLIT, BINLOG_ONLY (0,1,2)
+
+--let $binlog_alter_two_phase= `select @@binlog_alter_two_phase`
+set binlog_alter_two_phase=NO;
+select @@binlog_alter_two_phase;
+
+set binlog_alter_two_phase=Yes;
+select @@binlog_alter_two_phase;
+
+--echo # wrong value
+--error ER_WRONG_VALUE_FOR_VAR
+set binlog_alter_two_phase=BINLOG_OY;
+select @@binlog_alter_two_phase;
+
+--echo #true and false
+set binlog_alter_two_phase=false;
+select @@binlog_alter_two_phase;
+
+set binlog_alter_two_phase=true;
+select @@binlog_alter_two_phase;
+
+set binlog_alter_two_phase=0;
+select @@binlog_alter_two_phase;
+
+set binlog_alter_two_phase=1;
+select @@binlog_alter_two_phase;
+
+--echo ##wrong value
+--error ER_WRONG_VALUE_FOR_VAR
+set binlog_alter_two_phase=2;
+select @@binlog_alter_two_phase;
+
+
+--echo #Global value
+set global binlog_alter_two_phase=NO;
+connect (con1,localhost,root,,);
+select @@binlog_alter_two_phase;
+disconnect con1;
+
+connection default;
+set global binlog_alter_two_phase=Yes;
+connect (con1,localhost,root,,);
+select @@binlog_alter_two_phase;
+disconnect con1;
+
+connection default;
+--eval set global binlog_alter_two_phase=$binlog_alter_two_phase;
diff --git a/sql/log.cc b/sql/log.cc
index 70ceecc66f8..6e5d71dd70f 100644
--- a/sql/log.cc
+++ b/sql/log.cc
@@ -57,6 +57,7 @@
#include "semisync_master.h"
#include "sp_rcontext.h"
#include "sp_head.h"
+#include "sql_table.h"
#include "wsrep_mysqld.h"
#ifdef WITH_WSREP
@@ -576,13 +577,78 @@ public:
ulong binlog_id;
/* Set if we get an error during commit that must be returned from unlog(). */
bool delayed_error;
-
+ //Will be reset when gtid is written into binlog
+ uint16 gtid_flags3;
+ uint64 sa_seq_no;
private:
binlog_cache_mngr& operator=(const binlog_cache_mngr& info);
binlog_cache_mngr(const binlog_cache_mngr& info);
};
+bool write_bin_log_start_alter(THD *thd, bool& partial_alter,
+ uint64 start_alter_id, bool if_exists,
+ MEM_ROOT *mem)
+{
+ if (start_alter_id)
+ {
+ {
+ Write_log_with_flags wlwf (thd, Log_event::FL_START_ALTER_E1);
+ if (write_bin_log(thd, true, thd->query(), thd->query_length()))
+ {
+ DBUG_ASSERT(thd->is_error());
+ return true;
+ }
+ }
+ Master_info *mi= thd->rgi_slave->rli->mi;
+ start_alter_info *info= thd->rgi_slave->sa_info;
+
+ info->sa_seq_no= start_alter_id;
+ info->domain_id= thd->variables.gtid_domain_id;
+ info->state= start_alter_state::REGISTERED;
+ mysql_mutex_lock(&mi->start_alter_list_lock);
+ mi->start_alter_list.push_back(info, mem);
+ mysql_mutex_unlock(&mi->start_alter_list_lock);
+ thd->rgi_slave->start_alter_ev->update_pos(thd->rgi_slave);
+ thd->rgi_slave->commit_orderer.wait_for_prior_commit(thd);
+ thd->rgi_slave->mark_start_commit();
+ thd->wakeup_subsequent_commits(0);
+ thd->rgi_slave->finish_start_alter_event_group();
+ thd->rgi_slave->set_finish_event_group_called(true);
+ return false;
+ }
+
+ rpl_group_info *rgi= thd->rgi_slave ? thd->rgi_slave : thd->rgi_fake;
+
+ if (!(rgi && rgi->direct_commit_alter) &&
+ thd->variables.binlog_alter_two_phase)
+ {
+ /* slave applier can handle here only regular ALTER */
+ DBUG_ASSERT(!rgi || !(rgi->gtid_ev_flags_extra &
+ (Log_event::FL_START_ALTER_E1 |
+ Log_event::FL_COMMIT_ALTER_E1 |
+ Log_event::FL_ROLLBACK_ALTER_E1)));
+
+ // After logging binlog state stays flagged with SA flags3 an seq_no
+ thd->binlog_setup_trx_data()->gtid_flags3|= Log_event::FL_START_ALTER_E1;
+ if(write_bin_log_with_if_exists(thd, false, true, if_exists, false))
+ {
+ DBUG_ASSERT(thd->is_error());
+ return true;
+ }
+ partial_alter= true;
+ }
+ else if (rgi && rgi->direct_commit_alter)
+ {
+ DBUG_ASSERT(rgi->gtid_ev_flags_extra &
+ Log_event::FL_COMMIT_ALTER_E1);
+
+ partial_alter= true;
+ }
+
+ return false;
+}
+
bool LOGGER::is_log_table_enabled(uint log_table_type)
{
switch (log_table_type) {
@@ -5752,6 +5818,33 @@ binlog_cache_mngr *THD::binlog_setup_trx_data()
DBUG_RETURN(cache_mngr);
}
+
+/*
+ Two phase logged ALTER getter and setter methods.
+*/
+uint16 THD::get_binlog_flags_for_alter()
+{
+ return mysql_bin_log.is_open() ? binlog_setup_trx_data()->gtid_flags3 : 0;
+}
+
+void THD::set_binlog_flags_for_alter(uint16 flags)
+{
+ if (mysql_bin_log.is_open())
+ binlog_setup_trx_data()->gtid_flags3= flags;
+}
+
+uint64 THD::get_binlog_start_alter_seq_no()
+{
+ return mysql_bin_log.is_open() ? binlog_setup_trx_data()->sa_seq_no : 0;
+}
+
+void THD::set_binlog_start_alter_seq_no(uint64 s_no)
+{
+ if (mysql_bin_log.is_open())
+ binlog_setup_trx_data()->sa_seq_no= s_no;
+}
+
+
/*
Function to start a statement and optionally a transaction for the
binary log.
@@ -6264,6 +6357,8 @@ MYSQL_BIN_LOG::write_gtid_event(THD *thd, bool standalone,
DBUG_RETURN(true);
thd->set_last_commit_gtid(gtid);
+ if (thd->get_binlog_flags_for_alter() & Log_event::FL_START_ALTER_E1)
+ thd->set_binlog_start_alter_seq_no(gtid.seq_no);
Gtid_log_event gtid_event(thd, seq_no, domain_id, standalone,
LOG_EVENT_SUPPRESS_USE_F, is_transactional,
@@ -6447,7 +6542,6 @@ MYSQL_BIN_LOG::lookup_domain_in_binlog_state(uint32 domain_id,
return false;
}
-
int
MYSQL_BIN_LOG::bump_seq_no_counter_if_needed(uint32 domain_id, uint64 seq_no)
{
diff --git a/sql/log_event.cc b/sql/log_event.cc
index afca79b008a..68fa1f7bb20 100644
--- a/sql/log_event.cc
+++ b/sql/log_event.cc
@@ -1375,6 +1375,7 @@ code_name(int code)
case Q_MASTER_DATA_WRITTEN_CODE: return "Q_MASTER_DATA_WRITTEN_CODE";
case Q_HRNOW: return "Q_HRNOW";
case Q_XID: return "XID";
+ case Q_GTID_FLAGS3: return "Q_GTID_FLAGS3";
}
sprintf(buf, "CODE#%d", code);
return buf;
@@ -1423,7 +1424,8 @@ Query_log_event::Query_log_event(const uchar *buf, uint event_len,
flags2_inited(0), sql_mode_inited(0), charset_inited(0), flags2(0),
auto_increment_increment(1), auto_increment_offset(1),
time_zone_len(0), lc_time_names_number(0), charset_database_number(0),
- table_map_for_update(0), xid(0), master_data_written(0)
+ table_map_for_update(0), xid(0), master_data_written(0), gtid_extra_flags(0),
+ sa_seq_no(0)
{
ulong data_len;
uint32 tmp;
@@ -1439,28 +1441,28 @@ Query_log_event::Query_log_event(const uchar *buf, uint event_len,
post_header_len= description_event->post_header_len[event_type-1];
DBUG_PRINT("info",("event_len: %u common_header_len: %d post_header_len: %d",
event_len, common_header_len, post_header_len));
-
+
/*
We test if the event's length is sensible, and if so we compute data_len.
We cannot rely on QUERY_HEADER_LEN here as it would not be format-tolerant.
We use QUERY_HEADER_MINIMAL_LEN which is the same for 3.23, 4.0 & 5.0.
*/
if (event_len < (uint)(common_header_len + post_header_len))
- DBUG_VOID_RETURN;
+ DBUG_VOID_RETURN;
data_len= event_len - (common_header_len + post_header_len);
buf+= common_header_len;
-
- thread_id= slave_proxy_id= uint4korr(buf + Q_THREAD_ID_OFFSET);
- exec_time= uint4korr(buf + Q_EXEC_TIME_OFFSET);
- db_len= buf[Q_DB_LEN_OFFSET]; // TODO: add a check of all *_len vars
- error_code= uint2korr(buf + Q_ERR_CODE_OFFSET);
+
+ thread_id = slave_proxy_id = uint4korr(buf + Q_THREAD_ID_OFFSET);
+ exec_time = uint4korr(buf + Q_EXEC_TIME_OFFSET);
+ db_len = (uchar)buf[Q_DB_LEN_OFFSET]; // TODO: add a check of all *_len vars
+ error_code = uint2korr(buf + Q_ERR_CODE_OFFSET);
/*
5.0 format starts here.
Depending on the format, we may or not have affected/warnings etc
The remnent post-header to be parsed has length:
*/
- tmp= post_header_len - QUERY_HEADER_MINIMAL_LEN;
+ tmp= post_header_len - QUERY_HEADER_MINIMAL_LEN;
if (tmp)
{
status_vars_len= uint2korr(buf + Q_STATUS_VARS_LEN_OFFSET);
@@ -1607,13 +1609,27 @@ Query_log_event::Query_log_event(const uchar *buf, uint event_len,
pos+= 3;
break;
}
- case Q_XID:
+ case Q_XID:
{
CHECK_SPACE(pos, end, 8);
xid= uint8korr(pos);
pos+= 8;
break;
}
+ case Q_GTID_FLAGS3:
+ {
+ CHECK_SPACE(pos, end, 2);
+ gtid_extra_flags = uint2korr(pos);
+ pos+= 2;
+ if (gtid_extra_flags & (Log_event::FL_COMMIT_ALTER_E1 |
+ Log_event::FL_ROLLBACK_ALTER_E1))
+ {
+ CHECK_SPACE(pos, end, 8);
+ sa_seq_no = uint8korr(pos);
+ pos+= 8;
+ }
+ break;
+ }
default:
/* That's why you must write status vars in growing order of code */
DBUG_PRINT("info",("Query_log_event has unknown status vars (first has\
@@ -2617,6 +2633,11 @@ Gtid_log_event::Gtid_log_event(const uchar *buf, uint event_len,
DBUG_ASSERT(extra_engines > 0);
}
+ if (flags_extra & (FL_COMMIT_ALTER_E1 | FL_ROLLBACK_ALTER_E1))
+ {
+ sa_seq_no= uint8korr(buf);
+ buf+= 8;
+ }
}
/*
the strict '<' part of the assert corresponds to extra zero-padded
diff --git a/sql/log_event.h b/sql/log_event.h
index 3adc7a26d93..226693b7378 100644
--- a/sql/log_event.h
+++ b/sql/log_event.h
@@ -240,7 +240,8 @@ class String;
1 + 8 /* type, table_map_for_update */ + \
1 + 4 /* type, master_data_written */ + \
1 + 3 /* type, sec_part of NOW() */ + \
- 1 + 16 + 1 + 60/* type, user_len, user, host_len, host */)
+ 1 + 16 + 1 + 60/* type, user_len, user, host_len, host */ + \
+ 1 + 2 + 8 /* type, flags3, seq_no */)
#define MAX_LOG_EVENT_HEADER ( /* in order of Query_log_event::write */ \
LOG_EVENT_HEADER_LEN + /* write_header */ \
QUERY_HEADER_LEN + /* write_data */ \
@@ -321,6 +322,7 @@ class String;
#define Q_HRNOW 128
#define Q_XID 129
+#define Q_GTID_FLAGS3 130
/* Intvar event post-header */
/* Intvar event data */
@@ -1195,6 +1197,15 @@ public:
};
/*
+ _E1 suffix below stands for Extra to infer the extra flags,
+ their "1st" generation (more *generations* can come when necessary).
+ Used in Gtid_log_event as well as Query_log_event
+ */
+ static const uint16 FL_START_ALTER_E1= 2;
+ static const uint16 FL_COMMIT_ALTER_E1= 4;
+ static const uint16 FL_ROLLBACK_ALTER_E1= 8;
+
+ /*
The following type definition is to be used whenever data is placed
and manipulated in a common buffer. Use this typedef for buffers
that contain data containing binary and character data.
@@ -2148,6 +2159,12 @@ public:
Q_MASTER_DATA_WRITTEN_CODE to the slave's server binlog.
*/
uint32 master_data_written;
+ /*
+ A copy of Gtid event's extra flags that is relevant for two-phase
+ logged ALTER.
+ */
+ uint16 gtid_extra_flags;
+ uint64 sa_seq_no; /* data part for CA/RA flags */
#ifdef MYSQL_SERVER
@@ -2159,6 +2176,7 @@ public:
#endif /* HAVE_REPLICATION */
#else
bool print_query_header(IO_CACHE* file, PRINT_EVENT_INFO* print_event_info);
+ bool print_verbose(IO_CACHE* cache, PRINT_EVENT_INFO* print_event_info);
bool print(FILE* file, PRINT_EVENT_INFO* print_event_info);
#endif
@@ -2172,8 +2190,10 @@ public:
my_free(data_buf);
}
Log_event_type get_type_code() { return QUERY_EVENT; }
- static int dummy_event(String *packet, ulong ev_offset, enum enum_binlog_checksum_alg checksum_alg);
- static int begin_event(String *packet, ulong ev_offset, enum enum_binlog_checksum_alg checksum_alg);
+ static int dummy_event(String *packet, ulong ev_offset,
+ enum enum_binlog_checksum_alg checksum_alg);
+ static int begin_event(String *packet, ulong ev_offset,
+ enum enum_binlog_checksum_alg checksum_alg);
#ifdef MYSQL_SERVER
bool write();
virtual bool write_post_header_for_derived() { return FALSE; }
@@ -2199,6 +2219,9 @@ public: /* !!! Public in this patch to allow old usage */
size_t event_len,
enum enum_binlog_checksum_alg
checksum_alg);
+ int handle_split_alter_query_log_event(rpl_group_info *rgi,
+ bool &skip_error_check);
+
#endif /* HAVE_REPLICATION */
/*
If true, the event always be applied by slave SQL thread or be printed by
@@ -3606,6 +3629,7 @@ public:
uint64 seq_no;
uint64 commit_id;
uint32 domain_id;
+ uint64 sa_seq_no; // start alter identifier for CA/RA
#ifdef MYSQL_SERVER
event_xid_t xid;
#else
diff --git a/sql/log_event_client.cc b/sql/log_event_client.cc
index 7e0bf7d8e4c..e1fa3d84bc0 100644
--- a/sql/log_event_client.cc
+++ b/sql/log_event_client.cc
@@ -17,6 +17,7 @@
*/
+#include "log_event.h"
#ifndef MYSQL_CLIENT
#error MYSQL_CLIENT must be defined here
#endif
@@ -2005,6 +2006,19 @@ err:
return 1;
}
+bool Query_log_event::print_verbose(IO_CACHE* cache, PRINT_EVENT_INFO* print_event_info)
+{
+ if (my_b_printf(cache, "### ") ||
+ my_b_write(cache, (uchar *) query, q_len) ||
+ my_b_printf(cache, "\n"))
+ {
+ goto err;
+ }
+ return 0;
+
+err:
+ return 1;
+}
bool Query_log_event::print(FILE* file, PRINT_EVENT_INFO* print_event_info)
{
@@ -2020,9 +2034,41 @@ bool Query_log_event::print(FILE* file, PRINT_EVENT_INFO* print_event_info)
goto err;
if (!is_flashback)
{
- if (my_b_write(&cache, (uchar*) query, q_len) ||
- my_b_printf(&cache, "\n%s\n", print_event_info->delimiter))
- goto err;
+ if (gtid_extra_flags & (Log_event::FL_START_ALTER_E1 |
+ Log_event::FL_COMMIT_ALTER_E1 |
+ Log_event::FL_ROLLBACK_ALTER_E1))
+ {
+ bool do_print_encoded=
+ print_event_info->base64_output_mode != BASE64_OUTPUT_NEVER &&
+ print_event_info->base64_output_mode != BASE64_OUTPUT_DECODE_ROWS &&
+ !print_event_info->short_form;
+ bool comment_mode= gtid_extra_flags & (Log_event::FL_START_ALTER_E1 |
+ Log_event :: FL_ROLLBACK_ALTER_E1)
+ && do_print_encoded;
+ if(comment_mode)
+ my_b_printf(&cache, "/*!100600 ");
+ if (do_print_encoded)
+ my_b_printf(&cache, "BINLOG '\n");
+ if (print_base64(&cache, print_event_info, do_print_encoded))
+ goto err;
+ if (do_print_encoded)
+ {
+ if(comment_mode)
+ my_b_printf(&cache, "' */%s\n", print_event_info->delimiter);
+ else
+ my_b_printf(&cache, "'%s\n", print_event_info->delimiter);
+ }
+ if (print_event_info->verbose && print_verbose(&cache, print_event_info))
+ {
+ goto err;
+ }
+ }
+ else
+ {
+ if (my_b_write(&cache, (uchar*) query, q_len) ||
+ my_b_printf(&cache, "\n%s\n", print_event_info->delimiter))
+ goto err;
+ }
}
else // is_flashback == 1
{
@@ -3854,6 +3900,15 @@ Gtid_log_event::print(FILE *file, PRINT_EVENT_INFO *print_event_info)
if (flags2 & FL_WAITED)
if (my_b_write_string(&cache, " waited"))
goto err;
+ if (flags_extra & FL_START_ALTER_E1)
+ if (my_b_write_string(&cache, " START ALTER"))
+ goto err;
+ if (flags_extra & FL_COMMIT_ALTER_E1)
+ if (my_b_printf(&cache, " COMMIT ALTER id= %lu", sa_seq_no))
+ goto err;
+ if (flags_extra & FL_ROLLBACK_ALTER_E1)
+ if (my_b_printf(&cache, " ROLLBACK ALTER id= %lu", sa_seq_no))
+ goto err;
if (my_b_printf(&cache, "\n"))
goto err;
diff --git a/sql/log_event_server.cc b/sql/log_event_server.cc
index 5cf48f10ad8..a68af180ed6 100644
--- a/sql/log_event_server.cc
+++ b/sql/log_event_server.cc
@@ -52,6 +52,7 @@
#include "compat56.h"
#include "wsrep_mysqld.h"
#include "sql_insert.h"
+#include "sql_table.h"
#include <my_bitmap.h>
#include "rpl_utility.h"
@@ -1303,11 +1304,26 @@ bool Query_log_event::write()
start+= 8;
}
+ if (gtid_extra_flags)
+ {
+ *start++= Q_GTID_FLAGS3;
+ int2store(start, gtid_extra_flags);
+ start+= 2;
+ if (gtid_extra_flags &
+ (Log_event::FL_COMMIT_ALTER_E1 |
+ Log_event::FL_ROLLBACK_ALTER_E1))
+ {
+ int8store(start, sa_seq_no);
+ start+= 8;
+ }
+ }
+
+
/*
NOTE: When adding new status vars, please don't forget to update
the MAX_SIZE_LOG_EVENT_STATUS in log_event.h and update the function
code_name() in this file.
-
+
Here there could be code like
if (command-line-option-which-says-"log_this_variable" && inited)
{
@@ -1415,7 +1431,9 @@ Query_log_event::Query_log_event(THD* thd_arg, const char* query_arg,
lc_time_names_number(thd_arg->variables.lc_time_names->number),
charset_database_number(0),
table_map_for_update((ulonglong)thd_arg->table_map_for_update),
- master_data_written(0)
+ master_data_written(0),
+ gtid_extra_flags(thd_arg->get_binlog_flags_for_alter()),
+ sa_seq_no(0)
{
/* status_vars_len is set just before writing the event */
@@ -1551,11 +1569,15 @@ Query_log_event::Query_log_event(THD* thd_arg, const char* query_arg,
use_cache= trx_cache= TRUE;
break;
default:
- use_cache= sqlcom_can_generate_row_events(thd);
+ use_cache= (gtid_extra_flags) ? false : sqlcom_can_generate_row_events(thd);
break;
}
}
+ if (gtid_extra_flags & (Log_event::FL_COMMIT_ALTER_E1 |
+ Log_event::FL_ROLLBACK_ALTER_E1))
+ sa_seq_no= thd_arg->get_binlog_start_alter_seq_no();
+
if (!use_cache || direct)
{
cache_type= Log_event::EVENT_NO_CACHE;
@@ -1627,6 +1649,177 @@ bool test_if_equal_repl_errors(int expected_error, int actual_error)
}
+static start_alter_info *get_new_start_alter_info(THD *thd)
+{
+ /*
+ Why on global memory ?- So that process_commit/rollback_alter should not get
+ error when spawned threads exits too early.
+ */
+ start_alter_info *info;
+ if (!(info= (start_alter_info *)my_malloc(PSI_INSTRUMENT_ME,
+ sizeof(start_alter_info), MYF(MY_WME))))
+ {
+ sql_print_error("Failed to allocate memory for ddl log free list");
+ return 0;
+ }
+ info->sa_seq_no= 0;
+ info->domain_id= 0;
+ info->state= start_alter_state::INVALID;
+ mysql_cond_init(0, &info->start_alter_cond, NULL);
+ info->error= 0;
+ info->err_msg= nullptr;
+ return info;
+}
+
+
+/*
+ Perform necessary actions for two-phase-logged ALTER parts, to
+ return
+
+ 0 when the event's query proceeds normal parsing and execution
+ 1 when the event skips parsing and execution
+ -1 as error.
+*/
+int Query_log_event::handle_split_alter_query_log_event(rpl_group_info *rgi,
+ bool &skip_error_check)
+{
+ rgi->gtid_ev_flags_extra= gtid_extra_flags;
+ if (gtid_extra_flags & Log_event::FL_START_ALTER_E1)
+ {
+ //No Slave, Normal Slave, Start Alter under Worker 1 will simple binlog and exit
+ if(!rgi->rpt || rgi->reserved_start_alter_thread)
+ {
+ /*
+ We will just write the binlog and move to next event , because COMMIT
+ Alter will take care of actual work
+ */
+ rgi->reserved_start_alter_thread= false;
+ Write_log_with_flags wlwf(thd, Log_event::FL_START_ALTER_E1);
+ if (write_bin_log(thd, false, thd->query(), thd->query_length()))
+ return -1;
+
+ my_ok(thd);
+ return 1;
+ }
+ rgi->sa_info= get_new_start_alter_info(thd);
+
+ return 0;
+ }
+
+ bool is_CA= (gtid_extra_flags & Log_event::FL_COMMIT_ALTER_E1) ? true : false;
+ if (is_CA)
+ {
+ DBUG_EXECUTE_IF("rpl_slave_stop_CA_before_binlog",
+ {
+ debug_sync_set_action(thd,
+ STRING_WITH_LEN("now signal CA_1_processing "
+ "WAIT_FOR proceed_CA_1"));
+ });
+ }
+ start_alter_info *info=NULL;
+ Master_info *mi= NULL;
+
+ rgi->gtid_ev_sa_seq_no= sa_seq_no;
+ // is set for both the direct execution and the write to binlog
+ thd->set_binlog_start_alter_seq_no(sa_seq_no);
+ mi= rgi->rli->mi;
+ mysql_mutex_lock(&mi->start_alter_list_lock);
+ {
+ List_iterator<start_alter_info> info_iterator(mi->start_alter_list);
+ while ((info= info_iterator++))
+ {
+ if(info->sa_seq_no == rgi->gtid_ev_sa_seq_no &&
+ info->domain_id == rgi->current_gtid.domain_id)
+ {
+ info_iterator.remove();
+ break;
+ }
+ }
+ }
+ mysql_mutex_unlock(&mi->start_alter_list_lock);
+
+ if (!info )
+ {
+ if (is_CA)
+ {
+ /*
+ error handeling, direct_commit_alter is turned on, so that we dont
+ wait for master reply in mysql_alter_table (in wait_for_master)
+ */
+ rgi->direct_commit_alter= true;
+ return 0;
+ }
+ else
+ {
+ //Just write the binlog because there is nothing to be done
+ goto write_binlog;
+ }
+ }
+
+ DBUG_ASSERT(info->state == start_alter_state::REGISTERED);
+
+ mysql_mutex_lock(&mi->start_alter_lock);
+ if (is_CA)
+ info->state= start_alter_state::COMMIT_ALTER;
+ else
+ info->state= start_alter_state::ROLLBACK_ALTER;
+ mysql_cond_broadcast(&info->start_alter_cond);
+ mysql_mutex_unlock(&mi->start_alter_lock);
+
+ /*
+ Wait till Start Alter worker has changed the state to ::COMPLETED
+ when start alter worker reaches the old code write_bin_log(), it will
+ change state to COMMITTED
+ */
+ mysql_mutex_lock(&mi->start_alter_lock);
+ while(info->state != start_alter_state::COMPLETED )
+ mysql_cond_wait(&info->start_alter_cond, &mi->start_alter_lock);
+ mysql_mutex_unlock(&mi->start_alter_lock);
+
+write_binlog:
+ /*
+ throw the rollback alter error when info->error is not zero
+ */
+ if(!is_CA)
+ {
+ if(info && info->error)
+ my_message_sql(info->error, info->err_msg, MYF(0));
+
+ /*
+ logged error_code can be zero and still we got rollback alter,
+ this can happen in only these case , for reference look
+ query_error_code
+ if (error == ER_SERVER_SHUTDOWN || error == ER_QUERY_INTERRUPTED ||
+ error == ER_NEW_ABORTING_CONNECTION || error == ER_CONNECTION_KILLED)
+ error= 0;
+ */
+ else if(this->error_code)
+ {
+ my_message_sql(this->error_code, "Rollback Alter Expected Error logged in"
+ " binary log.", MYF(0));
+ }
+ }
+ {
+ Write_log_with_flags wlwf(thd, is_CA ? Log_event::FL_COMMIT_ALTER_E1 :
+ Log_event::FL_ROLLBACK_ALTER_E1);
+
+ if (write_bin_log(thd, false, thd->query(), thd->query_length()))
+ return -1;
+ }
+ if (!thd->is_error())
+ {
+ skip_error_check= true;
+ my_ok(thd);
+ }
+ if (info)
+ {
+ mysql_cond_destroy(&info->start_alter_cond);
+ my_free(info);
+ }
+ return 1;
+}
+
+
/**
@todo
Compare the values of "affected rows" around here. Something
@@ -1655,6 +1848,7 @@ int Query_log_event::do_apply_event(rpl_group_info *rgi,
Relay_log_info const *rli= rgi->rli;
Rpl_filter *rpl_filter= rli->mi->rpl_filter;
bool current_stmt_is_commit;
+ bool skip_error_check= false;
DBUG_ENTER("Query_log_event::do_apply_event");
/*
@@ -1665,6 +1859,7 @@ int Query_log_event::do_apply_event(rpl_group_info *rgi,
you.
*/
thd->catalog= catalog_len ? (char *) catalog : (char *)"";
+ rgi->start_alter_ev= this;
size_t valid_len= Well_formed_prefix(system_charset_info,
db, db_len, NAME_LEN).length();
@@ -1887,39 +2082,55 @@ int Query_log_event::do_apply_event(rpl_group_info *rgi,
thd->variables.option_bits|= OPTION_MASTER_SQL_ERROR;
thd->variables.option_bits&= ~OPTION_GTID_BEGIN;
}
- /* Execute the query (note that we bypass dispatch_command()) */
- Parser_state parser_state;
- if (!parser_state.init(thd, thd->query(), thd->query_length()))
+
+ int sa_result= 0;
+ if (gtid_extra_flags & (Log_event::FL_START_ALTER_E1 |
+ Log_event::FL_COMMIT_ALTER_E1 |
+ Log_event::FL_ROLLBACK_ALTER_E1))
+ sa_result= handle_split_alter_query_log_event(rgi, skip_error_check);
+ if (sa_result == 0)
{
- DBUG_ASSERT(thd->m_digest == NULL);
- thd->m_digest= & thd->m_digest_state;
- DBUG_ASSERT(thd->m_statement_psi == NULL);
- thd->m_statement_psi= MYSQL_START_STATEMENT(&thd->m_statement_state,
- stmt_info_rpl.m_key,
- thd->db.str, thd->db.length,
- thd->charset(), NULL);
- THD_STAGE_INFO(thd, stage_starting);
- MYSQL_SET_STATEMENT_TEXT(thd->m_statement_psi, thd->query(), thd->query_length());
- if (thd->m_digest != NULL)
- thd->m_digest->reset(thd->m_token_array, max_digest_length);
-
- if (thd->slave_thread)
- {
- /*
- To be compatible with previous releases, the slave thread uses the global
- log_slow_disabled_statements value, wich can be changed dynamically, so we
- have to set the sql_log_slow respectively.
- */
- thd->variables.sql_log_slow= !MY_TEST(global_system_variables.log_slow_disabled_statements & LOG_SLOW_DISABLE_SLAVE);
- }
-
- mysql_parse(thd, thd->query(), thd->query_length(), &parser_state);
- /* Finalize server status flags after executing a statement. */
- thd->update_server_status();
- log_slow_statement(thd);
- thd->lex->restore_set_statement_var();
+ /* Execute the query (note that we bypass dispatch_command()) */
+ Parser_state parser_state;
+ if (!parser_state.init(thd, thd->query(), thd->query_length()))
+ {
+ DBUG_ASSERT(thd->m_digest == NULL);
+ thd->m_digest= & thd->m_digest_state;
+ DBUG_ASSERT(thd->m_statement_psi == NULL);
+ thd->m_statement_psi= MYSQL_START_STATEMENT(&thd->m_statement_state,
+ stmt_info_rpl.m_key,
+ thd->db.str, thd->db.length,
+ thd->charset(), NULL);
+ THD_STAGE_INFO(thd, stage_starting);
+ MYSQL_SET_STATEMENT_TEXT(thd->m_statement_psi, thd->query(), thd->query_length());
+ if (thd->m_digest != NULL)
+ thd->m_digest->reset(thd->m_token_array, max_digest_length);
+
+ if (thd->slave_thread)
+ {
+ /*
+ To be compatible with previous releases, the slave thread uses the global
+ log_slow_disabled_statements value, wich can be changed dynamically, so we
+ have to set the sql_log_slow respectively.
+ */
+ thd->variables.sql_log_slow= !MY_TEST(global_system_variables.log_slow_disabled_statements & LOG_SLOW_DISABLE_SLAVE);
+ }
+ mysql_parse(thd, thd->query(), thd->query_length(), &parser_state);
+ /* Finalize server status flags after executing a statement. */
+ thd->update_server_status();
+ log_slow_statement(thd);
+ thd->lex->restore_set_statement_var();
+ if (gtid_extra_flags)
+ rgi->direct_commit_alter= false;
+ }
+ }
+ else if(sa_result == -1)
+ {
+ rli->report(ERROR_LEVEL, expected_error, rgi->gtid_info(),
+ "TODO start alter error");
+ thd->is_slave_error= 1;
+ goto end;
}
-
thd->variables.option_bits&= ~OPTION_MASTER_SQL_ERROR;
}
else
@@ -1983,7 +2194,8 @@ compare_errors:
If we expected a non-zero error code, and we don't get the same error
code, and it should be ignored or is related to a concurrency issue.
*/
- actual_error= thd->is_error() ? thd->get_stmt_da()->sql_errno() : 0;
+ actual_error= thd->is_error() ? thd->get_stmt_da()->sql_errno() :
+ skip_error_check? expected_error : 0;
DBUG_PRINT("info",("expected_error: %d sql_errno: %d",
expected_error, actual_error));
@@ -2320,6 +2532,39 @@ int Start_log_event_v3::do_apply_event(rpl_group_info *rgi)
Format_description_log_event methods
****************************************************************************/
+/*
+ Auxiliary function to conduct cleanup of unfinished two-phase logged ALTERs.
+*/
+static void check_and_remove_stale_alter(Relay_log_info *rli)
+{
+ Master_info *mi= rli->mi;
+ start_alter_info *info=NULL;
+
+ mysql_mutex_lock(&mi->start_alter_list_lock);
+ List_iterator<start_alter_info> info_iterator(mi->start_alter_list);
+ while ((info= info_iterator++))
+ {
+ DBUG_ASSERT(info->state == start_alter_state::REGISTERED);
+
+ sql_print_warning("ALTER query started at u-%u-%llu could not "
+ "be completed because of unexpected master server "
+ "or its binlog change", info->sa_seq_no, // todo:gtid
+ 0, 0);
+ info_iterator.remove();
+ mysql_mutex_lock(&mi->start_alter_lock);
+ info->state= start_alter_state::ROLLBACK_ALTER;
+ mysql_mutex_unlock(&mi->start_alter_lock);
+ mysql_cond_broadcast(&info->start_alter_cond);
+ mysql_mutex_lock(&mi->start_alter_lock);
+ while(info->state != start_alter_state::COMPLETED)
+ mysql_cond_wait(&info->start_alter_cond, &mi->start_alter_lock);
+ mysql_mutex_unlock(&mi->start_alter_lock);
+ mysql_cond_destroy(&info->start_alter_cond);
+ my_free(info);
+ }
+ mysql_mutex_unlock(&mi->start_alter_list_lock);
+}
+
bool Format_description_log_event::write()
{
bool ret;
@@ -2396,16 +2641,23 @@ int Format_description_log_event::do_apply_event(rpl_group_info *rgi)
original place when it comes to us; we'll know this by checking
log_pos ("artificial" events have log_pos == 0).
*/
- if (!is_artificial_event() && created && thd->transaction->all.ha_list)
+ if (!is_artificial_event() && created && !thd->rgi_fake)
{
- /* This is not an error (XA is safe), just an information */
- rli->report(INFORMATION_LEVEL, 0, NULL,
- "Rolling back unfinished transaction (no COMMIT "
- "or ROLLBACK in relay log). A probable cause is that "
- "the master died while writing the transaction to "
- "its binary log, thus rolled back too.");
- rgi->cleanup_context(thd, 1);
+ // check_and_remove stale Start Alter:s
+ if (flags & LOG_EVENT_BINLOG_IN_USE_F)
+ check_and_remove_stale_alter(rli);
+ if (thd->transaction->all.ha_list)
+ {
+ /* This is not an error (XA is safe), just an information */
+ rli->report(INFORMATION_LEVEL, 0, NULL,
+ "Rolling back unfinished transaction (no COMMIT "
+ "or ROLLBACK in relay log). A probable cause is that "
+ "the master died while writing the transaction to "
+ "its binary log, thus rolled back too.");
+ rgi->cleanup_context(thd, 1);
+ }
}
+ DBUG_ASSERT(!thd->rgi_fake || !thd->transaction->all.ha_list);
/*
If this event comes from ourselves, there is no cleaning task to
@@ -3325,6 +3577,14 @@ Gtid_log_event::Gtid_log_event(THD *thd_arg, uint64 seq_no_arg,
if (extra_engines > 0)
flags_extra|= FL_EXTRA_MULTI_ENGINE;
}
+ if (thd->get_binlog_flags_for_alter())
+ {
+ flags_extra |= thd->get_binlog_flags_for_alter();
+ if (flags_extra & (Log_event::FL_COMMIT_ALTER_E1 |
+ Log_event::FL_ROLLBACK_ALTER_E1))
+ sa_seq_no= thd->get_binlog_start_alter_seq_no();
+ flags2|= FL_DDL;
+ }
}
@@ -3402,6 +3662,12 @@ Gtid_log_event::write()
write_len++;
}
+ if (flags_extra & (FL_COMMIT_ALTER_E1 | FL_ROLLBACK_ALTER_E1))
+ {
+ int8store(buf + write_len, sa_seq_no);
+ write_len+= 8;
+ }
+
if (write_len < GTID_HEADER_LEN)
{
bzero(buf+write_len, GTID_HEADER_LEN-write_len);
@@ -3465,6 +3731,20 @@ Gtid_log_event::pack_info(Protocol *protocol)
p= strmov(p, " cid=");
p= longlong10_to_str(commit_id, p, 10);
}
+ if (flags_extra & FL_START_ALTER_E1)
+ {
+ p= strmov(p, " START ALTER");
+ }
+ if (flags_extra & FL_COMMIT_ALTER_E1)
+ {
+ p= strmov(p, " COMMIT ALTER id=");
+ p= longlong10_to_str(sa_seq_no, p, 10);
+ }
+ if (flags_extra & FL_ROLLBACK_ALTER_E1)
+ {
+ p= strmov(p, " ROLLBACK ALTER id=");
+ p= longlong10_to_str(sa_seq_no, p, 10);
+ }
protocol->store(buf, p-buf, &my_charset_bin);
}
@@ -3479,6 +3759,10 @@ Gtid_log_event::do_apply_event(rpl_group_info *rgi)
thd->variables.gtid_domain_id= this->domain_id;
thd->variables.gtid_seq_no= this->seq_no;
rgi->gtid_ev_flags2= flags2;
+
+ rgi->gtid_ev_flags_extra= flags_extra;
+ //OTODO ?? feels like repeated code. Does choose_worker really need it? When NO - remove
+ rgi->gtid_ev_sa_seq_no= sa_seq_no;
thd->reset_for_next_command();
if (opt_gtid_strict_mode && opt_bin_log && opt_log_slave_updates)
diff --git a/sql/mysqld.cc b/sql/mysqld.cc
index 93a548b476e..8a73b6e3ae8 100644
--- a/sql/mysqld.cc
+++ b/sql/mysqld.cc
@@ -910,6 +910,8 @@ PSI_mutex_key key_BINLOG_LOCK_index, key_BINLOG_LOCK_xid_list,
key_LOCK_user_conn, key_LOCK_uuid_short_generator, key_LOG_LOCK_log,
key_master_info_data_lock, key_master_info_run_lock,
key_master_info_sleep_lock, key_master_info_start_stop_lock,
+ key_master_info_start_alter_lock,
+ key_master_info_start_alter_list_lock,
key_mutex_slave_reporting_capability_err_lock, key_relay_log_info_data_lock,
key_rpl_group_info_sleep_lock,
key_relay_log_info_log_space_lock, key_relay_log_info_run_lock,
@@ -994,6 +996,8 @@ static PSI_mutex_info all_server_mutexes[]=
{ &key_master_info_start_stop_lock, "Master_info::start_stop_lock", 0},
{ &key_master_info_run_lock, "Master_info::run_lock", 0},
{ &key_master_info_sleep_lock, "Master_info::sleep_lock", 0},
+ { &key_master_info_start_alter_lock, "Master_info::start_alter_lock", 0},
+ { &key_master_info_start_alter_list_lock, "Master_info::start_alter_lock", 0},
{ &key_mutex_slave_reporting_capability_err_lock, "Slave_reporting_capability::err_lock", 0},
{ &key_relay_log_info_data_lock, "Relay_log_info::data_lock", 0},
{ &key_relay_log_info_log_space_lock, "Relay_log_info::log_space_lock", 0},
diff --git a/sql/mysqld.h b/sql/mysqld.h
index 8c0b92c6446..3a8710f9a85 100644
--- a/sql/mysqld.h
+++ b/sql/mysqld.h
@@ -332,6 +332,8 @@ extern PSI_mutex_key key_BINLOG_LOCK_index, key_BINLOG_LOCK_xid_list,
key_LOCK_user_conn, key_LOG_LOCK_log,
key_master_info_data_lock, key_master_info_run_lock,
key_master_info_sleep_lock, key_master_info_start_stop_lock,
+ key_master_info_start_alter_lock,
+ key_master_info_start_alter_list_lock,
key_mutex_slave_reporting_capability_err_lock, key_relay_log_info_data_lock,
key_relay_log_info_log_space_lock, key_relay_log_info_run_lock,
key_rpl_group_info_sleep_lock,
diff --git a/sql/rpl_gtid.cc b/sql/rpl_gtid.cc
index 306ae878060..bea7e971ffc 100644
--- a/sql/rpl_gtid.cc
+++ b/sql/rpl_gtid.cc
@@ -607,6 +607,8 @@ rpl_slave_state::record_gtid(THD *thd, const rpl_gtid *gtid, uint64 sub_id,
wait_for_commit* suspended_wfc;
void *hton= NULL;
LEX_CSTRING gtid_pos_table_name;
+ TABLE *tbl= nullptr;
+ MDL_savepoint m_start_of_statement_svp(thd->mdl_context.mdl_savepoint());
DBUG_ENTER("record_gtid");
*out_hton= NULL;
@@ -625,6 +627,18 @@ rpl_slave_state::record_gtid(THD *thd, const rpl_gtid *gtid, uint64 sub_id,
if (!in_statement)
thd->reset_for_next_command();
+ if (thd->rgi_slave && (thd->rgi_slave->gtid_ev_flags_extra &
+ Log_event::FL_START_ALTER_E1))
+ {
+ /*
+ store the open table table list in ptr, so that is close_thread_tables
+ is called start alter tables are not closed
+ */
+ mysql_mutex_lock(&thd->LOCK_thd_data);
+ tbl= thd->open_tables;
+ thd->open_tables= nullptr;
+ mysql_mutex_unlock(&thd->LOCK_thd_data);
+ }
/*
Only the SQL thread can call select_gtid_pos_table without a mutex
Other threads needs to use a mutex and take into account that the
@@ -734,12 +748,22 @@ end:
{
if (err || (err= ha_commit_trans(thd, FALSE)))
ha_rollback_trans(thd, FALSE);
-
close_thread_tables(thd);
- if (in_transaction)
- thd->mdl_context.release_statement_locks();
- else
- thd->release_transactional_locks();
+ if (!thd->rgi_slave || !(thd->rgi_slave->gtid_ev_flags_extra &
+ Log_event::FL_START_ALTER_E1))
+ {
+ if (in_transaction)
+ thd->mdl_context.release_statement_locks();
+ else
+ thd->release_transactional_locks();
+ }
+ }
+ if (tbl)
+ {
+ mysql_mutex_lock(&thd->LOCK_thd_data);
+ thd->open_tables= tbl;
+ mysql_mutex_unlock(&thd->LOCK_thd_data);
+ thd->mdl_context.rollback_to_savepoint(m_start_of_statement_svp);
}
thd->lex->restore_backup_query_tables_list(&lex_backup);
thd->variables.option_bits= thd_saved_option;
diff --git a/sql/rpl_mi.cc b/sql/rpl_mi.cc
index 4fd36891a4b..00ea7743810 100644
--- a/sql/rpl_mi.cc
+++ b/sql/rpl_mi.cc
@@ -85,6 +85,14 @@ Master_info::Master_info(LEX_CSTRING *connection_name_arg,
mysql_mutex_init(key_master_info_data_lock, &data_lock, MY_MUTEX_INIT_FAST);
mysql_mutex_init(key_master_info_start_stop_lock, &start_stop_lock,
MY_MUTEX_INIT_SLOW);
+ /*
+ start_alter_lock will protect individual start_alter_info while
+ start_alter_list_lock is for list insertion and deletion operations
+ */
+ mysql_mutex_init(key_master_info_start_alter_lock, &start_alter_lock,
+ MY_MUTEX_INIT_FAST);
+ mysql_mutex_init(key_master_info_start_alter_list_lock, &start_alter_list_lock,
+ MY_MUTEX_INIT_FAST);
mysql_mutex_setflags(&run_lock, MYF_NO_DEADLOCK_DETECTION);
mysql_mutex_setflags(&data_lock, MYF_NO_DEADLOCK_DETECTION);
mysql_mutex_init(key_master_info_sleep_lock, &sleep_lock, MY_MUTEX_INIT_FAST);
@@ -121,6 +129,8 @@ Master_info::~Master_info()
mysql_mutex_destroy(&data_lock);
mysql_mutex_destroy(&sleep_lock);
mysql_mutex_destroy(&start_stop_lock);
+ mysql_mutex_destroy(&start_alter_lock);
+ mysql_mutex_destroy(&start_alter_list_lock);
mysql_cond_destroy(&data_cond);
mysql_cond_destroy(&start_cond);
mysql_cond_destroy(&stop_cond);
diff --git a/sql/rpl_mi.h b/sql/rpl_mi.h
index a4a06d42a5c..44508e48c0d 100644
--- a/sql/rpl_mi.h
+++ b/sql/rpl_mi.h
@@ -227,7 +227,7 @@ class Master_info : public Slave_reporting_capability
File fd; // we keep the file open, so we need to remember the file pointer
IO_CACHE file;
- mysql_mutex_t data_lock, run_lock, sleep_lock, start_stop_lock;
+ mysql_mutex_t data_lock, run_lock, sleep_lock, start_stop_lock, start_alter_lock, start_alter_list_lock;
mysql_cond_t data_cond, start_cond, stop_cond, sleep_cond;
THD *io_thd;
MYSQL* mysql;
@@ -352,6 +352,17 @@ class Master_info : public Slave_reporting_capability
ACK from slave, or if delay_master is enabled.
*/
int semi_ack;
+ List <start_alter_info> start_alter_list;
+};
+
+struct start_alter_thd_args
+{
+ rpl_group_info *rgi;
+ LEX_CSTRING query;
+ LEX_CSTRING *db;
+ char *catalog;
+ bool shutdown;
+ CHARSET_INFO *cs;
};
int init_master_info(Master_info* mi, const char* master_info_fname,
diff --git a/sql/rpl_parallel.cc b/sql/rpl_parallel.cc
index 49ec08a9cea..a6a659cf950 100644
--- a/sql/rpl_parallel.cc
+++ b/sql/rpl_parallel.cc
@@ -1412,7 +1412,10 @@ handle_rpl_parallel_thread(void *arg)
if (end_of_group)
{
in_event_group= false;
- finish_event_group(rpt, event_gtid_sub_id, entry, rgi);
+ if (!rgi->get_finish_event_group_called())
+ finish_event_group(rpt, event_gtid_sub_id, entry, rgi);
+ else
+ rgi->set_finish_event_group_called(false);
rpt->loc_free_rgi(rgi);
thd->rgi_slave= group_rgi= rgi= NULL;
skip_event_group= false;
@@ -1690,6 +1693,9 @@ rpl_parallel_change_thread_count(rpl_parallel_thread_pool *pool,
{
mysql_mutex_lock(&pool->threads[i]->LOCK_rpl_thread);
pool->threads[i]->delay_start= false;
+ pool->threads[i]->current_start_alter_id= 0;
+ pool->threads[i]->current_start_alter_domain_id= 0;
+ pool->threads[i]->reserved_start_alter_thread= false;
mysql_cond_signal(&pool->threads[i]->COND_rpl_thread);
while (!pool->threads[i]->running)
mysql_cond_wait(&pool->threads[i]->COND_rpl_thread,
@@ -1969,6 +1975,15 @@ rpl_parallel_thread::get_rgi(Relay_log_info *rli, Gtid_log_event *gtid_ev,
rgi->retry_start_offset= rli->future_event_relay_log_pos-event_size;
rgi->retry_event_count= 0;
rgi->killed_for_retry= rpl_group_info::RETRY_KILL_NONE;
+ /* rgi is transaction specific so we need to move this value to rgi */
+ rgi->reserved_start_alter_thread= reserved_start_alter_thread;
+ rgi->rpt= this;
+ /*
+ We can remove the reserved_start_alter_thread flag.
+ If we get more concurrent alter handle_split_alter will
+ automatically set this flag again.
+ */
+ reserved_start_alter_thread= false;
return rgi;
}
@@ -2033,6 +2048,10 @@ rpl_parallel_thread::loc_free_gco(group_commit_orderer *gco)
loc_gco_list= gco;
}
+void rpl_group_info::finish_start_alter_event_group()
+{
+ finish_event_group(rpt, this->gtid_sub_id, this->parallel_entry, this);
+}
rpl_parallel_thread::rpl_parallel_thread()
: channel_name_length(0), last_error_number(0), last_error_timestamp(0),
@@ -2042,7 +2061,7 @@ rpl_parallel_thread::rpl_parallel_thread()
rpl_parallel_thread_pool::rpl_parallel_thread_pool()
- : threads(0), free_list(0), count(0), inited(false), busy(false),
+ : threads(0), free_list(0), count(0), inited(false),current_start_alters(0), busy(false),
pfs_bkp{0, false, NULL}
{
}
@@ -2175,6 +2194,129 @@ rpl_parallel_thread_pool::copy_pool_for_pfs(Relay_log_info *rli)
}
}
+/*
+ START ALTER , COMMIT ALTER / ROLLBACK ALTER scheduling
+
+ Steps:-
+ 1. (For Gtid_log_event SA). Get the worker thread which is either
+ e->rpl_threads[i] is NULL means worker from poll has not been assigned yet
+ e->rpl_threads[i]->current_owner != &e->rpl_threads[i]
+ Thread has been released, or about to //same as choose_thread logic
+ !e->rpl_threads[i]->current_start_alter_id is 0 , safe to schedule.
+ We dont want to schedule on worker which already have been scheduled SA
+ but CA/RA has not been scheduled yet. current_start_alter_id will indicate
+ this. If we dont do this we will get deadlock.
+ 2. (For Gtid_log_event SA)
+ call choose_thread_internal so that e->rpl_threads[idx] is not null
+ update the current_start_alter_id
+ 3. (For Gtid_log_event SA)
+ update local e->pending_start_alters(local) variable and
+ pool->current_start_alters(global)
+ We need 2 status variable (global and local) because we can have
+ slave_domain_parallel_threads != pool->threads.
+ 4. (For CA/RA Gtid_log_event)
+ Update e->pending_start_alters and pool->current_start_alters
+ while holding mutex lock on pool (if SA is not assigned to
+ reserved thread)
+
+
+ @returns
+ true Worker allocated (choose_thread_internal called)
+ false Worker not allocated (choose_thread_internal not called)
+*/
+static bool handle_split_alter(rpl_parallel_entry *e,
+ Gtid_log_event *gtid_ev, uint32 *idx,
+ //choose_thread_internal specific
+ bool *did_enter_cond, rpl_group_info* rgi,
+ PSI_stage_info *old_stage)
+{
+ uint16 flags_extra= gtid_ev->flags_extra;
+ bool thread_allocated= false;
+ //Step 1
+ if (flags_extra & Log_event::FL_START_ALTER_E1 ||
+ //This will arrange finding threads for CA/RA as well
+ //as concurrent DDL
+ e->pending_start_alters)
+ {
+ /*
+ j is needed for round robin scheduling, we will start with rpl_thread_idx
+ go till rpl_thread_max and then start with 0 to rpl_thread_idx
+ */
+ int j= e->rpl_thread_idx;
+ for(uint i= 0; i < e->rpl_thread_max; i++)
+ {
+ if (!e->rpl_threads[j] || e->rpl_threads[j]->current_owner
+ != &e->rpl_threads[j] || !e->rpl_threads[j]->current_start_alter_id)
+ {
+ //This condition will hit atleast one time no matter what happens
+ *idx= j;
+ DBUG_PRINT("info", ("Start alter id %d", j));
+ goto idx_found;
+ }
+ j++;
+ j= j % e->rpl_thread_max;
+ }
+ //We did not find and idx
+ DBUG_ASSERT(0);
+ return false;
+idx_found:
+ e->rpl_thread_idx= *idx;
+ e->choose_thread_internal(*idx, did_enter_cond, rgi, old_stage);
+ thread_allocated= true;
+ if (flags_extra & Log_event::FL_START_ALTER_E1)
+ {
+ mysql_mutex_assert_owner(&e->rpl_threads[*idx]->LOCK_rpl_thread);
+ e->rpl_threads[e->rpl_thread_idx]->current_start_alter_id= gtid_ev->seq_no;
+ e->rpl_threads[e->rpl_thread_idx]->current_start_alter_domain_id=
+ gtid_ev->domain_id;
+ /*
+ We are locking LOCK_rpl_thread_pool becuase we are going to update
+ current_start_alters
+ */
+ mysql_mutex_lock(&global_rpl_thread_pool.LOCK_rpl_thread_pool);
+ if (e->pending_start_alters < e->rpl_thread_max - 1 &&
+ global_rpl_thread_pool.current_start_alters
+ < global_rpl_thread_pool.count - 1)
+ {
+ e->pending_start_alters++;
+ global_rpl_thread_pool.current_start_alters++;
+ }
+ else
+ {
+ e->rpl_threads[*idx]->reserved_start_alter_thread= true;
+ e->rpl_threads[*idx]->current_start_alter_id= 0;
+ e->rpl_threads[*idx]->current_start_alter_domain_id= 0;
+ }
+ mysql_mutex_unlock(&global_rpl_thread_pool.LOCK_rpl_thread_pool);
+ }
+ }
+ if(flags_extra & (Log_event::FL_COMMIT_ALTER_E1 |
+ Log_event::FL_ROLLBACK_ALTER_E1 ))
+ {
+ //Free the corrosponding rpt current_start_alter_id
+ for(uint i= 0; i < e->rpl_thread_max; i++)
+ {
+ if(e->rpl_threads[i] &&
+ e->rpl_threads[i]->current_start_alter_id == gtid_ev->sa_seq_no &&
+ e->rpl_threads[i]->current_start_alter_domain_id == gtid_ev->domain_id)
+ {
+ mysql_mutex_lock(&global_rpl_thread_pool.LOCK_rpl_thread_pool);
+ e->rpl_threads[i]->current_start_alter_id= 0;
+ e->rpl_threads[i]->current_start_alter_domain_id= 0;
+ global_rpl_thread_pool.current_start_alters--;
+ e->pending_start_alters--;
+ DBUG_PRINT("info", ("Commit/Rollback alter id %d", i));
+ mysql_mutex_unlock(&global_rpl_thread_pool.LOCK_rpl_thread_pool);
+ break;
+ }
+ }
+ }
+
+ return thread_allocated;
+
+}
+
+
/*
Obtain a worker thread that we can queue an event to.
@@ -2208,25 +2350,32 @@ rpl_parallel_entry::choose_thread(rpl_group_info *rgi, bool *did_enter_cond,
Gtid_log_event *gtid_ev)
{
uint32 idx;
- Relay_log_info *rli= rgi->rli;
- rpl_parallel_thread *thr;
idx= rpl_thread_idx;
if (gtid_ev)
{
+ if (++idx >= rpl_thread_max)
+ idx= 0;
+ //rpl_thread_idx will be updated handle_split_alter
+ if (handle_split_alter(this, gtid_ev, &idx, did_enter_cond, rgi, old_stage))
+ return rpl_threads[idx];
if (gtid_ev->flags2 &
(Gtid_log_event::FL_COMPLETED_XA | Gtid_log_event::FL_PREPARED_XA))
+ {
idx= my_hash_sort(&my_charset_bin, gtid_ev->xid.key(),
gtid_ev->xid.key_length()) % rpl_thread_max;
- else
- {
- ++idx;
- if (idx >= rpl_thread_max)
- idx= 0;
}
rpl_thread_idx= idx;
}
- thr= rpl_threads[idx];
+ return choose_thread_internal(idx, did_enter_cond, rgi, old_stage);
+}
+
+rpl_parallel_thread * rpl_parallel_entry::choose_thread_internal(uint idx,
+ bool *did_enter_cond, rpl_group_info *rgi,
+ PSI_stage_info *old_stage)
+{
+ rpl_parallel_thread* thr= rpl_threads[idx];
+ Relay_log_info *rli= rgi->rli;
if (thr)
{
*did_enter_cond= false;
@@ -2366,6 +2515,7 @@ rpl_parallel::find(uint32 domain_id)
e->domain_id= domain_id;
e->stop_on_error_sub_id= (uint64)ULONGLONG_MAX;
e->pause_sub_id= (uint64)ULONGLONG_MAX;
+ e->pending_start_alters= 0;
if (my_hash_insert(&domain_hash, (uchar *)e))
{
my_free(e);
@@ -2451,6 +2601,14 @@ rpl_parallel::wait_for_done(THD *thd, Relay_log_info *rli)
{
if ((rpt= e->rpl_threads[j]))
{
+ /*
+ Dont wait for SA workers , But wait for CA/RA workers
+ If CA/RA is executed that means corresponding SA is also executed
+ And remaning SA will never recieve CA/RA so we have to manualy send it
+ */
+ if (rpt->thd->rgi_slave &&
+ (rpt->thd->rgi_slave->gtid_ev_flags_extra & Log_event::FL_START_ALTER_E1))
+ continue;
mysql_mutex_lock(&rpt->LOCK_rpl_thread);
while (rpt->current_owner == &e->rpl_threads[j])
mysql_cond_wait(&rpt->COND_rpl_thread_stop, &rpt->LOCK_rpl_thread);
@@ -2814,6 +2972,7 @@ rpl_parallel::do_event(rpl_group_info *serial_rgi, Log_event *ev,
gtid.server_id= gtid_ev->server_id;
gtid.seq_no= gtid_ev->seq_no;
rli->update_relay_log_state(&gtid, 1);
+ serial_rgi->gtid_ev_flags_extra= gtid_ev->flags_extra;
if (process_gtid_for_restart_pos(rli, &gtid))
{
/*
diff --git a/sql/rpl_parallel.h b/sql/rpl_parallel.h
index d3c46301ff8..e54fd2abb48 100644
--- a/sql/rpl_parallel.h
+++ b/sql/rpl_parallel.h
@@ -99,6 +99,18 @@ struct rpl_parallel_thread {
bool running;
bool stop;
bool pause_for_ftwrl;
+ /*
+ 0 = No start alter assigned
+ >0 = Start alter assigned
+ */
+ uint64 current_start_alter_id;
+ uint32 current_start_alter_domain_id;
+ /*
+ This flag is true when Start Alter just needs to be binlogged only.
+ This scenario will happens when there is congestion , and we can not
+ allocate independent worker to start alter.
+ */
+ bool reserved_start_alter_thread;
mysql_mutex_t LOCK_rpl_thread;
mysql_cond_t COND_rpl_thread;
mysql_cond_t COND_rpl_thread_queue;
@@ -297,6 +309,12 @@ struct rpl_parallel_thread_pool {
mysql_cond_t COND_rpl_thread_pool;
uint32 count;
bool inited;
+
+ /*
+ Lock first LOCK_rpl_thread_pool and then LOCK_rpl_thread to
+ update this variable.
+ */
+ uint32 current_start_alters;
/*
While FTWRL runs, this counter is incremented to make SQL thread or
STOP/START slave not try to start new activity while that operation
@@ -328,6 +346,7 @@ struct rpl_parallel_entry {
*/
uint32 need_sub_id_signal;
uint64 last_commit_id;
+ uint32 pending_start_alters;
bool active;
/*
Set when SQL thread is shutting down, and no more events can be processed,
@@ -414,6 +433,9 @@ struct rpl_parallel_entry {
rpl_parallel_thread * choose_thread(rpl_group_info *rgi, bool *did_enter_cond,
PSI_stage_info *old_stage,
Gtid_log_event *gtid_ev);
+ rpl_parallel_thread *
+ choose_thread_internal(uint idx, bool *did_enter_cond, rpl_group_info *rgi,
+ PSI_stage_info *old_stage);
int queue_master_restart(rpl_group_info *rgi,
Format_description_log_event *fdev);
};
diff --git a/sql/rpl_rli.cc b/sql/rpl_rli.cc
index 4ba843a51ab..79d1dff2fb9 100644
--- a/sql/rpl_rli.cc
+++ b/sql/rpl_rli.cc
@@ -2152,16 +2152,24 @@ rpl_group_info::reinit(Relay_log_info *rli)
long_find_row_note_printed= false;
did_mark_start_commit= false;
gtid_ev_flags2= 0;
+ gtid_ev_flags_extra= 0;
+ gtid_ev_sa_seq_no= 0;
last_master_timestamp = 0;
gtid_ignore_duplicate_state= GTID_DUPLICATE_NULL;
speculation= SPECULATE_NO;
+ rpt= NULL;
+ start_alter_ev= NULL;
+ direct_commit_alter= false;
commit_orderer.reinit();
}
rpl_group_info::rpl_group_info(Relay_log_info *rli)
: thd(0), wait_commit_sub_id(0),
wait_commit_group_info(0), parallel_entry(0),
- deferred_events(NULL), m_annotate_event(0), is_parallel_exec(false)
+ deferred_events(NULL), m_annotate_event(0), is_parallel_exec(false),
+ gtid_ev_flags2(0), gtid_ev_flags_extra(0), gtid_ev_sa_seq_no(0),
+ reserved_start_alter_thread(0), finish_event_group_called(0), rpt(NULL),
+ start_alter_ev(NULL), direct_commit_alter(false), sa_info(NULL)
{
reinit(rli);
bzero(&current_gtid, sizeof(current_gtid));
@@ -2170,7 +2178,6 @@ rpl_group_info::rpl_group_info(Relay_log_info *rli)
mysql_cond_init(key_rpl_group_info_sleep_cond, &sleep_cond, NULL);
}
-
rpl_group_info::~rpl_group_info()
{
free_annotate_event();
diff --git a/sql/rpl_rli.h b/sql/rpl_rli.h
index cc807852bf2..d3ccbfd4a27 100644
--- a/sql/rpl_rli.h
+++ b/sql/rpl_rli.h
@@ -641,6 +641,33 @@ struct inuse_relaylog {
}
};
+enum start_alter_state
+{
+ INVALID= 0,
+ REGISTERED, // Start Alter exist, Default state
+ COMMIT_ALTER, // COMMIT the alter
+ ROLLBACK_ALTER, // Rollback the alter
+ COMPLETED // COMMIT/ROLLBACK Alter written in binlog
+};
+
+struct start_alter_info
+{
+ /*
+ Unique among replication channel at one point of time
+ */
+ uint64 sa_seq_no; //key for searching
+ uint32 domain_id;
+ /*
+ 0 prepared and not error from commit and rollback
+ >0 error expected in commit/rollback
+ Rollback can be logged with 0 error if master is killed
+ */
+ uint error;
+ const char *err_msg;
+ enum start_alter_state state;
+ /* We are not using mysql_cond_t because we do not need PSI */
+ mysql_cond_t start_alter_cond;
+};
/*
This is data for various state needed to be kept for the processing of
@@ -760,6 +787,9 @@ struct rpl_group_info
bool did_mark_start_commit;
/* Copy of flags2 from GTID event. */
uchar gtid_ev_flags2;
+ /* Copy of flags3 from GTID event. */
+ uint16 gtid_ev_flags_extra;
+ uint64 gtid_ev_sa_seq_no;
enum {
GTID_DUPLICATE_NULL=0,
GTID_DUPLICATE_IGNORE=1,
@@ -834,6 +864,15 @@ struct rpl_group_info
RETRY_KILL_KILLED
};
uchar killed_for_retry;
+ bool reserved_start_alter_thread;
+ bool finish_event_group_called;
+ /*
+ Used for two phase alter table
+ */
+ rpl_parallel_thread *rpt;
+ Query_log_event *start_alter_ev;
+ bool direct_commit_alter;
+ start_alter_info *sa_info;
rpl_group_info(Relay_log_info *rli_);
~rpl_group_info();
@@ -961,6 +1000,19 @@ struct rpl_group_info
if (!is_parallel_exec)
rli->event_relay_log_pos= future_event_relay_log_pos;
}
+
+ void finish_start_alter_event_group();
+
+ bool get_finish_event_group_called()
+ {
+ return finish_event_group_called;
+ }
+
+ void set_finish_event_group_called(bool value)
+ {
+ finish_event_group_called= value;
+ }
+
};
diff --git a/sql/share/errmsg-utf8.txt b/sql/share/errmsg-utf8.txt
index f31f21faeb0..601ab52e7c3 100644
--- a/sql/share/errmsg-utf8.txt
+++ b/sql/share/errmsg-utf8.txt
@@ -7996,3 +7996,5 @@ ER_PARTITION_CONVERT_SUBPARTITIONED
eng "Convert partition is not supported for subpartitioned table."
ER_PROVIDER_NOT_LOADED
eng "MariaDB tried to use the %s, but its provider plugin is not loaded"
+ER_MANUAL_SPLIT_ALTER
+ eng "Manual splitting of ALTER is not allowed"
diff --git a/sql/slave.cc b/sql/slave.cc
index 3c5b830fbe2..5e05ceff198 100644
--- a/sql/slave.cc
+++ b/sql/slave.cc
@@ -3930,6 +3930,10 @@ apply_event_and_update_pos_apply(Log_event* ev, THD* thd, rpl_group_info *rgi,
DBUG_PRINT("info", ("apply_event error = %d", exec_res));
if (exec_res == 0)
{
+ if (thd->rgi_slave && (thd->rgi_slave->gtid_ev_flags_extra &
+ Log_event::FL_START_ALTER_E1) &&
+ thd->rgi_slave->get_finish_event_group_called())
+ DBUG_RETURN(exec_res ? 1 : 0);
int error= ev->update_pos(rgi);
#ifndef DBUG_OFF
DBUG_PRINT("info", ("update_pos error = %d", error));
@@ -5570,8 +5574,32 @@ pthread_handler_t handle_slave_sql(void *arg)
err:
if (mi->using_parallel())
+ {
rli->parallel.wait_for_done(thd, rli);
- /* Gtid_list_log_event::do_apply_event has already reported the GTID until */
+ /*
+ shutdown the loner alter threads waiting on C/R ALter
+ */
+ start_alter_info *info=NULL;
+ mysql_mutex_lock(&mi->start_alter_list_lock);
+ List_iterator<start_alter_info> info_iterator(mi->start_alter_list);
+ while ((info= info_iterator++))
+ {
+ mysql_mutex_lock(&mi->start_alter_lock);
+ info->state= start_alter_state::ROLLBACK_ALTER;
+ mysql_cond_broadcast(&info->start_alter_cond);
+ mysql_mutex_unlock(&mi->start_alter_lock);
+ mysql_mutex_lock(&mi->start_alter_lock);
+ while(info->state == start_alter_state::ROLLBACK_ALTER)
+ mysql_cond_wait(&info->start_alter_cond, &mi->start_alter_lock);
+ mysql_mutex_unlock(&mi->start_alter_lock);
+ DBUG_ASSERT(info->state == start_alter_state::COMPLETED);
+ info_iterator.remove();
+ mysql_cond_destroy(&info->start_alter_cond);
+ my_free(info);
+ }
+ mysql_mutex_unlock(&mi->start_alter_list_lock);
+ };
+ /* Gtid_list_log_event::do_apply_event has already reported the GTID until */
if (rli->stop_for_until && rli->until_condition != Relay_log_info::UNTIL_GTID)
{
if (global_system_variables.log_warnings > 2)
@@ -5692,7 +5720,7 @@ err_during_init:
/* Forget the relay log's format */
delete rli->relay_log.description_event_for_exec;
rli->relay_log.description_event_for_exec= 0;
- rli->reset_inuse_relaylog();
+ //rli->reset_inuse_relaylog();
/* Wake up master_pos_wait() */
mysql_mutex_unlock(&rli->data_lock);
DBUG_PRINT("info",("Signaling possibly waiting master_pos_wait() functions"));
@@ -6294,12 +6322,19 @@ static int queue_event(Master_info* mi, const uchar *buf, ulong event_len)
Rotate_log_event rev(buf, checksum_alg != BINLOG_CHECKSUM_ALG_OFF ?
event_len - BINLOG_CHECKSUM_LEN : event_len,
mi->rli.relay_log.description_event_for_queue);
-
- if (unlikely(mi->gtid_reconnect_event_skip_count) &&
- unlikely(!mi->gtid_event_seen) &&
- rev.is_artificial_event() &&
- (mi->prev_master_id != mi->master_id ||
- strcmp(rev.new_log_ident, mi->master_log_name) != 0))
+ bool master_changed= false;
+ bool maybe_crashed= false;
+ // Exclude server start scenario
+ if ((mi->prev_master_id && mi->master_id) &&
+ (mi->prev_master_id != mi->master_id))
+ master_changed= true;
+ if ((mi->master_log_name[0]!='\0') &&
+ (strcmp(rev.new_log_ident, mi->master_log_name) != 0))
+ maybe_crashed= true;
+
+ if (unlikely((mi->gtid_reconnect_event_skip_count && master_changed) ||
+ maybe_crashed) &&
+ unlikely(!mi->gtid_event_seen) && rev.is_artificial_event())
{
/*
Artificial Rotate_log_event is the first event we receive at the start
@@ -6335,26 +6370,37 @@ static int queue_event(Master_info* mi, const uchar *buf, ulong event_len)
case likewise rollback the partially received event group.
*/
Format_description_log_event fdle(4);
+ fdle.checksum_alg= checksum_alg;
+
+ /*
+ Possible crash is flagged in being created FD' common header
+ to conduct any necessary cleanup by the slave applier.
+ */
+ if (maybe_crashed)
+ fdle.flags |= LOG_EVENT_BINLOG_IN_USE_F;
- if (mi->prev_master_id != mi->master_id)
- sql_print_warning("The server_id of master server changed in the "
- "middle of GTID %u-%u-%llu. Assuming a change of "
- "master server, so rolling back the previously "
- "received partial transaction. Expected: %lu, "
- "received: %lu", mi->last_queued_gtid.domain_id,
- mi->last_queued_gtid.server_id,
- mi->last_queued_gtid.seq_no,
- mi->prev_master_id, mi->master_id);
- else if (strcmp(rev.new_log_ident, mi->master_log_name) != 0)
- sql_print_warning("Unexpected change of master binlog file name in the "
- "middle of GTID %u-%u-%llu, assuming that master has "
- "crashed and rolling back the transaction. Expected: "
- "'%s', received: '%s'",
- mi->last_queued_gtid.domain_id,
- mi->last_queued_gtid.server_id,
- mi->last_queued_gtid.seq_no,
- mi->master_log_name, rev.new_log_ident);
+ if (mi->gtid_reconnect_event_skip_count)
+ {
+ if (master_changed)
+ sql_print_warning("The server_id of master server changed in the "
+ "middle of GTID %u-%u-%llu. Assuming a change of "
+ "master server, so rolling back the previously "
+ "received partial transaction. Expected: %lu, "
+ "received: %lu", mi->last_queued_gtid.domain_id,
+ mi->last_queued_gtid.server_id,
+ mi->last_queued_gtid.seq_no,
+ mi->prev_master_id, mi->master_id);
+ else
+ sql_print_warning("Unexpected change of master binlog file name in "
+ "the middle of GTID %u-%u-%llu, assuming that "
+ "master has crashed and rolling back the "
+ "transaction. Expected: '%s', received: '%s'",
+ mi->last_queued_gtid.domain_id,
+ mi->last_queued_gtid.server_id,
+ mi->last_queued_gtid.seq_no, mi->master_log_name,
+ rev.new_log_ident);
+ }
mysql_mutex_lock(log_lock);
if (likely(!rli->relay_log.write_event(&fdle) &&
!rli->relay_log.flush_and_sync(NULL)))
diff --git a/sql/sql_alter.cc b/sql/sql_alter.cc
index 58902fc859f..242bd774804 100644
--- a/sql/sql_alter.cc
+++ b/sql/sql_alter.cc
@@ -19,6 +19,9 @@
#include "sql_table.h" // mysql_alter_table,
// mysql_exchange_partition
#include "sql_alter.h"
+#include "rpl_mi.h"
+#include "slave.h"
+#include "debug_sync.h"
#include "wsrep_mysqld.h"
Alter_info::Alter_info(const Alter_info &rhs, MEM_ROOT *mem_root)
diff --git a/sql/sql_binlog.cc b/sql/sql_binlog.cc
index bab2afb957a..a39b5b22bb6 100644
--- a/sql/sql_binlog.cc
+++ b/sql/sql_binlog.cc
@@ -20,6 +20,7 @@
#include "sql_parse.h"
#include "sql_acl.h"
#include "rpl_rli.h"
+#include "rpl_mi.h"
#include "slave.h"
#include "log_event.h"
@@ -70,7 +71,8 @@ static int check_event_type(int type, Relay_log_info *rli)
/* It is always allowed to execute FD events. */
return 0;
-
+
+ case QUERY_EVENT:
case TABLE_MAP_EVENT:
case WRITE_ROWS_EVENT_V1:
case UPDATE_ROWS_EVENT_V1:
@@ -213,13 +215,20 @@ void mysql_client_binlog_statement(THD* thd)
rli= thd->rli_fake;
if (!rli && (rli= thd->rli_fake= new Relay_log_info(FALSE, "BINLOG_BASE64_EVENT")))
rli->sql_driver_thd= thd;
+ static LEX_CSTRING connection_name= { STRING_WITH_LEN("BINLOG_BASE64_EVENT") };
+ rli->mi= new Master_info(&connection_name, false);
if (!(rgi= thd->rgi_fake))
rgi= thd->rgi_fake= new rpl_group_info(rli);
rgi->thd= thd;
-
+ thd->system_thread_info.rpl_sql_info=
+ new rpl_sql_thread_info(rli->mi->rpl_filter);
const char *error= 0;
Log_event *ev = 0;
my_bool is_fragmented= FALSE;
+ sql_digest_state *m_digest;
+ PSI_statement_locker *m_statement_psi;
+ LEX_CSTRING save_db;
+ my_thread_id thread_id= 0;
/*
Out of memory check
@@ -373,7 +382,30 @@ void mysql_client_binlog_statement(THD* thd)
LEX *backup_lex;
thd->backup_and_reset_current_lex(&backup_lex);
+ if (ev->get_type_code() == QUERY_EVENT)
+ {
+ m_digest= thd->m_digest;
+ m_statement_psi= thd->m_statement_psi;
+ save_db.str= my_strndup(key_memory_THD_db, thd->db.str,
+ thd->db.length, MYF(MY_WME));
+ save_db.length= thd->db.length;
+ if (save_db.str == NULL)
+ {
+ my_error(ER_OUT_OF_RESOURCES, MYF(0));
+ goto end;
+ }
+ thd->m_digest= NULL;
+ thd->m_statement_psi= NULL;
+ thread_id= thd->variables.pseudo_thread_id;
+ }
err= ev->apply_event(rgi);
+ if (ev->get_type_code() == QUERY_EVENT)
+ {
+ thd->m_digest= m_digest;
+ thd->m_statement_psi= m_statement_psi;
+ thd->reset_db(&save_db);
+ thd->variables.pseudo_thread_id= thread_id;
+ }
thd->restore_current_lex(backup_lex);
}
thd->variables.option_bits=
@@ -413,5 +445,9 @@ end:
thd->variables.option_bits= thd_options;
rgi->slave_close_thread_tables(thd);
my_free(buf);
+ delete rli->mi;
+ delete thd->system_thread_info.rpl_sql_info;
+ delete rgi;
+ rgi= thd->rgi_fake= NULL;
DBUG_VOID_RETURN;
}
diff --git a/sql/sql_class.h b/sql/sql_class.h
index 0d02f74686f..8e54f938574 100644
--- a/sql/sql_class.h
+++ b/sql/sql_class.h
@@ -81,6 +81,7 @@ class Wsrep_applier_service;
class Reprepare_observer;
class Relay_log_info;
struct rpl_group_info;
+struct rpl_parallel_thread;
class Rpl_filter;
class Query_log_event;
class Load_log_event;
@@ -853,6 +854,7 @@ typedef struct system_variables
vers_asof_timestamp_t vers_asof_timestamp;
ulong vers_alter_history;
+ ulong binlog_alter_two_phase;
} SV;
/**
@@ -2996,6 +2998,11 @@ public:
}
bool binlog_table_should_be_logged(const LEX_CSTRING *db);
+ // Accessors and setters of two-phase loggable ALTER binlog properties
+ uint16 get_binlog_flags_for_alter();
+ void set_binlog_flags_for_alter(uint16);
+ uint64 get_binlog_start_alter_seq_no();
+ void set_binlog_start_alter_seq_no(uint64);
#endif /* MYSQL_CLIENT */
public:
@@ -7776,5 +7783,30 @@ extern THD_list server_threads;
void setup_tmp_table_column_bitmaps(TABLE *table, uchar *bitmaps,
uint field_count);
+/*
+ RAII utility class to ease binlogging with temporary setting
+ THD etc context and restoring the original one upon logger execution.
+*/
+class Write_log_with_flags
+{
+ THD* m_thd;
+
+public:
+~Write_log_with_flags()
+ {
+ m_thd->set_binlog_flags_for_alter(0);
+ m_thd->set_binlog_start_alter_seq_no(0);
+ }
+
+ Write_log_with_flags(THD *thd, uint16 flags, uint64 seq_no= 0) : m_thd(thd)
+ {
+ m_thd->set_binlog_flags_for_alter(flags);
+ if (seq_no == 0)
+ seq_no= m_thd->get_binlog_start_alter_seq_no();
+ if (seq_no != 0)
+ m_thd->set_binlog_start_alter_seq_no(seq_no);
+ }
+};
+
#endif /* MYSQL_SERVER */
#endif /* SQL_CLASS_INCLUDED */
diff --git a/sql/sql_table.cc b/sql/sql_table.cc
index 7a7a56c1336..c1dc869cc5e 100644
--- a/sql/sql_table.cc
+++ b/sql/sql_table.cc
@@ -19,6 +19,7 @@
/* drop and alter of tables */
#include "mariadb.h"
+#include "sql_class.h"
#include "sql_priv.h"
#include "unireg.h"
#include "debug_sync.h"
@@ -58,6 +59,9 @@
#include "ddl_log.h"
#include "debug.h" // debug_crash_here()
#include <algorithm>
+#include "rpl_mi.h"
+#include "log.h"
+
#ifdef _WIN32
#include <io.h>
@@ -89,6 +93,12 @@ static int mysql_prepare_create_table(THD *, HA_CREATE_INFO *, Alter_info *,
static uint blob_length_by_type(enum_field_types type);
static bool fix_constraints_names(THD *, List<Virtual_column_info> *,
const HA_CREATE_INFO *);
+bool write_bin_log_start_alter(THD *thd, bool& partial_alter,
+ uint64 start_alter_id, bool log_if_exists,
+ MEM_ROOT *mem);
+static bool wait_for_master(THD *thd);
+static int process_master_state(THD *thd, int alter_result,
+ uint64 &start_alter_id);
/**
@brief Helper function for explain_filename
@@ -984,7 +994,8 @@ end:
*/
int write_bin_log(THD *thd, bool clear_error,
- char const *query, ulong query_length, bool is_trans)
+ char const *query, ulong query_length, bool is_trans,
+ bool log_zero_errcode)
{
int error= 0;
if (mysql_bin_log.is_open())
@@ -997,7 +1008,7 @@ int write_bin_log(THD *thd, bool clear_error,
errcode= query_error_code(thd, TRUE);
error= thd->binlog_query(THD::STMT_QUERY_TYPE,
query, query_length, is_trans, FALSE, FALSE,
- errcode) > 0;
+ log_zero_errcode? 0 : errcode) > 0;
thd_proc_info(thd, 0);
}
return error;
@@ -1011,14 +1022,23 @@ int write_bin_log(THD *thd, bool clear_error,
*/
int write_bin_log_with_if_exists(THD *thd, bool clear_error,
- bool is_trans, bool add_if_exists)
+ bool is_trans, bool add_if_exists,
+ bool commit_alter)
{
int result;
ulonglong save_option_bits= thd->variables.option_bits;
if (add_if_exists)
thd->variables.option_bits|= OPTION_IF_EXISTS;
+ if (commit_alter)
+ thd->set_binlog_flags_for_alter(Log_event::FL_COMMIT_ALTER_E1);
+
result= write_bin_log(thd, clear_error, thd->query(), thd->query_length(),
is_trans);
+ if (commit_alter)
+ {
+ thd->set_binlog_flags_for_alter(0);
+ thd->set_binlog_start_alter_seq_no(0);
+ }
thd->variables.option_bits= save_option_bits;
return result;
}
@@ -7283,6 +7303,51 @@ static bool notify_tabledef_changed(TABLE_LIST *table_list)
DBUG_RETURN(0);
}
+/*
+ The function is invoked in error branches of ALTER processing.
+ Write Rollback alter in case of partial_alter is true else
+ call process_master_state.
+
+ Returns
+ false on Success
+ true otherwise
+*/
+bool write_bin_log_start_alter_rollback(THD *thd, uint64 &start_alter_id,
+ bool &partial_alter, bool if_exists)
+{
+ if (start_alter_id)
+ {
+ start_alter_info *info= thd->rgi_slave->sa_info;
+ if (info->sa_seq_no == 0)
+ {
+ /*
+ Error occurred before SA got to processing incl its binlogging.
+ So it's a failure to apply and thus no need to wait for master's
+ completion result.
+ */
+ return true;
+ }
+ /*
+ We have to call wait for master here because in main calculation
+ we can error out before calling wait for master
+ (for example if copy_data_between_tables fails)
+ */
+ if (info->state == start_alter_state::REGISTERED)
+ wait_for_master(thd);
+ if(process_master_state(thd, 1, start_alter_id))
+ return true;
+ }
+ else if (partial_alter) // Write only if SA written
+ {
+ // Send the rollback message
+ Write_log_with_flags wlwf(thd, Log_event::FL_ROLLBACK_ALTER_E1);
+ if(write_bin_log_with_if_exists(thd, false, false, if_exists, false))
+ return true;
+ partial_alter= false;
+ }
+ return false;
+}
+
/**
Perform in-place alter table.
@@ -7297,8 +7362,9 @@ static bool notify_tabledef_changed(TABLE_LIST *table_list)
@param target_mdl_request Metadata request/lock on the target table name.
@param alter_ctx ALTER TABLE runtime context.
- @retval true Error
- @retval false Success
+ @retval >=1 Error{ 1= ROLLBACK recieved from master , 2= error
+ in alter so no ROLLBACK in binlog }
+ @retval 0 Success
@note
If mysql_alter_table does not need to copy the table, it is
@@ -7321,13 +7387,16 @@ static bool mysql_inplace_alter_table(THD *thd,
MDL_request *target_mdl_request,
DDL_LOG_STATE *ddl_log_state,
TRIGGER_RENAME_PARAM *trigger_param,
- Alter_table_ctx *alter_ctx)
+ Alter_table_ctx *alter_ctx,
+ bool &partial_alter,
+ uint64 &start_alter_id, bool if_exists)
{
Open_table_context ot_ctx(thd, MYSQL_OPEN_REOPEN | MYSQL_OPEN_IGNORE_KILLED);
handlerton *db_type= table->s->db_type();
Alter_info *alter_info= ha_alter_info->alter_info;
bool reopen_tables= false;
bool res, commit_succeded_with_error= 0;
+
const enum_alter_inplace_result inplace_supported=
ha_alter_info->inplace_supported;
DBUG_ENTER("mysql_inplace_alter_table");
@@ -7406,11 +7475,22 @@ static bool mysql_inplace_alter_table(THD *thd,
MDL_SHARED_NO_WRITE,
thd->variables.lock_wait_timeout))
goto cleanup;
+ // If alter_algorithm == Instant.
+ if ((table->s->tmp_table == NO_TMP_TABLE || table->s->table_creation_was_logged
+ || start_alter_id)
+ && alter_info->algorithm(thd) != Alter_info::ALTER_TABLE_ALGORITHM_INSTANT)
+ if (write_bin_log_start_alter(thd, partial_alter, start_alter_id,
+ if_exists, &table->s->mem_root))
+ goto cleanup;
+
+ DBUG_EXECUTE_IF("start_alter_kill_after_binlog", {
+ DBUG_SUICIDE();
+ });
+
// It's now safe to take the table level lock.
if (lock_tables(thd, table_list, alter_ctx->tables_opened, 0))
goto cleanup;
-
DEBUG_SYNC(thd, "alter_table_inplace_after_lock_upgrade");
THD_STAGE_INFO(thd, stage_alter_inplace_prepare);
@@ -7489,14 +7569,23 @@ static bool mysql_inplace_alter_table(THD *thd,
DEBUG_SYNC(thd, "alter_table_inplace_after_lock_downgrade");
THD_STAGE_INFO(thd, stage_alter_inplace);
+ DBUG_EXECUTE_IF("start_alter_delay_master", {
+ debug_sync_set_action(thd,
+ STRING_WITH_LEN("now wait_for alter_cont"));
+ });
/* We can abort alter table for any table type */
thd->abort_on_warning= !ha_alter_info->ignore && thd->is_strict_mode();
res= table->file->ha_inplace_alter_table(altered_table, ha_alter_info);
thd->abort_on_warning= false;
+
+ if (start_alter_id && wait_for_master(thd))
+ goto rollback;
+
if (res)
goto rollback;
+
DEBUG_SYNC(thd, "alter_table_inplace_before_lock_upgrade");
// Upgrade to EXCLUSIVE before commit.
if (wait_while_table_is_used(thd, table, HA_EXTRA_PREPARE_FOR_RENAME))
@@ -7605,7 +7694,7 @@ static bool mysql_inplace_alter_table(THD *thd,
thd->is_error())
{
// Since changes were done in-place, we can't revert them.
- DBUG_RETURN(true);
+ goto err;
}
debug_crash_here("ddl_log_alter_after_rename_frm");
@@ -7622,7 +7711,7 @@ static bool mysql_inplace_alter_table(THD *thd,
If the rename fails we will still have a working table
with the old name, but with other changes applied.
*/
- DBUG_RETURN(true);
+ goto err;
}
debug_crash_here("ddl_log_alter_before_rename_triggers");
if (Table_triggers_list::change_table_name(thd, trigger_param,
@@ -7667,6 +7756,12 @@ static bool mysql_inplace_alter_table(THD *thd,
if (thd->locked_tables_list.reopen_tables(thd, false))
thd->locked_tables_list.unlink_all_closed_tables(thd, NULL, 0);
}
+
+err:
+ if (partial_alter || start_alter_id)
+ write_bin_log_start_alter_rollback(thd, start_alter_id, partial_alter,
+ if_exists);
+
DBUG_RETURN(true);
}
@@ -9367,6 +9462,124 @@ static bool log_and_ok(THD *thd)
return(0);
}
+/*
+ Wait for master to send result of Alter table.
+ Returns
+ true when Rollback is decided
+ false otherwise
+*/
+static bool wait_for_master(THD *thd)
+{
+ start_alter_info* info= thd->rgi_slave->sa_info;
+ Master_info *mi= thd->rgi_slave->rli->mi;
+
+ DBUG_ASSERT(info);
+ DBUG_ASSERT(info->state != start_alter_state::INVALID);
+ DBUG_ASSERT(mi);
+
+ mysql_mutex_lock(&mi->start_alter_lock);
+ while (info->state == start_alter_state::REGISTERED)
+ {
+ mysql_cond_wait(&info->start_alter_cond, &mi->start_alter_lock);
+ }
+ if (info->state == start_alter_state::ROLLBACK_ALTER)
+ {
+ /*
+ SA thread will not give error , We will set the error in info->error
+ and then RA worker will output the error
+ We can modify the info->error without taking mutex, because CA worker
+ will be waiting on ::COMPLETED wait condition
+ */
+ if(thd->is_error())
+ {
+ info->error= thd->get_stmt_da()->sql_errno();
+ info->err_msg= thd->get_stmt_da()->message();
+ }
+ thd->clear_error();
+ }
+ mysql_mutex_unlock(&mi->start_alter_lock);
+
+ return info->state == start_alter_state::ROLLBACK_ALTER;
+}
+
+/*
+ In this function, we are going to change info->state to ::COMPLETED.
+ This means we are messaging CA/RA worker that we have binlogged, so our
+ here is finished.
+*/
+static void alter_committed(THD *thd, start_alter_info* info, Master_info *mi)
+{
+ start_alter_state tmp= info->state;
+ mysql_mutex_lock(&mi->start_alter_lock);
+ info->state= start_alter_state::COMPLETED;
+ mysql_cond_broadcast(&info->start_alter_cond);
+ mysql_mutex_unlock(&mi->start_alter_lock);
+ if (tmp == start_alter_state::ROLLBACK_ALTER)
+ thd->clear_error();
+}
+
+/*
+ process_master_state:- process the info->state recieved from master
+ We will comapre master state with alter_result
+ In the case of ROLLBACK_ALTER alter_result > 0
+ In the case of COMMIT_ALTER alter_result == 0
+ if the condition is not satisfied we will report error and
+ Return 1. Make sure wait_for_master is called before calling this function
+ This function should be called only at the write binlog time of commit/
+ rollback alter. We will alter_committed if everything is fine.
+
+ @retval 1 error
+ @retval 0 Ok
+*/
+static int process_master_state(THD *thd, int alter_result,
+ uint64 &start_alter_id)
+{
+ start_alter_info *info= thd->rgi_slave->sa_info;
+
+ DBUG_ASSERT(info->state > start_alter_state::REGISTERED);
+ /* this function shouldn't be called twice */
+ DBUG_ASSERT(start_alter_id);
+
+ start_alter_id= 0;
+ if ((info->state == start_alter_state::ROLLBACK_ALTER && alter_result > 0)
+ || (info->state == start_alter_state::COMMIT_ALTER && !alter_result))
+ {
+ alter_committed(thd, info, thd->rgi_slave->rli->mi);
+ return 0;
+ }
+ else
+ {
+ thd->rgi_slave->rli->report(ERROR_LEVEL, 0, thd->rgi_slave->gtid_info(),
+ "Query caused different errors on master and slave. "
+ "Error on master: message (format)='%s' error code=%d ; "
+ "Error on slave: actual message='%s', error code=%d. "
+ "Default database: '%s'. Query: '%s'",
+ ER_THD(thd, 0),
+ 0, thd->get_stmt_da()->message() ,
+ thd->get_stmt_da()->sql_errno(),
+ thd->get_db(), thd->query());
+ thd->is_slave_error= 1;
+ return 1;
+ }
+}
+
+/*
+ Returns a (global unique) identifier of START Alter when slave applier
+ executes mysql_alter_table().
+ In non-slave context it is zero.
+*/
+static uint64 get_start_alter_id(THD *thd)
+{
+ DBUG_ASSERT(!(thd->rgi_slave &&
+ (thd->rgi_slave->gtid_ev_flags_extra &
+ Log_event::FL_START_ALTER_E1)) ||
+ !thd->rgi_slave->direct_commit_alter);
+ return
+ thd->rgi_slave &&
+ (thd->rgi_slave->gtid_ev_flags_extra & Log_event::FL_START_ALTER_E1) ?
+ thd->variables.gtid_seq_no : 0;
+}
+
/**
Alter table
@@ -9424,6 +9637,14 @@ bool mysql_alter_table(THD *thd, const LEX_CSTRING *new_db,
bool partition_changed= false;
bool fast_alter_partition= false;
#endif
+ bool partial_alter= false;
+ /*
+ start_alter_id is the gtid seq no of the START Alter - the 1st part
+ of two-phase loggable ALTER. The variable is meaningful only
+ on slave execution.
+ */
+ uint64 start_alter_id= get_start_alter_id(thd);
+
/*
Create .FRM for new version of table with a temporary name.
We don't log the statement, it will be logged later.
@@ -10342,7 +10563,8 @@ do_continue:;
&ha_alter_info,
&target_mdl_request, &ddl_log_state,
&trigger_param,
- &alter_ctx);
+ &alter_ctx, partial_alter,
+ start_alter_id, if_exists);
thd->count_cuted_fields= org_count_cuted_fields;
inplace_alter_table_committed= ha_alter_info.inplace_alter_table_committed;
inplace_alter_table_committed_argument=
@@ -10353,7 +10575,6 @@ do_continue:;
goto err_cleanup;
}
cleanup_table_after_inplace_alter_keep_files(&altered_table);
-
goto end_inplace;
}
else
@@ -10396,7 +10617,24 @@ do_continue:;
}
else
thd->close_unused_temporary_table_instances(table_list);
+ /*
+ We are going to log start alter
+ 1. not a tmp table
+ 2. tmp_table but binlog_format != row
+ 3. start_alter_id != 0 (master logged it)
+ */
+ if (table->s->tmp_table == NO_TMP_TABLE ||
+ (!thd->is_current_stmt_binlog_format_row() &&
+ table_creation_was_logged && !binlog_as_create_select) ||
+ start_alter_id)
+ if (write_bin_log_start_alter(thd, partial_alter, start_alter_id,
+ if_exists, &table->s->mem_root))
+ goto err_new_table_cleanup;
+ DBUG_EXECUTE_IF("start_alter_delay_master", {
+ debug_sync_set_action(thd,
+ STRING_WITH_LEN("now wait_for alter_cont"));
+ });
// It's now safe to take the table level lock.
if (lock_tables(thd, table_list, alter_ctx.tables_opened,
MYSQL_LOCK_USE_MALLOC))
@@ -10509,6 +10747,13 @@ do_continue:;
}
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
+ if (start_alter_id)
+ {
+ DBUG_ASSERT(thd->slave_thread);
+
+ if (wait_for_master(thd))
+ goto err_new_table_cleanup;
+ }
if (table->s->tmp_table != NO_TMP_TABLE)
{
/* Release lock if this is a transactional temporary table */
@@ -10552,15 +10797,22 @@ do_continue:;
binlog_commit(thd, true);
}
+ if(start_alter_id)
+ {
+ thd->mark_tmp_table_as_free_for_reuse(new_table);
+ if (process_master_state(thd, 0, start_alter_id))
+ DBUG_RETURN(true);
+ }
/* We don't replicate alter table statement on temporary tables */
- if (!thd->is_current_stmt_binlog_format_row() &&
+ else if (!thd->is_current_stmt_binlog_format_row() &&
table_creation_was_logged &&
!binlog_as_create_select)
{
int tmp_error;
thd->binlog_xid= thd->query_id;
ddl_log_update_xid(&ddl_log_state, thd->binlog_xid);
- tmp_error= write_bin_log_with_if_exists(thd, true, false, log_if_exists);
+ tmp_error= write_bin_log_with_if_exists(thd, true, false, log_if_exists,
+ partial_alter);
thd->binlog_xid= 0;
if (tmp_error)
goto err_cleanup;
@@ -10790,12 +11042,19 @@ end_inplace:
DBUG_ASSERT(!(mysql_bin_log.is_open() &&
thd->is_current_stmt_binlog_format_row() &&
(create_info->tmp_table())));
- if (!binlog_as_create_select)
+
+ if(start_alter_id)
+ {
+ if (process_master_state(thd, 0, start_alter_id))
+ DBUG_RETURN(true);
+ }
+ else if (!binlog_as_create_select)
{
int tmp_error;
thd->binlog_xid= thd->query_id;
ddl_log_update_xid(&ddl_log_state, thd->binlog_xid);
- tmp_error= write_bin_log_with_if_exists(thd, true, false, log_if_exists);
+ tmp_error= write_bin_log_with_if_exists(thd, true, false, log_if_exists,
+ partial_alter);
thd->binlog_xid= 0;
if (tmp_error)
goto err_cleanup;
@@ -10892,6 +11151,10 @@ err_cleanup:
/* Signal to storage engine that ddl log is committed */
(*inplace_alter_table_committed)(inplace_alter_table_committed_argument);
}
+ DEBUG_SYNC(thd, "alter_table_after_temp_table_drop");
+ if (partial_alter || start_alter_id)
+ write_bin_log_start_alter_rollback(thd, start_alter_id, partial_alter,
+ if_exists);
DBUG_RETURN(true);
err_with_mdl:
@@ -10902,6 +11165,9 @@ err_with_mdl:
remove all references to the altered table from the list of locked
tables and release the exclusive metadata lock.
*/
+ if (partial_alter || start_alter_id)
+ write_bin_log_start_alter_rollback(thd, start_alter_id, partial_alter,
+ if_exists);
thd->locked_tables_list.unlink_all_closed_tables(thd, NULL, 0);
if (!table_list->table)
thd->mdl_context.release_all_locks_for_name(mdl_ticket);
diff --git a/sql/sql_table.h b/sql/sql_table.h
index eaa03bfaf8c..f71d1afc098 100644
--- a/sql/sql_table.h
+++ b/sql/sql_table.h
@@ -209,9 +209,10 @@ void sp_prepare_create_field(THD *thd, Column_definition *sql_field);
bool mysql_write_frm(ALTER_PARTITION_PARAM_TYPE *lpt, uint flags);
int write_bin_log(THD *thd, bool clear_error,
char const *query, ulong query_length,
- bool is_trans= FALSE);
+ bool is_trans= FALSE, bool log_zero_errcode= false);
int write_bin_log_with_if_exists(THD *thd, bool clear_error,
- bool is_trans, bool add_if_exists);
+ bool is_trans, bool add_if_exists,
+ bool commit_alter= false);
void promote_first_timestamp_column(List<Create_field> *column_definitions);
diff --git a/sql/sys_vars.cc b/sql/sys_vars.cc
index e18ca6392d8..39133a4599a 100644
--- a/sql/sys_vars.cc
+++ b/sql/sys_vars.cc
@@ -2409,6 +2409,14 @@ static Sys_var_bit Sys_skip_parallel_replication(
SESSION_ONLY(option_bits), NO_CMD_LINE, OPTION_RPL_SKIP_PARALLEL,
DEFAULT(FALSE));
+static const char *binlog_alter_two_phase_enum[]=
+ {"No","Yes", NullS};
+static Sys_var_enum Sys_binlog_alter_two_phase(
+ "binlog_alter_two_phase",
+ "If set split the alter into 2 statement START ALTER and COMMIT/ROLLBACK"
+ "ALTER",
+ SESSION_VAR(binlog_alter_two_phase), CMD_LINE(REQUIRED_ARG), binlog_alter_two_phase_enum,
+ DEFAULT(0));
static bool
check_gtid_ignore_duplicates(sys_var *self, THD *thd, set_var *var)