~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/serialize/replication_event_reader.cc

  • Committer: Brian Aker
  • Date: 2009-01-24 09:43:35 UTC
  • Revision ID: brian@gir-3.local-20090124094335-6qdtvc35gl5fvivz
Adding in an example singe thread scheduler

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>
28
1
#include <sys/types.h>
29
2
#include <sys/stat.h>
30
3
#include <fcntl.h>
31
 
#include <limits.h>
32
 
#include <cerrno>
33
4
#include <iostream>
 
5
#include <fstream>
34
6
#include <string>
35
 
#include <algorithm>
36
 
#include <vector>
37
7
#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>
44
 
 
 
8
#include <drizzled/serialize/replication_event.pb.h>
45
9
using namespace std;
46
 
using namespace google;
47
 
using namespace drizzled;
48
 
 
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;
93
 
  }
94
 
}
95
 
 
96
 
static void printTransaction(const message::Transaction &transaction)
97
 
{
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)
105
 
  {
106
 
    const message::Statement &statement= transaction.statement(x);
107
 
    printStatement(statement);
108
 
  }
109
 
  cout << "COMMIT;" << endl;
 
10
 
 
11
/*
 
12
  Example reader application for master.info data.
 
13
*/
 
14
 
 
15
void printRecord(const drizzle::EventList *list)
 
16
{
 
17
  using namespace drizzle;
 
18
  uint32_t x;
 
19
 
 
20
  for (x= 0; x < list->event_size(); x++)
 
21
  {
 
22
    const drizzle::Event event= list->event(x);
 
23
 
 
24
    if (x != 0)
 
25
      cout << endl << "##########################################################################################" << endl << endl;
 
26
 
 
27
    switch (event.type())
 
28
    {
 
29
    case Event::DDL:
 
30
      cout << "DDL ";
 
31
      break;
 
32
    case Event::INSERT:
 
33
      {
 
34
        uint32_t x;
 
35
 
 
36
        cout << "INSERT INTO " << event.table() << " (";
 
37
 
 
38
        for (x= 0; x < event.field_names_size() ; x++)
 
39
        {
 
40
          if (x != 0)
 
41
            cout << ", ";
 
42
 
 
43
          cout << event.field_names(x);
 
44
        }
 
45
 
 
46
        cout << ") VALUES " << endl;
 
47
 
 
48
        for (x= 0; x < event.values_size(); x++)
 
49
        {
 
50
          uint32_t y;
 
51
          Event_Value values= event.values(x);
 
52
 
 
53
          if (x != 0)
 
54
            cout << ", ";
 
55
 
 
56
          cout << "(";
 
57
          for (y= 0; y < values.value_size() ; y++)
 
58
          {
 
59
            if (y != 0)
 
60
              cout << ", ";
 
61
 
 
62
            cout << "\"" << values.value(y) << "\"";
 
63
          }
 
64
          cout << ")";
 
65
        }
 
66
 
 
67
        cout << ";" << endl;
 
68
        break;
 
69
      }
 
70
    case Event::DELETE:
 
71
      {
 
72
        uint32_t x;
 
73
        Event_Value values= event.values(0);
 
74
 
 
75
        cout << "DELETE FROM " << event.table() << " WHERE " << event.primary_key() << " IN (";
 
76
 
 
77
        for (x= 0; x < values.value_size() ; x++)
 
78
        {
 
79
          if (x != 0)
 
80
            cout << ", ";
 
81
 
 
82
          cout << "\"" << values.value(x) << "\"";
 
83
        }
 
84
 
 
85
        cout << ")" << endl;
 
86
        break;
 
87
      }
 
88
    case Event::UPDATE:
 
89
      {
 
90
        uint32_t count;
 
91
 
 
92
        for (count= 0; count < event.values_size() ; count++)
 
93
        {
 
94
          uint32_t x;
 
95
          Event_Value values= event.values(count);
 
96
 
 
97
          cout << "UPDATE "  << event.table() << " SET ";
 
98
 
 
99
          for (x= 1; x < values.value_size() ; x++)
 
100
          {
 
101
            if (x != 1)
 
102
              cout << ", ";
 
103
 
 
104
            cout << event.field_names(x - 1) << " = \"" << values.value(x) << "\"";
 
105
          }
 
106
 
 
107
          cout << " WHERE " << event.primary_key() << " = " << values.value(0) << endl;
 
108
        }
 
109
 
 
110
        break;
 
111
      }
 
112
    case Event::COMMIT:
 
113
      cout << "COMMIT" << endl;
 
114
      break;
 
115
    }
 
116
    cout << endl;
 
117
 
 
118
    if (event.has_sql())
 
119
      cout << "Original SQL: " << event.sql() << endl;
 
120
 
 
121
    cout << "AUTOCOMMIT: " << event.autocommit() << endl;
 
122
    cout << "Server id: " << event.server_id() << endl;
 
123
    cout << "Query id: " << event.query_id() << endl;
 
124
    cout << "Transaction id: " << event.transaction_id() << endl;
 
125
    cout << "Schema: " << event.schema() << endl;
 
126
    if (event.type() != Event::DDL)
 
127
      cout << "Table Name: " << event.table() << endl;
 
128
  }
110
129
}
111
130
 
112
131
int main(int argc, char* argv[])
114
133
  GOOGLE_PROTOBUF_VERIFY_VERSION;
115
134
  int file;
116
135
 
117
 
  if (argc < 2 || argc > 3)
118
 
  {
119
 
    fprintf(stderr, _("Usage: %s TRANSACTION_LOG [--checksum] \n"), argv[0]);
120
 
    return -1;
121
 
  }
122
 
 
123
 
  message::Transaction transaction;
124
 
 
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);
145
 
 
146
 
  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;
153
 
 
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)
158
 
  {
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
 
 
 
136
  if (argc != 2)
 
137
  {
 
138
    cerr << "Usage:  " << argv[0] << " replication event log " << endl;
 
139
    return -1;
 
140
  }
 
141
 
 
142
  drizzle::EventList list;
 
143
 
 
144
  if ((file= open(argv[1], O_RDONLY)) == -1)
 
145
  {
 
146
    cerr << "Can not open file: " << argv[0] << endl;
 
147
  }
 
148
 
 
149
  while (1)
 
150
  {
 
151
    uint64_t length;
 
152
    char *buffer= NULL;
 
153
    char *temp_buffer;
 
154
 
 
155
    /* Read the size */
 
156
    if (read(file, &length, sizeof(uint64_t)) != sizeof(uint64_t))
 
157
      break;
 
158
 
 
159
    temp_buffer= (char *)realloc(buffer, length);
185
160
    if (temp_buffer == NULL)
186
161
    {
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
 
    }
220
 
 
221
 
    /* Print the transaction */
222
 
    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;
 
162
      cerr << "Memory allocation failure trying to " << length << "."  << endl;
 
163
      exit(1);
 
164
    }
 
165
    buffer= temp_buffer;
 
166
 
 
167
    /* Read the record */
 
168
    if (read(file, buffer, length) != length)
 
169
    {
 
170
      cerr << "Could not read entire record." << endl;
 
171
      exit(1);
 
172
    }
 
173
    list.ParseFromArray(buffer, length);
 
174
 
 
175
    /* Print the record */
 
176
    printRecord(&list);
236
177
  }
237
 
  if (buffer)
238
 
    free(buffer);
239
 
  
240
 
  delete coded_input;
241
 
  delete raw_input;
242
 
 
243
 
  return (result == true ? 0 : 1);
 
178
 
 
179
 
 
180
  return 0;
244
181
}
245