~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/transaction_log/module.cc

  • Committer: Brian Aker
  • Date: 2010-07-28 23:07:42 UTC
  • mto: This revision was merged to the branch mainline in revision 1671.
  • Revision ID: brian@gaz-20100728230742-idji8pjd3trphd1a
Fix up a few additional cases around case insensitive usage for
unordered_map. This also places the code in util/string.h behind an
additional namespace of "util"

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
 *
30
30
 * registration.
31
31
 */
32
32
 
33
 
#include <config.h>
 
33
#include "config.h"
34
34
 
35
35
#include "transaction_log.h"
36
36
#include "transaction_log_applier.h"
38
38
#include "data_dictionary_schema.h"
39
39
#include "print_transaction_message.h"
40
40
#include "hexdump_transaction_message.h"
41
 
 
42
 
#include <errno.h>
 
41
#include "background_worker.h"
43
42
 
44
43
#include <drizzled/plugin/plugin.h>
45
44
#include <drizzled/session.h>
 
45
#include <drizzled/set_var.h>
46
46
#include <drizzled/gettext.h>
47
 
#include <boost/program_options.hpp>
48
 
#include <drizzled/module/option_map.h>
49
47
 
50
 
namespace po= boost::program_options;
51
48
using namespace std;
52
49
using namespace drizzled;
53
50
 
54
 
/**
55
 
 * The name of the main transaction log file on disk.  With no prefix,
56
 
 * this goes into Drizzle's $datadir.
57
 
 */
58
 
static const char DEFAULT_LOG_FILE_PATH[]= "transaction.log"; /* In datadir... */
59
51
/** 
60
52
 * 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.
61
55
 */
62
56
static bool sysvar_transaction_log_enabled= false;
63
 
 
64
57
/** Transaction Log plugin system variable - The path to the log file used */
65
 
static string sysvar_transaction_log_file;
66
 
 
 
58
static char* sysvar_transaction_log_file= NULL;
67
59
/** 
68
60
 * Transaction Log plugin system variable - A debugging variable to assist 
69
61
 * in truncating the log file. 
70
62
 */
71
63
static bool sysvar_transaction_log_truncate_debug= false;
 
64
/**
 
65
 * The name of the main transaction log file on disk.  With no prefix,
 
66
 * this goes into Drizzle's $datadir.
 
67
 */
 
68
static const char DEFAULT_LOG_FILE_PATH[]= "transaction.log"; /* In datadir... */
72
69
/** 
73
70
 * Transaction Log plugin system variable - Should we write a CRC32 checksum for 
74
71
 * each written Transaction message?
78
75
 * Numeric option controlling the sync/flush behaviour of the transaction
79
76
 * log.  Options are:
80
77
 *
81
 
 * TransactionLog::FLUSH_FREQUENCY_OS == 0            ... let OS do sync'ing
82
 
 * TransactionLog::FLUSH_FREQUENCY_EVERY_WRITE == 1   ... sync on every write
83
 
 * TransactionLog::FLUSH_FREQUENCY_EVERY_SECOND == 2  ... sync at most once a second
 
78
 * TransactionLog::SYNC_METHOD_OS == 0            ... let OS do sync'ing
 
79
 * TransactionLog::SYNC_METHOD_EVERY_WRITE == 1   ... sync on every write
 
80
 * TransactionLog::SYNC_METHOD_EVERY_SECOND == 2  ... sync at most once a second
84
81
 */
85
 
typedef constrained_check<uint32_t, 2, 0> flush_constraint;
86
 
static flush_constraint sysvar_transaction_log_flush_frequency;
 
82
static uint32_t sysvar_transaction_log_sync_method= 0;
87
83
/**
88
84
 * Transaction Log plugin system variable - Number of slots to create
89
85
 * for managing write buffers
90
86
 */
91
 
typedef constrained_check<uint32_t, 8192, 4> write_buffers_constraint;
92
 
static write_buffers_constraint sysvar_transaction_log_num_write_buffers;
 
87
static uint32_t sysvar_transaction_log_num_write_buffers= 8;
93
88
/**
94
89
 * Transaction Log plugin system variable - The name of the replicator plugin
95
90
 * to pair the transaction log's applier with.  Defaults to "default"
96
91
 */
 
