~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/transaction_log/module.cc

  • Committer: Stewart Smith
  • Date: 2010-11-03 03:27:09 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-20101103032709-oyvfrc6eb8fzj0mr
fix docs warning: docs/unlock.rst:2: (WARNING/2) Title underline too short.

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
 
43
45
#include <drizzled/plugin/plugin.h>
44
46
#include <drizzled/session.h>
45
 
#include <drizzled/set_var.h>
46
47
#include <drizzled/gettext.h>
 
48
#include <boost/program_options.hpp>
 
49
#include <drizzled/module/option_map.h>
47
50
 
 
51
namespace po= boost::program_options;
48
52
using namespace std;
49
53
using namespace drizzled;
50
54
 
 
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... */
51
60
/** 
52
61
 * Transaction Log plugin system variable - Is the log enabled? Only used on init().  
53
 
 * The enable() and disable() methods of the TransactionLog class control online
54
 
 * disabling.
55
62
 */
56
63
static bool sysvar_transaction_log_enabled= false;
 
64
 
57
65
/** Transaction Log plugin system variable - The path to the log file used */
58
 
static char* sysvar_transaction_log_file= NULL;
 
66
static char* sysvar_transaction_log_file= (char *)DEFAULT_LOG_FILE_PATH;
 
67
 
59
68
/** 
60
69
 * Transaction Log plugin system variable - A debugging variable to assist 
61
70
 * in truncating the log file. 
62
71
 */
63
72
static bool sysvar_transaction_log_truncate_debug= false;
64
 
static const char DEFAULT_LOG_FILE_PATH[]= "transaction.log"; /* In datadir... */
65
73
/** 
66
74
 * Transaction Log plugin system variable - Should we write a CRC32 checksum for 
67
75
 * each written Transaction message?
71
79
 * Numeric option controlling the sync/flush behaviour of the transaction
72
80
 * log.  Options are:
73
81
 *
74
 
 * TransactionLog::SYNC_METHOD_OS == 0            ... let OS do sync'ing
75
 
 * TransactionLog::SYNC_METHOD_EVERY_WRITE == 1   ... sync on every write
76
 
 * TransactionLog::SYNC_METHOD_EVERY_SECOND == 2  ... sync at most once a second
77
 
 */
78
 
static uint32_t sysvar_transaction_log_sync_method= 0;
 
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
 
85
 */
 
86
static uint32_t sysvar_transaction_log_flush_frequency= 0;
 
87
/**
 
88
 * Transaction Log plugin system variable - Number of slots to create
 
89
 * for managing write buffers
 
90
 */
 
91
static uint32_t sysvar_transaction_log_num_write_buffers= 8;
 
92
/**
 
93
 * Transaction Log plugin system variable - The name of the replicator plugin
 
94
 * to pair the transaction log's applier with.  Defaults to "default"
 
95
 */
 
96
static const char DEFAULT_USE_REPLICATOR[]= "default";
 
97
static char *sysvar_transaction_log_use_replicator= (char *)DEFAULT_USE_REPLICATOR;
79
98
 
80
99
/** DATA_DICTIONARY views */
81
100
static TransactionLogTool *transaction_log_tool;
93
112
extern plugin::Create_function<PrintTransactionMessageFunction> *print_transaction_message_func_factory;
94
113
extern plugin::Create_function<HexdumpTransactionMessageFunction> *hexdump_transaction_message_func_factory;
95
114
 
96
 
static int init(drizzled::plugin::Context &context)
97
 
