~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/transaction_log/transaction_log.h

code clean move Item_func_abs, Item_func_int_exp, Item_func_ln, Item_func_log to functions directory

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
 
 * 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
 
  static const uint32_t HEADER_TRAILER_BYTES= sizeof(uint32_t) + /* 4-byte msg type header */
53
 
                                              sizeof(uint32_t) + /* 4-byte length header */
54
 
                                              sizeof(uint32_t); /* 4 byte checksum trailer */
55
 
 
56
 
  typedef std::vector<TransactionLogEntry> Entries;
57
 
  typedef std::vector<TransactionLogTransactionEntry> TransactionEntries;
58
 
  /**
59
 
   * The state the log is in
60
 
   */
61
 
  enum Status
62
 
  {
63
 
    CRASHED= 0,
64
 
    OFFLINE, /* Default state, uninited. */
65
 
    ONLINE,
66
 
    WRITING
67
 
  };
68
 
  static const uint32_t SYNC_METHOD_OS= 0; ///< Rely on operating system to sync log file
69
 
  static const uint32_t SYNC_METHOD_EVERY_WRITE= 1; //< Sync on every write to the log file
70
 
  static const uint32_t SYNC_METHOD_EVERY_SECOND= 2; ///< Sync no more than once a second
71
 
public:
72
 
  TransactionLog(const std::string in_log_file_path,
73
 
                 uint32_t in_sync_method);
74
 
 
75
 
  /** Destructor */
76
 
  ~TransactionLog();
77
 
 
78
 
  /**
79
 
   * Returns the current offset into the log
80
 
   */
81
 
  inline off_t getLogOffset()
82
 
  {
83
 
    return log_offset;
84
 
  }
85
 
 
86
 
  /**
87
 
   * Returns the filename of the transaction log
88
 
   */
89
 
  const std::string &getLogFilename();
90
 
 
91
 
  /**
92
 
   * Returns the filename of the transaction log
93
 
   */
94
 
  const std::string &getLogFilepath();
95
 
  
96
 
  /**
97
 
   * Returns the state that the log is in
98
 
   */
99
 
  inline enum Status getState()
100
 
  {
101
 
    return state;
102
 
  }
103
 
 
104
 
  /**
105
 
   * Writes a chunk of data to the log file of a specified
106
 
   * length and returns the offset at which the chunk of
107
 
   * data was written.
108
 
   *
109
 
   * @param[in] Bytes to write
110
 
   * @param[in[ Length of bytes to write
111
 
   *
112
 
   * @retval
113
 
   *  Returns the write offset if the write succeeded, OFF_T_MAX otherwise.
114
 
   */
115
 
  off_t writeEntry(const uint8_t *data, size_t data_length);
116
 
 
117
 
  /**
118
 
   * Truncates the existing log file
119
 
   *
120
 
   * @note 
121
 
   *
122
 
   * This is only called currently during debugging and testing of the 
123
 
   * command log...when the global command_log_truncate variable is 
124
 
   * set to anything other than false, this is called.
125
 
   */
126
 
  void truncate();
127
 
 
128
 
  /**
129
 
   * Takes a global transaction ID and a reference to a string to fill
130
 
   * with the name of the log file which contains the command with the 
131
 
   * transaction ID.  If the transaction ID is contained in a log file, 
132
 
   * the function returns true, false otherwise.
133
 
   *
134
 
   * @param[in] Global transaction ID to search on
135
 
   * @param[inout] String to fill with name of logfile containing command with
136
 
   *               the needed transaction ID
137
 
   *
138
 
   * @retval
139
 
   *  true if found
140
 
   * @retval
141
 
   *  false otherwise
142
 
   */
143
 
  bool findLogFilenameContainingTransactionId(const drizzled::ReplicationServices::GlobalTransactionId &to_find,
144
 
                                              std::string &out_filename) const;
145
 
 
146
 
  /**
147
 
   * Returns whether the log is currently in error.
148
 
   */
149
 
  bool hasError() const;
150
 
 
151
 
  /**
152
 
   * Returns the log's current error message
153
 
   */
154
 
  const std::string &getErrorMessage() const;
155
 
private:
156
 
  /* Don't allows these */
157
 
  TransactionLog();
158
 
  TransactionLog(const TransactionLog &other);
159
 
  TransactionLog &operator=(const TransactionLog &other);
160
 
  /**
161
 
   * Clears the current error message
162
 
   */
163
 
  void clearError();
164
 
  /**
165
 
   * Helper method which synchronizes/flushes the transaction log file
166
 
   * according to the transaction_log_sync_method system variable
167
 
   *
168
 
   * @retval
169
 
   *   0 == Success
170
 
   * @retval
171
 
   *   >0 == Failure. Error code.
172
 
   */
173
 
  int syncLogFile();
174
 
 
175
 
  int log_file; ///< Handle for our log file
176
 
  Status state; ///< The state the log is in
177
 
  const std::string log_file_path; ///< Full path to the log file
178
 
  std::string log_file_name; ///< Name of the log file
179
 
  drizzled::atomic<off_t> log_offset; ///< Offset in log file where log will write next command
180
 
  bool has_error; ///< Is the log in error?
181
 
  std::string error_message; ///< Current error message
182
 
  uint32_t sync_method; ///< Determines behaviour of syncing log file
183
 
  time_t last_sync_time; ///< Last time the log file was synced (only set in SYNC_METHOD_EVERY_SECOND)
184
 
};
185
 
 
186
 
#endif /* PLUGIN_TRANSACTION_LOG_TRANSACTION_LOG_H */