~drizzle-trunk/drizzle/development

1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2008-2009 Sun Microsystems, Inc.
5
 *  Copyright (C) 2010 Jay Pipes <jaypipes@gmail.com>
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
6
 *
7
 *  Authors:
8
 *
9
 *  Jay Pipes <jaypipes@gmail.com.com>
10
 *
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.
15
 *
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.
20
 *
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
24
 */
25
26
/**
27
 * @file
28
 *
29
 * Transaction log module initialization and plugin
30
 * registration.
31
 */
32
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
33
#include <config.h>
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
34
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
1702.3.3 by LinuxJedi
Fix errors in FreeBSD build
42
#include <errno.h>
43
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
44
#include <drizzled/plugin/plugin.h>
45
#include <drizzled/session.h>
46
#include <drizzled/gettext.h>
1684.5.1 by Vijay Samuel
Merge re factored command line for transaction_log
47
#include <boost/program_options.hpp>
48
#include <drizzled/module/option_map.h>
2198.1.2 by Olaf van der Spek
Refactor includes
49
#include <drizzled/plugin/function.h>
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
50
1684.5.1 by Vijay Samuel
Merge re factored command line for transaction_log
51
namespace po= boost::program_options;
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
52
using namespace std;
53
using namespace drizzled;
54
1840.1.4 by Monty Taylor
We were strdup-ing an option all the time, but only freeing it if the transaction_log is enabled.
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... */
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
60
/** 
61
 * Transaction Log plugin system variable - Is the log enabled? Only used on init().  
62
 */
63
static bool sysvar_transaction_log_enabled= false;
1840.1.4 by Monty Taylor
We were strdup-ing an option all the time, but only freeing it if the transaction_log is enabled.
64
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
65
/** Transaction Log plugin system variable - The path to the log file used */
1897.4.10 by Monty Taylor
Converted transaction_log.
66
static string sysvar_transaction_log_file;
1840.1.4 by Monty Taylor
We were strdup-ing an option all the time, but only freeing it if the transaction_log is enabled.
67
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
68
/** 
69
 * Transaction Log plugin system variable - A debugging variable to assist 
70
 * in truncating the log file. 
71
 */
72
static bool sysvar_transaction_log_truncate_debug= false;
73
/** 
74
 * Transaction Log plugin system variable - Should we write a CRC32 checksum for 
75
 * each written Transaction message?
76
 */
77
static bool sysvar_transaction_log_checksum_enabled= false;
78
/**
79
 * Numeric option controlling the sync/flush behaviour of the transaction
80
 * log.  Options are:
81
 *
1726.2.1 by Vijay Samuel
Merge changes to transaction_log. Changed --transaction-log.sync-method to --transaction-log.flush-frequency
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
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
85
 */
1964.2.10 by Monty Taylor
Changed show SHOW_INT works ... man, trying to cast things as a method of data storage is lame.
86
typedef constrained_check<uint32_t, 2, 0> flush_constraint;
1897.4.10 by Monty Taylor
Converted transaction_log.
87
static flush_constraint sysvar_transaction_log_flush_frequency;
1405.4.1 by Jay Pipes
Adds a vector of write buffers to the transaction log applier
88
/**
89
 * Transaction Log plugin system variable - Number of slots to create
90
 * for managing write buffers
91
 */
1897.4.10 by Monty Taylor
Converted transaction_log.
92
typedef constrained_check<uint32_t, 8192, 4> write_buffers_constraint;
93
static write_buffers_constraint sysvar_transaction_log_num_write_buffers;
1405.6.1 by Jay Pipes
This patch adds the following functionality:
94
/**
95
 * Transaction Log plugin system variable - The name of the replicator plugin
96
 * to pair the transaction log's applier with.  Defaults to "default"
97
 */
98
static const char DEFAULT_USE_REPLICATOR[]= "default";
1897.4.10 by Monty Taylor
Converted transaction_log.
99
static string sysvar_transaction_log_use_replicator;
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
100
101
/** DATA_DICTIONARY views */
102
static TransactionLogTool *transaction_log_tool;
103
static TransactionLogEntriesTool *transaction_log_entries_tool;
104
static TransactionLogTransactionsTool *transaction_log_transactions_tool;
105
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;
112
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;
116
1666.4.19 by Monty Taylor
Free strdup'd option strings.
117
TransactionLog::~TransactionLog()
118
{
119
  /* Clear up any resources we've consumed */
120
  if (log_file != -1)
121
  {
122
    (void) close(log_file);
123
  }
1897.4.10 by Monty Taylor
Converted transaction_log.
124
}
1666.4.19 by Monty Taylor
Free strdup'd option strings.
125
1897.4.10 by Monty Taylor
Converted transaction_log.
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
  }
