Update all config.h usage.
[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 "mem_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 Server* server_startup_st::last()
59 {
60 return servers.back();
61 }
62
63 void server_startup_st::push_server(Server *arg)
64 {
65 servers.push_back(arg);
66
67 std::string server_config_string;
68 if (arg->has_socket())
69 {
70 server_config_string+= "--socket=";
71 server_config_string+= '"';
72 server_config_string+= arg->socket();
73 server_config_string+= '"';
74 server_config_string+= " ";
75 }
76 else
77 {
78 char port_str[NI_MAXSERV]= { 0 };
79 snprintf(port_str, sizeof(port_str), "%u", int(arg->port()));
80
81 server_config_string+= "--server=";
82 server_config_string+= arg->hostname();
83 server_config_string+= ":";
84 server_config_string+= port_str;
85 server_config_string+= " ";
86 }
87
88 server_list+= server_config_string;
89
90 }
91
92 Server* server_startup_st::pop_server()
93 {
94 Server *tmp= servers.back();
95 servers.pop_back();
96 return tmp;
97 }
98
99 // host_to_shutdown => host number to shutdown in array
100 bool server_startup_st::shutdown(uint32_t host_to_shutdown)
101 {
102 if (servers.size() > host_to_shutdown)
103 {
104 Server* tmp= servers[host_to_shutdown];
105
106 if (tmp and tmp->kill() == false)
107 { }
108 else
109 {
110 return true;
111 }
112 }
113
114 return false;
115 }
116
117 void server_startup_st::clear()
118 {
119 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); ++iter)
120 {
121 delete *iter;
122 }
123 servers.clear();
124 }
125
126 bool server_startup_st::check() const
127 {
128 bool success= true;
129 for (std::vector<Server *>::const_iterator iter= servers.begin(); iter != servers.end(); ++iter)
130 {
131 if ((*iter)->check() == false)
132 {
133 success= false;
134 }
135 }
136
137 return success;
138 }
139
140 bool server_startup_st::shutdown()
141 {
142 bool success= true;
143 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); ++iter)
144 {
145 if ((*iter)->has_pid() and (*iter)->kill() == false)
146 {
147 Error << "Unable to kill:" << *(*iter);
148 success= false;
149 }
150 }
151
152 return success;
153 }
154
155 void server_startup_st::restart()
156 {
157 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); ++iter)
158 {
159 (*iter)->start();
160 }
161 }
162
163 #define MAGIC_MEMORY 123575
164 server_startup_st::server_startup_st() :
165 _magic(MAGIC_MEMORY),
166 _socket(false),
167 _sasl(false),
168 _count(0),
169 udp(0),
170 _servers_to_run(5)
171 { }
172
173 server_startup_st::~server_startup_st()
174 {
175 clear();
176 }
177
178 bool server_startup_st::validate()
179 {
180 return _magic == MAGIC_MEMORY;
181 }
182
183 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)
184 {
185 return construct.start_server(server_type, try_port, argc, argv, opt_startup_message);
186 }
187
188 bool server_startup_st::start_server(const std::string& server_type, in_port_t try_port, int argc, const char *argv[], const bool opt_startup_message)
189 {
190 if (try_port <= 0)
191 {
192 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "was passed the invalid port number %d", int(try_port));
193 }
194
195 libtest::Server *server= NULL;
196 try {
197 if (0)
198 { }
199 else if (server_type.compare("gearmand") == 0)
200 {
201 if (GEARMAND_BINARY)
202 {
203 if (HAVE_LIBGEARMAN)
204 {
205 server= build_gearmand("localhost", try_port);
206 }
207 }
208 }
209 else if (server_type.compare("hostile-gearmand") == 0)
210 {
211 if (GEARMAND_BINARY)
212 {
213 if (HAVE_LIBGEARMAN)
214 {
215 server= build_gearmand("localhost", try_port, "gearmand/hostile_gearmand");
216 }
217 }
218 }
219 else if (server_type.compare("drizzled") == 0)
220 {
221 if (DRIZZLED_BINARY)
222 {
223 if (HAVE_LIBDRIZZLE)
224 {
225 server= build_drizzled("localhost", try_port);
226 }
227 }
228 }
229 else if (server_type.compare("blobslap_worker") == 0)
230 {
231 if (GEARMAND_BINARY)
232 {
233 if (GEARMAND_BLOBSLAP_WORKER)
234 {
235 if (HAVE_LIBGEARMAN)
236 {
237 server= build_blobslap_worker(try_port);
238 }
239 }
240 }
241 }
242 else if (server_type.compare("memcached-sasl") == 0)
243 {
244 if (MEMCACHED_SASL_BINARY)
245 {
246 if (HAVE_LIBMEMCACHED)
247 {
248 server= build_memcached_sasl("localhost", try_port, username(), password());
249 }
250 }
251 }
252 else if (server_type.compare("memcached") == 0)
253 {
254 if (HAVE_MEMCACHED_BINARY)
255 {
256 if (HAVE_LIBMEMCACHED)
257 {
258 server= build_memcached("localhost", try_port);
259 }
260 }
261 }
262 else if (server_type.compare("memcached-light") == 0)
263 {
264 if (MEMCACHED_LIGHT_BINARY)
265 {
266 if (HAVE_LIBMEMCACHED)
267 {
268 server= build_memcached_light("localhost", try_port);
269 }
270 }
271 }
272
273 if (server == NULL)
274 {
275 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Launching of an unknown server was attempted: %s", server_type.c_str());
276 }
277 }
278 catch (...)
279 {
280 throw;
281 }
282
283 try {
284 /*
285 We will now cycle the server we have created.
286 */
287 if (server->cycle() == false)
288 {
289 Error << "Could not start up server " << *server;
290 delete server;
291 return false;
292 }
293
294 server->build(argc, argv);
295
296 #if 0
297 if (false)
298 {
299 Out << "Pausing for startup, hit return when ready.";
300 std::string gdb_command= server->base_command();
301 getchar();
302 }
303 else
304 #endif
305
306 if (server->start() == false)
307 {
308 delete server;
309 return false;
310 }
311 else
312 {
313 if (opt_startup_message)
314 {
315 Outn();
316 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
317 Outn();
318 }
319 }
320 }
321 catch (libtest::disconnected& err)
322 {
323 if (fatal::is_disabled() == false and try_port != LIBTEST_FAIL_PORT)
324 {
325 stream::cerr(err.file(), err.line(), err.func()) << err.what();
326 delete server;
327 return false;
328 }
329 }
330 catch (...)
331 {
332 delete server;
333 throw;
334 }
335
336 push_server(server);
337
338 return true;
339 }
340
341 bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, int argc,
342 const char *argv[],
343 const bool opt_startup_message)
344 {
345 (void)try_port;
346 Outn();
347
348 Server *server= NULL;
349 try {
350 if (0)
351 { }
352 else if (server_type.compare("gearmand") == 0)
353 {
354 Error << "Socket files are not supported for gearmand yet";
355 }
356 else if (server_type.compare("memcached-sasl") == 0)
357 {
358 if (MEMCACHED_SASL_BINARY)
359 {
360 if (HAVE_LIBMEMCACHED)
361 {
362 server= build_memcached_sasl_socket("localhost", try_port, username(), password());
363 }
364 else
365 {
366 Error << "Libmemcached was not found";
367 }
368 }
369 else
370 {
371 Error << "No memcached binary is available";
372 }
373 }
374 else if (server_type.compare("memcached") == 0)
375 {
376 if (MEMCACHED_BINARY)
377 {
378 if (HAVE_LIBMEMCACHED)
379 {
380 server= build_memcached_socket("localhost", try_port);
381 }
382 else
383 {
384 Error << "Libmemcached was not found";
385 }
386 }
387 else
388 {
389 Error << "No memcached binary is available";
390 }
391 }
392 else
393 {
394 Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
395 }
396
397 if (server == NULL)
398 {
399 Error << "Failure occured while creating server: " << server_type;
400 return false;
401 }
402
403 /*
404 We will now cycle the server we have created.
405 */
406 if (server->cycle() == false)
407 {
408 Error << "Could not start up server " << *server;
409 delete server;
410 return false;
411 }
412
413 server->build(argc, argv);
414
415 #if 0
416 if (false)
417 {
418 Out << "Pausing for startup, hit return when ready.";
419 std::string gdb_command= server->base_command();
420 std::string options;
421 Out << "run " << server->args(options);
422 getchar();
423 }
424 else
425 #endif
426 if (server->start() == false)
427 {
428 Error << "Failed to start " << *server;
429 delete server;
430 return false;
431 }
432 else
433 {
434 if (opt_startup_message)
435 {
436 Outn();
437 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
438 Outn();
439 }
440 }
441 }
442 catch (...)
443 {
444 delete server;
445 throw;
446 }
447
448 push_server(server);
449
450 set_default_socket(server->socket().c_str());
451
452 Outn();
453
454 return true;
455 }
456
457 std::string server_startup_st::option_string() const
458 {
459 std::string temp= server_list;
460 rtrim(temp);
461 return temp;
462 }
463
464
465 } // namespace libtest