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