Commit 92457d6e authored by unknown's avatar unknown

Added 'PURGE LOGS BEFORE' command

Added expire-logs-days option 


mysql-test/r/rpl_rotate_logs.result:
  Added test for 'PURGE LOGS BEFORE'
mysql-test/t/rpl_rotate_logs.test:
  Added test for 'PURGE LOGS BEFORE'
sql/log.cc:
  For 'Purge logs before' command
sql/mysqld.cc:
  Added 'expire-logs-days' option
sql/set_var.cc:
  Added 'expire-logs-days' option
sql/sql_class.h:
  For 'Purge logs before' command
sql/sql_parse.cc:
  Added 'expire-logs-days' option
sql/sql_yacc.yy:
  For 'Purge logs before' command
parent e1cf7fb3
......@@ -40,7 +40,12 @@ set insert_id=1234;
insert into t2 values(NULL);
set global sql_slave_skip_counter=1;
start slave;
purge master logs to 'master-bin.000003';
purge master logs to 'master-bin.000002';
show binary logs;
Log_name
master-bin.000002
master-bin.000003
purge logs before now();
show binary logs;
Log_name
master-bin.000003
......
......@@ -89,7 +89,9 @@ connection master;
#let slave catch up
sync_slave_with_master;
connection master;
purge master logs to 'master-bin.000003';
purge master logs to 'master-bin.000002';
show binary logs;
purge logs before now();
show binary logs;
insert into t2 values (65);
sync_slave_with_master;
......
......@@ -679,6 +679,19 @@ int MYSQL_LOG::purge_first_log(struct st_relay_log_info* rli)
DBUG_RETURN(error);
}
/*
Update log index_file
*/
int MYSQL_LOG::update_log_index(LOG_INFO* log_info)
{
if (copy_up_file_and_fill(&index_file, log_info->index_file_start_offset))
return LOG_INFO_IO;
// now update offsets in index file for running threads
adjust_linfo_offsets(log_info->index_file_start_offset);
return 0;
}
/*
Remove all logs before the given log from disk and from the index file.
......@@ -731,15 +744,7 @@ int MYSQL_LOG::purge_logs(THD* thd, const char* to_log)
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);
error= update_log_index(&log_info);
err:
pthread_mutex_unlock(&LOCK_index);
......@@ -789,11 +794,10 @@ int MYSQL_LOG::purge_logs_before_date(THD* thd, time_t purge_time)
!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
if (!my_stat(log_info.log_file_name, &stat_area, MYF(0)) ||
stat_area.st_mtime >= purge_time)
break;
my_delete(log_info.log_file_name, MYF(0));
if (find_next_log(&log_info, 0))
break;
}
......@@ -802,15 +806,7 @@ int MYSQL_LOG::purge_logs_before_date(THD* thd, time_t purge_time)
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);
error= update_log_index(&log_info);
err:
pthread_mutex_unlock(&LOCK_index);
......@@ -1235,7 +1231,7 @@ bool MYSQL_LOG::write(Log_event* event_info)
}
pthread_mutex_unlock(&LOCK_log);
if (should_rotate && ~expire_logs_days)
if (should_rotate && expire_logs_days)
{
long purge_time= time(0) - expire_logs_days*24*60*60;
if (purge_time >= 0)
......
......@@ -392,7 +392,7 @@ ulong max_connections,max_insert_delayed_threads,max_used_connections,
max_connect_errors, max_user_connections = 0;
ulong thread_id=1L,current_pid;
ulong slow_launch_threads = 0;
ulong expire_logs_days = ~0L;
ulong expire_logs_days = 0;
char mysql_real_data_home[FN_REFLEN],
language[LIBLEN],reg_ext[FN_EXTLEN],
......@@ -2159,7 +2159,7 @@ The server will not act as a slave.");
open_log(&mysql_bin_log, glob_hostname, opt_bin_logname, "-bin",
opt_binlog_index_name,LOG_BIN);
using_update_log=1;
if (~expire_logs_days)
if (expire_logs_days)
{
long purge_time= time(0) - expire_logs_days*24*60*60;
if (purge_time >= 0)
......@@ -4031,7 +4031,7 @@ struct my_option my_long_options[] =
"Logs will be rotated after expire-log-days days. ",
(gptr*) &expire_logs_days,
(gptr*) &expire_logs_days, 0, GET_ULONG,
REQUIRED_ARG, ~0L, 0, 99, 0, 1, 0},
REQUIRED_ARG, 0, 0, 99, 0, 1, 0},
{0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
};
......
......@@ -118,6 +118,8 @@ sys_var_long_ptr sys_delayed_insert_timeout("delayed_insert_timeout",
&delayed_insert_timeout);
sys_var_long_ptr sys_delayed_queue_size("delayed_queue_size",
&delayed_queue_size);
sys_var_long_ptr sys_expire_logs_days("expire_logs_days",
&expire_logs_days);
sys_var_bool_ptr sys_flush("flush", &myisam_flush);
sys_var_long_ptr sys_flush_time("flush_time", &flush_time);
sys_var_thd_ulong sys_interactive_timeout("interactive_timeout",
......@@ -332,6 +334,7 @@ sys_var *sys_variables[]=
&sys_delayed_insert_timeout,
&sys_delayed_queue_size,
&sys_error_count,
&sys_expire_logs_days,
&sys_flush,
&sys_flush_time,
&sys_foreign_key_checks,
......@@ -435,6 +438,7 @@ struct show_var_st init_vars[]= {
{sys_delayed_insert_limit.name, (char*) &sys_delayed_insert_limit,SHOW_SYS},
{sys_delayed_insert_timeout.name, (char*) &sys_delayed_insert_timeout, SHOW_SYS},
{sys_delayed_queue_size.name,(char*) &sys_delayed_queue_size, SHOW_SYS},
{sys_expire_logs_days.name, (char*) &sys_expire_logs_days, SHOW_SYS},
{sys_flush.name, (char*) &sys_flush, SHOW_SYS},
{sys_flush_time.name, (char*) &sys_flush_time, SHOW_SYS},
{"ft_min_word_len", (char*) &ft_min_word_len, SHOW_LONG},
......
......@@ -133,6 +133,7 @@ class MYSQL_LOG {
int generate_new_name(char *new_name,const char *old_name);
void make_log_name(char* buf, const char* log_ident);
bool is_active(const char* log_file_name);
int update_log_index(LOG_INFO* linfo);
int purge_logs(THD* thd, const char* to_log);
int purge_logs_before_date(THD* thd, time_t purge_time);
int purge_first_log(struct st_relay_log_info* rli);
......
......@@ -3813,7 +3813,7 @@ bool reload_acl_and_cache(THD *thd, ulong options, TABLE_LIST *tables)
mysql_log.new_file(1);
mysql_update_log.new_file(1);
mysql_bin_log.new_file(1);
if (~expire_logs_days)
if (expire_logs_days)
{
long purge_time= time(0) - expire_logs_days*24*60*60;
if (purge_time >= 0)
......
......@@ -3565,10 +3565,9 @@ purge:
;
purge_options:
LOGS_SYM
purge_option
| MASTER_SYM LOGS_SYM
purge_option;
LOGS_SYM purge_option
| MASTER_SYM LOGS_SYM purge_option
;
purge_option:
TO_SYM TEXT_STRING
......@@ -3586,7 +3585,8 @@ purge_option:
Item *tmp= new Item_func_unix_timestamp($2);
Lex->sql_command = SQLCOM_PURGE_BEFORE;
Lex->purge_time= tmp->val_int();
};
}
;
/* kill threads */
......@@ -3596,7 +3596,6 @@ kill:
LEX *lex=Lex;
if ($2->check_cols(1) || $2->fix_fields(lex->thd, 0, &$2))
{
send_error(lex->thd, ER_SET_CONSTANTS_ONLY);
YYABORT;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment