summaryrefslogtreecommitdiff
path: root/sql/log.cc
diff options
context:
space:
mode:
Diffstat (limited to 'sql/log.cc')
-rw-r--r--sql/log.cc81
1 files changed, 80 insertions, 1 deletions
diff --git a/sql/log.cc b/sql/log.cc
index 5dcb5857026..b179788ea14 100644
--- a/sql/log.cc
+++ b/sql/log.cc
@@ -746,6 +746,78 @@ err:
DBUG_RETURN(error);
}
+/*
+ Remove all logs before the given file date from disk and from the
+ index file.
+
+ SYNOPSIS
+ purge_logs_before_date()
+ thd Thread pointer
+ before_date Delete all log files before given date.
+
+ NOTES
+ If any of the logs before the deleted one is in use,
+ only purge logs up to this one.
+
+ RETURN VALUES
+ 0 ok
+ LOG_INFO_PURGE_NO_ROTATE Binary file that can't be rotated
+*/
+
+int MYSQL_LOG::purge_logs_before_date(THD* thd, time_t purge_time)
+{
+ int error;
+ LOG_INFO log_info;
+ MY_STAT stat_area;
+
+ DBUG_ENTER("purge_logs_before_date");
+
+ if (no_rotate)
+ DBUG_RETURN(LOG_INFO_PURGE_NO_ROTATE);
+
+ pthread_mutex_lock(&LOCK_index);
+
+ /*
+ Delete until we find curren file
+ or a file that is used or a file
+ that is older than purge_time.
+ */
+ if ((error=find_log_pos(&log_info, NullS, 0 /*no mutex*/)))
+ goto err;
+
+ while (strcmp(log_file_name, log_info.log_file_name) &&
+ !log_in_use(log_info.log_file_name))
+ {
+ /* It's not fatal even if we can't delete a log file */
+ if (my_stat(log_info.log_file_name, &stat_area, MYF(0)) &&
+ stat_area.st_mtime < purge_time)
+ my_delete(log_info.log_file_name, MYF(0));
+ else
+ break;
+ if (find_next_log(&log_info, 0))
+ break;
+ }
+
+ /*
+ If we get killed -9 here, the sysadmin would have to edit
+ the log index file after restart - otherwise, this should be safe
+ */
+
+ if (copy_up_file_and_fill(&index_file, log_info.index_file_start_offset))
+ {
+ error= LOG_INFO_IO;
+ goto err;
+ }
+
+ // now update offsets in index file for running threads
+ adjust_linfo_offsets(log_info.index_file_start_offset);
+
+err:
+ pthread_mutex_unlock(&LOCK_index);
+ DBUG_RETURN(error);
+}
+
+
/*
Create a new log file name
@@ -1033,6 +1105,7 @@ bool MYSQL_LOG::write(THD *thd,enum enum_server_command command,
bool MYSQL_LOG::write(Log_event* event_info)
{
bool error=0;
+ bool should_rotate = 0;
DBUG_ENTER("MYSQL_LOG::write(event)");
if (!inited) // Can't use mutex if not init
@@ -1045,7 +1118,6 @@ bool MYSQL_LOG::write(Log_event* event_info)
/* In most cases this is only called if 'is_open()' is true */
if (is_open())
{
- bool should_rotate = 0;
THD *thd=event_info->thd;
const char *local_db = event_info->get_db();
#ifdef USING_TRANSACTIONS
@@ -1163,6 +1235,13 @@ err:
}
pthread_mutex_unlock(&LOCK_log);
+ if (should_rotate && ~expire_logs_days)
+ {
+ long purge_time= time(0) - expire_logs_days*24*60*60;
+ if (purge_time >= 0)
+ error= purge_logs_before_date(current_thd, purge_time);
+ }
+
DBUG_RETURN(error);
}