~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/protobuf_replicator/protobuf_replicator.cc

  • Committer: Brian Aker
  • Date: 2009-04-13 16:22:40 UTC
  • mfrom: (971.1.78 mordred)
  • Revision ID: brian@gaz-20090413162240-ugi3gvhofmcuglzl
Merge Monty

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2006 MySQL AB
2
 
 
3
 
   This program is free software; you can redistribute it and/or modify
4
 
   it under the terms of the GNU General Public License as published by
5
 
   the Free Software Foundation; version 2 of the License.
6
 
 
7
 
   This program is distributed in the hope that it will be useful,
8
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 
   GNU General Public License for more details.
11
 
 
12
 
   You should have received a copy of the GNU General Public License
13
 
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
 
 
16
 
#include <drizzled/server_includes.h>
17
 
#include <drizzled/gettext.h>
18
 
#include <drizzled/session.h>
19
 
#include <drizzled/error.h>
20
 
#include <drizzled/plugin/replicator.h>
21
 
#include <drizzled/serialize/serialize.h>
22
 
 
23
 
#include <iostream>
24
 
#include <fstream>
25
 
#include <string>
26
 
 
27
 
using namespace std;
28
 
 
29
 
static bool isEnabled;
30
 
static char *log_directory= NULL;
31
 
 
32
 
static bool write_to_disk(int file, drizzle::EventList *list)
33
 
{
34
 
  string buffer;
35
 
  size_t length;
36
 
  size_t written;
37
 
 
38
 
  list->SerializePartialToString(&buffer);
39
 
 
40
 
  length= buffer.length();
41
 
 
42
 
  cout << "Writing record of " << length << "." << endl;
43
 
 
44
 
  if ((written= write(file, &length, sizeof(uint64_t))) != sizeof(uint64_t))
45
 
  {
46
 
    cerr << "Only wrote " << written << " out of " << length << "." << endl;
47
 
    return true;
48
 
  }
49
 
 
50
 
  if ((written= write(file, buffer.c_str(), length)) != length)
51
 
  {
52
 
    cerr << "Only wrote " << written << " out of " << length << "." << endl;
53
 
    return true;
54
 
  }
55
 
 
56
 
  return false;
57
 
}
58
 
 
59
 
class Protobuf_replicator: public Replicator
60
 
