40
40
using namespace drizzled;
41
41
using namespace google;
43
namespace drizzle_plugin
44
* The hostname to connect to
46
static char* sysvar_rabbitmq_host= NULL;
49
static port_constraint sysvar_rabbitmq_port;
52
RabbitMQLog::RabbitMQLog(const string &name,
53
const std::string &exchange,
54
const std::string &routingkey,
55
RabbitMQHandler* mqHandler) :
56
plugin::TransactionApplier(name),
57
_rabbitMQHandler(mqHandler),
59
_routingkey(routingkey)
51
static int sysvar_rabbitmq_port= 0;
56
static char* sysvar_rabbitmq_username= NULL;
61
static char* sysvar_rabbitmq_password= NULL;
64
* rabbitmq virtualhost
66
static char* sysvar_rabbitmq_virtualhost= NULL;
69
* rabbitmq exchangename
71
static char* sysvar_rabbitmq_exchange= NULL;
74
* rabbitmq routing key
76
static char* sysvar_rabbitmq_routingkey= NULL;
79
* Is the rabbitmq log enabled?
81
static bool sysvar_rabbitmq_log_enabled= false;
84
* The name of the replicator plugin
85
* to pair the rabbitmq log's applier with.
86
* Defaults to "default"
88
static char *sysvar_rabbitmq_use_replicator= NULL;
89
static const char DEFAULT_USE_REPLICATOR[]= "default";
92
RabbitMQLog::RabbitMQLog(const string name_arg,
93
RabbitMQHandler* mqHandler)
94
:plugin::TransactionApplier(name_arg)
96
rabbitMQHandler= mqHandler;
62
99
RabbitMQLog::~RabbitMQLog()
65
103
plugin::ReplicationReturnCode
66
104
RabbitMQLog::apply(Session &, const message::Transaction &to_apply)
68
106
size_t message_byte_length= to_apply.ByteSize();
69
uint8_t* buffer= new uint8_t[message_byte_length];
107
uint8_t* buffer= static_cast<uint8_t *>(malloc(message_byte_length));
70
108
if(buffer == NULL)
72
errmsg_printf(error::ERROR, _("Failed to allocate enough memory to transaction message\n"));
110
errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to allocate enough memory to transaction message\n"));
74
112
return plugin::UNKNOWN_ERROR;
103
141
static int init(drizzled::module::Context &context)
105
const module::option_map &vm= context.getOptions();
109
rabbitmqHandler= new RabbitMQHandler(vm["host"].as<string>(),
110
sysvar_rabbitmq_port,
111
vm["username"].as<string>(),
112
vm["password"].as<string>(),
113
vm["virtualhost"].as<string>());
117
errmsg_printf(error::ERROR, _("Failed to allocate the RabbitMQHandler. Got error: %s\n"),
123
rabbitmqLogger= new RabbitMQLog("rabbit_log_applier",
124
vm["exchange"].as<string>(),
125
vm["routingkey"].as<string>(),
130
errmsg_printf(error::ERROR, _("Failed to allocate the RabbitMQLog instance. Got error: %s\n"),
135
context.add(rabbitmqLogger);
136
ReplicationServices &replication_services= ReplicationServices::singleton();
137
replication_services.attachApplier(rabbitmqLogger, vm["use-replicator"].as<string>());
139
context.registerVariable(new sys_var_const_string_val("host", vm["host"].as<string>()));
140
context.registerVariable(new sys_var_constrained_value_readonly<in_port_t>("port", sysvar_rabbitmq_port));
141
context.registerVariable(new sys_var_const_string_val("username", vm["username"].as<string>()));
142
context.registerVariable(new sys_var_const_string_val("password", vm["password"].as<string>()));
143
context.registerVariable(new sys_var_const_string_val("virtualhost", vm["virtualhost"].as<string>()));
144
context.registerVariable(new sys_var_const_string_val("exchange", vm["exchange"].as<string>()));
145
context.registerVariable(new sys_var_const_string_val("routingkey", vm["routingkey"].as<string>()));
143
if(sysvar_rabbitmq_log_enabled)
147
rabbitmqHandler= new RabbitMQHandler(sysvar_rabbitmq_host,
148
sysvar_rabbitmq_port,
149
sysvar_rabbitmq_username,
150
sysvar_rabbitmq_password,
151
sysvar_rabbitmq_virtualhost);
155
errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to allocate the RabbitMQHandler. Got error: %s\n"),
161
rabbitmqLogger= new RabbitMQLog("rabbit_log_applier", rabbitmqHandler);
165
errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to allocate the RabbitMQLog instance. Got error: %s\n"),
170
context.add(rabbitmqLogger);
171
ReplicationServices &replication_services= ReplicationServices::singleton();
172
string replicator_name(sysvar_rabbitmq_use_replicator);
173
replication_services.attachApplier(rabbitmqLogger, replicator_name);
180
static DRIZZLE_SYSVAR_BOOL(enable,
181
sysvar_rabbitmq_log_enabled,
183
N_("Enable rabbitmq log"),
184
NULL, /* check func */
185
NULL, /* update func */
186
false /* default */);
189
static DRIZZLE_SYSVAR_STR(hostname,
190
sysvar_rabbitmq_host,
192
N_("Host name to connect to"),
193
NULL, /* check func */
194
NULL, /* update func*/
195
"localhost" /* default */);
198
static DRIZZLE_SYSVAR_INT(port,
199
sysvar_rabbitmq_port,
202
NULL, /* check func */
203
NULL, /* update func */
209
static DRIZZLE_SYSVAR_STR(username,
210
sysvar_rabbitmq_username,
212
N_("RabbitMQ username"),
213
NULL, /* check func */
214
NULL, /* update func*/
215
"guest" /* default */);
217
static DRIZZLE_SYSVAR_STR(password,
218
sysvar_rabbitmq_password,
220
N_("RabbitMQ password"),
221
NULL, /* check func */
222
NULL, /* update func*/
223
"guest" /* default */);
225
static DRIZZLE_SYSVAR_STR(virtualhost,
226
sysvar_rabbitmq_virtualhost,
228
N_("RabbitMQ virtualhost"),
229
NULL, /* check func */
230
NULL, /* update func*/
233
static DRIZZLE_SYSVAR_STR(exchange,
234
sysvar_rabbitmq_exchange,
236
N_("Name of RabbitMQ exchange to publish to"),
237
NULL, /* check func */
238
NULL, /* update func*/
239
"ReplicationExchange" /* default */);
241
static DRIZZLE_SYSVAR_STR(routingkey,
242
sysvar_rabbitmq_routingkey,
244
N_("Name of RabbitMQ routing key to use"),
245
NULL, /* check func */
246
NULL, /* update func*/
247
"ReplicationRoutingKey" /* default */);
249
static DRIZZLE_SYSVAR_STR(use_replicator,
250
sysvar_rabbitmq_use_replicator,
252
N_("Name of the replicator plugin to use (default='default_replicator')"),
253
NULL, /* check func */
254
NULL, /* update func*/
255
DEFAULT_USE_REPLICATOR /* default */);
151
257
static void init_options(drizzled::module::option_context &context)
154
po::value<string>()->default_value("localhost"),
155
_("Host name to connect to"));
157
po::value<port_constraint>(&sysvar_rabbitmq_port)->default_value(5672),
158
_("Port to connect to"));
159
context("virtualhost",
160
po::value<string>()->default_value("/"),
161
_("RabbitMQ virtualhost"));
163
po::value<string>()->default_value("guest"),
164
_("RabbitMQ username"));
166
po::value<string>()->default_value("guest"),
167
_("RabbitMQ password"));
168
context("use-replicator",
169
po::value<string>()->default_value("default_replicator"),
170
_("Name of the replicator plugin to use (default='default_replicator')"));
172
po::value<string>()->default_value("ReplicationExchange"),
173
_("Name of RabbitMQ exchange to publish to"));
174
context("routingkey",
175
po::value<string>()->default_value("ReplicationRoutingKey"),
176
_("Name of RabbitMQ routing key to use"));
260
po::value<bool>(&sysvar_rabbitmq_log_enabled)->default_value(false)->zero_tokens(),
261
N_("Enable rabbitmq log"));
179
} /* namespace drizzle_plugin */
264
static drizzle_sys_var* system_variables[]= {
265
DRIZZLE_SYSVAR(enable),
266
DRIZZLE_SYSVAR(hostname),
267
DRIZZLE_SYSVAR(port),
268
DRIZZLE_SYSVAR(username),
269
DRIZZLE_SYSVAR(password),
270
DRIZZLE_SYSVAR(virtualhost),
271
DRIZZLE_SYSVAR(exchange),
272
DRIZZLE_SYSVAR(routingkey),
273
DRIZZLE_SYSVAR(use_replicator),
181
DRIZZLE_PLUGIN(drizzle_plugin::init, NULL, drizzle_plugin::init_options);
277
DRIZZLE_PLUGIN(init, system_variables, init_options);