~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/transaction_log/module.cc

  • Committer: Monty Taylor
  • Date: 2010-08-20 20:54:13 UTC
  • mto: (1728.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 1729.
  • Revision ID: mordred@inaugust.com-20100820205413-2zebseruww4uq3dg
Removed a HASH in xa_resource_manager. It's not actually used, but I left it
in case someone did actually want to implement RECOVER some day.

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
#include "hexdump_transaction_message.h"
41
41
#include "background_worker.h"
42
42
 
43
 
#include <errno.h>
44
 
 
45
43
#include <drizzled/plugin/plugin.h>
46
44
#include <drizzled/session.h>
 
45
#include <drizzled/set_var.h>
47
46
#include <drizzled/gettext.h>
48
47
#include <boost/program_options.hpp>
49
48
#include <drizzled/module/option_map.h>
52
51
using namespace std;
53
52
using namespace drizzled;
54
53
 
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
54
/** 
61
55
 * Transaction Log plugin system variable - Is the log enabled? Only used on init().  
 
56
 * The enable() and disable() methods of the TransactionLog class control online
 
57
 * disabling.
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?
79
78
 * Numeric option controlling the sync/flush behaviour of the transaction
80
79
 * log.  Options are:
81
80
 *
82
 
 * TransactionLog::FLUSH_FREQUENCY_OS == 0            ... let OS do sync'ing
83
 
 * TransactionLog::FLUSH_FREQUENCY_EVERY_WRITE == 1   ... sync on every write
84
 
 * TransactionLog::FLUSH_FREQUENCY_EVERY_SECOND == 2  ... sync at most once a second
 
81
 * TransactionLog::SYNC_METHOD_OS == 0            ... let OS do sync'ing
 
82
 * TransactionLog::SYNC_METHOD_EVERY_WRITE == 1   ... sync on every write
 
83
 * TransactionLog::SYNC_METHOD_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_sync_method= 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("sync-method"))
 
132
  {
 
133
    if (sysvar_transaction_log_sync_method > 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_sync_method,
165
174
                                                  sysvar_transaction_log_checksum_enabled);
166
175
 
167
176
    if (transaction_log == NULL)
168
177
    {
169
 
      char errmsg[STRERROR_MAX];
170
 
      strerror_r(errno, errmsg, sizeof(errmsg));
171
178
      errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to allocate the TransactionLog instance.  Got error: %s\n"), 
172
 
                    errmsg);
 
179
                    strerror(errno));
173
180
      return 1;
174
181
    }
175
182
    else
187
194
    transaction_log_index= new (nothrow) TransactionLogIndex(*transaction_log);
188
195
    if (transaction_log_index == NULL)
189
196
    {
190
 
      char errmsg[STRERROR_MAX];
191
 
      strerror_r(errno, errmsg, sizeof(errmsg));
192
197
      errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to allocate the TransactionLogIndex instance.  Got error: %s\n"), 
193
 
                    errmsg);
 
198
                    strerror(errno));
194
199
      return 1;
195
200
    }
196
201
    else
208
213
    transaction_log_applier= new (nothrow) TransactionLogApplier("transaction_log_applier",
209
214
                                                                 transaction_log, 
210
215
                                                                 transaction_log_index, 
211
 
                                                                 static_cast<uint32_t>(sysvar_transaction_log_num_write_buffers));
 
216
                                                                 sysvar_transaction_log_num_write_buffers);
212
217
    if (transaction_log_applier == NULL)
213
218
    {
214
 
      char errmsg[STRERROR_MAX];
215
 
      strerror_r(errno, errmsg, sizeof(errmsg));
216
219
      errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to allocate the TransactionLogApplier instance.  Got error: %s\n"), 
217
 
                    errmsg);
 
220
                    strerror(errno));
218
221
      return 1;
219
222
    }
220
223
    context.add(transaction_log_applier);
221
224
    ReplicationServices &replication_services= ReplicationServices::singleton();
222
 
    replication_services.attachApplier(transaction_log_applier,
223
 
                                       sysvar_transaction_log_use_replicator);
 
225
    string replicator_name(sysvar_transaction_log_use_replicator);
 
226
    replication_services.attachApplier(transaction_log_applier, replicator_name);
224
227
 
225
228
    /* Setup DATA_DICTIONARY views */
226
229
 
251
254
}
252
255
 
253
256
 
 
257
static void set_truncate_debug(Session *,
 
258
                               drizzle_sys_var *, 
 
259
                               void *, 
 
260
                               const void *save)
 
261
{
 
262
  /* 
 
263
   * The const void * save comes directly from the check function, 
 
264
   * which should simply return the result from the set statement. 
 
265
   */
 
266
  if (transaction_log)
 
267
  {
 
268
    if (*(bool *)save != false)
 
269
    {
 
270
      transaction_log->truncate();
 
271
      transaction_log_index->clear();
 
272
    }
 
273
  }
 
274
}
 
275
 
 
276
static DRIZZLE_SYSVAR_BOOL(enable,
 
277
                           sysvar_transaction_log_enabled,
 
278
                           PLUGIN_VAR_NOCMDARG,
 
279
                           N_("Enable transaction log"),
 
280
                           NULL, /* check func */
 
281
                           NULL, /* update func */
 
282
                           false /* default */);
 