92
static char *sysvar_transaction_log_use_replicator= NULL;
97
93
static const char DEFAULT_USE_REPLICATOR[]= "default";
98
 
static string sysvar_transaction_log_use_replicator;
99
94
 
100
95
/** DATA_DICTIONARY views */
101
96
static TransactionLogTool *transaction_log_tool;
113
108
extern plugin::Create_function<PrintTransactionMessageFunction> *print_transaction_message_func_factory;
114
109
extern plugin::Create_function<HexdumpTransactionMessageFunction> *hexdump_transaction_message_func_factory;
115
110
 
116
 
TransactionLog::~TransactionLog()
117
 
{
118
 
  /* Clear up any resources we've consumed */
119
 
  if (log_file != -1)
120
 
  {
121
 
    (void) close(log_file);
122
 
  }
123
 
}
124
 
 
125
 
static void set_truncate_debug(Session *, sql_var_t)
126
 
{
127
 
  if (transaction_log)
128
 
  {
129
 
    if (sysvar_transaction_log_truncate_debug)
130
 
    {
131
 
      transaction_log->truncate();
132
 
      transaction_log_index->clear();
133
 
      sysvar_transaction_log_truncate_debug= false;
134
 
    }
135
 
  }
136
 
}
137
 
 
138
111
static int init(drizzled::module::Context &context)
139
112
{
140
 
  context.registerVariable(new sys_var_bool_ptr_readonly("enable",
141
 
                                                         &sysvar_transaction_log_enabled));
142
 
  context.registerVariable(new sys_var_bool_ptr("truncate-debug",
143
 
                                                &sysvar_transaction_log_truncate_debug,
144
 
                                                set_truncate_debug));
145
 
 
146
 
  context.registerVariable(new sys_var_const_string("file",
147
 
                                                    sysvar_transaction_log_file));
148
 
  context.registerVariable(new sys_var_const_string("use-replicator",
149
 
                                                    sysvar_transaction_log_use_replicator));
150
 
  context.registerVariable(new sys_var_bool_ptr_readonly("enable-checksum",
151
 
                                                         &sysvar_transaction_log_checksum_enabled));
152
 
  context.registerVariable(new sys_var_constrained_value_readonly<uint32_t>("flush-frequency", sysvar_transaction_log_flush_frequency));
153
 
 
154
 
  context.registerVariable(new sys_var_constrained_value_readonly<uint32_t>("num-write-buffers",
155
 
                                                                            sysvar_transaction_log_num_write_buffers));
156
 
 
157
 
 
158
113
  /* Create and initialize the transaction log itself */
159
114
  if (sysvar_transaction_log_enabled)
160
115
  {
161
 
  
162
 
    transaction_log= new (nothrow) TransactionLog(sysvar_transaction_log_file,
163
 
                                                  static_cast<int>(sysvar_transaction_log_flush_frequency),
 
116
    transaction_log= new (nothrow) TransactionLog(string(sysvar_transaction_log_file),
 
117
                                                  sysvar_transaction_log_sync_method,
164
118
                                                  sysvar_transaction_log_checksum_enabled);
165
119
 
166
120
    if (transaction_log == NULL)
167
121
    {
168
 
      sql_perror(_("Failed to allocate the TransactionLog instance"), sysvar_transaction_log_file);
 
122
      errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to allocate the TransactionLog instance.  Got error: %s\n"), 
 
123
                    strerror(errno));
169
124
      return 1;
170
125
    }
171
126
    else
173
128
      /* Check to see if the log was not created properly */
174
129
      if (transaction_log->hasError())
175
130
      {
176
 
        errmsg_printf(error::ERROR, _("Failed to initialize the Transaction Log.  Got error: %s\n"), 
 
131
        errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to initialize the Transaction Log.  Got error: %s\n"), 
177
132
                      transaction_log->getErrorMessage().c_str());
178
133
        return 1;
179
134
      }
183
138
    transaction_log_index= new (nothrow) TransactionLogIndex(*transaction_log);
184
139
    if (transaction_log_index == NULL)
185
140
    {
186
 
      sql_perror(_("Failed to allocate the TransactionLogIndex instance"), sysvar_transaction_log_file);
 
141
      errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to allocate the TransactionLogIndex instance.  Got error: %s\n"), 
 
142
                    strerror(errno));
187
143
      return 1;
188
144
    }
189
145
    else
191
147
      /* Check to see if the index was not created properly */
192
148
      if (transaction_log_index->hasError())
193
149
      {
194
 
        errmsg_printf(error::ERROR, _("Failed to initialize the Transaction Log Index.  Got error: %s\n"), 
 
150
        errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to initialize the Transaction Log Index.  Got error: %s\n"), 
195
151
                      transaction_log_index->getErrorMessage().c_str());
196
152
        return 1;
197
153
      }
201
157
    transaction_log_applier= new (nothrow) TransactionLogApplier("transaction_log_applier",
202
158
                                                                 transaction_log, 
203
159
                                                                 transaction_log_index, 
204
 
                                                                 static_cast<uint32_t>(sysvar_transaction_log_num_write_buffers));
 
160
                                                                 sysvar_transaction_log_num_write_buffers);
