1
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008-2009 Sun Microsystems, Inc.
5
* Copyright (C) 2010 Jay Pipes <jaypipes@gmail.com>
4
* Copyright (C) 2008-2009 Sun Microsystems
9
* Jay Pipes <jaypipes@gmail.com.com>
8
* Jay Pipes <joinfu@sun.com>
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
29
* Defines the API of the transaction log file descriptor.
28
* Defines the API of the default transaction log.
30
* @see drizzled/plugin/replicator.h
31
* @see drizzled/plugin/applier.h
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.
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.
38
42
#ifndef PLUGIN_TRANSACTION_LOG_TRANSACTION_LOG_H
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>
44
50
#include "transaction_log_entry.h"
55
class TransactionLog: public drizzled::plugin::TransactionApplier
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 */
52
62
typedef std::vector<TransactionLogEntry> Entries;
53
63
typedef std::vector<TransactionLogTransactionEntry> TransactionEntries;
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
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);
86
* Applies a Transaction to the serial log
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
98
* @param Transaction message to be replicated
100
void apply(const drizzled::message::Transaction &to_apply);
76
103
* Returns the current offset into the log
78
105
inline off_t getLogOffset()
102
* Static helper method which returns the transaction
103
* log entry size in bytes of a given transaction
106
* @param[in] Transaction message
108
static size_t getLogEntrySize(const drizzled::message::Transaction &trx);
111
* Method which packs into a raw byte buffer
112
* a transaction log entry. Supplied buffer should
113
* be of adequate size.
115
* Returns a pointer to the start of the original
118
* @param[in] Transaction message to pack
119
* @param[in] Raw byte buffer
120
* @param[out] Pointer to storage for checksum of message
122
uint8_t *packTransactionIntoLogEntry(const drizzled::message::Transaction &trx,
124
uint32_t *checksum_out);
127
* Writes a chunk of data to the log file of a specified
128
* length and returns the offset at which the chunk of
131
* @param[in] Bytes to write
132
* @param[in[ Length of bytes to write
135
* Returns the write offset if the write succeeded, OFF_T_MAX otherwise.
137
off_t writeEntry(const uint8_t *data, size_t data_length);
140
129
* Truncates the existing log file
176
165
const std::string &getErrorMessage() const;
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 */
182
167
/* Don't allows these */
183
168
TransactionLog();
184
169
TransactionLog(const TransactionLog &other);
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)
213
197
#endif /* PLUGIN_TRANSACTION_LOG_TRANSACTION_LOG_H */