~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/transaction_log/module.cc

  • Committer: LinuxJedi
  • Date: 2010-08-28 09:23:52 UTC
  • mto: (1738.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 1739.
  • Revision ID: linuxjedi@linuxjedi-laptop-20100828092352-oe3zbtdy05kq9dtq
Make exit happen in main thread rather than signal handler thread thus avoiding a segfault due to a double kill of the signal handler thread

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
 
45
45
#include <drizzled/plugin/plugin.h>
46
46
#include <drizzled/session.h>
 
47
#include <drizzled/set_var.h>
47
48
#include <drizzled/gettext.h>
48
49
#include <boost/program_options.hpp>
49
50
#include <drizzled/module/option_map.h>
52
53
using namespace std;
53
54
using namespace drizzled;
54
55
 
55
 
/**
56
 
 * The name of the main transaction log file on disk.  With no prefix,
57
 
 * this goes into Drizzle's $datadir.
58
 
 */
59
 
static const char DEFAULT_LOG_FILE_PATH[]= "transaction.log"; /* In datadir... */
60
56
/** 
61
57
 * Transaction Log plugin system variable - Is the log enabled? Only used on init().  
 
58
 * The enable() and disable() methods of the TransactionLog class control online
 
59
 * disabling.
62
60
 */
63
61
static bool sysvar_transaction_log_enabled= false;
64
 
 
65
62
/** Transaction Log plugin system variable - The path to the log file used */
66
 
static string sysvar_transaction_log_file;
67
 
 
 
63
static char* sysvar_transaction_log_file= NULL;
68
64
/** 
69
65
 * Transaction Log plugin system variable - A debugging variable to assist 
70
66
 * in truncating the log file. 
71
67
 */
72
68
static bool sysvar_transaction_log_truncate_debug= false;
 
69
/**
 
70
 * The name of the main transaction log file on disk.  With no prefix,
 
71
 * this goes into Drizzle's $datadir.
 
72
 */
 
73
static const char DEFAULT_LOG_FILE_PATH[]= "transaction.log"; /* In datadir... */
73
74
/** 
74
75
 * Transaction Log plugin system variable - Should we write a CRC32 checksum for 
75
76
 * each written Transaction message?
83
84
 * TransactionLog::FLUSH_FREQUENCY_EVERY_WRITE == 1   ... sync on every write
84
85
 * TransactionLog::FLUSH_FREQUENCY_EVERY_SECOND == 2  ... sync at most once a second
85
86
 */
86
 
typedef constrained_check<uint32_t, 2, 0> flush_constraint;
87
 
static flush_constraint sysvar_transaction_log_flush_frequency;
 
87
static uint32_t sysvar_transaction_log_flush_frequency= 0;
88
88
/**
89
89
 * Transaction Log plugin system variable - Number of slots to create
90
90
 * for managing write buffers
91
91
 */
92
 
typedef constrained_check<uint32_t, 8192, 4> write_buffers_constraint;
93
 
static write_buffers_constraint sysvar_transaction_log_num_write_buffers;
 
92
static uint32_t sysvar_transaction_log_num_write_buffers= 8;
94
93
/**
95
94
 * Transaction Log plugin system variable - The name of the replicator plugin
96
95
 * to pair the transaction log's applier with.  Defaults to "default"
97
96
 */
 
97
static char *sysvar_transaction_log_use_replicator= NULL;
98
98
static const char DEFAULT_USE_REPLICATOR[]= "default";
99
 
static string sysvar_transaction_log_use_replicator;
100
99
 
101
100
/** DATA_DICTIONARY views */
102
101
static TransactionLogTool *transaction_log_tool;
121
120
  {
122
121
    (void) close(log_file);
123
122
  }
124
 
}
125
123
 
126
 
static void set_truncate_debug(Session *, sql_var_t)
127
 
{
128
 
  if (transaction_log)
129
 
  {
130
 
    if (sysvar_transaction_log_truncate_debug)
131
 
    {
132
 
      transaction_log->truncate();
133
 
      transaction_log_index->clear();
134
 
      sysvar_transaction_log_truncate_debug= false;
135
 
    }
136
 
  }
 
124
  /* These get strdup'd below */
 
125
  free(sysvar_transaction_log_file);
 
126
  free(sysvar_transaction_log_use_replicator);
137
127
}
138
128
 
