39
39
#include "drizzled/plugin/transaction_replicator.h"
40
40
#include "drizzled/plugin/transaction_applier.h"
41
41
#include "drizzled/message/transaction.pb.h"
42
#include "drizzled/message/table.pb.h"
43
#include "drizzled/message/statement_transform.h"
44
42
#include "drizzled/gettext.h"
45
43
#include "drizzled/session.h"
46
44
#include "drizzled/error.h"
50
50
using namespace std;
55
ReplicationServices::ReplicationServices()
55
ReplicationServices::ReplicationServices() :
60
void ReplicationServices::evaluateActivePlugins()
60
void ReplicationServices::normalizeReplicatorName(string &name)
63
* We loop through replicators and appliers, evaluating
64
* whether or not there is at least one active replicator
65
* and one active applier. If not, we set is_active
68
bool tmp_is_active= false;
70
if (replicators.empty() || appliers.empty())
77
* Determine if any remaining replicators and if those
78
* replicators are active...if not, set is_active
81
for (Replicators::iterator repl_iter= replicators.begin();
82
repl_iter != replicators.end();
85
if ((*repl_iter)->isEnabled())
62
transform(name.begin(),
66
if (name.find("replicator") == string::npos)
67
name.append("replicator", 10);
69
size_t found_underscore= name.find('_');
70
while (found_underscore != string::npos)
72
name.erase(found_underscore, 1);
73
found_underscore= name.find('_');
93
/* No active replicators. Set is_active to false and exit. */
78
bool ReplicationServices::evaluateRegisteredPlugins()
99
* OK, we know there's at least one active replicator.
101
* Now determine if any remaining replicators and if those
102
* replicators are active...if not, set is_active
81
* We loop through appliers that have registered with us
82
* and attempts to pair the applier with its requested
83
* replicator. If an applier has requested a replicator
84
* that has either not been built or has not registered
85
* with the replication services, we print an error and
91
if (replicators.empty() && not appliers.empty())
93
errmsg_printf(error::ERROR,
94
N_("You registered a TransactionApplier plugin but no "
95
"TransactionReplicator plugins were registered.\n"));
105
99
for (Appliers::iterator appl_iter= appliers.begin();
106
100
appl_iter != appliers.end();
109
if ((*appl_iter)->isEnabled())
103
plugin::TransactionApplier *applier= (*appl_iter).second;
104
string requested_replicator_name= (*appl_iter).first;
105
normalizeReplicatorName(requested_replicator_name);
108
Replicators::iterator repl_iter;
109
for (repl_iter= replicators.begin();
110
repl_iter != replicators.end();
113
string replicator_name= (*repl_iter)->getName();
114
normalizeReplicatorName(replicator_name);
116
if (requested_replicator_name.compare(replicator_name) == 0)
124
errmsg_printf(error::ERROR,
125
N_("You registered a TransactionApplier plugin but no "
126
"TransactionReplicator plugins were registered that match the "
127
"requested replicator name of '%s'.\n"
128
"We have deactivated the TransactionApplier '%s'.\n"),
129
requested_replicator_name.c_str(),
130
applier->getName().c_str());
131
applier->deactivate();
136
replication_streams.push_back(make_pair(*repl_iter, applier));
115
/* If we get here, there are no active appliers */
119
143
void ReplicationServices::attachReplicator(plugin::TransactionReplicator *in_replicator)
121
145
replicators.push_back(in_replicator);
122
evaluateActivePlugins();
125
148
void ReplicationServices::detachReplicator(plugin::TransactionReplicator *in_replicator)
127
150
replicators.erase(std::find(replicators.begin(), replicators.end(), in_replicator));
128
evaluateActivePlugins();
131
void ReplicationServices::attachApplier(plugin::TransactionApplier *in_applier)
133
appliers.push_back(in_applier);
134
evaluateActivePlugins();
137
void ReplicationServices::detachApplier(plugin::TransactionApplier *in_applier)
139
appliers.erase(std::find(appliers.begin(), appliers.end(), in_applier));
140
evaluateActivePlugins();
153
void ReplicationServices::attachApplier(plugin::TransactionApplier *in_applier, const string &requested_replicator_name)
155
appliers.push_back(make_pair(requested_replicator_name, in_applier));
158
void ReplicationServices::detachApplier(plugin::TransactionApplier *)
143
162
bool ReplicationServices::isActive() const
145
164
return is_active;
148
void ReplicationServices::pushTransactionMessage(message::Transaction &to_push)
167
plugin::ReplicationReturnCode ReplicationServices::pushTransactionMessage(Session &in_session,
168
message::Transaction &to_push)
150
vector<plugin::TransactionReplicator *>::iterator repl_iter= replicators.begin();
151
vector<plugin::TransactionApplier *>::iterator appl_start_iter, appl_iter;
152
appl_start_iter= appliers.begin();
154
plugin::TransactionReplicator *cur_repl;
155
plugin::TransactionApplier *cur_appl;
157
while (repl_iter != replicators.end())
170
plugin::ReplicationReturnCode result= plugin::SUCCESS;
172
for (ReplicationStreams::iterator iter= replication_streams.begin();
173
iter != replication_streams.end();
159
cur_repl= *repl_iter;
160
if (! cur_repl->isEnabled())
166
appl_iter= appl_start_iter;
167
while (appl_iter != appliers.end())
169
cur_appl= *appl_iter;
171
if (! cur_appl->isEnabled())
177
cur_repl->replicate(cur_appl, to_push);
176
plugin::TransactionReplicator *cur_repl= (*iter).first;
177
plugin::TransactionApplier *cur_appl= (*iter).second;
179
result= cur_repl->replicate(cur_appl, in_session, to_push);
181
if (result == plugin::SUCCESS)
180
184
* We update the timestamp for the last applied Transaction so that
181
185
* publisher plugins can ask the replication services when the