cl5_clcache.c 33 KB

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