283
 
 
284
static DRIZZLE_SYSVAR_BOOL(truncate_debug,
 
285
                           sysvar_transaction_log_truncate_debug,
 
286
                           PLUGIN_VAR_NOCMDARG,
 
287
                           N_("DEBUGGING - Truncate transaction log"),
 
288
                           NULL, /* check func */
 
289
                           set_truncate_debug, /* update func */
 
290
                           false /* default */);
 
291
 
 
292
static DRIZZLE_SYSVAR_STR(file,
 
293
                          sysvar_transaction_log_file,
 
294
                          PLUGIN_VAR_READONLY,
 
295
                          N_("Path to the file to use for transaction log"),
 
296
                          NULL, /* check func */
 
297
                          NULL, /* update func*/
 
298
                          DEFAULT_LOG_FILE_PATH /* default */);
 
299
 
 
300
static DRIZZLE_SYSVAR_STR(use_replicator,
 
301
                          sysvar_transaction_log_use_replicator,
 
302
                          PLUGIN_VAR_READONLY,
 
303
                          N_("Name of the replicator plugin to use (default='default_replicator')"),
 
304
                          NULL, /* check func */
 
305
                          NULL, /* update func*/
 
306
                          DEFAULT_USE_REPLICATOR /* default */);
 
307
 
 
308
static DRIZZLE_SYSVAR_BOOL(enable_checksum,
 
309
                           sysvar_transaction_log_checksum_enabled,
 
310
                           PLUGIN_VAR_NOCMDARG,
 
311
                           N_("Enable CRC32 Checksumming of each written transaction log entry"),
 
312
                           NULL, /* check func */
 
313
                           NULL, /* update func */
 
314
                           false /* default */);
 
315
 
 
316
static DRIZZLE_SYSVAR_UINT(sync_method,
 
317
                           sysvar_transaction_log_sync_method,
 
318
                           PLUGIN_VAR_OPCMDARG,
 
319
                           N_("0 == rely on operating system to sync log file (default), "
 
320
                              "1 == sync file at each transaction write, "
 
321
                              "2 == sync log file once per second"),
 
322
                           NULL, /* check func */
 
323
                           NULL, /* update func */
 
324
                           0, /* default */
 
325
                           0,
 
326
                           2,
 
327
                           0);
 
328
 
 
329
static DRIZZLE_SYSVAR_UINT(num_write_buffers,
 
330
                           sysvar_transaction_log_num_write_buffers,
 
331
                           PLUGIN_VAR_OPCMDARG,
 
332
                           N_("Number of slots for in-memory write buffers (default=8)."),
 
333
                           NULL, /* check func */
 
334
                           NULL, /* update func */
 
335
                           8, /* default */
 
336
                           4,
 
337
                           8192,
 
338
                           0);
 
339
 
254
340
static void init_options(drizzled::module::option_context &context)
255
341
{
256
342
  context("truncate-debug",
263
349
          po::value<bool>(&sysvar_transaction_log_enabled)->default_value(false)->zero_tokens(),
264
350
          N_("Enable transaction log"));
265
351
  context("file",
266
 
          po::value<string>(&sysvar_transaction_log_file)->default_value(DEFAULT_LOG_FILE_PATH),
 
352
          po::value<string>(),
267
353
          N_("Path to the file to use for transaction log"));
268
354
  context("use-replicator",
269
 
          po::value<string>(&sysvar_transaction_log_use_replicator)->default_value(DEFAULT_USE_REPLICATOR),
 
355
          po::value<string>(),
270
356
          N_("Name of the replicator plugin to use (default='default_replicator')")); 
271
 
  context("flush-frequency",
272
 
          po::value<flush_constraint>(&sysvar_transaction_log_flush_frequency)->default_value(0),
 
357
  context("sync-method",
 
358
          po::value<uint32_t>(&sysvar_transaction_log_sync_method)->default_value(0),
273
359
          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
360
  context("num-write-buffers",
275
 
          po::value<write_buffers_constraint>(&sysvar_transaction_log_num_write_buffers)->default_value(8),
 
361
          po::value<uint32_t>(&sysvar_transaction_log_num_write_buffers)->default_value(8),
276
362
          N_("Number of slots for in-memory write buffers (default=8)."));
277
363
}
278
364
 
279
 
DRIZZLE_PLUGIN(init, NULL, init_options);
 
365
static drizzle_sys_var* sys_variables[]= {
 
366
  DRIZZLE_SYSVAR(enable),
 
367
  DRIZZLE_SYSVAR(truncate_debug),
 
368
  DRIZZLE_SYSVAR(file),
 
369
  DRIZZLE_SYSVAR(enable_checksum),
 
370
  DRIZZLE_SYSVAR(sync_method),
 
371
  DRIZZLE_SYSVAR(num_write_buffers),
 
372
  DRIZZLE_SYSVAR(use_replicator),
 
373
  NULL
 
374
};
 
375
 
 
376
DRIZZLE_PLUGIN(init, sys_variables, init_options);