~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/replicator.cc

  • Committer: Monty Taylor
  • Date: 2008-12-18 07:42:01 UTC
  • mto: This revision was merged to the branch mainline in revision 714.
  • Revision ID: monty@inaugust.com-20081218074201-4m7d63t72qfuplwx
Fixed max() problem for solaris

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008-2009 Sun Microsystems, Inc.
5
 
 *  Copyright (C) 2009-2010 Jay Pipes <jaypipes@gmail.com>
6
 
 *
7
 
 *  Authors:
8
 
 *
9
 
 *    Jay Pipes <jaypipes@gmail.com>
 
4
 *  Copyright (C) 2008 Mark Atwood
10
5
 *
11
6
 *  This program is free software; you can redistribute it and/or modify
12
7
 *  it under the terms of the GNU General Public License as published by
22
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23
18
 */
24
19
 
25
 
/**
26
 
 * @file Server-side utility which is responsible for managing the 
27
 
 * communication between the kernel and the replication plugins:
28
 
 *
29
 
 * - TransactionReplicator
30
 
 * - TransactionApplier
31
 
 * - Publisher
32
 
 * - Subscriber
33
 
 *
34
 
 * ReplicationServices is a bridge between replication modules and the kernel,
35
 
 * and its primary function is to  */
36
 
 
37
 
#include <config.h>
38
 
#include <drizzled/replication_services.h>
39
 
#include <drizzled/plugin/transaction_replicator.h>
40
 
#include <drizzled/plugin/transaction_applier.h>
41
 
#include <drizzled/message/transaction.pb.h>
 
20
#include <drizzled/server_includes.h>
 
21
#include <drizzled/replicator.h>
42
22
#include <drizzled/gettext.h>
43
23
#include <drizzled/session.h>
44
 
#include <drizzled/error.h>
45
 
 
46
 
#include <string>
47
 
#include <vector>
48
 
#include <algorithm>
49
 
 
50
 
using namespace std;
51
 
 
52
 
namespace drizzled
53
 
