instance.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 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. #include <gear_config.h>
  38. #include <libgearman-server/common.h>
  39. #include "libgearman-server/plugins/base.h"
  40. #include "libgearman-server/plugins/queue/sqlite/instance.hpp"
  41. namespace gearmand {
  42. namespace queue {
  43. Instance::Instance(const std::string& schema_, const std::string& table_):
  44. _epoch_support(true),
  45. _in_trans(0),
  46. _db(NULL),
  47. _schema(schema_),
  48. _table(table_)
  49. {
  50. _delete_query+= "DELETE FROM ";
  51. _delete_query+= _table;
  52. _delete_query+= " WHERE unique_key=? and function_name=?";
  53. if (_epoch_support)
  54. {
  55. _insert_query+= "INSERT OR REPLACE INTO ";
  56. _insert_query+= _table;
  57. _insert_query+= " (priority, unique_key, function_name, data, when_to_run) VALUES (?,?,?,?,?)";
  58. }
  59. else
  60. {
  61. _insert_query+= "INSERT OR REPLACE INTO ";
  62. _insert_query+= _table;
  63. _insert_query+= " (priority, unique_key, function_name, data) VALUES (?,?,?,?,?)";
  64. }
  65. }
  66. Instance::~Instance()
  67. {
  68. if (_db)
  69. {
  70. sqlite3_close(_db);
  71. _db= NULL;
  72. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM, "sqlite shutdown database");
  73. }
  74. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM, "sqlite shutdown");
  75. }
  76. int Instance::_sqlite_query(const char *query, size_t query_size,
  77. sqlite3_stmt ** sth)
  78. {
  79. if (query_size > UINT32_MAX)
  80. {
  81. gearmand_log_error(GEARMAN_DEFAULT_LOG_PARAM, "query size too big [%u]", (uint32_t)query_size);
  82. return SQLITE_ERROR;
  83. }
  84. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM, "sqlite query: %s", query);
  85. int ret= sqlite3_prepare(_db, query, (int)query_size, sth, NULL);
  86. if (ret != SQLITE_OK)
  87. {
  88. if (*sth)
  89. {
  90. sqlite3_finalize(*sth);
  91. }
  92. *sth= NULL;
  93. gearmand_log_error(AT, "sqlite_prepare:%s", sqlite3_errmsg(_db));
  94. }
  95. return ret;
  96. }
  97. int Instance::_sqlite_lock()
  98. {
  99. if (_in_trans)
  100. {
  101. /* already in transaction */
  102. return SQLITE_OK;
  103. }
  104. char* error= NULL;
  105. sqlite3_exec(_db, "BEGIN TRANSACTION", NULL, NULL, &error);
  106. if (error != NULL)
  107. {
  108. gearmand_log_error(GEARMAN_DEFAULT_LOG_PARAM,
  109. "failed to begin transaction: %s",
  110. error);
  111. return sqlite3_errcode(_db);
  112. }
  113. _in_trans++;
  114. return SQLITE_OK;
  115. }
  116. gearmand_error_t Instance::_sqlite_dispatch(const std::string& arg, bool send_error)
  117. {
  118. char* error= NULL;
  119. sqlite3_exec(_db, arg.c_str(), NULL, NULL, &error);
  120. if (error != NULL)
  121. {
  122. if (send_error)
  123. {
  124. return gearmand_log_error(GEARMAN_DEFAULT_LOG_PARAM,
  125. "%s", error);
  126. }
  127. return GEARMAN_UNKNOWN_OPTION;
  128. }
  129. return GEARMAN_SUCCESS;
  130. }
  131. int Instance::_sqlite_commit()
  132. {
  133. if (_in_trans == 0)
  134. {
  135. /* not in transaction */
  136. return SQLITE_OK;
  137. }
  138. char* error= NULL;
  139. sqlite3_exec(_db, "COMMIT", NULL, NULL, &error);
  140. if (error != NULL)
  141. {
  142. gearmand_log_error(GEARMAN_DEFAULT_LOG_PARAM,
  143. "failed to commit transaction: %s", error);
  144. return sqlite3_errcode(_db);
  145. }
  146. _in_trans= 0;
  147. return SQLITE_OK;
  148. }
  149. gearmand_error_t Instance::init()
  150. {
  151. gearmand_info("Initializing libsqlite3 module");
  152. if (_schema.empty())
  153. {
  154. return gearmand_gerror("missing required --libsqlite3-db=<dbfile> argument", GEARMAN_QUEUE_ERROR);
  155. }
  156. assert(_db == NULL);
  157. if (sqlite3_open(_schema.c_str(), &_db) != SQLITE_OK)
  158. {
  159. _error_string= "sqlite3_open failed with: ";
  160. _error_string+= sqlite3_errmsg(_db);
  161. return gearmand_gerror(_error_string.c_str(), GEARMAN_QUEUE_ERROR);
  162. }
  163. if (_db == NULL)
  164. {
  165. _error_string= "Unknown error while opening up sqlite file";
  166. return gearmand_gerror(_error_string.c_str(), GEARMAN_QUEUE_ERROR);
  167. }
  168. sqlite3_stmt* sth;
  169. if (_sqlite_query(gearman_literal_param("SELECT name FROM sqlite_master WHERE type='table'"), &sth) != SQLITE_OK)
  170. {
  171. _error_string= "Unknown error while calling SELECT on sqlite file ";
  172. _error_string+= _schema;
  173. _error_string+= " :";
  174. _error_string+= sqlite3_errmsg(_db);
  175. return gearmand_gerror(_error_string.c_str(), GEARMAN_QUEUE_ERROR);
  176. }
  177. bool found= false;
  178. while (sqlite3_step(sth) == SQLITE_ROW)
  179. {
  180. char *table= NULL;
  181. if (sqlite3_column_type(sth, 0) == SQLITE_TEXT)
  182. {
  183. table= (char*)sqlite3_column_text(sth, 0);
  184. }
  185. else
  186. {
  187. std::string error_string("Column `name` from sqlite_master is not type TEXT");
  188. sqlite3_finalize(sth);
  189. return gearmand_gerror(error_string.c_str(), GEARMAN_QUEUE_ERROR);
  190. }
  191. if (_table.compare(table) == 0)
  192. {
  193. gearmand_log_info(GEARMAN_DEFAULT_LOG_PARAM, "sqlite module using table '%s'", table);
  194. found= true;
  195. break;
  196. }
  197. }
  198. if (sqlite3_finalize(sth) != SQLITE_OK)
  199. {
  200. return gearmand_gerror(sqlite3_errmsg(_db), GEARMAN_QUEUE_ERROR);
  201. }
  202. if (found == false)
  203. {
  204. std::string query("CREATE TABLE ");
  205. query+= _table;
  206. query+= " ( unique_key TEXT, function_name TEXT, priority INTEGER, data BLOB, when_to_run INTEGER, PRIMARY KEY (unique_key, function_name))";
  207. gearmand_log_info(GEARMAN_DEFAULT_LOG_PARAM, "sqlite module creating table '%s'", _table.c_str());
  208. gearmand_error_t ret= _sqlite_dispatch(query);
  209. if (ret != GEARMAN_SUCCESS)
  210. {
  211. return ret;
  212. }
  213. }
  214. else
  215. {
  216. std::string query("SELECT when_to_run FROM ");
  217. query+= _table;
  218. gearmand_error_t ret= _sqlite_dispatch(query);
  219. if (ret != GEARMAN_SUCCESS)
  220. {
  221. gearmand_info("No epoch support in sqlite queue");
  222. _epoch_support= false;
  223. }
  224. }
  225. return GEARMAN_SUCCESS;
  226. }
  227. int Instance::_sqlite_rollback()
  228. {
  229. if (_in_trans == 0)
  230. {
  231. /* not in transaction */
  232. return SQLITE_OK;
  233. }
  234. char* error= NULL;
  235. sqlite3_exec(_db, "ROLLBACK", NULL, NULL, &error);
  236. if (error != NULL)
  237. {
  238. gearmand_log_error(GEARMAN_DEFAULT_LOG_PARAM,
  239. "failed to commit transaction: %s", error);
  240. return sqlite3_errcode(_db);
  241. }
  242. _in_trans= 0;
  243. return SQLITE_OK;
  244. }
  245. gearmand_error_t Instance::add(gearman_server_st *server,
  246. const char *unique, size_t unique_size,
  247. const char *function_name,
  248. size_t function_name_size,
  249. const void *data, size_t data_size,
  250. gearman_job_priority_t priority,
  251. int64_t when)
  252. {
  253. (void)server;
  254. sqlite3_stmt* sth;
  255. if (when and _epoch_support == false)
  256. {
  257. return gearmand_gerror("Table lacks when_to_run field", GEARMAN_QUEUE_ERROR);
  258. }
  259. if (unique_size > UINT32_MAX || function_name_size > UINT32_MAX ||
  260. data_size > UINT32_MAX)
  261. {
  262. gearmand_log_error(GEARMAN_DEFAULT_LOG_PARAM, "size too big [%u]", (uint32_t)unique_size);
  263. return GEARMAN_QUEUE_ERROR;
  264. }
  265. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM, "sqlite add: %.*s at %ld", (uint32_t)unique_size, (char *)unique, (long int)when);
  266. if (_sqlite_lock() != SQLITE_OK)
  267. {
  268. return GEARMAN_QUEUE_ERROR;
  269. }
  270. if (_sqlite_query(insert_query().c_str(), insert_query().size(), &sth) != SQLITE_OK)
  271. {
  272. return GEARMAN_QUEUE_ERROR;
  273. }
  274. if (sqlite3_bind_int(sth, 1, priority) != SQLITE_OK)
  275. {
  276. _sqlite_rollback();
  277. gearmand_log_error(GEARMAN_DEFAULT_LOG_PARAM, "failed to bind int [%d]: %s", priority, sqlite3_errmsg(_db));
  278. sqlite3_finalize(sth);
  279. return GEARMAN_QUEUE_ERROR;
  280. }
  281. if (sqlite3_bind_text(sth, 2, (const char *)unique, (int)unique_size,
  282. SQLITE_TRANSIENT) != SQLITE_OK)
  283. {
  284. _sqlite_rollback();
  285. gearmand_log_error(GEARMAN_DEFAULT_LOG_PARAM, "failed to bind text [%.*s]: %s", (uint32_t)unique_size, (char*)unique, sqlite3_errmsg(_db));
  286. sqlite3_finalize(sth);
  287. return GEARMAN_QUEUE_ERROR;
  288. }
  289. if (sqlite3_bind_text(sth, 3, (const char *)function_name, (int)function_name_size,
  290. SQLITE_TRANSIENT) != SQLITE_OK)
  291. {
  292. _sqlite_rollback();
  293. gearmand_log_error(GEARMAN_DEFAULT_LOG_PARAM, "failed to bind text [%.*s]: %s", (uint32_t)function_name_size, (char*)function_name, sqlite3_errmsg(_db));
  294. sqlite3_finalize(sth);
  295. return GEARMAN_QUEUE_ERROR;
  296. }
  297. if (sqlite3_bind_blob(sth, 4, data, (int)data_size,
  298. SQLITE_TRANSIENT) != SQLITE_OK)
  299. {
  300. _sqlite_rollback();
  301. gearmand_log_error(GEARMAN_DEFAULT_LOG_PARAM, "failed to bind blob: %s", sqlite3_errmsg(_db));
  302. sqlite3_finalize(sth);
  303. return GEARMAN_QUEUE_ERROR;
  304. }
  305. // epoch data
  306. if (sqlite3_bind_int64(sth, 5, when) != SQLITE_OK)
  307. {
  308. _sqlite_rollback();
  309. gearmand_log_error(GEARMAN_DEFAULT_LOG_PARAM, "failed to bind int64_t(%ld): %s", (long int)when, sqlite3_errmsg(_db));
  310. sqlite3_finalize(sth);
  311. return GEARMAN_QUEUE_ERROR;
  312. }
  313. if (sqlite3_step(sth) != SQLITE_DONE)
  314. {
  315. gearmand_log_error(GEARMAN_DEFAULT_LOG_PARAM, "insert error: %s", sqlite3_errmsg(_db));
  316. if (sqlite3_finalize(sth) != SQLITE_OK )
  317. {
  318. gearmand_log_error(GEARMAN_DEFAULT_LOG_PARAM, "finalize error: %s", sqlite3_errmsg(_db));
  319. }
  320. return GEARMAN_QUEUE_ERROR;
  321. }
  322. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM,
  323. "sqlite data: priority: %d, unique_key: %s, function_name: %s",
  324. priority, (char*)unique, (char*)function_name);
  325. sqlite3_finalize(sth);
  326. if (_sqlite_commit() != SQLITE_OK)
  327. {
  328. return GEARMAN_QUEUE_ERROR;
  329. }
  330. return GEARMAN_SUCCESS;
  331. }
  332. gearmand_error_t Instance::flush(gearman_server_st *server)
  333. {
  334. (void)server;
  335. gearmand_debug("sqlite flush");
  336. return GEARMAN_SUCCESS;
  337. }
  338. gearmand_error_t Instance::done(gearman_server_st *server,
  339. const char *unique,
  340. size_t unique_size,
  341. const char *function_name,
  342. size_t function_name_size)
  343. {
  344. (void)server;
  345. sqlite3_stmt* sth;
  346. if (unique_size > UINT32_MAX)
  347. {
  348. gearmand_log_error(GEARMAN_DEFAULT_LOG_PARAM,
  349. "unique key size too big [%u]", (uint32_t)unique_size);
  350. return GEARMAN_QUEUE_ERROR;
  351. }
  352. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM, "sqlite done: %.*s", (uint32_t)unique_size, (char *)unique);
  353. if (_sqlite_lock() != SQLITE_OK)
  354. {
  355. return GEARMAN_QUEUE_ERROR;
  356. }
  357. if (_sqlite_query(delete_query().c_str(), delete_query().size(), &sth) != SQLITE_OK)
  358. {
  359. return GEARMAN_QUEUE_ERROR;
  360. }
  361. sqlite3_bind_text(sth, 1, (const char *)unique, (int)unique_size, SQLITE_TRANSIENT);
  362. sqlite3_bind_text(sth, 2, (const char *)function_name, (int)function_name_size, SQLITE_TRANSIENT);
  363. if (sqlite3_step(sth) != SQLITE_DONE)
  364. {
  365. gearmand_log_error(GEARMAN_DEFAULT_LOG_PARAM,
  366. "delete error: %s",
  367. sqlite3_errmsg(_db));
  368. sqlite3_finalize(sth);
  369. return GEARMAN_QUEUE_ERROR;
  370. }
  371. sqlite3_finalize(sth);
  372. if (_sqlite_commit() != SQLITE_OK)
  373. {
  374. return GEARMAN_QUEUE_ERROR;
  375. }
  376. return GEARMAN_SUCCESS;
  377. }
  378. gearmand_error_t Instance::replay(gearman_server_st *server)
  379. {
  380. gearmand_info("sqlite replay start");
  381. std::string query;
  382. if (_epoch_support)
  383. {
  384. query+= "SELECT unique_key,function_name,priority,data,when_to_run FROM ";
  385. }
  386. else
  387. {
  388. query+= "SELECT unique_key,function_name,priority,data FROM ";
  389. }
  390. query+= _table;
  391. sqlite3_stmt* sth;
  392. if (_sqlite_query(query.c_str(), query.size(), &sth) != SQLITE_OK)
  393. {
  394. return GEARMAN_QUEUE_ERROR;
  395. }
  396. while (sqlite3_step(sth) == SQLITE_ROW)
  397. {
  398. const char *unique, *function_name;
  399. size_t unique_size, function_name_size;
  400. if (sqlite3_column_type(sth,0) == SQLITE_TEXT)
  401. {
  402. unique= (char *)sqlite3_column_text(sth,0);
  403. unique_size= (size_t) sqlite3_column_bytes(sth,0);
  404. }
  405. else
  406. {
  407. sqlite3_finalize(sth);
  408. gearmand_log_error(GEARMAN_DEFAULT_LOG_PARAM,
  409. "column %d is not type TEXT", 0);
  410. return GEARMAN_QUEUE_ERROR;
  411. }
  412. if (sqlite3_column_type(sth,1) == SQLITE_TEXT)
  413. {
  414. function_name= (char *)sqlite3_column_text(sth,1);
  415. function_name_size= (size_t)sqlite3_column_bytes(sth,1);
  416. }
  417. else
  418. {
  419. sqlite3_finalize(sth);
  420. gearmand_log_error(GEARMAN_DEFAULT_LOG_PARAM,
  421. "column %d is not type TEXT", 1);
  422. return GEARMAN_QUEUE_ERROR;
  423. }
  424. gearman_job_priority_t priority;
  425. if (sqlite3_column_type(sth,2) == SQLITE_INTEGER)
  426. {
  427. priority= (gearman_job_priority_t)sqlite3_column_int64(sth,2);
  428. }
  429. else
  430. {
  431. sqlite3_finalize(sth);
  432. gearmand_log_error(GEARMAN_DEFAULT_LOG_PARAM,
  433. "column %d is not type INTEGER", 2);
  434. return GEARMAN_QUEUE_ERROR;
  435. }
  436. if (sqlite3_column_type(sth,3) != SQLITE_BLOB)
  437. {
  438. sqlite3_finalize(sth);
  439. gearmand_log_error(GEARMAN_DEFAULT_LOG_PARAM, "column %d is not type TEXT", 3);
  440. return GEARMAN_QUEUE_ERROR;
  441. }
  442. size_t data_size= (size_t)sqlite3_column_bytes(sth,3);
  443. char* data= (char*)malloc(data_size);
  444. /* need to make a copy here ... gearman_server_job_free will free it later */
  445. if (data == NULL)
  446. {
  447. sqlite3_finalize(sth);
  448. gearmand_perror("malloc");
  449. return GEARMAN_MEMORY_ALLOCATION_FAILURE;
  450. }
  451. memcpy(data, sqlite3_column_blob(sth,3), data_size);
  452. int64_t when;
  453. if (_epoch_support)
  454. {
  455. if (sqlite3_column_type(sth, 4) == SQLITE_INTEGER)
  456. {
  457. when= (int64_t)sqlite3_column_int64(sth, 4);
  458. }
  459. else
  460. {
  461. sqlite3_finalize(sth);
  462. gearmand_log_error(GEARMAN_DEFAULT_LOG_PARAM, "column %d is not type INTEGER", 3);
  463. return GEARMAN_QUEUE_ERROR;
  464. }
  465. }
  466. else
  467. {
  468. when= 0;
  469. }
  470. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM, "sqlite replay: %s", (char*)function_name);
  471. gearmand_error_t gret= Instance::replay_add(server,
  472. NULL,
  473. unique, unique_size,
  474. function_name, function_name_size,
  475. data, data_size,
  476. priority, when);
  477. if (gearmand_failed(gret))
  478. {
  479. sqlite3_finalize(sth);
  480. return gret;
  481. }
  482. }
  483. sqlite3_finalize(sth);
  484. return GEARMAN_SUCCESS;
  485. }
  486. } // namespace queue
  487. } // namespace gearmand