cl5_clcache.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2005 Red Hat, Inc.
  35. * All rights reserved.
  36. * END COPYRIGHT BLOCK **/
  37. #ifdef HAVE_CONFIG_H
  38. # include <config.h>
  39. #endif
  40. #include "errno.h" /* ENOMEM, EVAL used by Berkeley DB */
  41. #include "db.h" /* Berkeley DB */
  42. #include "cl5.h" /* changelog5Config */
  43. #include "cl5_clcache.h"
  44. /* newer bdb uses DB_BUFFER_SMALL instead of ENOMEM as the
  45. error return if the given buffer in which to load a
  46. key or value is too small - if it is not defined, define
  47. it here to ENOMEM
  48. */
  49. #ifndef DB_BUFFER_SMALL
  50. #define DB_BUFFER_SMALL ENOMEM
  51. #endif
  52. /*
  53. * Constants for the buffer pool:
  54. *
  55. * DEFAULT_CLC_BUFFER_PAGE_COUNT
  56. * Little performance boost if it is too small.
  57. *
  58. * DEFAULT_CLC_BUFFER_PAGE_SIZE
  59. * Its value is determined based on the DB requirement that
  60. * the buffer size should be the multiple of 1024.
  61. */
  62. #define DEFAULT_CLC_BUFFER_COUNT_MIN 10
  63. #define DEFAULT_CLC_BUFFER_COUNT_MAX 0
  64. #define DEFAULT_CLC_BUFFER_PAGE_COUNT 32
  65. #define DEFAULT_CLC_BUFFER_PAGE_SIZE 1024
  66. enum {
  67. CLC_STATE_READY = 0, /* ready to iterate */
  68. CLC_STATE_UP_TO_DATE, /* remote RUV already covers the CSN */
  69. CLC_STATE_CSN_GT_RUV, /* local RUV doesn't conver the CSN */
  70. CLC_STATE_NEW_RID, /* unknown RID to local RUVs */
  71. CLC_STATE_UNSAFE_RUV_CHANGE,/* (RUV1 < maxcsn-in-buffer) && (RUV1 < RUV1') */
  72. CLC_STATE_DONE, /* no more change */
  73. CLC_STATE_ABORTING /* abort replication session */
  74. };
  75. typedef struct clc_busy_list CLC_Busy_List;
  76. struct csn_seq_ctrl_block {
  77. ReplicaId rid; /* RID this block serves */
  78. CSN *consumer_maxcsn; /* Don't send CSN <= this */
  79. CSN *local_maxcsn; /* Don't send CSN > this */
  80. CSN *prev_local_maxcsn; /* */
  81. int state; /* CLC_STATE_* */
  82. };
  83. /*
  84. * Each cl5replayiterator acquires a buffer from the buffer pool
  85. * at the beginning of a replication session, and returns it back
  86. * at the end.
  87. */
  88. struct clc_buffer {
  89. char *buf_agmt_name; /* agreement acquired this buffer */
  90. ReplicaId buf_consumer_rid; /* help checking threshold csn */
  91. const RUV *buf_consumer_ruv; /* used to skip change */
  92. const RUV *buf_local_ruv; /* used to refresh local_maxcsn */
  93. /*
  94. * fields for retriving data from DB
  95. */
  96. int buf_state;
  97. CSN *buf_current_csn;
  98. int buf_load_flag; /* db flag DB_MULTIPLE_KEY, DB_SET, DB_NEXT */
  99. DBC *buf_cursor;
  100. DBT buf_key; /* current csn string */
  101. DBT buf_data; /* data retrived from db */
  102. void *buf_record_ptr; /* ptr to the current record in data */
  103. CSN *buf_missing_csn; /* used to detect persistent missing of CSN */
  104. CSN *buf_prev_missing_csn; /* used to surpress the repeated messages */
  105. /* fields for control the CSN sequence sent to the consumer */
  106. struct csn_seq_ctrl_block **buf_cscbs;
  107. int buf_num_cscbs; /* number of csn sequence ctrl blocks */
  108. int buf_max_cscbs;
  109. /* fields for debugging stat */
  110. int buf_load_cnt; /* number of loads for session */
  111. int buf_record_cnt; /* number of changes for session */
  112. int buf_record_skipped; /* number of changes skipped */
  113. int buf_skipped_new_rid; /* number of changes skipped due to new_rid */
  114. int buf_skipped_csn_gt_cons_maxcsn; /* number of changes skipped due to csn greater than consumer maxcsn */
  115. int buf_skipped_up_to_date; /* number of changes skipped due to consumer being up-to-date for the given rid */
  116. int buf_skipped_csn_gt_ruv; /* number of changes skipped due to preceedents are not covered by local RUV snapshot */
  117. int buf_skipped_csn_covered; /* number of changes skipped due to CSNs already covered by consumer RUV */
  118. /*
  119. * fields that should be accessed via bl_lock or pl_lock
  120. */
  121. CLC_Buffer *buf_next; /* next buffer in the same list */
  122. CLC_Busy_List *buf_busy_list; /* which busy list I'm in */
  123. };
  124. /*
  125. * Each changelog has a busy buffer list
  126. */
  127. struct clc_busy_list {
  128. PRLock *bl_lock;
  129. DB *bl_db; /* changelog db handle */
  130. CLC_Buffer *bl_buffers; /* busy buffers of this list */
  131. CLC_Busy_List *bl_next; /* next busy list in the pool */
  132. };
  133. /*
  134. * Each process has a buffer pool
  135. */
  136. struct clc_pool {
  137. Slapi_RWLock *pl_lock; /* cl writer and agreements */
  138. DB_ENV **pl_dbenv; /* pointer to DB_ENV for all the changelog files */
  139. CLC_Busy_List *pl_busy_lists; /* busy buffer lists, one list per changelog file */
  140. int pl_buffer_cnt_now; /* total number of buffers */
  141. int pl_buffer_cnt_min; /* free a newly returned buffer if _now > _min */
  142. int pl_buffer_cnt_max; /* no use */
  143. int pl_buffer_default_pages; /* num of pages in a new buffer */
  144. };
  145. /* static variables */
  146. static struct clc_pool *_pool = NULL; /* process's buffer pool */
  147. /* static prototypes */
  148. static int clcache_adjust_anchorcsn ( CLC_Buffer *buf );
  149. static void clcache_refresh_consumer_maxcsns ( CLC_Buffer *buf );
  150. static int clcache_refresh_local_maxcsns ( CLC_Buffer *buf );
  151. static int clcache_skip_change ( CLC_Buffer *buf );
  152. static int clcache_load_buffer_bulk ( CLC_Buffer *buf, int flag );
  153. static int clcache_open_cursor ( DB_TXN *txn, CLC_Buffer *buf, DBC **cursor );
  154. static int clcache_cursor_get ( DBC *cursor, CLC_Buffer *buf, int flag );
  155. static struct csn_seq_ctrl_block *clcache_new_cscb ();
  156. static void clcache_free_cscb ( struct csn_seq_ctrl_block ** cscb );
  157. static CLC_Buffer *clcache_new_buffer ( ReplicaId consumer_rid );
  158. static void clcache_delete_buffer ( CLC_Buffer **buf );
  159. static CLC_Busy_List *clcache_new_busy_list ();
  160. static void clcache_delete_busy_list ( CLC_Busy_List **bl );
  161. static int clcache_enqueue_busy_list( DB *db, CLC_Buffer *buf );
  162. static void csn_dup_or_init_by_csn ( CSN **csn1, CSN *csn2 );
  163. /*
  164. * Initiates the process buffer pool. This should be done
  165. * once and only once when process starts.
  166. */
  167. int
  168. clcache_init ( DB_ENV **dbenv )
  169. {
  170. if (_pool) {
  171. return 0; /* already initialized */
  172. }
  173. if (NULL == dbenv) {
  174. return -1;
  175. }
  176. _pool = (struct clc_pool*) slapi_ch_calloc ( 1, sizeof ( struct clc_pool ));
  177. _pool->pl_dbenv = dbenv;
  178. _pool->pl_buffer_cnt_min = DEFAULT_CLC_BUFFER_COUNT_MIN;
  179. _pool->pl_buffer_cnt_max = DEFAULT_CLC_BUFFER_COUNT_MAX;
  180. _pool->pl_buffer_default_pages = DEFAULT_CLC_BUFFER_COUNT_MAX;
  181. _pool->pl_lock = slapi_new_rwlock ();
  182. return 0;
  183. }
  184. /*
  185. * This is part of a callback function when changelog configuration
  186. * is read or updated.
  187. */
  188. void
  189. clcache_set_config ()
  190. {
  191. slapi_rwlock_wrlock ( _pool->pl_lock );
  192. _pool->pl_buffer_cnt_max = CL5_DEFAULT_CONFIG_CACHESIZE;
  193. /*
  194. * According to http://www.sleepycat.com/docs/api_c/dbc_get.html,
  195. * data buffer should be a multiple of 1024 bytes in size
  196. * for DB_MULTIPLE_KEY operation.
  197. */
  198. _pool->pl_buffer_default_pages = CL5_DEFAULT_CONFIG_CACHEMEMSIZE / DEFAULT_CLC_BUFFER_PAGE_SIZE + 1;
  199. _pool->pl_buffer_default_pages = DEFAULT_CLC_BUFFER_PAGE_COUNT;
  200. if ( _pool->pl_buffer_default_pages <= 0 ) {
  201. _pool->pl_buffer_default_pages = DEFAULT_CLC_BUFFER_PAGE_COUNT;
  202. }
  203. slapi_rwlock_unlock ( _pool->pl_lock );
  204. }
  205. /*
  206. * Gets the pointer to a thread dedicated buffer, or allocates
  207. * a new buffer if there is no buffer allocated yet for this thread.
  208. *
  209. * This is called when a cl5replayiterator is created for
  210. * a replication session.
  211. */
  212. int
  213. clcache_get_buffer ( CLC_Buffer **buf, DB *db, ReplicaId consumer_rid, const RUV *consumer_ruv, const RUV *local_ruv )
  214. {
  215. int rc = 0;
  216. int need_new;
  217. if ( buf == NULL ) return CL5_BAD_DATA;
  218. *buf = NULL;
  219. /* if the pool was re-initialized, the thread private cache will be invalid,
  220. so we must get a new one */
  221. need_new = (!_pool || !_pool->pl_busy_lists || !_pool->pl_busy_lists->bl_buffers);
  222. if ( (!need_new) && (NULL != ( *buf = (CLC_Buffer*) get_thread_private_cache())) ) {
  223. slapi_log_error ( SLAPI_LOG_REPL, get_thread_private_agmtname(),
  224. "clcache_get_buffer: found thread private buffer cache %p\n", *buf);
  225. slapi_log_error ( SLAPI_LOG_REPL, get_thread_private_agmtname(),
  226. "clcache_get_buffer: _pool is %p _pool->pl_busy_lists is %p _pool->pl_busy_lists->bl_buffers is %p\n",
  227. _pool, _pool ? _pool->pl_busy_lists : NULL,
  228. (_pool && _pool->pl_busy_lists) ? _pool->pl_busy_lists->bl_buffers : NULL);
  229. (*buf)->buf_state = CLC_STATE_READY;
  230. (*buf)->buf_load_cnt = 0;
  231. (*buf)->buf_record_cnt = 0;
  232. (*buf)->buf_record_skipped = 0;
  233. (*buf)->buf_cursor = NULL;
  234. (*buf)->buf_skipped_new_rid = 0;
  235. (*buf)->buf_skipped_csn_gt_cons_maxcsn = 0;
  236. (*buf)->buf_skipped_up_to_date = 0;
  237. (*buf)->buf_skipped_csn_gt_ruv = 0;
  238. (*buf)->buf_skipped_csn_covered = 0;
  239. (*buf)->buf_cscbs = (struct csn_seq_ctrl_block **) slapi_ch_calloc(MAX_NUM_OF_MASTERS + 1,
  240. sizeof(struct csn_seq_ctrl_block *));
  241. (*buf)->buf_num_cscbs = 0;
  242. (*buf)->buf_max_cscbs = MAX_NUM_OF_MASTERS;
  243. }
  244. else {
  245. *buf = clcache_new_buffer ( consumer_rid );
  246. if ( *buf ) {
  247. if ( 0 == clcache_enqueue_busy_list ( db, *buf ) ) {
  248. set_thread_private_cache ( (void*) (*buf) );
  249. }
  250. else {
  251. clcache_delete_buffer ( buf );
  252. }
  253. }
  254. }
  255. if ( NULL != *buf ) {
  256. (*buf)->buf_consumer_ruv = consumer_ruv;
  257. (*buf)->buf_local_ruv = local_ruv;
  258. }
  259. else {
  260. slapi_log_error ( SLAPI_LOG_FATAL, get_thread_private_agmtname(),
  261. "clcache_get_buffer: can't allocate new buffer\n" );
  262. rc = CL5_MEMORY_ERROR;
  263. }
  264. return rc;
  265. }
  266. /*
  267. * Returns a buffer back to the buffer pool.
  268. */
  269. void
  270. clcache_return_buffer ( CLC_Buffer **buf )
  271. {
  272. int i;
  273. slapi_log_error ( SLAPI_LOG_REPL, (*buf)->buf_agmt_name,
  274. "session end: state=%d load=%d sent=%d skipped=%d skipped_new_rid=%d "
  275. "skipped_csn_gt_cons_maxcsn=%d skipped_up_to_date=%d "
  276. "skipped_csn_gt_ruv=%d skipped_csn_covered=%d\n",
  277. (*buf)->buf_state,
  278. (*buf)->buf_load_cnt,
  279. (*buf)->buf_record_cnt - (*buf)->buf_record_skipped,
  280. (*buf)->buf_record_skipped, (*buf)->buf_skipped_new_rid,
  281. (*buf)->buf_skipped_csn_gt_cons_maxcsn,
  282. (*buf)->buf_skipped_up_to_date, (*buf)->buf_skipped_csn_gt_ruv,
  283. (*buf)->buf_skipped_csn_covered);
  284. for ( i = 0; i < (*buf)->buf_num_cscbs; i++ ) {
  285. clcache_free_cscb ( &(*buf)->buf_cscbs[i] );
  286. }
  287. slapi_ch_free((void **)&(*buf)->buf_cscbs);
  288. if ( (*buf)->buf_cursor ) {
  289. (*buf)->buf_cursor->c_close ( (*buf)->buf_cursor );
  290. (*buf)->buf_cursor = NULL;
  291. }
  292. }
  293. /*
  294. * Loads a buffer from DB.
  295. *
  296. * anchorcsn - passed in for the first load of a replication session;
  297. * flag - DB_SET to load in the key CSN record.
  298. * DB_NEXT to load in the records greater than key CSN.
  299. * return - DB error code instead of cl5 one because of the
  300. * historic reason.
  301. */
  302. int
  303. clcache_load_buffer ( CLC_Buffer *buf, CSN *anchorcsn, int flag )
  304. {
  305. int rc = 0;
  306. clcache_refresh_local_maxcsns ( buf );
  307. /* Set the loading key */
  308. if ( anchorcsn ) {
  309. clcache_refresh_consumer_maxcsns ( buf );
  310. buf->buf_load_flag = DB_MULTIPLE_KEY;
  311. csn_as_string ( anchorcsn, 0, (char*)buf->buf_key.data );
  312. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  313. "session start: anchorcsn=%s\n", (char*)buf->buf_key.data );
  314. }
  315. else if ( csn_get_time(buf->buf_current_csn) == 0 ) {
  316. /* time == 0 means this csn has never been set */
  317. rc = DB_NOTFOUND;
  318. }
  319. else if ( clcache_adjust_anchorcsn ( buf ) != 0 ) {
  320. rc = DB_NOTFOUND;
  321. }
  322. else {
  323. csn_as_string ( buf->buf_current_csn, 0, (char*)buf->buf_key.data );
  324. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  325. "load next: anchorcsn=%s\n", (char*)buf->buf_key.data );
  326. }
  327. if ( rc == 0 ) {
  328. buf->buf_state = CLC_STATE_READY;
  329. rc = clcache_load_buffer_bulk ( buf, flag );
  330. /* Reset some flag variables */
  331. if ( rc == 0 ) {
  332. int i;
  333. for ( i = 0; i < buf->buf_num_cscbs; i++ ) {
  334. buf->buf_cscbs[i]->state = CLC_STATE_READY;
  335. }
  336. }
  337. else if ( anchorcsn ) {
  338. /* Report error only when the missing is persistent */
  339. if ( buf->buf_missing_csn && csn_compare (buf->buf_missing_csn, anchorcsn) == 0 ) {
  340. if (!buf->buf_prev_missing_csn || csn_compare (buf->buf_prev_missing_csn, anchorcsn)) {
  341. slapi_log_error ( SLAPI_LOG_FATAL, buf->buf_agmt_name,
  342. "Can't locate CSN %s in the changelog (DB rc=%d). If replication stops, the consumer may need to be reinitialized.\n",
  343. (char*)buf->buf_key.data, rc );
  344. csn_dup_or_init_by_csn (&buf->buf_prev_missing_csn, anchorcsn);
  345. }
  346. }
  347. else {
  348. csn_dup_or_init_by_csn (&buf->buf_missing_csn, anchorcsn);
  349. }
  350. }
  351. }
  352. if ( rc != 0 ) {
  353. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  354. "clcache_load_buffer: rc=%d\n", rc );
  355. }
  356. return rc;
  357. }
  358. static int
  359. clcache_load_buffer_bulk ( CLC_Buffer *buf, int flag )
  360. {
  361. DB_TXN *txn = NULL;
  362. DBC *cursor = NULL;
  363. int rc = 0;
  364. int tries = 0;
  365. #if 0 /* txn control seems not improving anything so turn it off */
  366. if ( *(_pool->pl_dbenv) ) {
  367. txn_begin( *(_pool->pl_dbenv), NULL, &txn, 0 );
  368. }
  369. #endif
  370. if (NULL == buf) {
  371. slapi_log_error ( SLAPI_LOG_FATAL, "clcache_load_buffer_bulk",
  372. "NULL buf\n" );
  373. return rc;
  374. }
  375. if (NULL == buf->buf_busy_list) {
  376. slapi_log_error ( SLAPI_LOG_FATAL, "clcache_load_buffer_bulk",
  377. "%s%sno buf_busy_list\n",
  378. buf->buf_agmt_name?buf->buf_agmt_name:"",
  379. buf->buf_agmt_name?": ":"" );
  380. return rc;
  381. }
  382. PR_Lock ( buf->buf_busy_list->bl_lock );
  383. retry:
  384. if ( 0 == ( rc = clcache_open_cursor ( txn, buf, &cursor )) ) {
  385. if ( flag == DB_NEXT ) {
  386. /* For bulk read, position the cursor before read the next block */
  387. rc = cursor->c_get ( cursor,
  388. & buf->buf_key,
  389. & buf->buf_data,
  390. DB_SET );
  391. }
  392. /*
  393. * Continue if the error is no-mem since we don't need to
  394. * load in the key record anyway with DB_SET.
  395. */
  396. if ( 0 == rc || DB_BUFFER_SMALL == rc )
  397. rc = clcache_cursor_get ( cursor, buf, flag );
  398. }
  399. /*
  400. * Don't keep a cursor open across the whole replication session.
  401. * That had caused noticeable DB resource contention.
  402. */
  403. if ( cursor ) {
  404. cursor->c_close ( cursor );
  405. cursor = NULL;
  406. }
  407. if ((rc == DB_LOCK_DEADLOCK) && (tries < MAX_TRIALS)) {
  408. PRIntervalTime interval;
  409. tries++;
  410. slapi_log_error ( SLAPI_LOG_TRACE, "clcache_load_buffer_bulk",
  411. "deadlock number [%d] - retrying\n", tries );
  412. /* back off */
  413. interval = PR_MillisecondsToInterval(slapi_rand() % 100);
  414. DS_Sleep(interval);
  415. goto retry;
  416. }
  417. if ((rc == DB_LOCK_DEADLOCK) && (tries >= MAX_TRIALS)) {
  418. slapi_log_error ( SLAPI_LOG_REPL, "clcache_load_buffer_bulk",
  419. "could not load buffer from changelog after %d tries\n", tries );
  420. }
  421. #if 0 /* txn control seems not improving anything so turn it off */
  422. if ( txn ) {
  423. txn->commit ( txn, DB_TXN_NOSYNC );
  424. }
  425. #endif
  426. PR_Unlock ( buf->buf_busy_list->bl_lock );
  427. buf->buf_record_ptr = NULL;
  428. if ( 0 == rc ) {
  429. DB_MULTIPLE_INIT ( buf->buf_record_ptr, &buf->buf_data );
  430. if ( NULL == buf->buf_record_ptr )
  431. rc = DB_NOTFOUND;
  432. else
  433. buf->buf_load_cnt++;
  434. }
  435. return rc;
  436. }
  437. /*
  438. * Gets the next change from the buffer.
  439. * *key : output - key of the next change, or NULL if no more change
  440. * *data: output - data of the next change, or NULL if no more change
  441. */
  442. int
  443. clcache_get_next_change ( CLC_Buffer *buf, void **key, size_t *keylen, void **data, size_t *datalen, CSN **csn )
  444. {
  445. int skip = 1;
  446. int rc = 0;
  447. do {
  448. *key = *data = NULL;
  449. *keylen = *datalen = 0;
  450. if ( buf->buf_record_ptr ) {
  451. DB_MULTIPLE_KEY_NEXT ( buf->buf_record_ptr, &buf->buf_data,
  452. *key, *keylen, *data, *datalen );
  453. }
  454. /*
  455. * We're done with the current buffer. Now load the next chunk.
  456. */
  457. if ( NULL == *key && CLC_STATE_READY == buf->buf_state ) {
  458. rc = clcache_load_buffer ( buf, NULL, DB_NEXT );
  459. if ( 0 == rc && buf->buf_record_ptr ) {
  460. DB_MULTIPLE_KEY_NEXT ( buf->buf_record_ptr, &buf->buf_data,
  461. *key, *keylen, *data, *datalen );
  462. }
  463. }
  464. /* Compare the new change to the local and remote RUVs */
  465. if ( NULL != *key ) {
  466. buf->buf_record_cnt++;
  467. csn_init_by_string ( buf->buf_current_csn, (char*)*key );
  468. skip = clcache_skip_change ( buf );
  469. if (skip) buf->buf_record_skipped++;
  470. }
  471. }
  472. while ( rc == 0 && *key && skip );
  473. if ( NULL == *key ) {
  474. *key = NULL;
  475. *csn = NULL;
  476. rc = DB_NOTFOUND;
  477. }
  478. else {
  479. *csn = buf->buf_current_csn;
  480. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  481. "load=%d rec=%d csn=%s\n",
  482. buf->buf_load_cnt, buf->buf_record_cnt, (char*)*key );
  483. }
  484. return rc;
  485. }
  486. static void
  487. clcache_refresh_consumer_maxcsns ( CLC_Buffer *buf )
  488. {
  489. int i;
  490. for ( i = 0; i < buf->buf_num_cscbs; i++ ) {
  491. csn_free(&buf->buf_cscbs[i]->consumer_maxcsn);
  492. ruv_get_largest_csn_for_replica (
  493. buf->buf_consumer_ruv,
  494. buf->buf_cscbs[i]->rid,
  495. &buf->buf_cscbs[i]->consumer_maxcsn );
  496. }
  497. }
  498. static int
  499. clcache_refresh_local_maxcsn ( const ruv_enum_data *rid_data, void *data )
  500. {
  501. struct clc_buffer *buf = (struct clc_buffer*) data;
  502. ReplicaId rid;
  503. int rc = 0;
  504. int i;
  505. rid = csn_get_replicaid ( rid_data->csn );
  506. /*
  507. * No need to create cscb for consumer's RID.
  508. * If RID==65535, the CSN is originated from a
  509. * legacy consumer. In this case the supplier
  510. * and the consumer may have the same RID.
  511. */
  512. if ( rid == buf->buf_consumer_rid && rid != MAX_REPLICA_ID )
  513. return rc;
  514. for ( i = 0; i < buf->buf_num_cscbs; i++ ) {
  515. if ( buf->buf_cscbs[i]->rid == rid )
  516. break;
  517. }
  518. if ( i >= buf->buf_num_cscbs ) {
  519. if( i + 1 > buf->buf_max_cscbs){
  520. buf->buf_cscbs = (struct csn_seq_ctrl_block **) slapi_ch_realloc((char *)buf->buf_cscbs,
  521. (i + 2) * sizeof(struct csn_seq_ctrl_block *));
  522. buf->buf_max_cscbs = i + 1;
  523. }
  524. buf->buf_cscbs[i] = clcache_new_cscb();
  525. if ( buf->buf_cscbs[i] == NULL ) {
  526. return -1;
  527. }
  528. buf->buf_cscbs[i]->rid = rid;
  529. buf->buf_num_cscbs++;
  530. }
  531. csn_dup_or_init_by_csn ( &buf->buf_cscbs[i]->local_maxcsn, rid_data->csn );
  532. if ( buf->buf_cscbs[i]->consumer_maxcsn &&
  533. csn_compare (buf->buf_cscbs[i]->consumer_maxcsn, rid_data->csn) >= 0 ) {
  534. /* No change need to be sent for this RID */
  535. buf->buf_cscbs[i]->state = CLC_STATE_UP_TO_DATE;
  536. }
  537. return rc;
  538. }
  539. static int
  540. clcache_refresh_local_maxcsns ( CLC_Buffer *buf )
  541. {
  542. int i;
  543. for ( i = 0; i < buf->buf_num_cscbs; i++ ) {
  544. csn_dup_or_init_by_csn ( &buf->buf_cscbs[i]->prev_local_maxcsn,
  545. buf->buf_cscbs[i]->local_maxcsn );
  546. }
  547. return ruv_enumerate_elements ( buf->buf_local_ruv, clcache_refresh_local_maxcsn, buf );
  548. }
  549. /*
  550. * Algorithm:
  551. *
  552. * 1. Snapshot local RUVs;
  553. * 2. Load buffer;
  554. * 3. Send to the consumer only those CSNs that are covered
  555. * by the RUVs snapshot taken in the first step;
  556. * All CSNs that are covered by the RUVs snapshot taken in the
  557. * first step are guaranteed in consecutive order for the respected
  558. * RIDs because of the the CSN pending list control;
  559. * A CSN that is not covered by the RUVs snapshot may be out of order
  560. * since it is possible that a smaller CSN might not have committed
  561. * yet by the time the buffer was loaded.
  562. * 4. Determine anchorcsn for each RID:
  563. *
  564. * Case| Local vs. Buffer | New Local | Next
  565. * | MaxCSN MaxCSN | MaxCSN | Anchor-CSN
  566. * ----+-------------------+-----------+----------------
  567. * 1 | Cl >= Cb | * | Cb
  568. * 2 | Cl < Cb | Cl | Cb
  569. * 3 | Cl < Cb | Cl2 | Cl
  570. *
  571. * 5. Determine anchorcsn for next load:
  572. * Anchor-CSN = min { all Next-Anchor-CSN, Buffer-MaxCSN }
  573. */
  574. static int
  575. clcache_adjust_anchorcsn ( CLC_Buffer *buf )
  576. {
  577. PRBool hasChange = PR_FALSE;
  578. struct csn_seq_ctrl_block *cscb;
  579. int i;
  580. if ( buf->buf_state == CLC_STATE_READY ) {
  581. for ( i = 0; i < buf->buf_num_cscbs; i++ ) {
  582. cscb = buf->buf_cscbs[i];
  583. if ( cscb->state == CLC_STATE_UP_TO_DATE )
  584. continue;
  585. /*
  586. * Case 3 unsafe ruv change: next buffer load should start
  587. * from where the maxcsn in the old ruv was. Since each
  588. * cscb has remembered the maxcsn sent to the consumer,
  589. * CSNs that may be loaded again could easily be skipped.
  590. */
  591. if ( cscb->prev_local_maxcsn &&
  592. csn_compare (cscb->prev_local_maxcsn, buf->buf_current_csn) < 0 &&
  593. csn_compare (cscb->local_maxcsn, cscb->prev_local_maxcsn) != 0 ) {
  594. hasChange = PR_TRUE;
  595. cscb->state = CLC_STATE_READY;
  596. csn_init_by_csn ( buf->buf_current_csn, cscb->prev_local_maxcsn );
  597. csn_as_string ( cscb->prev_local_maxcsn, 0, (char*)buf->buf_key.data );
  598. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  599. "adjust anchor csn upon %s\n",
  600. ( cscb->state == CLC_STATE_CSN_GT_RUV ? "out of sequence csn" : "unsafe ruv change") );
  601. continue;
  602. }
  603. /*
  604. * check if there are still changes to send for this RID
  605. * Assume we had compared the local maxcsn and the consumer
  606. * max csn before this function was called and hence the
  607. * cscb->state had been set accordingly.
  608. */
  609. if ( hasChange == PR_FALSE &&
  610. csn_compare (cscb->local_maxcsn, buf->buf_current_csn) > 0 ) {
  611. hasChange = PR_TRUE;
  612. }
  613. }
  614. }
  615. if ( !hasChange ) {
  616. buf->buf_state = CLC_STATE_DONE;
  617. }
  618. return buf->buf_state;
  619. }
  620. static int
  621. clcache_skip_change ( CLC_Buffer *buf )
  622. {
  623. struct csn_seq_ctrl_block *cscb = NULL;
  624. ReplicaId rid;
  625. int skip = 1;
  626. int i;
  627. char buf_cur_csn_str[CSN_STRSIZE];
  628. char oth_csn_str[CSN_STRSIZE];
  629. do {
  630. rid = csn_get_replicaid ( buf->buf_current_csn );
  631. /*
  632. * Skip CSN that is originated from the consumer,
  633. * unless the CSN is newer than the maxcsn.
  634. * If RID==65535, the CSN is originated from a
  635. * legacy consumer. In this case the supplier
  636. * and the consumer may have the same RID.
  637. */
  638. if (rid == buf->buf_consumer_rid && rid != MAX_REPLICA_ID){
  639. CSN *cons_maxcsn = NULL;
  640. ruv_get_max_csn(buf->buf_consumer_ruv, &cons_maxcsn);
  641. if ( csn_compare ( buf->buf_current_csn, cons_maxcsn) > 0 ) {
  642. /*
  643. * The consumer must have been "restored" and needs this newer update.
  644. */
  645. skip = 0;
  646. } else if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  647. csn_as_string(buf->buf_current_csn, 0, buf_cur_csn_str);
  648. csn_as_string(cons_maxcsn, 0, oth_csn_str);
  649. slapi_log_error(SLAPI_LOG_REPL, buf->buf_agmt_name,
  650. "Skipping update because the changelog buffer current csn [%s] is "
  651. "less than or equal to the consumer max csn [%s]\n",
  652. buf_cur_csn_str, oth_csn_str);
  653. buf->buf_skipped_csn_gt_cons_maxcsn++;
  654. }
  655. csn_free(&cons_maxcsn);
  656. break;
  657. }
  658. /* Skip helper entry (ENTRY_COUNT, PURGE_RUV and so on) */
  659. if ( cl5HelperEntry ( NULL, buf->buf_current_csn ) == PR_TRUE ) {
  660. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  661. "Skip helper entry type=%ld\n", csn_get_time( buf->buf_current_csn ));
  662. break;
  663. }
  664. /* Find csn sequence control block for the current rid */
  665. for (i = 0; i < buf->buf_num_cscbs && buf->buf_cscbs[i]->rid != rid; i++);
  666. /* Skip CSN whose RID is unknown to the local RUV snapshot */
  667. if ( i >= buf->buf_num_cscbs ) {
  668. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  669. csn_as_string(buf->buf_current_csn, 0, buf_cur_csn_str);
  670. slapi_log_error(SLAPI_LOG_REPL, buf->buf_agmt_name,
  671. "Skipping update because the changelog buffer current csn [%s] rid "
  672. "[%d] is not in the list of changelog csn buffers (length %d)\n",
  673. buf_cur_csn_str, rid, buf->buf_num_cscbs);
  674. }
  675. buf->buf_skipped_new_rid++;
  676. break;
  677. }
  678. cscb = buf->buf_cscbs[i];
  679. /* Skip if the consumer is already up-to-date for the RID */
  680. if ( cscb->state == CLC_STATE_UP_TO_DATE ) {
  681. buf->buf_skipped_up_to_date++;
  682. break;
  683. }
  684. /* Skip CSN whose preceedents are not covered by local RUV snapshot */
  685. if ( cscb->state == CLC_STATE_CSN_GT_RUV ) {
  686. buf->buf_skipped_csn_gt_ruv++;
  687. break;
  688. }
  689. /* Skip CSNs already covered by consumer RUV */
  690. if ( cscb->consumer_maxcsn &&
  691. csn_compare ( buf->buf_current_csn, cscb->consumer_maxcsn ) <= 0 ) {
  692. buf->buf_skipped_csn_covered++;
  693. break;
  694. }
  695. /* Send CSNs that are covered by the local RUV snapshot */
  696. if ( csn_compare ( buf->buf_current_csn, cscb->local_maxcsn ) <= 0 ) {
  697. skip = 0;
  698. csn_dup_or_init_by_csn ( &cscb->consumer_maxcsn, buf->buf_current_csn );
  699. break;
  700. }
  701. /*
  702. * Promote the local maxcsn to its next neighbor
  703. * to keep the current session going. Skip if we
  704. * are not sure if current_csn is the neighbor.
  705. */
  706. if ( csn_time_difference(buf->buf_current_csn, cscb->local_maxcsn) == 0 &&
  707. (csn_get_seqnum(buf->buf_current_csn) ==
  708. csn_get_seqnum(cscb->local_maxcsn) + 1) )
  709. {
  710. csn_init_by_csn ( cscb->local_maxcsn, buf->buf_current_csn );
  711. if(cscb->consumer_maxcsn){
  712. csn_init_by_csn ( cscb->consumer_maxcsn, buf->buf_current_csn );
  713. }
  714. skip = 0;
  715. break;
  716. }
  717. /* Skip CSNs not covered by local RUV snapshot */
  718. cscb->state = CLC_STATE_CSN_GT_RUV;
  719. buf->buf_skipped_csn_gt_ruv++;
  720. } while (0);
  721. #ifdef DEBUG
  722. if (skip && cscb) {
  723. char consumer[24] = {'\0'};
  724. char local[24] = {'\0'};
  725. char current[24] = {'\0'};
  726. if ( cscb->consumer_maxcsn )
  727. csn_as_string ( cscb->consumer_maxcsn, PR_FALSE, consumer );
  728. if ( cscb->local_maxcsn )
  729. csn_as_string ( cscb->local_maxcsn, PR_FALSE, local );
  730. csn_as_string ( buf->buf_current_csn, PR_FALSE, current );
  731. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  732. "Skip %s consumer=%s local=%s\n", current, consumer, local );
  733. }
  734. #endif
  735. return skip;
  736. }
  737. static struct csn_seq_ctrl_block *
  738. clcache_new_cscb ()
  739. {
  740. struct csn_seq_ctrl_block *cscb;
  741. cscb = (struct csn_seq_ctrl_block *) slapi_ch_calloc ( 1, sizeof (struct csn_seq_ctrl_block) );
  742. if (cscb == NULL) {
  743. slapi_log_error ( SLAPI_LOG_FATAL, NULL, "clcache: malloc failure\n" );
  744. }
  745. return cscb;
  746. }
  747. static void
  748. clcache_free_cscb ( struct csn_seq_ctrl_block ** cscb )
  749. {
  750. csn_free ( & (*cscb)->consumer_maxcsn );
  751. csn_free ( & (*cscb)->local_maxcsn );
  752. csn_free ( & (*cscb)->prev_local_maxcsn );
  753. slapi_ch_free ( (void **) cscb );
  754. }
  755. /*
  756. * Allocate and initialize a new buffer
  757. * It is called when there is a request for a buffer while
  758. * buffer free list is empty.
  759. */
  760. static CLC_Buffer *
  761. clcache_new_buffer ( ReplicaId consumer_rid )
  762. {
  763. CLC_Buffer *buf = NULL;
  764. int welldone = 0;
  765. do {
  766. buf = (CLC_Buffer*) slapi_ch_calloc (1, sizeof(CLC_Buffer));
  767. if ( NULL == buf )
  768. break;
  769. buf->buf_key.flags = DB_DBT_USERMEM;
  770. buf->buf_key.ulen = CSN_STRSIZE + 1;
  771. buf->buf_key.size = CSN_STRSIZE;
  772. buf->buf_key.data = slapi_ch_calloc( 1, buf->buf_key.ulen );
  773. if ( NULL == buf->buf_key.data )
  774. break;
  775. buf->buf_data.flags = DB_DBT_USERMEM;
  776. buf->buf_data.ulen = _pool->pl_buffer_default_pages * DEFAULT_CLC_BUFFER_PAGE_SIZE;
  777. buf->buf_data.data = slapi_ch_malloc( buf->buf_data.ulen );
  778. if ( NULL == buf->buf_data.data )
  779. break;
  780. if ( NULL == ( buf->buf_current_csn = csn_new()) )
  781. break;
  782. buf->buf_state = CLC_STATE_READY;
  783. buf->buf_agmt_name = get_thread_private_agmtname();
  784. buf->buf_consumer_rid = consumer_rid;
  785. buf->buf_num_cscbs = 0;
  786. buf->buf_max_cscbs = MAX_NUM_OF_MASTERS;
  787. buf->buf_cscbs = (struct csn_seq_ctrl_block **) slapi_ch_calloc(MAX_NUM_OF_MASTERS + 1,
  788. sizeof(struct csn_seq_ctrl_block *));
  789. welldone = 1;
  790. } while (0);
  791. if ( !welldone ) {
  792. clcache_delete_buffer ( &buf );
  793. }
  794. return buf;
  795. }
  796. /*
  797. * Deallocates a buffer.
  798. * It is called when a buffer is returned to the buffer pool
  799. * and the pool size is over the limit.
  800. */
  801. static void
  802. clcache_delete_buffer ( CLC_Buffer **buf )
  803. {
  804. if ( buf && *buf ) {
  805. slapi_ch_free (&( (*buf)->buf_key.data ));
  806. slapi_ch_free (&( (*buf)->buf_data.data ));
  807. csn_free (&( (*buf)->buf_current_csn ));
  808. csn_free (&( (*buf)->buf_missing_csn ));
  809. csn_free (&( (*buf)->buf_prev_missing_csn ));
  810. slapi_ch_free ( (void **) buf );
  811. }
  812. }
  813. static CLC_Busy_List *
  814. clcache_new_busy_list ()
  815. {
  816. CLC_Busy_List *bl;
  817. int welldone = 0;
  818. do {
  819. if ( NULL == (bl = ( CLC_Busy_List* ) slapi_ch_calloc (1, sizeof(CLC_Busy_List)) ))
  820. break;
  821. if ( NULL == (bl->bl_lock = PR_NewLock ()) )
  822. break;
  823. /*
  824. if ( NULL == (bl->bl_max_csn = csn_new ()) )
  825. break;
  826. */
  827. welldone = 1;
  828. }
  829. while (0);
  830. if ( !welldone ) {
  831. clcache_delete_busy_list ( &bl );
  832. }
  833. return bl;
  834. }
  835. static void
  836. clcache_delete_busy_list ( CLC_Busy_List **bl )
  837. {
  838. if ( bl && *bl ) {
  839. CLC_Buffer *buf = NULL;
  840. if ( (*bl)->bl_lock ) {
  841. PR_Lock ( (*bl)->bl_lock );
  842. }
  843. buf = (*bl)->bl_buffers;
  844. while (buf) {
  845. CLC_Buffer *next = buf->buf_next;
  846. clcache_delete_buffer(&buf);
  847. buf = next;
  848. }
  849. (*bl)->bl_buffers = NULL;
  850. (*bl)->bl_db = NULL;
  851. if ( (*bl)->bl_lock ) {
  852. PR_Unlock ( (*bl)->bl_lock );
  853. PR_DestroyLock ( (*bl)->bl_lock );
  854. (*bl)->bl_lock = NULL;
  855. }
  856. /* csn_free (&( (*bl)->bl_max_csn )); */
  857. slapi_ch_free ( (void **) bl );
  858. }
  859. }
  860. static int
  861. clcache_enqueue_busy_list ( DB *db, CLC_Buffer *buf )
  862. {
  863. CLC_Busy_List *bl;
  864. int rc = 0;
  865. slapi_rwlock_rdlock ( _pool->pl_lock );
  866. for ( bl = _pool->pl_busy_lists; bl && bl->bl_db != db; bl = bl->bl_next );
  867. slapi_rwlock_unlock ( _pool->pl_lock );
  868. if ( NULL == bl ) {
  869. if ( NULL == ( bl = clcache_new_busy_list ()) ) {
  870. rc = CL5_MEMORY_ERROR;
  871. }
  872. else {
  873. slapi_rwlock_wrlock ( _pool->pl_lock );
  874. bl->bl_db = db;
  875. bl->bl_next = _pool->pl_busy_lists;
  876. _pool->pl_busy_lists = bl;
  877. slapi_rwlock_unlock ( _pool->pl_lock );
  878. }
  879. }
  880. if ( NULL != bl ) {
  881. PR_Lock ( bl->bl_lock );
  882. buf->buf_busy_list = bl;
  883. buf->buf_next = bl->bl_buffers;
  884. bl->bl_buffers = buf;
  885. PR_Unlock ( bl->bl_lock );
  886. }
  887. return rc;
  888. }
  889. static int
  890. clcache_open_cursor ( DB_TXN *txn, CLC_Buffer *buf, DBC **cursor )
  891. {
  892. int rc;
  893. rc = buf->buf_busy_list->bl_db->cursor ( buf->buf_busy_list->bl_db, txn, cursor, 0 );
  894. if ( rc != 0 ) {
  895. slapi_log_error ( SLAPI_LOG_FATAL, get_thread_private_agmtname(),
  896. "clcache: failed to open cursor; db error - %d %s\n",
  897. rc, db_strerror(rc));
  898. }
  899. return rc;
  900. }
  901. static int
  902. clcache_cursor_get ( DBC *cursor, CLC_Buffer *buf, int flag )
  903. {
  904. int rc;
  905. rc = cursor->c_get ( cursor,
  906. & buf->buf_key,
  907. & buf->buf_data,
  908. buf->buf_load_flag | flag );
  909. if ( DB_BUFFER_SMALL == rc ) {
  910. /*
  911. * The record takes more space than the current size of the
  912. * buffer. Fortunately, buf->buf_data.size has been set by
  913. * c_get() to the actual data size needed. So we can
  914. * reallocate the data buffer and try to read again.
  915. */
  916. buf->buf_data.ulen = ( buf->buf_data.size / DEFAULT_CLC_BUFFER_PAGE_SIZE + 1 ) * DEFAULT_CLC_BUFFER_PAGE_SIZE;
  917. buf->buf_data.data = slapi_ch_realloc ( buf->buf_data.data, buf->buf_data.ulen );
  918. if ( buf->buf_data.data != NULL ) {
  919. rc = cursor->c_get ( cursor,
  920. &( buf->buf_key ),
  921. &( buf->buf_data ),
  922. buf->buf_load_flag | flag );
  923. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  924. "clcache: (%d | %d) buf key len %d reallocated and retry returns %d\n", buf->buf_load_flag, flag, buf->buf_key.size, rc );
  925. }
  926. }
  927. switch ( rc ) {
  928. case EINVAL:
  929. slapi_log_error ( SLAPI_LOG_FATAL, buf->buf_agmt_name,
  930. "clcache_cursor_get: invalid parameter\n" );
  931. break;
  932. case DB_BUFFER_SMALL:
  933. slapi_log_error ( SLAPI_LOG_FATAL, buf->buf_agmt_name,
  934. "clcache_cursor_get: can't allocate %u bytes\n", buf->buf_data.ulen );
  935. break;
  936. default:
  937. break;
  938. }
  939. return rc;
  940. }
  941. static void
  942. csn_dup_or_init_by_csn ( CSN **csn1, CSN *csn2 )
  943. {
  944. if ( *csn1 == NULL )
  945. *csn1 = csn_new();
  946. csn_init_by_csn ( *csn1, csn2 );
  947. }
  948. void
  949. clcache_destroy()
  950. {
  951. if (_pool) {
  952. CLC_Busy_List *bl = NULL;
  953. if (_pool->pl_lock) {
  954. slapi_rwlock_wrlock (_pool->pl_lock);
  955. }
  956. bl = _pool->pl_busy_lists;
  957. while (bl) {
  958. CLC_Busy_List *next = bl->bl_next;
  959. clcache_delete_busy_list(&bl);
  960. bl = next;
  961. }
  962. _pool->pl_busy_lists = NULL;
  963. _pool->pl_dbenv = NULL;
  964. if (_pool->pl_lock) {
  965. slapi_rwlock_unlock(_pool->pl_lock);
  966. slapi_destroy_rwlock(_pool->pl_lock);
  967. _pool->pl_lock = NULL;
  968. }
  969. slapi_ch_free ( (void **) &_pool );
  970. }
  971. }