server_container.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. #include <libtest/common.h>
  22. #include <cassert>
  23. #include <cerrno>
  24. #include <cstdlib>
  25. #include <iostream>
  26. #include <algorithm>
  27. #include <functional>
  28. #include <locale>
  29. // trim from end
  30. static inline std::string &rtrim(std::string &s)
  31. {
  32. s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
  33. return s;
  34. }
  35. namespace libtest {
  36. void server_startup_st::push_server(Server *arg)
  37. {
  38. servers.push_back(arg);
  39. char port_str[NI_MAXSERV];
  40. snprintf(port_str, sizeof(port_str), "%u", int(arg->port()));
  41. std::string server_config_string;
  42. if (arg->has_socket())
  43. {
  44. server_config_string+= "--socket=";
  45. server_config_string+= '"';
  46. server_config_string+= arg->socket();
  47. server_config_string+= '"';
  48. server_config_string+= " ";
  49. }
  50. else
  51. {
  52. server_config_string+= "--server=";
  53. server_config_string+= arg->hostname();
  54. server_config_string+= ":";
  55. server_config_string+= port_str;
  56. server_config_string+= " ";
  57. }
  58. server_list+= server_config_string;
  59. }
  60. Server* server_startup_st::pop_server()
  61. {
  62. Server *tmp= servers.back();
  63. servers.pop_back();
  64. return tmp;
  65. }
  66. bool server_startup_st::shutdown(uint32_t number_of_host)
  67. {
  68. if (servers.size() > number_of_host)
  69. {
  70. Server* tmp= servers[number_of_host];
  71. if (tmp and tmp->has_pid() and not tmp->kill(tmp->pid()))
  72. { }
  73. else
  74. {
  75. return true;
  76. }
  77. }
  78. return false;
  79. }
  80. void server_startup_st::shutdown_and_remove()
  81. {
  82. for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
  83. {
  84. delete *iter;
  85. }
  86. servers.clear();
  87. }
  88. void server_startup_st::shutdown()
  89. {
  90. for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
  91. {
  92. if ((*iter)->has_pid() and not (*iter)->kill((*iter)->pid()))
  93. {
  94. Error << "Unable to kill:" << *(*iter);
  95. }
  96. }
  97. }
  98. void server_startup_st::restart()
  99. {
  100. for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
  101. {
  102. (*iter)->start();
  103. }
  104. }
  105. server_startup_st::~server_startup_st()
  106. {
  107. shutdown_and_remove();
  108. }
  109. bool server_startup_st::is_debug() const
  110. {
  111. return bool(getenv("LIBTEST_MANUAL_GDB"));
  112. }
  113. bool server_startup_st::is_valgrind() const
  114. {
  115. return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
  116. }
  117. bool server_startup_st::is_helgrind() const
  118. {
  119. return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
  120. }
  121. bool server_startup(server_startup_st& construct, const std::string& server_type, in_port_t try_port, int argc, const char *argv[])
  122. {
  123. Outn();
  124. (void)try_port;
  125. set_max_port(try_port);
  126. // Look to see if we are being provided ports to use
  127. {
  128. char variable_buffer[1024];
  129. snprintf(variable_buffer, sizeof(variable_buffer), "LIBTEST_PORT_%lu", (unsigned long)construct.count());
  130. char *var;
  131. if ((var= getenv(variable_buffer)))
  132. {
  133. in_port_t tmp= in_port_t(atoi(var));
  134. if (tmp > 0)
  135. try_port= tmp;
  136. }
  137. }
  138. libtest::Server *server= NULL;
  139. if (0)
  140. { }
  141. else if (server_type.compare("gearmand") == 0)
  142. {
  143. if (GEARMAND_BINARY)
  144. {
  145. if (HAVE_LIBGEARMAN)
  146. {
  147. server= build_gearmand("localhost", try_port);
  148. }
  149. else
  150. {
  151. Error << "Libgearman was not found";
  152. }
  153. }
  154. else
  155. {
  156. Error << "No gearmand binary is available";
  157. }
  158. }
  159. else if (server_type.compare("blobslap_worker") == 0)
  160. {
  161. if (GEARMAND_BINARY)
  162. {
  163. if (HAVE_LIBGEARMAN)
  164. {
  165. server= build_blobslap_worker(try_port);
  166. }
  167. else
  168. {
  169. Error << "Libgearman was not found";
  170. }
  171. }
  172. else
  173. {
  174. Error << "No gearmand binary is available";
  175. }
  176. }
  177. else if (server_type.compare("memcached-sasl") == 0)
  178. {
  179. if (MEMCACHED_SASL_BINARY)
  180. {
  181. if (HAVE_LIBMEMCACHED)
  182. {
  183. server= build_memcached_sasl("localhost", try_port, construct.username(), construct.password());
  184. }
  185. else
  186. {
  187. Error << "Libmemcached was not found";
  188. }
  189. }
  190. else
  191. {
  192. Error << "No memcached binary that was compiled with sasl is available";
  193. }
  194. }
  195. else if (server_type.compare("memcached") == 0)
  196. {
  197. if (MEMCACHED_BINARY)
  198. {
  199. if (HAVE_LIBMEMCACHED)
  200. {
  201. server= build_memcached("localhost", try_port);
  202. }
  203. else
  204. {
  205. Error << "Libmemcached was not found";
  206. }
  207. }
  208. else
  209. {
  210. Error << "No memcached binary is available";
  211. }
  212. }
  213. else
  214. {
  215. Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
  216. }
  217. if (server == NULL)
  218. {
  219. Error << "Failure occured while creating server: " << server_type;
  220. return false;
  221. }
  222. /*
  223. We will now cycle the server we have created.
  224. */
  225. if (not server->cycle())
  226. {
  227. Error << "Could not start up server " << *server;
  228. delete server;
  229. return false;
  230. }
  231. server->build(argc, argv);
  232. if (construct.is_debug())
  233. {
  234. Out << "Pausing for startup, hit return when ready.";
  235. std::string gdb_command= server->base_command();
  236. std::string options;
  237. Out << "run " << server->args(options);
  238. getchar();
  239. }
  240. else if (not server->start())
  241. {
  242. Error << "Failed to start " << *server;
  243. delete server;
  244. return false;
  245. }
  246. else
  247. {
  248. Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
  249. }
  250. construct.push_server(server);
  251. if (default_port() == 0)
  252. {
  253. assert(server->has_port());
  254. set_default_port(server->port());
  255. }
  256. Outn();
  257. return true;
  258. }
  259. bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[])
  260. {
  261. (void)try_port;
  262. Outn();
  263. Server *server= NULL;
  264. if (0)
  265. { }
  266. else if (server_type.compare("gearmand") == 0)
  267. {
  268. Error << "Socket files are not supported for gearmand yet";
  269. }
  270. else if (server_type.compare("memcached-sasl") == 0)
  271. {
  272. if (MEMCACHED_SASL_BINARY)
  273. {
  274. if (HAVE_LIBMEMCACHED)
  275. {
  276. server= build_memcached_sasl_socket("localhost", try_port, username(), password());
  277. }
  278. else
  279. {
  280. Error << "Libmemcached was not found";
  281. }
  282. }
  283. else
  284. {
  285. Error << "No memcached binary is available";
  286. }
  287. }
  288. else if (server_type.compare("memcached") == 0)
  289. {
  290. if (MEMCACHED_BINARY)
  291. {
  292. if (HAVE_LIBMEMCACHED)
  293. {
  294. server= build_memcached_socket("localhost", try_port);
  295. }
  296. else
  297. {
  298. Error << "Libmemcached was not found";
  299. }
  300. }
  301. else
  302. {
  303. Error << "No memcached binary is available";
  304. }
  305. }
  306. else
  307. {
  308. Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
  309. }
  310. if (server == NULL)
  311. {
  312. Error << "Failure occured while creating server: " << server_type;
  313. return false;
  314. }
  315. /*
  316. We will now cycle the server we have created.
  317. */
  318. if (not server->cycle())
  319. {
  320. Error << "Could not start up server " << *server;
  321. delete server;
  322. return false;
  323. }
  324. server->build(argc, argv);
  325. if (is_debug())
  326. {
  327. Out << "Pausing for startup, hit return when ready.";
  328. std::string gdb_command= server->base_command();
  329. std::string options;
  330. Out << "run " << server->args(options);
  331. getchar();
  332. }
  333. else if (not server->start())
  334. {
  335. Error << "Failed to start " << *server;
  336. delete server;
  337. return false;
  338. }
  339. else
  340. {
  341. Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
  342. }
  343. push_server(server);
  344. set_default_socket(server->socket().c_str());
  345. Outn();
  346. return true;
  347. }
  348. std::string server_startup_st::option_string() const
  349. {
  350. std::string temp= server_list;
  351. rtrim(temp);
  352. return temp;
  353. }
  354. } // namespace libtest