~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/transaction_log/transaction_log.h

Merge Stewart.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008-2009 Sun Microsystems, Inc.
5
 
 *  Copyright (C) 2010 Jay Pipes <jaypipes@gmail.com>
 
4
 *  Copyright (C) 2008-2009 Sun Microsystems
6
5
 *
7
6
 *  Authors:
8
7
 *
9
 
 *  Jay Pipes <jaypipes@gmail.com.com>
 
8
 *  Jay Pipes <joinfu@sun.com>
10
9
 *
11
10
 *  This program is free software; you can redistribute it and/or modify
12
11
 *  it under the terms of the GNU General Public License as published by
26
25
/**
27
26
 * @file
28
27
 *
29
 
 * Defines the API of the transaction log file descriptor.
 
28
 * Defines the API of the default transaction log.
 
29
 *
 
30
 * @see drizzled/plugin/replicator.h
 
31
 * @see drizzled/plugin/applier.h
30
32
 *
31
33
 * @details
32
34
 *
33
 
 * Basically, the TransactionLog is a descriptor for a log
34
 
 * file containing transaction messages that is written by
35
 
 * the TransactionLogApplier plugin(s).
 
35
 * The TransactionLog applies events it receives from the ReplicationServices
 
36
 * server component to a simple log file on disk.
 
37
 * 
 
38
 * Transactions are received in no guaranteed order and the command log
 
39
 * is in charge of writing these events to the log as they are received.
36
40
 */
37
41
 
38
42
#ifndef PLUGIN_TRANSACTION_LOG_TRANSACTION_LOG_H
40
44
 
41
45
#include <drizzled/atomics.h>
42
46
#include <drizzled/replication_services.h>
 
47
#include <drizzled/plugin/transaction_replicator.h>
 
48
#include <drizzled/plugin/transaction_applier.h>
43
49
 
44
50
#include "transaction_log_entry.h"
45
51
 
46
52
#include <vector>
47
53
#include <string>
48
54
 
49
 
class TransactionLog
 
55
class TransactionLog: public drizzled::plugin::TransactionApplier 
50
56
{
51
57
public:
 
58
  static const uint32_t HEADER_TRAILER_BYTES= sizeof(uint32_t) + /* 4-byte msg type header */
 
59
                                              sizeof(uint32_t) + /* 4-byte length header */
 
60
                                              sizeof(uint32_t); /* 4 byte checksum trailer */
 
61
 
52
62
  typedef std::vector<TransactionLogEntry> Entries;
53
63
  typedef std::vector<TransactionLogTransactionEntry> TransactionEntries;
54
64
  /**
61
71
    ONLINE,
62
72
    WRITING
63
73
  };
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
 
74
  static const uint32_t SYNC_METHOD_OS= 0; ///< Rely on operating system to sync log file
 
75
  static const uint32_t SYNC_METHOD_EVERY_WRITE= 1; //< Sync on every write to the log file
 
76
  static const uint32_t SYNC_METHOD_EVERY_SECOND= 2; ///< Sync no more than once a second
67
77
public:
68
 
  TransactionLog(const std::string in_log_file_path,
69
 
                 uint32_t in_flush_frequency,
 
78
  TransactionLog(std::string name_arg,
 
79
                 const std::string &in_log_file_path,
70
80
                 bool in_do_checksum);
71
81
 
72
82
  /** Destructor */
73
83
  ~TransactionLog();
74
84
 
75
85
  /**
 
86
   * Applies a Transaction to the serial log
 
87
   *
 
88
   * @note
 
89
   *
 
90
   * It is important to note that memory allocation for the 
 
91
   * supplied pointer is not guaranteed after the completion 
 
92
   * of this function -- meaning the caller can dispose of the
 
93
   * supplied message.  Therefore, appliers which are
 
94
   * implementing an asynchronous replication system must copy
 
95
   * the supplied message to their own controlled memory storage
 
96
   * area.
 
97
   *
 
98
   * @param Transaction message to be replicated
 
99
   */
 
100
  void apply(const drizzled::message::Transaction &to_apply);
 
101
 
 
102
  /**
76
103
   * Returns the current offset into the log
77
104
   */
78
105
  inline off_t getLogOffset()
99
126
  }
100
127
 
101
128
  /**
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
129
   * Truncates the existing log file
141
130
   *
142
131
   * @note 
175
164
   */
176
165
  const std::string &getErrorMessage() const;
177
166
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
167
  /* Don't allows these */
183
168
  TransactionLog();
184
169
  TransactionLog(const TransactionLog &other);
189
174
  void clearError();
190
175
  /**
191
176
   * Helper method which synchronizes/flushes the transaction log file
192
 
   * according to the transaction_log_flush_frequency system variable
 
177
   * according to the transaction_log_sync_method system variable
193
178
   *
194
179
   * @retval
195
180
   *   0 == Success
200
185
 
201
186
  int log_file; ///< Handle for our log file
202
187
  Status state; ///< The state the log is in
 
188
  drizzled::atomic<bool> do_checksum; ///< Do a CRC32 checksum when writing Transaction message to log?
203
189
  const std::string log_file_path; ///< Full path to the log file
204
190
  std::string log_file_name; ///< Name of the log file
205
191
  drizzled::atomic<off_t> log_offset; ///< Offset in log file where log will write next command
206
192
  bool has_error; ///< Is the log in error?
207
193
  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?
 
194
  time_t last_sync_time; ///< Last time the log file was synced (only set in SYNC_METHOD_EVERY_SECOND)
211
195
};
212
196
 
213
197
#endif /* PLUGIN_TRANSACTION_LOG_TRANSACTION_LOG_H */