Commit 9d68ccde authored by Michael Widenius's avatar Michael Widenius

Added option BACKUP_ALL to mysqld --myisam-recover to also get a backup of the...

Added option BACKUP_ALL to mysqld --myisam-recover to also get a backup of the index file before it's repaired.
Removed wrong call to translog_buffer_unlock() that caused 'unlocking not locked mutex' failure in Aria log handler.

extra/replace.c:
  Updated call to my_redel()
include/maria.h:
  Updated prototype for maria_change_to_newfile
include/my_sys.h:
  Updated prototypes
  Added my_create_backup_name
include/myisam.h:
  Updated prototypes
include/myisamchk.h:
  Added 'backup_time' to st_handler_check_param to be able to generate same name for backuped data and index file
mysys/my_redel.c:
  Added time_t option to my_redel() to be able to generate same backup extensions for many files
sql/handler.cc:
  Added start_time to st_ha_check_opt
sql/handler.h:
  Added start_time to HA_CHECK_OPT
sql/mysqld.cc:
  Added option BACKUP_ALL to --myisam-recover
storage/maria/ha_maria.cc:
  Remember start time for repair
storage/maria/ma_check.c:
  Use remembered start time for backup file names
  Removed some dead code
storage/maria/ma_loghandler.c:
  Removed wrong call to translog_buffer_unlock() that caused 'unlocking not locked mutex' failure in log handler.
storage/maria/maria_chk.c:
  Removed dead code (O_NEW_INDEX was never set)
  Report if table was 'crashed on repair'
storage/maria/maria_pack.c:
  Updated parameters to my_redel()
storage/myisam/ha_myisam.cc:
  Added recover option BACKUP_ALL to get a backup of the index file before it's repaired.
  Print information to log if we make a backup of data as part of repair.
storage/myisam/ha_myisam.h:
  Added HA_RECOVER_FULL_BACKUP
storage/myisam/mi_check.c:
  Use remembered start time for backup file names
  Added mi_make_backup_of_index()
storage/myisam/myisamchk.c:
  Removed dead code (O_NEW_INDEX was never set)
  Report if table was 'crashed on repair'
storage/myisam/myisampack.c:
  Updated call to my_redel()
