~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/drizzled.cc

  • Committer: Brian Aker
  • Date: 2008-10-20 04:28:21 UTC
  • mto: (492.3.21 drizzle-clean-code)
  • mto: This revision was merged to the branch mainline in revision 530.
  • Revision ID: brian@tangent.org-20081020042821-rqqdrccuu8195k3y
Second pass of thd cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
296
296
uint32_t lower_case_table_names= 1;
297
297
uint32_t tc_heuristic_recover= 0;
298
298
uint32_t volatile thread_count, thread_running;
299
 
uint64_t thd_startup_options;
 
299
uint64_t session_startup_options;
300
300
ulong back_log, connect_timeout, server_id;
301
301
ulong table_cache_size, table_def_size;
302
302
ulong what_to_log;
1206
1206
/**
1207
1207
  Close a connection.
1208
1208
 
1209
 
  @param thd            Thread handle
 
1209
  @param session                Thread handle
1210
1210
  @param errcode        Error code to print to console
1211
1211
  @param lock           1 if we have have to lock LOCK_thread_count
1212
1212
 
1213
1213
  @note
1214
1214
    For the connection that is doing shutdown, this is called twice
1215
1215
*/
1216
 
void close_connection(Session *thd, uint32_t errcode, bool lock)
 
1216
void close_connection(Session *session, uint32_t errcode, bool lock)
1217
1217
{
1218
1218
  st_vio *vio;
1219
1219
  if (lock)
1220
1220
    (void) pthread_mutex_lock(&LOCK_thread_count);
1221
 
  thd->killed= Session::KILL_CONNECTION;
1222
 
  if ((vio= thd->net.vio) != 0)
 
1221
  session->killed= Session::KILL_CONNECTION;
 
1222
  if ((vio= session->net.vio) != 0)
1223
1223
  {
1224
1224
    if (errcode)
1225
 
      net_send_error(thd, errcode, ER(errcode)); /* purecov: inspected */
1226
 
    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 */
1227
1227
  }
1228
1228
  if (lock)
1229
1229
    (void) pthread_mutex_unlock(&LOCK_thread_count);
1235
1235
/* ARGSUSED */
1236
1236
extern "C" RETSIGTYPE end_thread_signal(int sig __attribute__((unused)))
1237
1237
{
1238
 
  Session *thd=current_thd;
1239
 
  if (thd)
 
1238
  Session *session=current_session;
 
1239
  if (session)
1240
1240
  {
1241
1241
    statistic_increment(killed_threads, &LOCK_status);
1242
 
    thread_scheduler.end_thread(thd,0);         /* purecov: inspected */
 
1242
    thread_scheduler.end_thread(session,0);             /* purecov: inspected */
1243
1243
  }
1244
1244
  return;;                              /* purecov: deadcode */
1245
1245
}
1246
1246
 
1247
1247
 
1248
1248
/*
1249
 
  Unlink thd from global list of available connections and free thd
 
1249
  Unlink session from global list of available connections and free session
1250
1250
 
1251
1251
  SYNOPSIS
1252
 
    unlink_thd()
1253
 
    thd          Thread handler
 
1252
    unlink_session()
 
1253
    session              Thread handler
1254
1254
 
1255
1255
  NOTES
1256
1256
    LOCK_thread_count is locked and left locked
1257
1257
*/
1258
1258
 
1259
 
void unlink_thd(Session *thd)
 
1259
void unlink_session(Session *session)
1260
1260
{
1261
 
  thd->cleanup();
 
1261
  session->cleanup();
1262
1262
 
1263
1263
  pthread_mutex_lock(&LOCK_connection_count);
1264
1264
  --connection_count;
1266
1266
 
1267
1267
  (void) pthread_mutex_lock(&LOCK_thread_count);
1268
1268
  thread_count--;
1269
 
  delete thd;
 
1269
  delete session;
1270
1270
  return;;
1271
1271
}
1272
1272
 
1302
1302
      pthread_cond_signal(&COND_flush_thread_cache);
1303
1303
    if (wake_thread)
1304
1304
    {
1305
 
      Session *thd;
 
1305
      Session *session;
1306
1306
      wake_thread--;
1307
 
      thd= thread_cache.get();
1308
 
      thd->thread_stack= (char*) &thd;          // For store_globals
1309
 
      (void) thd->store_globals();
 
1307
      session= thread_cache.get();
 
1308
      session->thread_stack= (char*) &session;          // For store_globals
 
1309
      (void) session->store_globals();
1310
1310
      /*
1311
1311
        Session::mysys_var::abort is associated with physical thread rather
1312
1312
        than with Session object. So we need to reset this flag before using
1313
1313
        this thread for handling of new Session object/connection.
1314
1314
      */
1315
 
      thd->mysys_var->abort= 0;
1316
 
      thd->thr_create_utime= my_micro_time();
1317
 
      threads.append(thd);
 
1315
      session->mysys_var->abort= 0;
 
1316
      session->thr_create_utime= my_micro_time();
 
1317
      threads.append(session);
1318
1318
      return(1);
1319
1319
    }
1320
1320
  }
