cl5_clcache.c 24 KB

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