server_container.cc 9.8 KB

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