~clint-fewbar/drizzle/regex-policy-cache-limiter

« back to all changes in this revision

Viewing changes to plugin/transaction_log/transaction_log.h

  • Committer: Clint Byrum
  • Date: 2012-03-15 18:05:43 UTC
  • mfrom: (2224.1.302 workspace)
  • Revision ID: clint@ubuntu.com-20120315180543-9jxxm4q10k3np2ws
merging with latest 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, 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
 
 * Defines the API of the transaction log file descriptor.
30
 
 *
31
 
 * @details
32
 
 *
33
 
 * Basically, the TransactionLog is a descriptor for a log
34
 
 * file containing transaction messages that is written by
35
 
 * the TransactionLogApplier plugin(s).
36
 
 */
37
 
 
38
 
#ifndef PLUGIN_TRANSACTION_LOG_TRANSACTION_LOG_H
39
 
#define PLUGIN_TRANSACTION_LOG_TRANSACTION_LOG_H
40
 
 
41
 
#include <drizzled/atomics.h>
42
 
#include <drizzled/replication_services.h>
43
 
 
44
 
#include "transaction_log_entry.h"
45
 
 
46
 
#include <vector>
47
 
#include <string>
48
 
 
49
 
class TransactionLog
50
 
{
51
 
public:
52
 
  typedef std::vector<TransactionLogEntry> Entries;
53
 
  typedef std::vector<TransactionLogTransactionEntry> TransactionEntries;
54
 
  /**
55
 
   * The state the log is in
56
 
   */
57
 
  enum Status
58
 
  {
59
 
    CRASHED= 0,
60
 
    OFFLINE, /* Default state, uninited. */
61
 
    ONLINE,
62
 
    WRITING
63
 
  };
64
 
  static const uint32_t FLUSH_FREQUENCY_OS= 0; ///< Rely on operating system to sync log file
65
 
  static const uint32_t FLUSH_FREQUENCY_EVERY_WRITE= 1; //< Sync on every write to the log file
66
 
  static const uint32_t FLUSH_FREQUENCY_EVERY_SECOND= 2; ///< Sync no more than once a second
67
 
public:
68
 
  TransactionLog(const std::string in_log_file_path,
69
 
                 uint32_t in_flush_frequency,
70
 
                 bool in_do_checksum);
71
 
 
72
 
  /** Destructor */
73
 
  ~TransactionLog();
74
 
 
75
 
  /**
76
 
   * Returns the current offset into the log
77
 
   */
78
 
  inline off_t getLogOffset()
79
 
  {
80
 
    return log_offset;
81
 
  }
82
 
 
83
 
  /**
84
 
   * Returns the filename of the transaction log
85
 
   */
86
 
  const std::string &getLogFilename();
87
 
 
88
 
  /**
89
 
   * Returns the filename of the transaction log
90
 
   */
91
 
  const std::string &getLogFilepath();
92
 
  
93
 
  /**
94
 
   * Returns the state that the log is in
95
 
   */
96
 
  inline enum Status getState()
97
 
  {
98
 
    return state;
99
 
  }
100
 
 
101
 
  /**
102
 
   * Static helper method which returns the transaction
103
 
   * log entry size in bytes of a given transaction
104
 
   * message.
105
 
   *
106
 
   * @param[in] Transaction message
107
 
   */
108
 
  static size_t getLogEntrySize(const drizzled::message::Transaction &trx);
109
 
 
110
 
  /**
111
 
   * Method which packs into a raw byte buffer
112
 
   * a transaction log entry.  Supplied buffer should
113
 
   * be of adequate size.
114
 
   *
115
 
   * Returns a pointer to the start of the original
116
 
   * buffer.
117
 
   *
118
 
   * @param[in]   Transaction message to pack
119
 
   * @param[in]   Raw byte buffer
120
 
   * @param[out]  Pointer to storage for checksum of message
121
 
   */
122
 
  uint8_t *packTransactionIntoLogEntry(const drizzled::message::Transaction &trx,
123
 
                                       uint8_t *buffer,
124
 
                                       uint32_t *checksum_out);
125
 
 
126
 
  /**
127
 
   * Writes a chunk of data to the log file of a specified
128
 
   * length and returns the offset at which the chunk of
129
 
   * data was written.
130
 
   *
131
 
   * @param[in] Bytes to write
132
 
   * @param[in[ Length of bytes to write
133
 
   *
134
 
   * @retval
135
 
   *  Returns the write offset if the write succeeded, OFF_T_MAX otherwise.
136
 
   */
137
 
  off_t writeEntry(const uint8_t *data, size_t data_length);
138
 
 
139
 
  /**
140
 
   * Truncates the existing log file
141
 
   *
142
 
   * @note 
143
 
   *
144
 
   * This is only called currently during debugging and testing of the 
145
 
   * command log...when the global command_log_truncate variable is 
146
 
   * set to anything other than false, this is called.
147
 
   */
148
 
  void truncate();
149
 
 
150
 
  /**
151
 
   * Takes a global transaction ID and a reference to a string to fill
152
 
   * with the name of the log file which contains the command with the 
153
 
   * transaction ID.  If the transaction ID is contained in a log file, 
154
 
   * the function returns true, false otherwise.
155
 
   *
156
 
   * @param[in] Global transaction ID to search on
157
 
   * @param[inout] String to fill with name of logfile containing command with
158
 
   *               the needed transaction ID
159
 
   *
160
 
   * @retval
161
 
   *  true if found
162
 
   * @retval
163
 
   *  false otherwise
164
 
   */
165
 
  bool findLogFilenameContainingTransactionId(const drizzled::ReplicationServices::GlobalTransactionId &to_find,
166
 
                                              std::string &out_filename) const;
167
 
 
168
 
  /**
169
 
   * Returns whether the log is currently in error.
170
 
   */
171
 
  bool hasError() const;
172
 
 
173
 
  /**
174
 
   * Returns the log's current error message
175
 
   */
176
 
  const std::string &getErrorMessage() const;
177
 
private:
178
 
  static const uint32_t HEADER_TRAILER_BYTES= sizeof(uint32_t) + /* 4-byte msg type header */
179
 
                                              sizeof(uint32_t) + /* 4-byte length header */
180
 
                                              sizeof(uint32_t); /* 4 byte checksum trailer */
181
 
 
182
 
  /* Don't allows these */
183
 
  TransactionLog();
184
 
  TransactionLog(const TransactionLog &other);
185
 
  TransactionLog &operator=(const TransactionLog &other);
186
 
  /**
187
 
   * Clears the current error message
188
 
   */
189
 
  void clearError();
190
 
  /**
191
 
   * Helper method which synchronizes/flushes the transaction log file
192
 
   * according to the transaction_log_flush_frequency system variable
193
 
   *
194
 
   * @retval
195
 
   *   0 == Success
196
 
   * @retval
197
 
   *   >0 == Failure. Error code.
198
 
   */
199
 
  int syncLogFile();
200
 
 
201
 
  int log_file; ///< Handle for our log file
202
 
  Status state; ///< The state the log is in
203
 
  const std::string log_file_path; ///< Full path to the log file
204
 
  std::string log_file_name; ///< Name of the log file
205
 
  drizzled::atomic<off_t> log_offset; ///< Offset in log file where log will write next command
206
 
  bool has_error; ///< Is the log in error?
207
 
  std::string error_message; ///< Current error message
208
 
  uint32_t flush_frequency; ///< Determines behaviour of syncing log file
209
 
  time_t last_sync_time; ///< Last time the log file was synced (only set in FLUSH_FREQUENCY_EVERY_SECOND)
210
 
  bool do_checksum; ///< Do a CRC32 checksum when writing Transaction message to log?
211
 
};
212
 
 
213
 
#endif /* PLUGIN_TRANSACTION_LOG_TRANSACTION_LOG_H */