From d657438ea041cec292f366f0ad4427b4027da4c7 Mon Sep 17 00:00:00 2001
From: Matthias Leich <Matthias.Leich@sun.com>
Date: Tue, 13 Jan 2009 14:09:24 +0100
Subject: [PATCH] Fix for Bug#40377 sporadic pushbuild failure in log_state:
 result mismatch + add workaround for bug 38124 + messages into the protocol
 when sessions are switched + replace error numbers by error names + reset of
 system variables to initial values per subtest + remove a file created by
 this test + minor improvements in structure and formatting

---
 mysql-test/r/log_state.result |  81 ++++++++------
 mysql-test/t/log_state.test   | 200 +++++++++++++++++++++++++---------
 2 files changed, 195 insertions(+), 86 deletions(-)

diff --git a/mysql-test/r/log_state.result b/mysql-test/r/log_state.result
index 63903a034d2..5c3e3d789a1 100644
--- a/mysql-test/r/log_state.result
+++ b/mysql-test/r/log_state.result
@@ -1,3 +1,7 @@
+SET @old_general_log= @@global.general_log;
+SET @old_general_log_file= @@global.general_log_file;
+SET @old_slow_query_log= @@global.slow_query_log;
+SET @old_slow_query_log_file= @@global.slow_query_log_file;
 set global general_log= OFF;
 truncate table mysql.general_log;
 truncate table mysql.slow_log;
@@ -33,20 +37,26 @@ general_log	ON
 log	ON
 log_slow_queries	OFF
 slow_query_log	OFF
-set session long_query_time=1;
-select sleep(2);
-sleep(2)
+# Establish connection con1 (user=root)
+# Switch to connection con1
+set @long_query_time = <long_query_time>;
+set session long_query_time = @long_query_time;
+select sleep(@long_query_time + 1);
+sleep(@long_query_time + 1)
 0
 select * from mysql.slow_log where sql_text NOT LIKE '%slow_log%';
 start_time	user_host	query_time	lock_time	rows_sent	rows_examined	db	last_insert_id	insert_id	server_id	sql_text
+# Switch to connection default
 set global slow_query_log= ON;
-set session long_query_time=1;
-select sleep(2);
-sleep(2)
+# Switch to connection con1
+set session long_query_time = @long_query_time;
+select sleep(@long_query_time + 1);
+sleep(@long_query_time + 1)
 0
 select * from mysql.slow_log where sql_text NOT LIKE '%slow_log%';
 start_time	user_host	query_time	lock_time	rows_sent	rows_examined	db	last_insert_id	insert_id	server_id	sql_text
-TIMESTAMP	USER_HOST	QUERY_TIME	00:00:00	1	0	test	0	0	1	select sleep(2)
+TIMESTAMP	USER_HOST	QUERY_TIME	00:00:00	1	0	test	0	0	1	select sleep(@long_query_time + 1)
+# Switch to connection default
 show global variables
 where Variable_name = 'log' or Variable_name = 'log_slow_queries' or
 Variable_name = 'general_log' or Variable_name = 'slow_query_log';
@@ -92,8 +102,8 @@ slow_query_log_file	#
 show variables like 'log_output';
 Variable_name	Value
 log_output	FILE,TABLE
-set global general_log_file='/not exiting path/log.master';
-ERROR 42000: Variable 'general_log_file' can't be set to the value of '/not exiting path/log.master'
+set global general_log_file='/not existing path/log.master';
+ERROR 42000: Variable 'general_log_file' can't be set to the value of '/not existing path/log.master'
 set global general_log_file='MYSQLTEST_VARDIR';
 ERROR 42000: Variable 'general_log_file' can't be set to the value of 'MYSQLTEST_VARDIR'
 set global general_log_file='';
@@ -153,8 +163,10 @@ select * from mysql.general_log;
 event_time	user_host	thread_id	server_id	command_type	argument
 TIMESTAMP	USER_HOST	#	1	Query	drop table t1
 TIMESTAMP	USER_HOST	#	1	Query	select * from mysql.general_log
-SET @old_general_log_state = @@global.general_log;
-SET @old_slow_log_state = @@global.slow_query_log;
+SET @@global.general_log = @old_general_log;
+SET @@global.general_log_file = @old_general_log_file;
+SET @@global.slow_query_log = @old_slow_query_log;
+SET @@global.slow_query_log_file = @old_slow_query_log_file;
 SET GLOBAL general_log = ON;
 SET GLOBAL slow_query_log = ON;
 FLUSH TABLES WITH READ LOCK;