{
61
 
  int log_file;
62
 
public:
63
 
  Protobuf_replicator() : log_file(-1)
64
 
  {
65
 
    if (isEnabled)
66
 
    {
67
 
      string logname;
68
 
  
69
 
      logname.append(log_directory ? log_directory : "/tmp");
70
 
      logname.append("/replication_log");
71
 
  
72
 
      if ((log_file= open(logname.c_str(), O_TRUNC|O_CREAT|O_SYNC|O_WRONLY, S_IRWXU)) == -1)
73
 
      {
74
 
        cerr << "Can not open file: " << logname.c_str() << endl;
75
 
        log_file= -1;
76
 
        isEnabled= false;
77
 
      }
78
 
    }
79
 
  }
80
 
 
81
 
  ~Protobuf_replicator()
82
 
  {
83
 
    if (log_file != -1)
84
 
      close(log_file);
85
 
  }
86
 
 
87
 
  virtual bool statement_hook(Session *session, const char *query, size_t)
88
 
  {
89
 
    using namespace drizzle;
90
 
  
91
 
    drizzle::EventList list;
92
 
  
93
 
    if (isEnabled == false)
94
 
      return false;
95
 
    cerr << "Got into statement" <<endl;
96
 
  
97
 
    drizzle::Event *record= list.add_event();
98
 
    record->set_type(Event::DDL);
99
 
    record->set_autocommit(true);
100
 
    record->set_server_id("localhost");
101
 
    record->set_query_id(10);
102
 
    record->set_transaction_id("junk");
103
 
    record->set_schema(session->db);
104
 
    record->set_sql(query);
105
 
  
106
 
    return write_to_disk(log_file, &list);
107
 
  }
108
 
  
109
 
  virtual bool session_init_hook(Session *session)
110
 
  {
111
 
    using namespace drizzle;
112
 
  
113
 
    if (isEnabled == false)
114
 
      return false;
115
 
  
116
 
    drizzle::EventList *list= new drizzle::EventList;
117
 
    session->setReplicationData(list);
118
 
  
119
 
    drizzle::Event *record= list->add_event();
120
 
  
121
 
    record->set_type(Event::DDL);
122
 
    record->set_autocommit(true);
123
 
    record->set_server_id("localhost");
124
 
    record->set_query_id(10);
125
 
    record->set_transaction_id("junk");
126
 
    record->set_schema(session->db);
127
 
    record->set_sql("BEGIN");
128
 
  
129
 
    return false;
130
 
  }
131
 
  
132
 
  virtual bool row_insert_hook(Session *session, Table *)
133
 
  {
134
 
    using namespace drizzle;
135
 
  
136
 
    if (isEnabled == false)
137
 
      return false;
138
 
  
139
 
    drizzle::EventList *list= (drizzle::EventList *)session->getReplicationData();
140
 
    drizzle::Event *record= list->add_event();
141
 
  
142
 
    record->set_type(Event::INSERT);
143
 
    record->set_autocommit(true);
144
 
    record->set_server_id("localhost");
145
 
    record->set_query_id(10);
146
 
    record->set_transaction_id("junk");
147
 
    record->set_schema(session->db);
148
 
    record->set_sql(session->query);
149
 
  
150
 
    return false;
151
 
  }
152
 
  
153
 
  virtual bool row_update_hook(Session *session, Table *, 
154
 
                               const unsigned char *, 
155
 
                               const unsigned char *)
156
 
  {
157
 
    using namespace drizzle;
158
 
  
159
 
    if (isEnabled == false)
160
 
      return false;
161
 
  
162
 
    drizzle::EventList *list= (drizzle::EventList *)session->getReplicationData();
163
 
    drizzle::Event *record= list->add_event();
164
 
  
165
 
    record->set_type(Event::UPDATE);
166
 
    record->set_autocommit(true);
167
 
    record->set_server_id("localhost");
168
 
    record->set_query_id(10);
169
 
    record->set_transaction_id("junk");
170
 
    record->set_schema(session->db);
171
 
    record->set_sql(session->query);
172
 
  
173
 
    return false;
174
 
  }
175
 
  
176
 
  virtual bool row_delete_hook(Session *session, Table *)
177
 
  {
178
 
    using namespace drizzle;
179
 
  
180
 
    if (isEnabled == false)
181
 
      return false;
182
 
  
183
 
    drizzle::EventList *list= (drizzle::EventList *)session->getReplicationData();
184
 
    drizzle::Event *record= list->add_event();
185
 
  
186
 
    record->set_type(Event::DELETE);
187
 
    record->set_autocommit(true);
188
 
    record->set_server_id("localhost");
189
 
    record->set_query_id(10);
190
 
    record->set_transaction_id("junk");
191
 
    record->set_schema(session->db);
192
 
    record->set_sql(session->query);
193
 
  
194
 
    return false;
195
 
  }
196
 
  
197
 
  virtual bool end_transaction_hook(Session *session,
198
 
                                    bool autocommit, bool commit)
199
 
  {
200
 
    bool error;
201
 
    using namespace drizzle;
202
 
  
203
 
    if (isEnabled == false)
204
 
      return false;
205
 
  
206
 
    cerr << "Got into end" <<endl;
207
 
  
208
 
    drizzle::EventList *list= (drizzle::EventList *)session->getReplicationData();
209
 
    drizzle::Event *record= list->add_event();
210
 
  
211
 
    record->set_type(Event::DELETE);
212
 
    record->set_autocommit(true);
213
 
    record->set_server_id("localhost");
214
 
    record->set_query_id(10);
215
 
    record->set_transaction_id("junk");
216
 
    record->set_schema(session->db);
217
 
  
218
 
    if (commit)
219
 
    {
220
 
      if (autocommit)
221
 
        record->set_sql("COMMIT");
222
 
      else
223
 
        record->set_sql("AUTOCOMMIT");
224
 
    }
225
 
    else
226
 
      record->set_sql("ROLLBACK");
227
 
  
228
 
    error= write_to_disk(log_file, list);
229
 
  
230
 
    session->setReplicationData(NULL);
231
 
    delete(list);
232
 
  
233
 
    return error;
234
 
  }
235
 
};
236
 
 
237
 
static int init(void *p)
238
 
{
239
 
  Replicator **repl = static_cast<Replicator **>(p);
240
 
 
241
 
  *repl= new Protobuf_replicator();
242
 
 
243
 
  return 0;
244
 
}
245
 
 
246
 
static int deinit(void *p)
247
 
{
248
 
  Protobuf_replicator *repl = static_cast<Protobuf_replicator *>(p);
249
 
 
250
 
  delete repl;
251
 
 
252
 
  return 0;
253
 
}
254
 
 
255
 
static DRIZZLE_SYSVAR_BOOL(
256
 
  enabled,
257
 
  isEnabled,
258
 
  PLUGIN_VAR_NOCMDARG,
259
 
  N_("Enable Replicator"),
260
 
  NULL, /* check func */
261
 
  NULL, /* update func */
262
 
  false /* default */);
263
 
 
264
 
static DRIZZLE_SYSVAR_STR(
265
 
  directory,
266
 
  log_directory,
267
 
  PLUGIN_VAR_READONLY,
268
 
  N_("Directory to place replication logs."),
269
 
  NULL, /* check func */
270
 
  NULL, /* update func*/
271
 
  NULL /* default */);
272
 
 
273
 
static struct st_mysql_sys_var* system_variables[]= {
274
 
  DRIZZLE_SYSVAR(directory),
275
 
  DRIZZLE_SYSVAR(enabled),
276
 
  NULL,
277
 
};
278
 
 
279
 
drizzle_declare_plugin(protobuf_replicator)
280
 
{
281
 
  DRIZZLE_REPLICATOR_PLUGIN,
282
 
  "replicator",
283
 
  "0.1",
284
 
  "Brian Aker",
285
 
  "Basic replication module",
286
 
  PLUGIN_LICENSE_GPL,
287
 
  init, /* Plugin Init */
288
 
  deinit, /* Plugin Deinit */
289
 
  NULL,   /* status variables */
290
 
  system_variables,   /* system variables */
291
 
  NULL    /* config options */
292
 
}
293
 
drizzle_declare_plugin_end;