38
38
#include "data_dictionary_schema.h"
39
39
#include "print_transaction_message.h"
40
40
#include "hexdump_transaction_message.h"
41
#include "background_worker.h"
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>
50
namespace po= boost::program_options;
51
48
using namespace std;
52
49
using namespace drizzled;
55
* The name of the main transaction log file on disk. With no prefix,
56
* this goes into Drizzle's $datadir.
58
static const char DEFAULT_LOG_FILE_PATH[]= "transaction.log"; /* In datadir... */
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
62
56
static bool sysvar_transaction_log_enabled= false;
64
57
/** Transaction Log plugin system variable - The path to the log file used */
65
static string sysvar_transaction_log_file;
58
static char* sysvar_transaction_log_file= NULL;
68
60
* Transaction Log plugin system variable - A debugging variable to assist
69
61
* in truncating the log file.
71
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... */
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:
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
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;
88
84
* Transaction Log plugin system variable - Number of slots to create
89
85
* for managing write buffers
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;
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"
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;
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;
116
TransactionLog::~TransactionLog()
118
/* Clear up any resources we've consumed */
121
(void) close(log_file);
125
static void set_truncate_debug(Session *, sql_var_t)
129
if (sysvar_transaction_log_truncate_debug)
131
transaction_log->truncate();
132
transaction_log_index->clear();
133
sysvar_transaction_log_truncate_debug= false;
138
111
static int init(drizzled::module::Context &context)
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));
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));
154
context.registerVariable(new sys_var_constrained_value_readonly<uint32_t>("num-write-buffers",
155
sysvar_transaction_log_num_write_buffers));
158
113
/* Create and initialize the transaction log itself */
159
114
if (sysvar_transaction_log_enabled)
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);
166
120
if (transaction_log == NULL)
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"),
201
157
transaction_log_applier= new (nothrow) TransactionLogApplier("transaction_log_applier",
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)
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"),
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);
215
172
/* Setup DATA_DICTIONARY views */
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);
191
* Setup the background worker thread which maintains
192
* summary information about the transaction log.
194
if (initTransactionLogBackgroundWorker())
195
return 1; /* Error message output handled in function above */
237
static void init_options(drizzled::module::option_context &context)
201
static void set_truncate_debug(Session *,
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"));
246
po::value<bool>(&sysvar_transaction_log_enabled)->default_value(false)->zero_tokens(),
247
_("Enable transaction log"));
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)."));
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();
262
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, NULL);