stress_worker.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2012 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. using namespace libtest;
  40. #include <libgearman/gearman.h>
  41. #include <libhostile/hostile.h>
  42. #include "tests/client.h"
  43. #include <tests/start_worker.h>
  44. #include "tests/burnin.h"
  45. #include "tests/workers/v2/echo_or_react.h"
  46. #include <cerrno>
  47. #include <cstdio>
  48. #include <cstdlib>
  49. #include <cstring>
  50. #include <unistd.h>
  51. #include <sys/time.h>
  52. #define WORKER_FUNCTION_NAME "foo"
  53. static bool has_hostile()
  54. {
  55. #if defined(HAVE_LIBHOSTILE) && HAVE_LIBHOSTILE
  56. if (1)
  57. {
  58. return true;
  59. }
  60. #endif
  61. return false;
  62. }
  63. static in_port_t hostile_server= 0;
  64. static in_port_t stress_server= 0;
  65. static in_port_t& current_server_= stress_server;
  66. static void reset_server()
  67. {
  68. current_server_= stress_server;
  69. }
  70. static in_port_t current_server()
  71. {
  72. return current_server_;
  73. }
  74. static void set_server(in_port_t& arg)
  75. {
  76. current_server_= arg;
  77. }
  78. struct client_thread_context_st
  79. {
  80. size_t count;
  81. size_t payload_size;
  82. client_thread_context_st() :
  83. count(0),
  84. payload_size(0)
  85. { }
  86. void increment()
  87. {
  88. count++;
  89. }
  90. };
  91. extern "C" {
  92. static __attribute__((noreturn)) void *client_thread(void *object)
  93. {
  94. client_thread_context_st *success= (client_thread_context_st *)object;
  95. fatal_assert(success);
  96. fatal_assert(success->count == 0);
  97. Client client(current_server());
  98. {
  99. gearman_client_set_timeout(&client, 400);
  100. for (size_t x= 0; x < 100; x++)
  101. {
  102. int oldstate;
  103. pthread_setcanceltype(PTHREAD_CANCEL_DISABLE, &oldstate);
  104. libtest::vchar_t payload;
  105. payload.resize(success->payload_size);
  106. gearman_return_t rc;
  107. void *value= gearman_client_do(&client, WORKER_FUNCTION_NAME,
  108. NULL,
  109. &payload[0],
  110. payload.size() ? random() % payload.size() : 0,
  111. NULL, &rc);
  112. pthread_setcanceltype(oldstate, NULL);
  113. if (gearman_success(rc))
  114. {
  115. success->increment();
  116. }
  117. if (value)
  118. {
  119. free(value);
  120. }
  121. }
  122. }
  123. pthread_exit(0);
  124. }
  125. }
  126. static bool join_thread(pthread_t& thread_arg, struct timespec& ts)
  127. {
  128. int error;
  129. ts.tv_sec+= 10;
  130. if (HAVE_PTHREAD_TIMEDJOIN_NP)
  131. {
  132. #if defined(HAVE_PTHREAD_TIMEDJOIN_NP) && HAVE_PTHREAD_TIMEDJOIN_NP
  133. int limit= 2;
  134. while (--limit)
  135. {
  136. switch ((error= pthread_timedjoin_np(thread_arg, NULL, &ts)))
  137. {
  138. case ETIMEDOUT:
  139. libtest::dream(1, 0);
  140. continue;
  141. case 0:
  142. return true;
  143. case ESRCH:
  144. return false;
  145. default:
  146. Error << "pthread_timedjoin_np() " << strerror(error);
  147. return false;
  148. }
  149. }
  150. Out << "pthread_timedjoin_np() " << strerror(error);
  151. if ((error= pthread_cancel(thread_arg)) != 0)
  152. {
  153. Error << "pthread_cancel() " << strerror(error);
  154. return false;
  155. }
  156. #endif
  157. return true;
  158. }
  159. if ((error= pthread_join(thread_arg, NULL)) != 0)
  160. {
  161. Error << "pthread_join() " << strerror(error);
  162. return false;
  163. }
  164. return true;
  165. }
  166. static test_return_t send_random_port_data_TEST(void* )
  167. {
  168. set_alarm(1200, 0);
  169. SimpleClient client("localhost", current_server());
  170. for (size_t x= 0; x < 200; ++x)
  171. {
  172. libtest::vchar_t message_;
  173. libtest::vchar_t response_;
  174. libtest::vchar::make(message_, size_t(random() % 1024));
  175. client.send_data(message_, response_);
  176. }
  177. return TEST_SUCCESS;
  178. }
  179. static test_return_t worker_ramp_exec(const size_t payload_size)
  180. {
  181. set_alarm(1200, 0);
  182. std::vector<pthread_t> children;
  183. children.resize(number_of_cpus());
  184. std::vector<client_thread_context_st> success;
  185. success.resize(children.size());
  186. for (size_t x= 0; x < children.size(); x++)
  187. {
  188. success[x].payload_size= payload_size;
  189. pthread_create(&children[x], NULL, client_thread, &success[x]);
  190. }
  191. for (size_t x= 0; x < children.size(); x++)
  192. {
  193. struct timespec ts;
  194. bool join_success= false;
  195. int limit= 2;
  196. while (join_success == false and --limit)
  197. {
  198. #if defined(HAVE_LIBRT) && HAVE_LIBRT
  199. if (HAVE_LIBRT) // This won't be called on OSX, etc,...
  200. {
  201. if (clock_gettime(CLOCK_REALTIME, &ts) == -1)
  202. {
  203. Error << "clock_gettime(CLOCK_REALTIME) " << strerror(errno);
  204. continue;
  205. }
  206. join_success= join_thread(children[x], ts);
  207. }
  208. else
  209. #endif
  210. {
  211. struct timeval tv;
  212. if (gettimeofday(&tv, NULL) == -1)
  213. {
  214. Error << "gettimeofday() " << strerror(errno);
  215. continue;
  216. }
  217. TIMEVAL_TO_TIMESPEC(&tv, &ts);
  218. join_success= join_thread(children[x], ts);
  219. }
  220. }
  221. if (join_success == false)
  222. {
  223. pthread_cancel(children[x]);
  224. Error << "Something went very wrong, it is likely threads were not cleaned up";
  225. }
  226. }
  227. return TEST_SUCCESS;
  228. }
  229. static test_return_t worker_ramp_TEST(void *)
  230. {
  231. return worker_ramp_exec(0);
  232. }
  233. static test_return_t worker_ramp_1K_TEST(void *)
  234. {
  235. return worker_ramp_exec(1024);
  236. }
  237. static test_return_t worker_ramp_10K_TEST(void *)
  238. {
  239. return worker_ramp_exec(1024*10);
  240. }
  241. static test_return_t worker_ramp_SETUP(void *object)
  242. {
  243. test_skip_valgrind();
  244. worker_handles_st *handles= (worker_handles_st*)object;
  245. gearman_function_t echo_react_fn= gearman_function_create(echo_or_react_worker_v2);
  246. for (uint32_t x= 0; x < 10; x++)
  247. {
  248. worker_handle_st *worker;
  249. if ((worker= test_worker_start(current_server(), NULL, WORKER_FUNCTION_NAME, echo_react_fn, NULL, gearman_worker_options_t())) == NULL)
  250. {
  251. return TEST_FAILURE;
  252. }
  253. handles->push(worker);
  254. }
  255. return TEST_SUCCESS;
  256. }
  257. static test_return_t worker_ramp_TEARDOWN(void* object)
  258. {
  259. worker_handles_st *handles= (worker_handles_st*)object;
  260. handles->reset();
  261. reset_server();
  262. return TEST_SUCCESS;
  263. }
  264. static test_return_t hostile_gearmand_SETUP(void* object)
  265. {
  266. test_skip(true, has_hostile());
  267. test_skip_valgrind();
  268. test_skip(true, libtest::is_massive());
  269. // Programmer error
  270. assert(hostile_server);
  271. set_server(hostile_server);
  272. worker_ramp_SETUP(object);
  273. return TEST_SUCCESS;
  274. }
  275. static test_return_t recv_SETUP(void* object)
  276. {
  277. test_skip_valgrind();
  278. test_skip(true, libtest::is_massive());
  279. worker_ramp_SETUP(object);
  280. set_recv_close(true, 20, 20);
  281. return TEST_SUCCESS;
  282. }
  283. static test_return_t recv_corrupt_SETUP(void* object)
  284. {
  285. test_skip_valgrind();
  286. test_skip(true, libtest::is_massive());
  287. worker_ramp_SETUP(object);
  288. set_recv_corrupt(true, 20, 20);
  289. return TEST_SUCCESS;
  290. }
  291. static test_return_t resv_TEARDOWN(void* object)
  292. {
  293. set_recv_close(true, 0, 0);
  294. worker_handles_st *handles= (worker_handles_st*)object;
  295. handles->kill_all();
  296. reset_server();
  297. return TEST_SUCCESS;
  298. }
  299. static test_return_t send_SETUP(void* object)
  300. {
  301. test_skip_valgrind();
  302. test_skip(true, libtest::is_massive());
  303. worker_ramp_SETUP(object);
  304. set_send_close(true, 20, 20);
  305. return TEST_SUCCESS;
  306. }
  307. static test_return_t accept_SETUP(void* object)
  308. {
  309. test_skip_valgrind();
  310. test_skip(true, libtest::is_massive());
  311. worker_ramp_SETUP(object);
  312. set_accept_close(true, 20, 20);
  313. return TEST_SUCCESS;
  314. }
  315. static test_return_t poll_HOSTILE_POLL_CLOSED_SETUP(void* object)
  316. {
  317. test_skip_valgrind();
  318. test_skip(true, libtest::is_massive());
  319. worker_ramp_SETUP(object);
  320. set_poll_close(true, 4, 0, HOSTILE_POLL_CLOSED);
  321. return TEST_SUCCESS;
  322. }
  323. static test_return_t poll_HOSTILE_POLL_SHUT_WR_SETUP(void* object)
  324. {
  325. test_skip_valgrind();
  326. test_skip(true, libtest::is_massive());
  327. worker_ramp_SETUP(object);
  328. set_poll_close(true, 4, 0, HOSTILE_POLL_SHUT_WR);
  329. return TEST_SUCCESS;
  330. }
  331. static test_return_t poll_HOSTILE_POLL_SHUT_RD_SETUP(void* object)
  332. {
  333. test_skip_valgrind();
  334. test_skip(true, libtest::is_massive());
  335. worker_ramp_SETUP(object);
  336. set_poll_close(true, 4, 0, HOSTILE_POLL_SHUT_RD);
  337. return TEST_SUCCESS;
  338. }
  339. static test_return_t poll_TEARDOWN(void* object)
  340. {
  341. set_poll_close(false, 0, 0, HOSTILE_POLL_CLOSED);
  342. worker_handles_st *handles= (worker_handles_st*)object;
  343. handles->kill_all();
  344. reset_server();
  345. return TEST_SUCCESS;
  346. }
  347. static test_return_t send_TEARDOWN(void* object)
  348. {
  349. set_send_close(true, 0, 0);
  350. worker_handles_st *handles= (worker_handles_st*)object;
  351. handles->kill_all();
  352. reset_server();
  353. return TEST_SUCCESS;
  354. }
  355. static test_return_t accept_TEARDOWN(void* object)
  356. {
  357. set_accept_close(true, 0, 0);
  358. worker_handles_st *handles= (worker_handles_st*)object;
  359. handles->kill_all();
  360. reset_server();
  361. return TEST_SUCCESS;
  362. }
  363. /*********************** World functions **************************************/
  364. static void *world_create(server_startup_st& servers, test_return_t& error)
  365. {
  366. stress_server= libtest::default_port();
  367. if (server_startup(servers, "gearmand", stress_server, 0, NULL) == false)
  368. {
  369. error= TEST_SKIPPED;
  370. return NULL;
  371. }
  372. if (has_hostile())
  373. {
  374. hostile_server= libtest::get_free_port();
  375. if (server_startup(servers, "gearmand", hostile_server, 0, NULL) == false)
  376. {
  377. hostile_server= 0;
  378. }
  379. }
  380. return new worker_handles_st;
  381. }
  382. static bool world_destroy(void *object)
  383. {
  384. worker_handles_st *handles= (worker_handles_st *)object;
  385. delete handles;
  386. return TEST_SUCCESS;
  387. }
  388. test_st burnin_TESTS[] ={
  389. {"burnin", 0, burnin_TEST },
  390. {0, 0, 0}
  391. };
  392. test_st dos_TESTS[] ={
  393. {"send random port data", 0, send_random_port_data_TEST },
  394. {0, 0, 0}
  395. };
  396. test_st worker_TESTS[] ={
  397. {"first pass", 0, worker_ramp_TEST },
  398. {"second pass", 0, worker_ramp_TEST },
  399. {"first pass 1K jobs", 0, worker_ramp_1K_TEST },
  400. {"first pass 10K jobs", 0, worker_ramp_10K_TEST },
  401. {0, 0, 0}
  402. };
  403. collection_st collection[] ={
  404. {"burnin", burnin_setup, burnin_cleanup, burnin_TESTS },
  405. {"dos", 0, 0, dos_TESTS },
  406. {"plain", worker_ramp_SETUP, worker_ramp_TEARDOWN, worker_TESTS },
  407. {"plain against hostile server", hostile_gearmand_SETUP, worker_ramp_TEARDOWN, worker_TESTS },
  408. {"hostile recv()", recv_SETUP, resv_TEARDOWN, worker_TESTS },
  409. {"hostile recv() corrupt", recv_corrupt_SETUP, resv_TEARDOWN, worker_TESTS },
  410. {"hostile send()", send_SETUP, send_TEARDOWN, worker_TESTS },
  411. {"hostile accept()", accept_SETUP, accept_TEARDOWN, worker_TESTS },
  412. {"hostile poll(CLOSED)", poll_HOSTILE_POLL_CLOSED_SETUP, poll_TEARDOWN, worker_TESTS },
  413. {"hostile poll(SHUT_RD)", poll_HOSTILE_POLL_SHUT_RD_SETUP, poll_TEARDOWN, worker_TESTS },
  414. {"hostile poll(SHUT_WR)", poll_HOSTILE_POLL_SHUT_WR_SETUP, poll_TEARDOWN, worker_TESTS },
  415. {0, 0, 0, 0}
  416. };
  417. void get_world(libtest::Framework *world)
  418. {
  419. world->collections(collection);
  420. world->create(world_create);
  421. world->destroy(world_destroy);
  422. }