205
161
    if (transaction_log_applier == NULL)
206
162
    {
207
 
      sql_perror(_("Failed to allocate the TransactionLogApplier instance"), sysvar_transaction_log_file);
 
163
      errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to allocate the TransactionLogApplier instance.  Got error: %s\n"), 
 
164
                    strerror(errno));
208
165
      return 1;
209
166
    }
210
167
    context.add(transaction_log_applier);
211
168
    ReplicationServices &replication_services= ReplicationServices::singleton();
212
 
    replication_services.attachApplier(transaction_log_applier,
213
 
                                       sysvar_transaction_log_use_replicator);
 
169
    string replicator_name(sysvar_transaction_log_use_replicator);
 
170
    replication_services.attachApplier(transaction_log_applier, replicator_name);
214
171
 
215
172
    /* Setup DATA_DICTIONARY views */
216
173
 
229
186
    hexdump_transaction_message_func_factory=
230
187
      new plugin::Create_function<HexdumpTransactionMessageFunction>("hexdump_transaction_message");
231
188
    context.add(hexdump_transaction_message_func_factory);
 
189
 
 
190
    /* 
 
191
     * Setup the background worker thread which maintains
 
192
     * summary information about the transaction log.
 
193
     */
 
194
    if (initTransactionLogBackgroundWorker())
 
195
      return 1; /* Error message output handled in function above */
232
196
  }
233
197
  return 0;
234
198
}
235
199
 
236
200
 
237
 
static void init_options(drizzled::module::option_context &context)
 
201
static void set_truncate_debug(Session *,
 
202
                               drizzle_sys_var *, 
 
203
                               void *, 
 
204
                               const void *save)
238
205
{
239
 
  context("truncate-debug",
240
 
          po::value<bool>(&sysvar_transaction_log_truncate_debug)->default_value(false)->zero_tokens(),
241
 
          _("DEBUGGING - Truncate transaction log"));
242
 
  context("enable-checksum",
243
 
          po::value<bool>(&sysvar_transaction_log_checksum_enabled)->default_value(false)->zero_tokens(),
244
 
          _("Enable CRC32 Checksumming of each written transaction log entry"));  
245
 
  context("enable",
246
 
          po::value<bool>(&sysvar_transaction_log_enabled)->default_value(false)->zero_tokens(),
247
 
          _("Enable transaction log"));
248
 
  context("file",
249
 
          po::value<string>(&sysvar_transaction_log_file)->default_value(DEFAULT_LOG_FILE_PATH),
250
 
          _("Path to the file to use for transaction log"));
251
 
  context("use-replicator",
252
 
          po::value<string>(&sysvar_transaction_log_use_replicator)->default_value(DEFAULT_USE_REPLICATOR),
253
 
          _("Name of the replicator plugin to use (default='default_replicator')")); 
254
 
  context("flush-frequency",
255
 
          po::value<flush_constraint>(&sysvar_transaction_log_flush_frequency)->default_value(0),
256
 
          _("0 == rely on operating system to sync log file (default), 1 == sync file at each transaction write, 2 == sync log file once per second"));
257
 
  context("num-write-buffers",
258
 
          po::value<write_buffers_constraint>(&sysvar_transaction_log_num_write_buffers)->default_value(8),
259
 
          _("Number of slots for in-memory write buffers (default=8)."));
 
206
  /* 
 
207
   * The const void * save comes directly from the check function, 
 
208
   * which should simply return the result from the set statement. 
 
209
   */
 
210
  if (transaction_log)
 
211
  {
 
212
    if (*(bool *)save != false)
 
213
    {
 
214
      transaction_log->truncate();
 
215
      transaction_log_index->clear();
 
216
    }
 
217
  }
260
218
}
261
219
 