{
 
115
TransactionLog::~TransactionLog()
 
116
{
 
117
  /* Clear up any resources we've consumed */
 
118
  if (log_file != -1)
 
119
  {
 
120
    (void) close(log_file);
 
121
  }
 
122
 
 
123
  /* These get strdup'd below */
 
124
  free(sysvar_transaction_log_file);
 
125
  free(sysvar_transaction_log_use_replicator);
 
126
}
 
127
 
 
128
static int init(drizzled::module::Context &context)
 
129
{
 
130
  const module::option_map &vm= context.getOptions();
 
131
 
 
132
  if (vm.count("flush-frequency"))
 
133
  {
 
134
    if (sysvar_transaction_log_flush_frequency > 2)
 
135
    {
 
136
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for sync-method\n"));
 
137
      exit(-1);
 
138
    }
 
139
  }
 
140
 
 
141
  if (vm.count("num-write-buffers"))
 
142
  {
 
143
    if (sysvar_transaction_log_num_write_buffers < 4 || sysvar_transaction_log_num_write_buffers > 8192)
 
144
    {
 
145
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for num-write-buffers\n"));
 
146
      exit(-1);
 
147
    }
 
148
  }
 
149
 
 
150
 
98
151
  /* Create and initialize the transaction log itself */
99
152
  if (sysvar_transaction_log_enabled)
100
153
  {
 
154
    if (vm.count("file"))
 
155
    {
 
156
      sysvar_transaction_log_file= strdup(vm["file"].as<string>().c_str());
 
157
    }
 
158
    else
 
159
    {
 
160
      sysvar_transaction_log_file= strdup(DEFAULT_LOG_FILE_PATH);
 
161
    }
 
162
  
 
163
    if (vm.count("use-replicator"))
 
164
    {
 
165
      sysvar_transaction_log_use_replicator= strdup(vm["use-replicator"].as<string>().c_str());
 
166
    }
 
167
    else
 
168
    {
 
169
      sysvar_transaction_log_use_replicator= strdup(DEFAULT_USE_REPLICATOR);
 
170
    }
101
171
    transaction_log= new (nothrow) TransactionLog(string(sysvar_transaction_log_file),
102
 
                                                  sysvar_transaction_log_sync_method);
 
172
                                                  sysvar_transaction_log_flush_frequency,
 
173
                                                  sysvar_transaction_log_checksum_enabled);
103
174
 
104
175
    if (transaction_log == NULL)
105
176
    {
 
177
      char errmsg[STRERROR_MAX];
 
178
      strerror_r(errno, errmsg, sizeof(errmsg));
106
179
      errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to allocate the TransactionLog instance.  Got error: %s\n"), 
107
 
                    strerror(errno));
 
180
                    errmsg);
108
181
      return 1;
109
182
    }
110
183
    else
117
190
        return 1;
118
191
      }
119
192
    }
 
193
 
 
194
    /* Create and initialize the transaction log index */
 
195
    transaction_log_index= new (nothrow) TransactionLogIndex(*transaction_log);
 
196
    if (transaction_log_index == NULL)
 
197
    {
 
198
      char errmsg[STRERROR_MAX];
 
199
      strerror_r(errno, errmsg, sizeof(errmsg));
 
200
      errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to allocate the TransactionLogIndex instance.  Got error: %s\n"), 
 
201
                    errmsg);
 
202
      return 1;
 
203
    }
 
204
    else
 
205
    {
 
206
      /* Check to see if the index was not created properly */
 
207
      if (transaction_log_index->hasError())
 
208
      {
 
209
        errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to initialize the Transaction Log Index.  Got error: %s\n"), 
 
210
                      transaction_log_index->getErrorMessage().c_str());
 
211
        return 1;
 
212
      }
 
213
    }
 
214
 
120
215
    /* Create the applier plugin and register it */
121
216
    transaction_log_applier= new (nothrow) TransactionLogApplier("transaction_log_applier",
122
 
                                                                 *transaction_log, 
123
 
                                                                 sysvar_transaction_log_checksum_enabled);
 
217
                                                                 transaction_log, 
 
218
                                                                 transaction_log_index, 
 
219
                                                                 sysvar_transaction_log_num_write_buffers);
124
220
    if (transaction_log_applier == NULL)
125
221
    {
 
222
      char errmsg[STRERROR_MAX];
 
223
      strerror_r(errno, errmsg, sizeof(errmsg));
126
224
      errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to allocate the TransactionLogApplier instance.  Got error: %s\n"), 
127
 
                    strerror(errno));
 
225
                    errmsg);
128
226
      return 1;
129
227
    }
130
228
    context.add(transaction_log_applier);
 
229
    ReplicationServices &replication_services= ReplicationServices::singleton();
 
230
    string replicator_name(sysvar_transaction_log_use_replicator);
 
231
    replication_services.attachApplier(transaction_log_applier, replicator_name);
131
232
 
132
233
    /* Setup DATA_DICTIONARY views */
133
234
 
147
248
      new plugin::Create_function<HexdumpTransactionMessageFunction>("hexdump_transaction_message");
148
249
    context.add(hexdump_transaction_message_func_factory);
149
250
 
150
 
    /* Create and initialize the transaction log index */
151
 
    transaction_log_index= new (nothrow) TransactionLogIndex(*transaction_log);
152
 
    if (transaction_log_index == NULL)
153
 
    {
154
 
      errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to allocate the TransactionLogIndex instance.  Got error: %s\n"), 
155
 
                    strerror(errno));
156
 
      return 1;
157
 
    }
158
 
    else
159
 
    {
160
 
      /* Check to see if the index was not created properly */
161
 
      if (transaction_log_index->hasError())
162
 
      {
163
 
        errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to initialize the Transaction Log Index.  Got error: %s\n"), 
164
 
                      transaction_log_index->getErrorMessage().c_str());
165
 
        return 1;
166
 
      }
167
 
    }
168
 
 
169
251
    /* 
170
252
     * Setup the background worker thread which maintains
171
253
     * summary information about the transaction log.
198
280
 
199
281
static DRIZZLE_SYSVAR_BOOL(enable,
200
282
                           sysvar_transaction_log_enabled,
201
 
                           PLUGIN_VAR_NOCMDARG,
 
283
                           PLUGIN_VAR_NOCMDARG | PLUGIN_VAR_READONLY,
202
284
                           N_("Enable transaction log"),
203
285
                           NULL, /* check func */