{
54
 
 
55
 
ReplicationServices::ReplicationServices() :
56
 
  is_active(false)
57
 
{
58
 
}
59
 
 
60
 
void ReplicationServices::normalizeReplicatorName(string &name)
61
 
{
62
 
  transform(name.begin(),
63
 
            name.end(),
64
 
            name.begin(),
65
 
            ::tolower);
66
 
  if (name.find("replicator") == string::npos)
67
 
    name.append("replicator", 10);
68
 
  {
69
 
    size_t found_underscore= name.find('_');
70
 
    while (found_underscore != string::npos)
71
 
    {
72
 
      name.erase(found_underscore, 1);
73
 
      found_underscore= name.find('_');
74
 
    }
75
 
  }
76
 
}
77
 
 
78
 
bool ReplicationServices::evaluateRegisteredPlugins()
79
 
{
 
24
 
 
25
int replicator_initializer(st_plugin_int *plugin)
 
26
{
 
27
  replicator_t *p;
 
28
 
 
29
  p= new replicator_t;
 
30
  if (p == NULL) return 1;
 
31
  memset(p, 0, sizeof(replicator_t));
 
32
 
 
33
  plugin->data= (void *)p;
 
34
 
 
35
  if (plugin->plugin->init)
 
36
  {
 
37
    if (plugin->plugin->init((void *)p))
 
38
    {
 
39
      /* TRANSLATORS: The leading word "replicator" is the name
 
40
        of the plugin api, and so should not be translated. */
 
41
      sql_print_error(_("replicator plugin '%s' init() failed"),
 
42
                      plugin->name.str);
 
43
      goto err;
 
44
    }
 
45
  }
 
46
  return 0;
 
47
 
 
48
 err:
 
49
  delete p;
 
50
  return 1;
 
51
}
 
52
 
 
53
int replicator_finalizer(st_plugin_int *plugin)
 
54
{
 
55
  replicator_t *p= (replicator_t *) plugin->data;
 
56
 
 
57
  if (plugin->plugin->deinit)
 
58
    {
 
59
      if (plugin->plugin->deinit((void *)p))
 
60
        {
 
61
          /* TRANSLATORS: The leading word "replicator" is the name
 
62
             of the plugin api, and so should not be translated. */
 
63
          sql_print_error(_("replicator plugin '%s' deinit() failed"),
 
64
                          plugin->name.str);
 
65
        }
 
66
    }
 
67
 
 
68
  if (p) delete p;
 
69
 
 
70
  return 0;
 
71
}
 
72
 
 
73
/* This gets called by plugin_foreach once for each loaded replicator plugin */
 
74
static bool replicator_session_iterate(Session *session, plugin_ref plugin, void *)
 
75
{
 
76
  replicator_t *repl= plugin_data(plugin, replicator_t *);
 
77
  bool error;
 
78
 
 
79
  /* call this loaded replicator plugin's replicator_func1 function pointer */
 
80
  if (repl && repl->session_init)
 
81
  {
 
82
    error= repl->session_init(session);
 
83
    if (error)
 
84
    {
 
85
      /* TRANSLATORS: The leading word "replicator" is the name
 
86
        of the plugin api, and so should not be translated. */
 
87
      sql_print_error(_("replicator plugin '%s' session_init() failed"),
 
88
                      (char *)plugin_name(plugin));
 
89
      return true;
 
90
    }
 
91
  }
 
92
 
 
93
  return false;
 
94
}
 
95
 
 
96
/*
 
97
  This call is called once at the begining of each transaction.
 
98
*/
 
99
extern handlerton *binlog_hton;
 
100
bool replicator_session_init(Session *session)
 
101
{
 
102
  bool foreach_rv;
 
103
 
 
104
  if (session->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN))
 
105
    trans_register_ha(session, true, binlog_hton);
 
106
  trans_register_ha(session, false, binlog_hton);
 
107
 
 
108
  if (session->getReplicationData())
 
109
    return false;
 
110
 
80
111
  /* 
81
 
   * We loop through appliers that have registered with us
82
 
   * and attempts to pair the applier with its requested
83
 
   * replicator.  If an applier has requested a replicator
84
 
   * that has either not been built or has not registered
85
 
   * with the replication services, we print an error and
86
 
   * return false
87
 
   */
88
 
  if (appliers.empty())
89
 
    return true;
90
 
 
91
 
  if (replicators.empty() && not appliers.empty())
92
 
  {
93
 
    errmsg_printf(error::ERROR,
94
 
                  N_("You registered a TransactionApplier plugin but no "
95
 
                     "TransactionReplicator plugins were registered.\n"));
96
 
    return false;
97
 
  }
98
 
 
99
 
  for (Appliers::iterator appl_iter= appliers.begin();
100
 
       appl_iter != appliers.end();
101
 
       ++appl_iter)
102
 
  {
103
 
    plugin::TransactionApplier *applier= (*appl_iter).second;
104
 
    string requested_replicator_name= (*appl_iter).first;
105
 
    normalizeReplicatorName(requested_replicator_name);
106
 
 
107
 
    bool found= false;
108
 
    Replicators::iterator repl_iter;
109
 
    for (repl_iter= replicators.begin();
110
 
         repl_iter != replicators.end();
111
 
         ++repl_iter)
112
 
    {
113
 
      string replicator_name= (*repl_iter)->getName();
114
 
      normalizeReplicatorName(replicator_name);
115
 
 
116
 
      if (requested_replicator_name.compare(replicator_name) == 0)
117
 
      {
118
 
        found= true;
119
 
        break;
120
 
      }
121
 
    }
122
 
    if (not found)
123
 
    {
124
 
      errmsg_printf(error::ERROR,
125
 
                    N_("You registered a TransactionApplier plugin but no "
126
 
                       "TransactionReplicator plugins were registered that match the "
127
 
                       "requested replicator name of '%s'.\n"
128
 
                       "We have deactivated the TransactionApplier '%s'.\n"),
129
 
                       requested_replicator_name.c_str(),
130
 
                       applier->getName().c_str());
131
 
      applier->deactivate();
132
 
      return false;
133
 
    }
134
 
    else
135
 
    {
136
 
      replication_streams.push_back(make_pair(*repl_iter, applier));
137
 
    }
138
 
  }
139
 
  is_active= true;
140
 
  return true;
141
 
}
142
 
 
143
 
void ReplicationServices::attachReplicator(plugin::TransactionReplicator *in_replicator)
144
 
{
145
 
  replicators.push_back(in_replicator);
146
 
}
147
 
 
148
 
void ReplicationServices::detachReplicator(plugin::TransactionReplicator *in_replicator)
149
 
{
150
 
  replicators.erase(std::find(replicators.begin(), replicators.end(), in_replicator));
151
 
}
152
 
 
153
 
void ReplicationServices::attachApplier(plugin::TransactionApplier *in_applier, const string &requested_replicator_name)
154
 
{
155
 
  appliers.push_back(make_pair(requested_replicator_name, in_applier));
156
 
}
157
 
 
158
 
void ReplicationServices::detachApplier(plugin::TransactionApplier *)
159
 
{
160
 
}
161
 
 
162
 
bool ReplicationServices::isActive() const
163
 
{
164
 
  return is_active;
165
 
}
166
 
 
167
 
plugin::ReplicationReturnCode ReplicationServices::pushTransactionMessage(Session &in_session,
168
 
                                                                          message::Transaction &to_push)
169
 
{
170
 
  plugin::ReplicationReturnCode result= plugin::SUCCESS;
171
 
 
172
 
  for (ReplicationStreams::iterator iter= replication_streams.begin();
173
 
       iter != replication_streams.end();
174
 
       ++iter)
175
 
  {
176
 
    plugin::TransactionReplicator *cur_repl= (*iter).first;
177
 
    plugin::TransactionApplier *cur_appl= (*iter).second;
178
 
 
179
 
    result= cur_repl->replicate(cur_appl, in_session, to_push);
180
 
 
181
 
    if (result == plugin::SUCCESS)
182
 
    {
183
 
      /* 
184
 
       * We update the timestamp for the last applied Transaction so that
185
 
       * publisher plugins can ask the replication services when the
186
 
       * last known applied Transaction was using the getLastAppliedTimestamp()
187
 
       * method.
188
 
       */
189
 
      last_applied_timestamp.fetch_and_store(to_push.transaction_context().end_timestamp());
190
 
    }
191
 
    else
192
 
      return result;
193
 
  }
194
 
  return result;
195
 
}
196
 
 
197
 
ReplicationServices::ReplicationStreams &ReplicationServices::getReplicationStreams()
198
 
{
199
 
  return replication_streams;
200
 
}
201
 
 
202
 
} /* namespace drizzled */
 
