~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/transaction_reader.cc

  • Committer: lbieber
  • Date: 2010-10-06 16:34:16 UTC
  • mfrom: (1816.1.3 build)
  • Revision ID: lbieber@orisndriz08-20101006163416-ea0sl59qgpglk21y
Merge Monty - Change the requirement from either libinnodb to libhaildb. Also, tied it to version 2.2
Merge Andrew - fix bug 650935: remove --compress from all clients
Merge Andrew - fix bug 653471: Add -A to drizzle client
Merge Travis - 621861 = To change C structs to C++ classes in Drizzle

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2009 Sun Microsystems
 
5
 *
 
6
 *  Authors:
 
7
 *
 
8
 *    Jay Pipes <joinfu@sun.com>
 
9
 *
 
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.
 
13
 *
 
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.
 
18
 *
 
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
 
22
 */
 
23
 
 
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
#include <sys/types.h>
 
30
#include <sys/stat.h>
 
31
#include <fcntl.h>
 
32
#include <limits.h>
 
33
#include <cstdio>
 
34
#include <cerrno>
 
35
#include <iostream>
 
36
#include <string>
 
37
#include <algorithm>
 
38
#include <vector>
 
39
#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>
 
44
 
 
45
#include <google/protobuf/io/coded_stream.h>
 
46
#include <google/protobuf/io/zero_copy_stream_impl.h>
 
47
 
 
48
using namespace std;
 
49
using namespace google;
 
50
using namespace drizzled;
 
51
 
 
52
static const char *replace_with_spaces= "\n\r";
 
53
 
 
54
static void printStatement(const message::Statement &statement)
 
55
{
 
56
  vector<string> sql_strings;
 
57
 
 
58
  message::transformStatementToSql(statement,
 
59
                                   sql_strings,
 
60
                                   message::DRIZZLE,
 
61
                                   true /* already in transaction */);
 
62
 
 
63
  for (vector<string>::iterator sql_string_iter= sql_strings.begin();
 
64
       sql_string_iter != sql_strings.end();
 
65
       ++sql_string_iter)
 
66
  {
 
67
    string &sql= *sql_string_iter;
 
68
 
 
69
    /* 
 
70
     * Replace \n and \r with spaces so that SQL statements 
 
71
     * are always on a single line 
 
72
     */
 
73
    {
 
74
      string::size_type found= sql.find_first_of(replace_with_spaces);
 
75
      while (found != string::npos)
 
76
      {
 
77
        sql[found]= ' ';
 
78
        found= sql.find_first_of(replace_with_spaces, found);
 
79
      }
 
80
    }
 
81
 
 
82
    /*
 
83
     * Embedded NUL characters are a pain in the ass.
 
84
     */
 
85
    {
 
86
      string::size_type found= sql.find_first_of('\0');
 
87
      while (found != string::npos)
 
88
      {
 
89
        sql[found]= '\\';
 
90
        sql.insert(found + 1, 1, '0');
 
91
        found= sql.find_first_of('\0', found);
 
92
      }
 
93
    }
 
94
 
 
95
    cout << sql << ';' << endl;
 
96
  }
 
97
}
 
98
 
 
99
static bool isEndStatement(const message::Statement &statement)
 
100
{
 
101
  switch (statement.type())
 
102
  {
 
103
    case (message::Statement::INSERT):
 
104
    {
 
105
      const message::InsertData &data= statement.insert_data();
 
106
      if (not data.end_segment())
 
107
        return false;
 
108
      break;
 
109
    }
 
110
    case (message::Statement::UPDATE):
 
111
    {
 
112
      const message::UpdateData &data= statement.update_data();
 
113
      if (not data.end_segment())
 
114
        return false;
 
115
      break;
 
116
    }
 
117
    case (message::Statement::DELETE):
 
118
    {
 
119
      const message::DeleteData &data= statement.delete_data();
 
120
      if (not data.end_segment())
 
121
        return false;
 
122
      break;
 
123
    }
 
124
    default:
 
125
      return true;
 
126
  }
 
127
  return true;
 
128
}
 
129
 
 
130
static bool isEndTransaction(const message::Transaction &transaction)
 
131
{
 
132
  const message::TransactionContext trx= transaction.transaction_context();
 
133
 
 
134
  size_t num_statements= transaction.statement_size();
 
135
 
 
136
  /*
 
137
   * If any Statement is partial, then we can expect another Transaction
 
138
   * message.
 
139
   */
 
140
  for (size_t x= 0; x < num_statements; ++x)
 
141
  {
 
142
    const message::Statement &statement= transaction.statement(x);
 
143
 
 
144
    if (not isEndStatement(statement))
 
145
      return false;
 
146
  }
 
147
 
 
148
  return true;
 
149
}
 
