1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008-2009 Sun Microsystems, Inc.
5
* Copyright (C) 2010 Jay Pipes <jaypipes@gmail.com>
9
* Jay Pipes <jaypipes@gmail.com.com>
11
* This program is free software; you can redistribute it and/or modify
12
* it under the terms of the GNU General Public License as published by
13
* the Free Software Foundation; either version 2 of the License, or
14
* (at your option) any later version.
16
* This program is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
* GNU General Public License for more details.
21
* You should have received a copy of the GNU General Public License
22
* along with this program; if not, write to the Free Software
23
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29
* Transaction log module initialization and plugin
35
#include "transaction_log.h"
36
#include "transaction_log_applier.h"
37
#include "transaction_log_index.h"
38
#include "data_dictionary_schema.h"
39
#include "print_transaction_message.h"
40
#include "hexdump_transaction_message.h"
41
#include "background_worker.h"
45
#include <drizzled/plugin/plugin.h>
46
#include <drizzled/session.h>
47
#include <drizzled/gettext.h>
48
#include <boost/program_options.hpp>
49
#include <drizzled/module/option_map.h>
51
namespace po= boost::program_options;
53
using namespace drizzled;
56
* The name of the main transaction log file on disk. With no prefix,
57
* this goes into Drizzle's $datadir.
59
static const char DEFAULT_LOG_FILE_PATH[]= "transaction.log"; /* In datadir... */
61
* Transaction Log plugin system variable - Is the log enabled? Only used on init().
63
static bool sysvar_transaction_log_enabled= false;
65
/** Transaction Log plugin system variable - The path to the log file used */
66
static string sysvar_transaction_log_file;
69
* Transaction Log plugin system variable - A debugging variable to assist
70
* in truncating the log file.
72
static bool sysvar_transaction_log_truncate_debug= false;
74
* Transaction Log plugin system variable - Should we write a CRC32 checksum for
75
* each written Transaction message?
77
static bool sysvar_transaction_log_checksum_enabled= false;
79
* Numeric option controlling the sync/flush behaviour of the transaction
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
86
typedef constrained_check<uint32_t, 2, 0> flush_constraint;
87
static flush_constraint sysvar_transaction_log_flush_frequency;
89
* Transaction Log plugin system variable - Number of slots to create
90
* for managing write buffers
92
typedef constrained_check<uint32_t, 8192, 4> write_buffers_constraint;
93
static write_buffers_constraint sysvar_transaction_log_num_write_buffers;
95
* Transaction Log plugin system variable - The name of the replicator plugin
96
* to pair the transaction log's applier with. Defaults to "default"
98
static const char DEFAULT_USE_REPLICATOR[]= "default";
99
static string sysvar_transaction_log_use_replicator;
101
/** DATA_DICTIONARY views */
102
static TransactionLogTool *transaction_log_tool;
103
static TransactionLogEntriesTool *transaction_log_entries_tool;
104
static TransactionLogTransactionsTool *transaction_log_transactions_tool;
106
/** Index defined in transaction_log_index.cc */
107
extern TransactionLogIndex *transaction_log_index;
108
/** Transaction Log descriptor defined in transaction_log.cc */
109
extern TransactionLog *transaction_log;
110
/** Transaction Log descriptor defined in transaction_log.cc */
111
extern TransactionLogApplier *transaction_log_applier;
113
/** Defined in print_transaction_message.cc */
114
extern plugin::Create_function<PrintTransactionMessageFunction> *print_transaction_message_func_factory;
115
extern plugin::Create_function<HexdumpTransactionMessageFunction> *hexdump_transaction_message_func_factory;
117
TransactionLog::~TransactionLog()
119
/* Clear up any resources we've consumed */
122
(void) close(log_file);
126
static void set_truncate_debug(Session *, sql_var_t)
130
if (sysvar_transaction_log_truncate_debug)
132
transaction_log->truncate();
133
transaction_log_index->clear();
134
sysvar_transaction_log_truncate_debug= false;
139
static int init(drizzled::module::Context &context)
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));
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));
155
context.registerVariable(new sys_var_constrained_value_readonly<uint32_t>("num-write-buffers",
156
sysvar_transaction_log_num_write_buffers));
159
/* Create and initialize the transaction log itself */
160
if (sysvar_transaction_log_enabled)
163
transaction_log= new (nothrow) TransactionLog(sysvar_transaction_log_file,
164
static_cast<int>(sysvar_transaction_log_flush_frequency),
165
sysvar_transaction_log_checksum_enabled);
167
if (transaction_log == NULL)
169
sql_perror(_("Failed to allocate the TransactionLog instance"), sysvar_transaction_log_file);
174
/* Check to see if the log was not created properly */
175
if (transaction_log->hasError())
177
errmsg_printf(error::ERROR, _("Failed to initialize the Transaction Log. Got error: %s\n"),
178
transaction_log->getErrorMessage().c_str());
183
/* Create and initialize the transaction log index */
184
transaction_log_index= new (nothrow) TransactionLogIndex(*transaction_log);
185
if (transaction_log_index == NULL)
187
sql_perror(_("Failed to allocate the TransactionLogIndex instance"), sysvar_transaction_log_file);
192
/* Check to see if the index was not created properly */
193
if (transaction_log_index->hasError())
195
errmsg_printf(error::ERROR, _("Failed to initialize the Transaction Log Index. Got error: %s\n"),
196
transaction_log_index->getErrorMessage().c_str());
201
/* Create the applier plugin and register it */
202
transaction_log_applier= new (nothrow) TransactionLogApplier("transaction_log_applier",
204
transaction_log_index,
205
static_cast<uint32_t>(sysvar_transaction_log_num_write_buffers));
206
if (transaction_log_applier == NULL)
208
sql_perror(_("Failed to allocate the TransactionLogApplier instance"), sysvar_transaction_log_file);
211
context.add(transaction_log_applier);
212
ReplicationServices &replication_services= ReplicationServices::singleton();
213
replication_services.attachApplier(transaction_log_applier,
214
sysvar_transaction_log_use_replicator);
216
/* Setup DATA_DICTIONARY views */
218
transaction_log_tool= new (nothrow) TransactionLogTool;
219
context.add(transaction_log_tool);
220
transaction_log_entries_tool= new (nothrow) TransactionLogEntriesTool;
221
context.add(transaction_log_entries_tool);
222
transaction_log_transactions_tool= new (nothrow) TransactionLogTransactionsTool;
223
context.add(transaction_log_transactions_tool);
225
/* Setup the module's UDFs */
226
print_transaction_message_func_factory=
227
new plugin::Create_function<PrintTransactionMessageFunction>("print_transaction_message");
228
context.add(print_transaction_message_func_factory);
230
hexdump_transaction_message_func_factory=
231
new plugin::Create_function<HexdumpTransactionMessageFunction>("hexdump_transaction_message");
232
context.add(hexdump_transaction_message_func_factory);
235
* Setup the background worker thread which maintains
236
* summary information about the transaction log.
238
if (initTransactionLogBackgroundWorker())
239
return 1; /* Error message output handled in function above */
245
static void init_options(drizzled::module::option_context &context)
247
context("truncate-debug",
248
po::value<bool>(&sysvar_transaction_log_truncate_debug)->default_value(false)->zero_tokens(),
249
_("DEBUGGING - Truncate transaction log"));
250
context("enable-checksum",
251
po::value<bool>(&sysvar_transaction_log_checksum_enabled)->default_value(false)->zero_tokens(),
252
_("Enable CRC32 Checksumming of each written transaction log entry"));
254
po::value<bool>(&sysvar_transaction_log_enabled)->default_value(false)->zero_tokens(),
255
_("Enable transaction log"));
257
po::value<string>(&sysvar_transaction_log_file)->default_value(DEFAULT_LOG_FILE_PATH),
258
_("Path to the file to use for transaction log"));
259
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')"));
262
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"));
265
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)."));
270
DRIZZLE_PLUGIN(init, NULL, init_options);