~drizzle-trunk/drizzle/development

636.1.1 by Mark Atwood
add replicator plugin type
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
4
 *  Copyright (C) 2008-2009 Sun Microsystems
636.1.1 by Mark Atwood
add replicator plugin type
5
 *
1039.5.11 by Jay Pipes
Adds multi-value INSERT test, modifies TransactionServices to support UPDATE statements (though WHERE clause still not done...)
6
 *  Authors:
7
 *
8
 *    Jay Pipes <joinfu@sun.com>
9
 *
636.1.1 by Mark Atwood
add replicator plugin type
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; version 2 of the License.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program; if not, write to the Free Software
21
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22
 */
23
1039.5.31 by Jay Pipes
This patch does a few things:
24
#ifndef DRIZZLED_REPLICATION_SERVICES_H
25
#define DRIZZLED_REPLICATION_SERVICES_H
636.1.1 by Mark Atwood
add replicator plugin type
26
1039.5.5 by Jay Pipes
This commit does two things:
27
#include "drizzled/atomics.h"
1143.2.10 by Jay Pipes
Phase 2 new replication work:
28
29
#include "drizzled/message/transaction.pb.h"
30
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
31
#include <vector>
636.1.1 by Mark Atwood
add replicator plugin type
32
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
33
34
namespace drizzled
35
{
36
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
37
/* some forward declarations needed */
38
class Session;
39
class Table;
40
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
41
namespace plugin
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
42
{
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
43
  class TransactionReplicator;
44
  class TransactionApplier;
45
}
1130.1.2 by Monty Taylor
Re-org'd the replication stuff into slots.
46
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
47
/**
48
 * This is a class which manages transforming internal 
49
 * transactional events into GPB messages and sending those
50
 * events out through registered replicators and appliers.
51
 */
1039.5.31 by Jay Pipes
This patch does a few things:
52
class ReplicationServices
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
53
{
1039.5.19 by Jay Pipes
Per Stew's suggestions in code review:
54
public:
55
  static const size_t DEFAULT_RECORD_SIZE= 100;
1039.5.39 by Jay Pipes
This patch does a couple things in preparation for publisher and
56
  typedef uint64_t GlobalTransactionId;
1143.2.15 by Jay Pipes
This patch does the following:
57
  /**
58
   * Types of messages that can go in the transaction
59
   * log file.  Every time something is written into the
60
   * transaction log, it is preceded by a header containing
61
   * the type of message which follows.
62
   */
63
  enum MessageType
64
  {
65
    TRANSACTION= 1, /* A GPB Transaction Message */
66
    BLOB= 2 /* A BLOB value */
67
  };
1143.4.6 by Jay Pipes
Adds test case to transaction log for TRUNCATE TABLE.
68
  typedef std::vector<plugin::TransactionReplicator *> Replicators;
69
  typedef std::vector<plugin::TransactionApplier *> Appliers;
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
70
private:
1039.5.5 by Jay Pipes
This commit does two things:
71
  /** 
72
   * Atomic boolean set to true if any *active* replicators
73
   * or appliers are actually registered.
74
   */
75
  atomic<bool> is_active;
1039.5.31 by Jay Pipes
This patch does a few things:
76
  /**
1143.2.10 by Jay Pipes
Phase 2 new replication work:
77
   * The timestamp of the last time a Transaction message was successfully
1039.5.31 by Jay Pipes
This patch does a few things:
78
   * applied (sent to an Applier)
79
   */
80
  atomic<uint64_t> last_applied_timestamp;
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
81
  /** Our collection of replicator plugins */
1143.4.6 by Jay Pipes
Adds test case to transaction log for TRUNCATE TABLE.
82
  Replicators replicators;
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
83
  /** Our collection of applier plugins */
1143.4.6 by Jay Pipes
Adds test case to transaction log for TRUNCATE TABLE.
84
  Appliers appliers;
1039.5.5 by Jay Pipes
This commit does two things:
85
  /**
86
   * Helper method which is called after any change in the
87
   * registered appliers or replicators to evaluate whether
88
   * any remaining plugins are actually active.
89
   * 
90
   * This method properly sets the is_active member variable.
91
   */
92
  void evaluateActivePlugins();
1143.2.10 by Jay Pipes
Phase 2 new replication work:
93
  /**
94
   * Helper method which returns the active Transaction message
95
   * for the supplied Session.  If one is not found, a new Transaction
96
   * message is allocated, initialized, and returned.
97
   *
98
   * @param The session processing the transaction
99
   */
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
100
  message::Transaction *getActiveTransaction(Session *in_session) const;
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
101
  /** 
102
   * Helper method which attaches a transaction context
1143.2.10 by Jay Pipes
Phase 2 new replication work:
103
   * the supplied transaction based on the supplied Session's
104
   * transaction information.  This method also ensure the
105
   * transaction message is attached properly to the Session object
106
   *
107
   * @param The transaction message to initialize
108
   * @param The Session processing this transaction
109
   */
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
110
  void initTransaction(message::Transaction &in_command, Session *in_session) const;
1143.2.10 by Jay Pipes
Phase 2 new replication work:
111
  /** 
112
   * Helper method which finalizes data members for the 
113
   * supplied transaction's context.
114
   *
115
   * @param The transaction message to finalize 
116
   * @param The Session processing this transaction
117
   */
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
118
  void finalizeTransaction(message::Transaction &in_command, Session *in_session) const;
1143.2.10 by Jay Pipes
Phase 2 new replication work:
119
  /**
120
   * Helper method which deletes transaction memory and
121
   * unsets Session's transaction and statement messages.
122
   */
123
  void cleanupTransaction(message::Transaction *in_transaction,
124
                          Session *in_session) const;
125
  /**
1143.4.8 by Jay Pipes
This commit fixes issues raised by Joe Daly in Bug#489823.
126
   * Returns true if the transaction contains any Statement
127
   * messages which are not end segments (i.e. a bulk statement has
128
   * previously been sent to replicators).
129
   *
130
   * @param The transaction to check
131
   */
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
132
  bool transactionContainsBulkSegment(const message::Transaction &transaction) const;
1143.4.8 by Jay Pipes
This commit fixes issues raised by Joe Daly in Bug#489823.
133
  /**
1143.2.10 by Jay Pipes
Phase 2 new replication work:
134
   * Helper method which initializes a Statement message
135
   *
136
   * @param The statement to initialize
137
   * @param The type of the statement
138
   * @param The session processing this statement
139
   */
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
140
  void initStatement(message::Statement &statement,
141
                     message::Statement::Type in_type,
1143.2.10 by Jay Pipes
Phase 2 new replication work:
142
                     Session *in_session) const;
143
  /**
144
   * Helper method which returns an initialized Statement
145
   * message for methods doing insertion of data.
146
   *
147
   * @param[in] Pointer to the Session doing the processing
148
   * @param[in] Pointer to the Table object being inserted into
149
   */
150
  message::Statement &getInsertStatement(Session *in_session,
151
                                         Table *in_table) const;
152
153
  /**
154
   * Helper method which initializes the header message for
155
   * insert operations.
156
   *
157
   * @param[inout] Statement message container to modify
158
   * @param[in] Pointer to the Session doing the processing
159
   * @param[in] Pointer to the Table being inserted into
160
   */
161
  void setInsertHeader(message::Statement &statement,
162
                       Session *in_session,
163
                       Table *in_table) const;
164
  /**
165
   * Helper method which returns an initialized Statement
166
   * message for methods doing updates of data.
167
   *
168
   * @param[in] Pointer to the Session doing the processing
169
   * @param[in] Pointer to the Table object being updated
170
   * @param[in] Pointer to the old data in the record
171
   * @param[in] Pointer to the new data in the record
172
   */
173
  message::Statement &getUpdateStatement(Session *in_session,
174
                                         Table *in_table,
175
                                         const unsigned char *old_record, 
176
                                         const unsigned char *new_record) const;
177
  /**
178
   * Helper method which initializes the header message for
179
   * update operations.
180
   *
181
   * @param[inout] Statement message container to modify
182
   * @param[in] Pointer to the Session doing the processing
183
   * @param[in] Pointer to the Table being updated
184
   * @param[in] Pointer to the old data in the record
185
   * @param[in] Pointer to the new data in the record
186
   */
187
  void setUpdateHeader(message::Statement &statement,
188
                       Session *in_session,
189
                       Table *in_table,
190
                       const unsigned char *old_record, 
191
                       const unsigned char *new_record) const;
192
  /**
193
   * Helper method which returns an initialized Statement
194
   * message for methods doing deletion of data.
195
   *
196
   * @param[in] Pointer to the Session doing the processing
197
   * @param[in] Pointer to the Table object being deleted from
198
   */
199
  message::Statement &getDeleteStatement(Session *in_session,
200
                                         Table *in_table) const;
201
202
  /**
203
   * Helper method which initializes the header message for
204
   * insert operations.
205
   *
206
   * @param[inout] Statement message container to modify
207
   * @param[in] Pointer to the Session doing the processing
208
   * @param[in] Pointer to the Table being deleted from
209
   */
210
  void setDeleteHeader(message::Statement &statement,
211
                       Session *in_session,
212
                       Table *in_table) const;
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
213
  /**
214
   * Helper method which pushes a constructed message out
215
   * to the registered replicator and applier plugins.
216
   *
217
   * @param Message to push out
218
   */
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
219
  void push(message::Transaction &to_push);
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
220
public:
221
  /**
1039.5.5 by Jay Pipes
This commit does two things:
222
   * Constructor
223
   */
1039.5.31 by Jay Pipes
This patch does a few things:
224
  ReplicationServices();
1130.1.2 by Monty Taylor
Re-org'd the replication stuff into slots.
225
226
  /**
227
   * Singleton method
228
   * Returns the singleton instance of ReplicationServices
229
   */
230
  static inline ReplicationServices &singleton()
231
  {
232
    static ReplicationServices replication_services;
233
    return replication_services;
234
  }
235
1039.5.5 by Jay Pipes
This commit does two things:
236
  /**
1039.5.31 by Jay Pipes
This patch does a few things:
237
   * Returns whether the ReplicationServices object
1039.5.5 by Jay Pipes
This commit does two things:
238
   * is active.  In other words, does it have both
239
   * a replicator and an applier that are *active*?
240
   */
241
  bool isActive() const;
242
  /**
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
243
   * Attaches a replicator to our internal collection of
244
   * replicators.
245
   *
246
   * @param Pointer to a replicator to attach/register
247
   */
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
248
  void attachReplicator(plugin::TransactionReplicator *in_replicator);
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
249
  /**
250
   * Detaches/unregisters a replicator with our internal
251
   * collection of replicators.
252
   *
253
   * @param Pointer to the replicator to detach
254
   */
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
255
  void detachReplicator(plugin::TransactionReplicator *in_replicator);
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
256
  /**
257
   * Attaches a applier to our internal collection of
258
   * appliers.
259
   *
260
   * @param Pointer to a applier to attach/register
261
   */
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
262
  void attachApplier(plugin::TransactionApplier *in_applier);
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
263
  /**
264
   * Detaches/unregisters a applier with our internal
265
   * collection of appliers.
266
   *
267
   * @param Pointer to the applier to detach
268
   */
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
269
  void detachApplier(plugin::TransactionApplier *in_applier);
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
270
  /**
1143.2.10 by Jay Pipes
Phase 2 new replication work:
271
   * Commits a normal transaction (see above) and pushes the
272
   * transaction message out to the replicators.
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
273
   *
274
   * @param Pointer to the Session committing the transaction
275
   */
1143.4.7 by Jay Pipes
Removes unused ReplicationServices::startNormalTransaction() and switches from while to for loop in evaluateActivePlugins().
276
  void commitTransaction(Session *in_session);
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
277
  /**
1143.2.10 by Jay Pipes
Phase 2 new replication work:
278
   * Marks the current active transaction message as being rolled
279
   * back and pushes the transaction message out to replicators.
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
280
   *
281
   * @param Pointer to the Session committing the transaction
282
   */
283
  void rollbackTransaction(Session *in_session);
284
  /**
1143.2.22 by Jay Pipes
Adds functionality to handle REPLACE statements correctly in the replication
285
   * Finalizes a Statement message and sets the Session's statement
286
   * message to NULL.
287
   *
288
   * @param The statement to initialize
289
   * @param The session processing this statement
290
   */
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
291
  void finalizeStatement(message::Statement &statement,
1143.2.22 by Jay Pipes
Adds functionality to handle REPLACE statements correctly in the replication
292
                         Session *in_session) const;
293
  /**
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
294
   * Creates a new InsertRecord GPB message and pushes it to
295
   * replicators.
296
   *
297
   * @param Pointer to the Session which has inserted a record
298
   * @param Pointer to the Table containing insert information
1143.4.15 by Jay Pipes
Adds error for when a record is inserted into a table containing
299
   *
300
   * Grr, returning "true" here on error because of the cursor
301
   * reversed bool return crap...fix that.
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
302
   */
1143.4.15 by Jay Pipes
Adds error for when a record is inserted into a table containing
303
  bool insertRecord(Session *in_session, Table *in_table);
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
304
  /**
305
   * Creates a new UpdateRecord GPB message and pushes it to
306
   * replicators.
307
   *
308
   * @param Pointer to the Session which has updated a record
309
   * @param Pointer to the Table containing update information
1039.5.11 by Jay Pipes
Adds multi-value INSERT test, modifies TransactionServices to support UPDATE statements (though WHERE clause still not done...)
310
   * @param Pointer to the raw bytes representing the old record/row
311
   * @param Pointer to the raw bytes representing the new record/row 
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
312
   */
1039.5.11 by Jay Pipes
Adds multi-value INSERT test, modifies TransactionServices to support UPDATE statements (though WHERE clause still not done...)
313
  void updateRecord(Session *in_session, 
314
                    Table *in_table, 
315
                    const unsigned char *old_record, 
316
                    const unsigned char *new_record);
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
317
  /**
318
   * Creates a new DeleteRecord GPB message and pushes it to
319
   * replicators.
320
   *
321
   * @param Pointer to the Session which has deleted a record
322
   * @param Pointer to the Table containing delete information
323
   */
324
  void deleteRecord(Session *in_session, Table *in_table);
325
  /**
1143.4.6 by Jay Pipes
Adds test case to transaction log for TRUNCATE TABLE.
326
   * Creates a TruncateTable Statement GPB message and add it
327
   * to the Session's active Transaction GPB message for pushing
328
   * out to the replicator streams.
329
   *
330
   * @param[in] Pointer to the Session which issued the statement
331
   * @param[in] The Table being truncated
332
   */
333
  void truncateTable(Session *in_session, Table *in_table);
334
  /**
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
335
   * Creates a new RawSql GPB message and pushes it to 
336
   * replicators.
337
   *
338
   * @TODO With a real data dictionary, this really shouldn't
339
   * be needed.  CREATE TABLE would map to insertRecord call
340
   * on the I_S, etc.  Not sure what to do with administrative
341
   * commands like CHECK TABLE, though..
342
   *
343
   * @param Pointer to the Session which issued the statement
344
   * @param Query string
345
   * @param Length of the query string
346
   */
347
  void rawStatement(Session *in_session, const char *in_query, size_t in_query_len);
1039.5.31 by Jay Pipes
This patch does a few things:
348
  /**
1143.2.10 by Jay Pipes
Phase 2 new replication work:
349
   * Returns the timestamp of the last Transaction which was sent to 
1039.5.31 by Jay Pipes
This patch does a few things:
350
   * an applier.
351
   */
352
  uint64_t getLastAppliedTimestamp() const;
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
353
};
354
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
355
} /* namespace drizzled */
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
356
1039.5.31 by Jay Pipes
This patch does a few things:
357
#endif /* DRIZZLED_REPLICATION_SERVICES_H */