add.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. #include <config.h>
  39. #include <libgearman/common.h>
  40. #include <libgearman/universal.hpp>
  41. #include <libgearman/add.hpp>
  42. #include <libgearman/packet.hpp>
  43. #include "libgearman/assert.hpp"
  44. #include "libgearman/uuid.hpp"
  45. #include <cerrno>
  46. #include <cstdio>
  47. #include <cstdlib>
  48. #include <cstring>
  49. #include <memory>
  50. gearman_task_st *add_task(gearman_client_st& client,
  51. void *context,
  52. gearman_command_t command,
  53. const gearman_string_t &function,
  54. gearman_unique_t &unique,
  55. const gearman_string_t &workload,
  56. time_t when,
  57. const gearman_actions_t &actions)
  58. {
  59. return add_task(client, NULL, context, command, function, unique, workload, when, actions);
  60. }
  61. gearman_task_st *add_task_ptr(gearman_client_st& client,
  62. gearman_task_st *task,
  63. void *context,
  64. gearman_command_t command,
  65. const char *function_name,
  66. const char *unique,
  67. const void *workload_str, size_t workload_size,
  68. time_t when,
  69. gearman_return_t *ret_ptr,
  70. const gearman_actions_t &actions)
  71. {
  72. gearman_return_t unused;
  73. if (ret_ptr == NULL)
  74. {
  75. ret_ptr= &unused;
  76. }
  77. gearman_string_t function= { gearman_string_param_cstr(function_name) };
  78. gearman_unique_t local_unique= gearman_unique_make(unique, unique ? strlen(unique) : 0);
  79. gearman_string_t workload= { static_cast<const char *>(workload_str), workload_size };
  80. task= add_task(client, task, context, command, function, local_unique, workload, when, actions);
  81. if (task == NULL)
  82. {
  83. *ret_ptr= gearman_universal_error_code(client.universal);
  84. return NULL;
  85. }
  86. *ret_ptr= GEARMAN_SUCCESS;
  87. return task;
  88. }
  89. gearman_task_st *add_task(gearman_client_st& client,
  90. gearman_task_st *task_shell,
  91. void *context,
  92. gearman_command_t command,
  93. const gearman_string_t &function,
  94. gearman_unique_t &unique,
  95. const gearman_string_t &workload,
  96. time_t when,
  97. const gearman_actions_t &actions)
  98. {
  99. if (gearman_size(function) == 0 or gearman_c_str(function) == NULL or gearman_size(function) > GEARMAN_FUNCTION_MAX_SIZE)
  100. {
  101. if (gearman_size(function) > GEARMAN_FUNCTION_MAX_SIZE)
  102. {
  103. gearman_error(client.universal, GEARMAN_INVALID_ARGUMENT, "function name longer then GEARMAN_MAX_FUNCTION_SIZE");
  104. }
  105. else
  106. {
  107. gearman_error(client.universal, GEARMAN_INVALID_ARGUMENT, "invalid function");
  108. }
  109. return NULL;
  110. }
  111. if (gearman_size(unique) > GEARMAN_MAX_UNIQUE_SIZE)
  112. {
  113. gearman_error(client.universal, GEARMAN_INVALID_ARGUMENT, "unique name longer then GEARMAN_MAX_UNIQUE_SIZE");
  114. return NULL;
  115. }
  116. if ((gearman_size(workload) && gearman_c_str(workload) == NULL) or (gearman_size(workload) == 0 && gearman_c_str(workload)))
  117. {
  118. gearman_error(client.universal, GEARMAN_INVALID_ARGUMENT, "invalid workload");
  119. return NULL;
  120. }
  121. task_shell= gearman_task_internal_create(client, task_shell);
  122. if (task_shell == NULL or task_shell->impl() == NULL)
  123. {
  124. gearman_error(client.universal, GEARMAN_MEMORY_ALLOCATION_FAILURE, "");
  125. return NULL;
  126. }
  127. assert(task_shell);
  128. assert(task_shell->impl());
  129. assert(task_shell->impl()->client);
  130. assert(task_shell->impl()->client == &client);
  131. Task* task= task_shell->impl();
  132. task->context= context;
  133. task->func= actions;
  134. if (gearman_size(unique))
  135. {
  136. task->unique_length= gearman_size(unique);
  137. memcpy(task->unique, gearman_c_str(unique), gearman_size(unique));
  138. }
  139. else
  140. {
  141. uuid_t uuid;
  142. safe_uuid_generate(uuid);
  143. uuid_unparse(uuid, task->unique);
  144. task->unique_length= GEARMAN_MAX_UUID_SIZE;
  145. }
  146. task->unique[task->unique_length]= 0;
  147. assert(task->client);
  148. assert(task->client == &client);
  149. gearman_return_t rc= GEARMAN_INVALID_ARGUMENT;
  150. switch (command)
  151. {
  152. case GEARMAN_COMMAND_SUBMIT_JOB:
  153. case GEARMAN_COMMAND_SUBMIT_JOB_LOW:
  154. case GEARMAN_COMMAND_SUBMIT_JOB_HIGH:
  155. rc= libgearman::protocol::submit(*task,
  156. command,
  157. function,
  158. workload);
  159. break;
  160. case GEARMAN_COMMAND_SUBMIT_JOB_EPOCH:
  161. rc= libgearman::protocol::submit_epoch(*task,
  162. function,
  163. workload,
  164. when);
  165. break;
  166. case GEARMAN_COMMAND_SUBMIT_JOB_BG:
  167. case GEARMAN_COMMAND_SUBMIT_JOB_LOW_BG:
  168. case GEARMAN_COMMAND_SUBMIT_JOB_HIGH_BG:
  169. rc= libgearman::protocol::submit_background(*task,
  170. command,
  171. function,
  172. workload);
  173. break;
  174. case GEARMAN_COMMAND_SUBMIT_REDUCE_JOB:
  175. case GEARMAN_COMMAND_SUBMIT_REDUCE_JOB_BACKGROUND:
  176. assert(0);
  177. rc= GEARMAN_INVALID_ARGUMENT;
  178. break;
  179. case GEARMAN_COMMAND_SUBMIT_JOB_SCHED:
  180. case GEARMAN_COMMAND_ALL_YOURS:
  181. case GEARMAN_COMMAND_CANT_DO:
  182. case GEARMAN_COMMAND_CAN_DO:
  183. case GEARMAN_COMMAND_CAN_DO_TIMEOUT:
  184. case GEARMAN_COMMAND_ECHO_REQ:
  185. case GEARMAN_COMMAND_ECHO_RES:
  186. case GEARMAN_COMMAND_ERROR:
  187. case GEARMAN_COMMAND_GET_STATUS:
  188. case GEARMAN_COMMAND_GRAB_JOB:
  189. case GEARMAN_COMMAND_GRAB_JOB_ALL:
  190. case GEARMAN_COMMAND_GRAB_JOB_UNIQ:
  191. case GEARMAN_COMMAND_JOB_ASSIGN:
  192. case GEARMAN_COMMAND_JOB_ASSIGN_ALL:
  193. case GEARMAN_COMMAND_JOB_ASSIGN_UNIQ:
  194. case GEARMAN_COMMAND_JOB_CREATED:
  195. case GEARMAN_COMMAND_MAX:
  196. case GEARMAN_COMMAND_NOOP:
  197. case GEARMAN_COMMAND_NO_JOB:
  198. case GEARMAN_COMMAND_OPTION_REQ:
  199. case GEARMAN_COMMAND_OPTION_RES:
  200. case GEARMAN_COMMAND_PRE_SLEEP:
  201. case GEARMAN_COMMAND_RESET_ABILITIES:
  202. case GEARMAN_COMMAND_SET_CLIENT_ID:
  203. case GEARMAN_COMMAND_STATUS_RES:
  204. case GEARMAN_COMMAND_TEXT:
  205. case GEARMAN_COMMAND_UNUSED:
  206. case GEARMAN_COMMAND_WORK_COMPLETE:
  207. case GEARMAN_COMMAND_WORK_DATA:
  208. case GEARMAN_COMMAND_WORK_EXCEPTION:
  209. case GEARMAN_COMMAND_WORK_FAIL:
  210. case GEARMAN_COMMAND_WORK_STATUS:
  211. case GEARMAN_COMMAND_WORK_WARNING:
  212. case GEARMAN_COMMAND_GET_STATUS_UNIQUE:
  213. case GEARMAN_COMMAND_STATUS_RES_UNIQUE:
  214. assert(0);
  215. rc= GEARMAN_INVALID_ARGUMENT;
  216. break;
  217. }
  218. if (gearman_success(rc))
  219. {
  220. client.new_tasks++;
  221. client.running_tasks++;
  222. task->options.send_in_use= true;
  223. return task->shell();
  224. }
  225. gearman_task_free(task->shell());
  226. return NULL;
  227. }
  228. gearman_task_st *add_reducer_task(gearman_client_st *client,
  229. gearman_command_t command,
  230. const gearman_job_priority_t,
  231. const gearman_string_t &function,
  232. const gearman_string_t &reducer,
  233. const gearman_unique_t &unique,
  234. const gearman_string_t &workload,
  235. const gearman_actions_t &actions,
  236. const time_t,
  237. void *context)
  238. {
  239. uuid_t uuid;
  240. const void *args[5];
  241. size_t args_size[5];
  242. if (gearman_size(function) == 0 or gearman_c_str(function) == NULL or gearman_size(function) > GEARMAN_FUNCTION_MAX_SIZE)
  243. {
  244. if (gearman_size(function) > GEARMAN_FUNCTION_MAX_SIZE)
  245. {
  246. gearman_error(client->universal, GEARMAN_INVALID_ARGUMENT, "function name longer then GEARMAN_MAX_FUNCTION_SIZE");
  247. }
  248. else
  249. {
  250. gearman_error(client->universal, GEARMAN_INVALID_ARGUMENT, "invalid function");
  251. }
  252. return NULL;
  253. }
  254. if (gearman_size(unique) > GEARMAN_MAX_UNIQUE_SIZE)
  255. {
  256. gearman_error(client->universal, GEARMAN_INVALID_ARGUMENT, "unique name longer then GEARMAN_MAX_UNIQUE_SIZE");
  257. return NULL;
  258. }
  259. if ((gearman_size(workload) and not gearman_c_str(workload)) or (gearman_size(workload) == 0 && gearman_c_str(workload)))
  260. {
  261. gearman_error(client->universal, GEARMAN_INVALID_ARGUMENT, "invalid workload");
  262. return NULL;
  263. }
  264. gearman_task_st *task= gearman_task_internal_create(*client, NULL);
  265. if (task == NULL)
  266. {
  267. gearman_error(client->universal, GEARMAN_MEMORY_ALLOCATION_FAILURE, "");
  268. return NULL;
  269. }
  270. task->impl()->context= context;
  271. task->impl()->func= actions;
  272. /**
  273. @todo fix it so that NULL is done by default by the API not by happenstance.
  274. */
  275. char function_buffer[1024];
  276. if (client->universal._namespace)
  277. {
  278. char *ptr= function_buffer;
  279. memcpy(ptr, gearman_string_value(client->universal._namespace), gearman_string_length(client->universal._namespace));
  280. ptr+= gearman_string_length(client->universal._namespace);
  281. memcpy(ptr, gearman_c_str(function), gearman_size(function) +1);
  282. ptr+= gearman_size(function);
  283. args[0]= function_buffer;
  284. args_size[0]= ptr- function_buffer +1;
  285. }
  286. else
  287. {
  288. args[0]= gearman_c_str(function);
  289. args_size[0]= gearman_size(function) + 1;
  290. }
  291. if (gearman_size(unique))
  292. {
  293. args[1]= gearman_c_str(unique);
  294. args_size[1]= gearman_size(unique) + 1;
  295. strncpy(task->impl()->unique, gearman_c_str(unique), gearman_size(unique));
  296. }
  297. else
  298. {
  299. safe_uuid_generate(uuid);
  300. uuid_unparse(uuid, task->impl()->unique);
  301. task->impl()->unique[GEARMAN_MAX_UUID_SIZE]= 0;
  302. args[1]= task->impl()->unique;
  303. args_size[1]= GEARMAN_MAX_UUID_SIZE +1; // +1 is for the needed null
  304. }
  305. assert_msg(command == GEARMAN_COMMAND_SUBMIT_REDUCE_JOB or command == GEARMAN_COMMAND_SUBMIT_REDUCE_JOB_BACKGROUND,
  306. "Command was not appropriate for request");
  307. char reducer_buffer[1024];
  308. if (client->universal._namespace)
  309. {
  310. char *ptr= reducer_buffer;
  311. memcpy(ptr, gearman_string_value(client->universal._namespace), gearman_string_length(client->universal._namespace));
  312. ptr+= gearman_string_length(client->universal._namespace);
  313. memcpy(ptr, gearman_c_str(reducer), gearman_size(reducer) +1);
  314. ptr+= gearman_size(reducer);
  315. args[2]= reducer_buffer;
  316. args_size[2]= ptr- reducer_buffer +1;
  317. }
  318. else
  319. {
  320. args[2]= gearman_c_str(reducer);
  321. args_size[2]= gearman_size(reducer) +1;
  322. }
  323. char aggregate[1];
  324. aggregate[0]= 0;
  325. args[3]= aggregate;
  326. args_size[3]= 1;
  327. assert_msg(gearman_c_str(workload), "Invalid workload (NULL)");
  328. assert_msg(gearman_size(workload), "Invalid workload of zero");
  329. args[4]= gearman_c_str(workload);
  330. args_size[4]= gearman_size(workload);
  331. gearman_return_t rc;
  332. if (gearman_success(rc= gearman_packet_create_args(client->universal, task->impl()->send,
  333. GEARMAN_MAGIC_REQUEST, command,
  334. args, args_size,
  335. 5)))
  336. {
  337. client->new_tasks++;
  338. client->running_tasks++;
  339. task->impl()->options.send_in_use= true;
  340. }
  341. else
  342. {
  343. gearman_gerror(client->universal, rc);
  344. gearman_task_free(task);
  345. task= NULL;
  346. }
  347. task->impl()->type= GEARMAN_TASK_KIND_EXECUTE;
  348. return task;
  349. }