~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/transaction_log/module.cc

  • Committer: Paweł Blokus
  • Date: 2010-06-09 20:36:51 UTC
  • mto: This revision was merged to the branch mainline in revision 1620.
  • Revision ID: pawel@pawel-desktop-20100609203651-mbq5x34bt9m3kv0o
minor style fixes

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
 
#include <boost/program_options.hpp>
49
 
#include <drizzled/module/option_map.h>
50
47
 
51
 
namespace po= boost::program_options;
52
48
using namespace std;
53
49
using namespace drizzled;
54
50
 
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
51
/** 
61
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.
62
55
 */
63
56
static bool sysvar_transaction_log_enabled= false;
64
 
 
65
57
/** Transaction Log plugin system variable - The path to the log file used */
66
 
static string sysvar_transaction_log_file;
67
 
 
 
58
static char* sysvar_transaction_log_file= NULL;
68
59
/** 
69
60
 * Transaction Log plugin system variable - A debugging variable to assist 
70
61
 * in truncating the log file. 
71
62
 */
72
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... */
73
69
/** 
74
70
 * Transaction Log plugin system variable - Should we write a CRC32 checksum for 
75
71
 * each written Transaction message?
79
75
 * Numeric option controlling the sync/flush behaviour of the transaction
80
76
 * log.  Options are:
81
77
 *
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
 
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
85
81
 */
86
 
typedef constrained_check<uint32_t, 2, 0> flush_constraint;
87
 
static flush_constraint sysvar_transaction_log_flush_frequency;
 
82
static uint32_t sysvar_transaction_log_sync_method= 0;
88
83
/**
89
84
 * Transaction Log plugin system variable - Number of slots to create
90
85
 * for managing write buffers
91
86
 */
92
 
typedef constrained_check<uint32_t, 8192, 4> write_buffers_constraint;
93
 
static write_buffers_constraint sysvar_transaction_log_num_write_buffers;
 
87
static uint32_t sysvar_transaction_log_num_write_buffers= 8;
94
88
/**
95
89
 * Transaction Log plugin system variable - The name of the replicator plugin
96
90
 * to pair the transaction log's applier with.  Defaults to "default"
97
91
 */
 
92
static char *sysvar_transaction_log_use_replicator= NULL;
98
93
static const char DEFAULT_USE_REPLICATOR[]= "default";
99
 
static string sysvar_transaction_log_use_replicator;
100
94
 
101
95
/** DATA_DICTIONARY views */
102
96
static TransactionLogTool *transaction_log_tool;
114
108
extern plugin::Create_function<PrintTransactionMessageFunction> *print_transaction_message_func_factory;
115
109
extern plugin::Create_function<HexdumpTransactionMessageFunction> *hexdump_transaction_message_func_factory;
116
110
 
117
 
TransactionLog::~TransactionLog()
118
 
{
119
 
  /* Clear up any resources we've consumed */
120
 
  if (log_file != -1)
121
 
  {
122
 
    (void) close(log_file);
123
 
  }
124
 
}
125
 
 
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
 
  }
137
 
}
138
 
 
139
111
static int init(drizzled::module::Context &context)
140
112
{
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
 
 
158
 
 
159
113
  /* Create and initialize the transaction log itself */
160
114
  if (sysvar_transaction_log_enabled)
161
115
  {
162
 
  
163
 
    transaction_log= new (nothrow) TransactionLog(sysvar_transaction_log_file,
164
 
                                                  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,
165
118
                                                  sysvar_transaction_log_checksum_enabled);
166
119
 
167
120
    if (transaction_log == NULL)
168
121
    {
169
 
      char errmsg[STRERROR_MAX];
170
 
      strerror_r(errno, errmsg, sizeof(errmsg));
171
122
      errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to allocate the TransactionLog instance.  Got error: %s\n"), 
172
 
                    errmsg);
 
123
                    strerror(errno));
173
124
      return 1;
174
125
    }
175
126
    else
187
138
    transaction_log_index= new (nothrow) TransactionLogIndex(*transaction_log);
188
139
    if (transaction_log_index == NULL)
189
140
    {
190
 
      char errmsg[STRERROR_MAX];
191
 
      strerror_r(errno, errmsg, sizeof(errmsg));
192
141
      errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to allocate the TransactionLogIndex instance.  Got error: %s\n"), 
193
 
                    errmsg);
 
142
                    strerror(errno));
194
143
      return 1;
195
144
    }
196
145
    else
208
157
    transaction_log_applier= new (nothrow) TransactionLogApplier("transaction_log_applier",
209
158
                                                                 transaction_log, 
210
159
                                                                 transaction_log_index, 
211
 
                                                                 static_cast<uint32_t>(sysvar_transaction_log_num_write_buffers));
 
160
                                                                 sysvar_transaction_log_num_write_buffers);
212
161
    if (transaction_log_applier == NULL)
213
162
    {
214
 
      char errmsg[STRERROR_MAX];
215
 
      strerror_r(errno, errmsg, sizeof(errmsg));
216
163
      errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to allocate the TransactionLogApplier instance.  Got error: %s\n"), 
217
 
                    errmsg);
 
164
                    strerror(errno));
218
165
      return 1;
219
166
    }
220
167
    context.add(transaction_log_applier);
221
168
    ReplicationServices &replication_services= ReplicationServices::singleton();
222
 
    replication_services.attachApplier(transaction_log_applier,
223
 
                                       sysvar_transaction_log_use_replicator);
 
169
    string replicator_name(sysvar_transaction_log_use_replicator);
 
170
    replication_services.attachApplier(transaction_log_applier, replicator_name);
224
171
 
225
172
    /* Setup DATA_DICTIONARY views */
226
173
 
251
198
}
252
199
 
253
200
 
254
 
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)
255
205
{
256
 
  context("truncate-debug",
257
 
          po::value<bool>(&sysvar_transaction_log_truncate_debug)->default_value(false)->zero_tokens(),
258
 
          N_("DEBUGGING - Truncate transaction log"));
259
 
  context("enable-checksum",
260
 
          po::value<bool>(&sysvar_transaction_log_checksum_enabled)->default_value(false)->zero_tokens(),
261
 
          N_("Enable CRC32 Checksumming of each written transaction log entry"));  
262
 
  context("enable",
263
 
          po::value<bool>(&sysvar_transaction_log_enabled)->default_value(false)->zero_tokens(),
264
 
          N_("Enable transaction log"));
265
 
  context("file",
266
 
          po::value<string>(&sysvar_transaction_log_file)->default_value(DEFAULT_LOG_FILE_PATH),
267
 
          N_("Path to the file to use for transaction log"));
268
 
  context("use-replicator",
269
 
          po::value<string>(&sysvar_transaction_log_use_replicator)->default_value(DEFAULT_USE_REPLICATOR),
270
 
          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),
273
 
          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
 
  context("num-write-buffers",
275
 
          po::value<write_buffers_constraint>(&sysvar_transaction_log_num_write_buffers)->default_value(8),
276
 
          N_("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
  }
277
218
}
278
219
 
279
 
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);