1666.4.19 by Monty Taylor
Free strdup'd option strings.
137
}
138
1530.2.6 by Monty Taylor
Moved plugin::Context to module::Context.
139
static int init(drizzled::module::Context &context)
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
140
{
1897.4.10 by Monty Taylor
Converted transaction_log.
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));
1964.2.10 by Monty Taylor
Changed show SHOW_INT works ... man, trying to cast things as a method of data storage is lame.
153
  context.registerVariable(new sys_var_constrained_value_readonly<uint32_t>("flush-frequency", sysvar_transaction_log_flush_frequency));
1897.4.10 by Monty Taylor
Converted transaction_log.
154
155
  context.registerVariable(new sys_var_constrained_value_readonly<uint32_t>("num-write-buffers",
156
                                                                            sysvar_transaction_log_num_write_buffers));
1684.5.1 by Vijay Samuel
Merge re factored command line for transaction_log
157
158
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
159
  /* Create and initialize the transaction log itself */
160
  if (sysvar_transaction_log_enabled)
161
  {
1840.1.4 by Monty Taylor
We were strdup-ing an option all the time, but only freeing it if the transaction_log is enabled.
162
  
2246.3.1 by Olaf van der Spek
Remove std::nothrow from new()
163
    transaction_log= new TransactionLog(sysvar_transaction_log_file,
1897.4.10 by Monty Taylor
Converted transaction_log.
164
                                                  static_cast<int>(sysvar_transaction_log_flush_frequency),
1405.4.1 by Jay Pipes
Adds a vector of write buffers to the transaction log applier
165
                                                  sysvar_transaction_log_checksum_enabled);
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
166
167
    if (transaction_log == NULL)
168
    {
2126.3.3 by Brian Aker
Merge in error message rework. Many error messages are fixed in this patch.
169
      sql_perror(_("Failed to allocate the TransactionLog instance"), sysvar_transaction_log_file);
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
170
      return 1;
171
    }
172
    else
173
    {
174
      /* Check to see if the log was not created properly */
175
      if (transaction_log->hasError())
176
      {
2126.3.3 by Brian Aker
Merge in error message rework. Many error messages are fixed in this patch.
177
        errmsg_printf(error::ERROR, _("Failed to initialize the Transaction Log.  Got error: %s\n"), 
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
178
                      transaction_log->getErrorMessage().c_str());
179
        return 1;
180
      }
181
    }
1455.5.1 by Jay Pipes
TransactionLogApplier now manages (frees) memory for TransactionLog and TransactionLogIndex
182
183
    /* Create and initialize the transaction log index */
2246.3.6 by Olaf van der Spek
Remove last nothrow bit
184
    transaction_log_index= new TransactionLogIndex(*transaction_log);
1455.5.1 by Jay Pipes
TransactionLogApplier now manages (frees) memory for TransactionLog and TransactionLogIndex
185
      /* Check to see if the index was not created properly */
186
      if (transaction_log_index->hasError())
187
      {
2126.3.3 by Brian Aker
Merge in error message rework. Many error messages are fixed in this patch.
188
        errmsg_printf(error::ERROR, _("Failed to initialize the Transaction Log Index.  Got error: %s\n"), 
1455.5.1 by Jay Pipes
TransactionLogApplier now manages (frees) memory for TransactionLog and TransactionLogIndex
189
                      transaction_log_index->getErrorMessage().c_str());
190
        return 1;
191
      }
192
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
193
    /* Create the applier plugin and register it */
2246.3.1 by Olaf van der Spek
Remove std::nothrow from new()
194
    transaction_log_applier= new TransactionLogApplier("transaction_log_applier",
1452.1.2 by mordred
Made the TransactionLogApplier own the memory of the TransactionLog.
195
                                                                 transaction_log, 
1455.5.1 by Jay Pipes
TransactionLogApplier now manages (frees) memory for TransactionLog and TransactionLogIndex
196
                                                                 transaction_log_index, 
1897.4.10 by Monty Taylor
Converted transaction_log.
197
                                                                 static_cast<uint32_t>(sysvar_transaction_log_num_write_buffers));
1324.2.5 by Monty Taylor
Merged trunk.
198
    context.add(transaction_log_applier);
2318.6.73 by Olaf van der Spek
Refactor
199
    ReplicationServices::attachApplier(transaction_log_applier, sysvar_transaction_log_use_replicator);
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
200
201
    /* Setup DATA_DICTIONARY views */
202
2246.3.1 by Olaf van der Spek
Remove std::nothrow from new()
203
    transaction_log_tool= new TransactionLogTool;
1324.2.5 by Monty Taylor
Merged trunk.
204
    context.add(transaction_log_tool);
2246.3.1 by Olaf van der Spek
Remove std::nothrow from new()
205
    transaction_log_entries_tool= new TransactionLogEntriesTool;
1324.2.5 by Monty Taylor
Merged trunk.
206
    context.add(transaction_log_entries_tool);
2246.3.1 by Olaf van der Spek
Remove std::nothrow from new()
207
    transaction_log_transactions_tool= new TransactionLogTransactionsTool;
1324.2.5 by Monty Taylor
Merged trunk.
208
    context.add(transaction_log_transactions_tool);
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
209
210
    /* Setup the module's UDFs */
211
    print_transaction_message_func_factory=
212
      new plugin::Create_function<PrintTransactionMessageFunction>("print_transaction_message");
1324.2.5 by Monty Taylor
Merged trunk.
213
    context.add(print_transaction_message_func_factory);
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
214
215
    hexdump_transaction_message_func_factory=
216
      new plugin::Create_function<HexdumpTransactionMessageFunction>("hexdump_transaction_message");
1324.2.5 by Monty Taylor
Merged trunk.
217
    context.add(hexdump_transaction_message_func_factory);
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
218
  }
