start_worker.cc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011-2013 Data Differential, http://datadifferential.com/
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following disclaimer
  17. * in the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * The names of its contributors may not be used to endorse or
  21. * promote products derived from this software without specific prior
  22. * written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. */
  37. #include "gear_config.h"
  38. #include <libtest/test.hpp>
  39. #include <libhostile/hostile.h>
  40. #include <cassert>
  41. #include <cstring>
  42. #include <memory>
  43. #include <signal.h>
  44. #include <stdlib.h>
  45. #include <sys/wait.h>
  46. #include <unistd.h>
  47. #include <cstdio>
  48. #include <tests/start_worker.h>
  49. #include <util/instance.hpp>
  50. using namespace libtest;
  51. using namespace datadifferential;
  52. #include "libgearman/worker.hpp"
  53. using namespace org::gearmand;
  54. #ifndef __INTEL_COMPILER
  55. #pragma GCC diagnostic ignored "-Wold-style-cast"
  56. #endif
  57. #define CONTEXT_MAGIC_MARKER 45
  58. struct context_st {
  59. worker_handle_st *handle;
  60. private:
  61. libgearman::Worker _worker;
  62. public:
  63. int magic;
  64. libtest::thread::Barrier* _sync_point;
  65. context_st(worker_handle_st* handle_,
  66. const libgearman::Worker& worker_) :
  67. handle(handle_),
  68. _worker(worker_),
  69. magic(CONTEXT_MAGIC_MARKER),
  70. _sync_point(handle_->sync_point())
  71. {
  72. }
  73. const libgearman::Worker& worker() const
  74. {
  75. return _worker;
  76. }
  77. void wait(void)
  78. {
  79. if (_sync_point)
  80. {
  81. _sync_point->wait();
  82. _sync_point= NULL;
  83. }
  84. }
  85. void fail(void)
  86. {
  87. handle->failed_startup= true;
  88. wait();
  89. }
  90. ~context_st()
  91. {
  92. }
  93. };
  94. static void thread_runner(context_st* con)
  95. {
  96. std::unique_ptr<context_st> context(con);
  97. assert(context.get());
  98. assert(context.get() == con);
  99. if (context.get() == NULL)
  100. {
  101. Error << "context_st passed to function was NULL";
  102. context->fail();
  103. return;
  104. }
  105. assert (context->magic == CONTEXT_MAGIC_MARKER);
  106. if (context->magic != CONTEXT_MAGIC_MARKER)
  107. {
  108. Error << "context_st had bad magic";
  109. context->fail();
  110. return;
  111. }
  112. libgearman::Worker worker(context->worker());
  113. if (&worker == NULL)
  114. {
  115. Error << "Failed to create Worker";
  116. context->fail();
  117. return;
  118. }
  119. assert(context->handle);
  120. if (context->handle == NULL)
  121. {
  122. Error << "Progammer error, no handle found";
  123. context->fail();
  124. return;
  125. }
  126. context->handle->set_worker_id(&worker);
  127. // Set worker id
  128. {
  129. if (gearman_failed(gearman_worker_set_identifier(&worker, gearman_literal_param("start_worker"))))
  130. {
  131. context->fail();
  132. return;
  133. }
  134. }
  135. // Check for a working server by pinging it with echo
  136. if (gearman_failed(gearman_worker_echo(&worker, gearman_literal_param("start_worker"))))
  137. {
  138. context->fail();
  139. return;
  140. }
  141. context->wait();
  142. gearman_return_t ret= GEARMAN_SUCCESS;
  143. while (context->handle->is_shutdown() == false)
  144. {
  145. ret= gearman_worker_work(&worker);
  146. if (ret == GEARMAN_NO_REGISTERED_FUNCTIONS)
  147. {
  148. context->handle->set_shutdown();
  149. continue;
  150. }
  151. if (ret == GEARMAN_SHUTDOWN)
  152. {
  153. gearman_return_t unreg_ret;
  154. if (gearman_failed((unreg_ret= gearman_worker_unregister_all(&worker))))
  155. {
  156. Error << "Failed to unregister " << gearman_strerror(unreg_ret);
  157. }
  158. continue;
  159. }
  160. if (ret != GEARMAN_SUCCESS and ret != GEARMAN_INVALID_ARGUMENT and ret != GEARMAN_WORK_FAIL)
  161. {
  162. context->handle->error();
  163. #if 0
  164. Error << context->function_name << ": " << gearman_strerror(ret) << ": " << gearman_worker_error(&worker);
  165. #endif
  166. }
  167. }
  168. }
  169. worker_handle_st *worker_run(const libgearman::Worker& worker_)
  170. {
  171. worker_handle_st *handle= new worker_handle_st();
  172. fatal_assert(handle);
  173. context_st *context= new context_st(handle, worker_);
  174. fatal_assert(context);
  175. handle->_thread= boost::shared_ptr<libtest::thread::Thread>(new libtest::thread::Thread(thread_runner, context));
  176. if (bool(handle->_thread) == false)
  177. {
  178. delete context;
  179. delete handle;
  180. FATAL("Could not allocate worker");
  181. }
  182. handle->wait();
  183. return handle;
  184. }
  185. worker_handle_st *test_worker_start(in_port_t port,
  186. const char *namespace_key,
  187. const char *function_name,
  188. const gearman_function_t &worker_fn,
  189. void *context_,
  190. gearman_worker_options_t options,
  191. int timeout)
  192. {
  193. worker_handle_st *handle= new worker_handle_st();
  194. fatal_assert(handle);
  195. libgearman::Worker worker(port);
  196. if (namespace_key)
  197. {
  198. gearman_worker_set_namespace(&worker, namespace_key, strlen(namespace_key));
  199. }
  200. // Set worker id
  201. {
  202. if (gearman_failed(gearman_worker_set_identifier(&worker, gearman_literal_param("start_worker"))))
  203. {
  204. delete handle;
  205. FATAL("gearman_worker_set_identifier() failed");
  206. }
  207. }
  208. // Check for a working server by pinging it with echo
  209. if (gearman_failed(gearman_worker_echo(&worker, gearman_literal_param("start_worker"))))
  210. {
  211. delete handle;
  212. FATAL("gearman_worker_echo() failed");
  213. }
  214. if (gearman_failed(gearman_worker_define_function(&worker,
  215. function_name, strlen(function_name),
  216. worker_fn,
  217. timeout,
  218. context_)))
  219. {
  220. delete handle;
  221. FATAL("gearman_worker_define_function() failed");
  222. }
  223. if (options != gearman_worker_options_t())
  224. {
  225. gearman_worker_add_options(&worker, options);
  226. }
  227. context_st *context= new context_st(handle, worker);
  228. fatal_assert(context);
  229. handle->_thread= boost::shared_ptr<libtest::thread::Thread>(new libtest::thread::Thread(thread_runner, context));
  230. if (bool(handle->_thread) == false)
  231. {
  232. delete context;
  233. delete handle;
  234. FATAL("Could not allocate worker");
  235. }
  236. handle->wait();
  237. return handle;
  238. }
  239. libtest::thread::Barrier* worker_handle_st::sync_point()
  240. {
  241. return &_sync_point;
  242. }
  243. void worker_handle_st::set_worker_id(gearman_worker_st* worker)
  244. {
  245. _worker_id= gearman_worker_id(worker);
  246. }
  247. worker_handle_st::worker_handle_st() :
  248. failed_startup(false),
  249. _shutdown(false),
  250. _error_count(0),
  251. _worker_id(gearman_id_t()),
  252. _sync_point(2)
  253. {
  254. }
  255. worker_handle_st::~worker_handle_st()
  256. {
  257. shutdown();
  258. }
  259. void worker_handle_st::wait()
  260. {
  261. _sync_point.wait();
  262. }
  263. void worker_handle_st::set_shutdown()
  264. {
  265. libtest::thread::ScopedLock l(_shutdown_lock);
  266. _shutdown= true;
  267. }
  268. bool worker_handle_st::is_shutdown()
  269. {
  270. libtest::thread::ScopedLock l(_shutdown_lock);
  271. return _shutdown;
  272. }
  273. bool worker_handle_st::shutdown()
  274. {
  275. if (is_shutdown())
  276. {
  277. return true;
  278. }
  279. set_shutdown();
  280. // This block issues an error, but the error is not fatal
  281. gearman_return_t rc;
  282. if (gearman_failed(rc= gearman_kill(_worker_id, GEARMAN_KILL)))
  283. {
  284. Error << "failed to shutdown " << rc;
  285. }
  286. _thread->join();
  287. return true;
  288. }
  289. bool worker_handle_st::check()
  290. {
  291. gearman_return_t rc;
  292. if (gearman_failed(rc= gearman_kill(_worker_id, GEARMAN_SIGNAL_CHECK)))
  293. {
  294. return false;
  295. }
  296. return true;
  297. }
  298. worker_handles_st::worker_handles_st()
  299. {
  300. }
  301. worker_handles_st::~worker_handles_st()
  302. {
  303. reset();
  304. }
  305. // Warning, this will not clean up memory
  306. void worker_handles_st::kill_all()
  307. {
  308. assert(valgrind_is_caller() == false);
  309. _workers.clear();
  310. }
  311. void worker_handles_st::reset()
  312. {
  313. for (std::vector<worker_handle_st *>::iterator iter= _workers.begin();
  314. iter != _workers.end();
  315. ++iter)
  316. {
  317. delete *iter;
  318. }
  319. _workers.clear();
  320. }
  321. void worker_handles_st::push(worker_handle_st *arg)
  322. {
  323. _workers.push_back(arg);
  324. }