1327
1327
 
1328
1328
  SYNOPSIS
1329
1329
    one_thread_per_connection_end()
1330
 
    thd           Thread handler
 
1330
    session               Thread handler
1331
1331
    put_in_cache  Store thread in cache, if there is room in it
1332
1332
                  Normally this is true in all cases except when we got
1333
1333
                  out of resources initializing the current thread
1341
1341
    0    Signal to handle_one_connection to reuse connection
1342
1342
*/
1343
1343
 
1344
 
bool one_thread_per_connection_end(Session *thd, bool put_in_cache)
 
1344
bool one_thread_per_connection_end(Session *session, bool put_in_cache)
1345
1345
{
1346
 
  unlink_thd(thd);
 
1346
  unlink_session(session);
1347
1347
  if (put_in_cache)
1348
1348
    put_in_cache= cache_thread();
1349
1349
  pthread_mutex_unlock(&LOCK_thread_count);
1382
1382
*/
1383
1383
extern "C" RETSIGTYPE abort_thread(int sig __attribute__((unused)))
1384
1384
{
1385
 
  Session *thd=current_thd;
1386
 
  if (thd)
1387
 
    thd->killed= Session::KILL_CONNECTION;
 
1385
  Session *session=current_session;
 
1386
  if (session)
 
1387
    session->killed= Session::KILL_CONNECTION;
1388
1388
  return;;
1389
1389
}
1390
1390
#endif
1453
1453
                     max_connections * sizeof(Session)) / 1024);
1454
1454
 
1455
1455
#ifdef HAVE_STACKTRACE
1456
 
  Session *thd= current_thd;
 
1456
  Session *session= current_session;
1457
1457
 
1458
1458
  if (!(test_flags & TEST_NO_STACKTRACE))
