56
56
static void mi_check_print_msg(MI_CHECK *param, const char* msg_type,
57
57
const char *fmt, va_list args)
59
Session* thd = (Session*)param->thd;
60
Protocol *protocol= thd->protocol;
59
Session* session = (Session*)param->session;
60
Protocol *protocol= session->protocol;
61
61
uint32_t length, msg_length;
62
62
char msgbuf[MI_MAX_MSG_BUF];
63
63
char name[NAME_LEN*2+2];
65
65
msg_length= vsnprintf(msgbuf, sizeof(msgbuf), fmt, args);
66
66
msgbuf[sizeof(msgbuf) - 1] = 0; // healthy paranoia
68
if (!session->vio_ok())
70
70
sql_print_error("%s",msgbuf);
391
391
volatile int *killed_ptr(MI_CHECK *param)
393
393
/* In theory Unsafe conversion, but should be ok for now */
394
return (int*) &(((Session *)(param->thd))->killed);
394
return (int*) &(((Session *)(param->session))->killed);
397
397
void mi_check_print_error(MI_CHECK *param, const char *fmt,...)
440
440
void _mi_report_crashed(MI_INFO *file, const char *message,
441
441
const char *sfile, uint32_t sline)
443
Session *cur_session;
445
445
pthread_mutex_lock(&file->s->intern_lock);
446
if ((cur_thd= (Session*) file->in_use.data))
447
sql_print_error("Got an error from thread_id=%lu, %s:%d", cur_thd->thread_id,
446
if ((cur_session= (Session*) file->in_use.data))
447
sql_print_error("Got an error from thread_id=%lu, %s:%d", cur_session->thread_id,
450
450
sql_print_error("Got an error from unknown thread, %s:%d", sfile, sline);
615
615
return mi_write(file,buf);
618
int ha_myisam::check(Session* thd, HA_CHECK_OPT* check_opt)
618
int ha_myisam::check(Session* session, HA_CHECK_OPT* check_opt)
620
620
if (!file) return HA_ADMIN_INTERNAL_ERROR;
623
623
MYISAM_SHARE* share = file->s;
624
const char *old_proc_info= thd->get_proc_info();
624
const char *old_proc_info= session->get_proc_info();
626
thd->set_proc_info("Checking table");
626
session->set_proc_info("Checking table");
627
627
myisamchk_init(¶m);
628
param.session = session;
629
629
param.op_name = "check";
630
630
param.db_name= table->s->db.str;
631
631
param.table_name= table->alias;
632
632
param.testflag = check_opt->flags | T_CHECK | T_SILENT;
633
param.stats_method= (enum_mi_stats_method)thd->variables.myisam_stats_method;
633
param.stats_method= (enum_mi_stats_method)session->variables.myisam_stats_method;
635
635
if (!(table->db_stat & HA_READ_ONLY))
636
636
param.testflag|= T_STATISTICS;
691
691
HA_STATUS_CONST);
694
else if (!mi_is_crashed(file) && !thd->killed)
694
else if (!mi_is_crashed(file) && !session->killed)
696
696
mi_mark_crashed(file);
697
697
file->update |= HA_STATE_CHANGED | HA_STATE_ROW_CHANGED;
700
thd->set_proc_info(old_proc_info);
700
session->set_proc_info(old_proc_info);
701
701
return error ? HA_ADMIN_CORRUPT : HA_ADMIN_OK;
708
708
two threads may do an analyze at the same time!
711
int ha_myisam::analyze(Session *thd,
711
int ha_myisam::analyze(Session *session,
712
712
HA_CHECK_OPT* check_opt __attribute__((unused)))
716
716
MYISAM_SHARE* share = file->s;
718
718
myisamchk_init(¶m);
719
param.session = session;
720
720
param.op_name= "analyze";
721
721
param.db_name= table->s->db.str;
722
722
param.table_name= table->alias;
723
723
param.testflag= (T_FAST | T_CHECK | T_SILENT | T_STATISTICS |
724
724
T_DONT_CHECK_CHECKSUM);
725
725
param.using_global_keycache = 1;
726
param.stats_method= (enum_mi_stats_method)thd->variables.myisam_stats_method;
726
param.stats_method= (enum_mi_stats_method)session->variables.myisam_stats_method;
728
728
if (!(share->state.changed & STATE_NOT_ANALYZED))
729
729
return HA_ADMIN_ALREADY_DONE;
735
735
error=update_state_info(¶m,file,UPDATE_STAT);
736
736
pthread_mutex_unlock(&share->intern_lock);
738
else if (!mi_is_crashed(file) && !thd->killed)
738
else if (!mi_is_crashed(file) && !session->killed)
739
739
mi_mark_crashed(file);
740
740
return error ? HA_ADMIN_CORRUPT : HA_ADMIN_OK;
744
int ha_myisam::repair(Session* thd, HA_CHECK_OPT *check_opt)
744
int ha_myisam::repair(Session* session, HA_CHECK_OPT *check_opt)
750
750
if (!file) return HA_ADMIN_INTERNAL_ERROR;
752
752
myisamchk_init(¶m);
753
param.session = session;
754
754
param.op_name= "repair";
755
755
param.testflag= ((check_opt->flags & ~(T_EXTEND)) |
756
756
T_SILENT | T_FORCE_CREATE | T_CALC_CHECKSUM |
757
757
(check_opt->flags & T_EXTEND ? T_REP : T_REP_BY_SORT));
758
758
param.sort_buffer_length= check_opt->sort_buffer_size;
759
759
start_records=file->state->records;
760
while ((error=repair(thd,param,0)) && param.retry_repair)
760
while ((error=repair(session,param,0)) && param.retry_repair)
762
762
param.retry_repair=0;
763
763
if (test_all_bits(param.testflag,
793
int ha_myisam::optimize(Session* thd, HA_CHECK_OPT *check_opt)
793
int ha_myisam::optimize(Session* session, HA_CHECK_OPT *check_opt)
796
796
if (!file) return HA_ADMIN_INTERNAL_ERROR;
799
799
myisamchk_init(¶m);
800
param.session = session;
801
801
param.op_name= "optimize";
802
802
param.testflag= (check_opt->flags | T_SILENT | T_FORCE_CREATE |
803
803
T_REP_BY_SORT | T_STATISTICS | T_SORT_INDEX);
804
804
param.sort_buffer_length= check_opt->sort_buffer_size;
805
if ((error= repair(thd,param,1)) && param.retry_repair)
805
if ((error= repair(session,param,1)) && param.retry_repair)
807
807
sql_print_warning("Warning: Optimize table got errno %d on %s.%s, retrying",
808
808
my_errno, param.db_name, param.table_name);
809
809
param.testflag&= ~T_REP_BY_SORT;
810
error= repair(thd,param,1);
810
error= repair(session,param,1);
816
int ha_myisam::repair(Session *thd, MI_CHECK ¶m, bool do_optimize)
816
int ha_myisam::repair(Session *session, MI_CHECK ¶m, bool do_optimize)
819
819
uint32_t local_testflag=param.testflag;
820
820
bool optimize_done= !do_optimize, statistics_done=0;
821
const char *old_proc_info= thd->get_proc_info();
821
const char *old_proc_info= session->get_proc_info();
822
822
char fixed_name[FN_REFLEN];
823
823
MYISAM_SHARE* share = file->s;
824
824
ha_rows rows= file->state->records;
843
843
param.table_name= table->alias;
844
844
param.tmpfile_createflag = O_RDWR | O_TRUNC;
845
845
param.using_global_keycache = 1;
846
param.session= session;
847
847
param.tmpdir= &mysql_tmpdir_list;
848
848
param.out_flag= 0;
849
849
my_stpcpy(fixed_name,file->filename);
851
851
// Don't lock tables if we have used LOCK Table
852
if (!thd->locked_tables &&
852
if (!session->locked_tables &&
853
853
mi_lock_database(file, table->s->tmp_table ? F_EXTRA_LCK : F_WRLCK))
855
855
mi_check_print_error(¶m,ER(ER_CANT_LOCK),my_errno);
871
871
local_testflag|= T_STATISTICS;
872
872
param.testflag|= T_STATISTICS; // We get this for free
873
873
statistics_done=1;
874
if (thd->variables.myisam_repair_threads>1)
874
if (session->variables.myisam_repair_threads>1)
877
877
/* TODO: respect myisam_repair_threads variable */
878
878
snprintf(buf, 40, "Repair with %d threads", my_count_bits(key_map));
879
thd->set_proc_info(buf);
879
session->set_proc_info(buf);
880
880
error = mi_repair_parallel(¶m, file, fixed_name,
881
881
param.testflag & T_QUICK);
882
thd->set_proc_info("Repair done"); // to reset proc_info, as
882
session->set_proc_info("Repair done"); // to reset proc_info, as
883
883
// it was pointing to local buffer
887
thd->set_proc_info("Repair by sorting");
887
session->set_proc_info("Repair by sorting");
888
888
error = mi_repair_by_sort(¶m, file, fixed_name,
889
889
param.testflag & T_QUICK);
894
thd->set_proc_info("Repair with keycache");
894
session->set_proc_info("Repair with keycache");
895
895
param.testflag &= ~T_REP_BY_SORT;
896
896
error= mi_repair(¶m, file, fixed_name,
897
897
param.testflag & T_QUICK);
905
905
(share->state.changed & STATE_NOT_SORTED_PAGES))
908
thd->set_proc_info("Sorting index");
908
session->set_proc_info("Sorting index");
909
909
error=mi_sort_index(¶m,file,fixed_name);
911
911
if (!statistics_done && (local_testflag & T_STATISTICS))
913
913
if (share->state.changed & STATE_NOT_ANALYZED)
916
thd->set_proc_info("Analyzing");
916
session->set_proc_info("Analyzing");
917
917
error = chk_key(¶m, file);
920
920
local_testflag&= ~T_STATISTICS; // Don't update statistics
923
thd->set_proc_info("Saving state");
923
session->set_proc_info("Saving state");
926
926
if ((share->state.changed & STATE_CHANGED) || mi_is_crashed(file))
958
958
file->update |= HA_STATE_CHANGED | HA_STATE_ROW_CHANGED;
959
959
update_state_info(¶m, file, 0);
961
thd->set_proc_info(old_proc_info);
962
if (!thd->locked_tables)
961
session->set_proc_info(old_proc_info);
962
if (!session->locked_tables)
963
963
mi_lock_database(file,F_UNLCK);
964
964
return(error ? HA_ADMIN_FAILED :
965
965
!optimize_done ? HA_ADMIN_ALREADY_DONE : HA_ADMIN_OK);
970
970
Assign table indexes to a specific key cache.
973
int ha_myisam::assign_to_keycache(Session* thd, HA_CHECK_OPT *check_opt)
973
int ha_myisam::assign_to_keycache(Session* session, HA_CHECK_OPT *check_opt)
975
975
KEY_CACHE *new_key_cache= check_opt->key_cache;
976
976
const char *errmsg= 0;
1106
1106
else if (mode == HA_KEY_SWITCH_NONUNIQ_SAVE)
1108
Session *thd=current_thd;
1108
Session *session=current_session;
1109
1109
MI_CHECK param;
1110
const char *save_proc_info= thd->get_proc_info();
1111
thd->set_proc_info("Creating index");
1110
const char *save_proc_info= session->get_proc_info();
1111
session->set_proc_info("Creating index");
1112
1112
myisamchk_init(¶m);
1113
1113
param.op_name= "recreating_index";
1114
1114
param.testflag= (T_SILENT | T_REP_BY_SORT | T_QUICK |
1115
1115
T_CREATE_MISSING_KEYS);
1116
1116
param.myf_rw&= ~MY_WAIT_IF_FULL;
1117
param.sort_buffer_length= thd->variables.myisam_sort_buff_size;
1118
param.stats_method= (enum_mi_stats_method)thd->variables.myisam_stats_method;
1117
param.sort_buffer_length= session->variables.myisam_sort_buff_size;
1118
param.stats_method= (enum_mi_stats_method)session->variables.myisam_stats_method;
1119
1119
param.tmpdir=&mysql_tmpdir_list;
1120
if ((error= (repair(thd,param,0) != HA_ADMIN_OK)) && param.retry_repair)
1120
if ((error= (repair(session,param,0) != HA_ADMIN_OK)) && param.retry_repair)
1122
1122
sql_print_warning("Warning: Enabling keys got errno %d on %s.%s, retrying",
1123
1123
my_errno, param.db_name, param.table_name);
1124
1124
/* Repairing by sort failed. Now try standard repair method. */
1125
1125
param.testflag&= ~(T_REP_BY_SORT | T_QUICK);
1126
error= (repair(thd,param,0) != HA_ADMIN_OK);
1126
error= (repair(session,param,0) != HA_ADMIN_OK);
1128
1128
If the standard repair succeeded, clear all error messages which
1129
1129
might have been set by the first repair. They can still be seen
1130
1130
with SHOW WARNINGS then.
1133
session->clear_error();
1135
1135
info(HA_STATUS_CONST);
1136
thd->set_proc_info(save_proc_info);
1136
session->set_proc_info(save_proc_info);
1183
1183
void ha_myisam::start_bulk_insert(ha_rows rows)
1185
Session *thd= current_thd;
1186
ulong size= cmin(thd->variables.read_buff_size,
1185
Session *session= current_session;
1186
ulong size= cmin(session->variables.read_buff_size,
1187
1187
(ulong) (table->s->avg_row_length*rows));
1189
1189
/* don't enable row cache if too few rows */
1206
1206
if (!file->bulk_insert &&
1207
1207
(!rows || rows >= MI_MIN_ROWS_TO_USE_BULK_INSERT))
1209
mi_init_bulk_insert(file, thd->variables.bulk_insert_buff_size, rows);
1209
mi_init_bulk_insert(file, session->variables.bulk_insert_buff_size, rows);
1249
1249
check_opt.flags|=T_QUICK;
1250
1250
sql_print_warning("Checking table: '%s'",table->s->path.str);
1252
old_query= thd->query;
1253
old_query_length= thd->query_length;
1252
old_query= session->query;
1253
old_query_length= session->query_length;
1254
1254
pthread_mutex_lock(&LOCK_thread_count);
1255
thd->query= table->s->table_name.str;
1256
thd->query_length= table->s->table_name.length;
1255
session->query= table->s->table_name.str;
1256
session->query_length= table->s->table_name.length;
1257
1257
pthread_mutex_unlock(&LOCK_thread_count);
1259
if ((marked_crashed= mi_is_crashed(file)) || check(thd, &check_opt))
1259
if ((marked_crashed= mi_is_crashed(file)) || check(session, &check_opt))
1261
1261
sql_print_warning("Recovering table: '%s'",table->s->path.str);
1262
1262
check_opt.flags=
1264
1264
(marked_crashed ? 0 : T_QUICK) |
1265
1265
(myisam_recover_options & HA_RECOVER_FORCE ? 0 : T_SAFE_REPAIR) |
1266
1266
T_AUTO_REPAIR);
1267
if (repair(thd, &check_opt))
1267
if (repair(session, &check_opt))
1270
1270
pthread_mutex_lock(&LOCK_thread_count);
1271
thd->query= old_query;
1272
thd->query_length= old_query_length;
1271
session->query= old_query;
1272
session->query_length= old_query_length;
1273
1273
pthread_mutex_unlock(&LOCK_thread_count);
1583
int ha_myisam::external_lock(Session *thd, int lock_type)
1583
int ha_myisam::external_lock(Session *session, int lock_type)
1585
file->in_use.data= thd;
1585
file->in_use.data= session;
1586
1586
return mi_lock_database(file, !table->s->tmp_table ?
1587
1587
lock_type : ((lock_type == F_UNLCK) ?
1588
1588
F_UNLCK : F_EXTRA_LCK));
1591
THR_LOCK_DATA **ha_myisam::store_lock(Session *thd __attribute__((unused)),
1591
THR_LOCK_DATA **ha_myisam::store_lock(Session *session __attribute__((unused)),
1592
1592
THR_LOCK_DATA **to,
1593
1593
enum thr_lock_type lock_type)