~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/main.cc

  • Committer: Stewart Smith
  • Date: 2010-11-03 03:30:27 UTC
  • mto: (1902.1.1 build) (1910.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 1903.
  • Revision ID: stewart@flamingspork.com-20101103033027-lskb6gxwwforfz71
fix docs warning: underline/overline too short for replace.rst

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2008 Sun Microsystems
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include <config.h>
 
20
#include "config.h"
21
21
 
22
22
#include <pthread.h>
23
23
#include <signal.h>
44
44
 
45
45
#include <boost/filesystem.hpp>
46
46
 
47
 
#include <drizzled/abort_exception.h>
48
 
#include <drizzled/catalog/local.h>
49
 
#include <drizzled/configmake.h>
50
 
#include <drizzled/data_home.h>
51
 
#include <drizzled/debug.h>
52
 
#include <drizzled/drizzled.h>
53
 
#include <drizzled/errmsg_print.h>
54
 
#include <drizzled/gettext.h>
55
 
#include <drizzled/internal/my_sys.h>
56
 
#include <drizzled/plugin.h>
57
 
#include <drizzled/plugin/client.h>
58
 
#include <drizzled/plugin/listen.h>
59
 
#include <drizzled/plugin/monitored_in_transaction.h>
60
 
#include <drizzled/pthread_globals.h>
61
 
#include <drizzled/replication_services.h>
62
 
#include <drizzled/session.h>
63
 
#include <drizzled/session/cache.h>
64
 
#include <drizzled/signal_handler.h>
65
 
#include <drizzled/transaction_services.h>
66
 
#include <drizzled/tztime.h>
67
 
#include <drizzled/unireg.h>
68
 
#include <drizzled/util/backtrace.h>
69
 
#include <drizzled/current_session.h>
70
 
#include <drizzled/daemon.h>
 
47
#include "drizzled/plugin.h"
 
48
#include "drizzled/gettext.h"
 
49
#include "drizzled/configmake.h"
 
50
#include "drizzled/session.h"
 
51
#include "drizzled/internal/my_sys.h"
 
52
#include "drizzled/unireg.h"
 
53
#include "drizzled/drizzled.h"
 
54
#include "drizzled/errmsg_print.h"
 
55
#include "drizzled/data_home.h"
 
56
#include "drizzled/plugin/listen.h"
 
57
#include "drizzled/plugin/client.h"
 
58
#include "drizzled/pthread_globals.h"
 
59
#include "drizzled/tztime.h"
 
60
#include "drizzled/signal_handler.h"
 
61
#include "drizzled/replication_services.h"
 
62
#include "drizzled/transaction_services.h"
 
63
 
 
64
#include "drizzled/util/backtrace.h"
71
65
 
72
66
using namespace drizzled;
73
67
using namespace std;
 
68
namespace fs=boost::filesystem;
74
69
 
75
70
static pthread_t select_thread;
76
71
static uint32_t thr_kill_signal;
77
72
 
78
 
extern bool opt_daemon;
79
 
 
80
73
 
81
74
/**
82
75
  All global error messages are sent here where the first one is stored
83
76
  for the client.
84
77
*/
85
 
static void my_message_sql(drizzled::error_t error, const char *str, myf MyFlags)
 
78
static void my_message_sql(uint32_t error, const char *str, myf MyFlags)
86
79
{
87
80
  Session *session;
88
81
  /*
95
88
      session->is_fatal_error= 1;
96
89
 
97
90
    /*
98
 
      @TODO There are two exceptions mechanism (Session and sp_rcontext),
 
91
      TODO: There are two exceptions mechanism (Session and sp_rcontext),
99
92
      this could be improved by having a common stack of handlers.
100
93
    */
101
 
    if (session->handle_error(error, str, DRIZZLE_ERROR::WARN_LEVEL_ERROR))
102
 
      return;
 
94
    if (session->handle_error(error, str,
 
95
                          DRIZZLE_ERROR::WARN_LEVEL_ERROR))
 
96
      return;;
103
97
 
104
98
    /*
105
 
      session->getLex()->current_select == 0 if lex structure is not inited
 
99
      session->lex->current_select == 0 if lex structure is not inited
106
100
      (not query command (COM_QUERY))
107
101
    */
108
 
    if (! (session->getLex()->current_select &&
109
 
           session->getLex()->current_select->no_error && !session->is_fatal_error))
 
102
    if (! (session->lex->current_select &&
 
103
        session->lex->current_select->no_error && !session->is_fatal_error))
110
104
    {
111
105
      if (! session->main_da.is_error())            // Return only first message
112
106
      {
113
 
        if (error == EE_OK)
 
107
        if (error == 0)
114
108
          error= ER_UNKNOWN_ERROR;
115
 
 
116
109
        if (str == NULL)
117
110
          str= ER(error);
118
 
 
119
111
        session->main_da.set_error_status(error, str);
120
112
      }
121
113
    }
129
121
      session->no_warnings_for_error= true;
130
122
      push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR, error, str);
131
123
      session->no_warnings_for_error= false;
 
124
      }
