~drizzle-trunk/drizzle/development

1283.2.2 by Marcus Eriksson
add copyright headers, change load_by_default to no, include config.h, change string initialization to () style
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
1283.2.1 by Marcus Eriksson
rabbitmq replication applier
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
1283.2.2 by Marcus Eriksson
add copyright headers, change load_by_default to no, include config.h, change string initialization to () style
3
 *
4
 *  Copyright (C) 2010 Marcus Eriksson
5
 *
6
 *  Authors:
7
 *
8
 *  Marcus Eriksson <krummas@gmail.com>
9
 *
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; either version 2 of the License, or
13
 *  (at your option) any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program; if not, write to the Free Software
22
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1283.2.1 by Marcus Eriksson
rabbitmq replication applier
23
 */
1283.2.2 by Marcus Eriksson
add copyright headers, change load_by_default to no, include config.h, change string initialization to () style
24
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
25
#include <config.h>
1283.2.1 by Marcus Eriksson
rabbitmq replication applier
26
#include "rabbitmq_log.h"
27
#include <drizzled/message/transaction.pb.h>
28
#include <google/protobuf/io/coded_stream.h>
29
#include <stdio.h>
1530.4.3 by Monty Taylor
Two more missed renames.
30
#include <drizzled/module/registry.h>
1283.2.1 by Marcus Eriksson
rabbitmq replication applier
31
#include <drizzled/plugin.h>
32
#include <stdint.h>
33
#include "rabbitmq_handler.h"
1666.3.1 by Vijay Samuel
Merge refactored command line for rabbitmq
34
#include <boost/program_options.hpp>
35
#include <drizzled/module/option_map.h>
36
37
namespace po= boost::program_options;
1283.2.1 by Marcus Eriksson
rabbitmq replication applier
38
39
using namespace std;
40
using namespace drizzled;
41
using namespace google;
42
1964.2.14 by Monty Taylor
RabbitMQ.
43
namespace drizzle_plugin
44
{
1283.2.1 by Marcus Eriksson
rabbitmq replication applier
45
46
/**
47
 * rabbitmq port
48
 */
1964.2.14 by Monty Taylor
RabbitMQ.
49
static port_constraint sysvar_rabbitmq_port;
50
51
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),
58
  _exchange(exchange),
59
  _routingkey(routingkey)
60
{ }
1283.2.1 by Marcus Eriksson
rabbitmq replication applier
61
62
RabbitMQLog::~RabbitMQLog() 
1964.2.14 by Monty Taylor
RabbitMQ.
63
{ }
1283.2.1 by Marcus Eriksson
rabbitmq replication applier
64
1441.1.4 by Jay Pipes
Add --rabbitmq-use-replicator=[default|filtered|...] functionality and update variables test case
65
plugin::ReplicationReturnCode
66
RabbitMQLog::apply(Session &, const message::Transaction &to_apply)
1283.2.1 by Marcus Eriksson
rabbitmq replication applier
67
{
68
  size_t message_byte_length= to_apply.ByteSize();
1964.2.14 by Monty Taylor
RabbitMQ.
69
  uint8_t* buffer= new uint8_t[message_byte_length];
1283.2.1 by Marcus Eriksson
rabbitmq replication applier
70
  if(buffer == NULL)
71
  {
2126.3.3 by Brian Aker
Merge in error message rework. Many error messages are fixed in this patch.
72
    errmsg_printf(error::ERROR, _("Failed to allocate enough memory to transaction message\n"));
1283.2.1 by Marcus Eriksson
rabbitmq replication applier
73
    deactivate();
1441.1.4 by Jay Pipes
Add --rabbitmq-use-replicator=[default|filtered|...] functionality and update variables test case
74
    return plugin::UNKNOWN_ERROR;
1283.2.1 by Marcus Eriksson
rabbitmq replication applier
75
  }
76
77
  to_apply.SerializeWithCachedSizesToArray(buffer);
78
  try
79
  {
1964.2.14 by Monty Taylor
RabbitMQ.
80
    _rabbitMQHandler->publish(buffer, 
81
                             int(message_byte_length), 
82
                             _exchange,
83
                             _routingkey);
1283.2.1 by Marcus Eriksson
rabbitmq replication applier
84
  }
85
  catch(exception& e)
86
  {
2126.3.3 by Brian Aker
Merge in error message rework. Many error messages are fixed in this patch.
87
    errmsg_printf(error::ERROR, _(e.what()));
1283.2.1 by Marcus Eriksson
rabbitmq replication applier
88
    deactivate();
1441.1.4 by Jay Pipes
Add --rabbitmq-use-replicator=[default|filtered|...] functionality and update variables test case
89
    return plugin::UNKNOWN_ERROR;
1283.2.1 by Marcus Eriksson
rabbitmq replication applier
90
  }
1964.2.14 by Monty Taylor
RabbitMQ.
91
  delete[] buffer;
1441.1.4 by Jay Pipes
Add --rabbitmq-use-replicator=[default|filtered|...] functionality and update variables test case
92
  return plugin::SUCCESS;
1283.2.1 by Marcus Eriksson
rabbitmq replication applier
93
}
94
1441.1.2 by Jay Pipes
Two little problems were preventing the plugin from being loaded:
95
static RabbitMQLog *rabbitmqLogger; ///< the actual plugin
96
static RabbitMQHandler* rabbitmqHandler; ///< the rabbitmq handler
1283.2.1 by Marcus Eriksson
rabbitmq replication applier
97
98
/**
99
 * Initialize the rabbitmq logger - instanciates the dependencies (the handler)
100
 * and creates the log handler with the dependency - makes it easier to swap out
101
 * handler implementation
102
 */
1530.2.6 by Monty Taylor
Moved plugin::Context to module::Context.
103
static int init(drizzled::module::Context &context)
1283.2.1 by Marcus Eriksson
rabbitmq replication applier
104
{
1700.2.1 by Vijay Samuel
Merge precautionary fixes for rabbitmq to prevent plugin from breaking when my_getopt is removed.
105
  const module::option_map &vm= context.getOptions();
106
  
1964.2.14 by Monty Taylor
RabbitMQ.
107
  try 
108
  {
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>());
114
  } 
115
  catch (exception& e) 
116
  {
2126.3.3 by Brian Aker
Merge in error message rework. Many error messages are fixed in this patch.
117
    errmsg_printf(error::ERROR, _("Failed to allocate the RabbitMQHandler.  Got error: %s\n"),
1964.2.14 by Monty Taylor
RabbitMQ.
118
                  e.what());
119
    return 1;
120
  }
121
  try 
122
  {
123
    rabbitmqLogger= new RabbitMQLog("rabbit_log_applier",
124
                                    vm["exchange"].as<string>(),
125
                                    vm["routingkey"].as<string>(),
126
                                    rabbitmqHandler);
127
  } 
128
  catch (exception& e) 
129
  {
2126.3.3 by Brian Aker
Merge in error message rework. Many error messages are fixed in this patch.
130
    errmsg_printf(error::ERROR, _("Failed to allocate the RabbitMQLog instance.  Got error: %s\n"), 
1964.2.14 by Monty Taylor
RabbitMQ.
131
                  e.what());
132
    return 1;
133
  }
134
135
  context.add(rabbitmqLogger);
136
  ReplicationServices &replication_services= ReplicationServices::singleton();
137
  replication_services.attachApplier(rabbitmqLogger, vm["use-replicator"].as<string>());
138
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>()));
146
1283.2.1 by Marcus Eriksson
rabbitmq replication applier
147
  return 0;
148
}
149
150
1666.3.1 by Vijay Samuel
Merge refactored command line for rabbitmq
151
static void init_options(drizzled::module::option_context &context)
152
{
1700.2.1 by Vijay Samuel
Merge precautionary fixes for rabbitmq to prevent plugin from breaking when my_getopt is removed.
153
  context("host", 
154
          po::value<string>()->default_value("localhost"),
2068.4.1 by Andrew Hutchings
Fix intl domain
155
          _("Host name to connect to"));
1964.2.14 by Monty Taylor
RabbitMQ.
156
  context("port",
1964.2.17 by Monty Taylor
Did logging_query. Fixed rabbitmq.
157
          po::value<port_constraint>(&sysvar_rabbitmq_port)->default_value(5672),
2068.4.1 by Andrew Hutchings
Fix intl domain
158
          _("Port to connect to"));
1964.2.17 by Monty Taylor
Did logging_query. Fixed rabbitmq.
159
  context("virtualhost",
160
          po::value<string>()->default_value("/"),
2068.4.1 by Andrew Hutchings
Fix intl domain
161
          _("RabbitMQ virtualhost"));
1700.2.1 by Vijay Samuel
Merge precautionary fixes for rabbitmq to prevent plugin from breaking when my_getopt is removed.
162
  context("username",
163
          po::value<string>()->default_value("guest"),
2068.4.1 by Andrew Hutchings
Fix intl domain
164
          _("RabbitMQ username"));
1700.2.1 by Vijay Samuel
Merge precautionary fixes for rabbitmq to prevent plugin from breaking when my_getopt is removed.
165
  context("password",
166
          po::value<string>()->default_value("guest"),
2068.4.1 by Andrew Hutchings
Fix intl domain
167
          _("RabbitMQ password"));
1964.2.14 by Monty Taylor
RabbitMQ.
168
  context("use-replicator",
169
          po::value<string>()->default_value("default_replicator"),
2068.4.1 by Andrew Hutchings
Fix intl domain
170
          _("Name of the replicator plugin to use (default='default_replicator')"));
1964.2.14 by Monty Taylor
RabbitMQ.
171
  context("exchange",
172
          po::value<string>()->default_value("ReplicationExchange"),
2068.4.1 by Andrew Hutchings
Fix intl domain
173
          _("Name of RabbitMQ exchange to publish to"));
1964.2.14 by Monty Taylor
RabbitMQ.
174
  context("routingkey",
175
          po::value<string>()->default_value("ReplicationRoutingKey"),
2068.4.1 by Andrew Hutchings
Fix intl domain
176
          _("Name of RabbitMQ routing key to use"));
1666.3.1 by Vijay Samuel
Merge refactored command line for rabbitmq
177
}
178
1964.2.14 by Monty Taylor
RabbitMQ.
179
} /* namespace drizzle_plugin */
1283.2.1 by Marcus Eriksson
rabbitmq replication applier
180
1964.2.14 by Monty Taylor
RabbitMQ.
181
DRIZZLE_PLUGIN(drizzle_plugin::init, NULL, drizzle_plugin::init_options);
1283.2.1 by Marcus Eriksson
rabbitmq replication applier
182