#################################################################
# Bug#18258933 CONCATENATION OF MYSQLBINLOG OUTPUT DOES NOT
# WORK WITH GTID

# Problem:  When mysqlbinlog processes and concatenates
# multiple binary log files into one output file, this
# output file is not in useful state. When it is later
# used for point-in-time recovery, it is producing
# ER_GTID_NEXT_TYPE_UNDEFINED_GROUP "ERROR 1837: When
# @@SESSION.GTID_NEXT is set to a GTID, you must explicitly
# set it to a different value after a COMMIT or ROLLBACK.
# Please check GTID_NEXT variable manual page for detailed
# explanation. Current @@SESSION.GTID_NEXT is 'xyz'"

# Solution: When mysqlbinlog processes second binary log's
#            start_log_event, it can output
#            "SET GTID_NEXT=AUTOMATIC" if required i.e., if
#            previous binary log is leaving the GTID_NEXT state
#            in 'undefined' state.

# Steps to verify the case:
# 1) On a Gtid enabled server, generate some dummy statements
#    in binary log ( to generate GTID_NEXT events in binary log 1)
# 2) Restart server with gtid-mode OFF
# 3) On a Gtid disabled sever, generate some dummy statements
#     in binary log ( to generate anonymous events in binary log 2)
# 4) Restart server again with gtid-mode ON.
# 5) On a Gtid enabled server, generate some dummy statements
#    in binary log ( to generate GTID_NEXT events in binary log 3)
# 6) Concat all three binary logs 1,2 and 3 and apply the generated
#    sql against mysql command line tool and it should not cause
#    any error (i.e., no ER_GTID_NEXT_TYPE_UNDEFINED_GROUP error)
#################################################################
--source include/have_binlog_format_statement.inc
--source include/have_gtid.inc

# Use this so that we can use rpl_restart_server.inc later.
--let $rpl_server_count= 1
--let $rpl_topology= none
--source include/rpl_init.inc
--source include/rpl_default_connections.inc


#Initial setup
--let $datadir= `SELECT @@datadir`
--let $binlog_file1= query_get_value(SHOW MASTER STATUS, File, 1)

# Step 1 : Dummy statements to generate gtid transactions (gtid-mode is ON)
# in binlog_file1
CREATE TABLE t1(i INT);
INSERT INTO t1 values (1);

# Step 2: Restart server with gtid-mode off (binlog will be rotated)
--let $rpl_server_number= 1
--let $rpl_server_parameters= --gtid-mode=off
--source include/rpl_restart_server.inc
--let $binlog_file2= query_get_value(SHOW MASTER STATUS, File, 1)

# Step 3: Dummy statements to generate anonymous transactions
# (gtid-mode is OFF)
CREATE TABLE t2(i INT);
INSERT INTO t2 values (2);

# Step 4: Restart server with gtid-mode ON (binlog will be rotated)
--let $rpl_server_number= 1
--let $rpl_server_parameters= --gtid-mode=on
--source include/rpl_restart_server.inc
--let $binlog_file3= query_get_value(SHOW MASTER STATUS, File, 1)

# Step 5 : Dummy statements to generate gtid transactions (gtid-mode is ON)
# in binlog_file3
CREATE TABLE t3(i INT);
INSERT INTO t3 values (3);

# Clean up command (let DROP TABLE be in binary log 4, when we apply
# all three binary logs, we can select the data and verify that the
# data is there.
FLUSH LOGS;
DROP TABLE t1,t2,t3;

# Step 6: Combine binlog_file1, binlog_file2, binlog_file3 and generate
# a .sql file.
--exec $MYSQL_BINLOG --force-if-open $datadir/$binlog_file1 $datadir/$binlog_file2  $datadir/$binlog_file3 > $MYSQLTEST_VARDIR/tmp/concat_three_files.sql

# Execute reset master, so that when we are applying gtid transactions,
# they should not skipped.
RESET MASTER;

# Step 7: Apply the generated .sql through mysql command line tool
# and it should not cause any issues
--exec $MYSQL --user=root --host=127.0.0.1 --port=$MASTER_MYPORT < $MYSQLTEST_VARDIR/tmp/concat_three_files.sql

# Step 8: Verify the data in t1 and t2
# [NOTE] there will not be table t3 created because CREATE TABLE t2, INSERT INTO t2
# (originally anonymous transactions) now occupies gtid numbers 3 and 4,
# hence CREATE TABLE t3, INSERT INTO t3(which has originally gtid numbers 3
# and 4) will be skipped when we are reapplying them.
SELECT * FROM t1;
SELECT * FROM t2;

# Clean up
DROP TABLE t1,t2;
--remove_file $MYSQLTEST_VARDIR/tmp/concat_three_files.sql
