################# suite/perfschema/t/show_plugin.test ##########################
#                                                                              #
# MySQL plugins can define their own status variables and system variables.    #
# This test exercises SHOW STATUS, SHOW VARIABLES and the status and system    #
# variable tables while the EXAMPLE plugin is loaded and unloaded.             #
#                                                                              #
# The EXAMPLE plugin defines the following system and status variables:        #
# Global system variables:                                                     # 
#   example_double_thdvar                                                      #
#   example_double_var                                                         #
#   example_enum_var                                                           #
#   example_ulong_var                                                          #
#                                                                              #
# Session variables:                                                           #
#   example_double_thdvar - Local and/or global                                #
#                                                                              #
# Global status:                                                               #
#   example_func_example  - Status function                                    #
#                                                                              #
################################################################################

--source include/have_perfschema.inc
--source include/not_embedded.inc
--source include/not_windows_embedded.inc
--source include/have_example_plugin.inc
--source include/have_debug_sync.inc

--enable_connect_log

--echo
--echo ================================================================================
--echo SETUP
--echo ================================================================================
--echo # Save the initial number of concurrent sessions
--source include/count_sessions.inc
--echo
--echo # Save current SHOW_COMPATIBILITY_56 setting
SELECT @@global.show_compatibility_56 INTO @show_compatibility_56_save;
--echo #
--echo # SET COMPATIBILITY MODE = OFF
--echo #
SET @@global.show_compatibility_56 = OFF;
--echo
--echo # Verify EXAMPLE plugin is not loaded
SELECT COUNT(*) = 0 AS "Expect 1" FROM information_schema.plugins WHERE plugin_name = "EXAMPLE";
--echo
--echo # Create one session to force local and global system variables;
connect(con0, localhost, root,,);
--connection default
--echo
--echo ================================================================================
--echo TEST 1- NO PLUGIN VARS
--echo ================================================================================
--source ../include/show_plugin_verifier.inc
--echo

--echo ================================================================================
--echo TEST 2 - PLUGIN LOAD, UNLOAD, RELOAD
--echo ================================================================================
--echo ================================================================================
--echo 2.1 - INSTALL PLUGIN
--echo ================================================================================
--replace_regex /\.dll/.so/
eval INSTALL PLUGIN example SONAME '$EXAMPLE_PLUGIN';

--echo
--echo ================================================================================
--echo 2.1a - FORCE SYNC OF LOCAL AND GLOBAL SYSTEM VARS
--echo ================================================================================
--source ../include/show_plugin_verifier.inc

--disconnect con0

--echo
--echo ================================================================================
--echo 2.2 - SET PLUGIN VARS
--echo ================================================================================
--echo # GLOBAL
SET GLOBAL example_ulong_var = 100;
SET GLOBAL example_enum_var = e1;
SET GLOBAL example_double_var = 100.9990;
SET GLOBAL example_double_thdvar = 101.9991;
--echo # SESSION
SET SESSION example_double_thdvar = 102.9992;

--echo
--echo ================================================================================
--echo 2.3 - VERIFY UPDATED PLUGIN VARS
--echo ================================================================================
--source ../include/show_plugin_verifier.inc

--echo
--echo ================================================================================
--echo 2.4 - UNINSTALL PLUGIN
--echo ================================================================================
UNINSTALL PLUGIN example;

--echo
--echo ================================================================================
--echo 2.5 - VERIFY NO PLUGIN VARS
--echo ================================================================================
--source ../include/show_plugin_verifier.inc

--echo
--echo ================================================================================
--echo 2.6 - REINSTALL PLUGIN
--echo ================================================================================
--echo # Reinstall EXAMPLE plugin
--replace_regex /\.dll/.so/
eval INSTALL PLUGIN example SONAME '$EXAMPLE_PLUGIN';

