1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
*
* Copyright (C) 2008-2009 Sun Microsystems, Inc.
* Copyright (C) 2010 Jay Pipes <jaypipes@gmail.com>
*
* Authors:
*
* Jay Pipes <jaypipes@gmail.com.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
*
* Defines the implementation of the transaction log file descriptor.
*
* @details
*
* Currently, the transaction log file uses a simple, single-file, append-only
* format.
*
* We have an atomic off_t called log_offset which keeps track of the
* offset into the log file for writing the next log entry. The log
* entries are written, one after the other, in the following way:
*
* <pre>
* --------------------------------------
* |<- 4 bytes ->|<- # Bytes of Entry ->|
* --------------------------------------
* | Entry Type | Serialized Entry |
* --------------------------------------
* </pre>
*
* The Entry Type is an integer defined as an enumeration in the
* /drizzled/message/transaction.proto file called TransactionLogEntry::Type.
*
* Each transaction log entry type is written to the log differently. Here,
* we cover the format of each log entry type.
*
* Committed and Prepared Transaction Log Entries
* -----------------------------------------------
*
* <pre>
* ------------------------------------------------------------------
* |<- 4 bytes ->|<- # Bytes of Transaction Message ->|<- 4 bytes ->|
* ------------------------------------------------------------------
* | Length | Serialized Transaction Message | Checksum |
* ------------------------------------------------------------------
* </pre>
*
* @todo
*
* Possibly look at a scoreboard approach with multiple file segments. For
* right now, though, this is just a quick simple implementation to serve
* as a skeleton and a springboard.
*/
#include <config.h>
#include "transaction_log.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <vector>
#include <string>
#include <drizzled/internal/my_sys.h> /* for internal::my_sync */
#include <drizzled/errmsg_print.h>
#include <drizzled/gettext.h>
#include <drizzled/message/transaction.pb.h>
#include <drizzled/transaction_services.h>
#include <drizzled/algorithm/crc32.h>
#include <google/protobuf/io/coded_stream.h>
using namespace std;
using namespace drizzled;
using namespace google;
TransactionLog *transaction_log= NULL; /* The singleton transaction log */
TransactionLog::TransactionLog(const string in_log_file_path,
uint32_t in_flush_frequency,
bool in_do_checksum) :
state(OFFLINE),
log_file_path(in_log_file_path),
has_error(false),
error_message(),
flush_frequency(in_flush_frequency),
do_checksum(in_do_checksum)
{
/* Setup our log file and determine the next write offset... */
log_file= open(log_file_path.c_str(), O_APPEND|O_CREAT|O_SYNC|O_WRONLY, S_IRWXU);
if (log_file == -1)
{
char errmsg[STRERROR_MAX];
strerror_r(errno, errmsg, sizeof(errmsg));
error_message.assign(_("Failed to open transaction log file "));
error_message.append(log_file_path);
error_message.append(" Got error: ");
error_message.append(errmsg);
error_message.push_back('\n');
has_error= true;
return;
}
/* For convenience, grab the log file name from the path */
if (log_file_path.find_first_of('/') != string::npos)
{
/* Strip to last / */
string tmp;
tmp= log_file_path.substr(log_file_path.find_last_of('/') + 1);
log_file_name.assign(tmp);
}
else
log_file_name.assign(log_file_path);
/*
* The offset of the next write is the current position of the log
* file, since it's opened in append mode...
*/
log_offset= lseek(log_file, 0, SEEK_END);
state= ONLINE;
}
uint8_t *TransactionLog::packTransactionIntoLogEntry(const message::Transaction &trx,
uint8_t *buffer,
uint32_t *checksum_out)
{
uint8_t *orig_buffer= buffer;
size_t message_byte_length= trx.ByteSize();
/*
* Write the header information, which is the message type and
* the length of the transaction message into the buffer
*/
buffer= protobuf::io::CodedOutputStream::WriteLittleEndian32ToArray(
static_cast<uint32_t>(ReplicationServices::TRANSACTION), buffer);
buffer= protobuf::io::CodedOutputStream::WriteLittleEndian32ToArray(
static_cast<uint32_t>(message_byte_length), buffer);
/*
* Now write the serialized transaction message, followed
* by the optional checksum into the buffer.
*/
buffer= trx.SerializeWithCachedSizesToArray(buffer);
if (do_checksum)
{
*checksum_out= drizzled::algorithm::crc32(
reinterpret_cast<char *>(buffer) - message_byte_length, message_byte_length);
}
else
*checksum_out= 0;
/* We always write in network byte order */
buffer= protobuf::io::CodedOutputStream::WriteLittleEndian32ToArray(*checksum_out, buffer);
/* Reset the pointer back to its original location... */
buffer= orig_buffer;
return orig_buffer;
}
off_t TransactionLog::writeEntry(const uint8_t *data, size_t data_length)
{
ssize_t written= 0;
/*
* Do an atomic increment on the offset of the log file position
*/
off_t cur_offset= log_offset.fetch_and_add(static_cast<off_t>(data_length));
/*
* Quick safety...if an error occurs above in another writer, the log
* file will be in a crashed state.
*/
if (unlikely(state == CRASHED))
{
/*
* Reset the log's offset in case we want to produce a decent error message including
* the original offset where an error occurred.
*/
log_offset= cur_offset;
return log_offset;
}
/* Write the full buffer in one swoop */
do
{
written= pwrite(log_file, data, data_length, cur_offset);
}
while (written == -1 && errno == EINTR); /* Just retry the write when interrupted by a signal... */
if (unlikely(written != static_cast<ssize_t>(data_length)))
{
char errmsg[STRERROR_MAX];
strerror_r(errno, errmsg, sizeof(errmsg));
errmsg_printf(error::ERROR,
_("Failed to write full size of log entry. Tried to write %" PRId64
" bytes at offset %" PRId64 ", but only wrote %" PRId32 " bytes. Error: %s\n"),
static_cast<int64_t>(data_length),
static_cast<int64_t>(cur_offset),
static_cast<int32_t>(written),
errmsg);
state= CRASHED;
/*
* Reset the log's offset in case we want to produce a decent error message including
* the original offset where an error occurred.
*/
log_offset= cur_offset;
}
int error_code= syncLogFile();
if (unlikely(error_code != 0))
{
sql_perror(_("Failed to sync log file."));
}
return cur_offset;
}
int TransactionLog::syncLogFile()
{
switch (flush_frequency)
{
case FLUSH_FREQUENCY_EVERY_WRITE:
return internal::my_sync(log_file, 0);
case FLUSH_FREQUENCY_EVERY_SECOND:
{
time_t now_time= time(NULL);
if (last_sync_time <= (now_time - 1))
{
last_sync_time= now_time;
return internal::my_sync(log_file, 0);
}
return 0;
}
case FLUSH_FREQUENCY_OS:
default:
return 0;
}
}
const string &TransactionLog::getLogFilename()
{
return log_file_name;
}
const string &TransactionLog::getLogFilepath()
{
return log_file_path;
}
void TransactionLog::truncate()
{
/*
* @note
*
* This is NOT THREAD SAFE! DEBUG/TEST code only!
*/
log_offset= (off_t) 0;
int result;
do
{
result= ftruncate(log_file, log_offset);
}
while (result == -1 && errno == EINTR);
}
bool TransactionLog::findLogFilenameContainingTransactionId(const ReplicationServices::GlobalTransactionId&,
string &out_filename) const
{
/*
* Currently, we simply return the single logfile name
* Eventually, we'll have an index/hash with upper and
* lower bounds to look up a log file with a transaction id
*/
out_filename.assign(log_file_path);
return true;
}
bool TransactionLog::hasError() const
{
return has_error;
}
void TransactionLog::clearError()
{
has_error= false;
error_message.clear();
}
const string &TransactionLog::getErrorMessage() const
{
return error_message;
}
size_t TransactionLog::getLogEntrySize(const message::Transaction &trx)
{
return trx.ByteSize() + HEADER_TRAILER_BYTES;
}
|