~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/main.cc

  • Committer: Mark Atwood
  • Date: 2012-01-04 16:59:32 UTC
  • mfrom: (2478.2.3 real-key-use-catalog)
  • Revision ID: me@mark.atwood.name-20120104165932-cm0xqs4by0u3p4cy
mergeĀ lp:~stewart/drizzle/key-use-catalog

Show diffs side-by-side

added added

removed removed

Lines of Context:
63
63
#include <drizzled/session/cache.h>
64
64
#include <drizzled/signal_handler.h>
65
65
#include <drizzled/transaction_services.h>
66
 
#include <drizzled/tztime.h>
67
66
#include <drizzled/unireg.h>
68
67
#include <drizzled/util/backtrace.h>
69
68
#include <drizzled/current_session.h>
70
69
#include <drizzled/daemon.h>
 
70
#include <drizzled/diagnostics_area.h>
 
71
#include <drizzled/sql_base.h>
 
72
#include <drizzled/sql_lex.h>
 
73
#include <drizzled/system_variables.h>
71
74
 
72
75
using namespace drizzled;
73
76
using namespace std;
84
87
*/
85
88
static void my_message_sql(drizzled::error_t error, const char *str, myf MyFlags)
86
89
{
87
 
  Session *session;
 
90
  Session* session= current_session;
88
91
  /*
89
92
    Put here following assertion when situation with EE_* error codes
90
93
    will be fixed
91
94
  */
92
 
  if ((session= current_session))
 
95
  if (session)
93
96
  {
94
97
    if (MyFlags & ME_FATALERROR)
 
98
    {
95
99
      session->is_fatal_error= 1;
 
100
    }
96
101
 
97
102
    /*
98
103
      @TODO There are two exceptions mechanism (Session and sp_rcontext),
99
104
      this could be improved by having a common stack of handlers.
100
 
    */
 
105
 
101
106
    if (session->handle_error(error, str, DRIZZLE_ERROR::WARN_LEVEL_ERROR))
102
107
      return;
 
108
    */
103
109
 
104
110
    /*
105
 
      session->getLex()->current_select == 0 if lex structure is not inited
 
111
      session->lex().current_select == 0 if lex structure is not inited
106
112
      (not query command (COM_QUERY))
107
113
    */
108
 
    if (! (session->getLex()->current_select &&
109
 
           session->getLex()->current_select->no_error && !session->is_fatal_error))
 
114
    if (! (session->lex().current_select &&
 
115
           session->lex().current_select->no_error && !session->is_fatal_error))
110
116
    {
111
 
      if (! session->main_da.is_error())            // Return only first message
 
117
      if (! session->main_da().is_error())            // Return only first message
112
118
      {
113
119
        if (error == EE_OK)
114
120
          error= ER_UNKNOWN_ERROR;
116
122
        if (str == NULL)
117
123
          str= ER(error);
118
124
 
119
 
        session->main_da.set_error_status(error, str);
 
125
        session->main_da().set_error_status(error, str);
120
126
      }
121
127
    }
122
128
 
143
149
  sigset_t set;
144
150
  struct sigaction sa;
145
151
 
146
 
  if (not (getDebug().test(debug::NO_STACKTRACE) || 
 
152
  if (not (getDebug().test(debug::NO_STACKTRACE) ||
147
153
        getDebug().test(debug::CORE_ON_SIGNAL)))
148
154
  {
149
155
    sa.sa_flags = SA_RESETHAND | SA_NODEFER;
237
243
 
238
244
  module::Registry &modules= module::Registry::singleton();
239
245
 
240
 
  MY_INIT(argv[0]);             // init my_sys library & pthreads
 
246
  drizzled::internal::my_progname= argv[0]; 
 
247
  drizzled::internal::my_init();
 
248
 
241
249
  /* nothing should come before this line ^^^ */
242
250
 
243
251
  /* Set signal used to kill Drizzle */
248
256
  /* Function generates error messages before abort */
249
257
  error_handler_hook= my_message_sql;
250
258
 
251
 
  /* init_common_variables must get basic settings such as data_home_dir
252
 
     and plugin_load_list. */
253
 
  if (init_basic_variables(argc, argv))
254
 
    unireg_abort(1);                            // Will do exit
 
259
  /* init_common_variables must get basic settings such as data_home_dir and plugin_load_list. */
 
260
  if (not init_variables_before_daemonizing(argc, argv))
 
261
  {
 
262
    unireg_abort << "init_variables_before_daemonizing() failed";                               // Will do exit
 
263
  }
255
264
 
256
 
  if (opt_daemon)
 
265
  if (opt_daemon and was_help_requested() == false)
257
266
  {
258
267
    if (signal(SIGHUP, SIG_IGN) == SIG_ERR)
259
268
    {
261
270
    }
262
271
    if (daemonize())
263
272
    {
264
 
      fprintf(stderr, "failed to daemon() in order to daemonize\n");
265
 
      exit(EXIT_FAILURE);
 
273
      unireg_abort << "--daemon failed";
266
274
    }
267
275
  }
268
276
 
269
 
  if (init_remaining_variables(modules))
270
 
    unireg_abort(1);                            // Will do exit
 
277
  if (not init_variables_after_daemonizing(modules))
 
278
  {
 
279
    unireg_abort << "init_variables_after_daemonizing() failed";                                // Will do exit
 
280
  }
 
281
 
271
282
 
272
283
  /*
273
284
    init signals & alarm
276
287
  init_signals();
277
288
 
278
289
 
279
 
  select_thread=pthread_self();
 
290
  select_thread= pthread_self();
280
291
  select_thread_in_use=1;
281
292
 
282
 
  if (not opt_help)
 
293
  if (was_help_requested() == false)
283
294
  {
284
295
    if (chdir(getDataHome().file_string().c_str()))
285
296
    {
286
 
      errmsg_printf(error::ERROR,
287
 
                    _("Data directory %s does not exist\n"),
288
 
                    getDataHome().file_string().c_str());
289
 
      unireg_abort(1);
290
 
    }
291
 
    if (mkdir("local", 0700))
292
 
    {
293
 
      /* We don't actually care */
294
 
    }
295
 
    if (chdir("local"))
296
 
    {
297
 
      errmsg_printf(error::ERROR,
298
 
                    _("Local catalog %s/local does not exist\n"),
299
 
                    getDataHome().file_string().c_str());
300
 
      unireg_abort(1);
301
 
    }
302
 
 
303
 
    boost::filesystem::path &full_data_home= getFullDataHome();
304
 
    full_data_home= boost::filesystem::system_complete(getDataHome());
305
 
    errmsg_printf(error::INFO, "Data Home directory is : %s", full_data_home.native_file_string().c_str());
 
297
      unireg_abort << "Data directory " << getDataHome().file_string() << " does not exist";
 
298
    }
 
299
 
 
300
    ifstream old_uuid_file ("server.uuid");
 
301
    if (old_uuid_file.is_open())
 
302
    {
 
303
      getline(old_uuid_file, server_uuid);
 
304
      old_uuid_file.close();
 
305
    } 
 
306
    else 
 
307
    {
 
308
      uuid_t uu;
 
309
      char uuid_string[37];
 
310
      uuid_generate_random(uu);
 
311
      uuid_unparse(uu, uuid_string);
 
312
      ofstream new_uuid_file ("server.uuid");
 
313
      new_uuid_file << uuid_string;
 
314
      new_uuid_file.close();
 
315
      server_uuid= string(uuid_string);
 
316
    }
 
317
 
 
318
    if (mkdir("local", 0700) == -1)
 
319
    {
 
320
      switch (errno)
 
321
      {
 
322
      case EEXIST:
 
323
        break;
 
324
 
 
325
      case EACCES:
 
326
        {
 
327
          char cwd[1024];
 
328
          unireg_abort << "Could not create local catalog, permission denied in directory:" << getcwd(cwd, sizeof(cwd));
 
329
        }
 
330
 
 
331
      default:
 
332
        {
 
333
          char cwd[1024];
 
334
          unireg_abort << "Could not create local catalog, in directory:" << getcwd(cwd, sizeof(cwd)) << " system error was:" << strerror(errno);
 
335
        }
 
336
      }
 
337
    }
 
338
 
 
339
    if (chdir("local") == -1)
 
340
    {
 
341
      unireg_abort << "Local catalog does not exist, was unable to chdir() to " << getDataHome().file_string();
 
342
    }
 
343
 
 
344
    setFullDataHome(boost::filesystem::system_complete(getDataHome()));
 
345
    errmsg_printf(error::INFO, "Data Home directory is : %s", getFullDataHome().native_file_string().c_str());
306
346
  }
307
347
 
308
 
 
309
 
 
310
348
  if (server_id == 0)
311
349
  {
312
350
    server_id= 1;
314
352
 
315
353
  try
316
354
  {
317
 
    if (init_server_components(modules))
318
 
      DRIZZLE_ABORT;
 
355
    init_server_components(modules);
319
356
  }
320
357
  catch (abort_exception& ex)
321
358
  {
325
362
    cout << _("In File: ") << *::boost::get_error_info<boost::throw_file>(ex) << endl;
326
363
    cout << _("On Line: ") << *::boost::get_error_info<boost::throw_line>(ex) << endl;
327
364
#endif
328
 
    unireg_abort(1);
 
365
    unireg_abort << "init_server_components() failed";
329
366
  }
330
367
 
331
368
 
340
377
   *
341
378
   * not checking return since unireg_abort() hangs
342
379
   */
343
 
  ReplicationServices &replication_services= ReplicationServices::singleton();
344
 
    (void) replication_services.evaluateRegisteredPlugins();
 
380
  (void) ReplicationServices::evaluateRegisteredPlugins();
345
381
 
346
382
  if (plugin::Listen::setup())
347
 
    unireg_abort(1);
 
383
  {
 
384
    unireg_abort << "Failed plugin::Listen::setup()";
 
385
  }
348
386
 
349
387
  assert(plugin::num_trx_monitored_objects > 0);
350
 
  if (drizzle_rm_tmp_tables())
351
 
  {
352
 
    abort_loop= true;
353
 
    select_thread_in_use=0;
354
 
    (void) pthread_kill(signal_thread, SIGTERM);
355
 
 
356
 
    (void) unlink(pid_file.file_string().c_str());      // Not needed anymore
357
 
 
358
 
    unireg_abort(1);
359
 
  }
360
 
 
361
 
  errmsg_printf(error::INFO, _(ER(ER_STARTUP)), internal::my_progname,
362
 
                PANDORA_RELEASE_VERSION, COMPILATION_COMMENT);
363
 
 
364
 
 
365
 
  TransactionServices &transaction_services= TransactionServices::singleton();
 
388
  drizzle_rm_tmp_tables();
 
389
  errmsg_printf(error::INFO, _(ER(ER_STARTUP)), internal::my_progname, PANDORA_RELEASE_VERSION, COMPILATION_COMMENT);
366
390
 
367
391
  /* Send server startup event */
368
392
  {
369
 
    Session::shared_ptr session;
370
 
 
371
 
    if ((session= Session::make_shared(plugin::Listen::getNullClient(), catalog::local())))
372
 
    {
373
 
      currentSession().release();
374
 
      currentSession().reset(session.get());
375
 
 
376
 
 
377
 
      transaction_services.sendStartupEvent(*session);
378
 
 
379
 
      plugin_startup_window(modules, *(session.get()));
380
 
    }
 
393
    Session::shared_ptr session= Session::make_shared(plugin::Listen::getNullClient(), catalog::local());
 
394
    setCurrentSession(session.get());
 
395
    TransactionServices::sendStartupEvent(*session);
 
396
    plugin_startup_window(modules, *session.get());
381
397
  }
382
398
 
383
399
  if (opt_daemon)
 
400
  {
384
401
    daemon_is_ready();
385
 
 
386
 
  /* 
 
402
  }
 
403
 
 
404
 
 
405
  errmsg_printf(error::INFO, "Drizzle startup complete, listening for connections will now begin.");
 
406
 
 
407
  /*
387
408
    Listen for new connections and start new session for each connection
388
409
     accepted. The listen.getClient() method will return NULL when the server
389
410
     should be shutdown.
390
411
   */
391
 
  plugin::Client *client;
392
 
  while ((client= plugin::Listen::getClient()) != NULL)
 
412
  while (plugin::Client* client= plugin::Listen::getClient())
393
413
  {
394
 
    Session::shared_ptr session;
395
 
    session= Session::make_shared(client, client->catalog());
396
 
 
397
 
    if (not session)
398
 
    {
399
 
      delete client;
400
 
      continue;
401
 
    }
 
414
    Session::shared_ptr session= Session::make_shared(client, client->catalog());
402
415
 
403
416
    /* If we error on creation we drop the connection and delete the session. */
404
417
    if (Session::schedule(session))
407
420
 
408
421
  /* Send server shutdown event */
409
422
  {
410
 
    Session::shared_ptr session;
411
 
 
412
 
    if ((session= Session::make_shared(plugin::Listen::getNullClient(), catalog::local())))
413
 
    {
414
 
      currentSession().release();
415
 
      currentSession().reset(session.get());
416
 
      transaction_services.sendShutdownEvent(*session.get());
417
 
    }
 
423
    Session::shared_ptr session= Session::make_shared(plugin::Listen::getNullClient(), catalog::local());
 
424
    setCurrentSession(session.get());
 
425
    TransactionServices::sendShutdownEvent(*session.get());
418
426
  }
419
427
 
420
428
  {
421
 
    boost::mutex::scoped_lock scopedLock(session::Cache::singleton().mutex());
 
429
    boost::mutex::scoped_lock scopedLock(session::Cache::mutex());
422
430
    select_thread_in_use= false;                        // For close_connections
423
431
  }
424
432
  COND_thread_count.notify_all();
425
433
 
426
434
  /* Wait until cleanup is done */
427
 
  session::Cache::singleton().shutdownSecond();
 
435
  session::Cache::shutdownSecond();
428
436
 
429
437
  clean_up(1);
430
438
  module::Registry::shutdown();
431
439
  internal::my_end();
432
440
 
 
441
  errmsg_printf(error::INFO, "Drizzle is now shutting down");
 
442
 
433
443
  return 0;
434
444
}
435
445