~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/transaction_log/module.cc

  • Committer: patrick crews
  • Date: 2010-10-07 19:35:15 UTC
  • mto: (1819.2.4 drizzle)
  • mto: This revision was merged to the branch mainline in revision 1825.
  • Revision ID: gleebix@gmail.com-20101007193515-jr6y1uz710lzte1o
Initial work on lp bug#656423 - remove use of 'mysql' from test-run tool.  Removed / substituted mtr->dtr mysql->drizzle.  Removed perl errors, but server won't start due to boost error.

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-2009 Sun Microsystems, Inc.
5
 
 *  Copyright (C) 2010 Jay Pipes <jaypipes@gmail.com>
 
4
 *  Copyright (C) 2008-2009 Sun Microsystems
 
5
 *  Copyright (c) 2010 Jay Pipes <jaypipes@gmail.com>
6
6
 *
7
7
 *  Authors:
8
8
 *
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().  
62
58
 */
63
59
static bool sysvar_transaction_log_enabled= false;
64
 
 
65
60
/** Transaction Log plugin system variable - The path to the log file used */
66
 
static string sysvar_transaction_log_file;
67
 
 
 
61
static char* sysvar_transaction_log_file= NULL;
68
62
/** 
69
63
 * Transaction Log plugin system variable - A debugging variable to assist 
70
64
 * in truncating the log file. 
71
65
 */
72
66
static bool sysvar_transaction_log_truncate_debug= false;
 
67
/**
 
68
 * The name of the main transaction log file on disk.  With no prefix,
 
69
 * this goes into Drizzle's $datadir.
 
70
 */
 
71
static const char DEFAULT_LOG_FILE_PATH[]= "transaction.log"; /* In datadir... */
73
72
/** 
74
73
 * Transaction Log plugin system variable - Should we write a CRC32 checksum for 
75
74
 * each written Transaction message?
83
82
 * TransactionLog::FLUSH_FREQUENCY_EVERY_WRITE == 1   ... sync on every write
84
83
 * TransactionLog::FLUSH_FREQUENCY_EVERY_SECOND == 2  ... sync at most once a second
85
84
 */
86
 
typedef constrained_check<uint32_t, 2, 0> flush_constraint;
87
 
static flush_constraint sysvar_transaction_log_flush_frequency;
 
85
static uint32_t sysvar_transaction_log_flush_frequency= 0;
88
86
/**
89
87
 * Transaction Log plugin system variable - Number of slots to create
90
88
 * for managing write buffers
91
89
 */
92
 
typedef constrained_check<uint32_t, 8192, 4> write_buffers_constraint;
93
 
static write_buffers_constraint sysvar_transaction_log_num_write_buffers;
 
90
static uint32_t sysvar_transaction_log_num_write_buffers= 8;
94
91
/**
95
92
 * Transaction Log plugin system variable - The name of the replicator plugin
96
93
 * to pair the transaction log's applier with.  Defaults to "default"
97
94
 */
 
95
static char *sysvar_transaction_log_use_replicator= NULL;
98
96
static const char DEFAULT_USE_REPLICATOR[]= "default";
99
 
static string sysvar_transaction_log_use_replicator;
100
97
 
101
98
/** DATA_DICTIONARY views */
102
99
static TransactionLogTool *transaction_log_tool;
121
118
  {
122
119
    (void) close(log_file);
123
120
  }
124
 
}
125
121
 
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
 
  }
 
122
  /* These get strdup'd below */
 
123
  free(sysvar_transaction_log_file);
 
124
  free(sysvar_transaction_log_use_replicator);
137
125
}
138
126
 