@@ -173,10 +185,9 @@ SET GLOBAL READ_ONLY = ON;
 SET GLOBAL general_log = ON;
 SET GLOBAL slow_query_log = ON;
 SET GLOBAL READ_ONLY = OFF;
-SET GLOBAL general_log = @old_general_log_state;
-SET GLOBAL slow_query_log = @old_slow_log_state;
-SET @old_general_log_state = @@global.general_log;
-SET @old_slow_log_state = @@global.slow_query_log;
+SET GLOBAL general_log = @old_general_log;
+SET GLOBAL slow_query_log = @old_slow_query_log;
+SET GLOBAL general_log = ON;
 SHOW VARIABLES LIKE 'general_log';
 Variable_name	Value
 general_log	ON
@@ -239,29 +250,24 @@ log_slow_queries	ON
 SELECT @@slow_query_log, @@log_slow_queries;
 @@slow_query_log	@@log_slow_queries
 1	1
-SET GLOBAL general_log = @old_general_log_state;
-SET GLOBAL slow_query_log = @old_slow_log_state;
-set @old_general_log_file= @@global.general_log_file;
-set @old_slow_query_log_file= @@global.slow_query_log_file;
-set global general_log_file= concat('/not exiting path/log.maste', 'r');
-ERROR 42000: Variable 'general_log_file' can't be set to the value of '/not exiting path/log.master'
-set global general_log_file= NULL;
+SET GLOBAL general_log = @old_general_log;
+SET GLOBAL slow_query_log = @old_slow_query_log;
+SET GLOBAL general_log_file= CONCAT('/not existing path/log.maste', 'r');
+ERROR 42000: Variable 'general_log_file' can't be set to the value of '/not existing path/log.master'
+SET GLOBAL general_log_file= NULL;
 ERROR 42000: Variable 'general_log_file' can't be set to the value of 'NULL'
-set global slow_query_log_file= concat('/not exiting path/log.maste', 'r');
-ERROR 42000: Variable 'slow_query_log_file' can't be set to the value of '/not exiting path/log.master'
-set global slow_query_log_file= NULL;
+SET GLOBAL slow_query_log_file= CONCAT('/not existing path/log.maste', 'r');
+ERROR 42000: Variable 'slow_query_log_file' can't be set to the value of '/not existing path/log.master'
+SET GLOBAL slow_query_log_file= NULL;
 ERROR 42000: Variable 'slow_query_log_file' can't be set to the value of 'NULL'
-set global general_log_file= @old_general_log_file;
-set global slow_query_log_file= @old_slow_query_log_file;
+SET GLOBAL general_log_file= @old_general_log_file;
+SET GLOBAL slow_query_log_file= @old_slow_query_log_file;
 
 # --
 # -- Bug#32748: Inconsistent handling of assignments to
-# -- general_log_file/slow_query_log_file.
+# --            general_log_file/slow_query_log_file.
 # --
 
-SET @general_log_file_saved = @@global.general_log_file;
-SET @slow_query_log_file_saved = @@global.slow_query_log_file;
-
 SET GLOBAL general_log_file = 'bug32748.query.log';
 SET GLOBAL slow_query_log_file = 'bug32748.slow.log';
 
@@ -270,8 +276,8 @@ Variable_name	Value
 general_log_file	bug32748.query.log
 slow_query_log_file	bug32748.slow.log
 
-SET GLOBAL general_log_file = @general_log_file_saved;
-SET GLOBAL slow_query_log_file = @slow_query_log_file_saved;
+SET GLOBAL general_log_file = @old_general_log_file;
+SET GLOBAL slow_query_log_file = @old_slow_query_log_file;
 
 # -- End of Bug#32748.
 deprecated:
@@ -298,4 +304,13 @@ SET GLOBAL general_log_file = @my_glf;
 SET GLOBAL slow_query_log_file = @my_sqlf;
 SET GLOBAL general_log = DEFAULT;
 SET GLOBAL slow_query_log = DEFAULT;
+SET @@global.general_log = @old_general_log;
+SET @@global.general_log_file = @old_general_log_file;
+SET @@global.slow_query_log = @old_slow_query_log;
+SET @@global.slow_query_log_file = @old_slow_query_log_file;
 End of 5.1 tests
