server_container.cc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 <cerrno>
  39. #include <cstdlib>
  40. #include <iostream>
  41. #include <algorithm>
  42. #include <functional>
  43. #include <locale>
  44. // trim from end
  45. static inline std::string &rtrim(std::string &s)
  46. {
  47. s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
  48. return s;
  49. }
  50. namespace libtest {
  51. Server* server_startup_st::last()
  52. {
  53. return servers.back();
  54. }
  55. void server_startup_st::push_server(Server *arg)
  56. {
  57. assert(arg);
  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, const char *argv[])
  156. {
  157. return construct.start_server(server_type, try_port, argv);
  158. }
  159. libtest::Server* server_startup_st::create(const std::string& server_type, in_port_t try_port, const bool is_socket)
  160. {
  161. libtest::Server *server= NULL;
  162. if (is_socket == false)
  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. }
  169. if (is_socket)
  170. {
  171. if (server_type.compare("memcached") == 0)
  172. {
  173. server= build_memcached_socket("localhost", try_port);
  174. }
  175. else
  176. {
  177. Error << "Socket is not support for server: " << server_type;
  178. return NULL;
  179. }
  180. }
  181. else if (server_type.compare("gearmand") == 0)
  182. {
  183. server= build_gearmand("localhost", try_port);
  184. }
  185. else if (server_type.compare("hostile-gearmand") == 0)
  186. {
  187. server= build_gearmand("localhost", try_port, "gearmand/hostile_gearmand");
  188. }
  189. else if (server_type.compare("drizzled") == 0)
  190. {
  191. if (has_drizzled())
  192. {
  193. if (has_libdrizzle())
  194. {
  195. server= build_drizzled("localhost", try_port);
  196. }
  197. }
  198. }
  199. else if (server_type.compare("blobslap_worker") == 0)
  200. {
  201. if (has_gearmand())
  202. {
  203. #ifdef GEARMAND_BLOBSLAP_WORKER
  204. if (GEARMAND_BLOBSLAP_WORKER)
  205. {
  206. if (HAVE_LIBGEARMAN)
  207. {
  208. server= build_blobslap_worker(try_port);
  209. }
  210. }
  211. #endif // GEARMAND_BLOBSLAP_WORKER
  212. }
  213. }
  214. else if (server_type.compare("memcached") == 0)
  215. {
  216. if (has_memcached())
  217. {
  218. server= build_memcached("localhost", try_port);
  219. }
  220. }
  221. return server;
  222. }
  223. class ServerPtr {
  224. public:
  225. ServerPtr(libtest::Server* server_):
  226. _server(server_)
  227. { }
  228. ~ServerPtr()
  229. {
  230. delete _server;
  231. }
  232. void reset()
  233. {
  234. delete _server;
  235. _server= NULL;
  236. }
  237. libtest::Server* release(libtest::Server* server_= NULL)
  238. {
  239. libtest::Server* tmp= _server;
  240. _server= server_;
  241. return tmp;
  242. }
  243. libtest::Server* operator->() const
  244. {
  245. return _server;
  246. }
  247. libtest::Server* operator&() const
  248. {
  249. return _server;
  250. }
  251. private:
  252. libtest::Server* _server;
  253. };
  254. bool server_startup_st::_start_server(const bool is_socket,
  255. const std::string& server_type,
  256. in_port_t try_port,
  257. const char *argv[])
  258. {
  259. try {
  260. ServerPtr server(create(server_type, try_port, is_socket));
  261. if (&server == NULL)
  262. {
  263. Error << "Could not allocate server: " << server_type;
  264. return false;
  265. }
  266. /*
  267. We will now cycle the server we have created. (In case it was already running?)
  268. */
  269. if (server->cycle() == false)
  270. {
  271. Error << "Could not start up server " << &server;
  272. return false;
  273. }
  274. server->init(argv);
  275. if (server->start())
  276. {
  277. {
  278. #ifdef DEBUG
  279. if (DEBUG)
  280. {
  281. Outn();
  282. Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
  283. Outn();
  284. }
  285. #endif
  286. }
  287. }
  288. else
  289. {
  290. return false;
  291. }
  292. if (is_socket and &server)
  293. {
  294. set_default_socket(server->socket().c_str());
  295. }
  296. push_server(server.release());
  297. }
  298. catch (const libtest::disconnected& err)
  299. {
  300. if (fatal::is_disabled() == false and try_port != LIBTEST_FAIL_PORT)
  301. {
  302. stream::cerr(err.file(), err.line(), err.func()) << err.what();
  303. return false;
  304. }
  305. }
  306. catch (const libtest::__test_result& err)
  307. {
  308. stream::cerr(err.file(), err.line(), err.func()) << err.what();
  309. return false;
  310. }
  311. catch (const std::exception& err)
  312. {
  313. Error << err.what();
  314. return false;
  315. }
  316. catch (...)
  317. {
  318. Error << "error occurred while creating server: " << server_type;
  319. return false;
  320. }
  321. return true;
  322. }
  323. bool server_startup_st::start_server(const std::string& server_type, in_port_t try_port, const char *argv[])
  324. {
  325. return _start_server(false, server_type, try_port, argv);
  326. }
  327. bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, const char *argv[])
  328. {
  329. return _start_server(true, server_type, try_port, argv);
  330. }
  331. std::string server_startup_st::option_string() const
  332. {
  333. std::string temp= server_list;
  334. rtrim(temp);
  335. return temp;
  336. }
  337. } // namespace libtest