262
 
DRIZZLE_PLUGIN(init, NULL, init_options);
 
220
static DRIZZLE_SYSVAR_BOOL(enable,
 
221
                           sysvar_transaction_log_enabled,
 
222
                           PLUGIN_VAR_NOCMDARG,
 
223
                           N_("Enable transaction log"),
 
224
                           NULL, /* check func */
 
225
                           NULL, /* update func */
 
226
                           false /* default */);
 
227
 
 
228
static DRIZZLE_SYSVAR_BOOL(truncate_debug,
 
229
                           sysvar_transaction_log_truncate_debug,
 
230
                           PLUGIN_VAR_NOCMDARG,
 
231
                           N_("DEBUGGING - Truncate transaction log"),
 
232
                           NULL, /* check func */
 
233
                           set_truncate_debug, /* update func */
 
234
                           false /* default */);
 
235
 
 
236
static DRIZZLE_SYSVAR_STR(file,
 
237
                          sysvar_transaction_log_file,
 
238
                          PLUGIN_VAR_READONLY,
 
239
                          N_("Path to the file to use for transaction log"),
 
240
                          NULL, /* check func */
 
241
                          NULL, /* update func*/
 
242
                          DEFAULT_LOG_FILE_PATH /* default */);
 
243
 
 
244
static DRIZZLE_SYSVAR_STR(use_replicator,
 
245
                          sysvar_transaction_log_use_replicator,
 
246
                          PLUGIN_VAR_READONLY,
 
247
                          N_("Name of the replicator plugin to use (default='default_replicator')"),
 
248
                          NULL, /* check func */
 
249
                          NULL, /* update func*/
 
250
                          DEFAULT_USE_REPLICATOR /* default */);
 
251
 
 
252
static DRIZZLE_SYSVAR_BOOL(enable_checksum,
 
253
                           sysvar_transaction_log_checksum_enabled,
 
254
                           PLUGIN_VAR_NOCMDARG,
 
255
                           N_("Enable CRC32 Checksumming of each written transaction log entry"),
 
256
                           NULL, /* check func */
 
257
                           NULL, /* update func */
 
258
                           false /* default */);
 
259
 
 
260
static DRIZZLE_SYSVAR_UINT(sync_method,
 
261
                           sysvar_transaction_log_sync_method,
 
262
                           PLUGIN_VAR_OPCMDARG,
 
263
                           N_("0 == rely on operating system to sync log file (default), "
 
264
                              "1 == sync file at each transaction write, "
 
265
                              "2 == sync log file once per second"),
 
266
                           NULL, /* check func */
 
267
                           NULL, /* update func */
 
268
                           0, /* default */
 
269
                           0,
 
270
                           2,
 
271
                           0);
 
272
 
 
273
static DRIZZLE_SYSVAR_UINT(num_write_buffers,
 
274
                           sysvar_transaction_log_num_write_buffers,
 
275
                           PLUGIN_VAR_OPCMDARG,
 
276
                           N_("Number of slots for in-memory write buffers (default=8)."),
 
277
                           NULL, /* check func */
 
278
                           NULL, /* update func */
 
279
                           8, /* default */
 
280
                           4,
 
281
                           8192,
 
282
                           0);
 
283
 
 
284
static drizzle_sys_var* sys_variables[]= {
 
285
  DRIZZLE_SYSVAR(enable),
 
286
  DRIZZLE_SYSVAR(truncate_debug),
 
287
  DRIZZLE_SYSVAR(file),
 
288
  DRIZZLE_SYSVAR(enable_checksum),
 
289
  DRIZZLE_SYSVAR(sync_method),
 
290
  DRIZZLE_SYSVAR(num_write_buffers),
 
291
  DRIZZLE_SYSVAR(use_replicator),
 
292
  NULL
 
293
};
 
294
 
 
295
DRIZZLE_PLUGIN(init, sys_variables, NULL);