--source include/have_debug_sync.inc
--source include/have_innodb.inc
--source include/have_debug_sync.inc
--source include/count_sessions.inc

# saving global variables which will be changed in this test
SET @innodb_show_verbose_locks_save = @@global.innodb_show_verbose_locks;
SET @innodb_show_locks_held_save = @@global.innodb_show_locks_held;
SET @innodb_status_output_locks_save = @@global.innodb_status_output_locks;

# create a simple table and fill it with 10 records
CREATE TABLE t (id INT PRIMARY KEY, value INT NOT NULL DEFAULT 0) ENGINE=InnoDB;
INSERT INTO t(id) VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);

# check "SHOW ENGINE INNODB STATUS" output when there are transactions
# blocked by locked records

# first, check regular output with monitor turned off
SET GLOBAL innodb_show_verbose_locks = 0;
--let expected_locks= 0
--source suite/innodb/include/percona_extended_innodb_status.inc

# then, verbose output with monitor turned off
SET GLOBAL innodb_show_verbose_locks = 1;
--let expected_locks= 4
--source suite/innodb/include/percona_extended_innodb_status.inc

# enable InnoDB lock monitor
SET GLOBAL innodb_status_output_locks = ON;

# after that, verbose output with monitor turned on
--let expected_locks= 12
--source suite/innodb/include/percona_extended_innodb_status.inc

# and finally, verbose output with monitor turned on and record limit set to 1
SET GLOBAL innodb_show_locks_held = 1;
--let expected_locks= 4
--source suite/innodb/include/percona_extended_innodb_status.inc

# in addition, setting record limit to 0 should act as verbose output with
# monitor turned off
SET GLOBAL innodb_show_locks_held = 0;
--let expected_locks= 4
--source suite/innodb/include/percona_extended_innodb_status.inc

# restoring affected global variables
SET GLOBAL innodb_show_verbose_locks = @innodb_show_verbose_locks_save;
SET GLOBAL innodb_show_locks_held = @innodb_show_locks_held_save;
SET GLOBAL innodb_status_output_locks = @innodb_status_output_locks_save;

DROP TABLE t;

--echo #
--echo # Bug 1586262: "Buffer pool size, bytes" always 0 in InnoDB status
--echo #

--let innodb_status= $MYSQL_TMP_DIR/innodb.status

--echo mysql -e "SHOW ENGINE INNODB STATUS"
--exec $MYSQL -e "SHOW ENGINE INNODB STATUS" > $innodb_status;

--let SEARCH_FILE=$innodb_status
--let SEARCH_PATTERN=Buffer pool size, bytes 25149440
--echo Grepping InnoDB status for $SEARCH_PATTERN
--source include/search_pattern_in_file.inc
--remove_file $innodb_status


--echo #
--echo # PS-6150: MySQL crash - sync_mutex_to_string
--echo #

connect (con1,localhost,root,,);
connect (con2,localhost,root,,);

--connection default
CREATE TABLE t1(id INT(15) NOT NULL AUTO_INCREMENT, a INT, PRIMARY KEY(id)) ENGINE=InnoDB;

--connection con1
SET DEBUG_SYNC='innobase_lock_autoinc SIGNAL autoinc_lock.locked WAIT_FOR autoinc_lock.continue'; 
--send INSERT INTO test.t1(a) VALUES(1)

--connection con2
# Be sure that con1 locked autoinc_lock
SET DEBUG_SYNC='now WAIT_FOR autoinc_lock.locked';

# The following will cause autoinc_lock to reach OS level locking and registering the lock in sync_array used by 'SHOW ENGINE INNODB STATUS' to list locked mutexes
SET SESSION debug="+d,catch_autoinc_mutex_os_lock";
SET DEBUG_SYNC='autoinc_mutex_wait SIGNAL autoinc_mutex_wait.locked'; 
--send INSERT INTO test.t1(a) VALUES(1)

--connection default
# wait up to the point when lock is registered in sync_array (in fact OS lock is not acquired yet, but this is enough)
SET DEBUG_SYNC='now WAIT_FOR autoinc_mutex_wait.locked';

# Send the query that caused the crash
--disable_result_log
SHOW ENGINE INNODB STATUS;
--enable_result_log


# cleanup
# Unblock connections.
--connection default
SET DEBUG_SYNC='now SIGNAL autoinc_lock.continue';
SET SESSION debug="-d,catch_autoinc_mutex_os_lock";

--connection con1
--reap

--connection con2
--reap

--disconnect con1
--disconnect con2

--connection default
DROP TABLE t1;

# wait until all additional connections close
--source include/wait_until_count_sessions.inc
SET DEBUG_SYNC = 'RESET';