1459
1459
  {
1460
 
    fprintf(stderr,"thd: 0x%lx\n",(long) thd);
 
1460
    fprintf(stderr,"session: 0x%lx\n",(long) session);
1461
1461
    fprintf(stderr,_("Attempting backtrace. You can use the following "
1462
1462
                     "information to find out\n"
1463
1463
                     "where mysqld died. If you see no messages after this, "
1464
1464
                     "something went\n"
1465
1465
                     "terribly wrong...\n"));
1466
 
    print_stacktrace(thd ? (unsigned char*) thd->thread_stack : (unsigned char*) 0,
 
1466
    print_stacktrace(session ? (unsigned char*) session->thread_stack : (unsigned char*) 0,
1467
1467
                     my_thread_stack_size);
1468
1468
  }
1469
 
  if (thd)
 
1469
  if (session)
1470
1470
  {
1471
1471
    const char *kreason= "UNKNOWN";
1472
 
    switch (thd->killed) {
 
1472
    switch (session->killed) {
1473
1473
    case Session::NOT_KILLED:
1474
1474
      kreason= "NOT_KILLED";
1475
1475
      break;
1489
1489
    fprintf(stderr, _("Trying to get some variables.\n"
1490
1490
                      "Some pointers may be invalid and cause the "
1491
1491
                      "dump to abort...\n"));
1492
 
    safe_print_str("thd->query", thd->query, 1024);
1493
 
    fprintf(stderr, "thd->thread_id=%"PRIu32"\n", (uint32_t) thd->thread_id);
1494
 
    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);
1495
1495
  }
1496
1496
  fflush(stderr);
1497
1497
#endif /* HAVE_STACKTRACE */
1808
1808
 
1809
1809
void my_message_sql(uint32_t error, const char *str, myf MyFlags)
1810
1810
{
1811
 
  Session *thd;
 
1811
  Session *session;
1812
1812
  /*
1813
1813
    Put here following assertion when situation with EE_* error codes
1814
1814
    will be fixed
1815
1815
  */
1816
 
  if ((thd= current_thd))
 
1816
  if ((session= current_session))
1817
1817
  {
1818
1818
    if (MyFlags & ME_FATALERROR)
1819
 
      thd->is_fatal_error= 1;
 
1819
      session->is_fatal_error= 1;
1820
1820
 
1821
1821
    /*
1822
1822
      TODO: There are two exceptions mechanism (Session and sp_rcontext),
1823
1823
      this could be improved by having a common stack of handlers.
1824
1824
    */
1825
 
    if (thd->handle_error(error, str,
 
1825
    if (session->handle_error(error, str,
1826
1826
                          DRIZZLE_ERROR::WARN_LEVEL_ERROR))
1827
1827
      return;;
1828
1828
 
1829
 
    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
1830
1830
 
1831
1831
    /*
1832
 
      thd->lex->current_select == 0 if lex structure is not inited
 
1832
      session->lex->current_select == 0 if lex structure is not inited
1833
1833
      (not query command (COM_QUERY))
1834
1834
    */
1835
 
    if (! (thd->lex->current_select &&
1836
 
        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))
1837
1837
    {
1838
 
      if (! thd->main_da.is_error())            // Return only first message
 
1838
      if (! session->main_da.is_error())            // Return only first message
1839
1839
      {
1840
1840
        if (error == 0)
1841
1841
          error= ER_UNKNOWN_ERROR;
1842
1842
        if (str == NULL)
1843
1843
          str= ER(error);
1844
 
        thd->main_da.set_error_status(thd, error, str);
 
1844
        session->main_da.set_error_status(session, error, str);
1845
1845
      }
1846
1846
    }
1847
1847
 
1848
 
    if (!thd->no_warnings_for_error && !thd->is_fatal_error)
 
1848
    if (!session->no_warnings_for_error && !session->is_fatal_error)
1849
1849
    {
1850
1850
      /*
1851
1851
        Suppress infinite recursion if there a memory allocation error
1852
1852
        inside push_warning.
1853
1853
      */
1854
 
      thd->no_warnings_for_error= true;
1855
 
      push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR, error, str);
1856
 
      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;
1857
1857
    }
1858
1858
  }
1859
 
  if (!thd || MyFlags & ME_NOREFRESH)
 
1859
  if (!session || MyFlags & ME_NOREFRESH)
1860
1860
    sql_print_error("%s: %s",my_progname,str); /* purecov: inspected */
1861
1861
  return;;
1862
1862
}
2701
2701
 
2702
2702
    This function will create new thread to handle the incoming
2703
2703
    connection.  If there are idle cached threads one will be used.
2704
 
    'thd' will be pushed into 'threads'.
 
2704
    'session' will be pushed into 'threads'.
2705
2705
 
2706
2706
    In single-threaded mode (\#define ONE_THREAD) connection will be
2707
2707
    handled inside this function.
2708
2708
 
2709
 
  @param[in,out] thd    Thread handle of future thread.
 
2709
  @param[in,out] session    Thread handle of future thread.
2710
2710
*/
2711
2711
 
2712
 
static void create_new_thread(Session *thd)
 
2712
static void create_new_thread(Session *session)
2713
2713
{
2714
2714
 
2715
2715
  /*
2723
2723
  {
2724
2724
    pthread_mutex_unlock(&LOCK_connection_count);
2725
2725
 
2726
 
    close_connection(thd, ER_CON_COUNT_ERROR, 1);
2727
 
    delete thd;
 
2726
    close_connection(session, ER_CON_COUNT_ERROR, 1);
 
2727
    delete session;
2728
2728
    return;;
2729
2729
  }
2730
2730
 
2740
2740
  pthread_mutex_lock(&LOCK_thread_count);
2741
2741
 
2742
2742
  /*
2743
 
    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
2744
2744
    the embedded library.
2745
2745
    TODO: refactor this to avoid code duplication there
2746
2746
  */
2747
 
  thd->thread_id= thd->variables.pseudo_thread_id= thread_id++;
 
2747
  session->thread_id= session->variables.pseudo_thread_id= thread_id++;
2748
2748
 
2749
2749
  thread_count++;
2750
2750
 
2751
 
  thread_scheduler.add_connection(thd);
 
2751
  thread_scheduler.add_connection(session);
2752
2752
 
2753
2753
  return;;
2754
2754
}
2777
2777
  int x;
2778
2778
  int sock,new_sock;
2779
2779
  uint32_t error_count=0;
2780
 
  Session *thd;
 
2780
  Session *session;
2781
2781
  struct sockaddr_storage cAddr;
2782
2782
 
2783
2783
  MAYBE_BROKEN_SYSCALL;
2866
2866
    ** Don't allow too many connections
2867
2867
    */
2868
2868
 
2869
 
    if (!(thd= new Session))
 
2869
    if (!(session= new Session))
2870
2870
    {
2871
2871
      (void) shutdown(new_sock, SHUT_RDWR);
2872
2872
      close(new_sock);
2873
2873
      continue;
2874
2874
    }
2875
 
    if (net_init_sock(&thd->net, new_sock, sock == 0))
 
2875
    if (net_init_sock(&session->net, new_sock, sock == 0))
2876
2876
    {
2877
 
      delete thd;
 
2877
      delete session;
2878
2878
      continue;
2879
2879
    }
2880
2880
 
2881
 
    create_new_thread(thd);
 
2881
    create_new_thread(session);
2882
2882
  }
2883
2883
}
2884
2884
 
3915
3915
  {0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
3916
3916
};
3917
3917
 
3918
 
static int show_net_compression(Session *thd __attribute__((unused)),
 
3918
static int show_net_compression(Session *session __attribute__((unused)),
3919
3919
                                SHOW_VAR *var,
3920
3920
                                char *buff __attribute__((unused)))
3921
3921
{
3922
3922
  var->type= SHOW_MY_BOOL;
3923
 
  var->value= (char *)&thd->net.compress;
 
3923
  var->value= (char *)&session->net.compress;
3924
3924
  return 0;
3925
3925
}
3926
3926
 
3927
3927
static st_show_var_func_container
3928
3928
show_net_compression_cont= { &show_net_compression };
3929
3929
 
3930
 
static int show_starttime(Session *thd, SHOW_VAR *var, char *buff)
 
3930
static int show_starttime(Session *session, SHOW_VAR *var, char *buff)
3931
3931
{
3932
3932
  var->type= SHOW_LONG;
3933
3933
  var->value= buff;
3934
 
  *((long *)buff)= (long) (thd->query_start() - server_start_time);
 
3934
  *((long *)buff)= (long) (session->query_start() - server_start_time);
3935
3935
  return 0;
3936
3936
}
3937
3937
 
3938
3938
static st_show_var_func_container
3939
3939
show_starttime_cont= { &show_starttime };
3940
3940
 
3941
 
static int show_flushstatustime(Session *thd, SHOW_VAR *var, char *buff)
 
3941
static int show_flushstatustime(Session *session, SHOW_VAR *var, char *buff)
3942
3942
{
3943
3943
  var->type= SHOW_LONG;
3944
3944
  var->value= buff;
3945
 
  *((long *)buff)= (long) (thd->query_start() - flush_status_time);
 
3945
  *((long *)buff)= (long) (session->query_start() - flush_status_time);
3946
3946
  return 0;
3947
3947
}
3948
3948
 
3949
3949
static st_show_var_func_container
3950
3950
show_flushstatustime_cont= { &show_flushstatustime };
3951
3951
 
3952
 
static int show_slave_running(Session *thd __attribute__((unused)),
 
3952
static int show_slave_running(Session *session __attribute__((unused)),
3953
3953
                              SHOW_VAR *var, char *buff)
3954
3954
{
3955
3955
  var->type= SHOW_MY_BOOL;
3964
3964
static st_show_var_func_container
3965
3965
show_slave_running_cont= { &show_slave_running };
3966
3966
 
3967
 
static int show_slave_retried_trans(Session *thd __attribute__((unused)),
 
3967
static int show_slave_retried_trans(Session *session __attribute__((unused)),
3968
3968
                                    SHOW_VAR *var, char *buff)
3969
3969
{
3970
3970
  /*
3989
3989
static st_show_var_func_container
3990
3990
show_slave_retried_trans_cont= { &show_slave_retried_trans };
3991
3991
 
3992
 
static int show_slave_received_heartbeats(Session *thd __attribute__((unused)),
 
3992
static int show_slave_received_heartbeats(Session *session __attribute__((unused)),
3993
3993
                                          SHOW_VAR *var, char *buff)
3994
3994
{
3995
3995
  pthread_mutex_lock(&LOCK_active_mi);
4010
4010
static st_show_var_func_container
4011
4011
show_slave_received_heartbeats_cont= { &show_slave_received_heartbeats };
4012
4012
 
4013
 
static int show_heartbeat_period(Session *thd __attribute__((unused)),
 
4013
static int show_heartbeat_period(Session *session __attribute__((unused)),
4014
4014
                                 SHOW_VAR *var, char *buff)
4015
4015
{
4016
4016
  pthread_mutex_lock(&LOCK_active_mi);
4029
4029
static st_show_var_func_container
4030
4030
show_heartbeat_period_cont= { &show_heartbeat_period};
4031
4031
 
4032
 
static int show_open_tables(Session *thd __attribute__((unused)),
 
4032
static int show_open_tables(Session *session __attribute__((unused)),
4033
4033
                            SHOW_VAR *var, char *buff)
4034
4034
{
4035
4035
  var->type= SHOW_LONG;
4038
4038
  return 0;
4039
4039
}
4040
4040
 
4041
 
static int show_table_definitions(Session *thd __attribute__((unused)),
 
4041
static int show_table_definitions(Session *session __attribute__((unused)),
4042
4042
                                  SHOW_VAR *var, char *buff)
4043
4043
{
4044
4044
  var->type= SHOW_LONG;
4247
4247
  log_error_file_ptr= log_error_file;
4248
4248
  language_ptr= language;
4249
4249
  mysql_data_home= mysql_real_data_home;
4250
 
  thd_startup_options= (OPTION_AUTO_IS_NULL | OPTION_BIN_LOG |
 
4250
  session_startup_options= (OPTION_AUTO_IS_NULL | OPTION_BIN_LOG |
4251
4251
                        OPTION_QUOTE_SHOW_CREATE | OPTION_SQL_NOTES);
4252
4252
  protocol_version= PROTOCOL_VERSION;
4253
4253
  what_to_log= ~ (1L << (uint) COM_TIME);
4964
4964
}
4965
4965
 
4966
4966
/** Clear most status variables. */
4967
 
void refresh_status(Session *thd)
 
4967
void refresh_status(Session *session)
4968
4968
{
4969
4969
  pthread_mutex_lock(&LOCK_status);
4970
4970
 
4971
4971
  /* Add thread's status variabes to global status */
4972
 
  add_to_status(&global_status_var, &thd->status_var);
 
4972
  add_to_status(&global_status_var, &session->status_var);
4973
4973
 
4974
4974
  /* Reset thread's status variables */
4975
 
  memset(&thd->status_var, 0, sizeof(thd->status_var));
 
4975
  memset(&session->status_var, 0, sizeof(session->status_var));
4976
4976
 
4977
4977
  /* Reset some global variables */
4978
4978
  reset_status_vars();