Skip to content

Commit

Permalink
Merge branch '5.5-ps-bug1416582' into 5.6-ps-bug1416582
Browse files Browse the repository at this point in the history
* 5.5-ps-bug1416582:
  Bug 1416582: Slow query log is rotated before it should when using max_slowlog_size
  • Loading branch information
gl-sergei committed May 26, 2015
2 parents 526c218 + 3370948 commit 85c461c
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 27 deletions.
21 changes: 21 additions & 0 deletions mysql-test/r/percona_bug1416582.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
SET @old_max_slowlog_size = @@global.max_slowlog_size;
SET @old_slow_query_log = @@global.slow_query_log;
SET @old_slow_query_log_file = @@global.slow_query_log_file;
SET @old_long_query_time = @@long_query_time;
SET GLOBAL max_slowlog_size=10240000;
SET long_query_time=0;
SET GLOBAL slow_query_log_file='MYSQLTEST_VARDIR/abcd';
SET GLOBAL slow_query_log=1;
SET GLOBAL slow_query_log=0;
SET GLOBAL slow_query_log=1;
SET GLOBAL slow_query_log=0;
SET GLOBAL slow_query_log=1;
FLUSH LOGS;
FLUSH LOGS;
FLUSH LOGS;
slow query log files count:
1
SET @@global.max_slowlog_size = @old_max_slowlog_size;
SET @@global.slow_query_log = @old_slow_query_log;
SET @@global.slow_query_log_file = @old_slow_query_log_file;
SET @@long_query_time = @old_long_query_time;
38 changes: 38 additions & 0 deletions mysql-test/t/percona_bug1416582.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#
# Bug 1416582: Slow query log is rotated before it should
# when using max_slowlog_size
#

SET @old_max_slowlog_size = @@global.max_slowlog_size;
SET @old_slow_query_log = @@global.slow_query_log;
SET @old_slow_query_log_file = @@global.slow_query_log_file;
SET @old_long_query_time = @@long_query_time;
SET GLOBAL max_slowlog_size=10240000;
SET long_query_time=0;

--replace_result $MYSQLTEST_VARDIR MYSQLTEST_VARDIR
eval SET GLOBAL slow_query_log_file='$MYSQLTEST_VARDIR/abcd';

# start/stop slog log should not cause log number increment
SET GLOBAL slow_query_log=1;
SET GLOBAL slow_query_log=0;
SET GLOBAL slow_query_log=1;
SET GLOBAL slow_query_log=0;
SET GLOBAL slow_query_log=1;

# FLUSH LOGS should not cause log number increment
FLUSH LOGS;
FLUSH LOGS;
FLUSH LOGS;

--echo slow query log files count:
perl;
my @files = <$ENV{'MYSQLTEST_VARDIR'}/abcd*>;
printf "%d\n", scalar(@files);
EOF