+# Close connection con1
+SET global general_log = @old_general_log;
+SET global general_log_file = @old_general_log_file;
+SET global slow_query_log = @old_slow_query_log;
+SET global slow_query_log_file = @old_slow_query_log_file;
diff --git a/mysql-test/t/log_state.test b/mysql-test/t/log_state.test
index 977b74aa1e3..e8ff60a8637 100644
--- a/mysql-test/t/log_state.test
+++ b/mysql-test/t/log_state.test
@@ -1,6 +1,28 @@
--- source include/not_embedded.inc
+### t/log_state.test ###
+#
+# This test suffers from server
+# Bug#38124 "general_log_file" variable silently unset when using expression
+# In short:
+#    SET GLOBAL general_log_file = @<whatever>
+#    SET GLOBAL slow_query_log = @<whatever>
+# cause that the value of these server system variables is set to default
+# instead of the assigned values. There comes no error message or warning.
+# If this bug is fixed please
+# 1. try this test with "let $fixed_bug38124 = 0;"
+# 2. remove all workarounds if 1. was successful.
+let $fixed_bug38124 = 0;
+
+--source include/not_embedded.inc
 --source include/have_csv.inc
 
+# Several subtests modify global variables. Save the initial values only here,
+# but reset to the initial values per subtest.
+SET @old_general_log= @@global.general_log;
+SET @old_general_log_file= @@global.general_log_file;
+SET @old_slow_query_log= @@global.slow_query_log;
+SET @old_slow_query_log_file= @@global.slow_query_log_file;
+
+
 --disable_ps_protocol
 set global general_log= OFF;
 truncate table mysql.general_log;
@@ -23,20 +45,32 @@ show global variables
 where Variable_name = 'log' or Variable_name = 'log_slow_queries' or
 Variable_name = 'general_log' or Variable_name = 'slow_query_log';
 
+--echo # Establish connection con1 (user=root)
 connect (con1,localhost,root,,);
+--echo # Switch to connection con1
 connection con1;
-set session long_query_time=1;
-select sleep(2);
+# Please increase @long_query_time if the corresponding selects show an
+# additional unexpected entry like
+#    start_time ... sql_text
+#    TIMESTAMP  ... set session long_query_time=...
+# (Bug#40377 sporadic pushbuild failure in log_state: result mismatch)
+--replace_result 2 <long_query_time>
+set @long_query_time = 2;
+set session long_query_time = @long_query_time;
+select sleep(@long_query_time + 1);
 --replace_column 1 TIMESTAMP 2 USER_HOST 3 QUERY_TIME
 select * from mysql.slow_log where sql_text NOT LIKE '%slow_log%';
+--echo # Switch to connection default
 connection default;
 
 set global slow_query_log= ON;
+--echo # Switch to connection con1
 connection con1;
-set session long_query_time=1;
-select sleep(2);
+set session long_query_time = @long_query_time;
+select sleep(@long_query_time + 1);
 --replace_column 1 TIMESTAMP 2 USER_HOST 3 QUERY_TIME
 select * from mysql.slow_log where sql_text NOT LIKE '%slow_log%';
+--echo # Switch to connection default
 connection default;
 show global variables
 where Variable_name = 'log' or Variable_name = 'log_slow_queries' or
@@ -71,16 +105,16 @@ show variables like 'slow_query_log_file';
 show variables like 'log_output';
 
 # Can't set general_log_file to a non existing file
---error 1231
-set global general_log_file='/not exiting path/log.master';
+--error ER_WRONG_VALUE_FOR_VAR
+set global general_log_file='/not existing path/log.master';
 
 # Can't set general_log_file to a directory
 --replace_result $MYSQLTEST_VARDIR MYSQLTEST_VARDIR
---error 1231
+--error ER_WRONG_VALUE_FOR_VAR
 eval set global general_log_file='$MYSQLTEST_VARDIR';
 
 # Can't set general_log_file to empty string
---error 1231
+--error ER_WRONG_VALUE_FOR_VAR
 set global general_log_file='';
 
 --replace_column 2 #
@@ -126,17 +160,30 @@ drop table t1;
 --replace_column 1 TIMESTAMP 2 USER_HOST 3 #
 select * from mysql.general_log;
 
-#
-# Bug#29129 (Resetting general_log while the GLOBAL READ LOCK is set causes
-# a deadlock)
+# Reset to initial values
+SET @@global.general_log = @old_general_log;
+SET @@global.general_log_file = @old_general_log_file;
+SET @@global.slow_query_log = @old_slow_query_log;
+SET @@global.slow_query_log_file = @old_slow_query_log_file;
+if(!$fixed_bug38124)
+{
+   --disable_query_log
+   let $my_var = `SELECT @old_general_log_file`;
+   eval SET @@global.general_log_file = '$my_var';
+   let $my_var = `SELECT @old_slow_query_log_file`;
+   eval SET @@global.slow_query_log_file = '$my_var';
+   --enable_query_log
+}
 