--echo
--echo ================================================================================
--echo 2.7 - SET PLUGIN VARS AGAIN
--echo ================================================================================
--echo # GLOBAL
SET GLOBAL example_ulong_var = 200;
SET GLOBAL example_enum_var = e2;
SET GLOBAL example_double_var = 200.8880;
SET GLOBAL example_double_thdvar = 201.8881;
--echo # SESSION
SET SESSION example_double_thdvar = 202.8882;

--echo
--echo ================================================================================
--echo 2.8 - VERIFY PLUGIN VARS
--echo ================================================================================
--source ../include/show_plugin_verifier.inc

--echo
--echo ================================================================================
--echo 2.9 - UNINSTALL PLUGIN
--echo ================================================================================
UNINSTALL PLUGIN example;

--echo
--echo ================================================================================
--echo 2.10 - VERIFY NO PLUGIN VARS
--echo ================================================================================
--source ../include/show_plugin_verifier.inc

--echo
--echo ================================================================================
--echo TEST 3 - SESSION PLUGIN VARS ON MULTIPLE CONNECTIONS
--echo ================================================================================
--echo
--echo ================================================================================
--echo 3.1 - INSTALL PLUGIN
--echo ================================================================================
--replace_regex /\.dll/.so/
eval INSTALL PLUGIN example SONAME '$EXAMPLE_PLUGIN';

--echo
--echo ================================================================================
--echo 3.2 - SET GLOBAL AND DEFAULT CONNECTION VARS
--echo ================================================================================
--connection default
SET GLOBAL example_ulong_var = 300;
SET GLOBAL example_enum_var = e1;
SET GLOBAL example_double_var = 301.0000;
SET GLOBAL example_double_thdvar = 302.0000;
SET SESSION example_double_thdvar = 300.0000;

--echo
--echo ================================================================================
--echo 3.3 - CONNECT 3 CLIENTS, SET LOCAL PLUGIN VARS
--echo ================================================================================
connect(con1, localhost, root,,);
SET SESSION example_double_thdvar = 300.1111;
--echo
connect(con2, localhost, root,,);
SET SESSION example_double_thdvar = 300.2222;
--echo
connect(con3, localhost, root,,);
SET SESSION example_double_thdvar = 300.3333;
--echo
--connection default

--echo
--echo ================================================================================
--echo 3.4 - VERIFY GLOBAL AND SESSION PLUGIN VARS
--echo ================================================================================
--source ../include/show_plugin_verifier.inc
--echo
--echo # Variables by thread
SELECT variable_name, variable_value FROM performance_schema.variables_by_thread
WHERE variable_name LIKE "example_%" ORDER BY variable_value;

--echo
--echo ================================================================================
--echo 3.5 - DISCONNECT CLIENTS
--echo ================================================================================
--connection con1
--disconnect con1
--source include/wait_until_disconnected.inc

--connection con2
--disconnect con2
--source include/wait_until_disconnected.inc

--connection con3
--disconnect con3
--source include/wait_until_disconnected.inc

--connection default

--echo
--echo ================================================================================
--echo 3.6 - VERIFY SESSION VARS ARE REMOVED
--echo ================================================================================
--source ../include/show_plugin_verifier.inc
--echo
--echo # Variables by thread
SELECT variable_name, variable_value FROM performance_schema.variables_by_thread
WHERE variable_name LIKE "example_%" ORDER BY variable_value;

--echo
--echo ================================================================================
--echo 3.7 - RECONNECT 3 CLIENTS, SET SESSION VARS FOR EACH
--echo ================================================================================
connect(con1, localhost, root,,);
SET SESSION example_double_thdvar = 311.1111;
--echo
connect(con2, localhost, root,,);
SET SESSION example_double_thdvar = 322.2222;
--echo
connect(con3, localhost, root,,);
SET SESSION example_double_thdvar = 333.3333;
--echo
--connection default

--echo
--echo ================================================================================
--echo 3.8 - VERIFY GLOBAL AND SESSION VARS
--echo ================================================================================
--source ../include/show_plugin_verifier.inc
--echo
--echo # Variables by thread
SELECT variable_name, variable_value FROM performance_schema.variables_by_thread
WHERE variable_name LIKE "example_%" ORDER BY variable_value;

