~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/replication_services.h

  • Committer: Brian Aker
  • Date: 2009-08-15 00:59:30 UTC
  • mfrom: (1115.1.7 merge)
  • Revision ID: brian@gaz-20090815005930-q47yenjrq1esiwsz
Merge of Trond + Brian

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
4
 *  Copyright (C) 2008-2009 Sun Microsystems
5
 
 *  Copyright (c) 2009-2010 Jay Pipes <jaypipes@gmail.com>
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
 
 
30
28
#include <vector>
31
29
 
32
 
namespace drizzled
33
 
{
34
 
 
35
30
/* some forward declarations needed */
36
31
class Session;
37
32
class Table;
38
33
 
39
 
namespace plugin
40
 
{
41
 
  class TransactionReplicator;
42
 
  class TransactionApplier;
43
 
}
44
 
namespace message
45
 
{
46
 
  class Transaction;
47
 
}
 
34
namespace drizzled
 
35
{
 
36
  namespace plugin
 
37
  {
 
38
    class Replicator;
 
39
    class Applier;
 
40
  }
 
41
  namespace message
 
42
  {
 
43
    class Command;
 
44
  }
 
45
}
 
46
 
 
47
void add_replicator(drizzled::plugin::Replicator *replicator);
 
48
void remove_replicator(drizzled::plugin::Replicator *replicator);
 
49
 
 
50
void add_applier(drizzled::plugin::Applier *applier);
 
51
void remove_applier(drizzled::plugin::Applier *applier);
48
52
 
49
53
/**
50
54
 * This is a class which manages transforming internal 
51
55
 * transactional events into GPB messages and sending those
52
56
 * events out through registered replicators and appliers.
53
57
 */
 
58
namespace drizzled
 
59
{
54
60
class ReplicationServices
55
61
{
56
62
public:
57
 
  typedef uint64_t GlobalTransactionId;
58
 
  /**
59
 
   * Types of messages that can go in the transaction
60
 
   * log file.  Every time something is written into the
61
 
   * transaction log, it is preceded by a header containing
62
 
   * the type of message which follows.
63
 
   */
64
 
  enum MessageType
65
 
  {
66
 
    TRANSACTION= 1, /* A GPB Transaction Message */
67
 
    BLOB= 2 /* A BLOB value */
68
 
  };
69
 
  typedef std::vector<plugin::TransactionReplicator *> Replicators;
70
 
  typedef std::vector<plugin::TransactionApplier *> Appliers;
 
63
  static const size_t DEFAULT_RECORD_SIZE= 100;
71
64
private:
72
65
  /** 
73
66
   * Atomic boolean set to true if any *active* replicators
75
68
   */
76
69
  atomic<bool> is_active;
77
70
  /**
78
 
   * The timestamp of the last time a Transaction message was successfully
 
71
   * The timestamp of the last time a Command message was successfully
79
72
   * applied (sent to an Applier)
80
73
   */
81
74
  atomic<uint64_t> last_applied_timestamp;
82
75
  /** Our collection of replicator plugins */
83
 
  Replicators replicators;
 
76
  std::vector<drizzled::plugin::Replicator *> replicators;
84
77
  /** Our collection of applier plugins */
85
 
  Appliers appliers;
 
78
  std::vector<drizzled::plugin::Applier *> appliers;
86
79
  /**
87
80
   * Helper method which is called after any change in the
88
81
   * registered appliers or replicators to evaluate whether
91
84
   * This method properly sets the is_active member variable.
92
85
   */
93
86
  void evaluateActivePlugins();
94
 
public:
95
87
  /** 
96
 
   * Helper method which pushes a constructed message out to the registered
97
 
   * replicator and applier plugins.
 
88
   * Helper method which attaches a transaction context
 
89
   * the supplied command based on the supplied Session's
 
90
   * transaction information.
 
91
   */
 
92
  void setCommandTransactionContext(drizzled::message::Command *in_command, Session *in_session) const;
 
93
  /**
 
94
   * Helper method which pushes a constructed message out
 
95
   * to the registered replicator and applier plugins.
98
96
   *
99
97
   * @param Message to push out
100
98
   */
101
 
  void pushTransactionMessage(message::Transaction &to_push);
 
99
  void push(drizzled::message::Command *to_push);
 
100
public:
102
101
  /**
103
102
   * Constructor
104
103
   */
105
104
  ReplicationServices();
106
 
 
107
 
  /**
108
 
   * Singleton method
109
 
   * Returns the singleton instance of ReplicationServices
110
 
   */
111
 
  static inline ReplicationServices &singleton()
112
 
  {
113
 
    static ReplicationServices replication_services;
114
 
    return replication_services;
115
 
  }
116
 
 
117
105
  /**
118
106
   * Returns whether the ReplicationServices object
119
107
   * is active.  In other words, does it have both
126
114
   *
127
115
   * @param Pointer to a replicator to attach/register
128
116
   */
129
 
  void attachReplicator(plugin::TransactionReplicator *in_replicator);
 
117
  void attachReplicator(drizzled::plugin::Replicator *in_replicator);
130
118
  /**
131
119
   * Detaches/unregisters a replicator with our internal
132
120
   * collection of replicators.
133
121
   *
134
122
   * @param Pointer to the replicator to detach
135
123
   */
136
 
  void detachReplicator(plugin::TransactionReplicator *in_replicator);
 
124
  void detachReplicator(drizzled::plugin::Replicator *in_replicator);
137
125
  /**
138
126
   * Attaches a applier to our internal collection of
139
127
   * appliers.
140
128
   *
141
129
   * @param Pointer to a applier to attach/register
142
130
   */
143
 
  void attachApplier(plugin::TransactionApplier *in_applier);
 
131
  void attachApplier(drizzled::plugin::Applier *in_applier);
144
132
  /**
145
133
   * Detaches/unregisters a applier with our internal
146
134
   * collection of appliers.
147
135
   *
148
136
   * @param Pointer to the applier to detach
149
137
   */
150
 
  void detachApplier(plugin::TransactionApplier *in_applier);
151
 
  /** Returns the timestamp of the last Transaction which was sent to an
152
 
   * applier.
 
138
  void detachApplier(drizzled::plugin::Applier *in_applier);
 
139
  /**
 
140
   * Creates a new StartTransaction GPB message and pushes
 
141
   * it to replicators.
 
142
   *
 
143
   * @param Pointer to the Session starting the transaction
 
144
   */
 
145
  void startTransaction(Session *in_session);
 
146
  /**
 
147
   * Creates a new CommitTransaction GPB message and pushes
 
148
   * it to replicators.
 
149
   *
 
150
   * @param Pointer to the Session committing the transaction
 
151
   */
 
152
  void commitTransaction(Session *in_session);
 
153
  /**
 
154
   * Creates a new RollbackTransaction GPB message and pushes
 
155
   * it to replicators.
 
156
   *
 
157
   * @param Pointer to the Session committing the transaction
 
158
   */
 
159
  void rollbackTransaction(Session *in_session);
 
160
  /**
 
161
   * Creates a new InsertRecord GPB message and pushes it to
 
162
   * replicators.
 
163
   *
 
164
   * @param Pointer to the Session which has inserted a record
 
165
   * @param Pointer to the Table containing insert information
 
166
   */
 
167
  void insertRecord(Session *in_session, Table *in_table);
 
168
  /**
 
169
   * Creates a new UpdateRecord GPB message and pushes it to
 
170
   * replicators.
 
171
   *
 
172
   * @param Pointer to the Session which has updated a record
 
173
   * @param Pointer to the Table containing update information
 
174
   * @param Pointer to the raw bytes representing the old record/row
 
175
   * @param Pointer to the raw bytes representing the new record/row 
 
176
   */
 
177
  void updateRecord(Session *in_session, 
 
178
                    Table *in_table, 
 
179
                    const unsigned char *old_record, 
 
180
                    const unsigned char *new_record);
 
181
  /**
 
182
   * Creates a new DeleteRecord GPB message and pushes it to
 
183
   * replicators.
 
184
   *
 
185
   * @param Pointer to the Session which has deleted a record
 
186
   * @param Pointer to the Table containing delete information
 
187
   */
 
188
  void deleteRecord(Session *in_session, Table *in_table);
 
189
  /**
 
190
   * Creates a new RawSql GPB message and pushes it to 
 
191
   * replicators.
 
192
   *
 
193
   * @TODO With a real data dictionary, this really shouldn't
 
194
   * be needed.  CREATE TABLE would map to insertRecord call
 
195
   * on the I_S, etc.  Not sure what to do with administrative
 
196
   * commands like CHECK TABLE, though..
 
197
   *
 
198
   * @param Pointer to the Session which issued the statement
 
199
   * @param Query string
 
200
   * @param Length of the query string
 
201
   */
 
202
  void rawStatement(Session *in_session, const char *in_query, size_t in_query_len);
 
203
  /**
 
204
   * Returns the timestamp of the last Command which was sent to 
 
205
   * an applier.
153
206
   */
154
207
  uint64_t getLastAppliedTimestamp() const;
155
208
};
156
209
 
157
 
} /* namespace drizzled */
 
210
} /* end namespace drizzled */
158
211
 
159
212
#endif /* DRIZZLED_REPLICATION_SERVICES_H */