139
127
static int init(drizzled::module::Context &context)
140
128
{
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
 
 
 
129
  const module::option_map &vm= context.getOptions();
 
130
 
 
131
  if (vm.count("flush-frequency"))
 
132
  {
 
133
    if (sysvar_transaction_log_flush_frequency > 2)
 
134
    {
 
135
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for sync-method\n"));
 
136
      exit(-1);
 
137
    }
 
138
  }
 
139
 
 
140
  if (vm.count("num-write-buffers"))
 
141
  {
 
142
    if (sysvar_transaction_log_num_write_buffers < 4 || sysvar_transaction_log_num_write_buffers > 8192)
 
143
    {
 
144
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for num-write-buffers\n"));
 
145
      exit(-1);
 
146
    }
 
147
  }
 
148
 
 
149
  if (vm.count("file"))
 
150
  {
 
151
    sysvar_transaction_log_file= strdup(vm["file"].as<string>().c_str());
 
152
  }
 
153
 
 
154
  else
 
155
  {
 
156
    sysvar_transaction_log_file= strdup(DEFAULT_LOG_FILE_PATH);
 
157
  }
 
158
 
 
159
  if (vm.count("use-replicator"))
 
160
  {
 
161
    sysvar_transaction_log_use_replicator= strdup(vm["use-replicator"].as<string>().c_str());
 
162
  }
 
163
 
 
164
  else
 
165
  {
 
166
    sysvar_transaction_log_use_replicator= strdup(DEFAULT_USE_REPLICATOR);
 
167
  }
158
168
 
159
169
  /* Create and initialize the transaction log itself */
160
170
  if (sysvar_transaction_log_enabled)
161
171
  {
162
 
  
163
 
    transaction_log= new (nothrow) TransactionLog(sysvar_transaction_log_file,
164
 
                                                  static_cast<int>(sysvar_transaction_log_flush_frequency),
 
172
    transaction_log= new (nothrow) TransactionLog(string(sysvar_transaction_log_file),
 
173
                                                  sysvar_transaction_log_flush_frequency,
165
174
                                                  sysvar_transaction_log_checksum_enabled);
166
175
 
167
176
    if (transaction_log == NULL)
168
177
    {
169
 
      sql_perror(_("Failed to allocate the TransactionLog instance"), sysvar_transaction_log_file);
 
178
      char errmsg[STRERROR_MAX];
 
179
      strerror_r(errno, errmsg, sizeof(errmsg));
 
180
      errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to allocate the TransactionLog instance.  Got error: %s\n"), 
 
181
                    errmsg);
170
182
      return 1;
171
183
    }
172
184
    else
174
186
      /* Check to see if the log was not created properly */
175
187
      if (transaction_log->hasError())
176
188
      {
177
 
        errmsg_printf(error::ERROR, _("Failed to initialize the Transaction Log.  Got error: %s\n"), 
 
189
        errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to initialize the Transaction Log.  Got error: %s\n"), 
178
190
                      transaction_log->getErrorMessage().c_str());
179
191
        return 1;
180
192
      }
184
196
    transaction_log_index= new (nothrow) TransactionLogIndex(*transaction_log);
185
197
    if (transaction_log_index == NULL)
186
198
    {
187
 
      sql_perror(_("Failed to allocate the TransactionLogIndex instance"), sysvar_transaction_log_file);
 
199
      char errmsg[STRERROR_MAX];
 
200
      strerror_r(errno, errmsg, sizeof(errmsg));
 
201
      errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to allocate the TransactionLogIndex instance.  Got error: %s\n"), 
 
202
                    errmsg);
188
203
      return 1;
189
204
    }
190
205
    else
192
207
      /* Check to see if the index was not created properly */
193
208
      if (transaction_log_index->hasError())
194
209
      {
195
 
        errmsg_printf(error::ERROR, _("Failed to initialize the Transaction Log Index.  Got error: %s\n"), 
 
210
        errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to initialize the Transaction Log Index.  Got error: %s\n"), 
196
211
                      transaction_log_index->getErrorMessage().c_str());
197
212
        return 1;
198
213
      }
202
217
    transaction_log_applier= new (nothrow) TransactionLogApplier("transaction_log_applier",
203
218
                                                                 transaction_log, 
204
219
                                                                 transaction_log_index, 
205
 
                                                                 static_cast<uint32_t>(sysvar_transaction_log_num_write_buffers));
 
220
                                                                 sysvar_transaction_log_num_write_buffers);
