queue.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011-2012 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 libmemcached Queue Storage Definitions
  41. */
  42. #include <libgearman-server/plugins/queue/libmemcached/queue.h>
  43. #if defined(GEARMAND_PLUGINS_QUEUE_LIBMEMCACHED_H)
  44. #pragma GCC diagnostic push
  45. #pragma GCC diagnostic ignored "-Wold-style-cast"
  46. using namespace gearmand;
  47. /**
  48. * @addtogroup gearmand::plugins::queue::Libmemcachedatic Static libmemcached Queue Storage Functions
  49. * @ingroup gearman_queue_libmemcached
  50. * @{
  51. */
  52. /**
  53. * Default values.
  54. */
  55. namespace gearmand { namespace plugins { namespace queue { class Libmemcached; }}}
  56. namespace gearmand {
  57. namespace queue {
  58. class LibmemcachedQueue : public gearmand::queue::Context
  59. {
  60. public:
  61. LibmemcachedQueue(memcached_server_st* servers, const std::string& prefix):
  62. memc_(nullptr),
  63. prefix_(prefix)
  64. {
  65. memc_= memcached_create(nullptr);
  66. memcached_server_push(memc_, servers);
  67. }
  68. bool init()
  69. {
  70. return bool(bool(memc_) and memcached_server_count(memc_));
  71. }
  72. ~LibmemcachedQueue()
  73. {
  74. memcached_free(memc_);
  75. memc_= nullptr;
  76. }
  77. gearmand_error_t add(gearman_server_st *server,
  78. const char *unique, size_t unique_size,
  79. const char *function_name, size_t function_name_size,
  80. const void *data, size_t data_size,
  81. gearman_job_priority_t priority,
  82. int64_t when);
  83. gearmand_error_t flush(gearman_server_st *server);
  84. gearmand_error_t done(gearman_server_st *server,
  85. const char *unique, size_t unique_size,
  86. const char *function_name, size_t function_name_size);
  87. gearmand_error_t replay(gearman_server_st *server);
  88. private:
  89. memcached_st* memc_;
  90. const std::string prefix_;
  91. };
  92. } // namespace queue
  93. } // namespace gearmand
  94. namespace gearmand {
  95. namespace plugins {
  96. namespace queue {
  97. class Libmemcached : public gearmand::plugins::Queue {
  98. public:
  99. Libmemcached ();
  100. ~Libmemcached ();
  101. gearmand_error_t initialize();
  102. std::string server_list;
  103. std::string prefix;
  104. private:
  105. };
  106. Libmemcached::Libmemcached() :
  107. Queue("libmemcached")
  108. {
  109. command_line_options().add_options()
  110. ("libmemcached-servers", boost::program_options::value(&server_list), "List of Memcached servers to use.")
  111. ("libmemcached-prefix", boost::program_options::value(&prefix)->default_value("gear_"), "Prefix to use in memcached keys");
  112. }
  113. Libmemcached::~Libmemcached()
  114. {
  115. }
  116. gearmand_error_t Libmemcached::initialize()
  117. {
  118. gearmand_info("Initializing libmemcached module");
  119. memcached_server_st *servers= memcached_servers_parse(server_list.c_str());
  120. if (!servers)
  121. {
  122. return gearmand_gerror("memcached_servers_parse", GEARMAND_QUEUE_ERROR);
  123. }
  124. gearmand::queue::LibmemcachedQueue* exec_queue = new gearmand::queue::LibmemcachedQueue { servers, prefix };
  125. if (exec_queue and exec_queue->init())
  126. {
  127. gearman_server_set_queue(Gearmand()->server, exec_queue);
  128. memcached_server_list_free(servers);
  129. return GEARMAND_SUCCESS;
  130. }
  131. return gearmand_gerror("Libmemcached::initialize()", GEARMAND_QUEUE_ERROR);
  132. }
  133. void initialize_libmemcached()
  134. {
  135. static Libmemcached local_instance {};
  136. }
  137. } // namespace queue
  138. } // namespace plugins
  139. } // namespace gearmand
  140. /* Queue callback functions. */
  141. namespace gearmand {
  142. namespace queue {
  143. gearmand_error_t LibmemcachedQueue::add(gearman_server_st *server,
  144. const char *unique,
  145. size_t unique_size,
  146. const char *function_name,
  147. size_t function_name_size,
  148. const void *data, size_t data_size,
  149. gearman_job_priority_t priority,
  150. int64_t when)
  151. {
  152. if (when) // No support for EPOCH jobs
  153. {
  154. return gearmand_gerror("libmemcached queue does not support epoch jobs", GEARMAND_QUEUE_ERROR);
  155. }
  156. (void)server;
  157. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM, "libmemcached add: %.*s", (uint32_t)unique_size, (char *)unique);
  158. char key[MEMCACHED_MAX_KEY];
  159. size_t key_length= (size_t)snprintf(key, MEMCACHED_MAX_KEY, "%s%.*s-%.*s",
  160. prefix_.c_str(),
  161. (int)function_name_size,
  162. (const char *)function_name, (int)unique_size,
  163. (const char *)unique);
  164. memcached_return_t rc= memcached_set(memc_, (const char *)key, key_length,
  165. (const char *)data, data_size, 0, (uint32_t)priority);
  166. if (!memcached_success(rc))
  167. {
  168. return gearmand_gerror(memcached_strerror(memc_, rc), GEARMAND_QUEUE_ERROR);
  169. }
  170. return GEARMAND_SUCCESS;
  171. }
  172. gearmand_error_t LibmemcachedQueue::flush(gearman_server_st *)
  173. {
  174. gearmand_debug("libmemcached flush");
  175. return GEARMAND_SUCCESS;
  176. }
  177. gearmand_error_t LibmemcachedQueue::done(gearman_server_st*,
  178. const char *unique, size_t unique_size,
  179. const char *function_name, size_t function_name_size)
  180. {
  181. char key[MEMCACHED_MAX_KEY];
  182. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM, "libmemcached done: %.*s", (uint32_t)unique_size, (char *)unique);
  183. size_t key_length= (size_t)snprintf(key, MEMCACHED_MAX_KEY, "%s%.*s-%.*s",
  184. prefix_.c_str(),
  185. (int)function_name_size,
  186. (const char *)function_name, (int)unique_size,
  187. (const char *)unique);
  188. /* For the moment we will assume it happened */
  189. memcached_return_t rc= memcached_delete(memc_, (const char *)key, key_length, 0);
  190. if (!memcached_success(rc))
  191. {
  192. if(rc != MEMCACHED_NOTFOUND)
  193. return gearmand_gerror(memcached_strerror(memc_, rc), GEARMAND_QUEUE_ERROR);
  194. }
  195. return GEARMAND_SUCCESS;
  196. }
  197. class Replay
  198. {
  199. public:
  200. Replay(gearman_server_st* server_arg, memcached_st* _memc, const std::string& _prefix) :
  201. server_(server_arg),
  202. memc_(nullptr),
  203. prefix_(_prefix)
  204. {
  205. memc_= memcached_clone(nullptr, _memc);
  206. }
  207. ~Replay()
  208. {
  209. memcached_free(memc_);
  210. memc_= nullptr;
  211. }
  212. bool init()
  213. {
  214. return bool(memc_);
  215. }
  216. memcached_st* memc()
  217. {
  218. assert(memc_);
  219. return memc_;
  220. }
  221. gearman_server_st* server()
  222. {
  223. return server_;
  224. }
  225. const char* prefix()
  226. {
  227. return prefix_.c_str();
  228. }
  229. private:
  230. gearman_server_st* server_;
  231. memcached_st* memc_;
  232. const std::string prefix_;
  233. };
  234. static memcached_return_t callback_loader(const memcached_st*,
  235. memcached_result_st* result,
  236. void *context)
  237. {
  238. Replay* replay= (Replay*)context;
  239. const char *key= memcached_result_key_value(result);
  240. const char *prefix= replay->prefix();
  241. if (strncmp(key, prefix, strlen(prefix)) != 0)
  242. {
  243. gearmand_debug("memcached key did not match specified prefix");
  244. return MEMCACHED_SUCCESS;
  245. }
  246. const char* function= key +strlen(prefix);
  247. const char* unique= index(function, '-');
  248. if (!unique)
  249. {
  250. gearmand_debug("memcached key was malformed was not found");
  251. return MEMCACHED_SUCCESS;
  252. }
  253. size_t function_len= size_t(unique -function);
  254. unique++;
  255. size_t unique_size= strlen(unique);
  256. assert(function);
  257. assert(function_len);
  258. /* need to make a copy here ... gearman_server_job_free will free it later */
  259. char* data= (char*)malloc(memcached_result_length(result));
  260. if (!data)
  261. {
  262. gearmand_perror(errno, "malloc");
  263. return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
  264. }
  265. memcpy(data, memcached_result_value(result), memcached_result_length(result));
  266. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM, "libmemcached replay_add: %.*s", (uint32_t)unique_size, (char *)unique);
  267. /* Currently not looking at failure cases */
  268. LibmemcachedQueue::replay_add(replay->server(),
  269. nullptr,
  270. unique, unique_size,
  271. function, function_len,
  272. data, memcached_result_length(result),
  273. static_cast<gearman_job_priority_t>(memcached_result_flags(result)), int64_t(0));
  274. return MEMCACHED_SUCCESS;
  275. }
  276. /* Grab the object and load it into the loader */
  277. static memcached_return_t callback_for_key(const memcached_st*,
  278. const char *key, size_t key_length,
  279. void *context)
  280. {
  281. Replay* replay= (Replay*)context;
  282. memcached_execute_fn callbacks{(memcached_execute_fn)&callback_loader};
  283. char *passable{(char *)key};
  284. memcached_return_t rc = memcached_mget(replay->memc(), &passable, &key_length, 1);
  285. if (memcached_success(rc))
  286. {
  287. gearmand_debug(memcached_strerror(replay->memc(), rc));
  288. }
  289. /* Just void errors for the moment, since other treads might have picked up the object. */
  290. (void)memcached_fetch_execute(replay->memc(), &callbacks, replay, 1);
  291. return MEMCACHED_SUCCESS;
  292. }
  293. /*
  294. If we have any failures for loading values back into replay we just ignore them.
  295. */
  296. gearmand_error_t LibmemcachedQueue::replay(gearman_server_st *server)
  297. {
  298. memcached_dump_fn callbacks{(memcached_dump_fn)&callback_for_key};
  299. gearmand_debug("libmemcached replay start");
  300. memcached_st* local_clone= memcached_clone(nullptr, memc_);
  301. if (local_clone)
  302. {
  303. Replay replay_exec(server, memc_, prefix_);
  304. if (replay_exec.init())
  305. {
  306. (void)memcached_dump(local_clone, &callbacks, (void *)&replay_exec, 1);
  307. }
  308. else
  309. {
  310. gearmand_debug("libmemcached failed to init() Replay");
  311. }
  312. memcached_free(local_clone);
  313. local_clone= nullptr;
  314. }
  315. gearmand_debug("libmemcached replay stop");
  316. return GEARMAND_SUCCESS;
  317. }
  318. } // queue
  319. } // gearmand
  320. #pragma GCC diagnostic pop
  321. #endif // defined(GEARMAND_PLUGINS_QUEUE_LIBMEMCACHED_H)