dbcl_data.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /*! \file
  25. * \brief DB_CLUSTER :: Core
  26. * \ingroup db_cluster
  27. * Module: \ref db_cluster
  28. */
  29. #include "../../parser/parse_param.h"
  30. #include "../../dprint.h"
  31. #include "../../hashes.h"
  32. #include "../../trim.h"
  33. #include "../../timer.h"
  34. #include "../../mem/mem.h"
  35. #include "../../mem/shm_mem.h"
  36. #include "dbcl_data.h"
  37. static dbcl_con_t *_dbcl_con_root = NULL;
  38. static dbcl_cls_t *_dbcl_cls_root = NULL;
  39. dbcl_con_t *dbcl_get_connection(str *name)
  40. {
  41. dbcl_con_t *sc;
  42. unsigned int conid;
  43. conid = core_case_hash(name, 0, 0);
  44. sc = _dbcl_con_root;
  45. while(sc)
  46. {
  47. if(conid==sc->conid && sc->name.len==name->len
  48. && strncmp(sc->name.s, name->s, name->len)==0)
  49. {
  50. LM_DBG("connection found [%.*s]\n", name->len, name->s);
  51. return sc;
  52. }
  53. sc = sc->next;
  54. }
  55. return NULL;
  56. }
  57. dbcl_cls_t *dbcl_get_cluster(str *name)
  58. {
  59. dbcl_cls_t *sc;
  60. unsigned int clsid;
  61. clsid = core_case_hash(name, 0, 0);
  62. sc = _dbcl_cls_root;
  63. while(sc)
  64. {
  65. if(clsid==sc->clsid && sc->name.len==name->len
  66. && strncmp(sc->name.s, name->s, name->len)==0)
  67. {
  68. LM_DBG("cluster found [%.*s]\n", name->len, name->s);
  69. return sc;
  70. }
  71. sc = sc->next;
  72. }
  73. return NULL;
  74. }
  75. int dbcl_init_con(str *name, str *url)
  76. {
  77. dbcl_con_t *sc;
  78. unsigned int conid;
  79. conid = core_case_hash(name, 0, 0);
  80. sc = _dbcl_con_root;
  81. while(sc)
  82. {
  83. if(conid==sc->conid && sc->name.len==name->len
  84. && strncmp(sc->name.s, name->s, name->len)==0)
  85. {
  86. LM_ERR("duplicate connection name\n");
  87. return -1;
  88. }
  89. sc = sc->next;
  90. }
  91. sc = (dbcl_con_t*)pkg_malloc(sizeof(dbcl_con_t));
  92. if(sc==NULL)
  93. {
  94. LM_ERR("no pkg memory\n");
  95. return -1;
  96. }
  97. memset(sc, 0, sizeof(dbcl_con_t));
  98. sc->conid = conid;
  99. sc->name = *name;
  100. sc->db_url = *url;
  101. sc->sinfo = (dbcl_shared_t*)shm_malloc(sizeof(dbcl_shared_t));
  102. if(sc->sinfo==NULL)
  103. {
  104. LM_ERR("no shm memory\n");
  105. return -1;
  106. }
  107. memset(sc->sinfo, 0, sizeof(dbcl_shared_t));
  108. sc->next = _dbcl_con_root;
  109. _dbcl_con_root = sc;
  110. return 0;
  111. }
  112. int dbcl_valid_con(dbcl_con_t *sc)
  113. {
  114. if(sc==NULL || sc->flags==0 || sc->dbh==NULL)
  115. return -1;
  116. if(sc->sinfo==NULL)
  117. return 0;
  118. if(sc->sinfo->state & DBCL_CON_INACTIVE)
  119. {
  120. if(sc->sinfo->aticks==0)
  121. return -1;
  122. if(sc->sinfo->aticks>get_ticks())
  123. return -1;
  124. sc->sinfo->aticks = 0;
  125. sc->sinfo->state &= ~DBCL_CON_INACTIVE;
  126. }
  127. return 0;
  128. }
  129. extern int dbcl_inactive_interval;
  130. int dbcl_inactive_con(dbcl_con_t *sc)
  131. {
  132. if(sc==NULL || sc->sinfo==NULL)
  133. return -1;
  134. sc->sinfo->aticks = get_ticks() + dbcl_inactive_interval;
  135. sc->sinfo->state |= DBCL_CON_INACTIVE;
  136. return 0;
  137. }
  138. int dbcl_parse_con_param(char *val)
  139. {
  140. str name;
  141. str tok;
  142. str in;
  143. char *p;
  144. /* parse: name=>db_url*/
  145. in.s = val;
  146. in.len = strlen(in.s);
  147. p = in.s;
  148. while(p<in.s+in.len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  149. p++;
  150. if(p>in.s+in.len || *p=='\0')
  151. goto error;
  152. name.s = p;
  153. while(p < in.s + in.len)
  154. {
  155. if(*p=='=' || *p==' ' || *p=='\t' || *p=='\n' || *p=='\r')
  156. break;
  157. p++;
  158. }
  159. if(p>in.s+in.len || *p=='\0')
  160. goto error;
  161. name.len = p - name.s;
  162. if(*p!='=')
  163. {
  164. while(p<in.s+in.len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  165. p++;
  166. if(p>in.s+in.len || *p=='\0' || *p!='=')
  167. goto error;
  168. }
  169. p++;
  170. if(*p!='>')
  171. goto error;
  172. p++;
  173. while(p<in.s+in.len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  174. p++;
  175. tok.s = p;
  176. tok.len = in.len + (int)(in.s - p);
  177. LM_DBG("connection: [%.*s] url: [%.*s]\n", name.len, name.s, tok.len, tok.s);
  178. return dbcl_init_con(&name, &tok);
  179. error:
  180. LM_ERR("invalid connection parameter [%.*s] at [%d]\n", in.len, in.s,
  181. (int)(p-in.s));
  182. return -1;
  183. }
  184. /**
  185. * cons: conid1=1s1p;...
  186. */
  187. int dbcl_cls_set_connections(dbcl_cls_t *cls, str *cons)
  188. {
  189. param_t* params_list = NULL;
  190. param_hooks_t phooks;
  191. param_t *pit=NULL;
  192. dbcl_con_t *sc;
  193. str s;
  194. int i;
  195. if(cls==NULL || cons==NULL)
  196. return -1;
  197. s = *cons;
  198. if(s.s[s.len-1]==';')
  199. s.len--;
  200. if (parse_params(&s, CLASS_ANY, &phooks, &params_list)<0)
  201. return -1;
  202. for (pit = params_list; pit; pit=pit->next)
  203. {
  204. sc = dbcl_get_connection(&pit->name);
  205. if(sc==NULL)
  206. {
  207. LM_ERR("invalid connection id [%.*s]\n",
  208. pit->name.len, pit->name.s);
  209. goto error;
  210. }
  211. s = pit->body;
  212. trim(&s);
  213. if(s.len!=4)
  214. {
  215. LM_ERR("invalid parameter [%.*s] for connection id [%.*s]\n",
  216. pit->body.len, pit->body.s,
  217. pit->name.len, pit->name.s);
  218. goto error;
  219. }
  220. if(s.s[0]<'0' || s.s[0]>'9')
  221. {
  222. LM_ERR("invalid parameter [%.*s] for connection id [%.*s]\n",
  223. pit->body.len, pit->body.s,
  224. pit->name.len, pit->name.s);
  225. goto error;
  226. }
  227. i = s.s[0] - '0';
  228. if(s.s[1]!='s' && s.s[1]!='S' && s.s[1]!='r' && s.s[1]!='R')
  229. {
  230. LM_ERR("invalid parameter [%.*s] for connection id [%.*s]\n",
  231. pit->body.len, pit->body.s,
  232. pit->name.len, pit->name.s);
  233. goto error;
  234. }
  235. if(cls->rlist[i].clen<DBCL_CLIST_SIZE)
  236. {
  237. if(cls->rlist[i].mode==0)
  238. cls->rlist[i].mode = s.s[1] | 32;
  239. cls->rlist[i].prio = i;
  240. cls->rlist[i].clist[cls->rlist[i].clen] = sc;
  241. LM_DBG("added con-id [%.*s] to rlist[%d] at [%d]\n",
  242. pit->name.len, pit->name.s, i, cls->rlist[i].clen);
  243. cls->rlist[i].clen++;
  244. } else {
  245. LM_WARN("too many read connections in cluster - con-id [%.*s]\n",
  246. pit->name.len, pit->name.s);
  247. }
  248. if(s.s[2]<'0' || s.s[2]>'9')
  249. {
  250. LM_ERR("invalid parameter [%.*s] for connection id [%.*s]\n",
  251. pit->body.len, pit->body.s,
  252. pit->name.len, pit->name.s);
  253. goto error;
  254. }
  255. i = s.s[2] - '0';
  256. if(s.s[3]!='s' && s.s[3]!='S' && s.s[3]!='r' && s.s[3]!='R'
  257. && s.s[3]!='p' && s.s[3]!='P')
  258. {
  259. LM_ERR("invalid parameter [%.*s] for connection id [%.*s]\n",
  260. pit->body.len, pit->body.s,
  261. pit->name.len, pit->name.s);
  262. goto error;
  263. }
  264. if(cls->wlist[i].clen<DBCL_CLIST_SIZE)
  265. {
  266. if(cls->wlist[i].mode==0)
  267. cls->wlist[i].mode = s.s[3] | 32;
  268. cls->wlist[i].prio = i;
  269. cls->wlist[i].clist[cls->wlist[i].clen] = sc;
  270. LM_DBG("added con-id [%.*s] to wlist[%d] at [%d]\n",
  271. pit->name.len, pit->name.s, i, cls->wlist[i].clen);
  272. cls->wlist[i].clen++;
  273. } else {
  274. LM_WARN("too many write connections in cluster - con-id [%.*s]\n",
  275. pit->name.len, pit->name.s);
  276. }
  277. }
  278. return 0;
  279. error:
  280. return -1;
  281. }
  282. int dbcl_init_cls(str *name, str *cons)
  283. {
  284. dbcl_cls_t *sc;
  285. unsigned int clsid;
  286. clsid = core_case_hash(name, 0, 0);
  287. sc = _dbcl_cls_root;
  288. while(sc)
  289. {
  290. if(clsid==sc->clsid && sc->name.len==name->len
  291. && strncmp(sc->name.s, name->s, name->len)==0)
  292. {
  293. LM_ERR("duplicate cluster name\n");
  294. return -1;
  295. }
  296. sc = sc->next;
  297. }
  298. sc = (dbcl_cls_t*)pkg_malloc(sizeof(dbcl_cls_t));
  299. if(sc==NULL)
  300. {
  301. LM_ERR("no pkg memory\n");
  302. return -1;
  303. }
  304. memset(sc, 0, sizeof(dbcl_cls_t));
  305. sc->clsid = clsid;
  306. sc->name = *name;
  307. /* parse cls con list */
  308. if(dbcl_cls_set_connections(sc, cons)<0)
  309. {
  310. LM_ERR("unable to add connections to cluster definition\n");
  311. pkg_free(sc);
  312. return -1;
  313. }
  314. sc->next = _dbcl_cls_root;
  315. _dbcl_cls_root = sc;
  316. return 0;
  317. }
  318. int dbcl_parse_cls_param(char *val)
  319. {
  320. str name;
  321. str tok;
  322. str in;
  323. char *p;
  324. /* parse: name=>conlist*/
  325. in.s = val;
  326. in.len = strlen(in.s);
  327. p = in.s;
  328. while(p<in.s+in.len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  329. p++;
  330. if(p>in.s+in.len || *p=='\0')
  331. goto error;
  332. name.s = p;
  333. while(p < in.s + in.len)
  334. {
  335. if(*p=='=' || *p==' ' || *p=='\t' || *p=='\n' || *p=='\r')
  336. break;
  337. p++;
  338. }
  339. if(p>in.s+in.len || *p=='\0')
  340. goto error;
  341. name.len = p - name.s;
  342. if(*p!='=')
  343. {
  344. while(p<in.s+in.len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  345. p++;
  346. if(p>in.s+in.len || *p=='\0' || *p!='=')
  347. goto error;
  348. }
  349. p++;
  350. if(*p!='>')
  351. goto error;
  352. p++;
  353. while(p<in.s+in.len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
  354. p++;
  355. tok.s = p;
  356. tok.len = in.len + (int)(in.s - p);
  357. LM_DBG("cluster: [%.*s] : con-list [%.*s]\n", name.len, name.s, tok.len, tok.s);
  358. return dbcl_init_cls(&name, &tok);
  359. error:
  360. LM_ERR("invalid cluster parameter [%.*s] at [%d]\n", in.len, in.s,
  361. (int)(p-in.s));
  362. return -1;
  363. }
  364. int dbcl_init_dbf(dbcl_cls_t *cls)
  365. {
  366. int i;
  367. int j;
  368. for(i=1; i<DBCL_PRIO_SIZE; i++)
  369. {
  370. for(j=0; j<cls->rlist[i].clen; j++)
  371. {
  372. if(cls->rlist[i].clist[j] != NULL && cls->rlist[i].clist[j]->flags==0)
  373. {
  374. if(db_bind_mod(&cls->rlist[i].clist[j]->db_url,
  375. &cls->rlist[i].clist[j]->dbf)<0)
  376. {
  377. LM_ERR("unable to bind database module\n");
  378. return -1;
  379. }
  380. cls->rlist[i].clist[j]->flags = 1;
  381. }
  382. }
  383. for(j=0; j<cls->wlist[i].clen; j++)
  384. {
  385. if(cls->wlist[i].clist[j] != NULL && cls->wlist[i].clist[j]->flags==0)
  386. {
  387. if(db_bind_mod(&cls->wlist[i].clist[j]->db_url,
  388. &cls->wlist[i].clist[j]->dbf)<0)
  389. {
  390. LM_ERR("unable to bind database module\n");
  391. return -1;
  392. }
  393. cls->wlist[i].clist[j]->flags = 1;
  394. }
  395. }
  396. }
  397. return 0;
  398. }
  399. int dbcl_init_connections(dbcl_cls_t *cls)
  400. {
  401. int i;
  402. int j;
  403. for(i=1; i<DBCL_PRIO_SIZE; i++)
  404. {
  405. for(j=0; j<cls->rlist[i].clen; j++)
  406. {
  407. if(cls->rlist[i].clist[j] != NULL && cls->rlist[i].clist[j]->flags!=0)
  408. {
  409. LM_DBG("setting up read connection [%.*s]\n",
  410. cls->rlist[i].clist[j]->name.len,
  411. cls->rlist[i].clist[j]->name.s);
  412. cls->rlist[i].clist[j]->dbh =
  413. cls->rlist[i].clist[j]->dbf.init(&cls->rlist[i].clist[j]->db_url);
  414. if(cls->rlist[i].clist[j]->dbh==NULL)
  415. {
  416. LM_WARN("cannot connect to database - connection [%.*s]\n",
  417. cls->rlist[i].clist[j]->name.len,
  418. cls->rlist[i].clist[j]->name.s);
  419. }
  420. }
  421. }
  422. for(j=0; j<cls->wlist[i].clen; j++)
  423. {
  424. if(cls->wlist[i].clist[j] != NULL && cls->wlist[i].clist[j]->flags!=0)
  425. {
  426. LM_DBG("setting up write connection [%.*s]\n",
  427. cls->wlist[i].clist[j]->name.len,
  428. cls->wlist[i].clist[j]->name.s);
  429. cls->wlist[i].clist[j]->dbh =
  430. cls->wlist[i].clist[j]->dbf.init(&cls->wlist[i].clist[j]->db_url);
  431. if(cls->wlist[i].clist[j]->dbh==NULL)
  432. {
  433. LM_WARN("cannot connect to database - connection [%.*s]\n",
  434. cls->wlist[i].clist[j]->name.len,
  435. cls->wlist[i].clist[j]->name.s);
  436. }
  437. }
  438. }
  439. }
  440. return 0;
  441. }
  442. int dbcl_close_connections(dbcl_cls_t *cls)
  443. {
  444. int i;
  445. int j;
  446. if(cls->ref > 0)
  447. return 0;
  448. for(i=1; i<DBCL_PRIO_SIZE; i++)
  449. {
  450. for(j=0; j<cls->rlist[i].clen; j++)
  451. {
  452. if(cls->rlist[i].clist[j] != NULL && cls->rlist[i].clist[j]->flags!=0
  453. && cls->rlist[i].clist[j]->dbh != NULL)
  454. {
  455. cls->rlist[i].clist[j]->dbf.close(cls->rlist[i].clist[j]->dbh);
  456. cls->rlist[i].clist[j]->dbh = NULL;
  457. }
  458. }
  459. for(j=0; j<cls->wlist[i].clen; j++)
  460. {
  461. if(cls->wlist[i].clist[j] != NULL && cls->wlist[i].clist[j]->flags!=0
  462. && cls->wlist[i].clist[j]->dbh != NULL)
  463. {
  464. cls->wlist[i].clist[j]->dbf.close(cls->wlist[i].clist[j]->dbh);
  465. cls->wlist[i].clist[j]->dbh = NULL;
  466. }
  467. }
  468. }
  469. return 0;
  470. }