parent a1bd9532
......@@ -1085,7 +1085,7 @@ static int convert_file(REPLACE *rep, char * name)
my_fclose(in,MYF(0)); my_fclose(out,MYF(0));
if (updated && ! error)
my_redel(org_name,tempname,MYF(MY_WME | MY_LINK_WARNING));
my_redel(org_name, tempname, 0, MYF(MY_WME | MY_LINK_WARNING));
else
my_delete(tempname,MYF(MY_WME));
if (!silent && ! error)
......
......@@ -430,7 +430,8 @@ int maria_repair_by_sort(HA_CHECK *param, MARIA_HA *info,
int maria_repair_parallel(HA_CHECK *param, register MARIA_HA *info,
const char *name, my_bool rep_quick);
int maria_change_to_newfile(const char *filename, const char *old_ext,
const char *new_ext, myf myflags);
const char *new_ext, time_t backup_time,
myf myflags);
void maria_lock_memory(HA_CHECK *param);
int maria_update_state_info(HA_CHECK *param, MARIA_HA *info, uint update);
void maria_update_key_parts(MARIA_KEYDEF *keyinfo, double *rec_per_key_part,
......
......@@ -135,6 +135,9 @@ extern int NEAR my_errno; /* Last error in mysys */
#define GETDATE_GMT 8
#define GETDATE_FIXEDLENGTH 16
/* Extra length needed for filename if one calls my_create_backup_name */
#define MY_BACKUP_NAME_EXTRA_LENGTH 17
/* defines when allocating data */
#ifdef SAFEMALLOC
#define my_malloc(SZ,FLAG) _mymalloc((SZ), __FILE__, __LINE__, FLAG )
......@@ -685,7 +688,10 @@ extern int my_message_no_curses(uint my_err, const char *str,myf MyFlags);
extern int my_message_curses(uint my_err, const char *str,myf MyFlags);
extern my_bool my_init(void);
extern void my_end(int infoflag);
extern int my_redel(const char *from, const char *to, int MyFlags);
extern int my_redel(const char *from, const char *to, time_t backup_time_stamp,
myf MyFlags);
void my_create_backup_name(char *to, const char *from,
time_t backup_time_stamp);
extern int my_copystat(const char *from, const char *to, int MyFlags);
extern char * my_filename(File fd);
......
......@@ -295,6 +295,8 @@ extern int mi_is_changed(struct st_myisam_info *info);
extern int mi_delete_all_rows(struct st_myisam_info *info);
extern ulong _mi_calc_blob_length(uint length , const uchar *pos);
extern uint mi_get_pointer_length(ulonglong file_length, uint def);
extern int mi_make_backup_of_index(struct st_myisam_info *info,
time_t backup_time, myf flags);
#define MEMMAP_EXTRA_MARGIN 7 /* Write this as a suffix for mmap file */
/* this is used to pass to mysql_myisamchk_table */
......@@ -390,7 +392,8 @@ int mi_repair_by_sort(HA_CHECK *param, register MI_INFO *info,
int mi_repair_parallel(HA_CHECK *param, register MI_INFO *info,
const char * name, int rep_quick);
int change_to_newfile(const char * filename, const char * old_ext,
const char * new_ext, uint raid_chunks,
const char * new_ext, time_t backup_time,
uint raid_chunks,
myf myflags);
int lock_file(HA_CHECK *param, File file, my_off_t start, int lock_type,
const char *filetype, const char *filename);
......
......@@ -74,7 +74,7 @@
#define TT_USEFRM 1
#define TT_FOR_UPGRADE 2
#define O_NEW_INDEX 1 /* Bits set in out_flag */
/* Bits set in out_flag */
#define O_NEW_DATA 2
#define O_DATA_LOST 4
......@@ -141,6 +141,7 @@ typedef struct st_handler_check_param
ulonglong use_buffers; /* Used as param to getopt() */
size_t read_buffer_length, write_buffer_length;
size_t sort_buffer_length, sort_key_blocks;
time_t backup_time; /* To sign backup files */
ulong rec_per_key_part[HA_MAX_KEY_SEG * HA_MAX_POSSIBLE_KEY];
double new_rec_per_key_part[HA_MAX_KEY_SEG * HA_MAX_POSSIBLE_KEY];
uint out_flag, warning_printed, error_printed, verbose;
......
......@@ -40,7 +40,8 @@ struct utimbuf {
#define REDEL_EXT ".BAK"
int my_redel(const char *org_name, const char *tmp_name, myf MyFlags)
int my_redel(const char *org_name, const char *tmp_name,
time_t backup_time_stamp, myf MyFlags)
{
int error=1;
DBUG_ENTER("my_redel");
......@@ -51,13 +52,9 @@ int my_redel(const char *org_name, const char *tmp_name, myf MyFlags)
goto end;
if (MyFlags & MY_REDEL_MAKE_BACKUP)
{
char name_buff[FN_REFLEN+20];
char ext[20];
ext[0]='-';
get_date(ext+1,2+4,(time_t) 0);
strmov(strend(ext),REDEL_EXT);
if (my_rename(org_name, fn_format(name_buff, org_name, "", ext, 2),
MyFlags))
char name_buff[FN_REFLEN + MY_BACKUP_NAME_EXTRA_LENGTH];
my_create_backup_name(name_buff, org_name, backup_time_stamp);
if (my_rename(org_name, name_buff, MyFlags))
goto end;
}
else if (my_delete_allow_opened(org_name, MyFlags))
......@@ -149,3 +146,23 @@ int my_copystat(const char *from, const char *to, int MyFlags)
#endif
return 0;
} /* my_copystat */
/**
Create a backup file name.
@fn my_create_backup_name()
@param to Store new file name here
@param from Original name
@info
The backup name is made by adding -YYMMDDHHMMSS.BAK to the file name
*/
void my_create_backup_name(char *to, const char *from, time_t backup_start)
{
char ext[MY_BACKUP_NAME_EXTRA_LENGTH+1];
ext[0]='-';
get_date(ext+1, GETDATE_SHORT_DATE | GETDATE_HHMMSSTIME, backup_start);
strmov(strend(ext),REDEL_EXT);
strmov(strmov(to, from), ext);
}
......@@ -3732,6 +3732,7 @@ int ha_create_table_from_engine(THD* thd, const char *db, const char *name)
void st_ha_check_opt::init()
{
flags= sql_flags= 0;
start_time= my_time(0);
}
......
......@@ -1021,6 +1021,7 @@ typedef struct st_ha_check_opt
st_ha_check_opt() {} /* Remove gcc warning */
uint flags; /* isam layer flags (e.g. for myisamchk) */
uint sql_flags; /* sql layer flags - for something myisamchk cannot do */
time_t start_time; /* When check/repair starts */
KEY_CACHE *key_cache; /* new key cache when changing key cache */
void init();
} HA_CHECK_OPT;
......
......@@ -6456,7 +6456,7 @@ each time the SQL thread starts.",
0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0},
#endif
{"myisam-recover", OPT_MYISAM_RECOVER,
"Syntax: myisam-recover=OFF or myisam-recover[=option[,option...]], where option can be DEFAULT, BACKUP, FORCE or QUICK.",
"Syntax: myisam-recover=OFF or myisam-recover[=option[,option...]], where option can be DEFAULT, BACKUP, BACKUP_ALL, FORCE or QUICK.",
&myisam_recover_options_str, &myisam_recover_options_str, 0,
GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
#ifdef WITH_NDBCLUSTER_STORAGE_ENGINE
......@@ -7503,7 +7503,11 @@ thread is in the relay logs.",
1024, 0},
{"thread_handling", OPT_THREAD_HANDLING,
"Define threads usage for handling queries: "
"one-thread-per-connection or no-threads.",
"one-thread-per-connection"
#if HAVE_POOL_OF_THREADS == 1
", pool-of-threads"
#endif
"or no-threads.",
&opt_thread_handling, &opt_thread_handling,
0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"updatable_views_with_limit", OPT_UPDATABLE_VIEWS_WITH_LIMIT,
......
......@@ -1316,6 +1316,7 @@ int ha_maria::repair(THD * thd, HA_CHECK_OPT *check_opt)
T_SILENT | T_FORCE_CREATE | T_CALC_CHECKSUM |
(check_opt->flags & T_EXTEND ? T_REP : T_REP_BY_SORT));
param.sort_buffer_length= THDVAR(thd, sort_buffer_size);
param.backup_time= check_opt->start_time;
start_records= file->state->records;
while ((error= repair(thd, &param, 0)) && param.retry_repair)
{
......
......@@ -2743,7 +2743,7 @@ int maria_repair(HA_CHECK *param, register MARIA_HA *info,
new_file= -1;
change_data_file_descriptor(info, -1);
if (maria_change_to_newfile(share->data_file_name.str, MARIA_NAME_DEXT,
DATA_TMP_EXT,
DATA_TMP_EXT, param->backup_time,
(param->testflag & T_BACKUP_DATA ?
MYF(MY_REDEL_MAKE_BACKUP): MYF(0)) |
sync_dir) ||
......@@ -3059,7 +3059,7 @@ int maria_sort_index(HA_CHECK *param, register MARIA_HA *info, char *name)
pthread_mutex_unlock(&share->intern_lock);
VOID(my_close(new_file,MYF(MY_WME)));
if (maria_change_to_newfile(share->index_file_name.str, MARIA_NAME_IEXT,
INDEX_TMP_EXT, sync_dir) ||
INDEX_TMP_EXT, 0, sync_dir) ||
_ma_open_keyfile(share))
goto err2;
info->lock_type= F_UNLCK; /* Force maria_readinfo to lock */
......@@ -3492,20 +3492,15 @@ int maria_zerofill(HA_CHECK *param, MARIA_HA *info, const char *name)
*/
int maria_change_to_newfile(const char * filename, const char * old_ext,
const char * new_ext, myf MyFlags)
const char * new_ext, time_t backup_time,
myf MyFlags)
{
char old_filename[FN_REFLEN],new_filename[FN_REFLEN];
#ifdef USE_RAID
if (raid_chunks)
return my_raid_redel(fn_format(old_filename,filename,"",old_ext,2+4),
fn_format(new_filename,filename,"",new_ext,2+4),
raid_chunks,
MYF(MY_WME | MY_LINK_WARNING | MyFlags));
#endif
/* Get real path to filename */
(void) fn_format(old_filename,filename,"",old_ext,2+4+32);
return my_redel(old_filename,
fn_format(new_filename,old_filename,"",new_ext,2+4),
backup_time,
MYF(MY_WME | MY_LINK_WARNING | MyFlags));
} /* maria_change_to_newfile */
......@@ -3875,7 +3870,7 @@ int maria_repair_by_sort(HA_CHECK *param, register MARIA_HA *info,
}
change_data_file_descriptor(info, -1);
if (maria_change_to_newfile(share->data_file_name.str, MARIA_NAME_DEXT,
DATA_TMP_EXT,
DATA_TMP_EXT, param->backup_time,
(param->testflag & T_BACKUP_DATA ?
MYF(MY_REDEL_MAKE_BACKUP): MYF(0)) |
sync_dir) ||
......@@ -4500,7 +4495,7 @@ err:
my_close(new_file,MYF(0));
info->dfile.file= new_file= -1;
if (maria_change_to_newfile(share->data_file_name.str, MARIA_NAME_DEXT,
DATA_TMP_EXT,
DATA_TMP_EXT, param->backup_time,
MYF((param->testflag & T_BACKUP_DATA ?
MY_REDEL_MAKE_BACKUP : 0) |
sync_dir)) ||
......
......@@ -2500,10 +2500,7 @@ my_bool translog_prev_buffer_flush_wait(struct st_translog_buffer *buffer)
pthread_cond_wait(&buffer->prev_sent_to_disk_cond, &buffer->mutex);
if (buffer->file != file || buffer->offset != offset ||
buffer->ver != ver)
{
translog_buffer_unlock(buffer);
DBUG_RETURN(1); /* some the thread flushed the buffer already */
}
} while(buffer->prev_buffer_offset != buffer->prev_sent_to_disk);
}
DBUG_RETURN(0);
......
......@@ -1211,7 +1211,7 @@ static int maria_chk(HA_CHECK *param, char *filename)
{ /* Change temp file to org file */
VOID(my_close(info->dfile.file, MYF(MY_WME))); /* Close new file */
error|=maria_change_to_newfile(filename,MARIA_NAME_DEXT,DATA_TMP_EXT,
MYF(0));
0, MYF(0));
if (_ma_open_datafile(info,info->s, NullS, -1))
error=1;
param->out_flag&= ~O_NEW_DATA; /* We are using new datafile */
......@@ -1349,11 +1349,9 @@ end2:
{
if (param->out_flag & O_NEW_DATA)
error|=maria_change_to_newfile(filename,MARIA_NAME_DEXT,DATA_TMP_EXT,
param->backup_time,
((param->testflag & T_BACKUP_DATA) ?
MYF(MY_REDEL_MAKE_BACKUP) : MYF(0)));
if (param->out_flag & O_NEW_INDEX)
error|=maria_change_to_newfile(filename,MARIA_NAME_IEXT,INDEX_TMP_EXT,
MYF(0));
}
if (opt_transaction_logging &&
share->base.born_transactional && !error &&
......@@ -1457,7 +1455,8 @@ static void descript(HA_CHECK *param, register MARIA_HA *info, char *name)
printf("UUID: %s\n", buff);
pos=buff;
if (share->state.changed & STATE_CRASHED)
strmov(buff,"crashed");
strmov(buff, share->state.changed & STATE_CRASHED_ON_REPAIR ?
"crashed on repair" : "crashed");
else
{
if (share->state.open_count)
......
......@@ -736,7 +736,7 @@ static int compress(PACK_MRG_INFO *mrg,char *result_table)
VOID(my_delete(new_name,MYF(MY_WME)));
}
else
error=my_redel(org_name,new_name,MYF(MY_WME | MY_COPYTIME));
error=my_redel(org_name, new_name, 0, MYF(MY_WME | MY_COPYTIME));
}
if (! error)
error=save_state(isam_file,mrg,new_length,glob_crc);
......
......@@ -33,7 +33,7 @@ ulong myisam_recover_options= HA_RECOVER_NONE;
/* bits in myisam_recover_options */
const char *myisam_recover_names[] =
{ "DEFAULT", "BACKUP", "FORCE", "QUICK", NullS};
{ "DEFAULT", "BACKUP", "FORCE", "QUICK", "BACKUP_ALL", NullS};
TYPELIB myisam_recover_typelib= {array_elements(myisam_recover_names)-1,"",
myisam_recover_names, NULL};
......@@ -1057,6 +1057,7 @@ int ha_myisam::repair(THD* thd, HA_CHECK_OPT *check_opt)
T_SILENT | T_FORCE_CREATE | T_CALC_CHECKSUM |
(check_opt->flags & T_EXTEND ? T_REP : T_REP_BY_SORT));
param.sort_buffer_length= thd->variables.myisam_sort_buff_size;
param.backup_time= check_opt->start_time;
start_records=file->state->records;
while ((error=repair(thd,param,0)) && param.retry_repair)
{
......@@ -1661,8 +1662,18 @@ bool ha_myisam::check_and_repair(THD *thd)
if ((marked_crashed= mi_is_crashed(file)) || check(thd, &check_opt))
{
sql_print_warning("Recovering table: '%s'",table->s->path.str);
if (myisam_recover_options & (HA_RECOVER_FULL_BACKUP | HA_RECOVER_BACKUP))
{
char buff[MY_BACKUP_NAME_EXTRA_LENGTH+1];
my_create_backup_name(buff, "", check_opt.start_time);
sql_print_information("Making backup of data with extension '%s'", buff);
}
if (myisam_recover_options & HA_RECOVER_FULL_BACKUP)
mi_make_backup_of_index(file, check_opt.start_time,
MYF(MY_WME | ME_JUST_WARNING));
check_opt.flags=
((myisam_recover_options & HA_RECOVER_BACKUP ? T_BACKUP_DATA : 0) |
(((myisam_recover_options &
(HA_RECOVER_BACKUP | HA_RECOVER_FULL_BACKUP)) ? T_BACKUP_DATA : 0) |
(marked_crashed ? 0 : T_QUICK) |
(myisam_recover_options & HA_RECOVER_FORCE ? 0 : T_SAFE_REPAIR) |
T_AUTO_REPAIR);
......
......@@ -29,6 +29,7 @@
#define HA_RECOVER_BACKUP 2 /* Make a backupfile on recover */
#define HA_RECOVER_FORCE 4 /* Recover even if we loose rows */
#define HA_RECOVER_QUICK 8 /* Don't check rows in data file */
#define HA_RECOVER_FULL_BACKUP 16 /* Make a copy of index file too */
extern ulong myisam_sort_buffer_size;
extern TYPELIB myisam_recover_typelib;
......
......@@ -1733,7 +1733,9 @@ err:
my_close(new_file,MYF(0));
info->dfile=new_file= -1;
if (change_to_newfile(share->data_file_name,MI_NAME_DEXT,
DATA_TMP_EXT, share->base.raid_chunks,
DATA_TMP_EXT,
param->backup_time,
share->base.raid_chunks,
(param->testflag & T_BACKUP_DATA ?
MYF(MY_REDEL_MAKE_BACKUP): MYF(0))) ||
mi_open_datafile(info,share,name,-1))
......@@ -1991,8 +1993,8 @@ int mi_sort_index(HA_CHECK *param, register MI_INFO *info, char * name)
VOID(my_close(share->kfile,MYF(MY_WME)));
share->kfile = -1;
VOID(my_close(new_file,MYF(MY_WME)));
if (change_to_newfile(share->index_file_name,MI_NAME_IEXT,INDEX_TMP_EXT,0,
MYF(0)) ||
if (change_to_newfile(share->index_file_name,MI_NAME_IEXT,INDEX_TMP_EXT,
0, 0, MYF(0)) ||
mi_open_keyfile(share))
goto err2;
info->lock_type= F_UNLCK; /* Force mi_readinfo to lock */
......@@ -2121,6 +2123,7 @@ err:
int change_to_newfile(const char * filename, const char * old_ext,
const char * new_ext,
time_t backup_time,
uint raid_chunks __attribute__((unused)),
myf MyFlags)
{
......@@ -2136,7 +2139,7 @@ int change_to_newfile(const char * filename, const char * old_ext,
(void) fn_format(old_filename,filename,"",old_ext,2+4+32);
return my_redel(old_filename,
fn_format(new_filename,old_filename,"",new_ext,2+4),
MYF(MY_WME | MY_LINK_WARNING | MyFlags));
backup_time, MYF(MY_WME | MY_LINK_WARNING | MyFlags));
} /* change_to_newfile */
......@@ -2544,7 +2547,8 @@ err:
my_close(new_file,MYF(0));
info->dfile=new_file= -1;
if (change_to_newfile(share->data_file_name,MI_NAME_DEXT,
DATA_TMP_EXT, share->base.raid_chunks,
DATA_TMP_EXT, param->backup_time,
share->base.raid_chunks,
(param->testflag & T_BACKUP_DATA ?
MYF(MY_REDEL_MAKE_BACKUP): MYF(0))) ||
mi_open_datafile(info,share,name,-1))
......@@ -3081,7 +3085,8 @@ err:
my_close(new_file,MYF(0));
info->dfile=new_file= -1;
if (change_to_newfile(share->data_file_name,MI_NAME_DEXT,
DATA_TMP_EXT, share->base.raid_chunks,
DATA_TMP_EXT, param->backup_time,
share->base.raid_chunks,
(param->testflag & T_BACKUP_DATA ?
MYF(MY_REDEL_MAKE_BACKUP): MYF(0))) ||
mi_open_datafile(info,share,name,-1))
......@@ -4743,3 +4748,10 @@ set_data_file_type(MI_SORT_INFO *sort_info, MYISAM_SHARE *share)
share->delete_record=tmp.delete_record;
}
}
int mi_make_backup_of_index(MI_INFO *info, time_t backup_time, myf flags)
{
char backup_name[FN_REFLEN + MY_BACKUP_NAME_EXTRA_LENGTH];
my_create_backup_name(backup_name, info->s->index_file_name, backup_time);
return my_copy(info->s->index_file_name, backup_name, flags);
}
......@@ -1032,7 +1032,7 @@ static int myisamchk(HA_CHECK *param, char * filename)
{ /* Change temp file to org file */
VOID(my_close(info->dfile,MYF(MY_WME))); /* Close new file */
error|=change_to_newfile(filename,MI_NAME_DEXT,DATA_TMP_EXT,
raid_chunks,
0, raid_chunks,
MYF(0));
if (mi_open_datafile(info,info->s, NULL, -1))
error=1;
......@@ -1164,12 +1164,9 @@ end2:
{
if (param->out_flag & O_NEW_DATA)
error|=change_to_newfile(filename,MI_NAME_DEXT,DATA_TMP_EXT,
raid_chunks,
param->backup_time, raid_chunks,
((param->testflag & T_BACKUP_DATA) ?
MYF(MY_REDEL_MAKE_BACKUP) : MYF(0)));
if (param->out_flag & O_NEW_INDEX)
error|=change_to_newfile(filename,MI_NAME_IEXT,INDEX_TMP_EXT,0,
MYF(0));
}
VOID(fflush(stdout)); VOID(fflush(stderr));
if (param->error_printed)
......@@ -1241,7 +1238,8 @@ static void descript(HA_CHECK *param, register MI_INFO *info, char * name)
}
pos=buff;
if (share->state.changed & STATE_CRASHED)
strmov(buff,"crashed");
strmov(buff, share->state.changed & STATE_CRASHED_ON_REPAIR ?
"crashed on repair" : "crashed");
else
{
if (share->state.open_count)
......
......@@ -720,7 +720,7 @@ static int compress(PACK_MRG_INFO *mrg,char *result_table)
VOID(my_delete(new_name,MYF(MY_WME)));
}
else
error=my_redel(org_name,new_name,MYF(MY_WME | MY_COPYTIME));
error=my_redel(org_name, new_name, 0, MYF(MY_WME | MY_COPYTIME));
}
if (! error)
error=save_state(isam_file,mrg,new_length,glob_crc);
......
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