retrocl_cn.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. #include "retrocl.h"
  13. static changeNumber retrocl_internal_cn = 0;
  14. static changeNumber retrocl_first_cn = 0;
  15. static int check_last_changenumber = 0;
  16. /*
  17. * Function: a2changeNumber
  18. *
  19. * Returns: changeNumber (long)
  20. *
  21. * Arguments: string
  22. *
  23. * Description: parses the string to a changenumber. changenumbers are
  24. * positive integers.
  25. *
  26. */
  27. static changeNumber a2changeNumber (const char *p)
  28. {
  29. changeNumber c;
  30. c = strntoul((char *)p,strlen(p),10);
  31. return c;
  32. }
  33. /*
  34. * Function: handle_cnum_entry
  35. * Arguments: op - pointer to Operation struct for this operation
  36. * e - pointer to returned entry.
  37. * Returns: nothing
  38. * Description: Search result handler for retrocl_getchangenum(). Sets the
  39. * op->o_handler_data to point to a structure which contains
  40. * the changenumber retrieved and an error code.
  41. */
  42. static int
  43. handle_cnum_entry( Slapi_Entry *e, void *callback_data )
  44. {
  45. cnumRet *cr = (cnumRet *)callback_data;
  46. Slapi_Value *sval=NULL;
  47. const struct berval *value;
  48. cr->cr_cnum = 0UL;
  49. cr->cr_time = NULL;
  50. if ( NULL != e ) {
  51. Slapi_Attr *chattr = NULL;
  52. sval = NULL;
  53. value = NULL;
  54. if ( slapi_entry_attr_find( e, attr_changenumber, &chattr ) == 0 ) {
  55. slapi_attr_first_value( chattr,&sval );
  56. if ( NULL != sval ) {
  57. value = slapi_value_get_berval ( sval );
  58. if( NULL != value && NULL != value->bv_val &&
  59. '\0' != value->bv_val[0]) {
  60. cr->cr_cnum = a2changeNumber( value->bv_val );
  61. }
  62. }
  63. }
  64. chattr = NULL;
  65. sval = NULL;
  66. value = NULL;
  67. chattr = NULL;
  68. sval = NULL;
  69. value = NULL;
  70. if ( slapi_entry_attr_find( e, attr_changetime, &chattr ) == 0 ) {
  71. slapi_attr_first_value( chattr,&sval );
  72. if ( NULL != sval) {
  73. value = slapi_value_get_berval ( sval );
  74. if (NULL != value && NULL != value->bv_val &&
  75. '\0' != value->bv_val[0]) {
  76. cr->cr_time = slapi_ch_strdup( value->bv_val );
  77. }
  78. }
  79. }
  80. }
  81. return 0;
  82. }
  83. /*
  84. * Function: handle_cnum_result
  85. * Arguments: err - error code returned from search
  86. * callback_data - private data for callback
  87. * Returns: nothing
  88. * Description: result handler for retrocl_getchangenum(). Sets the cr_lderr
  89. * field of the cnumRet struct to the error returned
  90. * from the backend.
  91. */
  92. static void
  93. handle_cnum_result( int err, void *callback_data )
  94. {
  95. cnumRet *cr = (cnumRet *)callback_data;
  96. cr->cr_lderr = err;
  97. }
  98. /*
  99. * Function: retrocl_get_changenumbers
  100. *
  101. * Returns: 0/-1
  102. *
  103. * Arguments: none
  104. *
  105. * Description: reads the first and last entry in the changelog to obtain
  106. * the starting and ending change numbers.
  107. *
  108. */
  109. int retrocl_get_changenumbers(void)
  110. {
  111. cnumRet cr;
  112. if (retrocl_be_changelog == NULL) return -1;
  113. cr.cr_cnum = 0;
  114. cr.cr_time = 0;
  115. slapi_seq_callback(RETROCL_CHANGELOG_DN,SLAPI_SEQ_FIRST,
  116. (char *)attr_changenumber, /* cast away const */
  117. NULL,NULL,0,&cr,NULL,handle_cnum_result,
  118. handle_cnum_entry, NULL);
  119. slapi_rwlock_wrlock(retrocl_cn_lock);
  120. retrocl_first_cn = cr.cr_cnum;
  121. slapi_ch_free(( void **) &cr.cr_time );
  122. slapi_seq_callback(RETROCL_CHANGELOG_DN,SLAPI_SEQ_LAST,
  123. (char *)attr_changenumber, /* cast away const */
  124. NULL,NULL,0,&cr,NULL,handle_cnum_result,
  125. handle_cnum_entry, NULL);
  126. retrocl_internal_cn = cr.cr_cnum;
  127. slapi_log_error(SLAPI_LOG_PLUGIN,"retrocl","Got changenumbers %lu and %lu\n",
  128. retrocl_first_cn,
  129. retrocl_internal_cn);
  130. slapi_rwlock_unlock(retrocl_cn_lock);
  131. slapi_ch_free(( void **) &cr.cr_time );
  132. return 0;
  133. }
  134. /*
  135. * Function: retrocl_getchangetime
  136. * Arguments: type - one of SLAPI_SEQ_FIRST, SLAPI_SEQ_LAST
  137. * Returns: The time of the requested change record. If the return value is
  138. * NO_TIME, the changelog could not be read.
  139. * If err is non-NULL, the memory it points to is set the the
  140. * error code returned from the backend. If "type" is not valid,
  141. * *err is set to -1.
  142. * Description: Get the first or last changenumber stored in the changelog,
  143. * depending on the value of argument "type".
  144. */
  145. time_t retrocl_getchangetime( int type, int *err )
  146. {
  147. cnumRet cr;
  148. time_t ret;
  149. if ( type != SLAPI_SEQ_FIRST && type != SLAPI_SEQ_LAST ) {
  150. if ( err != NULL ) {
  151. *err = -1;
  152. }
  153. return NO_TIME;
  154. }
  155. memset( &cr, '\0', sizeof( cnumRet ));
  156. slapi_seq_callback( RETROCL_CHANGELOG_DN, type,
  157. (char *)attr_changenumber, /* cast away const */
  158. NULL,
  159. NULL, 0, &cr, NULL,
  160. handle_cnum_result, handle_cnum_entry, NULL );
  161. if ( err != NULL ) {
  162. *err = cr.cr_lderr;
  163. }
  164. if ( NULL == cr.cr_time ) {
  165. ret = NO_TIME;
  166. } else {
  167. ret = parse_localTime( cr.cr_time );
  168. }
  169. slapi_ch_free(( void **) &cr.cr_time );
  170. return ret;
  171. }
  172. /*
  173. * Function: retrocl_forget_changenumbers
  174. *
  175. * Returns: none
  176. *
  177. * Arguments: none
  178. *
  179. * Description: used only when the server is shutting down
  180. *
  181. */
  182. void retrocl_forget_changenumbers(void)
  183. {
  184. slapi_rwlock_wrlock(retrocl_cn_lock);
  185. retrocl_first_cn = 0;
  186. retrocl_internal_cn = 0;
  187. slapi_rwlock_unlock(retrocl_cn_lock);
  188. }
  189. /*
  190. * Function: retrocl_get_first_changenumber
  191. *
  192. * Returns: changeNumber
  193. *
  194. * Arguments: none
  195. *
  196. * Description: used in root DSE
  197. *
  198. */
  199. changeNumber retrocl_get_first_changenumber(void)
  200. {
  201. changeNumber cn;
  202. slapi_rwlock_rdlock(retrocl_cn_lock);
  203. cn = retrocl_first_cn;
  204. slapi_rwlock_unlock(retrocl_cn_lock);
  205. return cn;
  206. }
  207. /*
  208. * Function: retrocl_set_first_changenumber
  209. *
  210. * Returns: none
  211. *
  212. * Arguments: changenumber
  213. *
  214. * Description: used in changelog trimming
  215. *
  216. */
  217. void retrocl_set_first_changenumber(changeNumber cn)
  218. {
  219. slapi_rwlock_wrlock(retrocl_cn_lock);
  220. retrocl_first_cn = cn;
  221. slapi_rwlock_unlock(retrocl_cn_lock);
  222. }
  223. /*
  224. * Function: retrocl_get_last_changenumber
  225. *
  226. * Returns:
  227. *
  228. * Arguments:
  229. *
  230. * Description: used in root DSE
  231. *
  232. */
  233. changeNumber retrocl_get_last_changenumber(void)
  234. {
  235. changeNumber cn;
  236. slapi_rwlock_rdlock(retrocl_cn_lock);
  237. cn = retrocl_internal_cn;
  238. slapi_rwlock_unlock(retrocl_cn_lock);
  239. return cn;
  240. }
  241. /*
  242. * Function: retrocl_commit_changenumber
  243. *
  244. * Returns: none
  245. *
  246. * Arguments: none, lock must be held
  247. *
  248. * Description: NOTE! MUST BE PRECEEDED BY retrocl_assign_changenumber
  249. *
  250. */
  251. void retrocl_commit_changenumber(void)
  252. {
  253. slapi_rwlock_wrlock(retrocl_cn_lock);
  254. if ( retrocl_first_cn == 0) {
  255. retrocl_first_cn = retrocl_internal_cn;
  256. }
  257. slapi_rwlock_unlock(retrocl_cn_lock);
  258. }
  259. /*
  260. * Function: retrocl_release_changenumber
  261. *
  262. * Returns: none
  263. *
  264. * Arguments: none, lock must be held
  265. *
  266. * Description: NOTE! MUST BE PRECEEDED BY retrocl_assign_changenumber
  267. *
  268. */
  269. void retrocl_release_changenumber(void)
  270. {
  271. slapi_rwlock_wrlock(retrocl_cn_lock);
  272. retrocl_internal_cn--;
  273. slapi_rwlock_unlock(retrocl_cn_lock);
  274. }
  275. /*
  276. * Function: retrocl_update_lastchangenumber
  277. *
  278. * Returns: 0/-1
  279. *
  280. * Arguments: none. The caller should have taken write lock for the change numbers
  281. *
  282. * Description: reads the last entry in the changelog to obtain
  283. * the last change number.
  284. *
  285. */
  286. int retrocl_update_lastchangenumber(void)
  287. {
  288. cnumRet cr;
  289. if (retrocl_be_changelog == NULL) return -1;
  290. slapi_rwlock_unlock(retrocl_cn_lock);
  291. cr.cr_cnum = 0;
  292. cr.cr_time = 0;
  293. slapi_seq_callback(RETROCL_CHANGELOG_DN,SLAPI_SEQ_LAST,
  294. (char *)attr_changenumber, /* cast away const */
  295. NULL,NULL,0,&cr,NULL,handle_cnum_result,
  296. handle_cnum_entry, NULL);
  297. slapi_rwlock_wrlock(retrocl_cn_lock);
  298. retrocl_internal_cn = cr.cr_cnum;
  299. slapi_log_error(SLAPI_LOG_PLUGIN,"retrocl","Refetched last changenumber = %lu \n",
  300. retrocl_internal_cn);
  301. slapi_ch_free(( void **) &cr.cr_time );
  302. return 0;
  303. }
  304. /*
  305. * Function: retrocl_assign_changenumber
  306. *
  307. * Returns: change number, 0 on error
  308. *
  309. * Arguments: none. Lock must be held.
  310. *
  311. * Description: NOTE! MUST BE FOLLOWED BY retrocl_commit_changenumber or
  312. * retrocl_release_changenumber
  313. *
  314. */
  315. changeNumber retrocl_assign_changenumber(void)
  316. {
  317. changeNumber cn;
  318. /* Before we assign the changenumber; we should check for the
  319. * validity of the internal assignment of retrocl_internal_cn
  320. * we had from the startup */
  321. slapi_rwlock_wrlock(retrocl_cn_lock);
  322. if((check_last_changenumber) ||
  323. ((retrocl_internal_cn <= retrocl_first_cn) &&
  324. (retrocl_internal_cn > 1 ))){
  325. /* the numbers have become out of sync - retrocl_get_changenumbers
  326. * gets called only once during startup and it may have had a problem
  327. * getting the last changenumber.
  328. * If there was any problem then update the lastchangenumber from the changelog db.
  329. * This function is being called by only the thread that is actually writing
  330. * to the changelog.
  331. *
  332. * after the first change was applied both _cn numbers are 1, that's ok
  333. */
  334. retrocl_update_lastchangenumber();
  335. check_last_changenumber = 0;
  336. }
  337. retrocl_internal_cn++;
  338. cn = retrocl_internal_cn;
  339. slapi_rwlock_unlock(retrocl_cn_lock);
  340. return cn;
  341. }
  342. void retrocl_set_check_changenumber(void)
  343. {
  344. slapi_rwlock_wrlock(retrocl_cn_lock);
  345. check_last_changenumber = 1;
  346. slapi_rwlock_unlock(retrocl_cn_lock);
  347. }