1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Mark Atwood
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
#include <drizzled/server_includes.h>
21
#include <drizzled/replicator.h>
22
#include <drizzled/gettext.h>
23
#include <drizzled/session.h>
25
int replicator_initializer(st_plugin_int *plugin)
29
if (plugin->plugin->init)
31
if (plugin->plugin->init((void *)&p))
33
/* TRANSLATORS: The leading word "replicator" is the name
34
of the plugin api, and so should not be translated. */
35
errmsg_printf(ERRMSG_LVL_ERROR,
36
_("replicator plugin '%s' init() failed"),
42
plugin->data= (void *)p;
47
int replicator_finalizer(st_plugin_int *plugin)
49
Replicator *p= static_cast<Replicator *>(plugin->data);
51
if (plugin->plugin->deinit)
53
if (plugin->plugin->deinit((void *)p))
55
/* TRANSLATORS: The leading word "replicator" is the name
56
of the plugin api, and so should not be translated. */
57
errmsg_printf(ERRMSG_LVL_ERROR,
58
_("replicator plugin '%s' deinit() failed"),
66
/* This gets called by plugin_foreach once for each loaded replicator plugin */
67
static bool replicator_session_iterate(Session *session, plugin_ref plugin, void *)
69
Replicator *repl= plugin_data(plugin, Replicator *);
72
/* call this loaded replicator plugin's session_init method */
75
error= repl->session_init(session);
78
/* TRANSLATORS: The leading word "replicator" is the name
79
of the plugin api, and so should not be translated. */
80
errmsg_printf(ERRMSG_LVL_ERROR,
81
_("replicator plugin '%s' session_init() failed"),
82
(char *)plugin_name(plugin));
91
This call is called once at the begining of each transaction.
93
extern StorageEngine *binlog_engine;
94
bool replicator_session_init(Session *session)
98
if (session->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN))
99
trans_register_ha(session, true, binlog_engine);
100
trans_register_ha(session, false, binlog_engine);
102
if (session->getReplicationData())
106
call replicator_session_iterate
107
once for each loaded replicator plugin
109
foreach_rv= plugin_foreach(session, replicator_session_iterate,
110
DRIZZLE_REPLICATOR_PLUGIN, NULL);
115
/* The plugin_foreach() iterator requires that we
116
convert all the parameters of a plugin api entry point
117
into just one single void ptr, plus the session.
118
So we will take all the additional paramters of replicator_do2,
119
and marshall them into a struct of this type, and
120
then just pass in a pointer to it.
122
enum repl_row_exec_t{
128
typedef struct replicator_row_parms_st
130
repl_row_exec_t type;
132
const unsigned char *before;
133
const unsigned char *after;
134
} replicator_row_parms_st;
137
/* This gets called by plugin_foreach once for each loaded replicator plugin */
138
static bool replicator_do_row_iterate (Session *session, plugin_ref plugin, void *p)
140
Replicator *repl= plugin_data(plugin, Replicator *);
141
replicator_row_parms_st *params= static_cast<replicator_row_parms_st *>(p);
143
switch (params->type) {
147
if (repl->row_insert(session, params->table))
149
/* TRANSLATORS: The leading word "replicator" is the name
150
of the plugin api, and so should not be translated. */
151
errmsg_printf(ERRMSG_LVL_ERROR,
152
_("replicator plugin '%s' row_insert() failed"),
153
(char *)plugin_name(plugin));
162
if (repl->row_update(session, params->table,
163
params->before, params->after))
165
/* TRANSLATORS: The leading word "replicator" is the name
166
of the plugin api, and so should not be translated. */
167
errmsg_printf(ERRMSG_LVL_ERROR,
168
_("replicator plugin '%s' row_update() failed"),
169
(char *)plugin_name(plugin));
178
if (repl->row_delete(session, params->table))
180
/* TRANSLATORS: The leading word "replicator" is the name
181
of the plugin api, and so should not be translated. */
182
errmsg_printf(ERRMSG_LVL_ERROR,
183
_("replicator plugin '%s' row_delete() failed"),
184
(char *)plugin_name(plugin));
194
/* This is the replicator_do_row entry point.
195
This gets called by the rest of the Drizzle server code */
196
static bool replicator_do_row (Session *session,
197
replicator_row_parms_st *params)
201
foreach_rv= plugin_foreach(session, replicator_do_row_iterate,
202
DRIZZLE_REPLICATOR_PLUGIN, params);
206
bool replicator_write_row(Session *session, Table *table)
208
replicator_row_parms_st param;
210
param.type= repl_insert;
215
return replicator_do_row(session, ¶m);
218
bool replicator_update_row(Session *session, Table *table,
219
const unsigned char *before,
220
const unsigned char *after)
222
replicator_row_parms_st param;
224
param.type= repl_update;
227
param.before= before;
229
return replicator_do_row(session, ¶m);
232
bool replicator_delete_row(Session *session, Table *table)
234
replicator_row_parms_st param;
236
param.type= repl_delete;
241
return replicator_do_row(session, ¶m);
247
Ok, not so much dragons, but this is where we handle either commits or rollbacks of
250
typedef struct replicator_row_end_st
254
} replicator_row_end_st;
256
/* We call this to end a statement (on each registered plugin) */
257
static bool replicator_end_transaction_iterate (Session *session, plugin_ref plugin, void *p)
259
Replicator *repl= plugin_data(plugin, Replicator *);
260
replicator_row_end_st *params= static_cast<replicator_row_end_st *>(p);
262
/* call this loaded replicator plugin's replicator_func1 function pointer */
265
if (repl->end_transaction(session, params->autocommit, params->commit))
267
/* TRANSLATORS: The leading word "replicator" is the name
268
of the plugin api, and so should not be translated. */
269
errmsg_printf(ERRMSG_LVL_ERROR,
270
_("replicator plugin '%s' end_transaction() failed"),
271
(char *)plugin_name(plugin));
279
bool replicator_end_transaction(Session *session, bool autocommit, bool commit)
282
replicator_row_end_st params;
284
params.autocommit= autocommit;
285
params.commit= commit;
287
/* We need to free any data we did an init of for the session */
288
foreach_rv= plugin_foreach(session, replicator_end_transaction_iterate,
289
DRIZZLE_REPLICATOR_PLUGIN, (void *) ¶ms);
295
If you can do real 2PC this is your hook poing to know that the event is coming.
297
Always true for the moment.
300
bool replicator_prepare(Session *)
308
typedef struct replicator_statement_st
312
} replicator_statement_st;
314
/* We call this to end a statement (on each registered plugin) */
315
static bool replicator_statement_iterate(Session *session, plugin_ref plugin, void *p)
317
Replicator *repl= plugin_data(plugin, Replicator *);
318
replicator_statement_st *params= (replicator_statement_st *)p;
320
/* call this loaded replicator plugin's replicator_func1 function pointer */
323
if (repl->statement(session, params->query, params->query_length))
325
/* TRANSLATORS: The leading word "replicator" is the name
326
of the plugin api, and so should not be translated. */
327
errmsg_printf(ERRMSG_LVL_ERROR,
328
_("replicator plugin '%s' statement() failed"),
329
(char *)plugin_name(plugin));
337
bool replicator_statement(Session *session, const char *query, size_t query_length)
340
replicator_statement_st params;
343
params.query_length= query_length;
345
/* We need to free any data we did an init of for the session */
346
foreach_rv= plugin_foreach(session, replicator_statement_iterate,
347
DRIZZLE_REPLICATOR_PLUGIN, (void *) ¶ms);