Take a pass at fixing the valgrind warning that happens when we have a failure to...
[m6w6/libmemcached] / libtest / server_container.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Data Differential YATL (i.e. libtest) library
4 *
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #include <config.h>
38 #include <libtest/common.h>
39
40 #include <cassert>
41 #include <cerrno>
42 #include <cstdlib>
43 #include <iostream>
44
45 #include <algorithm>
46 #include <functional>
47 #include <locale>
48
49 // trim from end
50 static inline std::string &rtrim(std::string &s)
51 {
52 s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
53 return s;
54 }
55
56 namespace libtest {
57
58 void server_startup_st::push_server(Server *arg)
59 {
60 servers.push_back(arg);
61
62 char port_str[NI_MAXSERV];
63 snprintf(port_str, sizeof(port_str), "%u", int(arg->port()));
64
65 std::string server_config_string;
66 if (arg->has_socket())
67 {
68 server_config_string+= "--socket=";
69 server_config_string+= '"';
70 server_config_string+= arg->socket();
71 server_config_string+= '"';
72 server_config_string+= " ";
73 }
74 else
75 {
76 server_config_string+= "--server=";
77 server_config_string+= arg->hostname();
78 server_config_string+= ":";
79 server_config_string+= port_str;
80 server_config_string+= " ";
81 }
82
83 server_list+= server_config_string;
84
85 }
86
87 Server* server_startup_st::pop_server()
88 {
89 Server *tmp= servers.back();
90 servers.pop_back();
91 return tmp;
92 }
93
94 // host_to_shutdown => host number to shutdown in array
95 bool server_startup_st::shutdown(uint32_t host_to_shutdown)
96 {
97 if (servers.size() > host_to_shutdown)
98 {
99 Server* tmp= servers[host_to_shutdown];
100
101 if (tmp and tmp->kill() == false)
102 { }
103 else
104 {
105 return true;
106 }
107 }
108
109 return false;
110 }
111
112 void server_startup_st::clear()
113 {
114 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
115 {
116 delete *iter;
117 }
118 servers.clear();
119 }
120
121 bool server_startup_st::check() const
122 {
123 bool success= true;
124 for (std::vector<Server *>::const_iterator iter= servers.begin(); iter != servers.end(); iter++)
125 {
126 if ((*iter)->check() == false)
127 {
128 success= false;
129 }
130 }
131
132 return success;
133 }
134
135 bool server_startup_st::shutdown()
136 {
137 bool success= true;
138 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
139 {
140 if ((*iter)->has_pid() and (*iter)->kill() == false)
141 {
142 Error << "Unable to kill:" << *(*iter);
143 success= false;
144 }
145 }
146
147 return success;
148 }
149
150 void server_startup_st::restart()
151 {
152 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
153 {
154 (*iter)->start();
155 }
156 }
157
158 #define MAGIC_MEMORY 123575
159 server_startup_st::server_startup_st() :
160 _magic(MAGIC_MEMORY),
161 _socket(false),
162 _sasl(false),
163 _count(0),
164 udp(0),
165 _servers_to_run(5)
166 { }
167
168 server_startup_st::~server_startup_st()
169 {
170 clear();
171 }
172
173 bool server_startup_st::validate()
174 {
175 return _magic == MAGIC_MEMORY;
176 }
177
178 bool server_startup(server_startup_st& construct, const std::string& server_type, in_port_t try_port, int argc, const char *argv[], const bool opt_startup_message)
179 {
180 if (try_port <= 0)
181 {
182 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "was passed the invalid port number %d", int(try_port));
183 }
184
185 libtest::Server *server= NULL;
186 try {
187 if (0)
188 { }
189 else if (server_type.compare("gearmand") == 0)
190 {
191 if (GEARMAND_BINARY)
192 {
193 if (HAVE_LIBGEARMAN)
194 {
195 server= build_gearmand("localhost", try_port);
196 }
197 }
198 }
199 else if (server_type.compare("drizzled") == 0)
200 {
201 if (DRIZZLED_BINARY)
202 {
203 if (HAVE_LIBDRIZZLE)
204 {
205 server= build_drizzled("localhost", try_port);
206 }
207 }
208 }
209 else if (server_type.compare("blobslap_worker") == 0)
210 {
211 if (GEARMAND_BINARY)
212 {
213 if (GEARMAND_BLOBSLAP_WORKER)
214 {
215 if (HAVE_LIBGEARMAN)
216 {
217 server= build_blobslap_worker(try_port);
218 }
219 }
220 }
221 }
222 else if (server_type.compare("memcached-sasl") == 0)
223 {
224 if (MEMCACHED_SASL_BINARY)
225 {
226 if (HAVE_LIBMEMCACHED)
227 {
228 server= build_memcached_sasl("localhost", try_port, construct.username(), construct.password());
229 }
230 }
231 }
232 else if (server_type.compare("memcached") == 0)
233 {
234 if (MEMCACHED_BINARY)
235 {
236 if (HAVE_LIBMEMCACHED)
237 {
238 server= build_memcached("localhost", try_port);
239 }
240 }
241 }
242 else if (server_type.compare("memcached-light") == 0)
243 {
244 if (MEMCACHED_LIGHT_BINARY)
245 {
246 if (HAVE_LIBMEMCACHED)
247 {
248 server= build_memcached_light("localhost", try_port);
249 }
250 }
251 }
252
253 if (server == NULL)
254 {
255 fatal_message("Launching of an unknown server was attempted");
256 }
257
258 /*
259 We will now cycle the server we have created.
260 */
261 if (server->cycle() == false)
262 {
263 Error << "Could not start up server " << *server;
264 delete server;
265 return false;
266 }
267
268 server->build(argc, argv);
269
270 if (false)
271 {
272 Out << "Pausing for startup, hit return when ready.";
273 std::string gdb_command= server->base_command();
274 std::string options;
275 #if 0
276 Out << "run " << server->args(options);
277 #endif
278 getchar();
279 }
280 else if (server->start() == false)
281 {
282 delete server;
283 return false;
284 }
285 else
286 {
287 if (opt_startup_message)
288 {
289 Outn();
290 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
291 Outn();
292 }
293 }
294 }
295 catch (...)
296 {
297 delete server;
298 throw;
299 }
300
301 construct.push_server(server);
302
303 return true;
304 }
305
306 bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[])
307 {
308 (void)try_port;
309 Outn();
310
311 Server *server= NULL;
312 try {
313 if (0)
314 { }
315 else if (server_type.compare("gearmand") == 0)
316 {
317 Error << "Socket files are not supported for gearmand yet";
318 }
319 else if (server_type.compare("memcached-sasl") == 0)
320 {
321 if (MEMCACHED_SASL_BINARY)
322 {
323 if (HAVE_LIBMEMCACHED)
324 {
325 server= build_memcached_sasl_socket("localhost", try_port, username(), password());
326 }
327 else
328 {
329 Error << "Libmemcached was not found";
330 }
331 }
332 else
333 {
334 Error << "No memcached binary is available";
335 }
336 }
337 else if (server_type.compare("memcached") == 0)
338 {
339 if (MEMCACHED_BINARY)
340 {
341 if (HAVE_LIBMEMCACHED)
342 {
343 server= build_memcached_socket("localhost", try_port);
344 }
345 else
346 {
347 Error << "Libmemcached was not found";
348 }
349 }
350 else
351 {
352 Error << "No memcached binary is available";
353 }
354 }
355 else
356 {
357 Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
358 }
359
360 if (server == NULL)
361 {
362 Error << "Failure occured while creating server: " << server_type;
363 return false;
364 }
365
366 /*
367 We will now cycle the server we have created.
368 */
369 if (server->cycle() == false)
370 {
371 Error << "Could not start up server " << *server;
372 delete server;
373 return false;
374 }
375
376 server->build(argc, argv);
377
378 if (false)
379 {
380 Out << "Pausing for startup, hit return when ready.";
381 std::string gdb_command= server->base_command();
382 std::string options;
383 #if 0
384 Out << "run " << server->args(options);
385 #endif
386 getchar();
387 }
388 else if (server->start() == false)
389 {
390 Error << "Failed to start " << *server;
391 delete server;
392 return false;
393 }
394 else
395 {
396 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
397 }
398 }
399 catch (...)
400 {
401 delete server;
402 throw;
403 }
404
405 push_server(server);
406
407 set_default_socket(server->socket().c_str());
408
409 Outn();
410
411 return true;
412 }
413
414 std::string server_startup_st::option_string() const
415 {
416 std::string temp= server_list;
417 rtrim(temp);
418 return temp;
419 }
420
421
422 } // namespace libtest