cl5_clcache.c 30 KB

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