###############################################################################
# Bug#21276561: FAILURE TO GENERATE GTID LEADS TO INCONSISTENCY
#
# Problem:
# =======
# If generating a GTID for a transaction fails, the  transaction is not written
# to the binary log but still gets  committed, which potentially leads to
# master/slave data inconsistency.
#
# Test:
# =====
# Simulate a scenario such that generation of GTID number reaches its maximum
# value and the new GTID cannot be generated because of that. Verify that this
# fatal error case is appropriately handled as per the binlog_error_action
# value specified by user.
###############################################################################
--source include/have_innodb.inc
--source include/have_gtid.inc
# This test case is binlog_format agnostic
--source include/have_binlog_format_statement.inc
# Don't test this under valgrind, memory leaks will occur
--source include/not_valgrind.inc

call mtr.add_suppression("An error occurred during flush stage of the commit");
call mtr.add_suppression("Attempting backtrace. You can use the following information to find out");

#Test case 1:
SET GLOBAL binlog_error_action=IGNORE_ERROR;
CREATE TABLE t1 (a INT PRIMARY KEY) ENGINE=InnoDB;
# @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty.
# Hence execute RESET MASTER.
RESET MASTER;
SET GLOBAL gtid_purged = CONCAT(@@GLOBAL.server_uuid, ':1-9223372036854775805');
INSERT INTO t1 VALUES (1);
--error ER_GNO_EXHAUSTED
INSERT INTO t1 VALUES (2);

# Check that transaction is committed
--let $assert_cond = COUNT(*) = 2 FROM t1;
--let $assert_text = Count of elements in t1 should be 2.
--source include/assert.inc
--source include/restart_mysqld.inc

RESET MASTER;
DROP TABLE t1;

#Test case 2:
CREATE TABLE t1 (a INT PRIMARY KEY) ENGINE=InnoDB;
# @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty.
# Hence execute RESET MASTER.
RESET MASTER;
SET GLOBAL gtid_purged = CONCAT(@@GLOBAL.server_uuid, ':1-9223372036854775805');
INSERT INTO t1 VALUES (1);
SET GLOBAL binlog_error_action=ABORT_SERVER;
--exec echo "wait" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
--error ER_BINLOG_LOGGING_IMPOSSIBLE
INSERT INTO t1 VALUES (2);

--exec echo "restart" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
--enable_reconnect
--source include/wait_until_connected_again.inc

RESET MASTER;
DROP TABLE t1;