112
    call replicator_session_iterate
 
113
    once for each loaded replicator plugin
 
114
  */
 
115
  foreach_rv= plugin_foreach(session, replicator_session_iterate,
 
116
                             DRIZZLE_REPLICATOR_PLUGIN, NULL);
 
117
 
 
118
  return foreach_rv;
 
119
}
 
120
 
 
121
/* The plugin_foreach() iterator requires that we
 
122
   convert all the parameters of a plugin api entry point
 
123
   into just one single void ptr, plus the session.
 
124
   So we will take all the additional paramters of replicator_do2,
 
125
   and marshall them into a struct of this type, and
 
126
   then just pass in a pointer to it.
 
127
*/
 
128
enum repl_row_exec_t{
 
129
  repl_insert,
 
130
  repl_update,
 
131
  repl_delete
 
132
};
 
133
 
 
134
typedef struct replicator_row_parms_st
 
135
{
 
136
  repl_row_exec_t type;
 
137
  Table *table;
 
138
  const unsigned char *before;
 
139
  const unsigned char *after;
 
140
} replicator_row_parms_st;
 
141
 
 
142
 
 
143
/* This gets called by plugin_foreach once for each loaded replicator plugin */
 
144
static bool replicator_do_row_iterate (Session *session, plugin_ref plugin, void *p)
 
