1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2009 Sun Microsystems
8
* Jay Pipes <joinfu@sun.com>
10
* This program is free software; you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation; version 2 of the License.
14
* This program is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
19
* You should have received a copy of the GNU General Public License
20
* along with this program; if not, write to the Free Software
21
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
#include <drizzled/definitions.h>
26
#include <drizzled/gettext.h>
27
#include <drizzled/replication_services.h>
28
#include <drizzled/algorithm/crc32.h>
29
#include <sys/types.h>
40
#include <drizzled/message/transaction.pb.h>
41
#include <drizzled/message/statement_transform.h>
42
#include <drizzled/message/transaction_manager.h>
43
#include <drizzled/util/convert.h>
45
#include <google/protobuf/io/coded_stream.h>
46
#include <google/protobuf/io/zero_copy_stream_impl.h>
48
#include <boost/program_options.hpp>
51
using namespace google;
52
using namespace drizzled;
54
namespace po= boost::program_options;
56
static const char *replace_with_spaces= "\n\r";
58
static void printStatement(const message::Statement &statement)
60
vector<string> sql_strings;
62
message::transformStatementToSql(statement,
65
true /* already in transaction */);
67
for (vector<string>::iterator sql_string_iter= sql_strings.begin();
68
sql_string_iter != sql_strings.end();
71
string &sql= *sql_string_iter;
74
* Replace \n and \r with spaces so that SQL statements
75
* are always on a single line
78
string::size_type found= sql.find_first_of(replace_with_spaces);
79
while (found != string::npos)
82
found= sql.find_first_of(replace_with_spaces, found);
87
* Embedded NUL characters are a pain in the ass.
90
string::size_type found= sql.find_first_of('\0');
91
while (found != string::npos)
94
sql.insert(found + 1, 1, '0');
95
found= sql.find_first_of('\0', found);
99
cout << sql << ';' << endl;
103
static bool isEndStatement(const message::Statement &statement)
105
switch (statement.type())
107
case (message::Statement::INSERT):
109
const message::InsertData &data= statement.insert_data();
110
if (not data.end_segment())
114
case (message::Statement::UPDATE):
116
const message::UpdateData &data= statement.update_data();
117
if (not data.end_segment())
121
case (message::Statement::DELETE):
123
const message::DeleteData &data= statement.delete_data();
124
if (not data.end_segment())
134
static bool isEndTransaction(const message::Transaction &transaction)
136
const message::TransactionContext trx= transaction.transaction_context();
138
size_t num_statements= transaction.statement_size();
141
* If any Statement is partial, then we can expect another Transaction
144
for (size_t x= 0; x < num_statements; ++x)
146
const message::Statement &statement= transaction.statement(x);
148
if (not isEndStatement(statement))
155
static void printEvent(const message::Event &event)
157
switch (event.type())
159
case message::Event::STARTUP:
161
cout << "-- EVENT: Server startup\n";
164
case message::Event::SHUTDOWN:
166
cout << "-- EVENT: Server shutdown\n";
171
cout << "-- EVENT: Unknown event\n";
177
static void printTransaction(const message::Transaction &transaction,
180
static uint64_t last_trx_id= 0;
181
bool should_commit= true;
182
const message::TransactionContext trx= transaction.transaction_context();
185
* First check to see if this is an event message.
187
if (transaction.has_event())
189
last_trx_id= trx.transaction_id();
190
if (not ignore_events)
191
printEvent(transaction.event());
195
size_t num_statements= transaction.statement_size();
199
* One way to determine when a new transaction begins is when the
200
* transaction id changes (if all transactions have their GPB messages
201
* grouped together, which this program will). We check that here.
203
if (trx.transaction_id() != last_trx_id)
204
cout << "START TRANSACTION;" << endl;
206
last_trx_id= trx.transaction_id();
208
for (x= 0; x < num_statements; ++x)
210
const message::Statement &statement= transaction.statement(x);
213
should_commit= isEndStatement(statement);
215
/* A ROLLBACK would be the only Statement within the Transaction
216
* since all other Statements will have been deleted from the
217
* Transaction message, so we should fall out of this loop immediately.
218
* We don't want to issue an unnecessary COMMIT, so we change
219
* should_commit to false here.
221
if (statement.type() == message::Statement::ROLLBACK)
222
should_commit= false;
224
printStatement(statement);
228
* If ALL Statements are end segments, we can commit this Transaction.
229
* We can also check to see if the transaction_id changed, but this
230
* wouldn't work for the last Transaction in the transaction log since
231
* we don't have another Transaction to compare to. Checking for all
232
* end segments (like we do above) covers this case.
235
cout << "COMMIT;" << endl;
238
int main(int argc, char* argv[])
240
GOOGLE_PROTOBUF_VERIFY_VERSION;
244
* Setup program options
246
po::options_description desc("Program options");
248
("help", N_("Display help and exit"))
249
("checksum", N_("Perform checksum"))
250
("ignore-events", N_("Ignore event messages"))
251
("input-file", po::value< vector<string> >(), N_("Transaction log file"));
254
* We allow one positional argument that will be transaction file name
256
po::positional_options_description pos;
257
pos.add("input-file", 1);
260
* Parse the program options
262
po::variables_map vm;
263
po::store(po::command_line_parser(argc, argv).
264
options(desc).positional(pos).run(), vm);
268
* If the help option was given, or not input file was supplied,
269
* print out usage information.
271
if (vm.count("help") || not vm.count("input-file"))
273
fprintf(stderr, _("Usage: %s [options] TRANSACTION_LOG \n"),
275
fprintf(stderr, _("OPTIONS:\n"));
276
fprintf(stderr, _("--help : Display help and exit\n"));
277
fprintf(stderr, _("--checksum : Perform checksum\n"));
278
fprintf(stderr, _("--ignore-events : Ignore event messages\n"));
282
bool do_checksum= vm.count("checksum") ? true : false;
283
bool ignore_events= vm.count("ignore-events") ? true : false;
285
string filename= vm["input-file"].as< vector<string> >()[0];
286
file= open(filename.c_str(), O_RDONLY);
289
fprintf(stderr, _("Cannot open file: %s\n"), filename.c_str());
293
message::Transaction transaction;
294
message::TransactionManager trx_mgr;
296
protobuf::io::ZeroCopyInputStream *raw_input= new protobuf::io::FileInputStream(file);
297
protobuf::io::CodedInputStream *coded_input= new protobuf::io::CodedInputStream(raw_input);
300
char *temp_buffer= NULL;
302
uint32_t previous_length= 0;
303
uint32_t checksum= 0;
305
uint32_t message_type= 0;
307
/* Read in the length of the command */
308
while (result == true &&
309
coded_input->ReadLittleEndian32(&message_type) == true &&
310
coded_input->ReadLittleEndian32(&length) == true)
312
if (message_type != ReplicationServices::TRANSACTION)
314
fprintf(stderr, _("Found a non-transaction message in log. Currently, not supported.\n"));
318
if (length > INT_MAX)
320
fprintf(stderr, _("Attempted to read record bigger than INT_MAX\n"));
327
* First time around...just malloc the length. This block gets rid
328
* of a GCC warning about uninitialized temp_buffer.
330
temp_buffer= (char *) malloc(static_cast<size_t>(length));
332
/* No need to allocate if we have a buffer big enough... */
333
else if (length > previous_length)
335
temp_buffer= (char *) realloc(buffer, static_cast<size_t>(length));
338
if (temp_buffer == NULL)
340
fprintf(stderr, _("Memory allocation failure trying to allocate %" PRIu64 " bytes.\n"),
341
static_cast<uint64_t>(length));
347
/* Read the Command */
348
result= coded_input->ReadRaw(buffer, (int) length);
351
char errmsg[STRERROR_MAX];
352
strerror_r(errno, errmsg, sizeof(errmsg));
353
fprintf(stderr, _("Could not read transaction message.\n"));
354
fprintf(stderr, _("GPB ERROR: %s.\n"), errmsg);
356
hexdump.reserve(length * 4);
357
bytesToHexdumpFormat(hexdump, reinterpret_cast<const unsigned char *>(buffer), length);
358
fprintf(stderr, _("HEXDUMP:\n\n%s\n"), hexdump.c_str());
362
result= transaction.ParseFromArray(buffer, static_cast<int32_t>(length));
365
fprintf(stderr, _("Unable to parse command. Got error: %s.\n"), transaction.InitializationErrorString().c_str());
369
hexdump.reserve(length * 4);
370
bytesToHexdumpFormat(hexdump, reinterpret_cast<const unsigned char *>(buffer), length);
371
fprintf(stderr, _("HEXDUMP:\n\n%s\n"), hexdump.c_str());
376
if (not isEndTransaction(transaction))
378
trx_mgr.store(transaction);
382
const message::TransactionContext trx= transaction.transaction_context();
383
uint64_t transaction_id= trx.transaction_id();
386
* If there are any previous Transaction messages for this transaction,
387
* store this one, then output all of them together.
389
if (trx_mgr.contains(transaction_id))
391
trx_mgr.store(transaction);
393
uint32_t size= trx_mgr.getTransactionBufferSize(transaction_id);
398
message::Transaction new_trx;
399
trx_mgr.getTransactionMessage(new_trx, transaction_id, idx);
400
printTransaction(new_trx, ignore_events);
404
/* No longer need this transaction */
405
trx_mgr.remove(transaction_id);
409
printTransaction(transaction, ignore_events);
413
/* Skip 4 byte checksum */
414
coded_input->ReadLittleEndian32(&checksum);
418
if (checksum != drizzled::algorithm::crc32(buffer, static_cast<size_t>(length)))
420
fprintf(stderr, _("Checksum failed. Wanted %" PRIu32 " got %" PRIu32 "\n"), checksum, drizzled::algorithm::crc32(buffer, static_cast<size_t>(length)));
424
previous_length= length;
433
return (result == true ? 0 : 1);