dbcl_api.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /*
  2. * $Id$
  3. *
  4. * DB CLuster core functions
  5. *
  6. * Copyright (C) 2012 Daniel-Constantin Mierla (asipto.com)
  7. *
  8. * This file is part of Kamailio, a free SIP server.
  9. *
  10. * Kamailio is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * Kamailio is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. /*! \file
  25. * \brief DB_CLUSTER :: Core
  26. * \ingroup db_cluster
  27. * Module: \ref db_cluster
  28. */
  29. #include "../../dprint.h"
  30. #include "../../hashes.h"
  31. #include "../../trim.h"
  32. #include "../../globals.h"
  33. #include "../../lib/srdb1/db.h"
  34. #include "dbcl_data.h"
  35. #include "dbcl_api.h"
  36. #define DBCL_READ(command) \
  37. do {\
  38. int ret;\
  39. int i;\
  40. int j;\
  41. int k;\
  42. db1_con_t *dbh=NULL;\
  43. dbcl_cls_t *cls=NULL;\
  44. cls = (dbcl_cls_t*)_h->tail;\
  45. ret = -1;\
  46. for(i=DBCL_PRIO_SIZE-1; i>0; i--)\
  47. {\
  48. if(cls->rlist[i].clen<=0) continue; \
  49. switch(cls->rlist[i].mode) {\
  50. case 's':\
  51. case 'S':\
  52. for(j=0; j<cls->rlist[i].clen; j++)\
  53. {\
  54. if(dbcl_valid_con(cls->rlist[i].clist[j])==0)\
  55. {\
  56. LM_DBG("serial operation - cluster [%.*s] (%d/%d)\n",\
  57. cls->name.len, cls->name.s, i, j);\
  58. dbh = cls->rlist[i].clist[j]->dbh;\
  59. ret = cls->rlist[i].clist[j]->dbf.command;\
  60. if (ret==0) {\
  61. cls->usedcon = cls->rlist[i].clist[j];\
  62. return 0;\
  63. } else {\
  64. LM_DBG("serial operation - failre on cluster"\
  65. " [%.*s] (%d/%d)\n",\
  66. cls->name.len, cls->name.s, i, j);\
  67. dbcl_inactive_con(cls->rlist[i].clist[j]);\
  68. }\
  69. }\
  70. }\
  71. break;\
  72. case 'r':\
  73. case 'R':\
  74. for(k=0; k<cls->rlist[i].clen; k++)\
  75. {\
  76. j = (process_no + k + cls->rlist[i].crt) % cls->rlist[i].clen;\
  77. if(dbcl_valid_con(cls->rlist[i].clist[j])==0)\
  78. {\
  79. LM_DBG("round robin operation - cluster [%.*s] (%d/%d)\n",\
  80. cls->name.len, cls->name.s, i, j);\
  81. dbh = cls->rlist[i].clist[j]->dbh;\
  82. ret = cls->rlist[i].clist[j]->dbf.command;\
  83. if (ret==0)\
  84. {\
  85. cls->usedcon = cls->rlist[i].clist[j];\
  86. cls->rlist[i].crt = (j+1) % cls->rlist[i].clen;\
  87. return 0;\
  88. } else {\
  89. LM_DBG("round robin operation - failre on cluster"\
  90. " [%.*s] (%d/%d)\n",\
  91. cls->name.len, cls->name.s, i, j);\
  92. dbcl_inactive_con(cls->rlist[i].clist[j]);\
  93. }\
  94. }\
  95. }\
  96. break;\
  97. default:\
  98. LM_ERR("invalid mode %c (%d)\n", cls->rlist[i].mode,\
  99. cls->rlist[i].mode);\
  100. return -1;\
  101. }\
  102. }\
  103. LM_DBG("no successful read on cluster [%.*s]\n",\
  104. cls->name.len, cls->name.s);\
  105. return ret;\
  106. } while(0)
  107. #define DBCL_WRITE(command) \
  108. do {\
  109. int ret;\
  110. int rc;\
  111. int rok;\
  112. int i;\
  113. int j;\
  114. int k;\
  115. db1_con_t *dbh=NULL;\
  116. dbcl_cls_t *cls=NULL;\
  117. cls = (dbcl_cls_t*)_h->tail;\
  118. ret = -1;\
  119. rok = 0;\
  120. rc = 0;\
  121. for(i=DBCL_PRIO_SIZE-1; i>0; i--)\
  122. {\
  123. if(cls->wlist[i].clen<=0) continue; \
  124. switch(cls->wlist[i].mode) {\
  125. case 's':\
  126. case 'S':\
  127. for(j=0; j<cls->wlist[i].clen; j++)\
  128. {\
  129. if(dbcl_valid_con(cls->wlist[i].clist[j])==0)\
  130. {\
  131. LM_DBG("serial operation - cluster [%.*s] (%d/%d)\n",\
  132. cls->name.len, cls->name.s, i, j);\
  133. dbh = cls->wlist[i].clist[j]->dbh;\
  134. ret = cls->wlist[i].clist[j]->dbf.command;\
  135. if (ret==0) {\
  136. cls->usedcon = cls->wlist[i].clist[j];\
  137. return 0;\
  138. } else {\
  139. LM_DBG("serial operation - failure on cluster"\
  140. " [%.*s] (%d/%d)\n",\
  141. cls->name.len, cls->name.s, i, j);\
  142. dbcl_inactive_con(cls->wlist[i].clist[j]);\
  143. }\
  144. }\
  145. }\
  146. break;\
  147. case 'r':\
  148. case 'R':\
  149. for(k=0; k<cls->wlist[i].clen; k++)\
  150. {\
  151. j = (process_no + k + cls->wlist[i].crt) % cls->wlist[i].clen;\
  152. if(dbcl_valid_con(cls->wlist[i].clist[j])==0)\
  153. {\
  154. LM_DBG("round robin operation - cluster [%.*s] (%d/%d)\n",\
  155. cls->name.len, cls->name.s, i, j);\
  156. dbh = cls->wlist[i].clist[j]->dbh;\
  157. ret = cls->wlist[i].clist[j]->dbf.command;\
  158. if (ret==0)\
  159. {\
  160. cls->usedcon = cls->wlist[i].clist[j];\
  161. cls->wlist[i].crt = (j+1) % cls->wlist[i].clen;\
  162. return 0;\
  163. } else {\
  164. LM_DBG("round robin operation - failure on cluster"\
  165. " [%.*s] (%d/%d)\n",\
  166. cls->name.len, cls->name.s, i, j);\
  167. dbcl_inactive_con(cls->wlist[i].clist[j]);\
  168. }\
  169. }\
  170. }\
  171. break;\
  172. case 'p':\
  173. case 'P':\
  174. for(j=0; j<cls->wlist[i].clen; j++)\
  175. {\
  176. if(dbcl_valid_con(cls->wlist[i].clist[j])==0)\
  177. {\
  178. LM_DBG("parallel operation - cluster [%.*s] (%d/%d)\n",\
  179. cls->name.len, cls->name.s, i, j);\
  180. dbh = cls->wlist[i].clist[j]->dbh;\
  181. rc = cls->wlist[i].clist[j]->dbf.command;\
  182. if(rc==0) {\
  183. cls->usedcon = cls->wlist[i].clist[j];\
  184. rok = 1;\
  185. } else {\
  186. LM_DBG("parallel operation - failure on cluster"\
  187. " [%.*s] (%d/%d)\n",\
  188. cls->name.len, cls->name.s, i, j);\
  189. dbcl_inactive_con(cls->wlist[i].clist[j]);\
  190. }\
  191. ret |= rc;\
  192. }\
  193. }\
  194. if (rok==1 && cls->wlist[i].clen>0)\
  195. return 0;\
  196. break;\
  197. default:\
  198. LM_ERR("invalid mode %c (%d)\n", cls->rlist[i].mode,\
  199. cls->rlist[i].mode);\
  200. return -1;\
  201. }\
  202. }\
  203. LM_DBG("no successful write on cluster [%.*s]\n",\
  204. cls->name.len, cls->name.s);\
  205. return ret;\
  206. } while(0)
  207. /*! \brief
  208. * Initialize database connection
  209. */
  210. db1_con_t* db_cluster_init(const str* _dburl)
  211. {
  212. db1_con_t *h=NULL;
  213. dbcl_cls_t *cls=NULL;
  214. str name;
  215. LM_DBG("initializing with cluster [%.*s]\n", _dburl->len, _dburl->s);
  216. if(_dburl->len<10 || strncmp(_dburl->s, "cluster://", 10)!=0)
  217. {
  218. LM_ERR("invlaid url for cluster module [%.*s]\n",
  219. _dburl->len, _dburl->s);
  220. return NULL;
  221. }
  222. name.s = _dburl->s + 10;
  223. name.len = _dburl->len - 10;
  224. trim(&name);
  225. cls = dbcl_get_cluster(&name);
  226. if(cls==NULL)
  227. {
  228. LM_ERR("cluster not found [%.*s]\n",
  229. _dburl->len, _dburl->s);
  230. return NULL;
  231. }
  232. if(dbcl_init_dbf(cls)<0)
  233. {
  234. LM_ERR("cluster [%.*s] - unable to bind to DB engines\n",
  235. _dburl->len, _dburl->s);
  236. return NULL;
  237. }
  238. dbcl_init_connections(cls);
  239. cls->ref++;
  240. h = (db1_con_t*)pkg_malloc(sizeof(db1_con_t));
  241. if (h==NULL) {
  242. LM_ERR("out of pkg\n");
  243. return NULL;
  244. }
  245. memset(h, 0, sizeof(db1_con_t));
  246. h->tail = (unsigned long)cls;
  247. return h;
  248. }
  249. /*! \brief
  250. * Close a database connection
  251. */
  252. void db_cluster_close(db1_con_t* _h)
  253. {
  254. dbcl_cls_t *cls=NULL;
  255. LM_DBG("executing db cluster close command\n");
  256. cls = (dbcl_cls_t*)_h->tail;
  257. cls->ref--;
  258. if(cls->ref > 0)
  259. return;
  260. /* close connections */
  261. dbcl_close_connections(cls);
  262. return;
  263. }
  264. /*! \brief
  265. * Free all memory allocated by get_result
  266. */
  267. int db_cluster_free_result(db1_con_t* _h, db1_res_t* _r)
  268. {
  269. dbcl_cls_t *cls=NULL;
  270. LM_DBG("executing db cluster free-result command\n");
  271. cls = (dbcl_cls_t*)_h->tail;
  272. if(cls->usedcon==NULL || cls->usedcon->dbh==NULL)
  273. return -1;
  274. return cls->usedcon->dbf.free_result(cls->usedcon->dbh, _r);
  275. }
  276. /*! \brief
  277. * Do a query
  278. */
  279. int db_cluster_query(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _op,
  280. const db_val_t* _v, const db_key_t* _c, const int _n, const int _nc,
  281. const db_key_t _o, db1_res_t** _r)
  282. {
  283. LM_DBG("executing db cluster query command\n");
  284. DBCL_READ(query(dbh, _k, _op, _v, _c, _n, _nc, _o, _r));
  285. }
  286. /*! \brief
  287. * fetch rows from a result
  288. */
  289. int db_cluster_fetch_result(const db1_con_t* _h, db1_res_t** _r, const int nrows)
  290. {
  291. dbcl_cls_t *cls=NULL;
  292. LM_DBG("executing db cluster fetch-result command\n");
  293. cls = (dbcl_cls_t*)_h->tail;
  294. if(cls->usedcon==NULL || cls->usedcon->dbh==NULL
  295. || cls->usedcon->dbf.fetch_result==NULL)
  296. return -1;
  297. return cls->usedcon->dbf.fetch_result(cls->usedcon->dbh, _r, nrows);
  298. }
  299. /*! \brief
  300. * Raw SQL query
  301. */
  302. int db_cluster_raw_query(const db1_con_t* _h, const str* _s, db1_res_t** _r)
  303. {
  304. LM_DBG("executing db cluster raw query command\n");
  305. DBCL_READ(raw_query(dbh, _s, _r));
  306. }
  307. /*! \brief
  308. * Insert a row into table
  309. */
  310. int db_cluster_insert(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v, const int _n)
  311. {
  312. LM_DBG("executing db cluster insert command\n");
  313. DBCL_WRITE(insert(dbh, _k, _v, _n));
  314. }
  315. /*! \brief
  316. * Delete a row from table
  317. */
  318. int db_cluster_delete(const db1_con_t* _h, const db_key_t* _k, const
  319. db_op_t* _o, const db_val_t* _v, const int _n)
  320. {
  321. LM_DBG("executing db cluster delete command\n");
  322. DBCL_WRITE(delete(dbh, _k, _o, _v, _n));
  323. }
  324. /*! \brief
  325. * Update a row in table
  326. */
  327. int db_cluster_update(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _o,
  328. const db_val_t* _v, const db_key_t* _uk, const db_val_t* _uv, const int _n,
  329. const int _un)
  330. {
  331. LM_DBG("executing db cluster update command\n");
  332. DBCL_WRITE(update(dbh, _k, _o, _v, _uk, _uv, _n, _un));
  333. }
  334. /*! \brief
  335. * Just like insert, but replace the row if it exists
  336. */
  337. int db_cluster_replace(const db1_con_t* _h, const db_key_t* _k,
  338. const db_val_t* _v, const int _n, const int _un, const int _m)
  339. {
  340. LM_DBG("executing db cluster replace command\n");
  341. DBCL_WRITE(replace(dbh, _k, _v, _n, _un, _m));
  342. }
  343. /*! \brief
  344. * Returns the last inserted ID
  345. */
  346. int db_cluster_last_inserted_id(const db1_con_t* _h)
  347. {
  348. dbcl_cls_t *cls=NULL;
  349. LM_DBG("executing db cluster last inserted id command\n");
  350. cls = (dbcl_cls_t*)_h->tail;
  351. if(cls->usedcon==NULL || cls->usedcon->dbh==NULL
  352. || cls->usedcon->dbf.last_inserted_id==NULL)
  353. return -1;
  354. return cls->usedcon->dbf.last_inserted_id(cls->usedcon->dbh);
  355. }
  356. /*! \brief
  357. * Returns number of affected rows for last query
  358. */
  359. int db_cluster_affected_rows(const db1_con_t* _h)
  360. {
  361. dbcl_cls_t *cls=NULL;
  362. LM_DBG("executing db cluster affected-rows command\n");
  363. cls = (dbcl_cls_t*)_h->tail;
  364. if(cls->usedcon==NULL || cls->usedcon->dbh==NULL
  365. || cls->usedcon->dbf.affected_rows==NULL)
  366. return -1;
  367. return cls->usedcon->dbf.affected_rows(cls->usedcon->dbh);
  368. }
  369. /*! \brief
  370. * Insert a row into table, update on duplicate key
  371. */
  372. int db_cluster_insert_update(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v,
  373. const int _n)
  374. {
  375. LM_DBG("executing db cluster insert-update command\n");
  376. DBCL_WRITE(insert_update(dbh, _k, _v, _n));
  377. }
  378. /*! \brief
  379. * Insert a row into table
  380. */
  381. int db_cluster_insert_delayed(const db1_con_t* _h, const db_key_t* _k,
  382. const db_val_t* _v, const int _n)
  383. {
  384. LM_DBG("executing db cluster insert delayed command\n");
  385. DBCL_WRITE(insert_delayed(dbh, _k, _v, _n));
  386. }
  387. /*! \brief
  388. * Store name of table that will be used by
  389. * subsequent database functions
  390. */
  391. int db_cluster_use_table(db1_con_t* _h, const str* _t)
  392. {
  393. int i;
  394. int j;
  395. int ret;
  396. dbcl_cls_t *cls=NULL;
  397. cls = (dbcl_cls_t*)_h->tail;
  398. ret = 0;
  399. LM_DBG("use table (%.*s) - cluster [%.*s]\n",
  400. _t->len, _t->s, cls->name.len, cls->name.s);
  401. for(i=DBCL_PRIO_SIZE-1; i>0; i--)
  402. {
  403. for(j=0; j<cls->rlist[i].clen; j++)
  404. {
  405. if(cls->rlist[i].clist[j] != NULL && cls->rlist[i].clist[j]->flags!=0
  406. && cls->rlist[i].clist[j]->dbh != NULL)
  407. {
  408. LM_DBG("set read table (%.*s) - cluster [%.*s] (%d/%d)\n",
  409. _t->len, _t->s, cls->name.len, cls->name.s, i, j);
  410. ret |= cls->rlist[i].clist[j]->dbf.use_table(cls->rlist[i].clist[j]->dbh, _t);
  411. }
  412. }
  413. for(j=0; j<cls->wlist[i].clen; j++)
  414. {
  415. if(cls->wlist[i].clist[j] != NULL && cls->wlist[i].clist[j]->flags!=0
  416. && cls->wlist[i].clist[j]->dbh != NULL)
  417. {
  418. LM_DBG("set write table (%.*s) - cluster [%.*s] (%d/%d)\n",
  419. _t->len, _t->s, cls->name.len, cls->name.s, i, j);
  420. ret |= cls->wlist[i].clist[j]->dbf.use_table(cls->wlist[i].clist[j]->dbh, _t);
  421. }
  422. }
  423. }
  424. return ret;
  425. }