150
 
 
151
static void printTransaction(const message::Transaction &transaction)
 
152
{
 
153
  static uint64_t last_trx_id= 0;
 
154
  bool should_commit= true;
 
155
  const message::TransactionContext trx= transaction.transaction_context();
 
156
 
 
157
  size_t num_statements= transaction.statement_size();
 
158
  size_t x;
 
159
 
 
160
  /*
 
161
   * One way to determine when a new transaction begins is when the
 
162
   * transaction id changes (if all transactions have their GPB messages
 
163
   * grouped together, which this program will). We check that here.
 
164
   */
 
165
  if (trx.transaction_id() != last_trx_id)
 
166
    cout << "START TRANSACTION;" << endl;
 
167
 
 
168
  last_trx_id= trx.transaction_id();
 
169
 
 
170
  for (x= 0; x < num_statements; ++x)
 
171
  {
 
172
    const message::Statement &statement= transaction.statement(x);
 
173
 
 
174
    if (should_commit)
 
175
      should_commit= isEndStatement(statement);
 
176
 
 
177
    printStatement(statement);
 
178
  }
 
179
 
 
180
  /*
 
181
   * If ALL Statements are end segments, we can commit this Transaction.
 
182
   * We can also check to see if the transaction_id changed, but this
 
183
   * wouldn't work for the last Transaction in the transaction log since
 
184
   * we don't have another Transaction to compare to. Checking for all
 
185
   * end segments (like we do above) covers this case.
 
186
   */
 
187
  if (should_commit)
 
188
    cout << "COMMIT;" << endl;
 
189
}
 
190
 
 
191
int main(int argc, char* argv[])
 
