40
40
#include "hexdump_transaction_message.h"
41
41
#include "background_worker.h"
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>
51
namespace po= boost::program_options;
52
48
using namespace std;
53
49
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
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
63
56
static bool sysvar_transaction_log_enabled= false;
65
57
/** Transaction Log plugin system variable - The path to the log file used */
66
static string sysvar_transaction_log_file;
58
static char* sysvar_transaction_log_file= NULL;
69
60
* Transaction Log plugin system variable - A debugging variable to assist
70
61
* in truncating the log file.
72
63
static bool sysvar_transaction_log_truncate_debug= false;
65
* The name of the main transaction log file on disk. With no prefix,
66
* this goes into Drizzle's $datadir.
68
static const char DEFAULT_LOG_FILE_PATH[]= "transaction.log"; /* In datadir... */
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:
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
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;
89
84
* Transaction Log plugin system variable - Number of slots to create
90
85
* 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;
87
static uint32_t sysvar_transaction_log_num_write_buffers= 8;
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"
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;
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;
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
111
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
113
/* Create and initialize the transaction log itself */
160
114
if (sysvar_transaction_log_enabled)
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);
167
120
if (transaction_log == NULL)
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"),
208
157
transaction_log_applier= new (nothrow) TransactionLogApplier("transaction_log_applier",
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)
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"),
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);
225
172
/* Setup DATA_DICTIONARY views */
254
static void init_options(drizzled::module::option_context &context)
201
static void set_truncate_debug(Session *,
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"));
263
po::value<bool>(&sysvar_transaction_log_enabled)->default_value(false)->zero_tokens(),
264
N_("Enable transaction log"));
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)."));
207
* The const void * save comes directly from the check function,
208
* which should simply return the result from the set statement.
212
if (*(bool *)save != false)
214
transaction_log->truncate();
215
transaction_log_index->clear();
279
DRIZZLE_PLUGIN(init, NULL, init_options);
220
static DRIZZLE_SYSVAR_BOOL(enable,
221
sysvar_transaction_log_enabled,
223
N_("Enable transaction log"),
224
NULL, /* check func */
225
NULL, /* update func */
226
false /* default */);
228
static DRIZZLE_SYSVAR_BOOL(truncate_debug,
229
sysvar_transaction_log_truncate_debug,
231
N_("DEBUGGING - Truncate transaction log"),
232
NULL, /* check func */
233
set_truncate_debug, /* update func */
234
false /* default */);
236
static DRIZZLE_SYSVAR_STR(file,
237
sysvar_transaction_log_file,
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 */);
244
static DRIZZLE_SYSVAR_STR(use_replicator,
245
sysvar_transaction_log_use_replicator,
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 */);
252
static DRIZZLE_SYSVAR_BOOL(enable_checksum,
253
sysvar_transaction_log_checksum_enabled,
255
N_("Enable CRC32 Checksumming of each written transaction log entry"),
256
NULL, /* check func */
257
NULL, /* update func */
258
false /* default */);
260
static DRIZZLE_SYSVAR_UINT(sync_method,
261
sysvar_transaction_log_sync_method,
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 */
273
static DRIZZLE_SYSVAR_UINT(num_write_buffers,
274
sysvar_transaction_log_num_write_buffers,
276
N_("Number of slots for in-memory write buffers (default=8)."),
277
NULL, /* check func */
278
NULL, /* update func */
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),
295
DRIZZLE_PLUGIN(init, sys_variables);