server_container.cc 10 KB

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