192
{
 
193
  GOOGLE_PROTOBUF_VERIFY_VERSION;
 
194
  int file;
 
195
 
 
196
  if (argc < 2 || argc > 3)
 
197
  {
 
198
    fprintf(stderr, _("Usage: %s TRANSACTION_LOG [--checksum] \n"), argv[0]);
 
199
    return -1;
 
200
  }
 
201
 
 
202
  message::Transaction transaction;
 
203
 
 
204
  file= open(argv[1], O_RDONLY);
 
205
  if (file == -1)
 
206
  {
 
207
    fprintf(stderr, _("Cannot open file: %s\n"), argv[1]);
 
208
    return -1;
 
209
  }
 
210
 
 
211
  bool do_checksum= false;
 
212
 
 
213
  if (argc == 3)
 
214
  {
 
215
    string checksum_arg(argv[2]);
 
216
    transform(checksum_arg.begin(), checksum_arg.end(), checksum_arg.begin(), ::tolower);
 
217
 
 
218
    if ("--checksum" == checksum_arg)
 
219
      do_checksum= true;
 
220
  }
 
221
 
 
222
  message::TransactionManager trx_mgr;
 
223
 
 
224
  protobuf::io::ZeroCopyInputStream *raw_input= new protobuf::io::FileInputStream(file);
 
225
  protobuf::io::CodedInputStream *coded_input= new protobuf::io::CodedInputStream(raw_input);
 
226
 
 
227
  char *buffer= NULL;
 
228
  char *temp_buffer= NULL;
 
229
  uint32_t length= 0;
 
230
  uint32_t previous_length= 0;
 
231
  uint32_t checksum= 0;
 
232
  bool result= true;
 
233
  uint32_t message_type= 0;
 
234
 
 
235
  /* Read in the length of the command */
 
236
  while (result == true && 
 
237
         coded_input->ReadLittleEndian32(&message_type) == true &&
 
238
         coded_input->ReadLittleEndian32(&length) == true)
 
239
  {
 
240
    if (message_type != ReplicationServices::TRANSACTION)
 
241
    {
 
242
      fprintf(stderr, _("Found a non-transaction message in log.  Currently, not supported.\n"));
 
243
      exit(1);
 
244
    }
 
245
 
 
246
    if (length > INT_MAX)
 
247
    {
 
248
      fprintf(stderr, _("Attempted to read record bigger than INT_MAX\n"));
 
249
      exit(1);
 
250
    }
 
251
 
 
252
    if (buffer == NULL)
 
253
    {
 
254
      /* 
 
255
       * First time around...just malloc the length.  This block gets rid
 
256
       * of a GCC warning about uninitialized temp_buffer.
 
257
       */
 
258
      temp_buffer= (char *) malloc(static_cast<size_t>(length));
 
259
    }
 
260
    /* No need to allocate if we have a buffer big enough... */
 
261
    else if (length > previous_length)
 
262
    {
 
263
      temp_buffer= (char *) realloc(buffer, static_cast<size_t>(length));
 
264
    }
 
265
 
 
266
    if (temp_buffer == NULL)
 
267
    {
 
268
      fprintf(stderr, _("Memory allocation failure trying to allocate %" PRIu64 " bytes.\n"),
 
269
              static_cast<uint64_t>(length));
 
270
      break;
 
271
    }
 
272
    else
 
273
      buffer= temp_buffer;
 
274
 
 
275
    /* Read the Command */
 
276
    result= coded_input->ReadRaw(buffer, (int) length);
 
277
    if (result == false)
 
278
    {
 
279
      char errmsg[STRERROR_MAX];
 
280
      strerror_r(errno, errmsg, sizeof(errmsg));
 
281
      fprintf(stderr, _("Could not read transaction message.\n"));
 
282
      fprintf(stderr, _("GPB ERROR: %s.\n"), errmsg);
 
283
      string hexdump;
 
284
      hexdump.reserve(length * 4);
 
285
      bytesToHexdumpFormat(hexdump, reinterpret_cast<const unsigned char *>(buffer), length);
 
286
      fprintf(stderr, _("HEXDUMP:\n\n%s\n"), hexdump.c_str());
 
287
      break;
 
288
    }
 
289
 
 
290
    result= transaction.ParseFromArray(buffer, static_cast<int32_t>(length));
 
291
    if (result == false)
 
292
    {
 
293
      fprintf(stderr, _("Unable to parse command. Got error: %s.\n"), transaction.InitializationErrorString().c_str());
 
294
      if (buffer != NULL)
 
295
      {
 
296
        string hexdump;
 
297
        hexdump.reserve(length * 4);
 
298
        bytesToHexdumpFormat(hexdump, reinterpret_cast<const unsigned char *>(buffer), length);
 
299
        fprintf(stderr, _("HEXDUMP:\n\n%s\n"), hexdump.c_str());
 
300
      }
 
301
      break;
 
302
    }
 
303
 
 
304
    if (not isEndTransaction(transaction))
 
305
    {
 
306
      trx_mgr.store(transaction);
 
307
    }
 
308
    else
 
309
    {
 
310
      const message::TransactionContext trx= transaction.transaction_context();
 
311
      uint64_t transaction_id= trx.transaction_id();
 
312
 
 
313
      /*
 
314
       * If there are any previous Transaction messages for this transaction,
 
315
       * store this one, then output all of them together.
 
316
       */
 
317
      if (trx_mgr.contains(transaction_id))
 
318
      {
 
319
        trx_mgr.store(transaction);
 
320
 
 
321
        uint32_t size= trx_mgr.getTransactionBufferSize(transaction_id);
 
322
        uint32_t idx= 0;
 
323
 
 
324
        while (idx != size)
 
325
        {
 
326
          message::Transaction new_trx;
 
327
          trx_mgr.getTransactionMessage(new_trx, transaction_id, idx);
 
328
          printTransaction(new_trx);
 
329
          idx++;
 
330
        }
 
331
 
 
332
        /* No longer need this transaction */
 
333
        trx_mgr.remove(transaction_id);
 
334
      }
 
335
      else
 
336
      {
 
337
        printTransaction(transaction);
 
338
      }
 
339
    }
 
340
 
 
341
    /* Skip 4 byte checksum */
 
342
    coded_input->ReadLittleEndian32(&checksum);
 
343
 
 
344
    if (do_checksum)
 
345
    {
 
346
      if (checksum != drizzled::algorithm::crc32(buffer, static_cast<size_t>(length)))
 
347
      {
 
348
        fprintf(stderr, _("Checksum failed. Wanted %" PRIu32 " got %" PRIu32 "\n"), checksum, drizzled::algorithm::crc32(buffer, static_cast<size_t>(length)));
 
349
      }
 
350
    }
 
351
 
 
352
    previous_length= length;
 
353
  } /* end while */
 
354
 
 
355
  if (buffer)
 
356
    free(buffer);
 
357
 
 
358
  delete coded_input;
 
359
  delete raw_input;
 
360
 
 
361
  return (result == true ? 0 : 1);
 
362
}
 
363