summaryrefslogtreecommitdiff
path: root/sql/log.cc
diff options
context:
space:
mode:
authorunknown <gluh@gluh.mysql.r18.ru>2003-02-16 20:39:12 +0400
committerunknown <gluh@gluh.mysql.r18.ru>2003-02-16 20:39:12 +0400
commite1cf7fb3a859c712a1df84a5cc82138a88472b59 (patch)
tree6731513d8cd2f0ea43a356705e4c70c1760d53aa /sql/log.cc
parentf993a29ef1069dc841e3f74d4227672cc2c90c55 (diff)
downloadmariadb-git-e1cf7fb3a859c712a1df84a5cc82138a88472b59.tar.gz
SCRUM: 'Replication: PURGE LOGS with date' task
Added: PURGE [MASTER] LOGS BEFORE date/date_expression expire-logs-days option With this option old files are deleted when - mysqld is started - log is rotated - someone does FLUSH LOGS sql/lex.h: For PURGE LOGS BEFORE sql/log.cc: Added purge_logs_before_date to MYSQL_LOG class sql/mysql_priv.h: For startup option --expire-logs-days sql/mysqld.cc: Added options expire-logs-days When mysqld is started old log files are deleted. sql/sql_class.h: For PURGE LOGS BEFORE and expire-log-days option sql/sql_lex.h: For PURGE LOGS BEFORE sql/sql_parse.cc: Old logs are deleted when someone does FLUSH LOGS sql/sql_repl.cc: For PURGE LOGS BEFORE and expire-log-days option sql/sql_repl.h: For PURGE LOGS BEFORE and expire-log-days option sql/sql_yacc.yy: For PURGE LOGS BEFORE
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);
}