cl5_clcache.c 26 KB

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