145
{
 
146
  replicator_t *repl= plugin_data(plugin, replicator_t *);
 
147
  replicator_row_parms_st *params= (replicator_row_parms_st *) p;
 
148
 
 
149
  switch (params->type) {
 
150
  case repl_insert:
 
151
    if (repl && repl->row_insert)
 
152
    {
 
153
      if (repl->row_insert(session, params->table))
 
154
      {
 
155
        /* TRANSLATORS: The leading word "replicator" is the name
 
156
          of the plugin api, and so should not be translated. */
 
157
        sql_print_error(_("replicator plugin '%s' row_insert() failed"),
 
158
                        (char *)plugin_name(plugin));
 
159
 
 
160
        return true;
 
161
      }
 
162
    }
 
163
    break;
 
164
  case repl_update:
 
165
    if (repl && repl->row_update)
 
166
    {
 
167
      if (repl->row_update(session, params->table, params->before, params->after))
 
168
      {
 
169
        /* TRANSLATORS: The leading word "replicator" is the name
 
170
          of the plugin api, and so should not be translated. */
 
171
        sql_print_error(_("replicator plugin '%s' row_update() failed"),
 
172
                        (char *)plugin_name(plugin));
 
173
 
 
174
        return true;
 
175
      }
 
176
    }
 
177
    break;
 
178
  case repl_delete:
 
179
    if (repl && repl->row_delete)
 
180
    {
 
181
      if (repl->row_delete(session, params->table))
 
182
      {
 
183
        /* TRANSLATORS: The leading word "replicator" is the name
 
184
          of the plugin api, and so should not be translated. */
 
185
        sql_print_error(_("replicator plugin '%s' row_delete() failed"),
 
186
                        (char *)plugin_name(plugin));
 
187
 
 
188
        return true;
 
189
      }
 
190
    }
 
191
    break;
 
192
  }
 
193
  return false;
 
194
}
 
195
 
 
196
/* This is the replicator_do2 entry point.
 
197
   This gets called by the rest of the Drizzle server code */
 
198
static bool replicator_do_row (Session *session, replicator_row_parms_st *params)
 
199
{
 
200
  bool foreach_rv;
 
201
 
 
202
  foreach_rv= plugin_foreach(session, replicator_do_row_iterate,
 
203
                             DRIZZLE_REPLICATOR_PLUGIN, params);
 
204
  return foreach_rv;
 
205
}
 
206
 
 
207
bool replicator_write_row(Session *session, Table *table)
 
208
{
 
209
  replicator_row_parms_st param;
 
210
 
 
211
  param.type= repl_insert;
 
212
  param.table= table;
 
213
  param.after= NULL;
 
214
  param.before= NULL;
 
215
 
 
216
  return replicator_do_row(session, &param);
 
217
}
 
218
 
 
219
bool replicator_update_row(Session *session, Table *table,
 
220
                           const unsigned char *before,
 
221
                           const unsigned char *after)
 
222
{
 
223
  replicator_row_parms_st param;
 
224
 
 
225
  param.type= repl_update;
 
226
  param.table= table;
 
227
  param.after= after;
 
228
  param.before= before;
 
229
 
 
230
  return replicator_do_row(session, &param);
 
231
}
 
232
 
 
233
bool replicator_delete_row(Session *session, Table *table)
 
