Update from trunk to launchpad
[awesomized/libmemcached] / libtest / server.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libtest
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23 #include <libtest/common.h>
24
25 #include <cassert>
26 #include <cerrno>
27 #include <cstdlib>
28 #include <iostream>
29
30 #include <algorithm>
31 #include <functional>
32 #include <locale>
33
34 // trim from end
35 static inline std::string &rtrim(std::string &s)
36 {
37 s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
38 return s;
39 }
40
41 #include <libtest/server.h>
42 #include <libtest/stream.h>
43 #include <libtest/killpid.h>
44
45 #ifdef HAVE_LIBGEARMAN
46 #include <libtest/gearmand.h>
47 #include <libtest/blobslap_worker.h>
48 #endif
49
50 #ifdef HAVE_LIBMEMCACHED
51 #include <libtest/memcached.h>
52 #endif
53
54 namespace libtest {
55
56 std::ostream& operator<<(std::ostream& output, const Server &arg)
57 {
58 if (arg.is_socket())
59 {
60 output << arg.hostname();
61 }
62 else
63 {
64 output << arg.hostname() << ":" << arg.port();
65 }
66
67 if (arg.has_pid())
68 {
69 output << " Pid:" << arg.pid();
70 }
71
72 if (arg.has_socket())
73 {
74 output << " Socket:" << arg.socket();
75 }
76
77 if (not arg.running().empty())
78 {
79 output << " Exec:" << arg.running();
80 }
81
82
83 return output; // for multiple << operators
84 }
85
86 void Server::nap(void)
87 {
88 #ifdef WIN32
89 sleep(1);
90 #else
91 struct timespec global_sleep_value= { 0, 50000 };
92 nanosleep(&global_sleep_value, NULL);
93 #endif
94 }
95
96 Server::Server(const std::string& host_arg, const in_port_t port_arg, bool is_socket_arg) :
97 _is_socket(is_socket_arg),
98 _pid(-1),
99 _port(port_arg),
100 _hostname(host_arg)
101 {
102 }
103
104 Server::~Server()
105 {
106 if (has_pid() and not kill(_pid))
107 {
108 Error << "Unable to kill:" << *this;
109 }
110 }
111
112 std::string server_startup_st::option_string() const
113 {
114 std::string temp= server_list;
115 rtrim(temp);
116 return temp;
117 }
118
119 // If the server exists, kill it
120 bool Server::cycle()
121 {
122 uint32_t limit= 3;
123
124 // Try to ping, and kill the server #limit number of times
125 pid_t current_pid;
126 while (--limit and (current_pid= get_pid()) != -1)
127 {
128 if (kill(current_pid))
129 {
130 Log << "Killed existing server," << *this << " with pid:" << current_pid;
131 nap();
132 continue;
133 }
134 }
135
136 // For whatever reason we could not kill it, and we reached limit
137 if (limit == 0)
138 {
139 Error << "Reached limit, could not kill server pid:" << current_pid;
140 return false;
141 }
142
143 return true;
144 }
145
146 // Grab a one off command
147 bool Server::command(std::string& command_arg)
148 {
149 rebuild_base_command();
150
151 command_arg+= _base_command;
152
153 if (args(command_arg))
154 {
155 return true;
156 }
157
158 return false;
159 }
160
161 bool Server::start()
162 {
163 // If we find that we already have a pid then kill it.
164 if (has_pid() and not kill(_pid))
165 {
166 Error << "Could not kill() existing server during start() pid:" << _pid;
167 return false;
168 }
169 assert(not has_pid());
170
171 _running.clear();
172 if (not command(_running))
173 {
174 Error << "Could not build command()";
175 return false;
176 }
177
178 if (is_valgrind() or is_helgrind())
179 {
180 _running+= " &";
181 }
182
183 if (system(_running.c_str()) == -1)
184 {
185 Error << "system() failed:" << strerror(errno);
186 _running.clear();
187 return false;
188 }
189
190 if (is_helgrind())
191 {
192 sleep(4);
193 }
194
195 if (pid_file_option() and not pid_file().empty())
196 {
197 Wait wait(pid_file());
198
199 if (not wait.successful())
200 {
201 Error << "Unable to open pidfile: " << pid_file();
202 }
203 }
204
205 int count= is_helgrind() ? 20 : 5;
206 while (not ping() and --count)
207 {
208 nap();
209 }
210
211 if (count == 0)
212 {
213 Error << "Failed to ping() server once started:" << *this;
214 _running.clear();
215 return false;
216 }
217
218 // A failing get_pid() at this point is considered an error
219 _pid= get_pid(true);
220
221 return has_pid();
222 }
223
224 void Server::reset_pid()
225 {
226 _running.clear();
227 _pid_file.clear();
228 _pid= -1;
229 }
230
231 pid_t Server::pid()
232 {
233 return _pid;
234 }
235
236 bool Server::set_socket_file()
237 {
238 char file_buffer[FILENAME_MAX];
239 file_buffer[0]= 0;
240
241 if (broken_pid_file())
242 {
243 snprintf(file_buffer, sizeof(file_buffer), "/tmp/%s.socketXXXXXX", name());
244 }
245 else
246 {
247 snprintf(file_buffer, sizeof(file_buffer), "tests/var/run/%s.socketXXXXXX", name());
248 }
249
250 int fd;
251 if ((fd= mkstemp(file_buffer)) == -1)
252 {
253 perror(file_buffer);
254 return false;
255 }
256 close(fd);
257 unlink(file_buffer);
258
259 _socket= file_buffer;
260
261 return true;
262 }
263
264 bool Server::set_pid_file()
265 {
266 char file_buffer[FILENAME_MAX];
267 file_buffer[0]= 0;
268
269 if (broken_pid_file())
270 {
271 snprintf(file_buffer, sizeof(file_buffer), "/tmp/%s.pidXXXXXX", name());
272 }
273 else
274 {
275 snprintf(file_buffer, sizeof(file_buffer), "tests/var/run/%s.pidXXXXXX", name());
276 }
277
278 int fd;
279 if ((fd= mkstemp(file_buffer)) == -1)
280 {
281 perror(file_buffer);
282 return false;
283 }
284 close(fd);
285 unlink(file_buffer);
286
287 _pid_file= file_buffer;
288
289 return true;
290 }
291
292 bool Server::set_log_file()
293 {
294 char file_buffer[FILENAME_MAX];
295 file_buffer[0]= 0;
296
297 snprintf(file_buffer, sizeof(file_buffer), "tests/var/log/%s.logXXXXXX", name());
298 int fd;
299 if ((fd= mkstemp(file_buffer)) == -1)
300 {
301 perror(file_buffer);
302 return false;
303 }
304 close(fd);
305
306 _log_file= file_buffer;
307
308 return true;
309 }
310
311 void Server::rebuild_base_command()
312 {
313 _base_command.clear();
314 if (is_libtool() and getenv("LIBTOOL_COMMAND"))
315 {
316 _base_command+= getenv("LIBTOOL_COMMAND");
317 _base_command+= " ";
318 }
319
320 if (is_debug() and getenv("GDB_COMMAND"))
321 {
322 _base_command+= getenv("GDB_COMMAND");
323 _base_command+= " ";
324 }
325 else if (is_valgrind() and getenv("VALGRIND_COMMAND"))
326 {
327 _base_command+= getenv("VALGRIND_COMMAND");
328 _base_command+= " ";
329 }
330 else if (is_helgrind() and getenv("HELGRIND_COMMAND"))
331 {
332 _base_command+= getenv("HELGRIND_COMMAND");
333 _base_command+= " ";
334 }
335
336 _base_command+= executable();
337 }
338
339 void Server::set_extra_args(const std::string &arg)
340 {
341 _extra_args= arg;
342 }
343
344 bool Server::args(std::string& options)
345 {
346 std::stringstream arg_buffer;
347
348 // Set a log file if it was requested (and we can)
349 if (getenv("LIBTEST_LOG") and log_file_option())
350 {
351 if (not set_log_file())
352 {
353 return false;
354 }
355
356 arg_buffer << " " << log_file_option() << _log_file;
357 }
358
359 // Update pid_file
360 if (pid_file_option())
361 {
362 if (not set_pid_file())
363 {
364 return false;
365 }
366
367 arg_buffer << " " << pid_file_option() << pid_file();
368 }
369
370 assert(daemon_file_option());
371 if (daemon_file_option() and not is_valgrind() and not is_helgrind())
372 {
373 arg_buffer << " " << daemon_file_option();
374 }
375
376 if (_is_socket and socket_file_option())
377 {
378 if (not set_socket_file())
379 {
380 return false;
381 }
382
383 arg_buffer << " " << socket_file_option() << "\"" << _socket << "\"";
384 }
385
386 assert(port_option());
387 if (port_option() and _port > 0)
388 {
389 arg_buffer << " " << port_option() << _port;
390 }
391
392 options+= arg_buffer.str();
393
394 if (not _extra_args.empty())
395 {
396 options+= _extra_args;
397 }
398
399 return true;
400 }
401
402 bool Server::is_debug() const
403 {
404 return bool(getenv("LIBTEST_MANUAL_GDB"));
405 }
406
407 bool Server::is_valgrind() const
408 {
409 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
410 }
411
412 bool Server::is_helgrind() const
413 {
414 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
415 }
416
417 bool Server::kill(pid_t pid_arg)
418 {
419 if (check_pid(pid_arg) and kill_pid(pid_arg)) // If we kill it, reset
420 {
421 if (broken_pid_file() and not pid_file().empty())
422 {
423 unlink(pid_file().c_str());
424 }
425
426 reset_pid();
427
428 return true;
429 }
430
431 return false;
432 }
433
434 void server_startup_st::push_server(Server *arg)
435 {
436 servers.push_back(arg);
437
438 char port_str[NI_MAXSERV];
439 snprintf(port_str, sizeof(port_str), "%u", int(arg->port()));
440
441 std::string server_config_string;
442 if (arg->has_socket())
443 {
444 server_config_string+= "--socket=";
445 server_config_string+= '"';
446 server_config_string+= arg->socket();
447 server_config_string+= '"';
448 server_config_string+= " ";
449 }
450 else
451 {
452 server_config_string+= "--server=";
453 server_config_string+= arg->hostname();
454 server_config_string+= ":";
455 server_config_string+= port_str;
456 server_config_string+= " ";
457 }
458
459 server_list+= server_config_string;
460
461 }
462
463 Server* server_startup_st::pop_server()
464 {
465 Server *tmp= servers.back();
466 servers.pop_back();
467 return tmp;
468 }
469
470 void server_startup_st::shutdown(bool remove)
471 {
472 if (remove)
473 {
474 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
475 {
476 delete *iter;
477 }
478 servers.clear();
479 }
480 else
481 {
482 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
483 {
484 if ((*iter)->has_pid() and not (*iter)->kill((*iter)->pid()))
485 {
486 Error << "Unable to kill:" << *(*iter);
487 }
488 }
489 }
490 }
491
492 server_startup_st::~server_startup_st()
493 {
494 shutdown(true);
495 }
496
497 bool server_startup_st::is_debug() const
498 {
499 return bool(getenv("LIBTEST_MANUAL_GDB"));
500 }
501
502 bool server_startup_st::is_valgrind() const
503 {
504 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
505 }
506
507 bool server_startup_st::is_helgrind() const
508 {
509 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
510 }
511
512
513 bool server_startup(server_startup_st& construct, const std::string& server_type, in_port_t try_port, int argc, const char *argv[])
514 {
515 Outn();
516
517 // Look to see if we are being provided ports to use
518 {
519 char variable_buffer[1024];
520 snprintf(variable_buffer, sizeof(variable_buffer), "LIBTEST_PORT_%lu", (unsigned long)construct.count());
521
522 char *var;
523 if ((var= getenv(variable_buffer)))
524 {
525 in_port_t tmp= in_port_t(atoi(var));
526
527 if (tmp > 0)
528 try_port= tmp;
529 }
530 }
531
532 Server *server= NULL;
533 if (0)
534 { }
535 else if (server_type.compare("gearmand") == 0)
536 {
537 #ifdef GEARMAND_BINARY
538 #ifdef HAVE_LIBGEARMAN
539 server= build_gearmand("localhost", try_port);
540 #else
541 Error << "Libgearman was not found";
542 #endif
543 #else
544 Error << "No gearmand binary is available";
545 #endif
546 }
547 else if (server_type.compare("blobslap_worker") == 0)
548 {
549 #ifdef GEARMAND_BINARY
550 #ifdef HAVE_LIBGEARMAN
551 server= build_blobslap_worker(try_port);
552 #else
553 Error << "Libgearman was not found";
554 #endif
555 #else
556 Error << "No gearmand binary is available";
557 #endif
558 }
559 else if (server_type.compare("memcached") == 0)
560 {
561 #ifdef MEMCACHED_BINARY
562 #ifdef HAVE_LIBMEMCACHED
563 server= build_memcached("localhost", try_port);
564 #else
565 Error << "Libmemcached was not found";
566 #endif
567 #else
568 Error << "No memcached binary is available";
569 #endif
570 }
571 else
572 {
573 Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
574 }
575
576 if (server == NULL)
577 {
578 Error << "Failure occured while creating server: " << server_type;
579 return false;
580 }
581
582 /*
583 We will now cycle the server we have created.
584 */
585 if (not server->cycle())
586 {
587 Error << "Could not start up server " << *server;
588 delete server;
589 return false;
590 }
591
592 server->build(argc, argv);
593
594 if (construct.is_debug())
595 {
596 Out << "Pausing for startup, hit return when ready.";
597 std::string gdb_command= server->base_command();
598 std::string options;
599 Out << "run " << server->args(options);
600 getchar();
601 }
602 else if (not server->start())
603 {
604 Error << "Failed to start " << *server;
605 delete server;
606 return false;
607 }
608 else
609 {
610 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
611 }
612
613 construct.push_server(server);
614
615 if (default_port() == 0)
616 {
617 assert(server->has_port());
618 set_default_port(server->port());
619 }
620
621 Outn();
622
623 return true;
624 }
625
626 bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[])
627 {
628 (void)try_port;
629 Outn();
630
631 Server *server= NULL;
632 if (0)
633 { }
634 else if (server_type.compare("gearmand") == 0)
635 {
636 Error << "Socket files are not supported for gearmand yet";
637 }
638 else if (server_type.compare("memcached") == 0)
639 {
640 #ifdef MEMCACHED_BINARY
641 #ifdef HAVE_LIBMEMCACHED
642 server= build_memcached_socket("localhost", try_port);
643 #else
644 Error << "Libmemcached was not found";
645 #endif
646 #else
647 Error << "No memcached binary is available";
648 #endif
649 }
650 else
651 {
652 Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
653 }
654
655 if (server == NULL)
656 {
657 Error << "Failure occured while creating server: " << server_type;
658 return false;
659 }
660
661 /*
662 We will now cycle the server we have created.
663 */
664 if (not server->cycle())
665 {
666 Error << "Could not start up server " << *server;
667 delete server;
668 return false;
669 }
670
671 server->build(argc, argv);
672
673 if (is_debug())
674 {
675 Out << "Pausing for startup, hit return when ready.";
676 std::string gdb_command= server->base_command();
677 std::string options;
678 Out << "run " << server->args(options);
679 getchar();
680 }
681 else if (not server->start())
682 {
683 Error << "Failed to start " << *server;
684 delete server;
685 return false;
686 }
687 else
688 {
689 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
690 }
691
692 push_server(server);
693
694 set_default_socket(server->socket().c_str());
695
696 Outn();
697
698 return true;
699 }
700
701 } // namespace libtest