SET @@global.max_slowlog_size = @old_max_slowlog_size;
SET @@global.slow_query_log = @old_slow_query_log;
SET @@global.slow_query_log_file = @old_slow_query_log_file;
SET @@long_query_time = @old_long_query_time;
6 changes: 3 additions & 3 deletions sql/binlog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3268,7 +3268,7 @@ bool MYSQL_BIN_LOG::open_binlog(const char *log_name,
DBUG_PRINT("enter",("name: %s", log_name));

if (init_and_set_log_file_name(log_name, new_name, LOG_BIN,
io_cache_type_arg, true))
io_cache_type_arg))
{
sql_print_error("MYSQL_BIN_LOG::open failed to generate new file name.");
DBUG_RETURN(1);
Expand Down Expand Up @@ -3310,7 +3310,7 @@ bool MYSQL_BIN_LOG::open_binlog(const char *log_name,
#ifdef HAVE_PSI_INTERFACE
m_key_file_log,
#endif
log_name, LOG_BIN, new_name, io_cache_type_arg, true))
log_name, LOG_BIN, new_name, io_cache_type_arg))
{
#ifdef HAVE_REPLICATION
close_purge_index_file();
Expand Down Expand Up @@ -5085,7 +5085,7 @@ int MYSQL_BIN_LOG::new_file_impl(bool need_lock_log, Format_description_log_even
We have to do this here and not in open as we want to store the
new file name in the current binary log file.
*/
if ((error= generate_new_name(new_name, name, log_type == LOG_BIN)))
if ((error= generate_new_name(new_name, name)))
goto end;
else
{
Expand Down
32 changes: 15 additions & 17 deletions sql/log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1418,7 +1418,7 @@ static void setup_windows_event_source()
nonzero if not possible to get unique filename.
*/

static int find_uniq_filename(char *name, ulong *next)
static int find_uniq_filename(char *name, ulong *next, bool need_next)
{
uint i;
char buff[FN_REFLEN], ext_buf[FN_REFLEN];
Expand Down Expand Up @@ -1465,7 +1465,7 @@ updating the index files.", max_found);
goto end;
}

*next= max_found + 1;
*next= (need_next || max_found == 0) ? max_found + 1 : max_found;
if (sprintf(ext_buf, "%06lu", *next) < 0)
{
error= 1;
Expand Down Expand Up @@ -1518,14 +1518,13 @@ void MYSQL_LOG::init(enum_log_type log_type_arg,
bool MYSQL_LOG::init_and_set_log_file_name(const char *log_name,
const char *new_name,
enum_log_type log_type_arg,
enum cache_type io_cache_type_arg,
bool unique)
enum cache_type io_cache_type_arg)
{
init(log_type_arg, io_cache_type_arg);

if (new_name && !strmov(log_file_name, new_name))
return TRUE;
else if (!new_name && generate_new_name(log_file_name, log_name, unique))
else if (!new_name && generate_new_name(log_file_name, log_name))
return TRUE;

return FALSE;
Expand Down Expand Up @@ -1558,8 +1557,7 @@ bool MYSQL_LOG::open(
PSI_file_key log_file_key,
#endif
const char *log_name, enum_log_type log_type_arg,
const char *new_name, enum cache_type io_cache_type_arg,
bool unique)
const char *new_name, enum cache_type io_cache_type_arg)
{
char buff[FN_REFLEN];
MY_STAT f_stat;
Expand All @@ -1578,7 +1576,7 @@ bool MYSQL_LOG::open(
}

if (init_and_set_log_file_name(name, new_name,
log_type_arg, io_cache_type_arg, unique) ||
log_type_arg, io_cache_type_arg) ||
DBUG_EVALUATE_IF("fault_injection_init_name", log_type == LOG_BIN, 0))
goto err;

Expand Down Expand Up @@ -1759,15 +1757,14 @@ void MYSQL_LOG::cleanup()
}


int MYSQL_LOG::generate_new_name(char *new_name, const char *log_name,
bool unique)
int MYSQL_LOG::generate_new_name(char *new_name, const char *log_name)
{
fn_format(new_name, log_name, mysql_data_home, "", 4);
if (unique)
if (log_type == LOG_BIN || max_slowlog_size > 0)
{
if (!fn_ext(log_name)[0])
{
if (find_uniq_filename(new_name, &cur_log_ext))
if (find_uniq_filename(new_name, &cur_log_ext, log_type == LOG_BIN))
{
my_printf_error(ER_NO_UNIQUE_LOGFILE, ER(ER_NO_UNIQUE_LOGFILE),
MYF(ME_FATALERROR), log_name);
Expand Down Expand Up @@ -1838,7 +1835,7 @@ void MYSQL_QUERY_LOG::reopen_file()
#ifdef HAVE_PSI_INTERFACE
m_log_file_key,
#endif
save_name, log_type, 0, io_cache_type, false);
save_name, log_type, 0, io_cache_type);
my_free(save_name);

mysql_mutex_unlock(&LOCK_log);
Expand Down Expand Up @@ -2006,6 +2003,9 @@ bool MYSQL_QUERY_LOG::write(THD *thd, ulonglong current_utime,
char affected_row_buff[21];
end= buff;

if (max_slowlog_size > 0)
error= rotate(max_slowlog_size, &need_purge);

if (!(specialflag & SPECIAL_SHORT_LOG_FORMAT))
{
if (opt_slow_query_log_timestamp_always || current_time != last_time)
Expand Down Expand Up @@ -2211,8 +2211,6 @@ bool MYSQL_QUERY_LOG::write(THD *thd, ulonglong current_utime,
my_strerror(errbuf, sizeof(errbuf), errno));
}
}
if (max_slowlog_size > 0)
error= rotate(max_slowlog_size, &need_purge);
}
ulong save_cur_ext = cur_log_ext;
mysql_mutex_unlock(&LOCK_log);
Expand Down Expand Up @@ -2258,7 +2256,7 @@ int MYSQL_QUERY_LOG::new_file()
if (cur_log_ext == (ulong)-1)
{
strcpy(new_name, name);
if ((error= generate_new_name(new_name, name, true)))
if ((error= generate_new_name(new_name, name)))
goto end;
}
else
Expand Down Expand Up @@ -2286,7 +2284,7 @@ int MYSQL_QUERY_LOG::new_file()
key_file_query_log,
#endif
name,
LOG_NORMAL, new_name, WRITE_CACHE, false);
LOG_NORMAL, new_name, WRITE_CACHE);

my_free(old_name);

Expand Down
12 changes: 5 additions & 7 deletions sql/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,20 +302,18 @@ class MYSQL_LOG
const char *log_name,
enum_log_type log_type,
const char *new_name,
enum cache_type io_cache_type_arg,
bool unique);
enum cache_type io_cache_type_arg);
bool init_and_set_log_file_name(const char *log_name,
const char *new_name,
enum_log_type log_type_arg,
enum cache_type io_cache_type_arg,
bool unique);
enum cache_type io_cache_type_arg);
void init(enum_log_type log_type_arg,
enum cache_type io_cache_type_arg);
void close(uint exiting);
inline bool is_open() { return log_state != LOG_CLOSED; }
const char *generate_name(const char *log_name, const char *suffix,
bool strip_ext, char *buff);
int generate_new_name(char *new_name, const char *log_name, bool unique);
int generate_new_name(char *new_name, const char *log_name);
protected:
/* LOCK_log is inited by init_pthread_objects() */
mysql_mutex_t LOCK_log;
Expand Down Expand Up @@ -390,7 +388,7 @@ class MYSQL_QUERY_LOG: public MYSQL_LOG
key_file_slow_log,
#endif
generate_name(log_name, "-slow.log", 0, buf),
LOG_NORMAL, 0, WRITE_CACHE, max_slowlog_size > 0);
LOG_NORMAL, 0, WRITE_CACHE);
}
bool open_query_log(const char *log_name)
{
Expand All @@ -400,7 +398,7 @@ class MYSQL_QUERY_LOG: public MYSQL_LOG
key_file_query_log,
#endif
generate_name(log_name, ".log", 0, buf),
LOG_NORMAL, 0, WRITE_CACHE, false);
LOG_NORMAL, 0, WRITE_CACHE);
}
int rotate(ulong max_size, bool *need_purge);
int new_file();
Expand Down

0 comments on commit 85c461c

Please sign in to comment.