server_container.cc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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(), [](int ch) {
  48. return !std::isspace(ch);
  49. }).base(), s.end());
  50. return s;
  51. }
  52. namespace libtest {
  53. Server* server_startup_st::last()
  54. {
  55. return servers.back();
  56. }
  57. void server_startup_st::push_server(Server *arg)
  58. {
  59. assert(arg);
  60. servers.push_back(arg);
  61. std::string server_config_string;
  62. if (arg->has_socket())
  63. {
  64. server_config_string+= "--socket=";
  65. server_config_string+= '"';
  66. server_config_string+= arg->socket();
  67. server_config_string+= '"';
  68. server_config_string+= " ";
  69. }
  70. else
  71. {
  72. libtest::vchar_t port_str;
  73. port_str.resize(NI_MAXSERV);
  74. snprintf(&port_str[0], port_str.size(), "%u", int(arg->port()));
  75. server_config_string+= "--server=";
  76. server_config_string+= arg->hostname();
  77. server_config_string+= ":";
  78. server_config_string+= &port_str[0];
  79. server_config_string+= " ";
  80. }
  81. server_list+= server_config_string;
  82. }
  83. Server* server_startup_st::pop_server()
  84. {
  85. Server *tmp= servers.back();
  86. servers.pop_back();
  87. return tmp;
  88. }
  89. // host_to_shutdown => host number to shutdown in array
  90. bool server_startup_st::shutdown(uint32_t host_to_shutdown)
  91. {
  92. if (servers.size() > host_to_shutdown)
  93. {
  94. Server* tmp= servers[host_to_shutdown];
  95. if (tmp and tmp->kill() == false)
  96. { }
  97. else
  98. {
  99. return true;
  100. }
  101. }
  102. return false;
  103. }
  104. void server_startup_st::clear()
  105. {
  106. std::for_each(servers.begin(), servers.end(), DeleteFromVector());
  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. udp(0),
  147. _servers_to_run(5)
  148. { }
  149. server_startup_st::~server_startup_st()
  150. {
  151. clear();
  152. }
  153. bool server_startup_st::validate()
  154. {
  155. return _magic == MAGIC_MEMORY;
  156. }
  157. bool server_startup(server_startup_st& construct, const std::string& server_type, in_port_t try_port, const char *argv[])
  158. {
  159. return construct.start_server(server_type, try_port, argv);
  160. }
  161. libtest::Server* server_startup_st::create(const std::string& server_type, in_port_t try_port, const bool is_socket)
  162. {
  163. libtest::Server *server= NULL;
  164. if (is_socket == false)
  165. {
  166. if (try_port <= 0)
  167. {
  168. throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "was passed the invalid port number %d", int(try_port));
  169. }
  170. }
  171. if (is_socket)
  172. {
  173. if (server_type.compare("memcached") == 0)
  174. {
  175. server= build_memcached_socket("localhost", try_port);
  176. }
  177. else
  178. {
  179. Error << "Socket is not support for server: " << server_type;
  180. return NULL;
  181. }
  182. }
  183. else if (server_type.compare("gearmand") == 0)
  184. {
  185. server= build_gearmand("localhost", try_port);
  186. }
  187. else if (server_type.compare("hostile-gearmand") == 0)
  188. {
  189. server= build_gearmand("localhost", try_port, "gearmand/hostile_gearmand");
  190. }
  191. else if (server_type.compare("drizzled") == 0)
  192. {
  193. if (has_drizzled())
  194. {
  195. if (has_libdrizzle())
  196. {
  197. server= build_drizzled("localhost", try_port);
  198. }
  199. }
  200. }
  201. else if (server_type.compare("blobslap_worker") == 0)
  202. {
  203. if (has_gearmand())
  204. {
  205. #ifdef GEARMAND_BLOBSLAP_WORKER
  206. if (GEARMAND_BLOBSLAP_WORKER)
  207. {
  208. if (HAVE_LIBGEARMAN)
  209. {
  210. server= build_blobslap_worker(try_port);
  211. }
  212. }
  213. #endif // GEARMAND_BLOBSLAP_WORKER
  214. }
  215. }
  216. else if (server_type.compare("memcached") == 0)
  217. {
  218. if (has_memcached())
  219. {
  220. server= build_memcached("localhost", try_port);
  221. }
  222. }
  223. return server;
  224. }
  225. class ServerPtr {
  226. public:
  227. ServerPtr(libtest::Server* server_):
  228. _server(server_)
  229. { }
  230. ~ServerPtr()
  231. {
  232. delete _server;
  233. }
  234. void reset()
  235. {
  236. delete _server;
  237. _server= NULL;
  238. }
  239. libtest::Server* release(libtest::Server* server_= NULL)
  240. {
  241. libtest::Server* tmp= _server;
  242. _server= server_;
  243. return tmp;
  244. }
  245. libtest::Server* operator->() const
  246. {
  247. return _server;
  248. }
  249. libtest::Server* operator&() const
  250. {
  251. return _server;
  252. }
  253. private:
  254. libtest::Server* _server;
  255. };
  256. bool server_startup_st::_start_server(const bool is_socket,
  257. const std::string& server_type,
  258. in_port_t try_port,
  259. const char *argv[])
  260. {
  261. try {
  262. ServerPtr server(create(server_type, try_port, is_socket));
  263. if (&server == NULL)
  264. {
  265. Error << "Could not allocate server: " << server_type;
  266. return false;
  267. }
  268. /*
  269. We will now cycle the server we have created. (In case it was already running?)
  270. */
  271. if (server->cycle() == false)
  272. {
  273. Error << "Could not start up server " << &server;
  274. return false;
  275. }
  276. server->init(argv);
  277. if (server->start())
  278. {
  279. {
  280. #ifdef DEBUG
  281. if (DEBUG)
  282. {
  283. Outn();
  284. Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
  285. Outn();
  286. }
  287. #endif
  288. }
  289. }
  290. else
  291. {
  292. return false;
  293. }
  294. if (is_socket and &server)
  295. {
  296. set_default_socket(server->socket().c_str());
  297. }
  298. push_server(server.release());
  299. }
  300. catch (const libtest::disconnected& err)
  301. {
  302. if (fatal::is_disabled() == false and try_port != LIBTEST_FAIL_PORT)
  303. {
  304. stream::cerr(err.file(), err.line(), err.func()) << err.what();
  305. return false;
  306. }
  307. }
  308. catch (const libtest::__test_result& err)
  309. {
  310. stream::cerr(err.file(), err.line(), err.func()) << err.what();
  311. return false;
  312. }
  313. catch (const std::exception& err)
  314. {
  315. Error << err.what();
  316. return false;
  317. }
  318. catch (...)
  319. {
  320. Error << "error occurred while creating server: " << server_type;
  321. return false;
  322. }
  323. return true;
  324. }
  325. bool server_startup_st::start_server(const std::string& server_type, in_port_t try_port, const char *argv[])
  326. {
  327. return _start_server(false, server_type, try_port, argv);
  328. }
  329. bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, const char *argv[])
  330. {
  331. return _start_server(true, server_type, try_port, argv);
  332. }
  333. std::string server_startup_st::option_string() const
  334. {
  335. std::string temp= server_list;
  336. rtrim(temp);
  337. return temp;
  338. }
  339. } // namespace libtest