cl5_clcache.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  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(void);
  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(void);
  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. int use_flag = flag;
  337. #if 0 /* txn control seems not improving anything so turn it off */
  338. if ( *(_pool->pl_dbenv) ) {
  339. txn_begin( *(_pool->pl_dbenv), NULL, &txn, 0 );
  340. }
  341. #endif
  342. if (NULL == buf) {
  343. slapi_log_error ( SLAPI_LOG_FATAL, "clcache_load_buffer_bulk",
  344. "NULL buf\n" );
  345. return rc;
  346. }
  347. if (NULL == buf->buf_busy_list) {
  348. slapi_log_error ( SLAPI_LOG_FATAL, "clcache_load_buffer_bulk",
  349. "%s%sno buf_busy_list\n",
  350. buf->buf_agmt_name?buf->buf_agmt_name:"",
  351. buf->buf_agmt_name?": ":"" );
  352. return rc;
  353. }
  354. PR_Lock ( buf->buf_busy_list->bl_lock );
  355. retry:
  356. if ( 0 == ( rc = clcache_open_cursor ( txn, buf, &cursor )) ) {
  357. if ( use_flag == DB_NEXT ) {
  358. /* For bulk read, position the cursor before read the next block */
  359. rc = cursor->c_get ( cursor,
  360. & buf->buf_key,
  361. & buf->buf_data,
  362. DB_SET );
  363. if (rc == DB_NOTFOUND) {
  364. /* the start position in the changelog is not found
  365. * 1. log an error
  366. * 2. try to find another starting position as close
  367. * as possible
  368. */
  369. slapi_log_error ( SLAPI_LOG_FATAL, "clcache_load_buffer_bulk",
  370. "changelog record with csn (%s) not found for DB_NEXT\n",
  371. (char *)buf->buf_key.data );
  372. rc = cursor->c_get ( cursor, & buf->buf_key, & buf->buf_data,
  373. DB_SET_RANGE );
  374. /* this moves the cursor ahead of the tageted csn,
  375. * so we achieved what was intended with DB_SET/DB_NEXT
  376. * continute at this csn.
  377. */
  378. use_flag = DB_CURRENT;
  379. }
  380. }
  381. /*
  382. * Continue if the error is no-mem since we don't need to
  383. * load in the key record anyway with DB_SET.
  384. */
  385. if ( 0 == rc || DB_BUFFER_SMALL == rc ) {
  386. rc = clcache_cursor_get ( cursor, buf, use_flag );
  387. if ( rc == DB_NOTFOUND && use_flag == DB_SET) {
  388. slapi_log_error ( SLAPI_LOG_FATAL, "clcache_load_buffer_bulk",
  389. "changelog record with csn (%s) not found for DB_SET\n",
  390. (char *)buf->buf_key.data );
  391. rc = clcache_cursor_get ( cursor, buf, DB_SET_RANGE );
  392. }
  393. }
  394. }
  395. /*
  396. * Don't keep a cursor open across the whole replication session.
  397. * That had caused noticeable DB resource contention.
  398. */
  399. if ( cursor ) {
  400. cursor->c_close ( cursor );
  401. cursor = NULL;
  402. }
  403. if ((rc == DB_LOCK_DEADLOCK) && (tries < MAX_TRIALS)) {
  404. PRIntervalTime interval;
  405. tries++;
  406. slapi_log_error ( SLAPI_LOG_TRACE, "clcache_load_buffer_bulk",
  407. "deadlock number [%d] - retrying\n", tries );
  408. /* back off */
  409. interval = PR_MillisecondsToInterval(slapi_rand() % 100);
  410. DS_Sleep(interval);
  411. use_flag = flag;
  412. goto retry;
  413. }
  414. if ((rc == DB_LOCK_DEADLOCK) && (tries >= MAX_TRIALS)) {
  415. slapi_log_error ( SLAPI_LOG_REPL, "clcache_load_buffer_bulk",
  416. "could not load buffer from changelog after %d tries\n", tries );
  417. }
  418. #if 0 /* txn control seems not improving anything so turn it off */
  419. if ( txn ) {
  420. txn->commit ( txn, DB_TXN_NOSYNC );
  421. }
  422. #endif
  423. PR_Unlock ( buf->buf_busy_list->bl_lock );
  424. buf->buf_record_ptr = NULL;
  425. if ( 0 == rc ) {
  426. DB_MULTIPLE_INIT ( buf->buf_record_ptr, &buf->buf_data );
  427. if ( NULL == buf->buf_record_ptr )
  428. rc = DB_NOTFOUND;
  429. else
  430. buf->buf_load_cnt++;
  431. }
  432. return rc;
  433. }
  434. /*
  435. * Gets the next change from the buffer.
  436. * *key : output - key of the next change, or NULL if no more change
  437. * *data: output - data of the next change, or NULL if no more change
  438. */
  439. int
  440. clcache_get_next_change ( CLC_Buffer *buf, void **key, size_t *keylen, void **data, size_t *datalen, CSN **csn )
  441. {
  442. int skip = 1;
  443. int rc = 0;
  444. do {
  445. *key = *data = NULL;
  446. *keylen = *datalen = 0;
  447. if ( buf->buf_record_ptr ) {
  448. DB_MULTIPLE_KEY_NEXT ( buf->buf_record_ptr, &buf->buf_data,
  449. *key, *keylen, *data, *datalen );
  450. }
  451. /*
  452. * We're done with the current buffer. Now load the next chunk.
  453. */
  454. if ( NULL == *key && CLC_STATE_READY == buf->buf_state ) {
  455. rc = clcache_load_buffer ( buf, NULL );
  456. if ( 0 == rc && buf->buf_record_ptr ) {
  457. DB_MULTIPLE_KEY_NEXT ( buf->buf_record_ptr, &buf->buf_data,
  458. *key, *keylen, *data, *datalen );
  459. }
  460. }
  461. /* Compare the new change to the local and remote RUVs */
  462. if ( NULL != *key ) {
  463. buf->buf_record_cnt++;
  464. csn_init_by_string ( buf->buf_current_csn, (char*)*key );
  465. skip = clcache_skip_change ( buf );
  466. if (skip) buf->buf_record_skipped++;
  467. }
  468. }
  469. while ( rc == 0 && *key && skip );
  470. if ( NULL == *key ) {
  471. *key = NULL;
  472. *csn = NULL;
  473. rc = DB_NOTFOUND;
  474. }
  475. else {
  476. *csn = buf->buf_current_csn;
  477. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  478. "load=%d rec=%d csn=%s\n",
  479. buf->buf_load_cnt, buf->buf_record_cnt, (char*)*key );
  480. }
  481. return rc;
  482. }
  483. static void
  484. clcache_refresh_consumer_maxcsns ( CLC_Buffer *buf )
  485. {
  486. int i;
  487. for ( i = 0; i < buf->buf_num_cscbs; i++ ) {
  488. ruv_get_largest_csn_for_replica (
  489. buf->buf_consumer_ruv,
  490. buf->buf_cscbs[i]->rid,
  491. &buf->buf_cscbs[i]->consumer_maxcsn );
  492. }
  493. }
  494. static int
  495. clcache_refresh_local_maxcsn ( const ruv_enum_data *rid_data, void *data )
  496. {
  497. struct clc_buffer *buf = (struct clc_buffer*) data;
  498. ReplicaId rid;
  499. int rc = 0;
  500. int i;
  501. rid = csn_get_replicaid ( rid_data->csn );
  502. /* we do not handle updates originated at the consumer if not required
  503. * and we ignore RID which have been cleaned
  504. */
  505. if ( (rid == buf->buf_consumer_rid && buf->buf_ignoreConsumerRID) ||
  506. is_cleaned_rid(rid) )
  507. return rc;
  508. for ( i = 0; i < buf->buf_num_cscbs; i++ ) {
  509. if ( buf->buf_cscbs[i]->rid == rid )
  510. break;
  511. }
  512. if ( i >= buf->buf_num_cscbs ) {
  513. if( i + 1 > buf->buf_max_cscbs){
  514. buf->buf_cscbs = (struct csn_seq_ctrl_block **) slapi_ch_realloc((char *)buf->buf_cscbs,
  515. (i + 2) * sizeof(struct csn_seq_ctrl_block *));
  516. buf->buf_max_cscbs = i + 1;
  517. }
  518. buf->buf_cscbs[i] = clcache_new_cscb();
  519. if ( buf->buf_cscbs[i] == NULL ) {
  520. return -1;
  521. }
  522. buf->buf_cscbs[i]->rid = rid;
  523. buf->buf_num_cscbs++;
  524. /* this is the first time we have a local change for the RID
  525. * we need to check what the consumer knows about it.
  526. */
  527. ruv_get_largest_csn_for_replica (
  528. buf->buf_consumer_ruv,
  529. buf->buf_cscbs[i]->rid,
  530. &buf->buf_cscbs[i]->consumer_maxcsn );
  531. }
  532. if (buf->buf_cscbs[i]->local_maxcsn)
  533. csn_dup_or_init_by_csn ( &buf->buf_cscbs[i]->prev_local_maxcsn, buf->buf_cscbs[i]->local_maxcsn );
  534. csn_dup_or_init_by_csn ( &buf->buf_cscbs[i]->local_maxcsn, rid_data->csn );
  535. csn_dup_or_init_by_csn ( &buf->buf_cscbs[i]->local_mincsn, rid_data->min_csn );
  536. if ( buf->buf_cscbs[i]->consumer_maxcsn &&
  537. csn_compare (buf->buf_cscbs[i]->consumer_maxcsn, rid_data->csn) >= 0 ) {
  538. /* No change need to be sent for this RID */
  539. buf->buf_cscbs[i]->state = CLC_STATE_UP_TO_DATE;
  540. }
  541. return rc;
  542. }
  543. static int
  544. clcache_refresh_local_maxcsns ( CLC_Buffer *buf )
  545. {
  546. return ruv_enumerate_elements ( buf->buf_local_ruv, clcache_refresh_local_maxcsn, buf );
  547. }
  548. /*
  549. * Algorithm:
  550. *
  551. * 1. Determine anchorcsn for each RID:
  552. * 2. Determine anchorcsn for next load:
  553. * Anchor-CSN = min { all Next-Anchor-CSN, Buffer-MaxCSN }
  554. */
  555. static int
  556. clcache_initial_anchorcsn ( CLC_Buffer *buf, int *flag )
  557. {
  558. PRBool hasChange = PR_FALSE;
  559. struct csn_seq_ctrl_block *cscb;
  560. int i;
  561. CSN *anchorcsn = NULL;
  562. if ( buf->buf_state == CLC_STATE_READY ) {
  563. for ( i = 0; i < buf->buf_num_cscbs; i++ ) {
  564. CSN *rid_anchor = NULL;
  565. int rid_flag = DB_NEXT;
  566. cscb = buf->buf_cscbs[i];
  567. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  568. char prevmax[CSN_STRSIZE];
  569. char local[CSN_STRSIZE];
  570. char curr[CSN_STRSIZE];
  571. char conmaxcsn[CSN_STRSIZE];
  572. csn_as_string(cscb->prev_local_maxcsn, 0, prevmax);
  573. csn_as_string(cscb->local_maxcsn, 0, local);
  574. csn_as_string(buf->buf_current_csn, 0, curr);
  575. csn_as_string(cscb->consumer_maxcsn, 0, conmaxcsn);
  576. slapi_log_error(SLAPI_LOG_REPL, "clcache_initial_anchorcsn" ,
  577. "%s - (cscb %d - state %d) - csnPrevMax (%s) "
  578. "csnMax (%s) csnBuf (%s) csnConsumerMax (%s)\n",
  579. buf->buf_agmt_name, i, cscb->state, prevmax, local,
  580. curr, conmaxcsn);
  581. }
  582. if (cscb->consumer_maxcsn == NULL) {
  583. /* the consumer hasn't seen changes for this RID */
  584. rid_anchor = cscb->local_mincsn;
  585. rid_flag = DB_SET;
  586. } else if ( csn_compare (cscb->local_maxcsn, cscb->consumer_maxcsn) > 0 ) {
  587. rid_anchor = cscb->consumer_maxcsn;
  588. }
  589. if (rid_anchor && (anchorcsn == NULL ||
  590. ( csn_compare(rid_anchor, anchorcsn) < 0))) {
  591. anchorcsn = rid_anchor;
  592. *flag = rid_flag;
  593. hasChange = PR_TRUE;
  594. }
  595. }
  596. }
  597. if ( !hasChange ) {
  598. buf->buf_state = CLC_STATE_DONE;
  599. } else {
  600. csn_init_by_csn(buf->buf_current_csn, anchorcsn);
  601. csn_as_string(buf->buf_current_csn, 0, (char *)buf->buf_key.data);
  602. slapi_log_error(SLAPI_LOG_REPL, "clcache_initial_anchorcsn",
  603. "anchor is now: %s\n", (char *)buf->buf_key.data);
  604. }
  605. return buf->buf_state;
  606. }
  607. static int
  608. clcache_adjust_anchorcsn ( CLC_Buffer *buf, int *flag )
  609. {
  610. PRBool hasChange = PR_FALSE;
  611. struct csn_seq_ctrl_block *cscb;
  612. int i;
  613. CSN *anchorcsn = NULL;
  614. if ( buf->buf_state == CLC_STATE_READY ) {
  615. for ( i = 0; i < buf->buf_num_cscbs; i++ ) {
  616. CSN *rid_anchor = NULL;
  617. int rid_flag = DB_NEXT;
  618. cscb = buf->buf_cscbs[i];
  619. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  620. char prevmax[CSN_STRSIZE];
  621. char local[CSN_STRSIZE];
  622. char curr[CSN_STRSIZE];
  623. char conmaxcsn[CSN_STRSIZE];
  624. csn_as_string(cscb->prev_local_maxcsn, 0, prevmax);
  625. csn_as_string(cscb->local_maxcsn, 0, local);
  626. csn_as_string(buf->buf_current_csn, 0, curr);
  627. csn_as_string(cscb->consumer_maxcsn, 0, conmaxcsn);
  628. slapi_log_error(SLAPI_LOG_REPL, "clcache_adjust_anchorcsn" ,
  629. "%s - (cscb %d - state %d) - csnPrevMax (%s) "
  630. "csnMax (%s) csnBuf (%s) csnConsumerMax (%s)\n",
  631. buf->buf_agmt_name, i, cscb->state, prevmax, local,
  632. curr, conmaxcsn);
  633. }
  634. if (csn_compare (cscb->local_maxcsn, cscb->prev_local_maxcsn) == 0 ||
  635. csn_compare (cscb->prev_local_maxcsn, buf->buf_current_csn) > 0 ) {
  636. if (csn_compare (cscb->local_maxcsn, cscb->consumer_maxcsn) > 0 ) {
  637. rid_anchor = buf->buf_current_csn;
  638. }
  639. } else {
  640. /* prev local max csn < csnBuffer AND different from local maxcsn */
  641. if (cscb->prev_local_maxcsn == NULL) {
  642. if (cscb->consumer_maxcsn == NULL) {
  643. /* the consumer hasn't seen changes for this RID */
  644. rid_anchor = cscb->local_mincsn;
  645. rid_flag = DB_SET;
  646. } else if ( csn_compare (cscb->local_maxcsn, cscb->consumer_maxcsn) > 0 ) {
  647. rid_anchor = cscb->consumer_maxcsn;
  648. }
  649. } else {
  650. /* csnPrevMaxSup > 0 */
  651. rid_anchor = cscb->consumer_maxcsn;
  652. }
  653. }
  654. if (rid_anchor && (anchorcsn == NULL ||
  655. ( csn_compare(rid_anchor, anchorcsn) < 0))) {
  656. anchorcsn = rid_anchor;
  657. *flag = rid_flag;
  658. hasChange = PR_TRUE;
  659. }
  660. }
  661. }
  662. if ( !hasChange ) {
  663. buf->buf_state = CLC_STATE_DONE;
  664. } else {
  665. csn_init_by_csn(buf->buf_current_csn, anchorcsn);
  666. csn_as_string(buf->buf_current_csn, 0, (char *)buf->buf_key.data);
  667. slapi_log_error(SLAPI_LOG_REPL, "clcache_adjust_anchorcsn",
  668. "anchor is now: %s\n", (char *)buf->buf_key.data);
  669. }
  670. return buf->buf_state;
  671. }
  672. static int
  673. clcache_skip_change ( CLC_Buffer *buf )
  674. {
  675. struct csn_seq_ctrl_block *cscb = NULL;
  676. ReplicaId rid;
  677. int skip = 1;
  678. int i;
  679. char buf_cur_csn_str[CSN_STRSIZE];
  680. do {
  681. rid = csn_get_replicaid ( buf->buf_current_csn );
  682. /*
  683. * Skip CSN that is originated from the consumer,
  684. * unless the CSN is newer than the maxcsn.
  685. * If RID==65535, the CSN is originated from a
  686. * legacy consumer. In this case the supplier
  687. * and the consumer may have the same RID.
  688. */
  689. if (rid == buf->buf_consumer_rid && buf->buf_ignoreConsumerRID){
  690. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  691. csn_as_string(buf->buf_current_csn, 0, buf_cur_csn_str);
  692. slapi_log_error(SLAPI_LOG_REPL, buf->buf_agmt_name,
  693. "Skipping update because the consumer with Rid: [%d] is "
  694. "ignored\n", rid);
  695. buf->buf_skipped_csn_gt_cons_maxcsn++;
  696. }
  697. break;
  698. }
  699. /* Skip helper entry (ENTRY_COUNT, PURGE_RUV and so on) */
  700. if ( cl5HelperEntry ( NULL, buf->buf_current_csn ) == PR_TRUE ) {
  701. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  702. "Skip helper entry type=%ld\n", csn_get_time( buf->buf_current_csn ));
  703. break;
  704. }
  705. /* Find csn sequence control block for the current rid */
  706. for (i = 0; i < buf->buf_num_cscbs && buf->buf_cscbs[i]->rid != rid; i++);
  707. /* Skip CSN whose RID is unknown to the local RUV snapshot */
  708. if ( i >= buf->buf_num_cscbs ) {
  709. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  710. csn_as_string(buf->buf_current_csn, 0, buf_cur_csn_str);
  711. slapi_log_error(SLAPI_LOG_REPL, buf->buf_agmt_name,
  712. "Skipping update because the changelog buffer current csn [%s] rid "
  713. "[%d] is not in the list of changelog csn buffers (length %d)\n",
  714. buf_cur_csn_str, rid, buf->buf_num_cscbs);
  715. }
  716. buf->buf_skipped_new_rid++;
  717. break;
  718. }
  719. cscb = buf->buf_cscbs[i];
  720. /* Skip if the consumer is already up-to-date for the RID */
  721. if ( cscb->state == CLC_STATE_UP_TO_DATE ) {
  722. buf->buf_skipped_up_to_date++;
  723. break;
  724. }
  725. /* Skip CSN whose preceedents are not covered by local RUV snapshot */
  726. if ( cscb->state == CLC_STATE_CSN_GT_RUV ) {
  727. buf->buf_skipped_csn_gt_ruv++;
  728. break;
  729. }
  730. /* Skip CSNs already covered by consumer RUV */
  731. if ( cscb->consumer_maxcsn &&
  732. csn_compare ( buf->buf_current_csn, cscb->consumer_maxcsn ) <= 0 ) {
  733. buf->buf_skipped_csn_covered++;
  734. break;
  735. }
  736. /* Send CSNs that are covered by the local RUV snapshot */
  737. if ( csn_compare ( buf->buf_current_csn, cscb->local_maxcsn ) <= 0 ) {
  738. skip = 0;
  739. csn_dup_or_init_by_csn ( &cscb->consumer_maxcsn, buf->buf_current_csn );
  740. break;
  741. }
  742. /*
  743. * Promote the local maxcsn to its next neighbor
  744. * to keep the current session going. Skip if we
  745. * are not sure if current_csn is the neighbor.
  746. */
  747. if ( csn_time_difference(buf->buf_current_csn, cscb->local_maxcsn) == 0 &&
  748. (csn_get_seqnum(buf->buf_current_csn) ==
  749. csn_get_seqnum(cscb->local_maxcsn) + 1) )
  750. {
  751. csn_init_by_csn ( cscb->local_maxcsn, buf->buf_current_csn );
  752. if(cscb->consumer_maxcsn){
  753. csn_init_by_csn ( cscb->consumer_maxcsn, buf->buf_current_csn );
  754. }
  755. skip = 0;
  756. break;
  757. }
  758. /* Skip CSNs not covered by local RUV snapshot */
  759. cscb->state = CLC_STATE_CSN_GT_RUV;
  760. buf->buf_skipped_csn_gt_ruv++;
  761. } while (0);
  762. #ifdef DEBUG
  763. if (skip && cscb) {
  764. char consumer[24] = {'\0'};
  765. char local[24] = {'\0'};
  766. char current[24] = {'\0'};
  767. if ( cscb->consumer_maxcsn )
  768. csn_as_string ( cscb->consumer_maxcsn, PR_FALSE, consumer );
  769. if ( cscb->local_maxcsn )
  770. csn_as_string ( cscb->local_maxcsn, PR_FALSE, local );
  771. csn_as_string ( buf->buf_current_csn, PR_FALSE, current );
  772. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  773. "Skip %s consumer=%s local=%s\n", current, consumer, local );
  774. }
  775. #endif
  776. return skip;
  777. }
  778. static struct csn_seq_ctrl_block *
  779. clcache_new_cscb(void)
  780. {
  781. struct csn_seq_ctrl_block *cscb;
  782. cscb = (struct csn_seq_ctrl_block *) slapi_ch_calloc ( 1, sizeof (struct csn_seq_ctrl_block) );
  783. if (cscb == NULL) {
  784. slapi_log_error ( SLAPI_LOG_FATAL, NULL, "clcache: malloc failure\n" );
  785. }
  786. return cscb;
  787. }
  788. static void
  789. clcache_free_cscb ( struct csn_seq_ctrl_block ** cscb )
  790. {
  791. csn_free ( & (*cscb)->consumer_maxcsn );
  792. csn_free ( & (*cscb)->local_maxcsn );
  793. csn_free ( & (*cscb)->prev_local_maxcsn );
  794. csn_free ( & (*cscb)->local_mincsn );
  795. slapi_ch_free ( (void **) cscb );
  796. }
  797. /*
  798. * Allocate and initialize a new buffer
  799. * It is called when there is a request for a buffer while
  800. * buffer free list is empty.
  801. */
  802. static CLC_Buffer *
  803. clcache_new_buffer ( ReplicaId consumer_rid )
  804. {
  805. CLC_Buffer *buf = NULL;
  806. int welldone = 0;
  807. do {
  808. buf = (CLC_Buffer*) slapi_ch_calloc (1, sizeof(CLC_Buffer));
  809. if ( NULL == buf )
  810. break;
  811. buf->buf_key.flags = DB_DBT_USERMEM;
  812. buf->buf_key.ulen = CSN_STRSIZE + 1;
  813. buf->buf_key.size = CSN_STRSIZE;
  814. buf->buf_key.data = slapi_ch_calloc( 1, buf->buf_key.ulen );
  815. if ( NULL == buf->buf_key.data )
  816. break;
  817. buf->buf_data.flags = DB_DBT_USERMEM;
  818. buf->buf_data.ulen = _pool->pl_buffer_default_pages * DEFAULT_CLC_BUFFER_PAGE_SIZE;
  819. buf->buf_data.data = slapi_ch_malloc( buf->buf_data.ulen );
  820. if ( NULL == buf->buf_data.data )
  821. break;
  822. if ( NULL == ( buf->buf_current_csn = csn_new()) )
  823. break;
  824. buf->buf_state = CLC_STATE_READY;
  825. buf->buf_agmt_name = get_thread_private_agmtname();
  826. buf->buf_consumer_rid = consumer_rid;
  827. buf->buf_num_cscbs = 0;
  828. buf->buf_max_cscbs = MAX_NUM_OF_MASTERS;
  829. buf->buf_cscbs = (struct csn_seq_ctrl_block **) slapi_ch_calloc(MAX_NUM_OF_MASTERS + 1,
  830. sizeof(struct csn_seq_ctrl_block *));
  831. welldone = 1;
  832. } while (0);
  833. if ( !welldone ) {
  834. clcache_delete_buffer ( &buf );
  835. }
  836. return buf;
  837. }
  838. /*
  839. * Deallocates a buffer.
  840. * It is called when a buffer is returned to the buffer pool
  841. * and the pool size is over the limit.
  842. */
  843. static void
  844. clcache_delete_buffer ( CLC_Buffer **buf )
  845. {
  846. if ( buf && *buf ) {
  847. slapi_ch_free (&( (*buf)->buf_key.data ));
  848. slapi_ch_free (&( (*buf)->buf_data.data ));
  849. csn_free (&( (*buf)->buf_current_csn ));
  850. csn_free (&( (*buf)->buf_missing_csn ));
  851. csn_free (&( (*buf)->buf_prev_missing_csn ));
  852. slapi_ch_free ( (void **) buf );
  853. }
  854. }
  855. static CLC_Busy_List *
  856. clcache_new_busy_list(void)
  857. {
  858. CLC_Busy_List *bl;
  859. int welldone = 0;
  860. do {
  861. if ( NULL == (bl = ( CLC_Busy_List* ) slapi_ch_calloc (1, sizeof(CLC_Busy_List)) ))
  862. break;
  863. if ( NULL == (bl->bl_lock = PR_NewLock ()) )
  864. break;
  865. /*
  866. if ( NULL == (bl->bl_max_csn = csn_new ()) )
  867. break;
  868. */
  869. welldone = 1;
  870. }
  871. while (0);
  872. if ( !welldone ) {
  873. clcache_delete_busy_list ( &bl );
  874. }
  875. return bl;
  876. }
  877. static void
  878. clcache_delete_busy_list ( CLC_Busy_List **bl )
  879. {
  880. if ( bl && *bl ) {
  881. CLC_Buffer *buf = NULL;
  882. if ( (*bl)->bl_lock ) {
  883. PR_Lock ( (*bl)->bl_lock );
  884. }
  885. buf = (*bl)->bl_buffers;
  886. while (buf) {
  887. CLC_Buffer *next = buf->buf_next;
  888. clcache_delete_buffer(&buf);
  889. buf = next;
  890. }
  891. (*bl)->bl_buffers = NULL;
  892. (*bl)->bl_db = NULL;
  893. if ( (*bl)->bl_lock ) {
  894. PR_Unlock ( (*bl)->bl_lock );
  895. PR_DestroyLock ( (*bl)->bl_lock );
  896. (*bl)->bl_lock = NULL;
  897. }
  898. /* csn_free (&( (*bl)->bl_max_csn )); */
  899. slapi_ch_free ( (void **) bl );
  900. }
  901. }
  902. static int
  903. clcache_enqueue_busy_list ( DB *db, CLC_Buffer *buf )
  904. {
  905. CLC_Busy_List *bl;
  906. int rc = 0;
  907. slapi_rwlock_rdlock ( _pool->pl_lock );
  908. for ( bl = _pool->pl_busy_lists; bl && bl->bl_db != db; bl = bl->bl_next );
  909. slapi_rwlock_unlock ( _pool->pl_lock );
  910. if ( NULL == bl ) {
  911. if ( NULL == ( bl = clcache_new_busy_list ()) ) {
  912. rc = CL5_MEMORY_ERROR;
  913. }
  914. else {
  915. slapi_rwlock_wrlock ( _pool->pl_lock );
  916. bl->bl_db = db;
  917. bl->bl_next = _pool->pl_busy_lists;
  918. _pool->pl_busy_lists = bl;
  919. slapi_rwlock_unlock ( _pool->pl_lock );
  920. }
  921. }
  922. if ( NULL != bl ) {
  923. PR_Lock ( bl->bl_lock );
  924. buf->buf_busy_list = bl;
  925. buf->buf_next = bl->bl_buffers;
  926. bl->bl_buffers = buf;
  927. PR_Unlock ( bl->bl_lock );
  928. }
  929. return rc;
  930. }
  931. static int
  932. clcache_open_cursor ( DB_TXN *txn, CLC_Buffer *buf, DBC **cursor )
  933. {
  934. int rc;
  935. rc = buf->buf_busy_list->bl_db->cursor ( buf->buf_busy_list->bl_db, txn, cursor, 0 );
  936. if ( rc != 0 ) {
  937. slapi_log_error ( SLAPI_LOG_FATAL, get_thread_private_agmtname(),
  938. "clcache: failed to open cursor; db error - %d %s\n",
  939. rc, db_strerror(rc));
  940. }
  941. return rc;
  942. }
  943. static int
  944. clcache_cursor_get ( DBC *cursor, CLC_Buffer *buf, int flag )
  945. {
  946. int rc;
  947. if (buf->buf_data.ulen > WORK_CLC_BUFFER_PAGE_SIZE) {
  948. /*
  949. * The buffer size had to be increased,
  950. * reset it to a smaller working size,
  951. * if not sufficient it will be increased again
  952. */
  953. buf->buf_data.ulen = WORK_CLC_BUFFER_PAGE_SIZE;
  954. }
  955. rc = cursor->c_get ( cursor,
  956. & buf->buf_key,
  957. & buf->buf_data,
  958. buf->buf_load_flag | flag );
  959. if ( DB_BUFFER_SMALL == rc ) {
  960. /*
  961. * The record takes more space than the current size of the
  962. * buffer. Fortunately, buf->buf_data.size has been set by
  963. * c_get() to the actual data size needed. So we can
  964. * reallocate the data buffer and try to read again.
  965. */
  966. buf->buf_data.ulen = ( buf->buf_data.size / DEFAULT_CLC_BUFFER_PAGE_SIZE + 1 ) * DEFAULT_CLC_BUFFER_PAGE_SIZE;
  967. buf->buf_data.data = slapi_ch_realloc ( buf->buf_data.data, buf->buf_data.ulen );
  968. if ( buf->buf_data.data != NULL ) {
  969. rc = cursor->c_get ( cursor,
  970. &( buf->buf_key ),
  971. &( buf->buf_data ),
  972. buf->buf_load_flag | flag );
  973. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  974. "clcache: (%d | %d) buf key len %d reallocated and retry returns %d\n", buf->buf_load_flag, flag, buf->buf_key.size, rc );
  975. }
  976. }
  977. switch ( rc ) {
  978. case EINVAL:
  979. slapi_log_error ( SLAPI_LOG_FATAL, buf->buf_agmt_name,
  980. "clcache_cursor_get: invalid parameter\n" );
  981. break;
  982. case DB_BUFFER_SMALL:
  983. slapi_log_error ( SLAPI_LOG_FATAL, buf->buf_agmt_name,
  984. "clcache_cursor_get: can't allocate %u bytes\n", buf->buf_data.ulen );
  985. break;
  986. default:
  987. break;
  988. }
  989. return rc;
  990. }
  991. static void
  992. csn_dup_or_init_by_csn ( CSN **csn1, CSN *csn2 )
  993. {
  994. if ( *csn1 == NULL )
  995. *csn1 = csn_new();
  996. csn_init_by_csn ( *csn1, csn2 );
  997. }
  998. void
  999. clcache_destroy()
  1000. {
  1001. if (_pool) {
  1002. CLC_Busy_List *bl = NULL;
  1003. if (_pool->pl_lock) {
  1004. slapi_rwlock_wrlock (_pool->pl_lock);
  1005. }
  1006. bl = _pool->pl_busy_lists;
  1007. while (bl) {
  1008. CLC_Busy_List *next = bl->bl_next;
  1009. clcache_delete_busy_list(&bl);
  1010. bl = next;
  1011. }
  1012. _pool->pl_busy_lists = NULL;
  1013. _pool->pl_dbenv = NULL;
  1014. if (_pool->pl_lock) {
  1015. slapi_rwlock_unlock(_pool->pl_lock);
  1016. slapi_destroy_rwlock(_pool->pl_lock);
  1017. _pool->pl_lock = NULL;
  1018. }
  1019. slapi_ch_free ( (void **) &_pool );
  1020. }
  1021. }