~posulliv/drizzle/optimizer-style-cleanup

« back to all changes in this revision

Viewing changes to plugin/transaction_log/module.cc

  • Committer: Padraig O'Sullivan
  • Date: 2010-03-15 14:05:26 UTC
  • mfrom: (1237.9.99 staging)
  • Revision ID: osullivan.padraig@gmail.com-20100315140526-opbgwdwn6tfecdkq
MergeĀ fromĀ trunk.

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
 
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
#include "background_worker.h"
 
42
 
 
43
#include <drizzled/plugin/plugin.h>
 
44
#include <drizzled/session.h>
 
45
#include <drizzled/set_var.h>
 
46
#include <drizzled/gettext.h>
 
47
 
 
48
using namespace std;
 
49
using namespace drizzled;
 
50
 
 
51
/** 
 
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
 
54
 * disabling.
 
55
 */
 
56
static bool sysvar_transaction_log_enabled= false;
 
57
/** Transaction Log plugin system variable - The path to the log file used */
 
58
static char* sysvar_transaction_log_file= NULL;
 
59
/** 
 
60
 * Transaction Log plugin system variable - A debugging variable to assist 
 
61
 * in truncating the log file. 
 
62
 */
 
63
static bool sysvar_transaction_log_truncate_debug= false;
 
64
static const char DEFAULT_LOG_FILE_PATH[]= "transaction.log"; /* In datadir... */
 
65
/** 
 
66
 * Transaction Log plugin system variable - Should we write a CRC32 checksum for 
 
67
 * each written Transaction message?
 
68
 */
 
69
static bool sysvar_transaction_log_checksum_enabled= false;
 
70
/**
 
71
 * Numeric option controlling the sync/flush behaviour of the transaction
 
72
 * log.  Options are:
 
73
 *
 
74
 * TransactionLog::SYNC_METHOD_OS == 0            ... let OS do sync'ing
 
75
 * TransactionLog::SYNC_METHOD_EVERY_WRITE == 1   ... sync on every write
 
76
 * TransactionLog::SYNC_METHOD_EVERY_SECOND == 2  ... sync at most once a second
 
77
 */
 
78
static uint32_t sysvar_transaction_log_sync_method= 0;
 
79
 
 
80
/** DATA_DICTIONARY views */
 
81
static TransactionLogTool *transaction_log_tool;
 
82
static TransactionLogEntriesTool *transaction_log_entries_tool;
 
83
static TransactionLogTransactionsTool *transaction_log_transactions_tool;
 
84
 
 
85
/** Index defined in transaction_log_index.cc */
 
86
extern TransactionLogIndex *transaction_log_index;
 
87
/** Transaction Log descriptor defined in transaction_log.cc */
 
88
extern TransactionLog *transaction_log;
 
89
/** Transaction Log descriptor defined in transaction_log.cc */
 
90
extern TransactionLogApplier *transaction_log_applier;
 
91
 
 
92
/** Defined in print_transaction_message.cc */
 
93
extern plugin::Create_function<PrintTransactionMessageFunction> *print_transaction_message_func_factory;
 
94
extern plugin::Create_function<HexdumpTransactionMessageFunction> *hexdump_transaction_message_func_factory;
 
95
 
 
96
static int init(drizzled::plugin::Registry &registry)
 
97
{
 
98
  /* Create and initialize the transaction log itself */
 
99
  if (sysvar_transaction_log_enabled)
 
100
  {
 
101
    transaction_log= new (nothrow) TransactionLog(string(sysvar_transaction_log_file),
 
102
                                                  sysvar_transaction_log_sync_method);
 
103
 
 
104
    if (transaction_log == NULL)
 
105
    {
 
106
      errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to allocate the TransactionLog instance.  Got error: %s\n"), 
 
107
                    strerror(errno));
 
108
      return 1;
 
109
    }
 
110
    else
 
111
    {
 
112
      /* Check to see if the log was not created properly */
 
113
      if (transaction_log->hasError())
 
114
      {
 
115
        errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to initialize the Transaction Log.  Got error: %s\n"), 
 
116
                      transaction_log->getErrorMessage().c_str());
 
117
        return 1;
 
118
      }
 
119
    }
 
120
    /* Create the applier plugin and register it */
 
121
    transaction_log_applier= new (nothrow) TransactionLogApplier("transaction_log_applier",
 
122
                                                                 *transaction_log, 
 
123
                                                                 sysvar_transaction_log_checksum_enabled);
 
124
    if (transaction_log_applier == NULL)
 
125
    {
 
126
      errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to allocate the TransactionLogApplier instance.  Got error: %s\n"), 
 
127
                    strerror(errno));
 
128
      return 1;
 
129
    }
 
130
    registry.add(transaction_log_applier);
 
131
 
 
132
    /* Setup DATA_DICTIONARY views */
 
133
 
 
134
    transaction_log_tool= new (nothrow) TransactionLogTool;
 
135
    registry.add(transaction_log_tool);
 
136
    transaction_log_entries_tool= new (nothrow) TransactionLogEntriesTool;
 
137
    registry.add(transaction_log_entries_tool);
 
138
    transaction_log_transactions_tool= new (nothrow) TransactionLogTransactionsTool;
 
139
    registry.add(transaction_log_transactions_tool);
 
140
 
 
141
    /* Setup the module's UDFs */
 
142
    print_transaction_message_func_factory=
 
143
      new plugin::Create_function<PrintTransactionMessageFunction>("print_transaction_message");
 
144
    registry.add(print_transaction_message_func_factory);
 
145
 
 
146
    hexdump_transaction_message_func_factory=
 
