cl5_clcache.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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. /* fields for control the CSN sequence sent to the consumer */
  105. struct csn_seq_ctrl_block *buf_cscbs [MAX_NUM_OF_MASTERS];
  106. int buf_num_cscbs; /* number of csn sequence ctrl blocks */
  107. /* fields for debugging stat */
  108. int buf_load_cnt; /* number of loads for session */
  109. int buf_record_cnt; /* number of changes for session */
  110. int buf_record_skipped; /* number of changes skipped */
  111. /*
  112. * fields that should be accessed via bl_lock or pl_lock
  113. */
  114. CLC_Buffer *buf_next; /* next buffer in the same list */
  115. CLC_Busy_List *buf_busy_list; /* which busy list I'm in */
  116. };
  117. /*
  118. * Each changelog has a busy buffer list
  119. */
  120. struct clc_busy_list {
  121. PRLock *bl_lock;
  122. DB *bl_db; /* changelog db handle */
  123. CLC_Buffer *bl_buffers; /* busy buffers of this list */
  124. CLC_Busy_List *bl_next; /* next busy list in the pool */
  125. };
  126. /*
  127. * Each process has a buffer pool
  128. */
  129. struct clc_pool {
  130. PRRWLock *pl_lock; /* cl writer and agreements */
  131. DB_ENV **pl_dbenv; /* pointer to DB_ENV for all the changelog files */
  132. CLC_Busy_List *pl_busy_lists; /* busy buffer lists, one list per changelog file */
  133. int pl_buffer_cnt_now; /* total number of buffers */
  134. int pl_buffer_cnt_min; /* free a newly returned buffer if _now > _min */
  135. int pl_buffer_cnt_max; /* no use */
  136. int pl_buffer_default_pages; /* num of pages in a new buffer */
  137. };
  138. /* static variables */
  139. static struct clc_pool *_pool = NULL; /* process's buffer pool */
  140. /* static prototypes */
  141. static int clcache_adjust_anchorcsn ( CLC_Buffer *buf );
  142. static void clcache_refresh_consumer_maxcsns ( CLC_Buffer *buf );
  143. static int clcache_refresh_local_maxcsns ( CLC_Buffer *buf );
  144. static int clcache_skip_change ( CLC_Buffer *buf );
  145. static int clcache_load_buffer_bulk ( CLC_Buffer *buf, int flag );
  146. static int clcache_open_cursor ( DB_TXN *txn, CLC_Buffer *buf, DBC **cursor );
  147. static int clcache_cursor_get ( DBC *cursor, CLC_Buffer *buf, int flag );
  148. static struct csn_seq_ctrl_block *clcache_new_cscb ();
  149. static void clcache_free_cscb ( struct csn_seq_ctrl_block ** cscb );
  150. static CLC_Buffer *clcache_new_buffer ( ReplicaId consumer_rid );
  151. static void clcache_delete_buffer ( CLC_Buffer **buf );
  152. static CLC_Busy_List *clcache_new_busy_list ();
  153. static void clcache_delete_busy_list ( CLC_Busy_List **bl );
  154. static int clcache_enqueue_busy_list( DB *db, CLC_Buffer *buf );
  155. static void csn_dup_or_init_by_csn ( CSN **csn1, CSN *csn2 );
  156. /*
  157. * Initiates the process buffer pool. This should be done
  158. * once and only once when process starts.
  159. */
  160. int
  161. clcache_init ( DB_ENV **dbenv )
  162. {
  163. if (_pool) {
  164. return 0; /* already initialized */
  165. }
  166. _pool = (struct clc_pool*) slapi_ch_calloc ( 1, sizeof ( struct clc_pool ));
  167. _pool->pl_dbenv = dbenv;
  168. _pool->pl_buffer_cnt_min = DEFAULT_CLC_BUFFER_COUNT_MIN;
  169. _pool->pl_buffer_cnt_max = DEFAULT_CLC_BUFFER_COUNT_MAX;
  170. _pool->pl_buffer_default_pages = DEFAULT_CLC_BUFFER_COUNT_MAX;
  171. _pool->pl_lock = PR_NewRWLock (PR_RWLOCK_RANK_NONE, "clcache_pl_lock");
  172. return 0;
  173. }
  174. /*
  175. * This is part of a callback function when changelog configuration
  176. * is read or updated.
  177. */
  178. void
  179. clcache_set_config ( CL5DBConfig *config )
  180. {
  181. if ( config == NULL ) return;
  182. PR_RWLock_Wlock ( _pool->pl_lock );
  183. _pool->pl_buffer_cnt_max = config->maxChCacheEntries;
  184. /*
  185. * According to http://www.sleepycat.com/docs/api_c/dbc_get.html,
  186. * data buffer should be a multiple of 1024 bytes in size
  187. * for DB_MULTIPLE_KEY operation.
  188. */
  189. _pool->pl_buffer_default_pages = config->maxChCacheSize / DEFAULT_CLC_BUFFER_PAGE_SIZE + 1;
  190. _pool->pl_buffer_default_pages = DEFAULT_CLC_BUFFER_PAGE_COUNT;
  191. if ( _pool->pl_buffer_default_pages <= 0 ) {
  192. _pool->pl_buffer_default_pages = DEFAULT_CLC_BUFFER_PAGE_COUNT;
  193. }
  194. PR_RWLock_Unlock ( _pool->pl_lock );
  195. }
  196. /*
  197. * Gets the pointer to a thread dedicated buffer, or allocates
  198. * a new buffer if there is no buffer allocated yet for this thread.
  199. *
  200. * This is called when a cl5replayiterator is created for
  201. * a replication session.
  202. */
  203. int
  204. clcache_get_buffer ( CLC_Buffer **buf, DB *db, ReplicaId consumer_rid, const RUV *consumer_ruv, const RUV *local_ruv )
  205. {
  206. int rc = 0;
  207. int need_new;
  208. if ( buf == NULL ) return CL5_BAD_DATA;
  209. *buf = NULL;
  210. /* if the pool was re-initialized, the thread private cache will be invalid,
  211. so we must get a new one */
  212. need_new = (!_pool || !_pool->pl_busy_lists || !_pool->pl_busy_lists->bl_buffers);
  213. if ( (!need_new) && (NULL != ( *buf = (CLC_Buffer*) get_thread_private_cache())) ) {
  214. slapi_log_error ( SLAPI_LOG_REPL, get_thread_private_agmtname(),
  215. "clcache_get_buffer: found thread private buffer cache %p\n", *buf);
  216. slapi_log_error ( SLAPI_LOG_REPL, get_thread_private_agmtname(),
  217. "clcache_get_buffer: _pool is %p _pool->pl_busy_lists is %p _pool->pl_busy_lists->bl_buffers is %p\n",
  218. _pool, _pool ? _pool->pl_busy_lists : NULL,
  219. (_pool && _pool->pl_busy_lists) ? _pool->pl_busy_lists->bl_buffers : NULL);
  220. (*buf)->buf_state = CLC_STATE_READY;
  221. (*buf)->buf_load_cnt = 0;
  222. (*buf)->buf_record_cnt = 0;
  223. (*buf)->buf_record_skipped = 0;
  224. (*buf)->buf_cursor = NULL;
  225. (*buf)->buf_num_cscbs = 0;
  226. }
  227. else {
  228. *buf = clcache_new_buffer ( consumer_rid );
  229. if ( *buf ) {
  230. if ( 0 == clcache_enqueue_busy_list ( db, *buf ) ) {
  231. set_thread_private_cache ( (void*) (*buf) );
  232. }
  233. else {
  234. clcache_delete_buffer ( buf );
  235. }
  236. }
  237. }
  238. if ( NULL != *buf ) {
  239. (*buf)->buf_consumer_ruv = consumer_ruv;
  240. (*buf)->buf_local_ruv = local_ruv;
  241. }
  242. else {
  243. slapi_log_error ( SLAPI_LOG_FATAL, get_thread_private_agmtname(),
  244. "clcache_get_buffer: can't allocate new buffer\n" );
  245. rc = CL5_MEMORY_ERROR;
  246. }
  247. return rc;
  248. }
  249. /*
  250. * Returns a buffer back to the buffer pool.
  251. */
  252. void
  253. clcache_return_buffer ( CLC_Buffer **buf )
  254. {
  255. int i;
  256. slapi_log_error ( SLAPI_LOG_REPL, (*buf)->buf_agmt_name,
  257. "session end: state=%d load=%d sent=%d skipped=%d\n",
  258. (*buf)->buf_state,
  259. (*buf)->buf_load_cnt,
  260. (*buf)->buf_record_cnt - (*buf)->buf_record_skipped,
  261. (*buf)->buf_record_skipped );
  262. for ( i = 0; i < (*buf)->buf_num_cscbs; i++ ) {
  263. clcache_free_cscb ( &(*buf)->buf_cscbs[i] );
  264. }
  265. (*buf)->buf_num_cscbs = 0;
  266. if ( (*buf)->buf_cursor ) {
  267. (*buf)->buf_cursor->c_close ( (*buf)->buf_cursor );
  268. (*buf)->buf_cursor = NULL;
  269. }
  270. }
  271. /*
  272. * Loads a buffer from DB.
  273. *
  274. * anchorcsn - passed in for the first load of a replication session;
  275. * flag - DB_SET to load in the key CSN record.
  276. * DB_NEXT to load in the records greater than key CSN.
  277. * return - DB error code instead of cl5 one because of the
  278. * historic reason.
  279. */
  280. int
  281. clcache_load_buffer ( CLC_Buffer *buf, CSN *anchorcsn, int flag )
  282. {
  283. int rc = 0;
  284. clcache_refresh_local_maxcsns ( buf );
  285. /* Set the loading key */
  286. if ( anchorcsn ) {
  287. clcache_refresh_consumer_maxcsns ( buf );
  288. buf->buf_load_flag = DB_MULTIPLE_KEY;
  289. csn_as_string ( anchorcsn, 0, (char*)buf->buf_key.data );
  290. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  291. "session start: anchorcsn=%s\n", (char*)buf->buf_key.data );
  292. }
  293. else if ( csn_get_time(buf->buf_current_csn) == 0 ) {
  294. /* time == 0 means this csn has never been set */
  295. rc = DB_NOTFOUND;
  296. }
  297. else if ( clcache_adjust_anchorcsn ( buf ) != 0 ) {
  298. rc = DB_NOTFOUND;
  299. }
  300. else {
  301. csn_as_string ( buf->buf_current_csn, 0, (char*)buf->buf_key.data );
  302. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  303. "load next: anchorcsn=%s\n", (char*)buf->buf_key.data );
  304. }
  305. if ( rc == 0 ) {
  306. buf->buf_state = CLC_STATE_READY;
  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 if ( anchorcsn ) {
  316. /* Report error only when the missing is persistent */
  317. if ( buf->buf_missing_csn && csn_compare (buf->buf_missing_csn, anchorcsn) == 0 ) {
  318. slapi_log_error ( SLAPI_LOG_FATAL, buf->buf_agmt_name,
  319. "Can't locate CSN %s in the changelog (DB rc=%d). The consumer may need to be reinitialized.\n",
  320. (char*)buf->buf_key.data, rc );
  321. }
  322. else {
  323. csn_dup_or_init_by_csn (&buf->buf_missing_csn, anchorcsn);
  324. }
  325. }
  326. }
  327. if ( rc != 0 ) {
  328. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  329. "clcache_load_buffer: rc=%d\n", rc );
  330. }
  331. return rc;
  332. }
  333. static int
  334. clcache_load_buffer_bulk ( CLC_Buffer *buf, int flag )
  335. {
  336. DB_TXN *txn = NULL;
  337. DBC *cursor = NULL;
  338. int rc;
  339. #if 0 /* txn control seems not improving anything so turn it off */
  340. if ( *(_pool->pl_dbenv) ) {
  341. txn_begin( *(_pool->pl_dbenv), NULL, &txn, 0 );
  342. }
  343. #endif
  344. PR_Lock ( buf->buf_busy_list->bl_lock );
  345. if ( 0 == ( rc = clcache_open_cursor ( txn, buf, &cursor )) ) {
  346. if ( flag == DB_NEXT ) {
  347. /* For bulk read, position the cursor before read the next block */
  348. rc = cursor->c_get ( cursor,
  349. & buf->buf_key,
  350. & buf->buf_data,
  351. DB_SET );
  352. }
  353. /*
  354. * Continue if the error is no-mem since we don't need to
  355. * load in the key record anyway with DB_SET.
  356. */
  357. if ( 0 == rc || DB_BUFFER_SMALL == rc )
  358. rc = clcache_cursor_get ( cursor, buf, flag );
  359. }
  360. /*
  361. * Don't keep a cursor open across the whole replication session.
  362. * That had caused noticable DB resource contention.
  363. */
  364. if ( cursor ) {
  365. cursor->c_close ( cursor );
  366. }
  367. #if 0 /* txn control seems not improving anything so turn it off */
  368. if ( txn ) {
  369. txn->commit ( txn, DB_TXN_NOSYNC );
  370. }
  371. #endif
  372. PR_Unlock ( buf->buf_busy_list->bl_lock );
  373. buf->buf_record_ptr = NULL;
  374. if ( 0 == rc ) {
  375. DB_MULTIPLE_INIT ( buf->buf_record_ptr, &buf->buf_data );
  376. if ( NULL == buf->buf_record_ptr )
  377. rc = DB_NOTFOUND;
  378. else
  379. buf->buf_load_cnt++;
  380. }
  381. return rc;
  382. }
  383. /*
  384. * Gets the next change from the buffer.
  385. * *key : output - key of the next change, or NULL if no more change
  386. * *data: output - data of the next change, or NULL if no more change
  387. */
  388. int
  389. clcache_get_next_change ( CLC_Buffer *buf, void **key, size_t *keylen, void **data, size_t *datalen, CSN **csn )
  390. {
  391. int skip = 1;
  392. int rc = 0;
  393. do {
  394. *key = *data = NULL;
  395. *keylen = *datalen = 0;
  396. if ( buf->buf_record_ptr ) {
  397. DB_MULTIPLE_KEY_NEXT ( buf->buf_record_ptr, &buf->buf_data,
  398. *key, *keylen, *data, *datalen );
  399. }
  400. /*
  401. * We're done with the current buffer. Now load the next chunk.
  402. */
  403. if ( NULL == *key && CLC_STATE_READY == buf->buf_state ) {
  404. rc = clcache_load_buffer ( buf, NULL, DB_NEXT );
  405. if ( 0 == rc && buf->buf_record_ptr ) {
  406. DB_MULTIPLE_KEY_NEXT ( buf->buf_record_ptr, &buf->buf_data,
  407. *key, *keylen, *data, *datalen );
  408. }
  409. }
  410. /* Compare the new change to the local and remote RUVs */
  411. if ( NULL != *key ) {
  412. buf->buf_record_cnt++;
  413. csn_init_by_string ( buf->buf_current_csn, (char*)*key );
  414. skip = clcache_skip_change ( buf );
  415. if (skip) buf->buf_record_skipped++;
  416. }
  417. }
  418. while ( rc == 0 && *key && skip );
  419. if ( NULL == *key ) {
  420. *key = NULL;
  421. *csn = NULL;
  422. rc = DB_NOTFOUND;
  423. }
  424. else {
  425. *csn = buf->buf_current_csn;
  426. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  427. "load=%d rec=%d csn=%s\n",
  428. buf->buf_load_cnt, buf->buf_record_cnt, (char*)*key );
  429. }
  430. return rc;
  431. }
  432. static void
  433. clcache_refresh_consumer_maxcsns ( CLC_Buffer *buf )
  434. {
  435. int i;
  436. for ( i = 0; i < buf->buf_num_cscbs; i++ ) {
  437. csn_free(&buf->buf_cscbs[i]->consumer_maxcsn);
  438. ruv_get_largest_csn_for_replica (
  439. buf->buf_consumer_ruv,
  440. buf->buf_cscbs[i]->rid,
  441. &buf->buf_cscbs[i]->consumer_maxcsn );
  442. }
  443. }
  444. static int
  445. clcache_refresh_local_maxcsn ( const ruv_enum_data *rid_data, void *data )
  446. {
  447. CLC_Buffer *buf = (CLC_Buffer*) data;
  448. ReplicaId rid;
  449. int rc = 0;
  450. int i;
  451. rid = csn_get_replicaid ( rid_data->csn );
  452. /*
  453. * No need to create cscb for consumer's RID.
  454. * If RID==65535, the CSN is originated from a
  455. * legacy consumer. In this case the supplier
  456. * and the consumer may have the same RID.
  457. */
  458. if ( rid == buf->buf_consumer_rid && rid != MAX_REPLICA_ID )
  459. return rc;
  460. for ( i = 0; i < buf->buf_num_cscbs; i++ ) {
  461. if ( buf->buf_cscbs[i]->rid == rid )
  462. break;
  463. }
  464. if ( i >= buf->buf_num_cscbs ) {
  465. buf->buf_cscbs[i] = clcache_new_cscb ();
  466. if ( buf->buf_cscbs[i] == NULL ) {
  467. return -1;
  468. }
  469. buf->buf_cscbs[i]->rid = rid;
  470. buf->buf_num_cscbs++;
  471. }
  472. csn_dup_or_init_by_csn ( &buf->buf_cscbs[i]->local_maxcsn, rid_data->csn );
  473. if ( buf->buf_cscbs[i]->consumer_maxcsn &&
  474. csn_compare (buf->buf_cscbs[i]->consumer_maxcsn, rid_data->csn) >= 0 ) {
  475. /* No change need to be sent for this RID */
  476. buf->buf_cscbs[i]->state = CLC_STATE_UP_TO_DATE;
  477. }
  478. return rc;
  479. }
  480. static int
  481. clcache_refresh_local_maxcsns ( CLC_Buffer *buf )
  482. {
  483. int i;
  484. for ( i = 0; i < buf->buf_num_cscbs; i++ ) {
  485. csn_dup_or_init_by_csn ( &buf->buf_cscbs[i]->prev_local_maxcsn,
  486. buf->buf_cscbs[i]->local_maxcsn );
  487. }
  488. return ruv_enumerate_elements ( buf->buf_local_ruv, clcache_refresh_local_maxcsn, buf );
  489. }
  490. /*
  491. * Algorithm:
  492. *
  493. * 1. Snapshot local RUVs;
  494. * 2. Load buffer;
  495. * 3. Send to the consumer only those CSNs that are covered
  496. * by the RUVs snapshot taken in the first step;
  497. * All CSNs that are covered by the RUVs snapshot taken in the
  498. * first step are guaranteed in consecutive order for the respected
  499. * RIDs because of the the CSN pending list control;
  500. * A CSN that is not covered by the RUVs snapshot may be out of order
  501. * since it is possible that a smaller CSN might not have committed
  502. * yet by the time the buffer was loaded.
  503. * 4. Determine anchorcsn for each RID:
  504. *
  505. * Case| Local vs. Buffer | New Local | Next
  506. * | MaxCSN MaxCSN | MaxCSN | Anchor-CSN
  507. * ----+-------------------+-----------+----------------
  508. * 1 | Cl >= Cb | * | Cb
  509. * 2 | Cl < Cb | Cl | Cb
  510. * 3 | Cl < Cb | Cl2 | Cl
  511. *
  512. * 5. Determine anchorcsn for next load:
  513. * Anchor-CSN = min { all Next-Anchor-CSN, Buffer-MaxCSN }
  514. */
  515. static int
  516. clcache_adjust_anchorcsn ( CLC_Buffer *buf )
  517. {
  518. PRBool hasChange = PR_FALSE;
  519. struct csn_seq_ctrl_block *cscb;
  520. int i;
  521. if ( buf->buf_state == CLC_STATE_READY ) {
  522. for ( i = 0; i < buf->buf_num_cscbs; i++ ) {
  523. cscb = buf->buf_cscbs[i];
  524. if ( cscb->state == CLC_STATE_UP_TO_DATE )
  525. continue;
  526. /*
  527. * Case 3 unsafe ruv change: next buffer load should start
  528. * from where the maxcsn in the old ruv was. Since each
  529. * cscb has remembered the maxcsn sent to the consumer,
  530. * CSNs that may be loaded again could easily be skipped.
  531. */
  532. if ( cscb->prev_local_maxcsn &&
  533. csn_compare (cscb->prev_local_maxcsn, buf->buf_current_csn) < 0 &&
  534. csn_compare (cscb->local_maxcsn, cscb->prev_local_maxcsn) != 0 ) {
  535. hasChange = PR_TRUE;
  536. cscb->state = CLC_STATE_READY;
  537. csn_init_by_csn ( buf->buf_current_csn, cscb->prev_local_maxcsn );
  538. csn_as_string ( cscb->prev_local_maxcsn, 0, (char*)buf->buf_key.data );
  539. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  540. "adjust anchor csn upon %s\n",
  541. ( cscb->state == CLC_STATE_CSN_GT_RUV ? "out of sequence csn" : "unsafe ruv change") );
  542. continue;
  543. }
  544. /*
  545. * check if there are still changes to send for this RID
  546. * Assume we had compared the local maxcsn and the consumer
  547. * max csn before this function was called and hence the
  548. * cscb->state had been set accordingly.
  549. */
  550. if ( hasChange == PR_FALSE &&
  551. csn_compare (cscb->local_maxcsn, buf->buf_current_csn) > 0 ) {
  552. hasChange = PR_TRUE;
  553. }
  554. }
  555. }
  556. if ( !hasChange ) {
  557. buf->buf_state = CLC_STATE_DONE;
  558. }
  559. return buf->buf_state;
  560. }
  561. static int
  562. clcache_skip_change ( CLC_Buffer *buf )
  563. {
  564. struct csn_seq_ctrl_block *cscb = NULL;
  565. ReplicaId rid;
  566. int skip = 1;
  567. int i;
  568. do {
  569. rid = csn_get_replicaid ( buf->buf_current_csn );
  570. /*
  571. * Skip CSN that is originated from the consumer.
  572. * If RID==65535, the CSN is originated from a
  573. * legacy consumer. In this case the supplier
  574. * and the consumer may have the same RID.
  575. */
  576. if (rid == buf->buf_consumer_rid && rid != MAX_REPLICA_ID)
  577. break;
  578. /* Skip helper entry (ENTRY_COUNT, PURGE_RUV and so on) */
  579. if ( cl5HelperEntry ( NULL, buf->buf_current_csn ) == PR_TRUE ) {
  580. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  581. "Skip helper entry type=%ld\n", csn_get_time( buf->buf_current_csn ));
  582. break;
  583. }
  584. /* Find csn sequence control block for the current rid */
  585. for (i = 0; i < buf->buf_num_cscbs && buf->buf_cscbs[i]->rid != rid; i++);
  586. /* Skip CSN whose RID is unknown to the local RUV snapshot */
  587. if ( i >= buf->buf_num_cscbs ) {
  588. buf->buf_state = CLC_STATE_NEW_RID;
  589. break;
  590. }
  591. cscb = buf->buf_cscbs[i];
  592. /* Skip if the consumer is already up-to-date for the RID */
  593. if ( cscb->state == CLC_STATE_UP_TO_DATE ) {
  594. break;
  595. }
  596. /* Skip CSN whose preceedents are not covered by local RUV snapshot */
  597. if ( cscb->state == CLC_STATE_CSN_GT_RUV ) {
  598. break;
  599. }
  600. /* Skip CSNs already covered by consumer RUV */
  601. if ( cscb->consumer_maxcsn &&
  602. csn_compare ( buf->buf_current_csn, cscb->consumer_maxcsn ) <= 0 ) {
  603. break;
  604. }
  605. /* Send CSNs that are covered by the local RUV snapshot */
  606. if ( csn_compare ( buf->buf_current_csn, cscb->local_maxcsn ) <= 0 ) {
  607. skip = 0;
  608. csn_dup_or_init_by_csn ( &cscb->consumer_maxcsn, buf->buf_current_csn );
  609. break;
  610. }
  611. /*
  612. * Promote the local maxcsn to its next neighbor
  613. * to keep the current session going. Skip if we
  614. * are not sure if current_csn is the neighbor.
  615. */
  616. if ( csn_time_difference(buf->buf_current_csn, cscb->local_maxcsn) == 0 &&
  617. (csn_get_seqnum(buf->buf_current_csn) ==
  618. csn_get_seqnum(cscb->local_maxcsn) + 1) ) {
  619. csn_init_by_csn ( cscb->local_maxcsn, buf->buf_current_csn );
  620. csn_init_by_csn ( cscb->consumer_maxcsn, buf->buf_current_csn );
  621. skip = 0;
  622. break;
  623. }
  624. /* Skip CSNs not covered by local RUV snapshot */
  625. cscb->state = CLC_STATE_CSN_GT_RUV;
  626. } while (0);
  627. #ifdef DEBUG
  628. if (skip && cscb) {
  629. char consumer[24] = {'\0'};
  630. char local[24] = {'\0'};
  631. char current[24] = {'\0'};
  632. if ( cscb->consumer_maxcsn )
  633. csn_as_string ( cscb->consumer_maxcsn, PR_FALSE, consumer );
  634. if ( cscb->local_maxcsn )
  635. csn_as_string ( cscb->local_maxcsn, PR_FALSE, local );
  636. csn_as_string ( buf->buf_current_csn, PR_FALSE, current );
  637. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  638. "Skip %s consumer=%s local=%s\n", current, consumer, local );
  639. }
  640. #endif
  641. return skip;
  642. }
  643. static struct csn_seq_ctrl_block *
  644. clcache_new_cscb ()
  645. {
  646. struct csn_seq_ctrl_block *cscb;
  647. cscb = (struct csn_seq_ctrl_block *) slapi_ch_calloc ( 1, sizeof (struct csn_seq_ctrl_block) );
  648. if (cscb == NULL) {
  649. slapi_log_error ( SLAPI_LOG_FATAL, NULL, "clcache: malloc failure\n" );
  650. }
  651. return cscb;
  652. }
  653. static void
  654. clcache_free_cscb ( struct csn_seq_ctrl_block ** cscb )
  655. {
  656. csn_free ( & (*cscb)->consumer_maxcsn );
  657. csn_free ( & (*cscb)->local_maxcsn );
  658. csn_free ( & (*cscb)->prev_local_maxcsn );
  659. slapi_ch_free ( (void **) cscb );
  660. }
  661. /*
  662. * Allocate and initialize a new buffer
  663. * It is called when there is a request for a buffer while
  664. * buffer free list is empty.
  665. */
  666. static CLC_Buffer *
  667. clcache_new_buffer ( ReplicaId consumer_rid )
  668. {
  669. CLC_Buffer *buf = NULL;
  670. int welldone = 0;
  671. do {
  672. buf = (CLC_Buffer*) slapi_ch_calloc (1, sizeof(CLC_Buffer));
  673. if ( NULL == buf )
  674. break;
  675. buf->buf_key.flags = DB_DBT_USERMEM;
  676. buf->buf_key.ulen = CSN_STRSIZE + 1;
  677. buf->buf_key.size = CSN_STRSIZE;
  678. buf->buf_key.data = slapi_ch_calloc( 1, buf->buf_key.ulen );
  679. if ( NULL == buf->buf_key.data )
  680. break;
  681. buf->buf_data.flags = DB_DBT_USERMEM;
  682. buf->buf_data.ulen = _pool->pl_buffer_default_pages * DEFAULT_CLC_BUFFER_PAGE_SIZE;
  683. buf->buf_data.data = slapi_ch_malloc( buf->buf_data.ulen );
  684. if ( NULL == buf->buf_data.data )
  685. break;
  686. if ( NULL == ( buf->buf_current_csn = csn_new()) )
  687. break;
  688. buf->buf_state = CLC_STATE_READY;
  689. buf->buf_agmt_name = get_thread_private_agmtname();
  690. buf->buf_consumer_rid = consumer_rid;
  691. buf->buf_num_cscbs = 0;
  692. welldone = 1;
  693. } while (0);
  694. if ( !welldone ) {
  695. clcache_delete_buffer ( &buf );
  696. }
  697. return buf;
  698. }
  699. /*
  700. * Deallocates a buffer.
  701. * It is called when a buffer is returned to the buffer pool
  702. * and the pool size is over the limit.
  703. */
  704. static void
  705. clcache_delete_buffer ( CLC_Buffer **buf )
  706. {
  707. if ( buf && *buf ) {
  708. slapi_ch_free (&( (*buf)->buf_key.data ));
  709. slapi_ch_free (&( (*buf)->buf_data.data ));
  710. csn_free (&( (*buf)->buf_current_csn ));
  711. csn_free (&( (*buf)->buf_missing_csn ));
  712. slapi_ch_free ( (void **) buf );
  713. }
  714. }
  715. static CLC_Busy_List *
  716. clcache_new_busy_list ()
  717. {
  718. CLC_Busy_List *bl;
  719. int welldone = 0;
  720. do {
  721. if ( NULL == (bl = ( CLC_Busy_List* ) slapi_ch_calloc (1, sizeof(CLC_Busy_List)) ))
  722. break;
  723. if ( NULL == (bl->bl_lock = PR_NewLock ()) )
  724. break;
  725. /*
  726. if ( NULL == (bl->bl_max_csn = csn_new ()) )
  727. break;
  728. */
  729. welldone = 1;
  730. }
  731. while (0);
  732. if ( !welldone ) {
  733. clcache_delete_busy_list ( &bl );
  734. }
  735. return bl;
  736. }
  737. static void
  738. clcache_delete_busy_list ( CLC_Busy_List **bl )
  739. {
  740. if ( bl && *bl ) {
  741. CLC_Buffer *buf = NULL;
  742. if ( (*bl)->bl_lock ) {
  743. PR_Lock ( (*bl)->bl_lock );
  744. }
  745. buf = (*bl)->bl_buffers;
  746. while (buf) {
  747. CLC_Buffer *next = buf->buf_next;
  748. clcache_delete_buffer(&buf);
  749. buf = next;
  750. }
  751. (*bl)->bl_buffers = NULL;
  752. (*bl)->bl_db = NULL;
  753. if ( (*bl)->bl_lock ) {
  754. PR_Unlock ( (*bl)->bl_lock );
  755. PR_DestroyLock ( (*bl)->bl_lock );
  756. (*bl)->bl_lock = NULL;
  757. }
  758. /* csn_free (&( (*bl)->bl_max_csn )); */
  759. slapi_ch_free ( (void **) bl );
  760. }
  761. }
  762. static int
  763. clcache_enqueue_busy_list ( DB *db, CLC_Buffer *buf )
  764. {
  765. CLC_Busy_List *bl;
  766. int rc = 0;
  767. PR_RWLock_Rlock ( _pool->pl_lock );
  768. for ( bl = _pool->pl_busy_lists; bl && bl->bl_db != db; bl = bl->bl_next );
  769. PR_RWLock_Unlock ( _pool->pl_lock );
  770. if ( NULL == bl ) {
  771. if ( NULL == ( bl = clcache_new_busy_list ()) ) {
  772. rc = CL5_MEMORY_ERROR;
  773. }
  774. else {
  775. PR_RWLock_Wlock ( _pool->pl_lock );
  776. bl->bl_db = db;
  777. bl->bl_next = _pool->pl_busy_lists;
  778. _pool->pl_busy_lists = bl;
  779. PR_RWLock_Unlock ( _pool->pl_lock );
  780. }
  781. }
  782. if ( NULL != bl ) {
  783. PR_Lock ( bl->bl_lock );
  784. buf->buf_busy_list = bl;
  785. buf->buf_next = bl->bl_buffers;
  786. bl->bl_buffers = buf;
  787. PR_Unlock ( bl->bl_lock );
  788. }
  789. return rc;
  790. }
  791. static int
  792. clcache_open_cursor ( DB_TXN *txn, CLC_Buffer *buf, DBC **cursor )
  793. {
  794. int rc;
  795. rc = buf->buf_busy_list->bl_db->cursor ( buf->buf_busy_list->bl_db, txn, cursor, 0 );
  796. if ( rc != 0 ) {
  797. slapi_log_error ( SLAPI_LOG_FATAL, get_thread_private_agmtname(),
  798. "clcache: failed to open cursor; db error - %d %s\n",
  799. rc, db_strerror(rc));
  800. }
  801. return rc;
  802. }
  803. static int
  804. clcache_cursor_get ( DBC *cursor, CLC_Buffer *buf, int flag )
  805. {
  806. int rc;
  807. rc = cursor->c_get ( cursor,
  808. & buf->buf_key,
  809. & buf->buf_data,
  810. buf->buf_load_flag | flag );
  811. if ( DB_BUFFER_SMALL == rc ) {
  812. /*
  813. * The record takes more space than the current size of the
  814. * buffer. Fortunately, buf->buf_data.size has been set by
  815. * c_get() to the actual data size needed. So we can
  816. * reallocate the data buffer and try to read again.
  817. */
  818. buf->buf_data.ulen = ( buf->buf_data.size / DEFAULT_CLC_BUFFER_PAGE_SIZE + 1 ) * DEFAULT_CLC_BUFFER_PAGE_SIZE;
  819. buf->buf_data.data = slapi_ch_realloc ( buf->buf_data.data, buf->buf_data.ulen );
  820. if ( buf->buf_data.data != NULL ) {
  821. rc = cursor->c_get ( cursor,
  822. &( buf->buf_key ),
  823. &( buf->buf_data ),
  824. buf->buf_load_flag | flag );
  825. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  826. "clcache: (%d | %d) buf key len %d reallocated and retry returns %d\n", buf->buf_load_flag, flag, buf->buf_key.size, rc );
  827. }
  828. }
  829. switch ( rc ) {
  830. case EINVAL:
  831. slapi_log_error ( SLAPI_LOG_FATAL, buf->buf_agmt_name,
  832. "clcache_cursor_get: invalid parameter\n" );
  833. break;
  834. case DB_BUFFER_SMALL:
  835. slapi_log_error ( SLAPI_LOG_FATAL, buf->buf_agmt_name,
  836. "clcache_cursor_get: can't allocate %u bytes\n", buf->buf_data.ulen );
  837. break;
  838. default:
  839. break;
  840. }
  841. return rc;
  842. }
  843. static void
  844. csn_dup_or_init_by_csn ( CSN **csn1, CSN *csn2 )
  845. {
  846. if ( *csn1 == NULL )
  847. *csn1 = csn_new();
  848. csn_init_by_csn ( *csn1, csn2 );
  849. }
  850. void
  851. clcache_destroy()
  852. {
  853. if (_pool) {
  854. CLC_Busy_List *bl = NULL;
  855. if (_pool->pl_lock) {
  856. PR_RWLock_Wlock (_pool->pl_lock);
  857. }
  858. bl = _pool->pl_busy_lists;
  859. while (bl) {
  860. CLC_Busy_List *next = bl->bl_next;
  861. clcache_delete_busy_list(&bl);
  862. bl = next;
  863. }
  864. _pool->pl_busy_lists = NULL;
  865. _pool->pl_dbenv = NULL;
  866. if (_pool->pl_lock) {
  867. PR_RWLock_Unlock(_pool->pl_lock);
  868. PR_DestroyRWLock(_pool->pl_lock);
  869. _pool->pl_lock = NULL;
  870. }
  871. slapi_ch_free ( (void **) &_pool );
  872. }
  873. }