219
  return 0;
220
}
221
222
1684.5.1 by Vijay Samuel
Merge re factored command line for transaction_log
223
static void init_options(drizzled::module::option_context &context)
224
{
225
  context("truncate-debug",
226
          po::value<bool>(&sysvar_transaction_log_truncate_debug)->default_value(false)->zero_tokens(),
2068.4.1 by Andrew Hutchings
Fix intl domain
227
          _("DEBUGGING - Truncate transaction log"));
1684.5.1 by Vijay Samuel
Merge re factored command line for transaction_log
228
  context("enable-checksum",
229
          po::value<bool>(&sysvar_transaction_log_checksum_enabled)->default_value(false)->zero_tokens(),
2068.4.1 by Andrew Hutchings
Fix intl domain
230
          _("Enable CRC32 Checksumming of each written transaction log entry"));  
1684.6.1 by Monty Taylor
Fixed a silly little thing. We should report this upstream to boost, because it's stupid.
231
  context("enable",
232
          po::value<bool>(&sysvar_transaction_log_enabled)->default_value(false)->zero_tokens(),
2068.4.1 by Andrew Hutchings
Fix intl domain
233
          _("Enable transaction log"));
1684.5.1 by Vijay Samuel
Merge re factored command line for transaction_log
234
  context("file",
1897.4.10 by Monty Taylor
Converted transaction_log.
235
          po::value<string>(&sysvar_transaction_log_file)->default_value(DEFAULT_LOG_FILE_PATH),
2068.4.1 by Andrew Hutchings
Fix intl domain
236
          _("Path to the file to use for transaction log"));
1684.5.1 by Vijay Samuel
Merge re factored command line for transaction_log
237
  context("use-replicator",
1897.4.10 by Monty Taylor
Converted transaction_log.
238
          po::value<string>(&sysvar_transaction_log_use_replicator)->default_value(DEFAULT_USE_REPLICATOR),
2068.4.1 by Andrew Hutchings
Fix intl domain
239
          _("Name of the replicator plugin to use (default='default_replicator')")); 
1726.2.1 by Vijay Samuel
Merge changes to transaction_log. Changed --transaction-log.sync-method to --transaction-log.flush-frequency
240
  context("flush-frequency",
1897.4.10 by Monty Taylor
Converted transaction_log.
241
          po::value<flush_constraint>(&sysvar_transaction_log_flush_frequency)->default_value(0),
2068.4.1 by Andrew Hutchings
Fix intl domain
242
          _("0 == rely on operating system to sync log file (default), 1 == sync file at each transaction write, 2 == sync log file once per second"));
1684.5.1 by Vijay Samuel
Merge re factored command line for transaction_log
243
  context("num-write-buffers",
1897.4.10 by Monty Taylor
Converted transaction_log.
244
          po::value<write_buffers_constraint>(&sysvar_transaction_log_num_write_buffers)->default_value(8),
2068.4.1 by Andrew Hutchings
Fix intl domain
245
          _("Number of slots for in-memory write buffers (default=8)."));
1684.5.1 by Vijay Samuel
Merge re factored command line for transaction_log
246
}
247
1897.4.10 by Monty Taylor
Converted transaction_log.
248
DRIZZLE_PLUGIN(init, NULL, init_options);