Merge lp:~ansharyan015/drizzle/logging_gearman_dynamic into lp:drizzle

Proposed by Daniel Nichter
Status: Merged
Approved by: Brian Aker
Approved revision: 2568
Merged at revision: 2575
Proposed branch: lp:~ansharyan015/drizzle/logging_gearman_dynamic
Merge into: lp:drizzle
Diff against target: 233 lines (+123/-12)
2 files modified
plugin/logging_gearman/docs/index.rst (+4/-2)
plugin/logging_gearman/logging_gearman.cc (+119/-10)
To merge this branch: bzr merge lp:~ansharyan015/drizzle/logging_gearman_dynamic
Reviewer Review Type Date Requested Status
Daniel Nichter (community) code review Approve
Drizzle Merge Team Pending
Review via email: mp+114034@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Daniel Nichter (daniel-nichter) :
review: Approve (code review)
Revision history for this message
Daniel Nichter (daniel-nichter) wrote :

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'plugin/logging_gearman/docs/index.rst'
2--- plugin/logging_gearman/docs/index.rst 2011-10-23 05:45:09 +0000
3+++ plugin/logging_gearman/docs/index.rst 2012-07-09 19:57:25 +0000
4@@ -14,6 +14,8 @@
5
6 --plugin-add=logging_gearman
7
8+If this plugin is loaded without passing out the arguments ``logging_gearman_host`` and ``logging_gearman_function``, default values will be used. Desired values for these can either be set at server startup by passing out these arguments or setting these values dynamically at the runtime using ``SET GLOBAL logging_gearman_host=<newhost>`` and ``SET GLOBAL logging_gearman_function=<newfunction>``.
9+
10 Loading the plugin may not enable or configure it. See the plugin's
11 :ref:`logging_gearman_configuration` and :ref:`logging_gearman_variables`.
12
13@@ -57,7 +59,7 @@
14 * ``logging_gearman_function``
15
16 :Scope: Global
17- :Dynamic: No
18+ :Dynamic: Yes
19 :Option: :option:`--logging-gearman.function`
20
21 Gearman Function to send logging to
22@@ -67,7 +69,7 @@
23 * ``logging_gearman_host``
24
25 :Scope: Global
26- :Dynamic: No
27+ :Dynamic: Yes
28 :Option: :option:`--logging-gearman.host`
29
30 Hostname for logging to a Gearman server
31
32=== modified file 'plugin/logging_gearman/logging_gearman.cc'
33--- plugin/logging_gearman/logging_gearman.cc 2012-01-16 02:37:54 +0000
34+++ plugin/logging_gearman/logging_gearman.cc 2012-07-09 19:57:25 +0000
35@@ -21,6 +21,7 @@
36
37 #include <boost/scoped_array.hpp>
38
39+#include <drizzled/item.h>
40 #include <drizzled/plugin.h>
41 #include <drizzled/plugin/logging.h>
42 #include <drizzled/gettext.h>
43@@ -40,10 +41,15 @@
44 #include <cerrno>
45 #include <memory>
46
47+using namespace drizzled;
48+
49 namespace drizzle_plugin {
50+namespace logging_gearman {
51
52 namespace po= boost::program_options;
53
54+bool updateHost(Session *, set_var*);
55+bool updateFunction(Session *, set_var*);
56 /* TODO make this dynamic as needed */
57 static const int MAX_MSG_LEN= 32*1024;
58
59@@ -159,8 +165,8 @@
60 public drizzled::plugin::Logging
61 {
62
63- const std::string _host;
64- const std::string _function;
65+ std::string sysvar_host;
66+ std::string sysvar_function;
67
68 int _gearman_client_ok;
69 gearman_client_st _gearman_client;
70@@ -173,8 +179,8 @@
71 LoggingGearman(const std::string &host,
72 const std::string &function) :
73 drizzled::plugin::Logging("gearman_query_log"),
74- _host(host),
75- _function(function),
76+ sysvar_host(host),
77+ sysvar_function(function),
78 _gearman_client_ok(0),
79 _gearman_client()
80 {
81@@ -269,7 +275,7 @@
82 char job_handle[GEARMAN_JOB_HANDLE_SIZE];
83
84 (void) gearman_client_do_background(&_gearman_client,
85- _function.c_str(),
86+ sysvar_function.c_str(),
87 NULL,
88 (void *) msgbuf.get(),
89 (size_t) msgbuf_len,
90@@ -277,19 +283,121 @@
91
92 return false;
93 }
94+
95+ /**
96+ * This function changes the current gearman host to the parameter passed to the function.
97+ *
98+ * @return True on success, False on error.
99+ */
100+ bool setHost(std::string &new_host)
101+ {
102+ gearman_return_t tmp_ret;
103+
104+ /*
105+ New server is added to the list of servers using gearman_client_add_server.
106+ If the call does not result in error, then all the servers are removed from the list and
107+ new server only is added. This is done to ensure that a bad server url does not replace
108+ the existing server url.
109+ TODO Create a new instance of gearman_client_st and add the new server in it. If success, release the
110+ old gearman_client_st and use new instance of gearman_client_st everywhere.
111+ */
112+ tmp_ret= gearman_client_add_server(&_gearman_client,
113+ new_host.c_str(), 0);
114+ if (tmp_ret != GEARMAN_SUCCESS)
115+ {
116+ drizzled::errmsg_printf(drizzled::error::ERROR, _("fail gearman_client_add_server(): %s"),
117+ gearman_client_error(&_gearman_client));
118+ return false;
119+ }
120+
121+ gearman_client_remove_servers(&_gearman_client);
122+ gearman_client_add_server(&_gearman_client, new_host.c_str(), 0);
123+ sysvar_host= new_host;
124+ return true;
125+ }
126+
127+ /**
128+ * This function changes the gearman function with the new one.
129+ *
130+ * @return True on success (as we dont have to do anything except updating the function string, this always return true.)
131+ */
132+ bool setFunction(std::string &new_function)
133+ {
134+ sysvar_function= new_function;
135+ return true;
136+ }
137+
138+ /**
139+ * Getter for host
140+ *
141+ * @return sysvar_host
142+ */
143+ std::string& getHost()
144+ {
145+ return sysvar_host;
146+ }
147+
148+ /**
149+ * Getter for function
150+ *
151+ * @return sysvar_function
152+ */
153+ std::string& getFunction()
154+ {
155+ return sysvar_function;
156+ }
157 };
158
159 static LoggingGearman *handler= NULL;
160
161-static int logging_gearman_plugin_init(drizzled::module::Context &context)
162+/**
163+ * This function is called when the value of gearman host is updated dynamically from the drizzle server
164+ *
165+ * @return False on success, True on error
166+ */
167+bool updateHost(Session *, set_var* var)
168+{
169+ if (not var->value->str_value.empty())
170+ {
171+ std::string newHost(var->value->str_value.data());
172+ if (handler->setHost(newHost))
173+ return false; //success
174+ else
175+ return true; // error
176+ }
177+ errmsg_printf(error::ERROR, _("logging_gearman_host cannot be NULL"));
178+ return true; // error
179+}
180+
181+/**
182+ * This function is called when the value of gearman function is updated dynamically
183+ *
184+ * @return False on error, True on success
185+ */
186+bool updateFunction(Session *, set_var* var)
187+{
188+ if (not var->value->str_value.empty())
189+ {
190+ std::string newFunction(var->value->str_value.data());
191+ if (handler->setFunction(newFunction))
192+ return false; //success
193+ else
194+ return true; // error
195+ }
196+ errmsg_printf(error::ERROR, _("logging_gearman_function cannot be NULL"));
197+ return true; // error
198+}
199+
200+
201+static int init(drizzled::module::Context &context)
202 {
203 const drizzled::module::option_map &vm= context.getOptions();
204
205 handler= new LoggingGearman(vm["host"].as<std::string>(),
206 vm["function"].as<std::string>());
207 context.add(handler);
208- context.registerVariable(new drizzled::sys_var_const_string_val("host", vm["host"].as<std::string>()));
209- context.registerVariable(new drizzled::sys_var_const_string_val("function", vm["function"].as<std::string>()));
210+ context.registerVariable(new sys_var_std_string("host", handler->getHost(), NULL, &updateHost));
211+ context.registerVariable(new sys_var_std_string("function", handler->getFunction(), NULL, &updateFunction));
212
213 return 0;
214 }
215@@ -304,6 +412,7 @@
216 _("Gearman Function to send logging to"));
217 }
218
219+} /* namespace logging_gearman */
220 } /* namespace drizzle_plugin */
221
222 DRIZZLE_DECLARE_PLUGIN
223@@ -314,8 +423,8 @@
224 "Mark Atwood",
225 N_("Logs queries to a Gearman server"),
226 drizzled::PLUGIN_LICENSE_GPL,
227- drizzle_plugin::logging_gearman_plugin_init,
228+ drizzle_plugin::logging_gearman::init,
229 NULL,
230- drizzle_plugin::init_options
231+ drizzle_plugin::logging_gearman::init_options
232 }
233 DRIZZLE_DECLARE_PLUGIN_END;

Subscribers

People subscribed via source and target branches

to all changes: