~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/transaction_log/module.cc

  • Committer: Stewart Smith
  • Date: 2008-10-15 04:21:24 UTC
  • mto: This revision was merged to the branch mainline in revision 516.
  • Revision ID: stewart@flamingspork.com-20081015042124-kdmb74bcbky1k1nz
remove my_pthread_[gs]etspecific

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2008-2009 Sun Microsystems, Inc.
5
 
 *  Copyright (C) 2010 Jay Pipes <jaypipes@gmail.com>
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
 
 
33
 
#include <config.h>
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
 
 
42
 
#include <errno.h>
43
 
 
44
 
#include <drizzled/plugin/plugin.h>
45
 
#include <drizzled/session.h>
46
 
#include <drizzled/gettext.h>
47
 
#include <boost/program_options.hpp>
48
 
#include <drizzled/module/option_map.h>
49
 
 
50
 
namespace po= boost::program_options;
51
 
using namespace std;
52
 
using namespace drizzled;
53
 
 
54
 
/**
55
 
 * The name of the main transaction log file on disk.  With no prefix,
56
 
 * this goes into Drizzle's $datadir.
57
 
 */
58
 
static const char DEFAULT_LOG_FILE_PATH[]= "transaction.log"; /* In datadir... */
59
 
/** 
60
 
 * Transaction Log plugin system variable - Is the log enabled? Only used on init().  
61
 
 */
62
 
static bool sysvar_transaction_log_enabled= false;
63
 
 
64
 
/** Transaction Log plugin system variable - The path to the log file used */
65
 
static string sysvar_transaction_log_file;
66
 
 
67
 
/** 
68
 
 * Transaction Log plugin system variable - A debugging variable to assist 
69
 
 * in truncating the log file. 
70
 
 */
71
 
static bool sysvar_transaction_log_truncate_debug= false;
72
 
/** 
73
 
 * Transaction Log plugin system variable - Should we write a CRC32 checksum for 
74
 
 * each written Transaction message?
75
 
 */
76
 
static bool sysvar_transaction_log_checksum_enabled= false;
77
 
/**
78
 
 * Numeric option controlling the sync/flush behaviour of the transaction
79
 
 * log.  Options are:
80
 
 *
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
84
 
 */
85
 
typedef constrained_check<uint32_t, 2, 0> flush_constraint;
86
 
static flush_constraint sysvar_transaction_log_flush_frequency;
87
 
/**
88
 
 * Transaction Log plugin system variable - Number of slots to create
89
 
 * for managing write buffers
90
 
 */
91
 
typedef constrained_check<uint32_t, 8192, 4> write_buffers_constraint;
92
 
static write_buffers_constraint sysvar_transaction_log_num_write_buffers;
93
 
/**
94
 
 * Transaction Log plugin system variable - The name of the replicator plugin
95
 
 * to pair the transaction log's applier with.  Defaults to "default"
96
 
 */
97
 
static const char DEFAULT_USE_REPLICATOR[]= "default";
98
 
static string sysvar_transaction_log_use_replicator;
99
 
 
100
 
/** DATA_DICTIONARY views */
101
 
static TransactionLogTool *transaction_log_tool;
102
 
static TransactionLogEntriesTool *transaction_log_entries_tool;
103
 
static TransactionLogTransactionsTool *transaction_log_transactions_tool;
104
 
 
105
 
/** Index defined in transaction_log_index.cc */
106
 
extern TransactionLogIndex *transaction_log_index;
107
 
/** Transaction Log descriptor defined in transaction_log.cc */
108
 
extern TransactionLog *transaction_log;
109
 
/** Transaction Log descriptor defined in transaction_log.cc */
110
 
extern TransactionLogApplier *transaction_log_applier;
111
 
 
112
 
/** Defined in print_transaction_message.cc */
113
 
extern plugin::Create_function<PrintTransactionMessageFunction> *print_transaction_message_func_factory;
114
 
extern plugin::Create_function<HexdumpTransactionMessageFunction> *hexdump_transaction_message_func_factory;
115
 
 
116
 
TransactionLog::~TransactionLog()
117
 
{
118
 
  /* Clear up any resources we've consumed */
119
 
  if (log_file != -1)
120
 
  {
121
 
    (void) close(log_file);
122
 
  }
123
 
}
124
 
 
125
 
static void set_truncate_debug(Session *, sql_var_t)
126
 
{
127
 
  if (transaction_log)
128
 
  {
129
 
    if (sysvar_transaction_log_truncate_debug)
130
 
    {
131
 
      transaction_log->truncate();
132
 
      transaction_log_index->clear();
133
 
      sysvar_transaction_log_truncate_debug= false;
134
 
    }
135
 
  }
136
 
}
137
 
 
138
 
static int init(drizzled::module::Context &context)
139
 
{
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));
145
 
 
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));
153
 
 
154
 
  context.registerVariable(new sys_var_constrained_value_readonly<uint32_t>("num-write-buffers",
155
 
                                                                            sysvar_transaction_log_num_write_buffers));
156
 
 
157
 
 
158
 
  /* Create and initialize the transaction log itself */
159
 
  if (sysvar_transaction_log_enabled)
160
 
  {
161
 
  
162
 
    transaction_log= new (nothrow) TransactionLog(sysvar_transaction_log_file,
163
 
                                                  static_cast<int>(sysvar_transaction_log_flush_frequency),
164
 
                                                  sysvar_transaction_log_checksum_enabled);
165
 
 
166
 
    if (transaction_log == NULL)
167
 
    {
168
 
      sql_perror(_("Failed to allocate the TransactionLog instance"), sysvar_transaction_log_file);
169
 
      return 1;
170
 
    }
171
 
    else
172
 
    {
173
 
      /* Check to see if the log was not created properly */
174
 
      if (transaction_log->hasError())
175
 
      {
176
 
        errmsg_printf(error::ERROR, _("Failed to initialize the Transaction Log.  Got error: %s\n"), 
177
 
                      transaction_log->getErrorMessage().c_str());
178
 
        return 1;
179
 
      }
180
 
    }
181
 
 
182
 
    /* Create and initialize the transaction log index */
183
 
    transaction_log_index= new (nothrow) TransactionLogIndex(*transaction_log);
184
 
    if (transaction_log_index == NULL)
185
 
    {
186
 
      sql_perror(_("Failed to allocate the TransactionLogIndex instance"), sysvar_transaction_log_file);
187
 
      return 1;
188
 
    }
189
 
    else
190
 
    {
191
 
      /* Check to see if the index was not created properly */
192
 
      if (transaction_log_index->hasError())
193
 
      {
194
 
        errmsg_printf(error::ERROR, _("Failed to initialize the Transaction Log Index.  Got error: %s\n"), 
195
 
                      transaction_log_index->getErrorMessage().c_str());
196
 
        return 1;
197
 
      }
198
 
    }
199
 
 
200
 
    /* Create the applier plugin and register it */
201
 
    transaction_log_applier= new (nothrow) TransactionLogApplier("transaction_log_applier",
202
 
                                                                 transaction_log, 
203
 
                                                                 transaction_log_index, 
204
 
                                                                 static_cast<uint32_t>(sysvar_transaction_log_num_write_buffers));
205
 
    if (transaction_log_applier == NULL)
206
 
    {
207
 
      sql_perror(_("Failed to allocate the TransactionLogApplier instance"), sysvar_transaction_log_file);
208
 
      return 1;
209
 
    }
210
 
    context.add(transaction_log_applier);
211
 
    ReplicationServices &replication_services= ReplicationServices::singleton();
212
 
    replication_services.attachApplier(transaction_log_applier,
213
 
                                       sysvar_transaction_log_use_replicator);
214
 
 
215
 
    /* Setup DATA_DICTIONARY views */
216
 
 
217
 
    transaction_log_tool= new (nothrow) TransactionLogTool;
218
 
    context.add(transaction_log_tool);
219
 
    transaction_log_entries_tool= new (nothrow) TransactionLogEntriesTool;
220
 
    context.add(transaction_log_entries_tool);
221
 
    transaction_log_transactions_tool= new (nothrow) TransactionLogTransactionsTool;
222
 
    context.add(transaction_log_transactions_tool);
223
 
 
224
 
    /* Setup the module's UDFs */
225
 
    print_transaction_message_func_factory=
226
 
      new plugin::Create_function<PrintTransactionMessageFunction>("print_transaction_message");
227
 
    context.add(print_transaction_message_func_factory);
228
 
 
229
 
    hexdump_transaction_message_func_factory=
230
 
      new plugin::Create_function<HexdumpTransactionMessageFunction>("hexdump_transaction_message");
231
 
    context.add(hexdump_transaction_message_func_factory);
232
 
  }
233
 
  return 0;
234
 
}
235
 
 
236
 
 
237
 
static void init_options(drizzled::module::option_context &context)
238
 
{
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"));  
245
 
  context("enable",
246
 
          po::value<bool>(&sysvar_transaction_log_enabled)->default_value(false)->zero_tokens(),
247
 
          _("Enable transaction log"));
248
 
  context("file",
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)."));
260
 
}
261
 
 
262
 
DRIZZLE_PLUGIN(init, NULL, init_options);