~drizzle-trunk/drizzle/development

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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2008-2009 Sun Microsystems
 *
 *  Authors:
 *
 *    Jay Pipes <joinfu@sun.com>
 *
 *  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
 */

/**
 * @file Server-side utility which is responsible for managing the 
 * communication between the kernel, replicator plugins, and applier plugins.
 *
 * ReplicationServices is a bridge between modules and the kernel, and its
 * primary function is to take internal events (for instance the start of a 
 * transaction, the changing of a record, or the rollback of a transaction) 
 * and construct GPB Messages that are passed to the registered replicator and
 * applier plugins.
 *
 * The reason for this functionality is to encapsulate all communication
 * between the kernel and the replicator/applier plugins into GPB Messages.
 * Instead of the plugin having to understand the (often fluidly changing)
 * mechanics of the kernel, all the plugin needs to understand is the message
 * format, and GPB messages provide a nice, clear, and versioned format for 
 * these messages.
 *
 * @see /drizzled/message/replication.proto
 */

#include "drizzled/server_includes.h"
#include "drizzled/replication_services.h"
#include "drizzled/plugin/replicator.h"
#include "drizzled/plugin/applier.h"
#include "drizzled/message/replication.pb.h"
#include "drizzled/message/table.pb.h"
#include "drizzled/gettext.h"
#include "drizzled/session.h"
#include "drizzled/plugin_registry.h"

#include <vector>

using namespace drizzled;

ReplicationServices replication_services;

void add_replicator(plugin::Replicator *replicator)
{
  replication_services.attachReplicator(replicator);
}

void remove_replicator(plugin::Replicator *replicator)
{
  replication_services.detachReplicator(replicator);
}

void add_applier(plugin::Applier *applier)
{
  replication_services.attachApplier(applier);
}

void remove_applier(plugin::Applier *applier)
{
  replication_services.detachApplier(applier);
}