206
221
    if (transaction_log_applier == NULL)
207
222
    {
208
 
      sql_perror(_("Failed to allocate the TransactionLogApplier instance"), sysvar_transaction_log_file);
 
223
      char errmsg[STRERROR_MAX];
 
224
      strerror_r(errno, errmsg, sizeof(errmsg));
 
225
      errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to allocate the TransactionLogApplier instance.  Got error: %s\n"), 
 
226
                    errmsg);
209
227
      return 1;
210
228
    }
211
229
    context.add(transaction_log_applier);
212
230
    ReplicationServices &replication_services= ReplicationServices::singleton();
213
 
    replication_services.attachApplier(transaction_log_applier,
214
 
                                       sysvar_transaction_log_use_replicator);
 
231
    string replicator_name(sysvar_transaction_log_use_replicator);
 
232
    replication_services.attachApplier(transaction_log_applier, replicator_name);
215
233
 
216
234
    /* Setup DATA_DICTIONARY views */
217
235
 
242
260
}
243
261
 
244
262
 
 
263
static void set_truncate_debug(Session *,
 
264
                               drizzle_sys_var *, 
 
265
                               void *, 
 
266
                               const void *save)
 
267
{
 
268
  /* 
 
269
   * The const void * save comes directly from the check function, 
 
270
   * which should simply return the result from the set statement. 
 
271
   */
 
272
  if (transaction_log)
 
273
  {
 
274
    if (*(bool *)save != false)
 
275
    {
 
276
      transaction_log->truncate();
 
277
      transaction_log_index->clear();
 
278
    }
 
279
  }
 
280
}
 
281
 
 
282
static DRIZZLE_SYSVAR_BOOL(enable,
 
283
                           sysvar_transaction_log_enabled,
 
284
                           PLUGIN_VAR_NOCMDARG | PLUGIN_VAR_READONLY,
 
285
                           N_("Enable transaction log"),
 
286
                           NULL, /* check func */
 
287
                           NULL, /* update func */
 
288
                           false /* default */);
 
289
 
 
290
static DRIZZLE_SYSVAR_BOOL(truncate_debug,
 
291
                           sysvar_transaction_log_truncate_debug,
 
292
                           PLUGIN_VAR_NOCMDARG,
 
293
                           N_("DEBUGGING - Truncate transaction log"),
 
294
                           NULL, /* check func */
 
295
                           set_truncate_debug, /* update func */
 
296
                           false /* default */);
 
297
 
 
298
static DRIZZLE_SYSVAR_STR(file,
 
299
                          sysvar_transaction_log_file,
 
300
                          PLUGIN_VAR_READONLY,
 
301
                          N_("Path to the file to use for transaction log"),
 
302
                          NULL, /* check func */
 
303
                          NULL, /* update func*/
 
304
                          DEFAULT_LOG_FILE_PATH /* default */);
 
305
 
 
306
static DRIZZLE_SYSVAR_STR(use_replicator,
 
307
                          sysvar_transaction_log_use_replicator,
 
308
                          PLUGIN_VAR_READONLY,
 
309
                          N_("Name of the replicator plugin to use (default='default_replicator')"),
 
310
                          NULL, /* check func */
 
311
                          NULL, /* update func*/
 
312
                          DEFAULT_USE_REPLICATOR /* default */);
 
313
 
 
314
static DRIZZLE_SYSVAR_BOOL(enable_checksum,
 
315
                           sysvar_transaction_log_checksum_enabled,
 
316
                           PLUGIN_VAR_NOCMDARG,
 
317
                           N_("Enable CRC32 Checksumming of each written transaction log entry"),
 
318
                           NULL, /* check func */
 
319
                           NULL, /* update func */
 
320
                           false /* default */);
 
