1
0

cl5_clcache.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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. PRRWLock *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 = PR_NewRWLock (PR_RWLOCK_RANK_NONE, "clcache_pl_lock");
  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. PR_RWLock_Wlock ( _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. PR_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;
  341. #if 0 /* txn control seems not improving anything so turn it off */
  342. if ( *(_pool->pl_dbenv) ) {
  343. txn_begin( *(_pool->pl_dbenv), NULL, &txn, 0 );
  344. }
  345. #endif
  346. PR_Lock ( buf->buf_busy_list->bl_lock );
  347. if ( 0 == ( rc = clcache_open_cursor ( txn, buf, &cursor )) ) {
  348. if ( flag == DB_NEXT ) {
  349. /* For bulk read, position the cursor before read the next block */
  350. rc = cursor->c_get ( cursor,
  351. & buf->buf_key,
  352. & buf->buf_data,
  353. DB_SET );
  354. }
  355. /*
  356. * Continue if the error is no-mem since we don't need to
  357. * load in the key record anyway with DB_SET.
  358. */
  359. if ( 0 == rc || DB_BUFFER_SMALL == rc )
  360. rc = clcache_cursor_get ( cursor, buf, flag );
  361. }
  362. /*
  363. * Don't keep a cursor open across the whole replication session.
  364. * That had caused noticable DB resource contention.
  365. */
  366. if ( cursor ) {
  367. cursor->c_close ( cursor );
  368. }
  369. #if 0 /* txn control seems not improving anything so turn it off */
  370. if ( txn ) {
  371. txn->commit ( txn, DB_TXN_NOSYNC );
  372. }
  373. #endif
  374. PR_Unlock ( buf->buf_busy_list->bl_lock );
  375. buf->buf_record_ptr = NULL;
  376. if ( 0 == rc ) {
  377. DB_MULTIPLE_INIT ( buf->buf_record_ptr, &buf->buf_data );
  378. if ( NULL == buf->buf_record_ptr )
  379. rc = DB_NOTFOUND;
  380. else
  381. buf->buf_load_cnt++;
  382. }
  383. return rc;
  384. }
  385. /*
  386. * Gets the next change from the buffer.
  387. * *key : output - key of the next change, or NULL if no more change
  388. * *data: output - data of the next change, or NULL if no more change
  389. */
  390. int
  391. clcache_get_next_change ( CLC_Buffer *buf, void **key, size_t *keylen, void **data, size_t *datalen, CSN **csn )
  392. {
  393. int skip = 1;
  394. int rc = 0;
  395. do {
  396. *key = *data = NULL;
  397. *keylen = *datalen = 0;
  398. if ( buf->buf_record_ptr ) {
  399. DB_MULTIPLE_KEY_NEXT ( buf->buf_record_ptr, &buf->buf_data,
  400. *key, *keylen, *data, *datalen );
  401. }
  402. /*
  403. * We're done with the current buffer. Now load the next chunk.
  404. */
  405. if ( NULL == *key && CLC_STATE_READY == buf->buf_state ) {
  406. rc = clcache_load_buffer ( buf, NULL, DB_NEXT );
  407. if ( 0 == rc && buf->buf_record_ptr ) {
  408. DB_MULTIPLE_KEY_NEXT ( buf->buf_record_ptr, &buf->buf_data,
  409. *key, *keylen, *data, *datalen );
  410. }
  411. }
  412. /* Compare the new change to the local and remote RUVs */
  413. if ( NULL != *key ) {
  414. buf->buf_record_cnt++;
  415. csn_init_by_string ( buf->buf_current_csn, (char*)*key );
  416. skip = clcache_skip_change ( buf );
  417. if (skip) buf->buf_record_skipped++;
  418. }
  419. }
  420. while ( rc == 0 && *key && skip );
  421. if ( NULL == *key ) {
  422. *key = NULL;
  423. *csn = NULL;
  424. rc = DB_NOTFOUND;
  425. }
  426. else {
  427. *csn = buf->buf_current_csn;
  428. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  429. "load=%d rec=%d csn=%s\n",
  430. buf->buf_load_cnt, buf->buf_record_cnt, (char*)*key );
  431. }
  432. return rc;
  433. }
  434. static void
  435. clcache_refresh_consumer_maxcsns ( CLC_Buffer *buf )
  436. {
  437. int i;
  438. for ( i = 0; i < buf->buf_num_cscbs; i++ ) {
  439. csn_free(&buf->buf_cscbs[i]->consumer_maxcsn);
  440. ruv_get_largest_csn_for_replica (
  441. buf->buf_consumer_ruv,
  442. buf->buf_cscbs[i]->rid,
  443. &buf->buf_cscbs[i]->consumer_maxcsn );
  444. }
  445. }
  446. static int
  447. clcache_refresh_local_maxcsn ( const ruv_enum_data *rid_data, void *data )
  448. {
  449. CLC_Buffer *buf = (CLC_Buffer*) data;
  450. ReplicaId rid;
  451. int rc = 0;
  452. int i;
  453. rid = csn_get_replicaid ( rid_data->csn );
  454. /*
  455. * No need to create cscb for consumer's RID.
  456. * If RID==65535, the CSN is originated from a
  457. * legacy consumer. In this case the supplier
  458. * and the consumer may have the same RID.
  459. */
  460. if ( rid == buf->buf_consumer_rid && rid != MAX_REPLICA_ID )
  461. return rc;
  462. for ( i = 0; i < buf->buf_num_cscbs; i++ ) {
  463. if ( buf->buf_cscbs[i]->rid == rid )
  464. break;
  465. }
  466. if ( i >= buf->buf_num_cscbs ) {
  467. buf->buf_cscbs[i] = clcache_new_cscb ();
  468. if ( buf->buf_cscbs[i] == NULL ) {
  469. return -1;
  470. }
  471. buf->buf_cscbs[i]->rid = rid;
  472. buf->buf_num_cscbs++;
  473. }
  474. csn_dup_or_init_by_csn ( &buf->buf_cscbs[i]->local_maxcsn, rid_data->csn );
  475. if ( buf->buf_cscbs[i]->consumer_maxcsn &&
  476. csn_compare (buf->buf_cscbs[i]->consumer_maxcsn, rid_data->csn) >= 0 ) {
  477. /* No change need to be sent for this RID */
  478. buf->buf_cscbs[i]->state = CLC_STATE_UP_TO_DATE;
  479. }
  480. return rc;
  481. }
  482. static int
  483. clcache_refresh_local_maxcsns ( CLC_Buffer *buf )
  484. {
  485. int i;
  486. for ( i = 0; i < buf->buf_num_cscbs; i++ ) {
  487. csn_dup_or_init_by_csn ( &buf->buf_cscbs[i]->prev_local_maxcsn,
  488. buf->buf_cscbs[i]->local_maxcsn );
  489. }
  490. return ruv_enumerate_elements ( buf->buf_local_ruv, clcache_refresh_local_maxcsn, buf );
  491. }
  492. /*
  493. * Algorithm:
  494. *
  495. * 1. Snapshot local RUVs;
  496. * 2. Load buffer;
  497. * 3. Send to the consumer only those CSNs that are covered
  498. * by the RUVs snapshot taken in the first step;
  499. * All CSNs that are covered by the RUVs snapshot taken in the
  500. * first step are guaranteed in consecutive order for the respected
  501. * RIDs because of the the CSN pending list control;
  502. * A CSN that is not covered by the RUVs snapshot may be out of order
  503. * since it is possible that a smaller CSN might not have committed
  504. * yet by the time the buffer was loaded.
  505. * 4. Determine anchorcsn for each RID:
  506. *
  507. * Case| Local vs. Buffer | New Local | Next
  508. * | MaxCSN MaxCSN | MaxCSN | Anchor-CSN
  509. * ----+-------------------+-----------+----------------
  510. * 1 | Cl >= Cb | * | Cb
  511. * 2 | Cl < Cb | Cl | Cb
  512. * 3 | Cl < Cb | Cl2 | Cl
  513. *
  514. * 5. Determine anchorcsn for next load:
  515. * Anchor-CSN = min { all Next-Anchor-CSN, Buffer-MaxCSN }
  516. */
  517. static int
  518. clcache_adjust_anchorcsn ( CLC_Buffer *buf )
  519. {
  520. PRBool hasChange = PR_FALSE;
  521. struct csn_seq_ctrl_block *cscb;
  522. int i;
  523. if ( buf->buf_state == CLC_STATE_READY ) {
  524. for ( i = 0; i < buf->buf_num_cscbs; i++ ) {
  525. cscb = buf->buf_cscbs[i];
  526. if ( cscb->state == CLC_STATE_UP_TO_DATE )
  527. continue;
  528. /*
  529. * Case 3 unsafe ruv change: next buffer load should start
  530. * from where the maxcsn in the old ruv was. Since each
  531. * cscb has remembered the maxcsn sent to the consumer,
  532. * CSNs that may be loaded again could easily be skipped.
  533. */
  534. if ( cscb->prev_local_maxcsn &&
  535. csn_compare (cscb->prev_local_maxcsn, buf->buf_current_csn) < 0 &&
  536. csn_compare (cscb->local_maxcsn, cscb->prev_local_maxcsn) != 0 ) {
  537. hasChange = PR_TRUE;
  538. cscb->state = CLC_STATE_READY;
  539. csn_init_by_csn ( buf->buf_current_csn, cscb->prev_local_maxcsn );
  540. csn_as_string ( cscb->prev_local_maxcsn, 0, (char*)buf->buf_key.data );
  541. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  542. "adjust anchor csn upon %s\n",
  543. ( cscb->state == CLC_STATE_CSN_GT_RUV ? "out of sequence csn" : "unsafe ruv change") );
  544. continue;
  545. }
  546. /*
  547. * check if there are still changes to send for this RID
  548. * Assume we had compared the local maxcsn and the consumer
  549. * max csn before this function was called and hence the
  550. * cscb->state had been set accordingly.
  551. */
  552. if ( hasChange == PR_FALSE &&
  553. csn_compare (cscb->local_maxcsn, buf->buf_current_csn) > 0 ) {
  554. hasChange = PR_TRUE;
  555. }
  556. }
  557. }
  558. if ( !hasChange ) {
  559. buf->buf_state = CLC_STATE_DONE;
  560. }
  561. return buf->buf_state;
  562. }
  563. static int
  564. clcache_skip_change ( CLC_Buffer *buf )
  565. {
  566. struct csn_seq_ctrl_block *cscb = NULL;
  567. ReplicaId rid;
  568. int skip = 1;
  569. int i;
  570. do {
  571. rid = csn_get_replicaid ( buf->buf_current_csn );
  572. /*
  573. * Skip CSN that is originated from the consumer.
  574. * If RID==65535, the CSN is originated from a
  575. * legacy consumer. In this case the supplier
  576. * and the consumer may have the same RID.
  577. */
  578. if (rid == buf->buf_consumer_rid && rid != MAX_REPLICA_ID)
  579. break;
  580. /* Skip helper entry (ENTRY_COUNT, PURGE_RUV and so on) */
  581. if ( cl5HelperEntry ( NULL, buf->buf_current_csn ) == PR_TRUE ) {
  582. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  583. "Skip helper entry type=%ld\n", csn_get_time( buf->buf_current_csn ));
  584. break;
  585. }
  586. /* Find csn sequence control block for the current rid */
  587. for (i = 0; i < buf->buf_num_cscbs && buf->buf_cscbs[i]->rid != rid; i++);
  588. /* Skip CSN whose RID is unknown to the local RUV snapshot */
  589. if ( i >= buf->buf_num_cscbs ) {
  590. buf->buf_state = CLC_STATE_NEW_RID;
  591. break;
  592. }
  593. cscb = buf->buf_cscbs[i];
  594. /* Skip if the consumer is already up-to-date for the RID */
  595. if ( cscb->state == CLC_STATE_UP_TO_DATE ) {
  596. break;
  597. }
  598. /* Skip CSN whose preceedents are not covered by local RUV snapshot */
  599. if ( cscb->state == CLC_STATE_CSN_GT_RUV ) {
  600. break;
  601. }
  602. /* Skip CSNs already covered by consumer RUV */
  603. if ( cscb->consumer_maxcsn &&
  604. csn_compare ( buf->buf_current_csn, cscb->consumer_maxcsn ) <= 0 ) {
  605. break;
  606. }
  607. /* Send CSNs that are covered by the local RUV snapshot */
  608. if ( csn_compare ( buf->buf_current_csn, cscb->local_maxcsn ) <= 0 ) {
  609. skip = 0;
  610. csn_dup_or_init_by_csn ( &cscb->consumer_maxcsn, buf->buf_current_csn );
  611. break;
  612. }
  613. /*
  614. * Promote the local maxcsn to its next neighbor
  615. * to keep the current session going. Skip if we
  616. * are not sure if current_csn is the neighbor.
  617. */
  618. if ( csn_time_difference(buf->buf_current_csn, cscb->local_maxcsn) == 0 &&
  619. (csn_get_seqnum(buf->buf_current_csn) ==
  620. csn_get_seqnum(cscb->local_maxcsn) + 1) ) {
  621. csn_init_by_csn ( cscb->local_maxcsn, buf->buf_current_csn );
  622. csn_init_by_csn ( cscb->consumer_maxcsn, buf->buf_current_csn );
  623. skip = 0;
  624. break;
  625. }
  626. /* Skip CSNs not covered by local RUV snapshot */
  627. cscb->state = CLC_STATE_CSN_GT_RUV;
  628. } while (0);
  629. #ifdef DEBUG
  630. if (skip && cscb) {
  631. char consumer[24] = {'\0'};
  632. char local[24] = {'\0'};
  633. char current[24] = {'\0'};
  634. if ( cscb->consumer_maxcsn )
  635. csn_as_string ( cscb->consumer_maxcsn, PR_FALSE, consumer );
  636. if ( cscb->local_maxcsn )
  637. csn_as_string ( cscb->local_maxcsn, PR_FALSE, local );
  638. csn_as_string ( buf->buf_current_csn, PR_FALSE, current );
  639. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  640. "Skip %s consumer=%s local=%s\n", current, consumer, local );
  641. }
  642. #endif
  643. return skip;
  644. }
  645. static struct csn_seq_ctrl_block *
  646. clcache_new_cscb ()
  647. {
  648. struct csn_seq_ctrl_block *cscb;
  649. cscb = (struct csn_seq_ctrl_block *) slapi_ch_calloc ( 1, sizeof (struct csn_seq_ctrl_block) );
  650. if (cscb == NULL) {
  651. slapi_log_error ( SLAPI_LOG_FATAL, NULL, "clcache: malloc failure\n" );
  652. }
  653. return cscb;
  654. }
  655. static void
  656. clcache_free_cscb ( struct csn_seq_ctrl_block ** cscb )
  657. {
  658. csn_free ( & (*cscb)->consumer_maxcsn );
  659. csn_free ( & (*cscb)->local_maxcsn );
  660. csn_free ( & (*cscb)->prev_local_maxcsn );
  661. slapi_ch_free ( (void **) cscb );
  662. }
  663. /*
  664. * Allocate and initialize a new buffer
  665. * It is called when there is a request for a buffer while
  666. * buffer free list is empty.
  667. */
  668. static CLC_Buffer *
  669. clcache_new_buffer ( ReplicaId consumer_rid )
  670. {
  671. CLC_Buffer *buf = NULL;
  672. int welldone = 0;
  673. do {
  674. buf = (CLC_Buffer*) slapi_ch_calloc (1, sizeof(CLC_Buffer));
  675. if ( NULL == buf )
  676. break;
  677. buf->buf_key.flags = DB_DBT_USERMEM;
  678. buf->buf_key.ulen = CSN_STRSIZE + 1;
  679. buf->buf_key.size = CSN_STRSIZE;
  680. buf->buf_key.data = slapi_ch_calloc( 1, buf->buf_key.ulen );
  681. if ( NULL == buf->buf_key.data )
  682. break;
  683. buf->buf_data.flags = DB_DBT_USERMEM;
  684. buf->buf_data.ulen = _pool->pl_buffer_default_pages * DEFAULT_CLC_BUFFER_PAGE_SIZE;
  685. buf->buf_data.data = slapi_ch_malloc( buf->buf_data.ulen );
  686. if ( NULL == buf->buf_data.data )
  687. break;
  688. if ( NULL == ( buf->buf_current_csn = csn_new()) )
  689. break;
  690. buf->buf_state = CLC_STATE_READY;
  691. buf->buf_agmt_name = get_thread_private_agmtname();
  692. buf->buf_consumer_rid = consumer_rid;
  693. buf->buf_num_cscbs = 0;
  694. welldone = 1;
  695. } while (0);
  696. if ( !welldone ) {
  697. clcache_delete_buffer ( &buf );
  698. }
  699. return buf;
  700. }
  701. /*
  702. * Deallocates a buffer.
  703. * It is called when a buffer is returned to the buffer pool
  704. * and the pool size is over the limit.
  705. */
  706. static void
  707. clcache_delete_buffer ( CLC_Buffer **buf )
  708. {
  709. if ( buf && *buf ) {
  710. slapi_ch_free (&( (*buf)->buf_key.data ));
  711. slapi_ch_free (&( (*buf)->buf_data.data ));
  712. csn_free (&( (*buf)->buf_current_csn ));
  713. csn_free (&( (*buf)->buf_missing_csn ));
  714. slapi_ch_free ( (void **) buf );
  715. }
  716. }
  717. static CLC_Busy_List *
  718. clcache_new_busy_list ()
  719. {
  720. CLC_Busy_List *bl;
  721. int welldone = 0;
  722. do {
  723. if ( NULL == (bl = ( CLC_Busy_List* ) slapi_ch_calloc (1, sizeof(CLC_Busy_List)) ))
  724. break;
  725. if ( NULL == (bl->bl_lock = PR_NewLock ()) )
  726. break;
  727. /*
  728. if ( NULL == (bl->bl_max_csn = csn_new ()) )
  729. break;
  730. */
  731. welldone = 1;
  732. }
  733. while (0);
  734. if ( !welldone ) {
  735. clcache_delete_busy_list ( &bl );
  736. }
  737. return bl;
  738. }
  739. static void
  740. clcache_delete_busy_list ( CLC_Busy_List **bl )
  741. {
  742. if ( bl && *bl ) {
  743. CLC_Buffer *buf = NULL;
  744. if ( (*bl)->bl_lock ) {
  745. PR_Lock ( (*bl)->bl_lock );
  746. }
  747. buf = (*bl)->bl_buffers;
  748. while (buf) {
  749. CLC_Buffer *next = buf->buf_next;
  750. clcache_delete_buffer(&buf);
  751. buf = next;
  752. }
  753. (*bl)->bl_buffers = NULL;
  754. (*bl)->bl_db = NULL;
  755. if ( (*bl)->bl_lock ) {
  756. PR_Unlock ( (*bl)->bl_lock );
  757. PR_DestroyLock ( (*bl)->bl_lock );
  758. (*bl)->bl_lock = NULL;
  759. }
  760. /* csn_free (&( (*bl)->bl_max_csn )); */
  761. slapi_ch_free ( (void **) bl );
  762. }
  763. }
  764. static int
  765. clcache_enqueue_busy_list ( DB *db, CLC_Buffer *buf )
  766. {
  767. CLC_Busy_List *bl;
  768. int rc = 0;
  769. PR_RWLock_Rlock ( _pool->pl_lock );
  770. for ( bl = _pool->pl_busy_lists; bl && bl->bl_db != db; bl = bl->bl_next );
  771. PR_RWLock_Unlock ( _pool->pl_lock );
  772. if ( NULL == bl ) {
  773. if ( NULL == ( bl = clcache_new_busy_list ()) ) {
  774. rc = CL5_MEMORY_ERROR;
  775. }
  776. else {
  777. PR_RWLock_Wlock ( _pool->pl_lock );
  778. bl->bl_db = db;
  779. bl->bl_next = _pool->pl_busy_lists;
  780. _pool->pl_busy_lists = bl;
  781. PR_RWLock_Unlock ( _pool->pl_lock );
  782. }
  783. }
  784. if ( NULL != bl ) {
  785. PR_Lock ( bl->bl_lock );
  786. buf->buf_busy_list = bl;
  787. buf->buf_next = bl->bl_buffers;
  788. bl->bl_buffers = buf;
  789. PR_Unlock ( bl->bl_lock );
  790. }
  791. return rc;
  792. }
  793. static int
  794. clcache_open_cursor ( DB_TXN *txn, CLC_Buffer *buf, DBC **cursor )
  795. {
  796. int rc;
  797. rc = buf->buf_busy_list->bl_db->cursor ( buf->buf_busy_list->bl_db, txn, cursor, 0 );
  798. if ( rc != 0 ) {
  799. slapi_log_error ( SLAPI_LOG_FATAL, get_thread_private_agmtname(),
  800. "clcache: failed to open cursor; db error - %d %s\n",
  801. rc, db_strerror(rc));
  802. }
  803. return rc;
  804. }
  805. static int
  806. clcache_cursor_get ( DBC *cursor, CLC_Buffer *buf, int flag )
  807. {
  808. int rc;
  809. rc = cursor->c_get ( cursor,
  810. & buf->buf_key,
  811. & buf->buf_data,
  812. buf->buf_load_flag | flag );
  813. if ( DB_BUFFER_SMALL == rc ) {
  814. /*
  815. * The record takes more space than the current size of the
  816. * buffer. Fortunately, buf->buf_data.size has been set by
  817. * c_get() to the actual data size needed. So we can
  818. * reallocate the data buffer and try to read again.
  819. */
  820. buf->buf_data.ulen = ( buf->buf_data.size / DEFAULT_CLC_BUFFER_PAGE_SIZE + 1 ) * DEFAULT_CLC_BUFFER_PAGE_SIZE;
  821. buf->buf_data.data = slapi_ch_realloc ( buf->buf_data.data, buf->buf_data.ulen );
  822. if ( buf->buf_data.data != NULL ) {
  823. rc = cursor->c_get ( cursor,
  824. &( buf->buf_key ),
  825. &( buf->buf_data ),
  826. buf->buf_load_flag | flag );
  827. slapi_log_error ( SLAPI_LOG_REPL, buf->buf_agmt_name,
  828. "clcache: (%d | %d) buf key len %d reallocated and retry returns %d\n", buf->buf_load_flag, flag, buf->buf_key.size, rc );
  829. }
  830. }
  831. switch ( rc ) {
  832. case EINVAL:
  833. slapi_log_error ( SLAPI_LOG_FATAL, buf->buf_agmt_name,
  834. "clcache_cursor_get: invalid parameter\n" );
  835. break;
  836. case DB_BUFFER_SMALL:
  837. slapi_log_error ( SLAPI_LOG_FATAL, buf->buf_agmt_name,
  838. "clcache_cursor_get: can't allocate %u bytes\n", buf->buf_data.ulen );
  839. break;
  840. default:
  841. break;
  842. }
  843. return rc;
  844. }
  845. static void
  846. csn_dup_or_init_by_csn ( CSN **csn1, CSN *csn2 )
  847. {
  848. if ( *csn1 == NULL )
  849. *csn1 = csn_new();
  850. csn_init_by_csn ( *csn1, csn2 );
  851. }
  852. void
  853. clcache_destroy()
  854. {
  855. if (_pool) {
  856. CLC_Busy_List *bl = NULL;
  857. if (_pool->pl_lock) {
  858. PR_RWLock_Wlock (_pool->pl_lock);
  859. }
  860. bl = _pool->pl_busy_lists;
  861. while (bl) {
  862. CLC_Busy_List *next = bl->bl_next;
  863. clcache_delete_busy_list(&bl);
  864. bl = next;
  865. }
  866. _pool->pl_busy_lists = NULL;
  867. _pool->pl_dbenv = NULL;
  868. if (_pool->pl_lock) {
  869. PR_RWLock_Unlock(_pool->pl_lock);
  870. PR_DestroyRWLock(_pool->pl_lock);
  871. _pool->pl_lock = NULL;
  872. }
  873. slapi_ch_free ( (void **) &_pool );
  874. }
  875. }