universal.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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 <cassert>
  44. #include <cerrno>
  45. #include <cstdarg>
  46. #include <cstdio>
  47. #include <cstdlib>
  48. #include <cstring>
  49. void gearman_universal_initialize(gearman_universal_st &self, const gearman_universal_options_t *options)
  50. {
  51. { // Set defaults on all options.
  52. self.options.dont_track_packets= false;
  53. self.options.non_blocking= false;
  54. self.options.stored_non_blocking= false;
  55. }
  56. if (options)
  57. {
  58. while (*options != GEARMAN_MAX)
  59. {
  60. /**
  61. @note Check for bad value, refactor gearman_add_options().
  62. */
  63. gearman_universal_add_options(self, *options);
  64. options++;
  65. }
  66. }
  67. self.verbose= GEARMAN_VERBOSE_NEVER;
  68. self.con_count= 0;
  69. self.packet_count= 0;
  70. self.pfds_size= 0;
  71. self.sending= 0;
  72. self.timeout= -1;
  73. self.con_list= NULL;
  74. self.packet_list= NULL;
  75. self.pfds= NULL;
  76. self.log_fn= NULL;
  77. self.log_context= NULL;
  78. self.allocator= gearman_default_allocator();
  79. self._namespace= NULL;
  80. self.error.rc= GEARMAN_SUCCESS;
  81. self.error.last_errno= 0;
  82. self.error.last_error[0]= 0;
  83. }
  84. void gearman_nap(int arg)
  85. {
  86. if (arg < 1)
  87. { }
  88. else
  89. {
  90. #ifdef WIN32
  91. sleep(arg/1000000);
  92. #else
  93. struct timespec global_sleep_value= { 0, static_cast<long>(arg * 1000)};
  94. nanosleep(&global_sleep_value, NULL);
  95. #endif
  96. }
  97. }
  98. void gearman_nap(gearman_universal_st &self)
  99. {
  100. gearman_nap(self.timeout);
  101. }
  102. void gearman_universal_clone(gearman_universal_st &destination, const gearman_universal_st &source)
  103. {
  104. gearman_universal_initialize(destination);
  105. (void)gearman_universal_set_option(destination, GEARMAN_NON_BLOCKING, source.options.non_blocking);
  106. (void)gearman_universal_set_option(destination, GEARMAN_DONT_TRACK_PACKETS, source.options.dont_track_packets);
  107. destination.timeout= source.timeout;
  108. destination._namespace= gearman_string_clone(source._namespace);
  109. for (gearman_connection_st *con= source.con_list; con; con= con->next)
  110. {
  111. if (not gearman_connection_copy(destination, *con))
  112. {
  113. gearman_universal_free(destination);
  114. return;
  115. }
  116. }
  117. }
  118. void gearman_universal_free(gearman_universal_st &universal)
  119. {
  120. gearman_free_all_cons(universal);
  121. gearman_free_all_packets(universal);
  122. gearman_string_free(universal._namespace);
  123. if (universal.pfds)
  124. {
  125. // created realloc()
  126. free(universal.pfds);
  127. }
  128. }
  129. gearman_return_t gearman_universal_set_option(gearman_universal_st &self, gearman_universal_options_t option, bool value)
  130. {
  131. switch (option)
  132. {
  133. case GEARMAN_NON_BLOCKING:
  134. self.options.non_blocking= value;
  135. break;
  136. case GEARMAN_DONT_TRACK_PACKETS:
  137. self.options.dont_track_packets= value;
  138. break;
  139. case GEARMAN_MAX:
  140. default:
  141. return GEARMAN_INVALID_COMMAND;
  142. }
  143. return GEARMAN_SUCCESS;
  144. }
  145. int gearman_universal_timeout(gearman_universal_st &self)
  146. {
  147. return self.timeout;
  148. }
  149. void gearman_universal_set_timeout(gearman_universal_st &self, int timeout)
  150. {
  151. self.timeout= timeout;
  152. }
  153. void gearman_set_log_fn(gearman_universal_st &self, gearman_log_fn *function,
  154. void *context, gearman_verbose_t verbose)
  155. {
  156. self.log_fn= function;
  157. self.log_context= context;
  158. self.verbose= verbose;
  159. }
  160. void gearman_set_workload_malloc_fn(gearman_universal_st& universal,
  161. gearman_malloc_fn *function,
  162. void *context)
  163. {
  164. universal.allocator.malloc= function;
  165. universal.allocator.context= context;
  166. }
  167. void gearman_set_workload_free_fn(gearman_universal_st& universal,
  168. gearman_free_fn *function,
  169. void *context)
  170. {
  171. universal.allocator.free= function;
  172. universal.allocator.context= context;
  173. }
  174. void gearman_free_all_cons(gearman_universal_st& universal)
  175. {
  176. while (universal.con_list)
  177. {
  178. delete universal.con_list;
  179. }
  180. }
  181. void gearman_reset(gearman_universal_st& universal)
  182. {
  183. for (gearman_connection_st *con= universal.con_list; con; con= con->next)
  184. {
  185. con->close();
  186. }
  187. }
  188. gearman_return_t gearman_flush_all(gearman_universal_st& universal)
  189. {
  190. for (gearman_connection_st *con= universal.con_list; con; con= con->next)
  191. {
  192. if (con->events & POLLOUT)
  193. continue;
  194. gearman_return_t ret= con->flush();
  195. if (gearman_failed(ret) and ret != GEARMAN_IO_WAIT)
  196. return ret;
  197. }
  198. return GEARMAN_SUCCESS;
  199. }
  200. gearman_return_t gearman_wait(gearman_universal_st& universal)
  201. {
  202. struct pollfd *pfds;
  203. if (universal.pfds_size < universal.con_count)
  204. {
  205. pfds= static_cast<pollfd*>(realloc(universal.pfds, universal.con_count * sizeof(struct pollfd)));
  206. if (not pfds)
  207. {
  208. gearman_perror(universal, "pollfd realloc");
  209. return GEARMAN_MEMORY_ALLOCATION_FAILURE;
  210. }
  211. universal.pfds= pfds;
  212. universal.pfds_size= universal.con_count;
  213. }
  214. else
  215. {
  216. pfds= universal.pfds;
  217. }
  218. nfds_t x= 0;
  219. for (gearman_connection_st *con= universal.con_list; con; con= con->next)
  220. {
  221. if (con->events == 0)
  222. continue;
  223. pfds[x].fd= con->fd;
  224. pfds[x].events= con->events;
  225. pfds[x].revents= 0;
  226. x++;
  227. }
  228. if (x == 0)
  229. {
  230. return gearman_error(universal, GEARMAN_NO_ACTIVE_FDS, "no active file descriptors");
  231. }
  232. int ret= 0;
  233. while (universal.timeout)
  234. {
  235. ret= poll(pfds, x, universal.timeout);
  236. if (ret == -1)
  237. {
  238. switch(errno)
  239. {
  240. case EINTR:
  241. continue;
  242. default:
  243. return gearman_perror(universal, "poll");
  244. }
  245. }
  246. break;
  247. }
  248. if (not ret)
  249. {
  250. gearman_error(universal, GEARMAN_TIMEOUT, "timeout reached");
  251. return GEARMAN_TIMEOUT;
  252. }
  253. x= 0;
  254. for (gearman_connection_st *con= universal.con_list; con; con= con->next)
  255. {
  256. if (con->events == 0)
  257. continue;
  258. int err;
  259. socklen_t len= sizeof (err);
  260. (void)getsockopt(con->fd, SOL_SOCKET, SO_ERROR, &err, &len);
  261. if (err)
  262. {
  263. con->cached_errno= err;
  264. }
  265. con->set_revents(pfds[x].revents);
  266. x++;
  267. }
  268. return GEARMAN_SUCCESS;
  269. }
  270. gearman_connection_st *gearman_ready(gearman_universal_st& universal)
  271. {
  272. /*
  273. We can't keep universal between calls since connections may be removed during
  274. processing. If this list ever gets big, we may want something faster.
  275. */
  276. for (gearman_connection_st *con= universal.con_list; con; con= con->next)
  277. {
  278. if (con->options.ready)
  279. {
  280. con->options.ready= false;
  281. return con;
  282. }
  283. }
  284. return NULL;
  285. }
  286. /**
  287. @note _push_blocking is only used for echo (and should be fixed
  288. when tricky flip/flop in IO is fixed).
  289. */
  290. static inline void _push_blocking(gearman_universal_st& universal, bool &orig_block_universal)
  291. {
  292. orig_block_universal= universal.options.non_blocking;
  293. universal.options.non_blocking= false;
  294. }
  295. static inline void _pop_non_blocking(gearman_universal_st& universal, bool orig_block_universal)
  296. {
  297. universal.options.non_blocking= orig_block_universal;
  298. }
  299. gearman_return_t gearman_echo(gearman_universal_st& universal,
  300. const void *workload,
  301. size_t workload_size)
  302. {
  303. gearman_packet_st packet;
  304. bool orig_block_universal;
  305. if (not workload)
  306. {
  307. gearman_error(universal, GEARMAN_INVALID_ARGUMENT, "workload was NULL");
  308. return GEARMAN_INVALID_ARGUMENT;
  309. }
  310. if (not workload_size)
  311. {
  312. gearman_error(universal, GEARMAN_INVALID_ARGUMENT, "workload_size was 0");
  313. return GEARMAN_INVALID_ARGUMENT;
  314. }
  315. gearman_return_t ret= gearman_packet_create_args(universal, packet, GEARMAN_MAGIC_REQUEST,
  316. GEARMAN_COMMAND_ECHO_REQ,
  317. &workload, &workload_size, 1);
  318. if (gearman_failed(ret))
  319. {
  320. return ret;
  321. }
  322. _push_blocking(universal, orig_block_universal);
  323. for (gearman_connection_st *con= universal.con_list; con; con= con->next)
  324. {
  325. ret= con->send(packet, true);
  326. if (gearman_failed(ret))
  327. {
  328. goto exit;
  329. }
  330. con->options.packet_in_use= true;
  331. gearman_packet_st *packet_ptr= con->receiving(con->_packet, ret, true);
  332. if (gearman_failed(ret))
  333. {
  334. con->free_private_packet();
  335. goto exit;
  336. }
  337. assert(packet_ptr);
  338. if (con->_packet.data_size != workload_size or
  339. memcmp(workload, con->_packet.data, workload_size))
  340. {
  341. con->free_private_packet();
  342. gearman_error(universal, GEARMAN_ECHO_DATA_CORRUPTION, "corruption during echo");
  343. ret= GEARMAN_ECHO_DATA_CORRUPTION;
  344. goto exit;
  345. }
  346. con->free_private_packet();
  347. }
  348. ret= GEARMAN_SUCCESS;
  349. exit:
  350. gearman_packet_free(&packet);
  351. _pop_non_blocking(universal, orig_block_universal);
  352. return ret;
  353. }
  354. bool gearman_request_option(gearman_universal_st &universal,
  355. gearman_string_t &option)
  356. {
  357. gearman_packet_st packet;
  358. bool orig_block_universal;
  359. const void *args[]= { gearman_c_str(option) };
  360. size_t args_size[]= { gearman_size(option) };
  361. gearman_return_t ret;
  362. ret= gearman_packet_create_args(universal, packet, GEARMAN_MAGIC_REQUEST,
  363. GEARMAN_COMMAND_OPTION_REQ,
  364. args, args_size, 1);
  365. if (gearman_failed(ret))
  366. {
  367. gearman_error(universal, GEARMAN_MEMORY_ALLOCATION_FAILURE, "gearman_packet_create_args()");
  368. return ret;
  369. }
  370. _push_blocking(universal, orig_block_universal);
  371. for (gearman_connection_st *con= universal.con_list; con != NULL; con= con->next)
  372. {
  373. ret= con->send(packet, true);
  374. if (gearman_failed(ret))
  375. {
  376. goto exit;
  377. }
  378. con->options.packet_in_use= true;
  379. gearman_packet_st *packet_ptr= con->receiving(con->_packet, ret, true);
  380. if (gearman_failed(ret))
  381. {
  382. con->free_private_packet();
  383. goto exit;
  384. }
  385. assert(packet_ptr);
  386. if (packet_ptr->command == GEARMAN_COMMAND_ERROR)
  387. {
  388. con->free_private_packet();
  389. gearman_error(universal, GEARMAN_INVALID_ARGUMENT, "invalid server option");
  390. ret= GEARMAN_INVALID_ARGUMENT;;
  391. goto exit;
  392. }
  393. con->free_private_packet();
  394. }
  395. ret= GEARMAN_SUCCESS;
  396. exit:
  397. gearman_packet_free(&packet);
  398. _pop_non_blocking(universal, orig_block_universal);
  399. return gearman_success(ret);
  400. }
  401. void gearman_free_all_packets(gearman_universal_st &universal)
  402. {
  403. while (universal.packet_list)
  404. {
  405. gearman_packet_free(universal.packet_list);
  406. }
  407. }
  408. /*
  409. * Local Definitions
  410. */
  411. void gearman_universal_set_namespace(gearman_universal_st& universal, const char *namespace_key, size_t namespace_key_size)
  412. {
  413. gearman_string_free(universal._namespace);
  414. universal._namespace= gearman_string_create(NULL, namespace_key, namespace_key_size);
  415. }