321
 
 
322
static DRIZZLE_SYSVAR_UINT(flush_frequency,
 
323
                           sysvar_transaction_log_flush_frequency,
 
324
                           PLUGIN_VAR_OPCMDARG,
 
325
                           N_("0 == rely on operating system to sync log file (default), "
 
326
                              "1 == sync file at each transaction write, "
 
327
                              "2 == sync log file once per second"),
 
328
                           NULL, /* check func */
 
329
                           NULL, /* update func */
 
330
                           0, /* default */
 
331
                           0,
 
332
                           2,
 
333
                           0);
 
334
 
 
335
static DRIZZLE_SYSVAR_UINT(num_write_buffers,
 
336
                           sysvar_transaction_log_num_write_buffers,
 
337
                           PLUGIN_VAR_OPCMDARG,
 
338
                           N_("Number of slots for in-memory write buffers (default=8)."),
 
339
                           NULL, /* check func */
 
340
                           NULL, /* update func */
 
341
                           8, /* default */
 
342
                           4,
 
343
                           8192,
 
344
                           0);
 
345
 
245
346
static void init_options(drizzled::module::option_context &context)
246
347
{
247
348
  context("truncate-debug",
248
349
          po::value<bool>(&sysvar_transaction_log_truncate_debug)->default_value(false)->zero_tokens(),
249
 
          _("DEBUGGING - Truncate transaction log"));
 
350
          N_("DEBUGGING - Truncate transaction log"));
250
351
  context("enable-checksum",
251
352
          po::value<bool>(&sysvar_transaction_log_checksum_enabled)->default_value(false)->zero_tokens(),
252
 
          _("Enable CRC32 Checksumming of each written transaction log entry"));  
 
353
          N_("Enable CRC32 Checksumming of each written transaction log entry"));  
253
354
  context("enable",
254
355
          po::value<bool>(&sysvar_transaction_log_enabled)->default_value(false)->zero_tokens(),
255
 
          _("Enable transaction log"));
 
356
          N_("Enable transaction log"));
256
357
  context("file",
257
 
          po::value<string>(&sysvar_transaction_log_file)->default_value(DEFAULT_LOG_FILE_PATH),
258
 
          _("Path to the file to use for transaction log"));
 
358
          po::value<string>(),
 
359
          N_("Path to the file to use for transaction log"));
259
360
  context("use-replicator",
260
 
          po::value<string>(&sysvar_transaction_log_use_replicator)->default_value(DEFAULT_USE_REPLICATOR),
261
 
          _("Name of the replicator plugin to use (default='default_replicator')")); 
 
361
          po::value<string>(),
 
362
          N_("Name of the replicator plugin to use (default='default_replicator')")); 
262
363
  context("flush-frequency",
263
 
          po::value<flush_constraint>(&sysvar_transaction_log_flush_frequency)->default_value(0),
264
 
          _("0 == rely on operating system to sync log file (default), 1 == sync file at each transaction write, 2 == sync log file once per second"));
 
364
          po::value<uint32_t>(&sysvar_transaction_log_flush_frequency)->default_value(0),
 
365
          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"));
265
366
  context("num-write-buffers",
266
 
          po::value<write_buffers_constraint>(&sysvar_transaction_log_num_write_buffers)->default_value(8),
267
 
          _("Number of slots for in-memory write buffers (default=8)."));
 
367
          po::value<uint32_t>(&sysvar_transaction_log_num_write_buffers)->default_value(8),
 
368
          N_("Number of slots for in-memory write buffers (default=8)."));
268
369
}
269
370
 
270
 
DRIZZLE_PLUGIN(init, NULL, init_options);
 
371
static drizzle_sys_var* sys_variables[]= {
 
372
  DRIZZLE_SYSVAR(enable),
 
373
  DRIZZLE_SYSVAR(truncate_debug),
 
374
  DRIZZLE_SYSVAR(file),
 
375
  DRIZZLE_SYSVAR(enable_checksum),
 
376
  DRIZZLE_SYSVAR(flush_frequency),
 
377
  DRIZZLE_SYSVAR(num_write_buffers),
 
378
  DRIZZLE_SYSVAR(use_replicator),
 
379
  NULL
 
380
};
 
381
 
 
382
DRIZZLE_PLUGIN(init, sys_variables, init_options);