~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/replicator/replicator.cc

  • Committer: Monty Taylor
  • Date: 2009-01-06 18:46:25 UTC
  • mto: This revision was merged to the branch mainline in revision 762.
  • Revision ID: mordred@inaugust.com-20090106184625-kqu7nsnwjwm5jv4s
Enabled dirty_close.

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
using namespace std;
 
27
 
 
28
static bool isEnabled;
 
29
static char *log_directory= NULL;
 
30
int log_file= -1;
 
31
 
 
32
static bool write_to_disk(int file, drizzle::EventList *list)
 
33
{
 
34
  std::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
static bool statement(Session *session, const char *query, size_t)
 
60
{
 
61
  using namespace drizzle;
 
62
 
 
63
  drizzle::EventList list;
 
64
 
 
65
  if (isEnabled == false)
 
66
    return false;
 
67
  cerr << "Got into statement" <<endl;
 
68
 
 
69
  drizzle::Event *record= list.add_event();
 
70
  record->set_type(Event::DDL);
 
71
  record->set_autocommit(true);
 
72
  record->set_server_id("localhost");
 
73
  record->set_query_id(10);
 
74
  record->set_transaction_id("junk");
 
75
  record->set_schema(session->db);
 
76
  record->set_sql(query);
 
77
 
 
78
  return write_to_disk(log_file, &list);
 
79
}
 
80
 
 
81
static bool session_init(Session *session)
 
82
{
 
83
  using namespace drizzle;
 
84
 
 
85
  if (isEnabled == false)
 
86
    return false;
 
87
 
 
88
  drizzle::EventList *list= new drizzle::EventList;
 
89
  session->setReplicationData(list);
 
90
 
 
91
  drizzle::Event *record= list->add_event();
 
92
 
 
93
  record->set_type(Event::DDL);
 
94
  record->set_autocommit(true);
 
95
  record->set_server_id("localhost");
 
96
  record->set_query_id(10);
 
97
  record->set_transaction_id("junk");
 
98
  record->set_schema(session->db);
 
99
  record->set_sql("BEGIN");
 
100
 
 
101
  return false;
 
102
}
 
103
 
 
104
static bool row_insert(Session *session, Table *)
 
105
{
 
106
  using namespace drizzle;
 
107
 
 
108
  if (isEnabled == false)
 
109
    return false;
 
110
 
 
111
  drizzle::EventList *list= (drizzle::EventList *)session->getReplicationData();
 
112
  drizzle::Event *record= list->add_event();
 
113
 
 
114
  record->set_type(Event::INSERT);
 
115
  record->set_autocommit(true);
 
116
  record->set_server_id("localhost");
 
117
  record->set_query_id(10);
 
118
  record->set_transaction_id("junk");
 
119
  record->set_schema(session->db);
 
120
  record->set_sql(session->query);
 
121
 
 
122
  return false;
 
123
}
 
124
 
 
125
static bool row_update(Session *session, Table *, 
 
126
                          const unsigned char *, 
 
127
                          const unsigned char *)
 
128
{
 
129
  using namespace drizzle;
 
130
 
 
131
  if (isEnabled == false)
 
132
    return false;
 
133
 
 
134
  drizzle::EventList *list= (drizzle::EventList *)session->getReplicationData();
 
135
  drizzle::Event *record= list->add_event();
 
136
 
 
137
  record->set_type(Event::UPDATE);
 
138
  record->set_autocommit(true);
 
139
  record->set_server_id("localhost");
 
140
  record->set_query_id(10);
 
141
  record->set_transaction_id("junk");
 
142
  record->set_schema(session->db);
 
143
  record->set_sql(session->query);
 
144
 
 
145
  return false;
 
146
}
 
147
 
 
148
static bool row_delete(Session *session, Table *)
 
149
{
 
150
  using namespace drizzle;
 
151
 
 
152
  if (isEnabled == false)
 
153
    return false;
 
154
 
 
155
  drizzle::EventList *list= (drizzle::EventList *)session->getReplicationData();
 
156
  drizzle::Event *record= list->add_event();
 
157
 
 
158
  record->set_type(Event::DELETE);
 
159
  record->set_autocommit(true);
 
160
  record->set_server_id("localhost");
 
161
  record->set_query_id(10);
 
162
  record->set_transaction_id("junk");
 
163
  record->set_schema(session->db);
 
164
  record->set_sql(session->query);
 
165
 
 
166
  return false;
 
167
}
 
168
 
 
169
static bool end_transaction(Session *session, bool autocommit, bool commit)
 
170
{
 
171
  bool error;
 
172
  using namespace drizzle;
 
173
 
 
174
  if (isEnabled == false)
 
175
    return false;
 
176
 
 
177
  cerr << "Got into end" <<endl;
 
178
 
 
179
  drizzle::EventList *list= (drizzle::EventList *)session->getReplicationData();
 
180
  drizzle::Event *record= list->add_event();
 
181
 
 
182
  record->set_type(Event::DELETE);
 
183
  record->set_autocommit(true);
 
184
  record->set_server_id("localhost");
 
185
  record->set_query_id(10);
 
186
  record->set_transaction_id("junk");
 
187
  record->set_schema(session->db);
 
188
 
 
189
  if (commit)
 
190
  {
 
191
    if (autocommit)
 
192
      record->set_sql("COMMIT");
 
193
    else
 
194
      record->set_sql("AUTOCOMMIT");
 
195
  }
 
196
  else
 
197
    record->set_sql("ROLLBACK");
 
198
 
 
199
  error= write_to_disk(log_file, list);
 
200
 
 
201
  session->setReplicationData(NULL);
 
202
  delete(list);
 
203
 
 
204
  return error;
 
205
}
 
206
 
 
207
static int init(void *p)
 
208
{
 
209
  replicator_t *repl = (replicator_t *)p;
 
210
 
 
211
  repl->statement= statement;
 
212
  repl->session_init= session_init;
 
213
  repl->row_insert= row_insert;
 
214
  repl->row_delete= row_delete;
 
215
  repl->row_update= row_update;
 
216
  repl->end_transaction= end_transaction;
 
217
 
 
218
 
 
219
  if (isEnabled)
 
220
  {
 
221
    using std::string;
 
222
    string logname;
 
223
 
 
224
    logname.append(log_directory ? log_directory : "/tmp");
 
225
    logname.append("/replication_log");
 
226
 
 
227
    if ((log_file= open(logname.c_str(), O_TRUNC|O_CREAT|O_SYNC|O_WRONLY, S_IRWXU)) == -1)
 
228
    {
 
229
      cerr << "Can not open file: " << logname.c_str() << endl;
 
230
      exit(0);
 
231
    }
 
232
  }
 
233
 
 
234
  return 0;
 
235
}
 
236
 
 
237
static int deinit(void *)
 
238
{
 
239
  if (log_file != -1)
 
240
    close(log_file);
 
241
 
 
242
  return 0;
 
243
}
 
244
 
 
245
static DRIZZLE_SYSVAR_BOOL(
 
246
  enabled,
 
247
  isEnabled,
 
248
  PLUGIN_VAR_NOCMDARG,
 
249
  N_("Enable Replicator"),
 
250
  NULL, /* check func */
 
251
  NULL, /* update func */
 
252
  false /* default */);
 
253
 
 
254
static DRIZZLE_SYSVAR_STR(
 
255
  directory,
 
256
  log_directory,
 
257
  PLUGIN_VAR_READONLY,
 
258
  N_("Directory to place replication logs."),
 
259
  NULL, /* check func */
 
260
  NULL, /* update func*/
 
261
  NULL /* default */);
 
262
 
 
263
static struct st_mysql_sys_var* system_variables[]= {
 
264
  DRIZZLE_SYSVAR(directory),
 
265
  DRIZZLE_SYSVAR(enabled),
 
266
  NULL,
 
267
};
 
268
 
 
269
mysql_declare_plugin(replicator)
 
270
{
 
271
  DRIZZLE_REPLICATOR_PLUGIN,
 
272
  "replicator",
 
273
  "0.1",
 
274
  "Brian Aker",
 
275
  "Basic replication module",
 
276
  PLUGIN_LICENSE_GPL,
 
277
  init, /* Plugin Init */
 
278
  deinit, /* Plugin Deinit */
 
279
  NULL,   /* status variables */
 
280
  system_variables,   /* system variables */
 
281
  NULL    /* config options */
 
282
}
 
283
mysql_declare_plugin_end;