204
286
                           NULL, /* update func */
212
294
                           set_truncate_debug, /* update func */
213
295
                           false /* default */);
214
296
 
215
 
static DRIZZLE_SYSVAR_STR(log_file,
 
297
static DRIZZLE_SYSVAR_STR(file,
216
298
                          sysvar_transaction_log_file,
217
299
                          PLUGIN_VAR_READONLY,
218
300
                          N_("Path to the file to use for transaction log"),
220
302
                          NULL, /* update func*/
221
303
                          DEFAULT_LOG_FILE_PATH /* default */);
222
304
 
 
305
static DRIZZLE_SYSVAR_STR(use_replicator,
 
306
                          sysvar_transaction_log_use_replicator,
 
307
                          PLUGIN_VAR_READONLY,
 
308
                          N_("Name of the replicator plugin to use (default='default_replicator')"),
 
309
                          NULL, /* check func */
 
310
                          NULL, /* update func*/
 
311
                          DEFAULT_USE_REPLICATOR /* default */);
 
312
 
223
313
static DRIZZLE_SYSVAR_BOOL(enable_checksum,
224
314
                           sysvar_transaction_log_checksum_enabled,
225
315
                           PLUGIN_VAR_NOCMDARG,
228
318
                           NULL, /* update func */
229
319
                           false /* default */);
230
320
 
231
 
static DRIZZLE_SYSVAR_UINT(sync_method,
232
 
                           sysvar_transaction_log_sync_method,
 
321
static DRIZZLE_SYSVAR_UINT(flush_frequency,
 
322
                           sysvar_transaction_log_flush_frequency,
233
323
                           PLUGIN_VAR_OPCMDARG,
234
324
                           N_("0 == rely on operating system to sync log file (default), "
235
325
                              "1 == sync file at each transaction write, "
241
331
                           2,
242
332
                           0);
243
333
 
 
334
static DRIZZLE_SYSVAR_UINT(num_write_buffers,
 
335
                           sysvar_transaction_log_num_write_buffers,
 
336
                           PLUGIN_VAR_OPCMDARG,
 
337
                           N_("Number of slots for in-memory write buffers (default=8)."),
 
338
                           NULL, /* check func */
 
339
                           NULL, /* update func */
 
340
                           8, /* default */
 
341
                           4,
 
342
                           8192,
 
343
                           0);
 
344
 
 
345
static void init_options(drizzled::module::option_context &context)
 
346
{
 
347
  context("truncate-debug",
 
348
          po::value<bool>(&sysvar_transaction_log_truncate_debug)->default_value(false)->zero_tokens(),
 
349
          N_("DEBUGGING - Truncate transaction log"));
 
350
  context("enable-checksum",
 
351
          po::value<bool>(&sysvar_transaction_log_checksum_enabled)->default_value(false)->zero_tokens(),
 
352
          N_("Enable CRC32 Checksumming of each written transaction log entry"));  
 
353
  context("enable",
 
354
          po::value<bool>(&sysvar_transaction_log_enabled)->default_value(false)->zero_tokens(),
 
355
          N_("Enable transaction log"));
 
356
  context("file",
 
357
          po::value<string>(),
 
358
          N_("Path to the file to use for transaction log"));
 
359
  context("use-replicator",
 
360
          po::value<string>(),
 
361
          N_("Name of the replicator plugin to use (default='default_replicator')")); 
 
362
  context("flush-frequency",
 
363
          po::value<uint32_t>(&sysvar_transaction_log_flush_frequency)->default_value(0),
 
364
          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"));
 
365
  context("num-write-buffers",
 
366
          po::value<uint32_t>(&sysvar_transaction_log_num_write_buffers)->default_value(8),
 
367
          N_("Number of slots for in-memory write buffers (default=8)."));
 
368
}
 
369
 
244
370
static drizzle_sys_var* sys_variables[]= {
245
371
  DRIZZLE_SYSVAR(enable),
246
372
  DRIZZLE_SYSVAR(truncate_debug),
247
 
  DRIZZLE_SYSVAR(log_file),
 
373
  DRIZZLE_SYSVAR(file),
248
374
  DRIZZLE_SYSVAR(enable_checksum),
249
 
  DRIZZLE_SYSVAR(sync_method),
 
375
  DRIZZLE_SYSVAR(flush_frequency),
 
376
  DRIZZLE_SYSVAR(num_write_buffers),
 
377
  DRIZZLE_SYSVAR(use_replicator),
250
378
  NULL
251
379
};
252
380
 
253
 
DRIZZLE_PLUGIN(init, sys_variables);
 
381
DRIZZLE_PLUGIN(init, sys_variables, init_options);