queue.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. * 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. /**
  38. * @file
  39. * @brief Redis Queue Storage Definitions
  40. */
  41. #include <gear_config.h>
  42. #include <libgearman-server/common.h>
  43. #include <libgearman-server/plugins/queue/redis/queue.h>
  44. #include <libgearman-server/plugins/queue/base.h>
  45. #if defined(HAVE_HIREDIS) && HAVE_HIREDIS
  46. #include <hiredis/hiredis.h>
  47. /* Queue callback functions. */
  48. static gearmand_error_t _hiredis_add(gearman_server_st *server, void *context,
  49. const char *unique,
  50. size_t unique_size,
  51. const char *function_name,
  52. size_t function_name_size,
  53. const void *data, size_t data_size,
  54. gearman_job_priority_t priority,
  55. int64_t when);
  56. static gearmand_error_t _hiredis_flush(gearman_server_st *server, void *context);
  57. static gearmand_error_t _hiredis_done(gearman_server_st *server, void *context,
  58. const char *unique,
  59. size_t unique_size,
  60. const char *function_name,
  61. size_t function_name_size);
  62. static gearmand_error_t _hiredis_replay(gearman_server_st *server, void *context,
  63. gearman_queue_add_fn *add_fn,
  64. void *add_context);
  65. namespace gearmand { namespace plugins { namespace queue { class Hiredis; }}}
  66. namespace gearmand {
  67. namespace plugins {
  68. namespace queue {
  69. class Hiredis : public Queue {
  70. public:
  71. Hiredis();
  72. ~Hiredis();
  73. gearmand_error_t initialize();
  74. redisContext* redis()
  75. {
  76. return _redis;
  77. }
  78. std::string server;
  79. std::string service;
  80. private:
  81. redisContext *_redis;
  82. };
  83. Hiredis::Hiredis() :
  84. Queue("redis"),
  85. server("127.0.0.1"),
  86. service("6379"),
  87. _redis(NULL)
  88. {
  89. command_line_options().add_options()
  90. ("redis-server", boost::program_options::value(&server), "Redis server")
  91. ("redis-port", boost::program_options::value(&service), "Redis server port/service");
  92. }
  93. Hiredis::~Hiredis()
  94. {
  95. }
  96. gearmand_error_t Hiredis::initialize()
  97. {
  98. int service_port= atoi(service.c_str());
  99. if ((_redis= redisConnect("127.0.0.1", service_port)) == NULL)
  100. {
  101. return gearmand_gerror("Could not connect to redis server", GEARMAND_QUEUE_ERROR);
  102. }
  103. gearmand_info("Initializing hiredis module");
  104. gearman_server_set_queue(Gearmand()->server, this, _hiredis_add, _hiredis_flush, _hiredis_done, _hiredis_replay);
  105. return GEARMAND_SUCCESS;
  106. }
  107. void initialize_redis()
  108. {
  109. static Hiredis local_instance;
  110. }
  111. } // namespace queue
  112. } // namespace plugins
  113. } // namespace gearmand
  114. typedef std::vector<char> vchar_t;
  115. #define GEARMAND_QUEUE_GEARMAND_DEFAULT_PREFIX "_gear_"
  116. #define GEARMAND_QUEUE_GEARMAND_DEFAULT_PREFIX_SIZE sizeof(GEARMAND_QUEUE_GEARMAND_DEFAULT_PREFIX)
  117. #define GEARMAND_KEY_LITERAL "%s-%.*s-%*s"
  118. static size_t build_key(vchar_t &key,
  119. const char *unique,
  120. size_t unique_size,
  121. const char *function_name,
  122. size_t function_name_size)
  123. {
  124. key.resize(function_name_size +unique_size +GEARMAND_QUEUE_GEARMAND_DEFAULT_PREFIX_SIZE +4);
  125. int key_size= snprintf(&key[0], key.size(), GEARMAND_KEY_LITERAL,
  126. GEARMAND_QUEUE_GEARMAND_DEFAULT_PREFIX,
  127. (int)function_name_size, function_name,
  128. (int)unique_size, unique);
  129. if (size_t(key_size) >= key.size() or key_size <= 0)
  130. {
  131. assert(0);
  132. return -1;
  133. }
  134. return key.size();
  135. }
  136. /**
  137. * @addtogroup gearman_queue_hiredis hiredis Queue Storage Functions
  138. * @ingroup gearman_queue
  139. * @{
  140. */
  141. /*
  142. * Private declarations
  143. */
  144. #pragma GCC diagnostic push
  145. #pragma GCC diagnostic ignored "-Wold-style-cast"
  146. /*
  147. * Private definitions
  148. */
  149. static gearmand_error_t _hiredis_add(gearman_server_st *, void *context,
  150. const char *unique,
  151. size_t unique_size,
  152. const char *function_name,
  153. size_t function_name_size,
  154. const void *data, size_t data_size,
  155. gearman_job_priority_t,
  156. int64_t when)
  157. {
  158. gearmand::plugins::queue::Hiredis *queue= (gearmand::plugins::queue::Hiredis *)context;
  159. if (when) // No support for EPOCH jobs
  160. {
  161. return GEARMAND_QUEUE_ERROR;
  162. }
  163. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM, "hires add: %.*s", (uint32_t)unique_size, (char *)unique);
  164. std::vector<char> key;
  165. build_key(key, unique, unique_size, function_name, function_name_size);
  166. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM, "hires key: %u", (uint32_t)key.size());
  167. redisReply *reply= (redisReply*)redisCommand(queue->redis(), "SET %b %b", &key[0], key.size(), data, data_size);
  168. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM, "got reply");
  169. if (reply == NULL)
  170. {
  171. return gearmand_log_gerror(GEARMAN_DEFAULT_LOG_PARAM, GEARMAND_QUEUE_ERROR, "failed to insert '%.*s' into redis", key.size(), &key[0]);
  172. }
  173. freeReplyObject(reply);
  174. return GEARMAND_SUCCESS;
  175. }
  176. static gearmand_error_t _hiredis_flush(gearman_server_st *, void *)
  177. {
  178. return GEARMAND_SUCCESS;
  179. }
  180. static gearmand_error_t _hiredis_done(gearman_server_st *, void *context,
  181. const char *unique,
  182. size_t unique_size,
  183. const char *function_name,
  184. size_t function_name_size)
  185. {
  186. gearmand::plugins::queue::Hiredis *queue= (gearmand::plugins::queue::Hiredis *)context;
  187. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM, "hires done: %.*s", (uint32_t)unique_size, (char *)unique);
  188. std::vector<char> key;
  189. build_key(key, unique, unique_size, function_name, function_name_size);
  190. redisReply *reply= (redisReply*)redisCommand(queue->redis(), "DEL %b", &key[0], key.size());
  191. if (reply == NULL)
  192. {
  193. return GEARMAND_QUEUE_ERROR;
  194. }
  195. freeReplyObject(reply);
  196. return GEARMAND_SUCCESS;
  197. }
  198. #pragma GCC diagnostic push
  199. #pragma GCC diagnostic ignored "-Wformat-nonliteral"
  200. static gearmand_error_t _hiredis_replay(gearman_server_st *server, void *context,
  201. gearman_queue_add_fn *add_fn,
  202. void *add_context)
  203. {
  204. gearmand::plugins::queue::Hiredis *queue= (gearmand::plugins::queue::Hiredis *)context;
  205. gearmand_info("hiredis replay start");
  206. redisReply *reply= (redisReply*)redisCommand(queue->redis(), "KEYS %s", GEARMAND_QUEUE_GEARMAND_DEFAULT_PREFIX);
  207. if (reply == NULL)
  208. {
  209. return gearmand_gerror("Failed to call KEYS during QUEUE replay", GEARMAND_QUEUE_ERROR);
  210. }
  211. for (size_t x= 0; x < reply->elements; x++)
  212. {
  213. char prefix[GEARMAND_QUEUE_GEARMAND_DEFAULT_PREFIX_SIZE];
  214. char function_name[GEARMAN_FUNCTION_MAX_SIZE];
  215. char unique[GEARMAN_MAX_UNIQUE_SIZE];
  216. char fmt_str[100] = "";
  217. int fmt_str_length= snprintf(fmt_str, sizeof(fmt_str), "%%%ds-%%%ds-%%%ds",
  218. int(GEARMAND_QUEUE_GEARMAND_DEFAULT_PREFIX_SIZE),
  219. int(GEARMAN_FUNCTION_MAX_SIZE),
  220. int(GEARMAN_MAX_UNIQUE_SIZE));
  221. if (fmt_str_length <= 0 or size_t(fmt_str_length) >= sizeof(fmt_str))
  222. {
  223. assert(fmt_str_length != 1);
  224. return gearmand_gerror("snprintf() failed to produce a valud fmt_str for redis key", GEARMAND_QUEUE_ERROR);
  225. }
  226. int ret= sscanf(reply->element[x]->str,
  227. fmt_str,
  228. prefix,
  229. function_name,
  230. unique);
  231. if (ret == 0)
  232. {
  233. continue;
  234. }
  235. redisReply *get_reply= (redisReply*)redisCommand(queue->redis(), "GET %s", reply->element[x]->str);
  236. if (get_reply == NULL)
  237. {
  238. continue;
  239. }
  240. (void)(add_fn)(server, add_context,
  241. unique, strlen(unique),
  242. function_name, strlen(function_name),
  243. get_reply->str, get_reply->len,
  244. GEARMAN_JOB_PRIORITY_NORMAL, 0);
  245. freeReplyObject(get_reply);
  246. }
  247. freeReplyObject(reply);
  248. return GEARMAND_SUCCESS;
  249. }
  250. #pragma GCC diagnostic pop
  251. #pragma GCC diagnostic pop
  252. #endif // defined(HAVE_HIREDIS) && HAVE_HIREDIS