147
      new plugin::Create_function<HexdumpTransactionMessageFunction>("hexdump_transaction_message");
 
148
    registry.add(hexdump_transaction_message_func_factory);
 
149
 
 
150
    /* Create and initialize the transaction log index */
 
151
    transaction_log_index= new (nothrow) TransactionLogIndex(*transaction_log);
 
152
    if (transaction_log_index == NULL)
 
153
    {
 
154
      errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to allocate the TransactionLogIndex instance.  Got error: %s\n"), 
 
155
                    strerror(errno));
 
156
      return 1;
 
157
    }
 
158
    else
 
159
    {
 
160
      /* Check to see if the index was not created properly */
 
161
      if (transaction_log_index->hasError())
 
162
      {
 
163
        errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to initialize the Transaction Log Index.  Got error: %s\n"), 
 
164
                      transaction_log_index->getErrorMessage().c_str());
 
165
        return 1;
 
166
      }
 
167
    }
 
168
 
 
169
    /* 
 
170
     * Setup the background worker thread which maintains
 
171
     * summary information about the transaction log.
 
172
     */
 
173
    if (initTransactionLogBackgroundWorker())
 
174
      return 1; /* Error message output handled in function above */
 
175
  }
 
176
  return 0;
 
177
}
 
178
 
 
179
static int deinit(drizzled::plugin::Registry &registry)
 
180
{
 
181
  /* Cleanup the transaction log itself */
 
182
  if (transaction_log)
 
183
  {
 
184
    registry.remove(transaction_log_applier);
 
185
    delete transaction_log;
 
186
    delete transaction_log_index;
 
187
 
 
188
    /* Cleanup the DATA_DICTIONARY views */
 
189
    registry.remove(transaction_log_tool);
 
190
    delete transaction_log_tool;
 
191
    registry.remove(transaction_log_entries_tool);
 
192
    delete transaction_log_entries_tool;
 
193
    registry.remove(transaction_log_transactions_tool);
 
194
    delete transaction_log_transactions_tool;
 
195
 
 
196
    /* Cleanup module UDFs */
 
197
    registry.remove(print_transaction_message_func_factory);
 
198
    delete print_transaction_message_func_factory;
 
199
    registry.remove(hexdump_transaction_message_func_factory);
 
200
    delete hexdump_transaction_message_func_factory;
 
201
  }
 
202
 
 
203
  return 0;
 
204
}
 
205
 
 
206
static void set_truncate_debug(Session *,
 
207
                               drizzle_sys_var *, 
 
208
                               void *, 
 
209
                               const void *save)
 
210
{
 
211
  /* 
 
212
   * The const void * save comes directly from the check function, 
 
213
   * which should simply return the result from the set statement. 
 
214
   */
 
215
  if (transaction_log)
 
216
  {
 
217
    if (*(bool *)save != false)
 
218
    {
 
219
      transaction_log->truncate();
 
220
      transaction_log_index->clear();
 
221
    }
 
222
  }
 
223
}
 
224
 
 
225
static DRIZZLE_SYSVAR_BOOL(enable,
 
226
                           sysvar_transaction_log_enabled,
 
227
                           PLUGIN_VAR_NOCMDARG,
 
228
                           N_("Enable transaction log"),
 
229
                           NULL, /* check func */
 
230
                           NULL, /* update func */
 
231
                           false /* default */);
 
232
 
 
233
static DRIZZLE_SYSVAR_BOOL(truncate_debug,
 
234
                           sysvar_transaction_log_truncate_debug,
 
235
                           PLUGIN_VAR_NOCMDARG,
 
236
                           N_("DEBUGGING - Truncate transaction log"),
 
237
                           NULL, /* check func */
 
238
                           set_truncate_debug, /* update func */
 
239
                           false /* default */);
 
240
 
 
241
static DRIZZLE_SYSVAR_STR(log_file,
 
242
                          sysvar_transaction_log_file,
 
243
                          PLUGIN_VAR_READONLY,
 
244
                          N_("Path to the file to use for transaction log"),
 
245
                          NULL, /* check func */
 
246
                          NULL, /* update func*/
 
247
                          DEFAULT_LOG_FILE_PATH /* default */);
 
248
 
 
249
static DRIZZLE_SYSVAR_BOOL(enable_checksum,
 
250
                           sysvar_transaction_log_checksum_enabled,
 
251
                           PLUGIN_VAR_NOCMDARG,
 
252
                           N_("Enable CRC32 Checksumming of each written transaction log entry"),
 
253
                           NULL, /* check func */
 
254
                           NULL, /* update func */
 
255
                           false /* default */);
 
256
 
 
257
static DRIZZLE_SYSVAR_UINT(sync_method,
 
258
                           sysvar_transaction_log_sync_method,
 
259
                           PLUGIN_VAR_OPCMDARG,
 
260
                           N_("0 == rely on operating system to sync log file (default), "
 
261
                              "1 == sync file at each transaction write, "
 
262
                              "2 == sync log file once per second"),
 
263
                           NULL, /* check func */
 
264
                           NULL, /* update func */
 
265
                           0, /* default */
 
266
                           0,
 
267
                           2,
 
268
                           0);
 
269
 
 
270
static drizzle_sys_var* sys_variables[]= {
 
271
  DRIZZLE_SYSVAR(enable),
 
272
  DRIZZLE_SYSVAR(truncate_debug),
 
273
  DRIZZLE_SYSVAR(log_file),
 
274
  DRIZZLE_SYSVAR(enable_checksum),
 
275
  DRIZZLE_SYSVAR(sync_method),
 
276
  NULL
 
277
};
 
278
 
 
279
DRIZZLE_PLUGIN(init, deinit, sys_variables);