universal.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011 Data Differential, http://datadifferential.com/
  6. * Copyright (C) 2008 Brian Aker, Eric Day
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are
  11. * met:
  12. *
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * * Redistributions in binary form must reproduce the above
  17. * copyright notice, this list of conditions and the following disclaimer
  18. * in the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * * The names of its contributors may not be used to endorse or
  22. * promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. */
  38. /**
  39. * @file
  40. * @brief Gearman State Definitions
  41. */
  42. #include <libgearman/common.h>
  43. #include <libgearman/connection.h>
  44. #include <libgearman/packet.hpp>
  45. #include <libgearman/allocator.hpp>
  46. #include <libgearman/universal.hpp>
  47. #include <libgearman/vector.hpp>
  48. #include <cassert>
  49. #include <cerrno>
  50. #include <cstdarg>
  51. #include <cstdio>
  52. #include <cstdlib>
  53. #include <cstring>
  54. void gearman_universal_initialize(gearman_universal_st &self, const gearman_universal_options_t *options)
  55. {
  56. { // Set defaults on all options.
  57. self.options.dont_track_packets= false;
  58. self.options.non_blocking= false;
  59. self.options.stored_non_blocking= false;
  60. }
  61. if (options)
  62. {
  63. while (*options != GEARMAN_MAX)
  64. {
  65. /**
  66. @note Check for bad value, refactor gearman_add_options().
  67. */
  68. gearman_universal_add_options(self, *options);
  69. options++;
  70. }
  71. }
  72. self.verbose= GEARMAN_VERBOSE_NEVER;
  73. self.con_count= 0;
  74. self.packet_count= 0;
  75. self.pfds_size= 0;
  76. self.sending= 0;
  77. self.timeout= -1;
  78. self.con_list= NULL;
  79. self.packet_list= NULL;
  80. self.pfds= NULL;
  81. self.log_fn= NULL;
  82. self.log_context= NULL;
  83. self.allocator= gearman_default_allocator();
  84. self._namespace= NULL;
  85. self.error.rc= GEARMAN_SUCCESS;
  86. self.error.last_errno= 0;
  87. self.error.last_error[0]= 0;
  88. }
  89. void gearman_nap(int arg)
  90. {
  91. if (arg < 1)
  92. { }
  93. else
  94. {
  95. #ifdef WIN32
  96. sleep(arg/1000000);
  97. #else
  98. struct timespec global_sleep_value= { 0, static_cast<long>(arg * 1000)};
  99. nanosleep(&global_sleep_value, NULL);
  100. #endif
  101. }
  102. }
  103. void gearman_nap(gearman_universal_st &self)
  104. {
  105. gearman_nap(self.timeout);
  106. }
  107. void gearman_universal_clone(gearman_universal_st &destination, const gearman_universal_st &source)
  108. {
  109. gearman_universal_initialize(destination);
  110. (void)gearman_universal_set_option(destination, GEARMAN_NON_BLOCKING, source.options.non_blocking);
  111. (void)gearman_universal_set_option(destination, GEARMAN_DONT_TRACK_PACKETS, source.options.dont_track_packets);
  112. destination.timeout= source.timeout;
  113. destination._namespace= gearman_string_clone(source._namespace);
  114. for (gearman_connection_st *con= source.con_list; con; con= con->next)
  115. {
  116. if (not gearman_connection_copy(destination, *con))
  117. {
  118. gearman_universal_free(destination);
  119. return;
  120. }
  121. }
  122. }
  123. void gearman_universal_free(gearman_universal_st &universal)
  124. {
  125. gearman_free_all_cons(universal);
  126. gearman_free_all_packets(universal);
  127. gearman_string_free(universal._namespace);
  128. if (universal.pfds)
  129. {
  130. // created realloc()
  131. free(universal.pfds);
  132. }
  133. }
  134. gearman_return_t gearman_universal_set_option(gearman_universal_st &self, gearman_universal_options_t option, bool value)
  135. {
  136. switch (option)
  137. {
  138. case GEARMAN_NON_BLOCKING:
  139. self.options.non_blocking= value;
  140. break;
  141. case GEARMAN_DONT_TRACK_PACKETS:
  142. self.options.dont_track_packets= value;
  143. break;
  144. case GEARMAN_MAX:
  145. default:
  146. return GEARMAN_INVALID_COMMAND;
  147. }
  148. return GEARMAN_SUCCESS;
  149. }
  150. int gearman_universal_timeout(gearman_universal_st &self)
  151. {
  152. return self.timeout;
  153. }
  154. void gearman_universal_set_timeout(gearman_universal_st &self, int timeout)
  155. {
  156. self.timeout= timeout;
  157. }
  158. void gearman_set_log_fn(gearman_universal_st &self, gearman_log_fn *function,
  159. void *context, gearman_verbose_t verbose)
  160. {
  161. self.log_fn= function;
  162. self.log_context= context;
  163. self.verbose= verbose;
  164. }
  165. void gearman_set_workload_malloc_fn(gearman_universal_st& universal,
  166. gearman_malloc_fn *function,
  167. void *context)
  168. {
  169. universal.allocator.malloc= function;
  170. universal.allocator.context= context;
  171. }
  172. void gearman_set_workload_free_fn(gearman_universal_st& universal,
  173. gearman_free_fn *function,
  174. void *context)
  175. {
  176. universal.allocator.free= function;
  177. universal.allocator.context= context;
  178. }
  179. void gearman_free_all_cons(gearman_universal_st& universal)
  180. {
  181. while (universal.con_list)
  182. {
  183. delete universal.con_list;
  184. }
  185. }
  186. void gearman_reset(gearman_universal_st& universal)
  187. {
  188. for (gearman_connection_st *con= universal.con_list; con; con= con->next)
  189. {
  190. con->close();
  191. }
  192. }
  193. gearman_return_t gearman_flush_all(gearman_universal_st& universal)
  194. {
  195. for (gearman_connection_st *con= universal.con_list; con; con= con->next)
  196. {
  197. if (con->events & POLLOUT)
  198. continue;
  199. gearman_return_t ret= con->flush();
  200. if (gearman_failed(ret) and ret != GEARMAN_IO_WAIT)
  201. return ret;
  202. }
  203. return GEARMAN_SUCCESS;
  204. }
  205. gearman_return_t gearman_wait(gearman_universal_st& universal)
  206. {
  207. struct pollfd *pfds;
  208. if (universal.pfds_size < universal.con_count)
  209. {
  210. pfds= static_cast<pollfd*>(realloc(universal.pfds, universal.con_count * sizeof(struct pollfd)));
  211. if (not pfds)
  212. {
  213. gearman_perror(universal, "pollfd realloc");
  214. return GEARMAN_MEMORY_ALLOCATION_FAILURE;
  215. }
  216. universal.pfds= pfds;
  217. universal.pfds_size= universal.con_count;
  218. }
  219. else
  220. {
  221. pfds= universal.pfds;
  222. }
  223. nfds_t x= 0;
  224. for (gearman_connection_st *con= universal.con_list; con; con= con->next)
  225. {
  226. if (con->events == 0)
  227. continue;
  228. pfds[x].fd= con->fd;
  229. pfds[x].events= con->events;
  230. pfds[x].revents= 0;
  231. x++;
  232. }
  233. if (x == 0)
  234. {
  235. return gearman_error(universal, GEARMAN_NO_ACTIVE_FDS, "no active file descriptors");
  236. }
  237. int ret= 0;
  238. while (universal.timeout)
  239. {
  240. ret= poll(pfds, x, universal.timeout);
  241. if (ret == -1)
  242. {
  243. switch(errno)
  244. {
  245. case EINTR:
  246. continue;
  247. default:
  248. return gearman_perror(universal, "poll");
  249. }
  250. }
  251. break;
  252. }
  253. if (not ret)
  254. {
  255. gearman_error(universal, GEARMAN_TIMEOUT, "timeout reached");
  256. return GEARMAN_TIMEOUT;
  257. }
  258. x= 0;
  259. for (gearman_connection_st *con= universal.con_list; con; con= con->next)
  260. {
  261. if (con->events == 0)
  262. continue;
  263. int err;
  264. socklen_t len= sizeof (err);
  265. (void)getsockopt(con->fd, SOL_SOCKET, SO_ERROR, &err, &len);
  266. if (err)
  267. {
  268. con->cached_errno= err;
  269. }
  270. con->set_revents(pfds[x].revents);
  271. x++;
  272. }
  273. return GEARMAN_SUCCESS;
  274. }
  275. gearman_connection_st *gearman_ready(gearman_universal_st& universal)
  276. {
  277. /*
  278. We can't keep universal between calls since connections may be removed during
  279. processing. If this list ever gets big, we may want something faster.
  280. */
  281. for (gearman_connection_st *con= universal.con_list; con; con= con->next)
  282. {
  283. if (con->options.ready)
  284. {
  285. con->options.ready= false;
  286. return con;
  287. }
  288. }
  289. return NULL;
  290. }
  291. /**
  292. @note _push_blocking is only used for echo (and should be fixed
  293. when tricky flip/flop in IO is fixed).
  294. */
  295. static inline void _push_blocking(gearman_universal_st& universal, bool &orig_block_universal)
  296. {
  297. orig_block_universal= universal.options.non_blocking;
  298. universal.options.non_blocking= false;
  299. }
  300. static inline void _pop_non_blocking(gearman_universal_st& universal, bool orig_block_universal)
  301. {
  302. universal.options.non_blocking= orig_block_universal;
  303. }
  304. gearman_return_t gearman_echo(gearman_universal_st& universal,
  305. const void *workload,
  306. size_t workload_size)
  307. {
  308. gearman_packet_st packet;
  309. bool orig_block_universal;
  310. if (not workload)
  311. {
  312. gearman_error(universal, GEARMAN_INVALID_ARGUMENT, "workload was NULL");
  313. return GEARMAN_INVALID_ARGUMENT;
  314. }
  315. if (not workload_size)
  316. {
  317. gearman_error(universal, GEARMAN_INVALID_ARGUMENT, "workload_size was 0");
  318. return GEARMAN_INVALID_ARGUMENT;
  319. }
  320. gearman_return_t ret= gearman_packet_create_args(universal, packet, GEARMAN_MAGIC_REQUEST,
  321. GEARMAN_COMMAND_ECHO_REQ,
  322. &workload, &workload_size, 1);
  323. if (gearman_failed(ret))
  324. {
  325. return ret;
  326. }
  327. _push_blocking(universal, orig_block_universal);
  328. for (gearman_connection_st *con= universal.con_list; con; con= con->next)
  329. {
  330. gearman_packet_st *packet_ptr;
  331. ret= con->send(packet, true);
  332. if (gearman_failed(ret))
  333. {
  334. goto exit;
  335. }
  336. packet_ptr= con->receiving(con->_packet, ret, true);
  337. if (gearman_failed(ret))
  338. {
  339. goto exit;
  340. }
  341. assert(packet_ptr);
  342. if (con->_packet.data_size != workload_size ||
  343. memcmp(workload, con->_packet.data, workload_size))
  344. {
  345. gearman_packet_free(&(con->_packet));
  346. gearman_error(universal, GEARMAN_ECHO_DATA_CORRUPTION, "corruption during echo");
  347. ret= GEARMAN_ECHO_DATA_CORRUPTION;
  348. goto exit;
  349. }
  350. gearman_packet_free(&(con->_packet));
  351. }
  352. ret= GEARMAN_SUCCESS;
  353. exit:
  354. gearman_packet_free(&packet);
  355. _pop_non_blocking(universal, orig_block_universal);
  356. return ret;
  357. }
  358. bool gearman_request_option(gearman_universal_st &universal,
  359. gearman_string_t &option)
  360. {
  361. gearman_connection_st *con;
  362. gearman_packet_st packet;
  363. bool orig_block_universal;
  364. const void *args[]= { gearman_c_str(option) };
  365. size_t args_size[]= { gearman_size(option) };
  366. gearman_return_t ret;
  367. ret= gearman_packet_create_args(universal, packet, GEARMAN_MAGIC_REQUEST,
  368. GEARMAN_COMMAND_OPTION_REQ,
  369. args, args_size, 1);
  370. if (gearman_failed(ret))
  371. {
  372. gearman_error(universal, GEARMAN_MEMORY_ALLOCATION_FAILURE, "gearman_packet_create_args()");
  373. return ret;
  374. }
  375. _push_blocking(universal, orig_block_universal);
  376. for (con= universal.con_list; con != NULL; con= con->next)
  377. {
  378. gearman_packet_st *packet_ptr;
  379. ret= con->send(packet, true);
  380. if (gearman_failed(ret))
  381. {
  382. gearman_packet_free(&(con->_packet));
  383. goto exit;
  384. }
  385. packet_ptr= con->receiving(con->_packet, ret, true);
  386. if (gearman_failed(ret))
  387. {
  388. gearman_packet_free(&(con->_packet));
  389. goto exit;
  390. }
  391. assert(packet_ptr);
  392. if (packet_ptr->command == GEARMAN_COMMAND_ERROR)
  393. {
  394. gearman_packet_free(&(con->_packet));
  395. gearman_error(universal, GEARMAN_INVALID_ARGUMENT, "invalid server option");
  396. ret= GEARMAN_INVALID_ARGUMENT;;
  397. goto exit;
  398. }
  399. gearman_packet_free(&(con->_packet));
  400. }
  401. ret= GEARMAN_SUCCESS;
  402. exit:
  403. gearman_packet_free(&packet);
  404. _pop_non_blocking(universal, orig_block_universal);
  405. return gearman_success(ret);
  406. }
  407. void gearman_free_all_packets(gearman_universal_st &universal)
  408. {
  409. while (universal.packet_list)
  410. gearman_packet_free(universal.packet_list);
  411. }
  412. /*
  413. * Local Definitions
  414. */
  415. void gearman_universal_set_namespace(gearman_universal_st& universal, const char *namespace_key, size_t namespace_key_size)
  416. {
  417. gearman_string_free(universal._namespace);
  418. universal._namespace= gearman_string_create(NULL, namespace_key, namespace_key_size);
  419. }