--echo
--echo ================================================================================
--echo 3.9 - UNINSTALL PLUGIN, LEAVE CLIENTS CONNECTED
--echo ================================================================================
UNINSTALL PLUGIN example;

--echo
--echo ================================================================================
--echo 3.10 - VERIFY SESSION VARS ARE REMOVED
--echo ================================================================================
--source ../include/show_plugin_verifier.inc
--echo
--echo # VARIABLES BY THREAD
SELECT variable_name, variable_value FROM performance_schema.variables_by_thread
WHERE variable_name LIKE "example_%" ORDER BY variable_value;

--echo
--echo ================================================================================
--echo 3.11 - DISCONNECT CLIENTS
--echo ================================================================================
--connection con1
--disconnect con1
--source include/wait_until_disconnected.inc

--connection con2
--disconnect con2
--source include/wait_until_disconnected.inc

--connection con3
--disconnect con3
--source include/wait_until_disconnected.inc

--connection default

--echo
--echo ================================================================================
--echo 3.12 - VERIFY CLIENTS ARE REMOVED
--echo ================================================================================
--source ../include/show_plugin_verifier.inc
--echo
--echo # VARIABLES BY THREAD
SELECT variable_name, variable_value FROM performance_schema.variables_by_thread
WHERE variable_name LIKE "example_%" ORDER BY variable_value;

--echo
--echo ================================================================================
--echo TEST 4 - BUG#18008907: DEADLOCK WITH CHANGE_USER, SHOW VARIABLES, INSTALL PLUGIN
--echo ================================================================================
--echo
--disable_result_log
DELIMITER |;

CREATE PROCEDURE install_no_such_plugin()
BEGIN
  INSTALL PLUGIN no_such_plugin SONAME 'no_such_object';
END|
--echo
CREATE PROCEDURE show_vars()
BEGIN
  -- select on information_schema.global_variables
  -- will fail, still need to cover execution of the code
  -- to verify potential dead locks.
  declare continue handler for sqlexception
  begin
  end;

  SELECT COUNT(*) FROM information_schema.global_variables;
  SELECT COUNT(*) FROM performance_schema.global_variables;
END|

DELIMITER ;|
--enable_result_log

--echo
--echo ================================================================================
--echo 4.1 - DEADLOCK SCENARIO 1:
--echo       con1 - has LOCK_system_variables_hash and waits on LOCK_plugin AND
--echo       default - has LOCK_plugin and waits on LOCK_system_variables_hash
--echo ================================================================================
connect(con1, localhost, root,,);
SET DEBUG_SYNC='acquired_LOCK_system_variables_hash SIGNAL install_plugin WAIT_FOR cont_show_vars';
--send call show_vars();
--connection default
SET DEBUG_SYNC='now WAIT_FOR install_plugin';
SET DEBUG_SYNC='acquired_LOCK_plugin SIGNAL cont_show_vars';
--send call install_no_such_plugin();
--connection con1

--echo
--echo # Without fix, reap will hang.
--disable_warnings
--replace_column 1 #
--reap;
--enable_warnings
SET DEBUG_SYNC='RESET';

