199
196
const char *first_keyword= "first", *binary_keyword= "BINARY";
200
197
const char *my_localhost= "localhost";
201
#if SIZEOF_OFF_T > 4 && defined(BIG_TABLES)
202
198
#define GET_HA_ROWS GET_ULL
204
#define GET_HA_ROWS GET_ULONG
208
201
Used with --help for detailed option
241
234
static char *default_collation_name;
242
235
static char *default_storage_engine_str;
243
236
static char compiled_default_collation_name[]= DRIZZLE_DEFAULT_COLLATION_NAME;
244
static I_List<THD> thread_cache;
237
static I_List<Session> thread_cache;
246
239
static pthread_cond_t COND_thread_cache, COND_flush_thread_cache;
303
296
uint32_t lower_case_table_names= 1;
304
297
uint32_t tc_heuristic_recover= 0;
305
298
uint32_t volatile thread_count, thread_running;
306
uint64_t thd_startup_options;
299
uint64_t session_startup_options;
307
300
ulong back_log, connect_timeout, server_id;
308
301
ulong table_cache_size, table_def_size;
309
302
ulong what_to_log;
435
428
/* Thread specific variables */
437
430
pthread_key(MEM_ROOT**,THR_MALLOC);
438
pthread_key(THD*, THR_THD);
431
pthread_key(Session*, THR_Session);
439
432
pthread_mutex_t LOCK_mysql_create_db, LOCK_open, LOCK_thread_count,
440
433
LOCK_status, LOCK_global_read_lock,
441
434
LOCK_error_log, LOCK_uuid_generator,
473
466
static char **defaults_argv;
474
467
static char *opt_bin_logname;
476
struct rand_struct sql_rand; ///< used by sql_class.cc:THD::THD()
469
struct rand_struct sql_rand; ///< used by sql_class.cc:Session::Session()
478
471
struct passwd *user_info;
479
472
static pthread_t select_thread;
583
576
statements and inform their clients that the server is about to die.
587
580
(void) pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
589
I_List_iterator<THD> it(threads);
582
I_List_iterator<Session> it(threads);
590
583
while ((tmp=it++))
592
585
/* We skip slave threads & scheduler on this first loop through. */
593
586
if (tmp->slave_thread)
596
tmp->killed= THD::KILL_CONNECTION;
589
tmp->killed= Session::KILL_CONNECTION;
597
590
thread_scheduler.post_kill_notification(tmp);
598
591
if (tmp->mysys_var)
1214
1207
Close a connection.
1216
@param thd Thread handle
1209
@param session Thread handle
1217
1210
@param errcode Error code to print to console
1218
1211
@param lock 1 if we have have to lock LOCK_thread_count
1221
1214
For the connection that is doing shutdown, this is called twice
1223
void close_connection(THD *thd, uint32_t errcode, bool lock)
1216
void close_connection(Session *session, uint32_t errcode, bool lock)
1227
1220
(void) pthread_mutex_lock(&LOCK_thread_count);
1228
thd->killed= THD::KILL_CONNECTION;
1229
if ((vio= thd->net.vio) != 0)
1221
session->killed= Session::KILL_CONNECTION;
1222
if ((vio= session->net.vio) != 0)
1232
net_send_error(thd, errcode, ER(errcode)); /* purecov: inspected */
1233
net_close(&(thd->net)); /* vio is freed in delete thd */
1225
net_send_error(session, errcode, ER(errcode)); /* purecov: inspected */
1226
net_close(&(session->net)); /* vio is freed in delete session */
1236
1229
(void) pthread_mutex_unlock(&LOCK_thread_count);
1243
1236
extern "C" RETSIGTYPE end_thread_signal(int sig __attribute__((unused)))
1245
THD *thd=current_thd;
1238
Session *session=current_session;
1248
1241
statistic_increment(killed_threads, &LOCK_status);
1249
thread_scheduler.end_thread(thd,0); /* purecov: inspected */
1242
thread_scheduler.end_thread(session,0); /* purecov: inspected */
1251
1244
return;; /* purecov: deadcode */
1256
Unlink thd from global list of available connections and free thd
1249
Unlink session from global list of available connections and free session
1253
session Thread handler
1263
1256
LOCK_thread_count is locked and left locked
1266
void unlink_thd(THD *thd)
1259
void unlink_session(Session *session)
1270
1263
pthread_mutex_lock(&LOCK_connection_count);
1271
1264
--connection_count;
1309
1302
pthread_cond_signal(&COND_flush_thread_cache);
1310
1303
if (wake_thread)
1314
thd= thread_cache.get();
1315
thd->thread_stack= (char*) &thd; // For store_globals
1316
(void) thd->store_globals();
1307
session= thread_cache.get();
1308
session->thread_stack= (char*) &session; // For store_globals
1309
(void) session->store_globals();
1318
THD::mysys_var::abort is associated with physical thread rather
1319
than with THD object. So we need to reset this flag before using
1320
this thread for handling of new THD object/connection.
1311
Session::mysys_var::abort is associated with physical thread rather
1312
than with Session object. So we need to reset this flag before using
1313
this thread for handling of new Session object/connection.
1322
thd->mysys_var->abort= 0;
1323
thd->thr_create_utime= my_micro_time();
1324
threads.append(thd);
1315
session->mysys_var->abort= 0;
1316
session->thr_create_utime= my_micro_time();
1317
threads.append(session);
1348
1341
0 Signal to handle_one_connection to reuse connection
1351
bool one_thread_per_connection_end(THD *thd, bool put_in_cache)
1344
bool one_thread_per_connection_end(Session *session, bool put_in_cache)
1346
unlink_session(session);
1354
1347
if (put_in_cache)
1355
1348
put_in_cache= cache_thread();
1356
1349
pthread_mutex_unlock(&LOCK_thread_count);
1457
1450
(global_system_variables.read_buff_size +
1458
1451
global_system_variables.sortbuff_size) *
1459
1452
thread_scheduler.max_threads +
1460
max_connections * sizeof(THD)) / 1024);
1453
max_connections * sizeof(Session)) / 1024);
1462
1455
#ifdef HAVE_STACKTRACE
1463
THD *thd= current_thd;
1456
Session *session= current_session;
1465
1458
if (!(test_flags & TEST_NO_STACKTRACE))
1467
fprintf(stderr,"thd: 0x%lx\n",(long) thd);
1460
fprintf(stderr,"session: 0x%lx\n",(long) session);
1468
1461
fprintf(stderr,_("Attempting backtrace. You can use the following "
1469
1462
"information to find out\n"
1470
1463
"where mysqld died. If you see no messages after this, "
1471
1464
"something went\n"
1472
1465
"terribly wrong...\n"));
1473
print_stacktrace(thd ? (unsigned char*) thd->thread_stack : (unsigned char*) 0,
1466
print_stacktrace(session ? (unsigned char*) session->thread_stack : (unsigned char*) 0,
1474
1467
my_thread_stack_size);
1478
1471
const char *kreason= "UNKNOWN";
1479
switch (thd->killed) {
1480
case THD::NOT_KILLED:
1472
switch (session->killed) {
1473
case Session::NOT_KILLED:
1481
1474
kreason= "NOT_KILLED";
1483
case THD::KILL_BAD_DATA:
1476
case Session::KILL_BAD_DATA:
1484
1477
kreason= "KILL_BAD_DATA";
1486
case THD::KILL_CONNECTION:
1479
case Session::KILL_CONNECTION:
1487
1480
kreason= "KILL_CONNECTION";
1489
case THD::KILL_QUERY:
1482
case Session::KILL_QUERY:
1490
1483
kreason= "KILL_QUERY";
1492
case THD::KILLED_NO_VALUE:
1485
case Session::KILLED_NO_VALUE:
1493
1486
kreason= "KILLED_NO_VALUE";
1496
1489
fprintf(stderr, _("Trying to get some variables.\n"
1497
1490
"Some pointers may be invalid and cause the "
1498
1491
"dump to abort...\n"));
1499
safe_print_str("thd->query", thd->query, 1024);
1500
fprintf(stderr, "thd->thread_id=%"PRIu32"\n", (uint32_t) thd->thread_id);
1501
fprintf(stderr, "thd->killed=%s\n", kreason);
1492
safe_print_str("session->query", session->query, 1024);
1493
fprintf(stderr, "session->thread_id=%"PRIu32"\n", (uint32_t) session->thread_id);
1494
fprintf(stderr, "session->killed=%s\n", kreason);
1503
1496
fflush(stderr);
1504
1497
#endif /* HAVE_STACKTRACE */
1816
1809
void my_message_sql(uint32_t error, const char *str, myf MyFlags)
1820
1813
Put here following assertion when situation with EE_* error codes
1823
if ((thd= current_thd))
1816
if ((session= current_session))
1825
1818
if (MyFlags & ME_FATALERROR)
1826
thd->is_fatal_error= 1;
1819
session->is_fatal_error= 1;
1829
TODO: There are two exceptions mechanism (THD and sp_rcontext),
1822
TODO: There are two exceptions mechanism (Session and sp_rcontext),
1830
1823
this could be improved by having a common stack of handlers.
1832
if (thd->handle_error(error, str,
1825
if (session->handle_error(error, str,
1833
1826
DRIZZLE_ERROR::WARN_LEVEL_ERROR))
1836
thd->is_slave_error= 1; // needed to catch query errors during replication
1829
session->is_slave_error= 1; // needed to catch query errors during replication
1839
thd->lex->current_select == 0 if lex structure is not inited
1832
session->lex->current_select == 0 if lex structure is not inited
1840
1833
(not query command (COM_QUERY))
1842
if (! (thd->lex->current_select &&
1843
thd->lex->current_select->no_error && !thd->is_fatal_error))
1835
if (! (session->lex->current_select &&
1836
session->lex->current_select->no_error && !session->is_fatal_error))
1845
if (! thd->main_da.is_error()) // Return only first message
1838
if (! session->main_da.is_error()) // Return only first message
1847
1840
if (error == 0)
1848
1841
error= ER_UNKNOWN_ERROR;
1849
1842
if (str == NULL)
1850
1843
str= ER(error);
1851
thd->main_da.set_error_status(thd, error, str);
1844
session->main_da.set_error_status(session, error, str);
1855
if (!thd->no_warnings_for_error && !thd->is_fatal_error)
1848
if (!session->no_warnings_for_error && !session->is_fatal_error)
1858
1851
Suppress infinite recursion if there a memory allocation error
1859
1852
inside push_warning.
1861
thd->no_warnings_for_error= true;
1862
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR, error, str);
1863
thd->no_warnings_for_error= false;
1854
session->no_warnings_for_error= true;
1855
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR, error, str);
1856
session->no_warnings_for_error= false;
1866
if (!thd || MyFlags & ME_NOREFRESH)
1859
if (!session || MyFlags & ME_NOREFRESH)
1867
1860
sql_print_error("%s: %s",my_progname,str); /* purecov: inspected */
2263
2256
(void)pthread_attr_setschedparam(&connection_attrib, &tmp_sched_param);
2266
if (pthread_key_create(&THR_THD,NULL) ||
2259
if (pthread_key_create(&THR_Session,NULL) ||
2267
2260
pthread_key_create(&THR_MALLOC,NULL))
2269
2262
sql_print_error(_("Can't create thread-keys"));
2709
2702
This function will create new thread to handle the incoming
2710
2703
connection. If there are idle cached threads one will be used.
2711
'thd' will be pushed into 'threads'.
2704
'session' will be pushed into 'threads'.
2713
2706
In single-threaded mode (\#define ONE_THREAD) connection will be
2714
2707
handled inside this function.
2716
@param[in,out] thd Thread handle of future thread.
2709
@param[in,out] session Thread handle of future thread.
2719
static void create_new_thread(THD *thd)
2712
static void create_new_thread(Session *session)
2747
2740
pthread_mutex_lock(&LOCK_thread_count);
2750
The initialization of thread_id is done in create_embedded_thd() for
2743
The initialization of thread_id is done in create_embedded_session() for
2751
2744
the embedded library.
2752
2745
TODO: refactor this to avoid code duplication there
2754
thd->thread_id= thd->variables.pseudo_thread_id= thread_id++;
2747
session->thread_id= session->variables.pseudo_thread_id= thread_id++;
2756
2749
thread_count++;
2758
thread_scheduler.add_connection(thd);
2751
thread_scheduler.add_connection(session);
2873
2866
** Don't allow too many connections
2876
if (!(thd= new THD))
2869
if (!(session= new Session))
2878
2871
(void) shutdown(new_sock, SHUT_RDWR);
2879
2872
close(new_sock);
2882
if (net_init_sock(&thd->net, new_sock, sock == 0))
2875
if (net_init_sock(&session->net, new_sock, sock == 0))
2888
create_new_thread(thd);
2881
create_new_thread(session);
3922
3915
{0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
3925
static int show_net_compression(THD *thd __attribute__((unused)),
3918
static int show_net_compression(Session *session __attribute__((unused)),
3927
3920
char *buff __attribute__((unused)))
3929
3922
var->type= SHOW_MY_BOOL;
3930
var->value= (char *)&thd->net.compress;
3923
var->value= (char *)&session->net.compress;
3934
3927
static st_show_var_func_container
3935
3928
show_net_compression_cont= { &show_net_compression };
3937
static int show_starttime(THD *thd, SHOW_VAR *var, char *buff)
3930
static int show_starttime(Session *session, SHOW_VAR *var, char *buff)
3939
3932
var->type= SHOW_LONG;
3940
3933
var->value= buff;
3941
*((long *)buff)= (long) (thd->query_start() - server_start_time);
3934
*((long *)buff)= (long) (session->query_start() - server_start_time);
3945
3938
static st_show_var_func_container
3946
3939
show_starttime_cont= { &show_starttime };
3948
static int show_flushstatustime(THD *thd, SHOW_VAR *var, char *buff)
3941
static int show_flushstatustime(Session *session, SHOW_VAR *var, char *buff)
3950
3943
var->type= SHOW_LONG;
3951
3944
var->value= buff;
3952
*((long *)buff)= (long) (thd->query_start() - flush_status_time);
3945
*((long *)buff)= (long) (session->query_start() - flush_status_time);
3956
3949
static st_show_var_func_container
3957
3950
show_flushstatustime_cont= { &show_flushstatustime };
3959
static int show_slave_running(THD *thd __attribute__((unused)),
3952
static int show_slave_running(Session *session __attribute__((unused)),
3960
3953
SHOW_VAR *var, char *buff)
3962
3955
var->type= SHOW_MY_BOOL;
3971
3964
static st_show_var_func_container
3972
3965
show_slave_running_cont= { &show_slave_running };
3974
static int show_slave_retried_trans(THD *thd __attribute__((unused)),
3967
static int show_slave_retried_trans(Session *session __attribute__((unused)),
3975
3968
SHOW_VAR *var, char *buff)
3996
3989
static st_show_var_func_container
3997
3990
show_slave_retried_trans_cont= { &show_slave_retried_trans };
3999
static int show_slave_received_heartbeats(THD *thd __attribute__((unused)),
3992
static int show_slave_received_heartbeats(Session *session __attribute__((unused)),
4000
3993
SHOW_VAR *var, char *buff)
4002
3995
pthread_mutex_lock(&LOCK_active_mi);
4017
4010
static st_show_var_func_container
4018
4011
show_slave_received_heartbeats_cont= { &show_slave_received_heartbeats };
4020
static int show_heartbeat_period(THD *thd __attribute__((unused)),
4013
static int show_heartbeat_period(Session *session __attribute__((unused)),
4021
4014
SHOW_VAR *var, char *buff)
4023
4016
pthread_mutex_lock(&LOCK_active_mi);
4036
4029
static st_show_var_func_container
4037
4030
show_heartbeat_period_cont= { &show_heartbeat_period};
4039
static int show_open_tables(THD *thd __attribute__((unused)),
4032
static int show_open_tables(Session *session __attribute__((unused)),
4040
4033
SHOW_VAR *var, char *buff)
4042
4035
var->type= SHOW_LONG;
4254
4247
log_error_file_ptr= log_error_file;
4255
4248
language_ptr= language;
4256
4249
mysql_data_home= mysql_real_data_home;
4257
thd_startup_options= (OPTION_AUTO_IS_NULL | OPTION_BIN_LOG |
4250
session_startup_options= (OPTION_AUTO_IS_NULL | OPTION_BIN_LOG |
4258
4251
OPTION_QUOTE_SHOW_CREATE | OPTION_SQL_NOTES);
4259
4252
protocol_version= PROTOCOL_VERSION;
4260
4253
what_to_log= ~ (1L << (uint) COM_TIME);
4704
4697
test_flags&= ~TEST_CORE_ON_SIGNAL;
4706
4699
/* Set global MyISAM variables from delay_key_write_options */
4707
fix_delay_key_write((THD*) 0, OPT_GLOBAL);
4700
fix_delay_key_write((Session*) 0, OPT_GLOBAL);
4708
4701
/* Set global slave_exec_mode from its option */
4709
4702
fix_slave_exec_mode(OPT_GLOBAL);
4973
4966
/** Clear most status variables. */
4974
void refresh_status(THD *thd)
4967
void refresh_status(Session *session)
4976
4969
pthread_mutex_lock(&LOCK_status);
4978
4971
/* Add thread's status variabes to global status */
4979
add_to_status(&global_status_var, &thd->status_var);
4972
add_to_status(&global_status_var, &session->status_var);
4981
4974
/* Reset thread's status variables */
4982
memset(&thd->status_var, 0, sizeof(thd->status_var));
4975
memset(&session->status_var, 0, sizeof(session->status_var));
4984
4977
/* Reset some global variables */
4985
4978
reset_status_vars();
5008
5001
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
5009
5002
/* Used templates */
5010
template class I_List<THD>;
5011
template class I_List_iterator<THD>;
5003
template class I_List<Session>;
5004
template class I_List_iterator<Session>;
5012
5005
template class I_List<i_string>;
5013
5006
template class I_List<i_string_pair>;
5014
5007
template class I_List<NAMED_LIST>;