1#
2# BUG#23300: Slow query log on slave does not log slow replicated statements
3#
4# Description:
5#   The slave should log slow queries replicated from master when
6#   --log-slow-slave-statements is used.
7#
8#   Test is implemented as follows:
9#      i) stop slave
10#     ii) On slave, set long_query_time to a small value.
11#     ii) start slave so that long_query_time variable is picked by sql thread
12#    iii) On master, do one short time query and one long time query, on slave
13#         and check that slow query is logged to slow query log
14#     iv) On slave, check that slow queries go into the slow log, when issued
15#         through a regular client connection
16#      v) On slave, check that slow queries go into the slow log when we use
17#         SET TIMESTAMP= 1 on a regular client connection.
18#     vi) check that when setting slow_query_log= OFF in a connection 'extra2'
19#         prevents logging slow queries in a connection 'extra'
20#
21# OBS:
22#   This test only runs for statement binlogging format because on row
23#   format slow queries do not get slow query logged. Due to the
24#   sleep() command, the insert would be binlogged in row format if
25#   binlog_format=MIXED. Hence, we do not use binlog_format=MIXED.
26
27source include/master-slave.inc;
28source include/have_binlog_format_statement.inc;
29
30CALL mtr.add_suppression("Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT");
31call mtr.add_suppression("Slave SQL.*Request to stop slave SQL Thread received while applying a group that has non-transactional changes; waiting for completion of the group");
32
33# Prepare slave for different long_query_time we need to stop the slave
34# and restart it as long_query_time variable is dynamic and, after
35# setting it, it only takes effect on new connections.
36#
37# Reference:
38#   http://dev.mysql.com/doc/refman/6.0/en/set-option.html
39connection slave;
40
41source include/stop_slave.inc;
42
43SET @old_log_output= @@log_output;
44SET GLOBAL log_output= 'TABLE';
45SET @old_long_query_time= @@long_query_time;
46SET GLOBAL long_query_time= 2;
47TRUNCATE mysql.slow_log;
48
49source include/start_slave.inc;
50
51connection master;
52CREATE TABLE t1 (a int, b int);
53
54# test:
55#   check that slave logs the slow query to the slow log
56
57let $slow_query= INSERT INTO t1 values(1, sleep(3));
58let $fast_query= INSERT INTO t1 values(1, 1);
59
60eval $fast_query;
61--disable_warnings
62eval $slow_query;
63--enable_warnings
64--source include/sync_slave_sql_with_master.inc
65
66let $found_slow_query= `SELECT count(*) = 1 FROM mysql.slow_log WHERE sql_text like '$slow_query'`;
67
68if (!$found_slow_query)
69{
70  SELECT * FROM mysql.slow_log;
71  die "Assertion failed! Slow query NOT FOUND in slow query log. Bailing out!";
72}
73TRUNCATE mysql.slow_log;
74
75# regular checks for slow query log (using a new connection - 'extra' - to slave)
76
77# test:
78#   when using direct connections to the slave, check that slow query is logged
79
80connect(extra,127.0.0.1,root,,test,$SLAVE_MYPORT);
81connection extra;
82
83let $fast_query= SELECT 1;
84let $slow_query= SELECT 1, sleep(3);
85
86eval $slow_query;
87eval $fast_query;
88
89let $found_slow_query= `SELECT count(*) = 1 FROM mysql.slow_log WHERE sql_text like '$slow_query'`;
90
91if (!$found_slow_query)
92{
93  SELECT * FROM mysql.slow_log;
94  die "Assertion failed! Slow query NOT FOUND in slow query log. Bailing out!";
95}
96TRUNCATE mysql.slow_log;
97
98# test:
99#   when using direct connections to the slave, check that when setting timestamp to 1 the
100#   slow query is logged.
101
102let $fast_query= SELECT 2;
103let $slow_query= SELECT 2, sleep(3);
104
105SET TIMESTAMP= 1;
106eval $slow_query;
107eval $fast_query;
108
109let $found_slow_query= `SELECT count(*) = 1 FROM mysql.slow_log WHERE sql_text like '$slow_query'`;
110
111if (!$found_slow_query)
112{
113  SELECT * FROM mysql.slow_log;
114  die "Assertion failed! Slow query NOT FOUND in slow query log. Bailing out!";
115}
116TRUNCATE mysql.slow_log;
117
118# test:
119#   check that when setting the slow_query_log= OFF on connection 'extra2'
120#   prevents connection 'extra' from logging to slow query log.
121
122let $fast_query= SELECT 3;
123let $slow_query= SELECT 3, sleep(3);
124
125connect(extra2,127.0.0.1,root,,test,$SLAVE_MYPORT);
126connection extra2;
127
128SET @old_slow_query_log= @@slow_query_log;
129SET GLOBAL slow_query_log= 'OFF';
130
131connection extra;
132
133eval $slow_query;
134eval $fast_query;
135
136let $found_fast_query= `SELECT count(*) = 1 FROM mysql.slow_log WHERE sql_text like '$fast_query'`;
137let $found_slow_query= `SELECT count(*) = 1 FROM mysql.slow_log WHERE sql_text like '$slow_query'`;
138
139if ($found_fast_query)
140{
141  SELECT * FROM mysql.slow_log;
142  die "Assertion failed! Fast query FOUND in slow query log when slow_query_log= OFF. Bailing out!";
143}
144
145if ($found_slow_query)
146{
147  SELECT * FROM mysql.slow_log;
148  die "Assertion failed! Slow query FOUND in slow query log when slow_query_log= OFF. Bailing out!";
149}
150TRUNCATE mysql.slow_log;
151
152# clean up: drop tables, reset the variables back to the previous value,
153#           disconnect extra connections
154connection extra2;
155
156SET GLOBAL slow_query_log= @old_slow_query_log;
157
158connection master;
159DROP TABLE t1;
160--source include/sync_slave_sql_with_master.inc
161
162source include/stop_slave.inc;
163
164SET GLOBAL long_query_time= @old_long_query_time;
165SET GLOBAL log_output= @old_log_output;
166
167source include/start_slave.inc;
168
169disconnect extra;
170disconnect extra2;
171
172#
173# BUG#50620: Adding an index to a table prevents slave from logging into slow log
174#
175
176--source include/rpl_reset.inc
177
178-- connection master
179SET @old_log_output= @@log_output;
180SET GLOBAL log_output= 'TABLE';
181SET GLOBAL long_query_time= 2;
182SET @old_long_query_time= @@long_query_time;
183SET SESSION long_query_time= 2;
184TRUNCATE mysql.slow_log;
185-- connection slave
186
187-- source include/stop_slave.inc
188SET @old_log_output= @@log_output;
189SET GLOBAL log_output= 'TABLE';
190SET @old_long_query_time= @@long_query_time;
191SET GLOBAL long_query_time= 2;
192TRUNCATE mysql.slow_log;
193-- source include/start_slave.inc
194
195let $slow_query= INSERT INTO t1 values(1, sleep(3));
196
197-- connection master
198CREATE TABLE t1 (a int, b int);
199
200-- echo ********************************************************************
201-- echo **** INSERT one row that exceeds long_query_time
202-- echo **** Outcome: query ends up in both master and slave slow log
203-- echo ********************************************************************
204
205-- disable_warnings
206-- eval $slow_query
207-- enable_warnings
208
209let $master_slow_query= `SELECT count(*) = 1 FROM mysql.slow_log WHERE sql_text like '$slow_query'`;
210--source include/sync_slave_sql_with_master.inc
211let $slave_slow_query= `SELECT count(*) = 1 FROM mysql.slow_log WHERE sql_text like '$slow_query'`;
212
213if ($master_slow_query != $slave_slow_query)
214{
215  -- connection master
216  -- echo ***********************************************
217  -- echo ** DUMPING MASTER SLOW LOG CONTENTS
218  -- echo ***********************************************
219  SELECT * FROM mysql.slow_log;
220
221  -- connection slave
222  -- echo ***********************************************
223  -- echo ** DUMPING SLAVE SLOW LOG CONTENTS
224  -- echo ***********************************************
225  SELECT * FROM mysql.slow_log;
226
227  -- die "Assertion failed! Master and slave slow log contents differ. Bailing out!"
228}
229
230if ($master_slow_query == $slave_slow_query)
231{
232  -- echo ### Assertion is good. Both Master and Slave exhibit the
233  -- echo ### same number of queries in slow log: $master_slow_query
234}
235
236TRUNCATE mysql.slow_log;
237-- connection master
238TRUNCATE mysql.slow_log;
239
240-- echo ********************************************************************
241-- echo **** Now do inserts again, but first add an index to the table.
242-- echo **** Outcome: Note that the slave contains the same one entry (as
243-- echo ****          the master does)  whereas before the patch it did not.
244-- echo ********************************************************************
245
246ALTER TABLE t1 ADD INDEX id1(a);
247
248-- disable_warnings
249-- eval $slow_query
250-- enable_warnings
251
252let $master_slow_query= `SELECT count(*) = 1 FROM mysql.slow_log WHERE sql_text like '$slow_query'`;
253--source include/sync_slave_sql_with_master.inc
254let $slave_slow_query= `SELECT count(*) = 1 FROM mysql.slow_log WHERE sql_text like '$slow_query'`;
255
256if ($master_slow_query != $slave_slow_query)
257{
258  -- connection master
259  -- echo ***********************************************
260  -- echo ** DUMPING MASTER SLOW LOG CONTENTS
261  -- echo ***********************************************
262  SELECT * FROM mysql.slow_log;
263
264  -- connection slave
265  -- echo ***********************************************
266  -- echo ** DUMPING SLAVE SLOW LOG CONTENTS
267  -- echo ***********************************************
268  SELECT * FROM mysql.slow_log;
269
270  -- die "Assertion failed! Master and slave slow log contents differ. Bailing out!"
271}
272
273if ($master_slow_query == $slave_slow_query)
274{
275  -- echo ### Assertion is good. Both Master and Slave exhibit the
276  -- echo ### same number of queries in slow log: $master_slow_query
277}
278
279-- echo ********************************************************************
280-- echo **** TRUNCATE the slow log then check whether runtime changes of
281-- echo **** log_slow_slave_statements work without slave restart.
282-- echo ********************************************************************
283
284SET @old_log_slow_slave_statements= @@global.log_slow_slave_statements;
285SET @@global.log_slow_slave_statements = off;
286TRUNCATE mysql.slow_log;
287
288-- connection master
289
290--disable_warnings
291-- eval $slow_query;
292--enable_warnings
293
294--source include/sync_slave_sql_with_master.inc
295-- connection slave
296
297let $slave_slow_query= `SELECT count(*) = 1 FROM mysql.slow_log WHERE sql_text like '$slow_query'`;
298if ($slave_slow_query)
299{
300  SELECT * FROM mysql.slow_log;
301  die "Assertion failed! Slow query FOUND in slow query log. Bailing out!";
302}
303
304SET @@global.log_slow_slave_statements = on;
305
306-- connection master
307
308--disable_warnings
309-- eval $slow_query;
310--enable_warnings
311
312--source include/sync_slave_sql_with_master.inc
313-- connection slave
314
315let $slave_slow_query= `SELECT count(*) = 1 FROM mysql.slow_log WHERE sql_text like '$slow_query'`;
316if (!$slave_slow_query)
317{
318  SELECT * FROM mysql.slow_log;
319  die "Assertion failed! Slow query NOT FOUND in slow query log. Bailing out!";
320}
321
322-- connection master
323SET @@global.log_output= @old_log_output;
324SET @@global.long_query_time= @old_long_query_time;
325DROP TABLE t1;
326
327--source include/sync_slave_sql_with_master.inc
328SET @@global.log_output= @old_log_output;
329SET @@global.long_query_time= @old_long_query_time;
330SET @@global.log_slow_slave_statements= @old_log_slow_slave_statements;
331--source include/rpl_end.inc
332