~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/replication_services.h

Reworked getTableNames() interface. Way simpler, less mallocs....

Added a straight vector<> for use with storage engines.

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>
 
4
 *  Copyright (C) 2008-2009 Sun Microsystems
6
5
 *
7
6
 *  Authors:
8
7
 *
9
 
 *    Jay Pipes <jaypipes@gmail.com>
 
8
 *    Jay Pipes <joinfu@sun.com>
10
9
 *
11
10
 *  This program is free software; you can redistribute it and/or modify
12
11
 *  it under the terms of the GNU General Public License as published by
26
25
#define DRIZZLED_REPLICATION_SERVICES_H
27
26
 
28
27
#include "drizzled/atomics.h"
29
 
#include "drizzled/plugin/replication.h"
30
 
 
31
 
#include <string>
32
28
#include <vector>
33
 
#include <utility>
34
 
 
35
 
#include "drizzled/visibility.h"
36
 
 
37
 
namespace drizzled
38
 
{
39
29
 
40
30
/* some forward declarations needed */
41
31
class Session;
42
32
class Table;
43
33
 
44
 
namespace plugin
45
 
{
46
 
  class TransactionReplicator;
47
 
  class TransactionApplier;
48
 
}
49
 
namespace message
50
 
{
51
 
  class Transaction;
52
 
}
 
34
namespace drizzled
 
35
{
 
36
  namespace plugin
 
37
  {
 
38
    class CommandReplicator;
 
39
    class CommandApplier;
 
40
  }
 
41
  namespace message
 
42
  {
 
43
    class Command;
 
44
  }
 
45
 
53
46
 
54
47
/**
55
48
 * This is a class which manages transforming internal 
56
49
 * transactional events into GPB messages and sending those
57
50
 * events out through registered replicators and appliers.
58
51
 */
59
 
class DRIZZLED_API ReplicationServices
 
52
class ReplicationServices
60
53
{
61
54
public:
 
55
  static const size_t DEFAULT_RECORD_SIZE= 100;
62
56
  typedef uint64_t GlobalTransactionId;
63
 
  /**
64
 
   * Types of messages that can go in the transaction
65
 
   * log file.  Every time something is written into the
66
 
   * transaction log, it is preceded by a header containing
67
 
   * the type of message which follows.
68
 
   */
69
 
  enum MessageType
70
 
  {
71
 
    TRANSACTION= 1, /* A GPB Transaction Message */
72
 
    BLOB= 2 /* A BLOB value */
73
 
  };
74
 
  typedef std::pair<plugin::TransactionReplicator *, plugin::TransactionApplier *> ReplicationPair;
75
 
  typedef std::vector<ReplicationPair> ReplicationStreams;
76
 
  /**
77
 
   * Method which is called after plugins have been loaded but
78
 
   * before the first client connects.  It determines if the registration
79
 
   * of applier and replicator plugins is proper and pairs
80
 
   * the applier and requested replicator plugins into the replication
81
 
   * streams.
82
 
   *
83
 
   * @todo
84
 
   *
85
 
   * This is only necessary because we don't yet have plugin dependency
86
 
   * tracking...
87
 
   */
88
 
  bool evaluateRegisteredPlugins();
89
 
  /** 
90
 
   * Helper method which pushes a constructed message out to the registered
91
 
   * replicator and applier plugins.
92
 
   *
93
 
   * @param Session descriptor
 
57
private:
 
58
  /** 
 
59
   * Atomic boolean set to true if any *active* replicators
 
60
   * or appliers are actually registered.
 
61
   */
 
62
  atomic<bool> is_active;
 
63
  /**
 
64
   * The timestamp of the last time a Command message was successfully
 
65
   * applied (sent to an Applier)
 
66
   */
 
67
  atomic<uint64_t> last_applied_timestamp;
 
68
  /** Our collection of replicator plugins */
 
69
  std::vector<drizzled::plugin::CommandReplicator *> replicators;
 
70
  /** Our collection of applier plugins */
 
71
  std::vector<drizzled::plugin::CommandApplier *> appliers;
 
72
  /**
 
73
   * Helper method which is called after any change in the
 
74
   * registered appliers or replicators to evaluate whether
 
75
   * any remaining plugins are actually active.
 
76
   * 
 
77
   * This method properly sets the is_active member variable.
 
78
   */
 
79
  void evaluateActivePlugins();
 
80
  /** 
 
81
   * Helper method which attaches a transaction context
 
82
   * the supplied command based on the supplied Session's
 
83
   * transaction information.
 
84
   */
 
85
  void setCommandTransactionContext(drizzled::message::Command &in_command, Session *in_session) const;
 
86
  /**
 
87
   * Helper method which pushes a constructed message out
 
88
   * to the registered replicator and applier plugins.
 
89
   *
94
90
   * @param Message to push out
95
91
   */
96
 
  plugin::ReplicationReturnCode pushTransactionMessage(Session &in_session,
97
 
                                                       message::Transaction &to_push);
 
92
  void push(drizzled::message::Command &to_push);
 
93
public:
98
94
  /**
99
95
   * Constructor
100
96
   */
116
112
   * a replicator and an applier that are *active*?
117
113
   */
118
114
  bool isActive() const;
119
 
 
120
 
  /**
121
 
   * Returns the list of replication streams
122
 
   */
123
 
  ReplicationStreams &getReplicationStreams();
124
 
 
125
115
  /**
126
116
   * Attaches a replicator to our internal collection of
127
117
   * replicators.
128
118
   *
129
119
   * @param Pointer to a replicator to attach/register
130
120
   */
131
 
  void attachReplicator(plugin::TransactionReplicator *in_replicator);
132
 
  
 
121
  void attachReplicator(drizzled::plugin::CommandReplicator *in_replicator);
133
122
  /**
134
123
   * Detaches/unregisters a replicator with our internal
135
124
   * collection of replicators.
136
125
   *
137
126
   * @param Pointer to the replicator to detach
138
127
   */
139
 
  void detachReplicator(plugin::TransactionReplicator *in_replicator);
140
 
  
 
128
  void detachReplicator(drizzled::plugin::CommandReplicator *in_replicator);
141
129
  /**
142
130
   * Attaches a applier to our internal collection of
143
131
   * appliers.
144
132
   *
145
133
   * @param Pointer to a applier to attach/register
146
 
   * @param The name of the replicator to pair with
147
134
   */
148
 
  void attachApplier(plugin::TransactionApplier *in_applier, const std::string &requested_replicator);
149
 
  
 
135
  void attachApplier(drizzled::plugin::CommandApplier *in_applier);
150
136
  /**
151
137
   * Detaches/unregisters a applier with our internal
152
138
   * collection of appliers.
153
139
   *
154
140
   * @param Pointer to the applier to detach
155
141
   */
156
 
  void detachApplier(plugin::TransactionApplier *in_applier);
157
 
 
158
 
  /** 
159
 
   * Returns the timestamp of the last Transaction which was sent to an
160
 
   * applier.
 
142
  void detachApplier(drizzled::plugin::CommandApplier *in_applier);
 
143
  /**
 
144
   * Creates a new StartTransaction GPB message and pushes
 
145
   * it to replicators.
 
146
   *
 
147
   * @param Pointer to the Session starting the transaction
 
148
   */
 
149
  void startTransaction(Session *in_session);
 
150
  /**
 
151
   * Creates a new CommitTransaction GPB message and pushes
 
152
   * it to replicators.
 
153
   *
 
154
   * @param Pointer to the Session committing the transaction
 
155
   */
 
156
  void commitTransaction(Session *in_session);
 
157
  /**
 
158
   * Creates a new RollbackTransaction GPB message and pushes
 
159
   * it to replicators.
 
160
   *
 
161
   * @param Pointer to the Session committing the transaction
 
162
   */
 
163
  void rollbackTransaction(Session *in_session);
 
164
  /**
 
165
   * Creates a new InsertRecord GPB message and pushes it to
 
166
   * replicators.
 
167
   *
 
168
   * @param Pointer to the Session which has inserted a record
 
169
   * @param Pointer to the Table containing insert information
 
170
   */
 
171
  void insertRecord(Session *in_session, Table *in_table);
 
172
  /**
 
173
   * Creates a new UpdateRecord GPB message and pushes it to
 
174
   * replicators.
 
175
   *
 
176
   * @param Pointer to the Session which has updated a record
 
177
   * @param Pointer to the Table containing update information
 
178
   * @param Pointer to the raw bytes representing the old record/row
 
179
   * @param Pointer to the raw bytes representing the new record/row 
 
180
   */
 
181
  void updateRecord(Session *in_session, 
 
182
                    Table *in_table, 
 
183
                    const unsigned char *old_record, 
 
184
                    const unsigned char *new_record);
 
185
  /**
 
186
   * Creates a new DeleteRecord GPB message and pushes it to
 
187
   * replicators.
 
188
   *
 
189
   * @param Pointer to the Session which has deleted a record
 
190
   * @param Pointer to the Table containing delete information
 
191
   */
 
192
  void deleteRecord(Session *in_session, Table *in_table);
 
193
  /**
 
194
   * Creates a new RawSql GPB message and pushes it to 
 
195
   * replicators.
 
196
   *
 
197
   * @TODO With a real data dictionary, this really shouldn't
 
198
   * be needed.  CREATE TABLE would map to insertRecord call
 
199
   * on the I_S, etc.  Not sure what to do with administrative
 
200
   * commands like CHECK TABLE, though..
 
201
   *
 
202
   * @param Pointer to the Session which issued the statement
 
203
   * @param Query string
 
204
   * @param Length of the query string
 
205
   */
 
206
  void rawStatement(Session *in_session, const char *in_query, size_t in_query_len);
 
207
  /**
 
208
   * Returns the timestamp of the last Command which was sent to 
 
209
   * an applier.
161
210
   */
162
211
  uint64_t getLastAppliedTimestamp() const;
163
 
private:
164
 
  typedef std::vector<plugin::TransactionReplicator *> Replicators;
165
 
  typedef std::vector<std::pair<std::string, plugin::TransactionApplier *> > Appliers;
166
 
  /** 
167
 
   * Atomic boolean set to true if any *active* replicators
168
 
   * or appliers are actually registered.
169
 
   */
170
 
  bool is_active;
171
 
  /**
172
 
   * The timestamp of the last time a Transaction message was successfully
173
 
   * applied (sent to an Applier)
174
 
   */
175
 
  atomic<uint64_t> last_applied_timestamp;
176
 
  /** Our collection of registered replicator plugins */
177
 
  Replicators replicators;
178
 
  /** Our collection of registered applier plugins and their requested replicator plugin names */
179
 
  Appliers appliers;
180
 
  /** Our replication streams */
181
 
  ReplicationStreams replication_streams;
182
 
  /**
183
 
   * Strips underscores and lowercases supplied replicator name
184
 
   * or requested name, and appends the suffix "replicator" if missing...
185
 
   */
186
 
  void normalizeReplicatorName(std::string &name);
187
212
};
188
213
 
189
 
} /* namespace drizzled */
 
214
} /* end namespace drizzled */
190
215
 
191
216
#endif /* DRIZZLED_REPLICATION_SERVICES_H */