server_container.cc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 "libtest/yatlcon.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. libtest::vchar_t port_str;
  71. port_str.resize(NI_MAXSERV);
  72. snprintf(&port_str[0], port_str.size(), "%u", int(arg->port()));
  73. server_config_string+= "--server=";
  74. server_config_string+= arg->hostname();
  75. server_config_string+= ":";
  76. server_config_string+= &port_str[0];
  77. server_config_string+= " ";
  78. }
  79. server_list+= server_config_string;
  80. }
  81. Server* server_startup_st::pop_server()
  82. {
  83. Server *tmp= servers.back();
  84. servers.pop_back();
  85. return tmp;
  86. }
  87. // host_to_shutdown => host number to shutdown in array
  88. bool server_startup_st::shutdown(uint32_t host_to_shutdown)
  89. {
  90. if (servers.size() > host_to_shutdown)
  91. {
  92. Server* tmp= servers[host_to_shutdown];
  93. if (tmp and tmp->kill() == false)
  94. { }
  95. else
  96. {
  97. return true;
  98. }
  99. }
  100. return false;
  101. }
  102. void server_startup_st::clear()
  103. {
  104. std::for_each(servers.begin(), servers.end(), DeleteFromVector());
  105. servers.clear();
  106. }
  107. bool server_startup_st::check() const
  108. {
  109. bool success= true;
  110. for (std::vector<Server *>::const_iterator iter= servers.begin(); iter != servers.end(); ++iter)
  111. {
  112. if ((*iter)->check() == false)
  113. {
  114. success= false;
  115. }
  116. }
  117. return success;
  118. }
  119. bool server_startup_st::shutdown()
  120. {
  121. bool success= true;
  122. for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); ++iter)
  123. {
  124. if ((*iter)->has_pid() and (*iter)->kill() == false)
  125. {
  126. Error << "Unable to kill:" << *(*iter);
  127. success= false;
  128. }
  129. }
  130. return success;
  131. }
  132. void server_startup_st::restart()
  133. {
  134. for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); ++iter)
  135. {
  136. (*iter)->start();
  137. }
  138. }
  139. #define MAGIC_MEMORY 123575
  140. server_startup_st::server_startup_st() :
  141. _magic(MAGIC_MEMORY),
  142. _socket(false),
  143. _sasl(false),
  144. udp(0),
  145. _servers_to_run(5)
  146. { }
  147. server_startup_st::~server_startup_st()
  148. {
  149. clear();
  150. }
  151. bool server_startup_st::validate()
  152. {
  153. return _magic == MAGIC_MEMORY;
  154. }
  155. 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)
  156. {
  157. return construct.start_server(server_type, try_port, argc, argv, opt_startup_message);
  158. }
  159. 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)
  160. {
  161. if (try_port <= 0)
  162. {
  163. throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "was passed the invalid port number %d", int(try_port));
  164. }
  165. libtest::Server *server= NULL;
  166. try {
  167. if (0)
  168. { }
  169. else if (server_type.compare("gearmand") == 0)
  170. {
  171. if (GEARMAND_BINARY)
  172. {
  173. server= build_gearmand("localhost", try_port);
  174. }
  175. }
  176. else if (server_type.compare("hostile-gearmand") == 0)
  177. {
  178. if (GEARMAND_BINARY)
  179. {
  180. server= build_gearmand("localhost", try_port, "gearmand/hostile_gearmand");
  181. }
  182. }
  183. else if (server_type.compare("drizzled") == 0)
  184. {
  185. if (DRIZZLED_BINARY)
  186. {
  187. if (HAVE_LIBDRIZZLE)
  188. {
  189. server= build_drizzled("localhost", try_port);
  190. }
  191. }
  192. }
  193. else if (server_type.compare("blobslap_worker") == 0)
  194. {
  195. if (GEARMAND_BINARY)
  196. {
  197. if (GEARMAND_BLOBSLAP_WORKER)
  198. {
  199. if (HAVE_LIBGEARMAN)
  200. {
  201. server= build_blobslap_worker(try_port);
  202. }
  203. }
  204. }
  205. }
  206. else if (server_type.compare("memcached") == 0)
  207. {
  208. if (HAVE_MEMCACHED_BINARY)
  209. {
  210. server= build_memcached("localhost", try_port);
  211. }
  212. }
  213. if (server == NULL)
  214. {
  215. throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Launching of an unknown server was attempted: %s", server_type.c_str());
  216. }
  217. }
  218. catch (...)
  219. {
  220. throw;
  221. }
  222. try {
  223. /*
  224. We will now cycle the server we have created.
  225. */
  226. if (server->cycle() == false)
  227. {
  228. Error << "Could not start up server " << *server;
  229. delete server;
  230. return false;
  231. }
  232. server->build(argc, argv);
  233. #if 0
  234. if (false)
  235. {
  236. Out << "Pausing for startup, hit return when ready.";
  237. std::string gdb_command= server->base_command();
  238. getchar();
  239. }
  240. else
  241. #endif
  242. if (server->start() == false)
  243. {
  244. delete server;
  245. return false;
  246. }
  247. else
  248. {
  249. if (opt_startup_message)
  250. {
  251. #if defined(DEBUG)
  252. if (DEBUG)
  253. {
  254. Outn();
  255. Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
  256. Outn();
  257. }
  258. #endif
  259. }
  260. }
  261. }
  262. catch (libtest::disconnected& err)
  263. {
  264. if (fatal::is_disabled() == false and try_port != LIBTEST_FAIL_PORT)
  265. {
  266. stream::cerr(err.file(), err.line(), err.func()) << err.what();
  267. delete server;
  268. return false;
  269. }
  270. }
  271. catch (...)
  272. {
  273. delete server;
  274. throw;
  275. }
  276. push_server(server);
  277. return true;
  278. }
  279. bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, int argc,
  280. const char *argv[],
  281. const bool opt_startup_message)
  282. {
  283. (void)try_port;
  284. Outn();
  285. Server *server= NULL;
  286. try {
  287. if (0)
  288. { }
  289. else if (server_type.compare("gearmand") == 0)
  290. {
  291. Error << "Socket files are not supported for gearmand yet";
  292. }
  293. else if (server_type.compare("memcached") == 0)
  294. {
  295. if (HAVE_MEMCACHED_BINARY)
  296. {
  297. server= build_memcached_socket("localhost", try_port);
  298. }
  299. else
  300. {
  301. Error << "No memcached binary is available";
  302. }
  303. }
  304. else
  305. {
  306. Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
  307. }
  308. if (server == NULL)
  309. {
  310. Error << "Failure occured while creating server: " << server_type;
  311. return false;
  312. }
  313. /*
  314. We will now cycle the server we have created.
  315. */
  316. if (server->cycle() == false)
  317. {
  318. Error << "Could not start up server " << *server;
  319. delete server;
  320. return false;
  321. }
  322. server->build(argc, argv);
  323. #if 0
  324. if (false)
  325. {
  326. Out << "Pausing for startup, hit return when ready.";
  327. std::string gdb_command= server->base_command();
  328. std::string options;
  329. Out << "run " << server->args(options);
  330. getchar();
  331. }
  332. else
  333. #endif
  334. if (server->start() == false)
  335. {
  336. Error << "Failed to start " << *server;
  337. delete server;
  338. return false;
  339. }
  340. else
  341. {
  342. if (opt_startup_message)
  343. {
  344. #if defined(DEBUG)
  345. if (DEBUG)
  346. {
  347. Outn();
  348. Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
  349. Outn();
  350. }
  351. #endif
  352. }
  353. }
  354. }
  355. catch (...)
  356. {
  357. delete server;
  358. throw;
  359. }
  360. push_server(server);
  361. set_default_socket(server->socket().c_str());
  362. Outn();
  363. return true;
  364. }
  365. std::string server_startup_st::option_string() const
  366. {
  367. std::string temp= server_list;
  368. rtrim(temp);
  369. return temp;
  370. }
  371. } // namespace libtest