132
125
    }
133
 
  }
134
 
 
135
 
  if (not session || MyFlags & ME_NOREFRESH)
136
 
  {
137
 
    errmsg_printf(error::ERROR, "%s: %s",internal::my_progname,str);
138
 
  }
 
126
    if (!session || MyFlags & ME_NOREFRESH)
 
127
        errmsg_printf(ERRMSG_LVL_ERROR, "%s: %s",internal::my_progname,str);
139
128
}
140
129
 
141
130
static void init_signals(void)
143
132
  sigset_t set;
144
133
  struct sigaction sa;
145
134
 
146
 
  if (not (getDebug().test(debug::NO_STACKTRACE) || 
147
 
        getDebug().test(debug::CORE_ON_SIGNAL)))
 
135
  if (!(test_flags.test(TEST_NO_STACKTRACE) || 
 
136
        test_flags.test(TEST_CORE_ON_SIGNAL)))
148
137
  {
149
138
    sa.sa_flags = SA_RESETHAND | SA_NODEFER;
150
139
    sigemptyset(&sa.sa_mask);
160
149
    sigaction(SIGFPE, &sa, NULL);
161
150
  }
162
151
 
163
 
  if (getDebug().test(debug::CORE_ON_SIGNAL))
 
152
  if (test_flags.test(TEST_CORE_ON_SIGNAL))
