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