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