cl5_clcache.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2005 Red Hat, Inc.
  35. * All rights reserved.
  36. * END COPYRIGHT BLOCK **/
  37. #ifdef HAVE_CONFIG_H
  38. # include <config.h>
  39. #endif
  40. #include "errno.h" /* ENOMEM, EVAL used by Berkeley DB */
  41. #include "db.h" /* Berkeley DB */
  42. #include "cl5.h" /* changelog5Config */
  43. #include "cl5_clcache.h"
  44. /* newer bdb uses DB_BUFFER_SMALL instead of ENOMEM as the
  45. error return if the given buffer in which to load a
  46. key or value is too small - if it is not defined, define
  47. it here to ENOMEM
  48. */
  49. #ifndef DB_BUFFER_SMALL
  50. #define DB_BUFFER_SMALL ENOMEM
  51. #endif
  52. /*
  53. * Constants for the buffer pool:
  54. *
  55. * DEFAULT_CLC_BUFFER_PAGE_COUNT
  56. * Little performance boost if it is too small.
  57. *
  58. * DEFAULT_CLC_BUFFER_PAGE_SIZE
  59. * Its value is determined based on the DB requirement that
  60. * the buffer size should be the multiple of 1024.
  61. */
  62. #define DEFAULT_CLC_BUFFER_COUNT_MIN 10
  63. #define DEFAULT_CLC_BUFFER_COUNT_MAX 0
  64. #define DEFAULT_CLC_BUFFER_PAGE_COUNT 32
  65. #define DEFAULT_CLC_BUFFER_PAGE_SIZE 1024
  66. enum {
  67. CLC_STATE_READY = 0, /* ready to iterate */
  68. CLC_STATE_UP_TO_DATE, /* remote RUV already covers the CSN */
  69. CLC_STATE_CSN_GT_RUV, /* local RUV doesn't conver the CSN */
  70. CLC_STATE_NEW_RID, /* unknown RID to local RUVs */
  71. CLC_STATE_UNSAFE_RUV_CHANGE,/* (RUV1 < maxcsn-in-buffer) && (RUV1 < RUV1') */
  72. CLC_STATE_DONE, /* no more change */
  73. CLC_STATE_ABORTING /* abort replication session */
  74. };
  75. typedef struct clc_busy_list CLC_Busy_List;
  76. struct csn_seq_ctrl_block {
  77. ReplicaId rid; /* RID this block serves */
  78. CSN *consumer_maxcsn; /* Don't send CSN <= this */
  79. CSN *local_maxcsn; /* Don't send CSN > this */
  80. CSN *prev_local_maxcsn; /* */
  81. int state; /* CLC_STATE_* */
  82. };
  83. /*
  84. * Each cl5replayiterator acquires a buffer from the buffer pool
  85. * at the beginning of a replication session, and returns it back
  86. * at the end.
  87. */
  88. struct clc_buffer {
  89. char *buf_agmt_name; /* agreement acquired this buffer */
  90. ReplicaId buf_consumer_rid; /* help checking threshold csn */
  91. const RUV *buf_consumer_ruv; /* used to skip change */
  92. const RUV *buf_local_ruv; /* used to refresh local_maxcsn */
  93. /*
  94. * fields for retriving data from DB
  95. */
  96. int buf_state;
  97. CSN *buf_current_csn;
  98. int buf_load_flag; /* db flag DB_MULTIPLE_KEY, DB_SET, DB_NEXT */
  99. DBC *buf_cursor;
  100. DBT buf_key; /* current csn string */
  101. DBT buf_data; /* data retrived from db */
  102. void *buf_record_ptr; /* ptr to the current record in data */
  103. CSN *buf_missing_csn; /* used to detect persistent missing of CSN */
  104. /* fields for control the CSN sequence sent to the consumer */
  105. struct csn_seq_ctrl_block *buf_cscbs [MAX_NUM_OF_MASTERS];
  106. int buf_num_cscbs; /* number of csn sequence ctrl blocks */
  107. /* fields for debugging stat */
  108. int buf_load_cnt; /* number of loads for session */
  109. int buf_record_cnt; /* number of changes for session */
  110. int buf_record_skipped; /* number of changes skipped */
  111. int buf_skipped_new_rid; /* number of changes skipped due to new_rid */
  112. int buf_skipped_csn_gt_cons_maxcsn; /* number of changes skipped due to csn greater than consumer maxcsn */
  113. int buf_skipped_up_to_date; /* number of changes skipped due to consumer being up-to-date for the given rid */
  114. int buf_skipped_csn_gt_ruv; /* number of changes skipped due to preceedents are not covered by local RUV snapshot */
  115. int buf_skipped_csn_covered; /* number of changes skipped due to CSNs already covered by consumer RUV */
  116. /*
  117. * fields that should be accessed via bl_lock or pl_lock
  118. */
  119. CLC_Buffer *buf_next; /* next buffer in the same list */
  120. CLC_Busy_List *buf_busy_list; /* which busy list I'm in */
  121. };
  122. /*
  123. * Each changelog has a busy buffer list
  124. */
  125. struct clc_busy_list {
  126. PRLock *bl_lock;
  127. DB *bl_db; /* changelog db handle */
  128. CLC_Buffer *bl_buffers; /* busy buffers of this list */
  129. CLC_Busy_List *bl_next; /* next busy list in the pool */
  130. };
  131. /*
  132. * Each process has a buffer pool
  133. */
  134. struct clc_pool {
  135. Slapi_RWLock *pl_lock; /* cl writer and agreements */
  136. DB_ENV **pl_dbenv; /* pointer to DB_ENV for all the changelog files */
  137. CLC_Busy_List *pl_busy_lists; /* busy buffer lists, one list per changelog file */
  138. int pl_buffer_cnt_now; /* total number of buffers */
  139. int pl_buffer_cnt_min; /* free a newly returned buffer if _now > _min */
  140. int pl_buffer_cnt_max; /* no use */
  141. int pl_buffer_default_pages; /* num of pages in a new buffer */
  142. };
  143. /* static variables */
  144. static struct clc_pool *_pool = NULL; /* process's buffer pool */
  145. /* static prototypes */
  146. static int clcache_adjust_anchorcsn ( CLC_Buffer *buf );
  147. static void clcache_refresh_consumer_maxcsns ( CLC_Buffer *buf );
  148. static int clcache_refresh_local_maxcsns ( CLC_Buffer *buf );
  149. static int clcache_skip_change ( CLC_Buffer *buf );
  150. static int clcache_load_buffer_bulk ( CLC_Buffer *buf, int flag );
  151. static int clcache_open_cursor ( DB_TXN *txn, CLC_Buffer *buf, DBC **cursor );
  152. static int clcache_cursor_get ( DBC *cursor, CLC_Buffer *buf, int flag );
  153. static struct csn_seq_ctrl_block *clcache_new_cscb ();
  154. static void clcache_free_cscb ( struct csn_seq_ctrl_block ** cscb );
  155. static CLC_Buffer *clcache_new_buffer ( ReplicaId consumer_rid );
  156. static void clcache_delete_buffer ( CLC_Buffer **buf );
  157. static CLC_Busy_List *clcache_new_busy_list ();
  158. static void clcache_delete_busy_list ( CLC_Busy_List **bl );
  159. static int clcache_enqueue_busy_list( DB *db, CLC_Buffer *buf );
  160. static void csn_dup_or_init_by_csn ( CSN **csn1, CSN *csn2 );
  161. /*
  162. * Initiates the process buffer pool. This should be done
  163. * once and only once when process starts.
  164. */
  165. int
  166. clcache_init ( DB_ENV **dbenv )
  167. {
  168. if (_pool) {
  169. return 0; /* already initialized */
  170. }
  171. if (NULL == dbenv) {
  172. return -1;
  173. }
  174. _pool = (struct clc_pool*) slapi_ch_calloc ( 1, sizeof ( struct clc_pool ));
  175. _pool->pl_dbenv = dbenv;
  176. _pool->pl_buffer_cnt_min = DEFAULT_CLC_BUFFER_COUNT_MIN;
  177. _pool->pl_buffer_cnt_max = DEFAULT_CLC_BUFFER_COUNT_MAX;
  178. _pool->pl_buffer_default_pages = DEFAULT_CLC_BUFFER_COUNT_MAX;
  179. _pool->pl_lock = slapi_new_rwlock ();
  180. return 0;
  181. }
  182. /*
  183. * This is part of a callback function when changelog configuration
  184. * is read or updated.
  185. */
  186. void
  187. clcache_set_config ()
  188. {
  189. slapi_rwlock_wrlock ( _pool->pl_lock );
  190. _pool->pl_buffer_cnt_max = CL5_DEFAULT_CONFIG_CACHESIZE;
  191. /*
  192. * According to http://www.sleepycat.com/docs/api_c/dbc_get.html,
  193. * data buffer should be a multiple of 1024 bytes in size
  194. * for DB_MULTIPLE_KEY operation.
  195. */
  196. _pool->pl_buffer_default_pages = CL5_DEFAULT_CONFIG_CACHEMEMSIZE / DEFAULT_CLC_BUFFER_PAGE_SIZE + 1;
  197. _pool->pl_buffer_default_pages = DEFAULT_CLC_BUFFER_PAGE_COUNT;
  198. if ( _pool->pl_buffer_default_pages <= 0 ) {
  199. _pool->pl_buffer_default_pages = DEFAULT_CLC_BUFFER_PAGE_COUNT;
  200. }
  201. slapi_rwlock_unlock ( _pool->pl_lock );
  202. }
  203. /*
  204. * Gets the pointer to a thread dedicated buffer, or allocates
  205. * a new buffer if there is no buffer allocated yet for this thread.
  206. *
  207. * This is called when a cl5replayiterator is created for
  208. * a replication session.
  209. */
  210. int
  211. clcache_get_buffer ( CLC_Buffer **buf, DB *db, ReplicaId consumer_rid, const RUV *consumer_ruv, const RUV *local_ruv )
  212. {
  213. int rc = 0;
  214. int need_new;
  215. if ( buf == NULL ) return CL5_BAD_DATA;
  216. *buf = NULL;
  217. /* if the pool was re-initialized, the thread private cache will be invalid,
  218. so we must get a new one */
  219. need_new = (!_pool || !_pool->pl_busy_lists || !_pool->pl_busy_lists->bl_buffers);
  220. if ( (!need_new) && (NULL != ( *buf = (CLC_Buffer*) get_thread_private_cache())) ) {
  221. slapi_log_error ( SLAPI_LOG_REPL, get_thread_private_agmtname(),
  222. "clcache_get_buffer: found thread private buffer cache %p\n", *buf);
  223. slapi_log_error ( SLAPI_LOG_REPL, get_thread_private_agmtname(),
  224. "clcache_get_buffer: _pool is %p _pool->pl_busy_lists is %p _pool->pl_busy_lists->bl_buffers is %p\n",
  225. _pool, _pool ? _pool->pl_busy_lists : NULL,
  226. (_pool && _pool->pl_busy_lists) ? _pool->pl_busy_lists->bl_buffers : NULL);
  227. (*buf)->buf_state = CLC_STATE_READY;
  228. (*buf)->buf_load_cnt = 0;
  229. (*buf)->buf_record_cnt = 0;
  230. (*buf)->buf_record_skipped = 0;
  231. (*buf)->buf_cursor = NULL;
  232. (*buf)->buf_num_cscbs = 0;
  233. (*buf)->buf_skipped_new_rid = 0;
  234. (*buf)->buf_skipped_csn_gt_cons_maxcsn = 0;
  235. (*buf)->buf_skipped_up_to_date = 0;
  236. (*buf)->buf_skipped_csn_gt_ruv = 0;
  237. (*buf)->buf_skipped_csn_covered = 0;
  238. }
  239. else {
  240. *buf = clcache_new_buffer ( consumer_rid );
  241. if ( *buf ) {
  242. if ( 0 == clcache_enqueue_busy_list ( db, *buf ) ) {
  243. set_thread_private_cache ( (void*) (*buf) );
  244. }
  245. else {
  246. clcache_delete_buffer ( buf );
  247. }
  248. }
  249. }
  250. if ( NULL != *buf ) {
  251. (*buf)->buf_consumer_ruv = consumer_ruv;
  252. (*buf)->buf_local_ruv = local_ruv;
  253. }
  254. else {
  255. slapi_log_error ( SLAPI_LOG_FATAL, get_thread_private_agmtname(),
  256. "clcache_get_buffer: can't allocate new buffer\n" );
  257. rc = CL5_MEMORY_ERROR;
  258. }
  259. return rc;
  260. }
  261. /*
  262. * Returns a buffer back to the buffer pool.
  263. */
  264. void
  265. clcache_return_buffer ( CLC_Buffer **buf )
  266. {
  267. int i;
  268. slapi_log_error ( SLAPI_LOG_REPL, (*buf)->buf_agmt_name,
  269. "session end: state=%d load=%d sent=%d skipped=%d skipped_new_rid=%d "
  270. "skipped_csn_gt_cons_maxcsn=%d skipped_up_to_date=%d "
  271. "skipped_csn_gt_ruv=%d skipped_csn_covered=%d\n",
  272. (*buf)->buf_state,
  273. (*buf)->buf_load_cnt,
  274. (*buf)->buf_record_cnt - (*buf)->buf_record_skipped,
  275. (*buf)->buf_record_skipped, (*buf)->buf_skipped_new_rid,
  276. (*buf)->buf_skipped_csn_gt_cons_maxcsn,
  277. (*buf)->buf_skipped_up_to_date, (*buf)->buf_skipped_csn_gt_ruv,
  278. (*buf)->buf_skipped_csn_covered);
  279. for ( i = 0; i < (*buf)->buf_num_cscbs; i++ ) {
  280. clcache_free_cscb ( &(*buf)->buf_cscbs[i] );
  281. }
  282. (*buf)->buf_num_cscbs = 0;
  283. if ( (*buf)->buf_cursor ) {
  284. (*buf)->buf_cursor->c_close ( (*buf)->buf_cursor );
  285. (*buf)->buf_cursor = NULL;
  286. }
  287. }
  288. /*
  289. * Loads a buffer from DB.
  290. *
  291. * anchorcsn - passed in for the first load of a replication session;
  292. * flag - DB_SET to load in the key CSN record.
  293. * DB_NEXT to load in the records greater than key CSN.
  294. * return - DB error code instead of cl5 one because of the
  295. * historic reason.
  296. */
  297. int
  298. clcache_load_buffer ( CLC_Buffer *buf, CSN *anchorcsn, int flag )
  299. {
  300. int rc = 0;
  301. clcache_refresh_local_maxcsns ( buf );
  302. /* Set the loading key */
  303. if ( anchorcsn ) {
  304. clcache_refresh_consumer_maxcsns ( buf );
  305. buf->buf_load_flag = DB_MULTIPLE_KEY;
  306. csn_as_string ( anchorcsn, 0, (char*)buf->buf_key.data );
  307. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  308. "session start: anchorcsn=%s\n", (char*)buf->buf_key.data );
  309. }
  310. else if ( csn_get_time(buf->buf_current_csn) == 0 ) {
  311. /* time == 0 means this csn has never been set */
  312. rc = DB_NOTFOUND;
  313. }
  314. else if ( clcache_adjust_anchorcsn ( buf ) != 0 ) {
  315. rc = DB_NOTFOUND;
  316. }
  317. else {
  318. csn_as_string ( buf->buf_current_csn, 0, (char*)buf->buf_key.data );
  319. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  320. "load next: anchorcsn=%s\n", (char*)buf->buf_key.data );
  321. }
  322. if ( rc == 0 ) {
  323. buf->buf_state = CLC_STATE_READY;
  324. rc = clcache_load_buffer_bulk ( buf, flag );
  325. /* Reset some flag variables */
  326. if ( rc == 0 ) {
  327. int i;
  328. for ( i = 0; i < buf->buf_num_cscbs; i++ ) {
  329. buf->buf_cscbs[i]->state = CLC_STATE_READY;
  330. }
  331. }
  332. else if ( anchorcsn ) {
  333. /* Report error only when the missing is persistent */
  334. if ( buf->buf_missing_csn && csn_compare (buf->buf_missing_csn, anchorcsn) == 0 ) {
  335. slapi_log_error ( SLAPI_LOG_FATAL, buf->buf_agmt_name,
  336. "Can't locate CSN %s in the changelog (DB rc=%d). The consumer may need to be reinitialized.\n",
  337. (char*)buf->buf_key.data, rc );
  338. }
  339. else {
  340. csn_dup_or_init_by_csn (&buf->buf_missing_csn, anchorcsn);
  341. }
  342. }
  343. }
  344. if ( rc != 0 ) {
  345. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  346. "clcache_load_buffer: rc=%d\n", rc );
  347. }
  348. return rc;
  349. }
  350. static int
  351. clcache_load_buffer_bulk ( CLC_Buffer *buf, int flag )
  352. {
  353. DB_TXN *txn = NULL;
  354. DBC *cursor = NULL;
  355. int rc = 0;
  356. int tries = 0;
  357. #if 0 /* txn control seems not improving anything so turn it off */
  358. if ( *(_pool->pl_dbenv) ) {
  359. txn_begin( *(_pool->pl_dbenv), NULL, &txn, 0 );
  360. }
  361. #endif
  362. if (NULL == buf) {
  363. slapi_log_error ( SLAPI_LOG_FATAL, "clcache_load_buffer_bulk",
  364. "NULL buf\n" );
  365. return rc;
  366. }
  367. if (NULL == buf->buf_busy_list) {
  368. slapi_log_error ( SLAPI_LOG_FATAL, "clcache_load_buffer_bulk",
  369. "%s%sno buf_busy_list\n",
  370. buf->buf_agmt_name?buf->buf_agmt_name:"",
  371. buf->buf_agmt_name?": ":"" );
  372. return rc;
  373. }
  374. PR_Lock ( buf->buf_busy_list->bl_lock );
  375. retry:
  376. if ( 0 == ( rc = clcache_open_cursor ( txn, buf, &cursor )) ) {
  377. if ( flag == DB_NEXT ) {
  378. /* For bulk read, position the cursor before read the next block */
  379. rc = cursor->c_get ( cursor,
  380. & buf->buf_key,
  381. & buf->buf_data,
  382. DB_SET );
  383. }
  384. /*
  385. * Continue if the error is no-mem since we don't need to
  386. * load in the key record anyway with DB_SET.
  387. */
  388. if ( 0 == rc || DB_BUFFER_SMALL == rc )
  389. rc = clcache_cursor_get ( cursor, buf, flag );
  390. }
  391. /*
  392. * Don't keep a cursor open across the whole replication session.
  393. * That had caused noticeable DB resource contention.
  394. */
  395. if ( cursor ) {
  396. cursor->c_close ( cursor );
  397. cursor = NULL;
  398. }
  399. if ((rc == DB_LOCK_DEADLOCK) && (tries < MAX_TRIALS)) {
  400. PRIntervalTime interval;
  401. tries++;
  402. slapi_log_error ( SLAPI_LOG_TRACE, "clcache_load_buffer_bulk",
  403. "deadlock number [%d] - retrying\n", tries );
  404. /* back off */
  405. interval = PR_MillisecondsToInterval(slapi_rand() % 100);
  406. DS_Sleep(interval);
  407. goto retry;
  408. }
  409. if ((rc == DB_LOCK_DEADLOCK) && (tries >= MAX_TRIALS)) {
  410. slapi_log_error ( SLAPI_LOG_REPL, "clcache_load_buffer_bulk",
  411. "could not load buffer from changelog after %d tries\n", tries );
  412. }
  413. #if 0 /* txn control seems not improving anything so turn it off */
  414. if ( txn ) {
  415. txn->commit ( txn, DB_TXN_NOSYNC );
  416. }
  417. #endif
  418. PR_Unlock ( buf->buf_busy_list->bl_lock );
  419. buf->buf_record_ptr = NULL;
  420. if ( 0 == rc ) {
  421. DB_MULTIPLE_INIT ( buf->buf_record_ptr, &buf->buf_data );
  422. if ( NULL == buf->buf_record_ptr )
  423. rc = DB_NOTFOUND;
  424. else
  425. buf->buf_load_cnt++;
  426. }
  427. return rc;
  428. }
  429. /*
  430. * Gets the next change from the buffer.
  431. * *key : output - key of the next change, or NULL if no more change
  432. * *data: output - data of the next change, or NULL if no more change
  433. */
  434. int
  435. clcache_get_next_change ( CLC_Buffer *buf, void **key, size_t *keylen, void **data, size_t *datalen, CSN **csn )
  436. {
  437. int skip = 1;
  438. int rc = 0;
  439. do {
  440. *key = *data = NULL;
  441. *keylen = *datalen = 0;
  442. if ( buf->buf_record_ptr ) {
  443. DB_MULTIPLE_KEY_NEXT ( buf->buf_record_ptr, &buf->buf_data,
  444. *key, *keylen, *data, *datalen );
  445. }
  446. /*
  447. * We're done with the current buffer. Now load the next chunk.
  448. */
  449. if ( NULL == *key && CLC_STATE_READY == buf->buf_state ) {
  450. rc = clcache_load_buffer ( buf, NULL, DB_NEXT );
  451. if ( 0 == rc && buf->buf_record_ptr ) {
  452. DB_MULTIPLE_KEY_NEXT ( buf->buf_record_ptr, &buf->buf_data,
  453. *key, *keylen, *data, *datalen );
  454. }
  455. }
  456. /* Compare the new change to the local and remote RUVs */
  457. if ( NULL != *key ) {
  458. buf->buf_record_cnt++;
  459. csn_init_by_string ( buf->buf_current_csn, (char*)*key );
  460. skip = clcache_skip_change ( buf );
  461. if (skip) buf->buf_record_skipped++;
  462. }
  463. }
  464. while ( rc == 0 && *key && skip );
  465. if ( NULL == *key ) {
  466. *key = NULL;
  467. *csn = NULL;
  468. rc = DB_NOTFOUND;
  469. }
  470. else {
  471. *csn = buf->buf_current_csn;
  472. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  473. "load=%d rec=%d csn=%s\n",
  474. buf->buf_load_cnt, buf->buf_record_cnt, (char*)*key );
  475. }
  476. return rc;
  477. }
  478. static void
  479. clcache_refresh_consumer_maxcsns ( CLC_Buffer *buf )
  480. {
  481. int i;
  482. for ( i = 0; i < buf->buf_num_cscbs; i++ ) {
  483. csn_free(&buf->buf_cscbs[i]->consumer_maxcsn);
  484. ruv_get_largest_csn_for_replica (
  485. buf->buf_consumer_ruv,
  486. buf->buf_cscbs[i]->rid,
  487. &buf->buf_cscbs[i]->consumer_maxcsn );
  488. }
  489. }
  490. static int
  491. clcache_refresh_local_maxcsn ( const ruv_enum_data *rid_data, void *data )
  492. {
  493. CLC_Buffer *buf = (CLC_Buffer*) data;
  494. ReplicaId rid;
  495. int rc = 0;
  496. int i;
  497. rid = csn_get_replicaid ( rid_data->csn );
  498. /*
  499. * No need to create cscb for consumer's RID.
  500. * If RID==65535, the CSN is originated from a
  501. * legacy consumer. In this case the supplier
  502. * and the consumer may have the same RID.
  503. */
  504. if ( rid == buf->buf_consumer_rid && rid != MAX_REPLICA_ID )
  505. return rc;
  506. for ( i = 0; i < buf->buf_num_cscbs; i++ ) {
  507. if ( buf->buf_cscbs[i]->rid == rid )
  508. break;
  509. }
  510. if ( i >= buf->buf_num_cscbs ) {
  511. buf->buf_cscbs[i] = clcache_new_cscb ();
  512. if ( buf->buf_cscbs[i] == NULL ) {
  513. return -1;
  514. }
  515. buf->buf_cscbs[i]->rid = rid;
  516. buf->buf_num_cscbs++;
  517. }
  518. csn_dup_or_init_by_csn ( &buf->buf_cscbs[i]->local_maxcsn, rid_data->csn );
  519. if ( buf->buf_cscbs[i]->consumer_maxcsn &&
  520. csn_compare (buf->buf_cscbs[i]->consumer_maxcsn, rid_data->csn) >= 0 ) {
  521. /* No change need to be sent for this RID */
  522. buf->buf_cscbs[i]->state = CLC_STATE_UP_TO_DATE;
  523. }
  524. return rc;
  525. }
  526. static int
  527. clcache_refresh_local_maxcsns ( CLC_Buffer *buf )
  528. {
  529. int i;
  530. for ( i = 0; i < buf->buf_num_cscbs; i++ ) {
  531. csn_dup_or_init_by_csn ( &buf->buf_cscbs[i]->prev_local_maxcsn,
  532. buf->buf_cscbs[i]->local_maxcsn );
  533. }
  534. return ruv_enumerate_elements ( buf->buf_local_ruv, clcache_refresh_local_maxcsn, buf );
  535. }
  536. /*
  537. * Algorithm:
  538. *
  539. * 1. Snapshot local RUVs;
  540. * 2. Load buffer;
  541. * 3. Send to the consumer only those CSNs that are covered
  542. * by the RUVs snapshot taken in the first step;
  543. * All CSNs that are covered by the RUVs snapshot taken in the
  544. * first step are guaranteed in consecutive order for the respected
  545. * RIDs because of the the CSN pending list control;
  546. * A CSN that is not covered by the RUVs snapshot may be out of order
  547. * since it is possible that a smaller CSN might not have committed
  548. * yet by the time the buffer was loaded.
  549. * 4. Determine anchorcsn for each RID:
  550. *
  551. * Case| Local vs. Buffer | New Local | Next
  552. * | MaxCSN MaxCSN | MaxCSN | Anchor-CSN
  553. * ----+-------------------+-----------+----------------
  554. * 1 | Cl >= Cb | * | Cb
  555. * 2 | Cl < Cb | Cl | Cb
  556. * 3 | Cl < Cb | Cl2 | Cl
  557. *
  558. * 5. Determine anchorcsn for next load:
  559. * Anchor-CSN = min { all Next-Anchor-CSN, Buffer-MaxCSN }
  560. */
  561. static int
  562. clcache_adjust_anchorcsn ( CLC_Buffer *buf )
  563. {
  564. PRBool hasChange = PR_FALSE;
  565. struct csn_seq_ctrl_block *cscb;
  566. int i;
  567. if ( buf->buf_state == CLC_STATE_READY ) {
  568. for ( i = 0; i < buf->buf_num_cscbs; i++ ) {
  569. cscb = buf->buf_cscbs[i];
  570. if ( cscb->state == CLC_STATE_UP_TO_DATE )
  571. continue;
  572. /*
  573. * Case 3 unsafe ruv change: next buffer load should start
  574. * from where the maxcsn in the old ruv was. Since each
  575. * cscb has remembered the maxcsn sent to the consumer,
  576. * CSNs that may be loaded again could easily be skipped.
  577. */
  578. if ( cscb->prev_local_maxcsn &&
  579. csn_compare (cscb->prev_local_maxcsn, buf->buf_current_csn) < 0 &&
  580. csn_compare (cscb->local_maxcsn, cscb->prev_local_maxcsn) != 0 ) {
  581. hasChange = PR_TRUE;
  582. cscb->state = CLC_STATE_READY;
  583. csn_init_by_csn ( buf->buf_current_csn, cscb->prev_local_maxcsn );
  584. csn_as_string ( cscb->prev_local_maxcsn, 0, (char*)buf->buf_key.data );
  585. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  586. "adjust anchor csn upon %s\n",
  587. ( cscb->state == CLC_STATE_CSN_GT_RUV ? "out of sequence csn" : "unsafe ruv change") );
  588. continue;
  589. }
  590. /*
  591. * check if there are still changes to send for this RID
  592. * Assume we had compared the local maxcsn and the consumer
  593. * max csn before this function was called and hence the
  594. * cscb->state had been set accordingly.
  595. */
  596. if ( hasChange == PR_FALSE &&
  597. csn_compare (cscb->local_maxcsn, buf->buf_current_csn) > 0 ) {
  598. hasChange = PR_TRUE;
  599. }
  600. }
  601. }
  602. if ( !hasChange ) {
  603. buf->buf_state = CLC_STATE_DONE;
  604. }
  605. return buf->buf_state;
  606. }
  607. static int
  608. clcache_skip_change ( CLC_Buffer *buf )
  609. {
  610. struct csn_seq_ctrl_block *cscb = NULL;
  611. ReplicaId rid;
  612. int skip = 1;
  613. int i;
  614. char buf_cur_csn_str[CSN_STRSIZE];
  615. char oth_csn_str[CSN_STRSIZE];
  616. do {
  617. rid = csn_get_replicaid ( buf->buf_current_csn );
  618. /*
  619. * Skip CSN that is originated from the consumer,
  620. * unless the CSN is newer than the maxcsn.
  621. * If RID==65535, the CSN is originated from a
  622. * legacy consumer. In this case the supplier
  623. * and the consumer may have the same RID.
  624. */
  625. if (rid == buf->buf_consumer_rid && rid != MAX_REPLICA_ID){
  626. CSN *cons_maxcsn = NULL;
  627. ruv_get_max_csn(buf->buf_consumer_ruv, &cons_maxcsn);
  628. if ( csn_compare ( buf->buf_current_csn, cons_maxcsn) > 0 ) {
  629. /*
  630. * The consumer must have been "restored" and needs this newer update.
  631. */
  632. skip = 0;
  633. } else if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  634. csn_as_string(buf->buf_current_csn, 0, buf_cur_csn_str);
  635. csn_as_string(cons_maxcsn, 0, oth_csn_str);
  636. slapi_log_error(SLAPI_LOG_REPL, buf->buf_agmt_name,
  637. "Skipping update because the changelog buffer current csn [%s] is "
  638. "less than or equal to the consumer max csn [%s]\n",
  639. buf_cur_csn_str, oth_csn_str);
  640. buf->buf_skipped_csn_gt_cons_maxcsn++;
  641. }
  642. csn_free(&cons_maxcsn);
  643. break;
  644. }
  645. /* Skip helper entry (ENTRY_COUNT, PURGE_RUV and so on) */
  646. if ( cl5HelperEntry ( NULL, buf->buf_current_csn ) == PR_TRUE ) {
  647. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  648. "Skip helper entry type=%ld\n", csn_get_time( buf->buf_current_csn ));
  649. break;
  650. }
  651. /* Find csn sequence control block for the current rid */
  652. for (i = 0; i < buf->buf_num_cscbs && buf->buf_cscbs[i]->rid != rid; i++);
  653. /* Skip CSN whose RID is unknown to the local RUV snapshot */
  654. if ( i >= buf->buf_num_cscbs ) {
  655. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  656. csn_as_string(buf->buf_current_csn, 0, buf_cur_csn_str);
  657. slapi_log_error(SLAPI_LOG_REPL, buf->buf_agmt_name,
  658. "Skipping update because the changelog buffer current csn [%s] rid "
  659. "[%d] is not in the list of changelog csn buffers (length %d)\n",
  660. buf_cur_csn_str, rid, buf->buf_num_cscbs);
  661. }
  662. buf->buf_skipped_new_rid++;
  663. break;
  664. }
  665. cscb = buf->buf_cscbs[i];
  666. /* Skip if the consumer is already up-to-date for the RID */
  667. if ( cscb->state == CLC_STATE_UP_TO_DATE ) {
  668. buf->buf_skipped_up_to_date++;
  669. break;
  670. }
  671. /* Skip CSN whose preceedents are not covered by local RUV snapshot */
  672. if ( cscb->state == CLC_STATE_CSN_GT_RUV ) {
  673. buf->buf_skipped_csn_gt_ruv++;
  674. break;
  675. }
  676. /* Skip CSNs already covered by consumer RUV */
  677. if ( cscb->consumer_maxcsn &&
  678. csn_compare ( buf->buf_current_csn, cscb->consumer_maxcsn ) <= 0 ) {
  679. buf->buf_skipped_csn_covered++;
  680. break;
  681. }
  682. /* Send CSNs that are covered by the local RUV snapshot */
  683. if ( csn_compare ( buf->buf_current_csn, cscb->local_maxcsn ) <= 0 ) {
  684. skip = 0;
  685. csn_dup_or_init_by_csn ( &cscb->consumer_maxcsn, buf->buf_current_csn );
  686. break;
  687. }
  688. /*
  689. * Promote the local maxcsn to its next neighbor
  690. * to keep the current session going. Skip if we
  691. * are not sure if current_csn is the neighbor.
  692. */
  693. if ( csn_time_difference(buf->buf_current_csn, cscb->local_maxcsn) == 0 &&
  694. (csn_get_seqnum(buf->buf_current_csn) ==
  695. csn_get_seqnum(cscb->local_maxcsn) + 1) )
  696. {
  697. csn_init_by_csn ( cscb->local_maxcsn, buf->buf_current_csn );
  698. if(cscb->consumer_maxcsn){
  699. csn_init_by_csn ( cscb->consumer_maxcsn, buf->buf_current_csn );
  700. }
  701. skip = 0;
  702. break;
  703. }
  704. /* Skip CSNs not covered by local RUV snapshot */
  705. cscb->state = CLC_STATE_CSN_GT_RUV;
  706. buf->buf_skipped_csn_gt_ruv++;
  707. } while (0);
  708. #ifdef DEBUG
  709. if (skip && cscb) {
  710. char consumer[24] = {'\0'};
  711. char local[24] = {'\0'};
  712. char current[24] = {'\0'};
  713. if ( cscb->consumer_maxcsn )
  714. csn_as_string ( cscb->consumer_maxcsn, PR_FALSE, consumer );
  715. if ( cscb->local_maxcsn )
  716. csn_as_string ( cscb->local_maxcsn, PR_FALSE, local );
  717. csn_as_string ( buf->buf_current_csn, PR_FALSE, current );
  718. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  719. "Skip %s consumer=%s local=%s\n", current, consumer, local );
  720. }
  721. #endif
  722. return skip;
  723. }
  724. static struct csn_seq_ctrl_block *
  725. clcache_new_cscb ()
  726. {
  727. struct csn_seq_ctrl_block *cscb;
  728. cscb = (struct csn_seq_ctrl_block *) slapi_ch_calloc ( 1, sizeof (struct csn_seq_ctrl_block) );
  729. if (cscb == NULL) {
  730. slapi_log_error ( SLAPI_LOG_FATAL, NULL, "clcache: malloc failure\n" );
  731. }
  732. return cscb;
  733. }
  734. static void
  735. clcache_free_cscb ( struct csn_seq_ctrl_block ** cscb )
  736. {
  737. csn_free ( & (*cscb)->consumer_maxcsn );
  738. csn_free ( & (*cscb)->local_maxcsn );
  739. csn_free ( & (*cscb)->prev_local_maxcsn );
  740. slapi_ch_free ( (void **) cscb );
  741. }
  742. /*
  743. * Allocate and initialize a new buffer
  744. * It is called when there is a request for a buffer while
  745. * buffer free list is empty.
  746. */
  747. static CLC_Buffer *
  748. clcache_new_buffer ( ReplicaId consumer_rid )
  749. {
  750. CLC_Buffer *buf = NULL;
  751. int welldone = 0;
  752. do {
  753. buf = (CLC_Buffer*) slapi_ch_calloc (1, sizeof(CLC_Buffer));
  754. if ( NULL == buf )
  755. break;
  756. buf->buf_key.flags = DB_DBT_USERMEM;
  757. buf->buf_key.ulen = CSN_STRSIZE + 1;
  758. buf->buf_key.size = CSN_STRSIZE;
  759. buf->buf_key.data = slapi_ch_calloc( 1, buf->buf_key.ulen );
  760. if ( NULL == buf->buf_key.data )
  761. break;
  762. buf->buf_data.flags = DB_DBT_USERMEM;
  763. buf->buf_data.ulen = _pool->pl_buffer_default_pages * DEFAULT_CLC_BUFFER_PAGE_SIZE;
  764. buf->buf_data.data = slapi_ch_malloc( buf->buf_data.ulen );
  765. if ( NULL == buf->buf_data.data )
  766. break;
  767. if ( NULL == ( buf->buf_current_csn = csn_new()) )
  768. break;
  769. buf->buf_state = CLC_STATE_READY;
  770. buf->buf_agmt_name = get_thread_private_agmtname();
  771. buf->buf_consumer_rid = consumer_rid;
  772. buf->buf_num_cscbs = 0;
  773. welldone = 1;
  774. } while (0);
  775. if ( !welldone ) {
  776. clcache_delete_buffer ( &buf );
  777. }
  778. return buf;
  779. }
  780. /*
  781. * Deallocates a buffer.
  782. * It is called when a buffer is returned to the buffer pool
  783. * and the pool size is over the limit.
  784. */
  785. static void
  786. clcache_delete_buffer ( CLC_Buffer **buf )
  787. {
  788. if ( buf && *buf ) {
  789. slapi_ch_free (&( (*buf)->buf_key.data ));
  790. slapi_ch_free (&( (*buf)->buf_data.data ));
  791. csn_free (&( (*buf)->buf_current_csn ));
  792. csn_free (&( (*buf)->buf_missing_csn ));
  793. slapi_ch_free ( (void **) buf );
  794. }
  795. }
  796. static CLC_Busy_List *
  797. clcache_new_busy_list ()
  798. {
  799. CLC_Busy_List *bl;
  800. int welldone = 0;
  801. do {
  802. if ( NULL == (bl = ( CLC_Busy_List* ) slapi_ch_calloc (1, sizeof(CLC_Busy_List)) ))
  803. break;
  804. if ( NULL == (bl->bl_lock = PR_NewLock ()) )
  805. break;
  806. /*
  807. if ( NULL == (bl->bl_max_csn = csn_new ()) )
  808. break;
  809. */
  810. welldone = 1;
  811. }
  812. while (0);
  813. if ( !welldone ) {
  814. clcache_delete_busy_list ( &bl );
  815. }
  816. return bl;
  817. }
  818. static void
  819. clcache_delete_busy_list ( CLC_Busy_List **bl )
  820. {
  821. if ( bl && *bl ) {
  822. CLC_Buffer *buf = NULL;
  823. if ( (*bl)->bl_lock ) {
  824. PR_Lock ( (*bl)->bl_lock );
  825. }
  826. buf = (*bl)->bl_buffers;
  827. while (buf) {
  828. CLC_Buffer *next = buf->buf_next;
  829. clcache_delete_buffer(&buf);
  830. buf = next;
  831. }
  832. (*bl)->bl_buffers = NULL;
  833. (*bl)->bl_db = NULL;
  834. if ( (*bl)->bl_lock ) {
  835. PR_Unlock ( (*bl)->bl_lock );
  836. PR_DestroyLock ( (*bl)->bl_lock );
  837. (*bl)->bl_lock = NULL;
  838. }
  839. /* csn_free (&( (*bl)->bl_max_csn )); */
  840. slapi_ch_free ( (void **) bl );
  841. }
  842. }
  843. static int
  844. clcache_enqueue_busy_list ( DB *db, CLC_Buffer *buf )
  845. {
  846. CLC_Busy_List *bl;
  847. int rc = 0;
  848. slapi_rwlock_rdlock ( _pool->pl_lock );
  849. for ( bl = _pool->pl_busy_lists; bl && bl->bl_db != db; bl = bl->bl_next );
  850. slapi_rwlock_unlock ( _pool->pl_lock );
  851. if ( NULL == bl ) {
  852. if ( NULL == ( bl = clcache_new_busy_list ()) ) {
  853. rc = CL5_MEMORY_ERROR;
  854. }
  855. else {
  856. slapi_rwlock_wrlock ( _pool->pl_lock );
  857. bl->bl_db = db;
  858. bl->bl_next = _pool->pl_busy_lists;
  859. _pool->pl_busy_lists = bl;
  860. slapi_rwlock_unlock ( _pool->pl_lock );
  861. }
  862. }
  863. if ( NULL != bl ) {
  864. PR_Lock ( bl->bl_lock );
  865. buf->buf_busy_list = bl;
  866. buf->buf_next = bl->bl_buffers;
  867. bl->bl_buffers = buf;
  868. PR_Unlock ( bl->bl_lock );
  869. }
  870. return rc;
  871. }
  872. static int
  873. clcache_open_cursor ( DB_TXN *txn, CLC_Buffer *buf, DBC **cursor )
  874. {
  875. int rc;
  876. rc = buf->buf_busy_list->bl_db->cursor ( buf->buf_busy_list->bl_db, txn, cursor, 0 );
  877. if ( rc != 0 ) {
  878. slapi_log_error ( SLAPI_LOG_FATAL, get_thread_private_agmtname(),
  879. "clcache: failed to open cursor; db error - %d %s\n",
  880. rc, db_strerror(rc));
  881. }
  882. return rc;
  883. }
  884. static int
  885. clcache_cursor_get ( DBC *cursor, CLC_Buffer *buf, int flag )
  886. {
  887. int rc;
  888. rc = cursor->c_get ( cursor,
  889. & buf->buf_key,
  890. & buf->buf_data,
  891. buf->buf_load_flag | flag );
  892. if ( DB_BUFFER_SMALL == rc ) {
  893. /*
  894. * The record takes more space than the current size of the
  895. * buffer. Fortunately, buf->buf_data.size has been set by
  896. * c_get() to the actual data size needed. So we can
  897. * reallocate the data buffer and try to read again.
  898. */
  899. buf->buf_data.ulen = ( buf->buf_data.size / DEFAULT_CLC_BUFFER_PAGE_SIZE + 1 ) * DEFAULT_CLC_BUFFER_PAGE_SIZE;
  900. buf->buf_data.data = slapi_ch_realloc ( buf->buf_data.data, buf->buf_data.ulen );
  901. if ( buf->buf_data.data != NULL ) {
  902. rc = cursor->c_get ( cursor,
  903. &( buf->buf_key ),
  904. &( buf->buf_data ),
  905. buf->buf_load_flag | flag );
  906. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  907. "clcache: (%d | %d) buf key len %d reallocated and retry returns %d\n", buf->buf_load_flag, flag, buf->buf_key.size, rc );
  908. }
  909. }
  910. switch ( rc ) {
  911. case EINVAL:
  912. slapi_log_error ( SLAPI_LOG_FATAL, buf->buf_agmt_name,
  913. "clcache_cursor_get: invalid parameter\n" );
  914. break;
  915. case DB_BUFFER_SMALL:
  916. slapi_log_error ( SLAPI_LOG_FATAL, buf->buf_agmt_name,
  917. "clcache_cursor_get: can't allocate %u bytes\n", buf->buf_data.ulen );
  918. break;
  919. default:
  920. break;
  921. }
  922. return rc;
  923. }
  924. static void
  925. csn_dup_or_init_by_csn ( CSN **csn1, CSN *csn2 )
  926. {
  927. if ( *csn1 == NULL )
  928. *csn1 = csn_new();
  929. csn_init_by_csn ( *csn1, csn2 );
  930. }
  931. void
  932. clcache_destroy()
  933. {
  934. if (_pool) {
  935. CLC_Busy_List *bl = NULL;
  936. if (_pool->pl_lock) {
  937. slapi_rwlock_wrlock (_pool->pl_lock);
  938. }
  939. bl = _pool->pl_busy_lists;
  940. while (bl) {
  941. CLC_Busy_List *next = bl->bl_next;
  942. clcache_delete_busy_list(&bl);
  943. bl = next;
  944. }
  945. _pool->pl_busy_lists = NULL;
  946. _pool->pl_dbenv = NULL;
  947. if (_pool->pl_lock) {
  948. slapi_rwlock_unlock(_pool->pl_lock);
  949. slapi_destroy_rwlock(_pool->pl_lock);
  950. _pool->pl_lock = NULL;
  951. }
  952. slapi_ch_free ( (void **) &_pool );
  953. }
  954. }