~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/transaction_reader.cc

  • Committer: Padraig O'Sullivan
  • Date: 2009-09-13 01:03:01 UTC
  • mto: (1126.9.2 captain-20090915-01)
  • mto: This revision was merged to the branch mainline in revision 1133.
  • Revision ID: osullivan.padraig@gmail.com-20090913010301-tcvvezipx1124acy
Added calls to the dtrace delete begin/end probes.

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/gettext.h>
26
 
#include <drizzled/replication_services.h>
27
 
#include <drizzled/algorithm/crc32.h>
 
1
#include <drizzled/global.h>
28
2
#include <sys/types.h>
29
3
#include <sys/stat.h>
30
4
#include <fcntl.h>
31
 
#include <limits.h>
32
 
#include <cerrno>
33
5
#include <iostream>
 
6
#include <fstream>
34
7
#include <string>
35
 
#include <algorithm>
36
 
#include <vector>
37
8
#include <unistd.h>
38
 
#include <drizzled/message/transaction.pb.h>
39
 
#include <drizzled/message/statement_transform.h>
40
 
#include <drizzled/util/convert.h>
41
 
 
42
 
#include <google/protobuf/io/coded_stream.h>
43
 
#include <google/protobuf/io/zero_copy_stream_impl.h>
 
9
#include <drizzled/message/replication.pb.h>
44
10
 
45
11
using namespace std;
46
 
using namespace google;
47
12
using namespace drizzled;
48
13
 
49
 
static const char *replace_with_spaces= "\n\r";
50
 
 
51
 
static void printStatement(const message::Statement &statement)
52
 