namespace drizzled
{

ReplicationServices::ReplicationServices()
{
  is_active= false;
}

void ReplicationServices::evaluateActivePlugins()
{
  /* 
   * We loop through replicators and appliers, evaluating
   * whether or not there is at least one active replicator
   * and one active applier.  If not, we set is_active
   * to false.
   */
  bool tmp_is_active= false;

  if (replicators.size() == 0 || appliers.size() == 0)
  {
    is_active= false;
    return;
  }

  /* 
   * Determine if any remaining replicators and if those
   * replicators are active...if not, set is_active
   * to false
   */
  std::vector<plugin::Replicator *>::iterator repl_iter= replicators.begin();
  while (repl_iter != replicators.end())
  {
    if ((*repl_iter)->isActive())
    {
      tmp_is_active= true;
      break;
    }
    ++repl_iter;
  }
  if (! tmp_is_active)
  {
    /* No active replicators. Set is_active to false and exit. */
    is_active= false;
    return;
  }

  /* 
   * OK, we know there's at least one active replicator.
   *
   * Now determine if any remaining replicators and if those
   * replicators are active...if not, set is_active
   * to false
   */
  std::vector<plugin::Applier *>::iterator appl_iter= appliers.begin();
  while (appl_iter != appliers.end())
  {
    if ((*appl_iter)->isActive())
    {
      is_active= true;
      return;
    }
    ++appl_iter;
  }
  /* If we get here, there are no active appliers */
  is_active= false;
}

void ReplicationServices::attachReplicator(plugin::Replicator *in_replicator)
{
  replicators.push_back(in_replicator);
  evaluateActivePlugins();
}

void ReplicationServices::detachReplicator(plugin::Replicator *in_replicator)
{
  replicators.erase(std::find(replicators.begin(), replicators.end(), in_replicator));
  evaluateActivePlugins();
}

void ReplicationServices::attachApplier(plugin::Applier *in_applier)
{
  appliers.push_back(in_applier);
  evaluateActivePlugins();
}

void ReplicationServices::detachApplier(plugin::Applier *in_applier)
{
  appliers.erase(std::find(appliers.begin(), appliers.end(), in_applier));
  evaluateActivePlugins();
}

bool ReplicationServices::isActive() const
{
  return is_active;
}

void ReplicationServices::setCommandTransactionContext(message::Command *in_command
                                                     , Session *in_session) const
{
  message::TransactionContext *trx= in_command->mutable_transaction_context();
  trx->set_server_id(in_session->getServerId());
  trx->set_transaction_id(in_session->getTransactionId());
}

void ReplicationServices::startTransaction(Session *in_session)
{
  if (! is_active)
    return;
  
  message::Command command;
  command.set_type(message::Command::START_TRANSACTION);
  command.set_timestamp(in_session->getCurrentTimestamp());

  setCommandTransactionContext(&command, in_session);
  
  push(&command);
}

void ReplicationServices::commitTransaction(Session *in_session)
{
  if (! is_active)
    return;
  
  message::Command command;
  command.set_type(message::Command::COMMIT);
  command.set_timestamp(in_session->getCurrentTimestamp());

  setCommandTransactionContext(&command, in_session);
  
  push(&command);
}

void ReplicationServices::rollbackTransaction(Session *in_session)
{
  if (! is_active)
    return;
  
  message::Command command;
  command.set_type(message::Command::ROLLBACK);
  command.set_timestamp(in_session->getCurrentTimestamp());

  setCommandTransactionContext(&command, in_session);
  
  push(&command);
}

void ReplicationServices::insertRecord(Session *in_session, Table *in_table)
{
  if (! is_active)
    return;

  message::Command command;
  command.set_type(message::Command::INSERT);
  command.set_timestamp(in_session->getCurrentTimestamp());

  setCommandTransactionContext(&command, in_session);

  const char *schema_name= in_table->getShare()->db.str;
  const char *table_name= in_table->getShare()->table_name.str;

  command.set_schema(schema_name);
  command.set_table(table_name);

  /* 
   * Now we construct the specialized InsertRecord command inside
   * the message::Command container...
   */
  message::InsertRecord *change_record= command.mutable_insert_record();

  Field *current_field;
  Field **table_fields= in_table->field;
  String *string_value= new (in_session->mem_root) String(ReplicationServices::DEFAULT_RECORD_SIZE);
  string_value->set_charset(system_charset_info);

  message::Table::Field *current_proto_field;

  /* We will read all the table's fields... */
  in_table->setReadSet();

  while ((current_field= *table_fields++) != NULL) 
  {
    current_proto_field= change_record->add_insert_field();
    current_proto_field->set_name(std::string(current_field->field_name));
    current_proto_field->set_type(message::Table::Field::VARCHAR); /* @TODO real types! */
    string_value= current_field->val_str(string_value);
    change_record->add_insert_value(std::string(string_value->c_ptr()));
    string_value->free();
  }
  
  push(&command);
}

void ReplicationServices::updateRecord(Session *in_session,
                                       Table *in_table, 
                                       const unsigned char *old_record, 
                                       const unsigned char *new_record)
{
  if (! is_active)
    return;
  
  message::Command command;
  command.set_type(message::Command::UPDATE);
  command.set_timestamp(in_session->getCurrentTimestamp());

  setCommandTransactionContext(&command, in_session);

  const char *schema_name= in_table->getShare()->db.str;
  const char *table_name= in_table->getShare()->table_name.str;

  command.set_schema(schema_name);
  command.set_table(table_name);

  /* 
   * Now we construct the specialized UpdateRecord command inside
   * the message::Command container...
   */
  message::UpdateRecord *change_record= command.mutable_update_record();

  Field *current_field;
  Field **table_fields= in_table->field;
  String *string_value= new (in_session->mem_root) String(ReplicationServices::DEFAULT_RECORD_SIZE);
  string_value->set_charset(system_charset_info);

  message::Table::Field *current_proto_field;

  while ((current_field= *table_fields++) != NULL) 
  {
    /*
     * The below really should be moved into the Field API and Record API.  But for now
     * we do this crazy pointer fiddling to figure out if the current field
     * has been updated in the supplied record raw byte pointers.
     */
    const unsigned char *old_ptr= (const unsigned char *) old_record + (ptrdiff_t) (current_field->ptr - in_table->record[0]); 
    const unsigned char *new_ptr= (const unsigned char *) new_record + (ptrdiff_t) (current_field->ptr - in_table->record[0]); 

    uint32_t field_length= current_field->pack_length(); /** @TODO This isn't always correct...check varchar diffs. */

    if (memcmp(old_ptr, new_ptr, field_length) != 0)
    {
      /* Field is changed from old to new */
      current_proto_field= change_record->add_update_field();
      current_proto_field->set_name(std::string(current_field->field_name));
      current_proto_field->set_type(message::Table::Field::VARCHAR); /* @TODO real types! */

      /* Store the original "read bit" for this field */
      bool is_read_set= current_field->isReadSet();

      /* We need to mark that we will "read" this field... */
      in_table->setReadSet(current_field->field_index);

      /* Read the string value of this field's contents */
      string_value= current_field->val_str(string_value);

      /* 
       * Reset the read bit after reading field to its original state.  This 
       * prevents the field from being included in the WHERE clause
       */
      current_field->setReadSet(is_read_set);

      change_record->add_after_value(std::string(string_value->c_ptr()));
      string_value->free();
    }

    /* 
     * Add the WHERE clause values now...the fields which return true
     * for isReadSet() are in the WHERE clause.  For tables with no
     * primary or unique key, all fields will be returned.
     */
    if (current_field->isReadSet())
    {
      current_proto_field= change_record->add_where_field();
      current_proto_field->set_name(std::string(current_field->field_name));
      current_proto_field->set_type(message::Table::Field::VARCHAR); /* @TODO real types! */
      string_value= current_field->val_str(string_value);
      change_record->add_where_value(std::string(string_value->c_ptr()));
      string_value->free();
    }
  }

  push(&command);
}

void ReplicationServices::deleteRecord(Session *in_session, Table *in_table)
{
  if (! is_active)
    return;
  
  message::Command command;
  command.set_type(message::Command::DELETE);
  command.set_timestamp(in_session->getCurrentTimestamp());

  setCommandTransactionContext(&command, in_session);

  const char *schema_name= in_table->getShare()->db.str;
  const char *table_name= in_table->getShare()->table_name.str;

  command.set_schema(schema_name);
  command.set_table(table_name);

  /* 
   * Now we construct the specialized DeleteRecord command inside
   * the message::Command container...
   */
  message::DeleteRecord *change_record= command.mutable_delete_record();
 
  Field *current_field;
  Field **table_fields= in_table->field;
  String *string_value= new (in_session->mem_root) String(ReplicationServices::DEFAULT_RECORD_SIZE);
  string_value->set_charset(system_charset_info);

  message::Table::Field *current_proto_field;

  while ((current_field= *table_fields++) != NULL)
  {
    /*
     * Add the WHERE clause values now...the fields which return true
     * for isReadSet() are in the WHERE clause.  For tables with no
     * primary or unique key, all fields will be returned.
     */
    if (current_field->isReadSet())
    {
      current_proto_field= change_record->add_where_field();
      current_proto_field->set_name(std::string(current_field->field_name));
      current_proto_field->set_type(message::Table::Field::VARCHAR); /* @TODO real types! */
      string_value= current_field->val_str(string_value);
      change_record->add_where_value(std::string(string_value->c_ptr()));
      string_value->free();
    }
  }
 
  push(&command);
}

void ReplicationServices::rawStatement(Session *in_session, const char *in_query, size_t in_query_len)
{
  if (! is_active)
    return;
  
  message::Command command;
  command.set_type(message::Command::RAW_SQL);
  command.set_timestamp(in_session->getCurrentTimestamp());

  setCommandTransactionContext(&command, in_session);

  std::string query(in_query, in_query_len);
  command.set_sql(query);

  push(&command);
}

void ReplicationServices::push(message::Command *to_push)
{
  std::vector<plugin::Replicator *>::iterator repl_iter= replicators.begin();
  std::vector<plugin::Applier *>::iterator appl_start_iter, appl_iter;
  appl_start_iter= appliers.begin();

  plugin::Replicator *cur_repl;
  plugin::Applier *cur_appl;

  while (repl_iter != replicators.end())
  {
    cur_repl= *repl_iter;
    if (! cur_repl->isActive())
    {
      ++repl_iter;
      continue;
    }
    
    appl_iter= appl_start_iter;
    while (appl_iter != appliers.end())
    {
      cur_appl= *appl_iter;

      if (! cur_appl->isActive())
      {
        ++appl_iter;
        continue;
      }

      cur_repl->replicate(cur_appl, to_push);
      
      /* 
       * We update the timestamp for the last applied Command so that
       * publisher plugins can ask the replication services when the
       * last known applied Command was using the getLastAppliedTimestamp()
       * method.
       */
      last_applied_timestamp.fetch_and_store(to_push->timestamp());
      ++appl_iter;
    }
    ++repl_iter;
  }
}

} /* end namespace drizzled */