add.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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 "gear_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/log.hpp"
  45. #include "libgearman/vector.h"
  46. #include "libgearman/uuid.hpp"
  47. #include "libhashkit-1.0/hashkit.h"
  48. #include <cerrno>
  49. #include <cstdio>
  50. #include <cstdlib>
  51. #include <cstring>
  52. #include <memory>
  53. namespace {
  54. bool is_background(gearman_command_t command)
  55. {
  56. switch (command)
  57. {
  58. case GEARMAN_COMMAND_SUBMIT_JOB_EPOCH:
  59. case GEARMAN_COMMAND_SUBMIT_JOB_SCHED:
  60. case GEARMAN_COMMAND_SUBMIT_JOB_BG:
  61. case GEARMAN_COMMAND_SUBMIT_JOB_LOW_BG:
  62. case GEARMAN_COMMAND_SUBMIT_JOB_HIGH_BG:
  63. case GEARMAN_COMMAND_SUBMIT_REDUCE_JOB_BACKGROUND:
  64. return true;
  65. case GEARMAN_COMMAND_SUBMIT_REDUCE_JOB:
  66. case GEARMAN_COMMAND_SUBMIT_JOB:
  67. case GEARMAN_COMMAND_SUBMIT_JOB_LOW:
  68. case GEARMAN_COMMAND_SUBMIT_JOB_HIGH:
  69. return true;
  70. case GEARMAN_COMMAND_ALL_YOURS:
  71. case GEARMAN_COMMAND_CANT_DO:
  72. case GEARMAN_COMMAND_CAN_DO:
  73. case GEARMAN_COMMAND_CAN_DO_TIMEOUT:
  74. case GEARMAN_COMMAND_ECHO_REQ:
  75. case GEARMAN_COMMAND_ECHO_RES:
  76. case GEARMAN_COMMAND_ERROR:
  77. case GEARMAN_COMMAND_GET_STATUS:
  78. case GEARMAN_COMMAND_GRAB_JOB:
  79. case GEARMAN_COMMAND_GRAB_JOB_ALL:
  80. case GEARMAN_COMMAND_GRAB_JOB_UNIQ:
  81. case GEARMAN_COMMAND_JOB_ASSIGN:
  82. case GEARMAN_COMMAND_JOB_ASSIGN_ALL:
  83. case GEARMAN_COMMAND_JOB_ASSIGN_UNIQ:
  84. case GEARMAN_COMMAND_JOB_CREATED:
  85. case GEARMAN_COMMAND_MAX:
  86. case GEARMAN_COMMAND_NOOP:
  87. case GEARMAN_COMMAND_NO_JOB:
  88. case GEARMAN_COMMAND_OPTION_REQ:
  89. case GEARMAN_COMMAND_OPTION_RES:
  90. case GEARMAN_COMMAND_PRE_SLEEP:
  91. case GEARMAN_COMMAND_RESET_ABILITIES:
  92. case GEARMAN_COMMAND_SET_CLIENT_ID:
  93. case GEARMAN_COMMAND_STATUS_RES:
  94. case GEARMAN_COMMAND_TEXT:
  95. case GEARMAN_COMMAND_UNUSED:
  96. case GEARMAN_COMMAND_WORK_COMPLETE:
  97. case GEARMAN_COMMAND_WORK_DATA:
  98. case GEARMAN_COMMAND_WORK_EXCEPTION:
  99. case GEARMAN_COMMAND_WORK_FAIL:
  100. case GEARMAN_COMMAND_WORK_STATUS:
  101. case GEARMAN_COMMAND_WORK_WARNING:
  102. case GEARMAN_COMMAND_GET_STATUS_UNIQUE:
  103. case GEARMAN_COMMAND_STATUS_RES_UNIQUE:
  104. assert(0);
  105. break;
  106. }
  107. return false;
  108. }
  109. } // namespace
  110. gearman_task_st *add_task(Client& client,
  111. void *context,
  112. gearman_command_t command,
  113. const gearman_string_t &function,
  114. gearman_unique_t &unique,
  115. const gearman_string_t &workload,
  116. time_t when,
  117. const gearman_actions_t &actions)
  118. {
  119. return add_task(client, NULL, context, command, function, unique, workload, when, actions);
  120. }
  121. gearman_task_st *add_task_ptr(Client& client,
  122. gearman_task_st *task,
  123. void *context,
  124. gearman_command_t command,
  125. const char *function_name,
  126. const char *unique,
  127. const void *workload_str, size_t workload_size,
  128. time_t when,
  129. gearman_return_t& ret_ptr,
  130. const gearman_actions_t &actions)
  131. {
  132. gearman_string_t function= { gearman_string_param_cstr(function_name) };
  133. gearman_unique_t local_unique= gearman_unique_make(unique, unique ? strlen(unique) : 0);
  134. gearman_string_t workload= { static_cast<const char *>(workload_str), workload_size };
  135. task= add_task(client, task, context, command, function, local_unique, workload, when, actions);
  136. if (task == NULL)
  137. {
  138. ret_ptr= client.universal.error_code();
  139. return NULL;
  140. }
  141. ret_ptr= GEARMAN_SUCCESS;
  142. return task;
  143. }
  144. gearman_task_st *add_task(Client& client,
  145. gearman_task_st *task_shell,
  146. void *context,
  147. gearman_command_t command,
  148. const gearman_string_t &function,
  149. gearman_unique_t &unique,
  150. const gearman_string_t &workload,
  151. time_t when,
  152. const gearman_actions_t &actions)
  153. {
  154. if (gearman_size(function) == 0 or gearman_c_str(function) == NULL or gearman_size(function) > GEARMAN_FUNCTION_MAX_SIZE)
  155. {
  156. if (gearman_size(function) > GEARMAN_FUNCTION_MAX_SIZE)
  157. {
  158. gearman_error(client.universal, GEARMAN_INVALID_ARGUMENT, "function name longer then GEARMAN_MAX_FUNCTION_SIZE");
  159. }
  160. else
  161. {
  162. gearman_error(client.universal, GEARMAN_INVALID_ARGUMENT, "invalid function");
  163. }
  164. return NULL;
  165. }
  166. if (gearman_size(unique) > GEARMAN_MAX_UNIQUE_SIZE)
  167. {
  168. gearman_error(client.universal, GEARMAN_INVALID_ARGUMENT, "unique name longer then GEARMAN_MAX_UNIQUE_SIZE");
  169. return NULL;
  170. }
  171. if ((gearman_size(workload) && gearman_c_str(workload) == NULL) or (gearman_size(workload) == 0 && gearman_c_str(workload)))
  172. {
  173. gearman_error(client.universal, GEARMAN_INVALID_ARGUMENT, "invalid workload");
  174. return NULL;
  175. }
  176. task_shell= gearman_task_internal_create(&client, task_shell);
  177. if (task_shell == NULL or task_shell->impl() == NULL)
  178. {
  179. assert(client.universal.error());
  180. return NULL;
  181. }
  182. assert(task_shell->impl()->client);
  183. Task* task= task_shell->impl();
  184. task->context= context;
  185. task->func= actions;
  186. if (gearman_unique_is_hash(unique))
  187. {
  188. task->unique_length= snprintf(task->unique, GEARMAN_MAX_UNIQUE_SIZE, "%u", libhashkit_murmur3(gearman_string_param(workload)));
  189. }
  190. else if ((task->unique_length= gearman_size(unique)))
  191. {
  192. if (task->unique_length >= GEARMAN_MAX_UNIQUE_SIZE)
  193. {
  194. task->unique_length= GEARMAN_MAX_UNIQUE_SIZE -1; // Leave space for NULL byte
  195. }
  196. /* We check to make sure there's space for the NULL above so this
  197. warning does not apply */
  198. #pragma GCC diagnostic push
  199. #ifdef __GNUC__
  200. #if __GNUC__ >= 8
  201. #pragma GCC diagnostic ignored "-Wstringop-truncation"
  202. #endif
  203. #endif
  204. strncpy(task->unique, gearman_c_str(unique), GEARMAN_MAX_UNIQUE_SIZE);
  205. #pragma GCC diagnostic pop
  206. task->unique[task->unique_length]= 0;
  207. }
  208. else
  209. {
  210. if (client.options.generate_unique or is_background(command))
  211. {
  212. if (safe_uuid_generate(task->unique, task->unique_length) == -1)
  213. {
  214. gearman_log_debug(task->client->universal, "uuid_generate_time_safe() failed or does not exist on this platform");
  215. }
  216. }
  217. else
  218. {
  219. task->unique_length= 0;
  220. task->unique[0]= 0;
  221. }
  222. }
  223. gearman_unique_t final_unique= gearman_unique_make(task->unique, task->unique_length);
  224. assert(task->client);
  225. gearman_return_t rc= GEARMAN_INVALID_ARGUMENT;
  226. switch (command)
  227. {
  228. case GEARMAN_COMMAND_SUBMIT_JOB:
  229. case GEARMAN_COMMAND_SUBMIT_JOB_LOW:
  230. case GEARMAN_COMMAND_SUBMIT_JOB_HIGH:
  231. rc= libgearman::protocol::submit(task->client->universal,
  232. task->send,
  233. final_unique,
  234. command,
  235. function,
  236. workload);
  237. break;
  238. case GEARMAN_COMMAND_SUBMIT_JOB_EPOCH:
  239. rc= libgearman::protocol::submit_epoch(task->client->universal,
  240. task->send,
  241. final_unique,
  242. function,
  243. workload,
  244. when);
  245. break;
  246. case GEARMAN_COMMAND_SUBMIT_JOB_BG:
  247. case GEARMAN_COMMAND_SUBMIT_JOB_LOW_BG:
  248. case GEARMAN_COMMAND_SUBMIT_JOB_HIGH_BG:
  249. rc= libgearman::protocol::submit_background(task->client->universal,
  250. task->send,
  251. final_unique,
  252. command,
  253. function,
  254. workload);
  255. break;
  256. case GEARMAN_COMMAND_SUBMIT_REDUCE_JOB:
  257. case GEARMAN_COMMAND_SUBMIT_REDUCE_JOB_BACKGROUND:
  258. rc= GEARMAN_INVALID_ARGUMENT;
  259. assert(rc != GEARMAN_INVALID_ARGUMENT);
  260. break;
  261. case GEARMAN_COMMAND_SUBMIT_JOB_SCHED:
  262. case GEARMAN_COMMAND_ALL_YOURS:
  263. case GEARMAN_COMMAND_CANT_DO:
  264. case GEARMAN_COMMAND_CAN_DO:
  265. case GEARMAN_COMMAND_CAN_DO_TIMEOUT:
  266. case GEARMAN_COMMAND_ECHO_REQ:
  267. case GEARMAN_COMMAND_ECHO_RES:
  268. case GEARMAN_COMMAND_ERROR:
  269. case GEARMAN_COMMAND_GET_STATUS:
  270. case GEARMAN_COMMAND_GRAB_JOB:
  271. case GEARMAN_COMMAND_GRAB_JOB_ALL:
  272. case GEARMAN_COMMAND_GRAB_JOB_UNIQ:
  273. case GEARMAN_COMMAND_JOB_ASSIGN:
  274. case GEARMAN_COMMAND_JOB_ASSIGN_ALL:
  275. case GEARMAN_COMMAND_JOB_ASSIGN_UNIQ:
  276. case GEARMAN_COMMAND_JOB_CREATED:
  277. case GEARMAN_COMMAND_MAX:
  278. case GEARMAN_COMMAND_NOOP:
  279. case GEARMAN_COMMAND_NO_JOB:
  280. case GEARMAN_COMMAND_OPTION_REQ:
  281. case GEARMAN_COMMAND_OPTION_RES:
  282. case GEARMAN_COMMAND_PRE_SLEEP:
  283. case GEARMAN_COMMAND_RESET_ABILITIES:
  284. case GEARMAN_COMMAND_SET_CLIENT_ID:
  285. case GEARMAN_COMMAND_STATUS_RES:
  286. case GEARMAN_COMMAND_TEXT:
  287. case GEARMAN_COMMAND_UNUSED:
  288. case GEARMAN_COMMAND_WORK_COMPLETE:
  289. case GEARMAN_COMMAND_WORK_DATA:
  290. case GEARMAN_COMMAND_WORK_EXCEPTION:
  291. case GEARMAN_COMMAND_WORK_FAIL:
  292. case GEARMAN_COMMAND_WORK_STATUS:
  293. case GEARMAN_COMMAND_WORK_WARNING:
  294. case GEARMAN_COMMAND_GET_STATUS_UNIQUE:
  295. case GEARMAN_COMMAND_STATUS_RES_UNIQUE:
  296. rc= GEARMAN_INVALID_ARGUMENT;
  297. assert(rc != GEARMAN_INVALID_ARGUMENT);
  298. break;
  299. }
  300. if (gearman_success(rc))
  301. {
  302. client.new_tasks++;
  303. client.running_tasks++;
  304. task->options.send_in_use= true;
  305. return task->shell();
  306. }
  307. gearman_task_free(task->shell());
  308. return NULL;
  309. }
  310. gearman_task_st *add_reducer_task(Client* client,
  311. gearman_command_t command,
  312. const gearman_job_priority_t,
  313. const gearman_string_t &function,
  314. const gearman_string_t &reducer,
  315. const gearman_unique_t &unique,
  316. const gearman_string_t &workload,
  317. const gearman_actions_t &actions,
  318. const time_t,
  319. void *context)
  320. {
  321. const void *args[5];
  322. size_t args_size[5];
  323. if (gearman_size(function) == 0 or gearman_c_str(function) == NULL or gearman_size(function) > GEARMAN_FUNCTION_MAX_SIZE)
  324. {
  325. if (gearman_size(function) > GEARMAN_FUNCTION_MAX_SIZE)
  326. {
  327. gearman_error(client->universal, GEARMAN_INVALID_ARGUMENT, "function name longer then GEARMAN_MAX_FUNCTION_SIZE");
  328. }
  329. else
  330. {
  331. gearman_error(client->universal, GEARMAN_INVALID_ARGUMENT, "invalid function");
  332. }
  333. return NULL;
  334. }
  335. if (gearman_size(unique) > GEARMAN_MAX_UNIQUE_SIZE)
  336. {
  337. gearman_error(client->universal, GEARMAN_INVALID_ARGUMENT, "unique name longer then GEARMAN_MAX_UNIQUE_SIZE");
  338. return NULL;
  339. }
  340. if ((gearman_size(workload) and not gearman_c_str(workload)) or (gearman_size(workload) == 0 && gearman_c_str(workload)))
  341. {
  342. gearman_error(client->universal, GEARMAN_INVALID_ARGUMENT, "invalid workload");
  343. return NULL;
  344. }
  345. gearman_task_st *task_shell= gearman_task_internal_create(client, NULL);
  346. if (task_shell == NULL)
  347. {
  348. assert(client->universal.error_code());
  349. return NULL;
  350. }
  351. Task* task= task_shell->impl();
  352. task->context= context;
  353. task->func= actions;
  354. /**
  355. @todo fix it so that NULL is done by default by the API not by happenstance.
  356. */
  357. char function_buffer[1024];
  358. if (client->universal._namespace)
  359. {
  360. char *ptr= function_buffer;
  361. memcpy(ptr, gearman_string_value(client->universal._namespace), gearman_string_length(client->universal._namespace));
  362. ptr+= gearman_string_length(client->universal._namespace);
  363. memcpy(ptr, gearman_c_str(function), gearman_size(function) +1);
  364. ptr+= gearman_size(function);
  365. args[0]= function_buffer;
  366. args_size[0]= ptr- function_buffer +1;
  367. }
  368. else
  369. {
  370. args[0]= gearman_c_str(function);
  371. args_size[0]= gearman_size(function) + 1;
  372. }
  373. if (gearman_unique_is_hash(unique))
  374. {
  375. task->unique_length= snprintf(task->unique, GEARMAN_MAX_UNIQUE_SIZE, "%u", libhashkit_murmur3(gearman_string_param(workload)));
  376. }
  377. else if ((task->unique_length= gearman_size(unique)))
  378. {
  379. if (task->unique_length >= GEARMAN_MAX_UNIQUE_SIZE)
  380. {
  381. task->unique_length= GEARMAN_MAX_UNIQUE_SIZE -1; // Leave space for NULL byte
  382. }
  383. /* We check to make sure there's space for the NULL above so this
  384. warning does not apply */
  385. #pragma GCC diagnostic push
  386. #ifdef __GNUC__
  387. #if __GNUC__ >= 8
  388. #pragma GCC diagnostic ignored "-Wstringop-truncation"
  389. #endif
  390. #endif
  391. strncpy(task->unique, gearman_c_str(unique), GEARMAN_MAX_UNIQUE_SIZE);
  392. #pragma GCC diagnostic pop
  393. task->unique[task->unique_length]= 0;
  394. }
  395. else
  396. {
  397. if (client->options.generate_unique or is_background(command))
  398. {
  399. safe_uuid_generate(task->unique, task->unique_length);
  400. }
  401. else
  402. {
  403. task->unique_length= 0;
  404. task->unique[0]= 0;
  405. }
  406. }
  407. args[1]= task->unique;
  408. args_size[1]= task->unique_length +1; // +1 is for the needed null
  409. assert_msg(command == GEARMAN_COMMAND_SUBMIT_REDUCE_JOB or command == GEARMAN_COMMAND_SUBMIT_REDUCE_JOB_BACKGROUND,
  410. "Command was not appropriate for request");
  411. char reducer_buffer[1024];
  412. if (client->universal._namespace)
  413. {
  414. char *ptr= reducer_buffer;
  415. memcpy(ptr, gearman_string_value(client->universal._namespace), gearman_string_length(client->universal._namespace));
  416. ptr+= gearman_string_length(client->universal._namespace);
  417. memcpy(ptr, gearman_c_str(reducer), gearman_size(reducer) +1);
  418. ptr+= gearman_size(reducer);
  419. args[2]= reducer_buffer;
  420. args_size[2]= ptr- reducer_buffer +1;
  421. }
  422. else
  423. {
  424. args[2]= gearman_c_str(reducer);
  425. args_size[2]= gearman_size(reducer) +1;
  426. }
  427. char aggregate[1];
  428. aggregate[0]= 0;
  429. args[3]= aggregate;
  430. args_size[3]= 1;
  431. assert_msg(gearman_c_str(workload), "Invalid workload (NULL)");
  432. assert_msg(gearman_size(workload), "Invalid workload of zero");
  433. args[4]= gearman_c_str(workload);
  434. args_size[4]= gearman_size(workload);
  435. gearman_return_t rc;
  436. if (gearman_success(rc= gearman_packet_create_args(client->universal, task->send,
  437. GEARMAN_MAGIC_REQUEST, command,
  438. args, args_size,
  439. 5)))
  440. {
  441. client->new_tasks++;
  442. client->running_tasks++;
  443. task->options.send_in_use= true;
  444. }
  445. else
  446. {
  447. gearman_gerror(client->universal, rc);
  448. gearman_task_free(task);
  449. task= NULL;
  450. }
  451. task->type= GEARMAN_TASK_KIND_EXECUTE;
  452. return task->shell();
  453. }