139
129
static int init(drizzled::module::Context &context)
140
130
{
141
 
  context.registerVariable(new sys_var_bool_ptr_readonly("enable",
142
 
                                                         &sysvar_transaction_log_enabled));
143
 
  context.registerVariable(new sys_var_bool_ptr("truncate-debug",
144
 
                                                &sysvar_transaction_log_truncate_debug,
145
 
                                                set_truncate_debug));
146
 
 
147
 
  context.registerVariable(new sys_var_const_string("file",
148
 
                                                    sysvar_transaction_log_file));
149
 
  context.registerVariable(new sys_var_const_string("use-replicator",
150
 
                                                    sysvar_transaction_log_use_replicator));
151
 
  context.registerVariable(new sys_var_bool_ptr_readonly("enable-checksum",
152
 
                                                         &sysvar_transaction_log_checksum_enabled));
153
 
  context.registerVariable(new sys_var_constrained_value_readonly<uint32_t>("flush-frequency", sysvar_transaction_log_flush_frequency));
154
 
 
155
 
  context.registerVariable(new sys_var_constrained_value_readonly<uint32_t>("num-write-buffers",
156
 
                                                                            sysvar_transaction_log_num_write_buffers));
157
 
 
 
131
  const module::option_map &vm= context.getOptions();
 
132
 
 
133
  if (vm.count("flush-frequency"))
 
134
  {
 
135
    if (sysvar_transaction_log_flush_frequency > 2)
 
136
    {
 
137
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for sync-method\n"));
 
138
      exit(-1);
 
139
    }
 
140
  }
 
141
 
 
142
  if (vm.count("num-write-buffers"))
 
143
  {
 
144
    if (sysvar_transaction_log_num_write_buffers < 4 || sysvar_transaction_log_num_write_buffers > 8192)
 
145
    {
 
146
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for num-write-buffers\n"));
 
147
      exit(-1);
 
148
    }
 
149
  }
 
150
 
 
151
  if (vm.count("file"))
 
152
  {
 
153
    sysvar_transaction_log_file= strdup(vm["file"].as<string>().c_str());
 
154
  }
 
155
 
 
156
  else
 
157
  {
 
158
    sysvar_transaction_log_file= strdup(DEFAULT_LOG_FILE_PATH);
 
159
  }
 
160
 
 
161
  if (vm.count("use-replicator"))
 
162
  {
 
163
    sysvar_transaction_log_use_replicator= strdup(vm["use-replicator"].as<string>().c_str());
 
164
  }
 
165
 
 
166
  else
 
167
  {
 
168
    sysvar_transaction_log_use_replicator= strdup(DEFAULT_USE_REPLICATOR);
 
169
  }
158
170
 
159
171
  /* Create and initialize the transaction log itself */
160
172
  if (sysvar_transaction_log_enabled)
161
173
  {
162
 
  
163
 
    transaction_log= new (nothrow) TransactionLog(sysvar_transaction_log_file,
164
 
                                                  static_cast<int>(sysvar_transaction_log_flush_frequency),
 
174
    transaction_log= new (nothrow) TransactionLog(string(sysvar_transaction_log_file),
 
175
                                                  sysvar_transaction_log_flush_frequency,
165
176
                                                  sysvar_transaction_log_checksum_enabled);
166
177
 
167
178
    if (transaction_log == NULL)
208
219
    transaction_log_applier= new (nothrow) TransactionLogApplier("transaction_log_applier",
209
220
                                                                 transaction_log, 
210
221
                                                                 transaction_log_index, 
211
 
                                                                 static_cast<uint32_t>(sysvar_transaction_log_num_write_buffers));
 
222
                                                                 sysvar_transaction_log_num_write_buffers);
212
223
    if (transaction_log_applier == NULL)
213
224
    {
214
225
      char errmsg[STRERROR_MAX];
219
230
    }
220
231
    context.add(transaction_log_applier);
221
232
    ReplicationServices &replication_services= ReplicationServices::singleton();
222
 
    replication_services.attachApplier(transaction_log_applier,
223
 
                                       sysvar_transaction_log_use_replicator);
 
233
    string replicator_name(sysvar_transaction_log_use_replicator);
 
234
    replication_services.attachApplier(transaction_log_applier, replicator_name);
224
235
 
225
236
    /* Setup DATA_DICTIONARY views */
226
237
 
251
262
}
252
263
 
253
264
 
 
265
static void set_truncate_debug(Session *,
 
266
                               drizzle_sys_var *, 
 
267
                               void *, 
 
268
                               const void *save)
 
269
{
 
270
  /* 
 
271
   * The const void * save comes directly from the check function, 
 
272
   * which should simply return the result from the set statement. 
 
273
   */
 
274
  if (transaction_log)
 
275
  {
 
276
    if (*(bool *)save != false)
 
277
    {
 
278
      transaction_log->truncate();
 
279
      transaction_log_index->clear();
 
280
    }
 
281
  }
 
282
}
 
283
 
 
284
static DRIZZLE_SYSVAR_BOOL(enable,
 
285
                           sysvar_transaction_log_enabled,
 
286
                           PLUGIN_VAR_NOCMDARG,
 
287
                           N_("Enable transaction log"),
 
288
                           NULL, /* check func */
 
289
                           NULL, /* update func */
 
290
                           false /* default */);
 
291
 
 
292
static DRIZZLE_SYSVAR_BOOL(truncate_debug,
 
293
                           sysvar_transaction_log_truncate_debug,
 
294
                           PLUGIN_VAR_NOCMDARG,
 
295
                           N_("DEBUGGING - Truncate transaction log"),
 
296
                           NULL, /* check func */
 
297
                           set_truncate_debug, /* update func */
 
298
                           false /* default */);
 
299
 
 
300
static DRIZZLE_SYSVAR_STR(file,
 
301
                          sysvar_transaction_log_file,
 
302
                          PLUGIN_VAR_READONLY,
 
303
                          N_("Path to the file to use for transaction log"),
 
304
                          NULL, /* check func */
 
305
                          NULL, /* update func*/
 
306
                          DEFAULT_LOG_FILE_PATH /* default */);
 
307
 
 
308
static DRIZZLE_SYSVAR_STR(use_replicator,
 
309
                          sysvar_transaction_log_use_replicator,
 
310
                          PLUGIN_VAR_READONLY,
 
311
                          N_("Name of the replicator plugin to use (default='default_replicator')"),
 
312
                          NULL, /* check func */
 
313
                          NULL, /* update func*/
 
314
                          DEFAULT_USE_REPLICATOR /* default */);
 
315
 
 
316
static DRIZZLE_SYSVAR_BOOL(enable_checksum,
 
317
                           sysvar_transaction_log_checksum_enabled,
 
318
                           PLUGIN_VAR_NOCMDARG,
 
319
                           N_("Enable CRC32 Checksumming of each written transaction log entry"),
 
320
                           NULL, /* check func */
 
321
                           NULL, /* update func */
 
322
                           false /* default */);
 
323
 
 
324
static DRIZZLE_SYSVAR_UINT(flush_frequency,
 
325
                           sysvar_transaction_log_flush_frequency,
 
326
                           PLUGIN_VAR_OPCMDARG,
 
327
                           N_("0 == rely on operating system to sync log file (default), "
 
328
                              "1 == sync file at each transaction write, "
 
329
                              "2 == sync log file once per second"),
 
330
                           NULL, /* check func */
 
331
                           NULL, /* update func */
 
332
                           0, /* default */
 
333
                           0,
 
334
                           2,
 
335
                           0);
 
336
 
 
337
static DRIZZLE_SYSVAR_UINT(num_write_buffers,
 
338
                           sysvar_transaction_log_num_write_buffers,
 
339
                           PLUGIN_VAR_OPCMDARG,
 
340
                           N_("Number of slots for in-memory write buffers (default=8)."),
 
341
                           NULL, /* check func */
 
342
                           NULL, /* update func */
 
343
                           8, /* default */
 
344
                           4,
 
345
                           8192,
 
346
                           0);
 
347
 
254
348
static void init_options(drizzled::module::option_context &context)
255
349
{
256
350
  context("truncate-debug",
263
357
          po::value<bool>(&sysvar_transaction_log_enabled)->default_value(false)->zero_tokens(),
264
358
          N_("Enable transaction log"));
265
359
  context("file",
266
 
          po::value<string>(&sysvar_transaction_log_file)->default_value(DEFAULT_LOG_FILE_PATH),
 
360
          po::value<string>(),
267
361
          N_("Path to the file to use for transaction log"));
268
362
  context("use-replicator",
269
 
          po::value<string>(&sysvar_transaction_log_use_replicator)->default_value(DEFAULT_USE_REPLICATOR),
 
363
          po::value<string>(),
270
364
          N_("Name of the replicator plugin to use (default='default_replicator')")); 
271
365
  context("flush-frequency",
272
 
          po::value<flush_constraint>(&sysvar_transaction_log_flush_frequency)->default_value(0),
 
366
          po::value<uint32_t>(&sysvar_transaction_log_flush_frequency)->default_value(0),
273
367
          N_("0 == rely on operating system to sync log file (default), 1 == sync file at each transaction write, 2 == sync log file once per second"));
274
368
  context("num-write-buffers",
275
 
          po::value<write_buffers_constraint>(&sysvar_transaction_log_num_write_buffers)->default_value(8),
 
369
          po::value<uint32_t>(&sysvar_transaction_log_num_write_buffers)->default_value(8),
276
370
          N_("Number of slots for in-memory write buffers (default=8)."));
277
371
}
278
372
 
279
 
DRIZZLE_PLUGIN(init, NULL, init_options);
 
373
static drizzle_sys_var* sys_variables[]= {
 
374
  DRIZZLE_SYSVAR(enable),
 
375
  DRIZZLE_SYSVAR(truncate_debug),
 
376
  DRIZZLE_SYSVAR(file),
 
377
  DRIZZLE_SYSVAR(enable_checksum),
 
378
  DRIZZLE_SYSVAR(flush_frequency),
 
379
  DRIZZLE_SYSVAR(num_write_buffers),
 
380
  DRIZZLE_SYSVAR(use_replicator),
 
381
  NULL
 
382
};
 
383
 
 
384
DRIZZLE_PLUGIN(init, sys_variables, init_options);