1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008-2009 Sun Microsystems
8
* Jay Pipes <joinfu@sun.com>
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.
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.
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
24
#ifndef DRIZZLED_REPLICATION_SERVICES_H
25
#define DRIZZLED_REPLICATION_SERVICES_H
27
#include "drizzled/atomics.h"
29
#include "drizzled/message/transaction.pb.h"
33
/* some forward declarations needed */
41
class TransactionReplicator;
42
class TransactionApplier;
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.
50
class ReplicationServices
53
static const size_t DEFAULT_RECORD_SIZE= 100;
54
typedef uint64_t GlobalTransactionId;
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.
63
TRANSACTION= 1, /* A GPB Transaction Message */
64
BLOB= 2 /* A BLOB value */
66
typedef std::vector<plugin::TransactionReplicator *> Replicators;
67
typedef std::vector<plugin::TransactionApplier *> Appliers;
70
* Atomic boolean set to true if any *active* replicators
71
* or appliers are actually registered.
73
atomic<bool> is_active;
75
* The timestamp of the last time a Transaction message was successfully
76
* applied (sent to an Applier)
78
atomic<uint64_t> last_applied_timestamp;
79
/** Our collection of replicator plugins */
80
Replicators replicators;
81
/** Our collection of applier plugins */
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.
88
* This method properly sets the is_active member variable.
90
void evaluateActivePlugins();
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.
96
* @param The session processing the transaction
98
drizzled::message::Transaction *getActiveTransaction(Session *in_session) const;
100
* Helper method which attaches a transaction context
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
105
* @param The transaction message to initialize
106
* @param The Session processing this transaction
108
void initTransaction(drizzled::message::Transaction &in_command, Session *in_session) const;
110
* Helper method which finalizes data members for the
111
* supplied transaction's context.
113
* @param The transaction message to finalize
114
* @param The Session processing this transaction
116
void finalizeTransaction(drizzled::message::Transaction &in_command, Session *in_session) const;
118
* Helper method which deletes transaction memory and
119
* unsets Session's transaction and statement messages.
121
void cleanupTransaction(message::Transaction *in_transaction,
122
Session *in_session) const;
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).
128
* @param The transaction to check
130
bool transactionContainsBulkSegment(const drizzled::message::Transaction &transaction) const;
132
* Helper method which initializes a Statement message
134
* @param The statement to initialize
135
* @param The type of the statement
136
* @param The session processing this statement
138
void initStatement(drizzled::message::Statement &statement,
139
drizzled::message::Statement::Type in_type,
140
Session *in_session) const;
142
* Helper method which returns an initialized Statement
143
* message for methods doing insertion of data.
145
* @param[in] Pointer to the Session doing the processing
146
* @param[in] Pointer to the Table object being inserted into
148
message::Statement &getInsertStatement(Session *in_session,
149
Table *in_table) const;
152
* Helper method which initializes the header message for
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
159
void setInsertHeader(message::Statement &statement,
161
Table *in_table) const;
163
* Helper method which returns an initialized Statement
164
* message for methods doing updates of data.
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
171
message::Statement &getUpdateStatement(Session *in_session,
173
const unsigned char *old_record,
174
const unsigned char *new_record) const;
176
* Helper method which initializes the header message for
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
185
void setUpdateHeader(message::Statement &statement,
188
const unsigned char *old_record,
189
const unsigned char *new_record) const;
191
* Helper method which returns an initialized Statement
192
* message for methods doing deletion of data.
194
* @param[in] Pointer to the Session doing the processing
195
* @param[in] Pointer to the Table object being deleted from
197
message::Statement &getDeleteStatement(Session *in_session,
198
Table *in_table) const;
201
* Helper method which initializes the header message for
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
208
void setDeleteHeader(message::Statement &statement,
210
Table *in_table) const;
212
* Helper method which pushes a constructed message out
213
* to the registered replicator and applier plugins.
215
* @param Message to push out
217
void push(drizzled::message::Transaction &to_push);
222
ReplicationServices();
226
* Returns the singleton instance of ReplicationServices
228
static inline ReplicationServices &singleton()
230
static ReplicationServices replication_services;
231
return replication_services;
235
* Returns whether the ReplicationServices object
236
* is active. In other words, does it have both
237
* a replicator and an applier that are *active*?
239
bool isActive() const;
241
* Attaches a replicator to our internal collection of
244
* @param Pointer to a replicator to attach/register
246
void attachReplicator(drizzled::plugin::TransactionReplicator *in_replicator);
248
* Detaches/unregisters a replicator with our internal
249
* collection of replicators.
251
* @param Pointer to the replicator to detach
253
void detachReplicator(drizzled::plugin::TransactionReplicator *in_replicator);
255
* Attaches a applier to our internal collection of
258
* @param Pointer to a applier to attach/register
260
void attachApplier(drizzled::plugin::TransactionApplier *in_applier);
262
* Detaches/unregisters a applier with our internal
263
* collection of appliers.
265
* @param Pointer to the applier to detach
267
void detachApplier(drizzled::plugin::TransactionApplier *in_applier);
269
* Commits a normal transaction (see above) and pushes the
270
* transaction message out to the replicators.
272
* @param Pointer to the Session committing the transaction
274
void commitTransaction(Session *in_session);
276
* Marks the current active transaction message as being rolled
277
* back and pushes the transaction message out to replicators.
279
* @param Pointer to the Session committing the transaction
281
void rollbackTransaction(Session *in_session);
283
* Finalizes a Statement message and sets the Session's statement
286
* @param The statement to initialize
287
* @param The session processing this statement
289
void finalizeStatement(drizzled::message::Statement &statement,
290
Session *in_session) const;
292
* Creates a new InsertRecord GPB message and pushes it to
295
* @param Pointer to the Session which has inserted a record
296
* @param Pointer to the Table containing insert information
298
* Grr, returning "true" here on error because of the cursor
299
* reversed bool return crap...fix that.
301
bool insertRecord(Session *in_session, Table *in_table);
303
* Creates a new UpdateRecord GPB message and pushes it to
306
* @param Pointer to the Session which has updated a record
307
* @param Pointer to the Table containing update information
308
* @param Pointer to the raw bytes representing the old record/row
309
* @param Pointer to the raw bytes representing the new record/row
311
void updateRecord(Session *in_session,
313
const unsigned char *old_record,
314
const unsigned char *new_record);
316
* Creates a new DeleteRecord GPB message and pushes it to
319
* @param Pointer to the Session which has deleted a record
320
* @param Pointer to the Table containing delete information
322
void deleteRecord(Session *in_session, Table *in_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.
328
* @param[in] Pointer to the Session which issued the statement
329
* @param[in] The Table being truncated
331
void truncateTable(Session *in_session, Table *in_table);
333
* Creates a new RawSql GPB message and pushes it to
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..
341
* @param Pointer to the Session which issued the statement
342
* @param Query string
343
* @param Length of the query string
345
void rawStatement(Session *in_session, const char *in_query, size_t in_query_len);
347
* Returns the timestamp of the last Transaction which was sent to
350
uint64_t getLastAppliedTimestamp() const;
353
} /* end namespace drizzled */
355
#endif /* DRIZZLED_REPLICATION_SERVICES_H */