-# save state
 
-SET @old_general_log_state = @@global.general_log;
-SET @old_slow_log_state = @@global.slow_query_log;
+###########################################################################
 
-# Test ON->OFF transition under a GLOBAL READ LOCK
+#
+# Bug#29129 (Resetting general_log while the GLOBAL READ LOCK is set causes
+#            a deadlock)
+#
 
+# Test ON->OFF transition under a GLOBAL READ LOCK
 SET GLOBAL general_log = ON;
 SET GLOBAL slow_query_log = ON;
 
@@ -148,7 +195,6 @@ SET GLOBAL slow_query_log = OFF;
 UNLOCK TABLES;
 
 # Test OFF->ON transition under a GLOBAL READ LOCK
-
 FLUSH TABLES WITH READ LOCK;
 
 SET GLOBAL general_log = ON;
@@ -157,7 +203,6 @@ SET GLOBAL slow_query_log = ON;
 UNLOCK TABLES;
 
 # Test ON->OFF transition under a GLOBAL READ_ONLY
-
 SET GLOBAL READ_ONLY = ON;
 
 SET GLOBAL general_log = OFF;
@@ -166,7 +211,6 @@ SET GLOBAL slow_query_log = OFF;
 SET GLOBAL READ_ONLY = OFF;
 
 # Test OFF->ON transition under a GLOBAL READ_ONLY
-
 SET GLOBAL READ_ONLY = ON;
 
 SET GLOBAL general_log = ON;
@@ -174,17 +218,18 @@ SET GLOBAL slow_query_log = ON;
 
 SET GLOBAL READ_ONLY = OFF;
 
-# Restore state
+# Reset to initial values
+SET GLOBAL general_log = @old_general_log;
+SET GLOBAL slow_query_log = @old_slow_query_log;
+
 
-SET GLOBAL general_log = @old_general_log_state;
-SET GLOBAL slow_query_log = @old_slow_log_state;
+###########################################################################
 
 #
-# Bug #29131: SHOW VARIABLES reports variable 'log' but SET doesn't recognize it
+# Bug#29131: SHOW VARIABLES reports variable 'log' but SET doesn't recognize it
 #
 
-SET @old_general_log_state = @@global.general_log;
-SET @old_slow_log_state = @@global.slow_query_log;
+SET GLOBAL general_log = ON;
 
 SHOW VARIABLES LIKE 'general_log';
 SHOW VARIABLES LIKE 'log';
@@ -210,39 +255,47 @@ SHOW VARIABLES LIKE 'slow_query_log';
 SHOW VARIABLES LIKE 'log_slow_queries';
 SELECT @@slow_query_log, @@log_slow_queries;
 
-SET GLOBAL general_log = @old_general_log_state;
-SET GLOBAL slow_query_log = @old_slow_log_state;
+SET GLOBAL general_log = @old_general_log;
+SET GLOBAL slow_query_log = @old_slow_query_log;
+
+
+###########################################################################
 
 #
-# Bug #31604: server crash when setting slow_query_log_file/general_log_file
+# Bug#31604: server crash when setting slow_query_log_file/general_log_file
 #
-set @old_general_log_file= @@global.general_log_file;
-set @old_slow_query_log_file= @@global.slow_query_log_file;
 
---error 1231
-set global general_log_file= concat('/not exiting path/log.maste', 'r');
---error 1231
-set global general_log_file= NULL;
---error 1231
-set global slow_query_log_file= concat('/not exiting path/log.maste', 'r');
---error 1231
-set global slow_query_log_file= NULL;
+--error ER_WRONG_VALUE_FOR_VAR
+SET GLOBAL general_log_file= CONCAT('/not existing path/log.maste', 'r');
+--error ER_WRONG_VALUE_FOR_VAR
+SET GLOBAL general_log_file= NULL;
+--error ER_WRONG_VALUE_FOR_VAR
+SET GLOBAL slow_query_log_file= CONCAT('/not existing path/log.maste', 'r');
+--error ER_WRONG_VALUE_FOR_VAR
+SET GLOBAL slow_query_log_file= NULL;
+
+# Reset to initial values in case a setting above was successful.
+SET GLOBAL general_log_file= @old_general_log_file;
+SET GLOBAL slow_query_log_file= @old_slow_query_log_file;
+if(!$fixed_bug38124)
+{
+   --disable_query_log
+   let $my_var = `SELECT @old_general_log_file`;
+   eval SET @@global.general_log_file = '$my_var';
+   let $my_var = `SELECT @old_slow_query_log_file`;
+   eval SET @@global.slow_query_log_file = '$my_var';
+   --enable_query_log
+}
 