--connection default
--replace_regex /(Can\'t open shared library).*$/\1/
--error ER_CANT_OPEN_LIBRARY
--reap;
--echo
SET DEBUG_SYNC='RESET';

--echo
--echo ================================================================================
--echo 4.2 - DEADLOCK SCENARIO 2:
--echo       default - has LOCK_system_variables_hash, waits on LOCK_global_system_variables
--echo       con1 - has LOCK_plugin, waits on LOCK_system_variables_hash AND
--echo       con2 - has LOCK_global_system_variables, waits on LOCK_plugin
--echo ================================================================================
SET DEBUG_SYNC='acquired_LOCK_system_variables_hash SIGNAL install_plugin WAIT_FOR nothing TIMEOUT 10';
--send call show_vars();
--connection con1
SET DEBUG_SYNC='now WAIT_FOR install_plugin';
SET DEBUG_SYNC='acquired_LOCK_plugin SIGNAL create_connection WAIT_FOR nothing TIMEOUT 10';
--send call install_no_such_plugin();
connect(con2, localhost, root,,);
SET DEBUG_SYNC='now WAIT_FOR create_connection';

--echo
--echo # Without fix, deadlock situation will occur on timeout of debug_syncs in
--echo # default and con1.  Because of this, change_user operation hangs.
--echo change_user
change_user;
--connection con1
--replace_regex /(Can\'t open shared library).*$/\1/
--error ER_CANT_OPEN_LIBRARY
--reap;

--echo
--connection default
--replace_column 1 #
--disable_warnings
--reap;
--enable_warnings

--echo
--disconnect con2

--echo
--echo ================================================================================
--echo 4.3 - DEADLOCK SCENARIO 3: CONCURRENT SHOW VARIABLES AND UNINSTALL PLUGIN
--echo ================================================================================
--echo #
--echo # INSTALL PLUGIN
--echo #
--replace_regex /\.dll/.so/
eval INSTALL PLUGIN example SONAME '$EXAMPLE_PLUGIN';
--connection con1

--echo
--echo # Acquiring LOCK_system_variables_hash and LOCK_plugin_delete
SET DEBUG_SYNC='acquired_LOCK_system_variables_hash SIGNAL uninstall_plugin WAIT_FOR go';

--send call show_vars();
connect(con2, localhost, root,,);

--echo #
--echo # UNINSTALL PLUGIN
--echo #
SET DEBUG_SYNC='now WAIT_FOR uninstall_plugin';
--send UNINSTALL PLUGIN example;
--connection default

--echo
--echo # Uninstall will wait until SHOW VARIABLES releases LOCK_plugin_delete.
let $wait_condition= SELECT count(*) = 1 FROM INFORMATION_SCHEMA.PROCESSLIST
                     WHERE INFO='UNINSTALL PLUGIN example' and
                     STATE='System lock';
--source include/wait_condition.inc
SET DEBUG_SYNC='now SIGNAL go';
--echo # Connect con1 - reap
--connection con1
--replace_column 1 #
--disable_warnings
--reap
--enable_warnings
--echo # Connect con2 - reap
--connection con2
--disable_warnings
--reap
--enable_warnings

--echo
--echo ================================================================================
--echo TEST 5 - BUG#22225549 MYSQL_CHANGE_USER/MYSQL_RESET_CONNECTION + SET INNODB...
--echo          Update to plugin-defined session variable triggers resync with global
--echo          variables and deadlocks on THD::LOCK_thd_sysvar.
--echo ================================================================================
#
# THD:LOCK_thd_sysvar is only used when SHOW_COMPATIBILITY_56 = OFF
#
SELECT @@global.show_compatibility_56;
#
# The deadlock occurs when plugin-defined session variables are resynced with the global
# variables. To force a resync, change the user and update a session variable from a
# plugin, in this case InnoDB.
#
select @@session.innodb_strict_mode;
let $innodb_strict_mode_save = `select @@session.innodb_strict_mode`;
select user(), current_user();
--echo change_user root
--change_user root
--echo #
--echo # Trigger a resync of session variables with global variables.
--echo #
set @@session.innodb_strict_mode=off;

--echo
--echo # Restore
--echo
eval set @@session.innodb_strict_mode=$innodb_strict_mode_save;
select @@session.innodb_strict_mode;

--echo
--echo ================================================================================
--echo CLEAN UP
--echo ================================================================================
--connection default
--echo
DROP PROCEDURE show_vars;
DROP PROCEDURE install_no_such_plugin;
SET DEBUG_SYNC='RESET';

--echo
--disconnect con1
--disconnect con2
--disable_connect_log

--echo
--echo # Restore SHOW_COMPATIBILITY_56 setting
SET @@global.show_compatibility_56 = @show_compatibility_56_save;

--echo
--echo # Wait till we reached the initial number of concurrent sessions
--source include/wait_until_count_sessions.inc
