24
24
#include "config.h"
25
#include <drizzled/definitions.h>
26
#include <drizzled/gettext.h>
27
#include <drizzled/replication_services.h>
28
#include <drizzled/algorithm/crc32.h>
29
25
#include <sys/types.h>
30
26
#include <sys/stat.h>
32
28
#include <limits.h>
35
30
#include <iostream>
37
32
#include <algorithm>
39
34
#include <unistd.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>
35
#include "drizzled/definitions.h"
36
#include "drizzled/gettext.h"
37
#include "drizzled/replication_services.h"
38
#include "drizzled/algorithm/crc32.h"
39
#include "drizzled/message/transaction.pb.h"
40
#include "drizzled/message/statement_transform.h"
41
#include "drizzled/message/transaction_manager.h"
42
#include "drizzled/util/convert.h"
45
44
#include <google/protobuf/io/coded_stream.h>
46
45
#include <google/protobuf/io/zero_copy_stream_impl.h>
248
260
("help", N_("Display help and exit"))
249
261
("checksum", N_("Perform checksum"))
250
262
("ignore-events", N_("Ignore event messages"))
251
("input-file", po::value< vector<string> >(), N_("Transaction log file"));
263
("input-file", po::value< vector<string> >(), N_("Transaction log file"))
264
("raw", N_("Print raw Protobuf messages instead of SQL"))
266
po::value<int>(&opt_start_pos),
267
N_("Start reading from the given file position"))
269
po::value<uint64_t>(&opt_transaction_id),
270
N_("Only output for the given transaction ID"));
254
273
* We allow one positional argument that will be transaction file name
271
290
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"));
292
cerr << desc << endl;
297
* Specifying both a transaction ID and a start position
300
if (vm.count("start-pos") && vm.count("transaction-id"))
302
cerr << _("Cannot use --start-pos and --transaction-id together\n");
282
306
bool do_checksum= vm.count("checksum") ? true : false;
283
307
bool ignore_events= vm.count("ignore-events") ? true : false;
308
bool print_as_raw= vm.count("raw") ? true : false;
285
310
string filename= vm["input-file"].as< vector<string> >()[0];
286
file= open(filename.c_str(), O_RDONLY);
311
int file= open(filename.c_str(), O_RDONLY);
289
fprintf(stderr, _("Cannot open file: %s\n"), filename.c_str());
314
cerr << _("Cannot open file: ") << filename << endl;
304
339
bool result= true;
305
340
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)
342
while (result == true)
345
* Odd thing to note about using CodedInputStream: This class wasn't
346
* intended to read large amounts of GPB messages. It has an upper
347
* limit on the number of bytes it will read (see Protobuf docs for
348
* this class for more info). A warning will be produced as you
349
* get close to this limit. Since this is a pretty lightweight class,
350
* we should be able to simply create a new one for each message we
353
protobuf::io::CodedInputStream coded_input(raw_input);
355
/* Read in the type and length of the command */
356
if (not coded_input.ReadLittleEndian32(&message_type) ||
357
not coded_input.ReadLittleEndian32(&length))
312
362
if (message_type != ReplicationServices::TRANSACTION)
314
fprintf(stderr, _("Found a non-transaction message in log. Currently, not supported.\n"));
364
cerr << _("Found a non-transaction message in log. Currently, not supported.\n");
318
368
if (length > INT_MAX)
320
fprintf(stderr, _("Attempted to read record bigger than INT_MAX\n"));
370
cerr << _("Attempted to read record bigger than INT_MAX\n");
324
374
if (buffer == NULL)
327
377
* First time around...just malloc the length. This block gets rid
328
378
* of a GCC warning about uninitialized temp_buffer.
338
388
if (temp_buffer == NULL)
340
fprintf(stderr, _("Memory allocation failure trying to allocate %" PRIu64 " bytes.\n"),
341
static_cast<uint64_t>(length));
390
cerr << _("Memory allocation failure trying to allocate ") << length << _(" bytes\n");
345
394
buffer= temp_buffer;
347
396
/* Read the Command */
348
result= coded_input->ReadRaw(buffer, (int) length);
397
result= coded_input.ReadRaw(buffer, (int) length);
349
398
if (result == false)
351
400
char errmsg[STRERROR_MAX];
352
401
strerror_r(errno, errmsg, sizeof(errmsg));
353
fprintf(stderr, _("Could not read transaction message.\n"));
354
fprintf(stderr, _("GPB ERROR: %s.\n"), errmsg);
402
cerr << _("Could not read transaction message.\n");
403
cerr << _("GPB ERROR: ") << errmsg << endl;;
356
405
hexdump.reserve(length * 4);
357
406
bytesToHexdumpFormat(hexdump, reinterpret_cast<const unsigned char *>(buffer), length);
358
fprintf(stderr, _("HEXDUMP:\n\n%s\n"), hexdump.c_str());
407
cerr << _("HEXDUMP:\n\n") << hexdump << endl;
362
411
result= transaction.ParseFromArray(buffer, static_cast<int32_t>(length));
363
412
if (result == false)
365
fprintf(stderr, _("Unable to parse command. Got error: %s.\n"), transaction.InitializationErrorString().c_str());
414
cerr << _("Unable to parse command. Got error: ")
415
<< transaction.InitializationErrorString() << endl;
366
416
if (buffer != NULL)
369
419
hexdump.reserve(length * 4);
370
420
bytesToHexdumpFormat(hexdump, reinterpret_cast<const unsigned char *>(buffer), length);
371
fprintf(stderr, _("HEXDUMP:\n\n%s\n"), hexdump.c_str());
421
cerr << _("HEXDUMP:\n\n") << hexdump << endl;
376
if (not isEndTransaction(transaction))
426
const message::TransactionContext trx= transaction.transaction_context();
427
uint64_t transaction_id= trx.transaction_id();
430
* If we are given a transaction ID, we only look for that one and
433
if (vm.count("transaction-id"))
378
trx_mgr.store(transaction);
435
if (opt_transaction_id == transaction_id)
437
printTransaction(transaction, ignore_events, print_as_raw);
441
/* Need to get the checksum bytes out of stream */
442
coded_input.ReadLittleEndian32(&checksum);
443
previous_length = length;
449
* No transaction ID given, so process all messages.
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))
453
if (not isEndTransaction(transaction))
391
455
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);
460
* If there are any previous Transaction messages for this transaction,
461
* store this one, then output all of them together.
463
if (trx_mgr.contains(transaction_id))
465
trx_mgr.store(transaction);
467
uint32_t size= trx_mgr.getTransactionBufferSize(transaction_id);
472
message::Transaction new_trx;
473
trx_mgr.getTransactionMessage(new_trx, transaction_id, idx);
474
printTransaction(new_trx, ignore_events, print_as_raw);
478
/* No longer need this transaction */
479
trx_mgr.remove(transaction_id);
483
printTransaction(transaction, ignore_events, print_as_raw);
486
} /* end ! vm.count("transaction-id") */
413
488
/* Skip 4 byte checksum */
414
coded_input->ReadLittleEndian32(&checksum);
489
coded_input.ReadLittleEndian32(&checksum);
418
493
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)));
495
cerr << _("Checksum failed. Wanted ")
498
<< drizzled::algorithm::crc32(buffer, static_cast<size_t>(length))