cl5_clcache.c 27 KB

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