{
53
 
  vector<string> sql_strings;
54
 
 
55
 
  message::transformStatementToSql(statement,
56
 
                                   sql_strings,
57
 
                                   message::DRIZZLE,
58
 
                                   true /* already in transaction */);
59
 
 
60
 
  for (vector<string>::iterator sql_string_iter= sql_strings.begin();
61
 
       sql_string_iter != sql_strings.end();
62
 
       ++sql_string_iter)
63
 
  {
64
 
    string &sql= *sql_string_iter;
65
 
 
66
 
    /* 
67
 
     * Replace \n and \r with spaces so that SQL statements 
68
 
     * are always on a single line 
69
 
     */
70
 
    {
71
 
      string::size_type found= sql.find_first_of(replace_with_spaces);
72
 
      while (found != string::npos)
73
 
      {
74
 
        sql[found]= ' ';
75
 
        found= sql.find_first_of(replace_with_spaces, found);
76
 
      }
77
 
    }
78
 
 
79
 
    /*
80
 
     * Embedded NUL characters are a pain in the ass.
81
 
     */
82
 
    {
83
 
      string::size_type found= sql.find_first_of('\0');
84
 
      while (found != string::npos)
85
 
      {
86
 
        sql[found]= '\\';
87
 
        sql.insert(found + 1, 1, '0');
88
 
        found= sql.find_first_of('\0', found);
89
 
      }
90
 
    }
91
 
 
92
 
    cout << sql << ';' << endl;
 
14
/**
 
15
 * @file Example application for reading change records and transactions
 
16
 */
 
17
 
 
18
static void printInsert(const message::Command &container,
 
19
                        const message::InsertRecord &record)
 
20
{
 
21
 
 
22
  cout << "INSERT INTO `" << container.schema() << "`.`"
 
23
       << container.table() << "` (";
 
24
 
 
25
  int32_t num_fields= record.insert_field_size();
 
26
 
 
27
  int32_t x;
 
28
  for (x= 0; x < num_fields; x++)
 
29
  {
 
30
    if (x != 0)
 
31
      cout << ", ";
 
32
 
 
33
    const message::Table::Field f= record.insert_field(x);
 
34
 
 
35
    cout << "`" << f.name() << "`";
 
36
  }
 
37
 
 
38
  cout << ") VALUES ";
 
39
 
 
40
  /* 
 
41
   * There may be an INSERT VALUES (),() type statement.  We know the
 
42
   * number of records is equal to the field_values array size divided
 
43
   * by the number of fields.
 
44
   *
 
45
   * So, we do an inner and an outer loop.  Outer loop is on the number
 
46
   * of records and the inner loop on the number of fields.  In this way, 
 
47
   * we know that record.field_values(outer_loop * num_fields) + inner_loop))
 
48
   * always gives us our correct field value.
 
49
   */
 
50
  int32_t num_records= (record.insert_value_size() / num_fields);
 
51
  int32_t y;
 
52
  for (x= 0; x < num_records; x++)
 
53
  {
 
54
    if (x != 0)
 
55
      cout << ", ";
 
56
 
 
57
    cout << "(";
 
58
    for (y= 0; y < num_fields; y++)
 
59
    {
 
60
      if (y != 0)
 
61
        cout << ", ";
 
62
 
 
63
      cout << "\"" << record.insert_value((x * num_fields) + y) << "\"";
 
64
    }
 
65
    cout << ")";
 
66
  }
 
67
 
 
68
  cout << ";";
 
69
}
 
70
 
 
71
static void printDeleteWithPK(const message::Command &container,
 
72
                              const message::DeleteRecord &record)
 
73
{
 
74
  cout << "DELETE FROM `" << container.schema() << "`.`" << container.table() << "`";
 
75
 
 
76
  int32_t num_where_fields= record.where_field_size();
 
77
  /* 
 
78
   * Make sure we catch anywhere we're not aligning the fields with
 
79
   * the field_values arrays...
 
80
   */
 
81
  assert(num_where_fields == record.where_value_size());
 
82
 
 
83
  cout << " WHERE ";
 
84
  int32_t x;
 
85
  for (x= 0; x < num_where_fields; x++)
 
86
  {
 
87
    if (x != 0)
 
88
      cout << " AND "; /* Always AND condition with a multi-column PK */
 
89
 
 
90
    const message::Table::Field f= record.where_field(x);
 
91
 
 
92
    /* Always equality conditions */
 
93
    cout << "`" << f.name() << "` = \"" << record.where_value(x) << "\"";
 
94
  }
 
95
}
 
96
 
 
97
static void printUpdateWithPK(const message::Command &container,
 
98
                              const message::UpdateRecord &record)
 
99
{
 
100
  int32_t num_update_fields= record.update_field_size();
 
101
  int32_t x;
 
102
 
 
103
  cout << "UPDATE `" << container.schema() << "`.`" << container.table() << "` SET ";
 
104
 
 
105
  for (x= 0;x < num_update_fields; x++)
 
106
  {
 
107
    message::Table::Field f= record.update_field(x);
 
108
    
 
109
    if (x != 0)
 
110
      cout << ", ";
 
111
 
 
112
    cout << "`" << f.name() << "` = \"" << record.after_value(x) << "\"";
 
113
  }
 
114
 
 
115
  int32_t num_where_fields= record.where_field_size();
 
116
  /* 
 
117
   * Make sure we catch anywhere we're not aligning the fields with
 
118
   * the field_values arrays...
 
119
   */
 
120
  assert(num_where_fields == record.where_value_size());
 
121
 
 
122
  cout << " WHERE ";
 
123
  for (x= 0;x < num_where_fields; x++)
 
124
  {
 
125
    if (x != 0)
 
126
      cout << " AND "; /* Always AND condition with a multi-column PK */
 
127
 
 
128
    const message::Table::Field f= record.where_field(x);
 
129
 
 
130
    /* Always equality conditions */
 
131
    cout << "`" << f.name() << "` = \"" << record.where_value(x) << "\"";
93
132
  }
94
133
}
95
134
 
96
135
static void printTransaction(const message::Transaction &transaction)
97
136
{
98
 
  const message::TransactionContext trx= transaction.transaction_context();
99
 
 
100
 
  size_t num_statements= transaction.statement_size();
101
 
  size_t x;
102
 
 
103
 
  cout << "START TRANSACTION;" << endl;
104
 
  for (x= 0; x < num_statements; ++x)
 
137
  int32_t e_size;
 
138
 
 
139
  cout << "/* Start Time: " << transaction.start_timestamp() << " */ START TRANSACTION;"<< endl;
 
140
 
 
141
  for (e_size= 0; e_size < transaction.command_size(); e_size++)
105
142
  {
106
 
    const message::Statement &statement= transaction.statement(x);
107
 
    printStatement(statement);
 
143
    const message::Command command= transaction.command(e_size);
 
144
 
 
145
    message::TransactionContext trx= command.transaction_context();
 
146
 
 
147
    cout << "/* SID: " << trx.server_id() << " XID: " << trx.transaction_id() << " */ ";
 
148
 
 
149
    switch (command.type())
 
150
    {
 
151
      case message::Command::START_TRANSACTION:
 
152
        cout << "START TRANSACTION;";
 
153
        break;
 
154
      case message::Command::COMMIT:
 
155
        cout << "COMMIT;";
 
156
        break;
 
157
      case message::Command::ROLLBACK:
 
158
        cout << "ROLLBACK;";
 
159
        break;
 
160
      case message::Command::INSERT:
 
161
      {
 
162
        printInsert(command, command.insert_record());
 
163
        break;
 
164
      }
 
165
      case message::Command::DELETE:
 
166
      {
 
167
        printDeleteWithPK(command, command.delete_record());
 
168
        break;
 
169
      }
 
170
      case message::Command::UPDATE:
 
171
      {
 
172
        printUpdateWithPK(command, command.update_record());
 
173
        break;
 
174
      }
 
175
      default:
 
176
      assert(0);
 
177
    }
 
178
    cout << endl;
108
179
  }
109
 
  cout << "COMMIT;" << endl;
 
180
  cout << "/* Commit Time: " << transaction.end_timestamp() << " */ COMMIT;" << endl;
110
181
}
111
182
 
112
183
int main(int argc, char* argv[])
114
185
  GOOGLE_PROTOBUF_VERIFY_VERSION;
115
186
  int file;
116
187
 
117
 
  if (argc < 2 || argc > 3)
 
188
  if (argc != 2)
118
189
  {
119
 
    fprintf(stderr, _("Usage: %s TRANSACTION_LOG [--checksum] \n"), argv[0]);
 
190
    cerr << "Usage:  " << argv[0] << " TRANSACTION_LOG" << endl;
120
191
    return -1;
121
192
  }
122
193
 
123
194
  message::Transaction transaction;
124
195
 
125
 
  file= open(argv[1], O_RDONLY);
126
 
  if (file == -1)
127
 
  {
128
 
    fprintf(stderr, _("Cannot open file: %s\n"), argv[1]);
129
 
    return -1;
130
 
  }
131
 
 
132
 
  bool do_checksum= false;
133
 
 
134
 
  if (argc == 3)
135
 
  {
136
 
    string checksum_arg(argv[2]);
137
 
    transform(checksum_arg.begin(), checksum_arg.end(), checksum_arg.begin(), ::tolower);
138
 
 
139
 
    if ("--checksum" == checksum_arg)
140
 
      do_checksum= true;
141
 
  }
142
 
 
143
 
  protobuf::io::ZeroCopyInputStream *raw_input= new protobuf::io::FileInputStream(file);
144
 
  protobuf::io::CodedInputStream *coded_input= new protobuf::io::CodedInputStream(raw_input);
 
196
  if ((file= open(argv[1], O_RDONLY)) == -1)
 
197
  {
 
198
    cerr << "Can not open file: " << argv[1] << endl;
 
199
  }
145
200
 
146
201
  char *buffer= NULL;
147
 
  char *temp_buffer= NULL;
148
 
  uint32_t length= 0;
149
 
  uint32_t previous_length= 0;
150
 
  uint32_t checksum= 0;
151
 
  bool result= true;
152
 
  uint32_t message_type= 0;
 
202
  char *temp_buffer;
153
203
 
154
 
  /* Read in the length of the command */
155
 
  while (result == true && 
156
 
         coded_input->ReadLittleEndian32(&message_type) == true &&
157
 
         coded_input->ReadLittleEndian32(&length) == true)
 
204
  while (1)
158
205
  {
159
 
    if (message_type != ReplicationServices::TRANSACTION)
160
 
    {
161
 
      fprintf(stderr, _("Found a non-transaction message in log.  Currently, not supported.\n"));
162
 
      exit(1);
163
 
    }
164
 
 
165
 
    if (length > INT_MAX)
166
 
    {
167
 
      fprintf(stderr, _("Attempted to read record bigger than INT_MAX\n"));
168
 
      exit(1);
169
 
    }
170
 
 
171
 
    if (buffer == NULL)
172
 
    {
173
 
      /* 
174
 
       * First time around...just malloc the length.  This block gets rid
175
 
       * of a GCC warning about uninitialized temp_buffer.
176
 
       */
177
 
      temp_buffer= (char *) malloc(static_cast<size_t>(length));
178
 
    }
179
 
    /* No need to allocate if we have a buffer big enough... */
180
 
    else if (length > previous_length)
181
 
    {
182
 
      temp_buffer= (char *) realloc(buffer, static_cast<size_t>(length));
183
 
    }
184
 
 
 
206
    uint64_t length;
 
207
 
 
208
    /* Read the size */
 
209
    if (read(file, &length, sizeof(uint64_t)) != sizeof(uint64_t))
 
210
      break;
 
211
 
 
212
    if (length > SIZE_MAX)
 
213
    {
 
214
      cerr << "Attempted to read record bigger than SIZE_MAX" << endl;
 
215
      exit(1);
 
216
    }
 
217
 
 
218
    temp_buffer= (char *)realloc(buffer, (size_t)length);
185
219
    if (temp_buffer == NULL)
186
220
    {
187
 
      fprintf(stderr, _("Memory allocation failure trying to allocate %" PRIu64 " bytes.\n"),
188
 
              static_cast<uint64_t>(length));
189
 
      break;
190
 
    }
191
 
    else
192
 
      buffer= temp_buffer;
193
 
 
194
 
    /* Read the Command */
195
 
    result= coded_input->ReadRaw(buffer, (int) length);
196
 
    if (result == false)
197
 
    {
198
 
      fprintf(stderr, _("Could not read transaction message.\n"));
199
 
      fprintf(stderr, _("GPB ERROR: %s.\n"), strerror(errno));
200
 
      string hexdump;
201
 
      hexdump.reserve(length * 4);
202
 
      bytesToHexdumpFormat(hexdump, reinterpret_cast<const unsigned char *>(buffer), length);
203
 
      fprintf(stderr, _("HEXDUMP:\n\n%s\n"), hexdump.c_str());
204
 
      break;
205
 
    }
206
 
 
207
 
    result= transaction.ParseFromArray(buffer, static_cast<int32_t>(length));
208
 
    if (result == false)
209
 
    {
210
 
      fprintf(stderr, _("Unable to parse command. Got error: %s.\n"), transaction.InitializationErrorString().c_str());
211
 
      if (buffer != NULL)
212
 
      {
213
 
        string hexdump;
214
 
        hexdump.reserve(length * 4);
215
 
        bytesToHexdumpFormat(hexdump, reinterpret_cast<const unsigned char *>(buffer), length);
216
 
        fprintf(stderr, _("HEXDUMP:\n\n%s\n"), hexdump.c_str());
217
 
      }
218
 
      break;
219
 
    }
 
221
      cerr << "Memory allocation failure trying to allocate " << length << " bytes."  << endl;
 
222
      exit(1);
 
223
    }
 
224
    memset(temp_buffer, 0, (size_t)length);
 
225
    buffer= temp_buffer;
 
226
    size_t read_bytes= 0;
 
227
 
 
228
    /* Read the transaction */
 
229
    if ((read_bytes= read(file, buffer, (size_t)length)) != (size_t)length)
 
230
    {
 
231
      cerr << "Could not read entire transaction. Read " << read_bytes << " bytes instead of " << length << " bytes." << endl;
 
232
      exit(1);
 
233
    }
 
234
    transaction.ParseFromArray(buffer, (int) length);
220
235
 
221
236
    /* Print the transaction */
222
237
    printTransaction(transaction);
223
 
 
224
 
    /* Skip 4 byte checksum */
225
 
    coded_input->ReadLittleEndian32(&checksum);
226
 
 
227
 
    if (do_checksum)
228
 
    {
229
 
      if (checksum != drizzled::algorithm::crc32(buffer, static_cast<size_t>(length)))
230
 
      {
231
 
        fprintf(stderr, _("Checksum failed. Wanted %" PRIu32 " got %" PRIu32 "\n"), checksum, drizzled::algorithm::crc32(buffer, static_cast<size_t>(length)));
232
 
      }
233
 
    }
234
 
 
235
 
    previous_length= length;
236
238
  }
237
 
  if (buffer)
238
 
    free(buffer);
239
 
  
240
 
  delete coded_input;
241
 
  delete raw_input;
242
 
 
243
 
  return (result == true ? 0 : 1);
 
239
  return 0;
244
240
}
245