234
{
 
235
  replicator_row_parms_st param;
 
236
 
 
237
  param.type= repl_delete;
 
238
  param.table= table;
 
239
  param.after= NULL;
 
240
  param.before= NULL;
 
241
 
 
242
  return replicator_do_row(session, &param);
 
243
}
 
244
 
 
245
/*
 
246
  Here be Dragons!
 
247
 
 
248
  Ok, not so much dragons, but this is where we handle either commits or rollbacks of
 
249
  statements.
 
250
*/
 
251
typedef struct replicator_row_end_st
 
252
{
 
253
  bool autocommit;
 
254
  bool commit;
 
255
} replicator_row_end_st;
 
256
 
 
257
/* We call this to end a statement (on each registered plugin) */
 
258
static bool replicator_end_transaction_iterate (Session *session, plugin_ref plugin, void *p)
 
259
{
 
260
  replicator_t *repl= plugin_data(plugin, replicator_t *);
 
261
  replicator_row_end_st *params= (replicator_row_end_st *)p;
 
262
 
 
263
  /* call this loaded replicator plugin's replicator_func1 function pointer */
 
264
  if (repl && repl->end_transaction)
 
265
  {
 
266
    if (repl->end_transaction(session, params->autocommit, params->commit))
 
267
    {
 
268
      /* TRANSLATORS: The leading word "replicator" is the name
 
269
        of the plugin api, and so should not be translated. */
 
270
      sql_print_error(_("replicator plugin '%s' end_transaction() failed"),
 
271
                      (char *)plugin_name(plugin));
 
272
      return true;
 
273
    }
 
274
  }
 
275
 
 
276
  return false;
 
277
}
 
278
 
 
279
bool replicator_end_transaction(Session *session, bool autocommit, bool commit)
 
280
{
 
281
  bool foreach_rv;
 
282
  replicator_row_end_st params;
 
283
 
 
284
  params.autocommit= autocommit;
 
285
  params.commit= commit;
 
286
 
 
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 *) &params);
 
290
 
 
291
  return foreach_rv;
 
292
}
 
293
 
 
294
/*
 
295
  If you can do real 2PC this is your hook poing to know that the event is coming.
 
296
 
 
297
  Always true for the moment.
 
298
 
 
299
*/
 
300
bool replicator_prepare(Session *)
 
301
{
 
302
  return false;
 
303
}
 
304
 
 
305
/*
 
306
  Replicate statement.
 
307
*/
 
308
typedef struct replicator_statement_st
 
309
{
 
310
  const char *query;
 
311
  size_t query_length;
 
312
} replicator_statement_st;
 
313
 
 
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)
 
316
{
 
317
  replicator_t *repl= plugin_data(plugin, replicator_t *);
 
318
  replicator_statement_st *params= (replicator_statement_st *)p;
 
319
 
 
320
  /* call this loaded replicator plugin's replicator_func1 function pointer */
 
321
  if (repl && repl->statement)
 
322
  {
 
323
    if (repl->statement(session, params->query, params->query_length))
 
324
    {
 
325
      /* TRANSLATORS: The leading word "replicator" is the name
 
326
        of the plugin api, and so should not be translated. */
 
327
      sql_print_error(_("replicator plugin '%s' statement() failed"),
 
328
                      (char *)plugin_name(plugin));
 
329
      return true;
 
330
    }
 
331
  }
 
332
 
 
333
  return false;
 
334
}
 
335
 
 
336
bool replicator_statement(Session *session, const char *query, size_t query_length)
 
337
{
 
338
  bool foreach_rv;
 
339
  replicator_statement_st params;
 
340
  
 
341
  params.query= query;
 
342
  params.query_length= query_length;
 
343
 
 
344
  /* We need to free any data we did an init of for the session */
 
345
  foreach_rv= plugin_foreach(session, replicator_statement_iterate,
 
346
                             DRIZZLE_REPLICATOR_PLUGIN, (void *) &params);
 
347
 
 
348
  return foreach_rv;
 
349
}