-set global general_log_file= @old_general_log_file;
-set global slow_query_log_file= @old_slow_query_log_file;
 
 ###########################################################################
 
 --echo
 --echo # --
 --echo # -- Bug#32748: Inconsistent handling of assignments to
---echo # -- general_log_file/slow_query_log_file.
+--echo # --            general_log_file/slow_query_log_file.
 --echo # --
 
---echo
-SET @general_log_file_saved = @@global.general_log_file;
-SET @slow_query_log_file_saved = @@global.slow_query_log_file;
-
 --echo
 SET GLOBAL general_log_file = 'bug32748.query.log';
 SET GLOBAL slow_query_log_file = 'bug32748.slow.log';
@@ -250,27 +303,37 @@ SET GLOBAL slow_query_log_file = 'bug32748.slow.log';
 --echo
 SHOW VARIABLES LIKE '%log_file';
 
+# Reset to initial values
 --echo
-SET GLOBAL general_log_file = @general_log_file_saved;
-SET GLOBAL slow_query_log_file = @slow_query_log_file_saved;
+SET GLOBAL general_log_file = @old_general_log_file;
+SET GLOBAL slow_query_log_file = @old_slow_query_log_file;
+if(!$fixed_bug38124)
+{
+   --disable_query_log
+   let $my_var = `SELECT @old_general_log_file`;
+   eval SET @@global.general_log_file = '$my_var';
+   let $my_var = `SELECT @old_slow_query_log_file`;
+   eval SET @@global.slow_query_log_file = '$my_var';
+   --enable_query_log
+}
 
 --echo
 --echo # -- End of Bug#32748.
 
-###########################################################################
-
 
+###########################################################################
 
-## WL#4403 - deprecate @log and @slow_log_queries variables
+# WL#4403 - deprecate @log and @slow_log_queries variables
 
-## these are all deprecated -- show for command-line as well!
+# These server system variables are all deprecated
+# -- show for command-line as well!
 --echo deprecated:
 SET GLOBAL log = 0;
 SET GLOBAL log_slow_queries = 0;
 SET GLOBAL log = DEFAULT;
 SET GLOBAL log_slow_queries = DEFAULT;
 
-## these are NOT deprecated
+# These server system variables are NOT deprecated.
 --echo not deprecated:
 SELECT @@global.general_log_file INTO @my_glf;
 SELECT @@global.slow_query_log_file INTO @my_sqlf;
@@ -283,6 +346,20 @@ SET GLOBAL slow_query_log_file = @my_sqlf;
 SET GLOBAL general_log = DEFAULT;
 SET GLOBAL slow_query_log = DEFAULT;
 
+## Reset to initial values
+SET @@global.general_log = @old_general_log;
+SET @@global.general_log_file = @old_general_log_file;
+SET @@global.slow_query_log = @old_slow_query_log;
+SET @@global.slow_query_log_file = @old_slow_query_log_file;
+if(!$fixed_bug38124)
+{
+   --disable_query_log
+   let $my_var = `SELECT @old_general_log_file`;
+   eval SET @@global.general_log_file = '$my_var';
+   let $my_var = `SELECT @old_slow_query_log_file`;
+   eval SET @@global.slow_query_log_file = '$my_var';
+   --enable_query_log
+}
 
 
 --echo End of 5.1 tests
@@ -290,10 +367,27 @@ SET GLOBAL slow_query_log = DEFAULT;
 --enable_ps_protocol
 
 #
-# Cleanup (must be done last to avoid delayed 'Quit' message in general log)
+# Cleanup
 #
+# Disconnect must be done last to avoid delayed 'Quit' message in general log
+--echo # Close connection con1
 disconnect con1;
+# Reset global system variables to initial values if forgotten somewhere above.
+SET global general_log = @old_general_log;
+SET global general_log_file = @old_general_log_file;
+SET global slow_query_log = @old_slow_query_log;
+SET global slow_query_log_file = @old_slow_query_log_file;
+if(!$fixed_bug38124)
+{
+   --disable_query_log
+   let $my_var = `SELECT @old_general_log_file`;
+   eval SET @@global.general_log_file = '$my_var';
+   let $my_var = `SELECT @old_slow_query_log_file`;
+   eval SET @@global.slow_query_log_file = '$my_var';
+   --enable_query_log
+}
 
 # Remove the log files that was created in the "default location"
 # i.e var/run
 --remove_file $MYSQLTEST_VARDIR/run/master.log
+--remove_file $MYSQLTEST_VARDIR/tmp/log.master
-- 
2.30.9