164
153
  {
165
154
    /* Change limits so that we will get a core file */
166
155
    struct rlimit rl;
167
156
    rl.rlim_cur = rl.rlim_max = RLIM_INFINITY;
168
157
    if (setrlimit(RLIMIT_CORE, &rl) && global_system_variables.log_warnings)
169
 
        errmsg_printf(error::WARN,
 
158
        errmsg_printf(ERRMSG_LVL_WARN,
170
159
                      _("setrlimit could not change the size of core files "
171
160
                        "to 'infinity';  We may not be able to generate a "
172
161
                        "core file on signals"));
191
180
#ifdef SIGTSTP
192
181
  sigaddset(&set,SIGTSTP);
193
182
#endif
194
 
  if (getDebug().test(debug::ALLOW_SIGINT))
 
183
  if (test_flags.test(TEST_SIGINT))
195
184
  {
196
185
    sa.sa_flags= 0;
197
186
    sa.sa_handler= drizzled_end_thread_signal;
209
198
  return;
210
199
}
211
200
 
212
 
static void GoogleProtoErrorThrower(google::protobuf::LogLevel level,
213
 
                                    const char* ,
214
 
                                    int, const string& ) throw(const char *)
 
201
static void GoogleProtoErrorThrower(google::protobuf::LogLevel level, const char* filename,
 
202
                       int line, const string& message) throw(const char *)
215
203
{
 
204
  (void)filename;
 
205
  (void)line;
 
206
  (void)message;
 
207
  std::cerr << "\n";
 
208
  drizzled::util::custom_backtrace();
 
209
  std::cerr << "\n";
216
210
  switch(level)
217
211
  {
218
212
  case google::protobuf::LOGLEVEL_INFO:
221
215
  case google::protobuf::LOGLEVEL_ERROR:
222
216
  case google::protobuf::LOGLEVEL_FATAL:
223
217
  default:
 
218
    std::cerr << "GoogleProtoErrorThrower(" << filename << ", " << line << ", " << message << ")";
224
219
    throw("error in google protocol buffer parsing");
225
220
  }
226
221
}
231
226
# if defined(HAVE_LOCALE_H)
232
227
  setlocale(LC_ALL, "");
233
228
# endif
234
 
  bindtextdomain("drizzle7", LOCALEDIR);
235
 
  textdomain("drizzle7");
 
229
  bindtextdomain("drizzle", LOCALEDIR);
 
230
  textdomain("drizzle");
236
231
#endif
237
232
 
238
233
  module::Registry &modules= module::Registry::singleton();
 
234
  plugin::Client *client;
 
235
  Session *session;
239
236
 
240
237
  MY_INIT(argv[0]);             // init my_sys library & pthreads
241
238
  /* nothing should come before this line ^^^ */
247
244
 
248
245
  /* Function generates error messages before abort */
249
246
  error_handler_hook= my_message_sql;
250
 
 
251
247
  /* init_common_variables must get basic settings such as data_home_dir
252
248
     and plugin_load_list. */
253
 
  if (init_basic_variables(argc, argv))
254
 
    unireg_abort(1);                            // Will do exit
255
 
 
256
 
  if (opt_daemon)
257
 
  {
258
 
    if (signal(SIGHUP, SIG_IGN) == SIG_ERR)
259
 
    {
260
 
      perror("Failed to ignore SIGHUP");
261
 
    }
262
 
    if (daemonize())
263
 
    {
264
 
      fprintf(stderr, "failed to daemon() in order to daemonize\n");
265
 
      exit(EXIT_FAILURE);
266
 
    }
267
 
  }
268
 
 
269
 
  if (init_remaining_variables(modules))
 
249
  if (init_common_variables(argc, argv, modules))
270
250
    unireg_abort(1);                            // Will do exit
271
251
 
272
252
  /*
283
263
  {
284
264
    if (chdir(getDataHome().file_string().c_str()))
285
265
    {
286
 
      errmsg_printf(error::ERROR,
 
266
      errmsg_printf(ERRMSG_LVL_ERROR,
287
267
                    _("Data directory %s does not exist\n"),
288
268
                    getDataHome().file_string().c_str());
289
269
      unireg_abort(1);
294
274
    }
295
275
    if (chdir("local"))
296
276
    {
297
 
      errmsg_printf(error::ERROR,
 
277
      errmsg_printf(ERRMSG_LVL_ERROR,
298
278
                    _("Local catalog %s/local does not exist\n"),
299
279
                    getDataHome().file_string().c_str());
300
280
      unireg_abort(1);
301
281
    }
302
282
 
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());
 
283
    full_data_home= fs::system_complete(getDataHome());
 
284
    getDataHomeCatalog()= "./";
 
285
    getDataHome()= "../";
306
286
  }
307
287
 
308
288
 
312
292
    server_id= 1;
313
293
  }
314
294
 
315
 
  try
316
 
  {
317
 
    if (init_server_components(modules))
318
 
      DRIZZLE_ABORT;
319
 
  }
320
 
  catch (abort_exception& ex)
321
 
  {
322
 
#if defined(DEBUG)
323
 
    cout << _("Drizzle has receieved an abort event.") << endl;
324
 
    cout << _("In Function: ") << *::boost::get_error_info<boost::throw_function>(ex) << endl;
325
 
    cout << _("In File: ") << *::boost::get_error_info<boost::throw_file>(ex) << endl;
326
 
    cout << _("On Line: ") << *::boost::get_error_info<boost::throw_line>(ex) << endl;
327
 
#endif
 
295
  if (init_server_components(modules))
328
296
    unireg_abort(1);
329
 
  }
330
 
 
331
297
 
332
298
  /**
333
299
   * This check must be done after init_server_components for now
346
312
  if (plugin::Listen::setup())
347
313
    unireg_abort(1);
348
314
 
 
315
 
349
316
  assert(plugin::num_trx_monitored_objects > 0);
350
 
  if (drizzle_rm_tmp_tables())
 
317
  if (drizzle_rm_tmp_tables() ||
 
318
      my_tz_init((Session *)0, default_tz_name))
351
319
  {
352
320
    abort_loop= true;
353
321
    select_thread_in_use=0;
355
323
 
356
324
    (void) unlink(pid_file.file_string().c_str());      // Not needed anymore
357
325
 
358
 
    unireg_abort(1);
 
326
    exit(1);
359
327
  }
360
328
 
361
 
  errmsg_printf(error::INFO, _(ER(ER_STARTUP)), internal::my_progname,
 
329
  errmsg_printf(ERRMSG_LVL_INFO, _(ER(ER_STARTUP)), internal::my_progname,
362
330
                PANDORA_RELEASE_VERSION, COMPILATION_COMMENT);
363
331
 
364
332
 
365
333
  TransactionServices &transaction_services= TransactionServices::singleton();
366
334
 
367
335
  /* Send server startup event */
 
336
  if ((session= new Session(plugin::Listen::getNullClient())))
368
337
  {
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
 
    }
 
338
    currentSession().release();
 
339
    currentSession().reset(session);
 
340
    transaction_services.sendStartupEvent(session);
 
341
    session->lockForDelete();
 
342
    delete session;
381
343
  }
382
344
 
383
 
  if (opt_daemon)
384
 
    daemon_is_ready();
385
345
 
386
 
  /* 
387
 
    Listen for new connections and start new session for each connection
 
346
  /* Listen for new connections and start new session for each connection
388
347
     accepted. The listen.getClient() method will return NULL when the server
389
 
     should be shutdown.
390
 
   */
391
 
  plugin::Client *client;
 
348
     should be shutdown. */
392
349
  while ((client= plugin::Listen::getClient()) != NULL)
393
350
  {
394
 
    Session::shared_ptr session;
395
 
    session= Session::make_shared(client, client->catalog());
396
 
 
397
 
    if (not session)
 
351
    if (!(session= new Session(client)))
398
352
    {
399
353
      delete client;
400
354
      continue;
401
355
    }
402
356
 
403
357
    /* If we error on creation we drop the connection and delete the session. */
404
 
    if (Session::schedule(session))
 
358
    if (session->schedule())
405
359
      Session::unlink(session);
406
360
  }
407
361
 
408
362
  /* Send server shutdown event */
409
 
  {
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
 
    }
418
 
  }
419
 
 
420
 
  {
421
 
    boost::mutex::scoped_lock scopedLock(session::Cache::singleton().mutex());
422
 
    select_thread_in_use= false;                        // For close_connections
423
 
  }
 
363
  if ((session= new Session(plugin::Listen::getNullClient())))
 
364
  {
 
365
    currentSession().release();
 
366
    currentSession().reset(session);
 
367
    transaction_services.sendShutdownEvent(session);
 
368
    session->lockForDelete();
 
369
    delete session;
 
370
  }
 
371
 
 
372
  LOCK_thread_count.lock();
 
373
  select_thread_in_use=0;                       // For close_connections
 
374
  LOCK_thread_count.unlock();
424
375
  COND_thread_count.notify_all();
425
376
 
426
377
  /* Wait until cleanup is done */
427
 
  session::Cache::singleton().shutdownSecond();
 
378
  {
 
379
    boost::mutex::scoped_lock scopedLock(LOCK_thread_count);
 
380
    while (!ready_to_exit)
 
381
      COND_server_end.wait(scopedLock);
 
382
  }
428
383
 
429
384
  clean_up(1);
430
385
  module::Registry::shutdown();