retrocl_trim.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. #include "retrocl.h"
  42. typedef struct _trim_status {
  43. time_t ts_c_max_age; /* Constraint - max age of a changelog entry */
  44. time_t ts_s_last_trim; /* Status - last time we trimmed */
  45. int ts_s_initialized; /* Status - non-zero if initialized */
  46. int ts_s_trimming; /* non-zero if trimming in progress */
  47. PRLock *ts_s_trim_mutex; /* protects ts_s_trimming */
  48. } trim_status;
  49. static trim_status ts = {0L, 0L, 0, 0, NULL};
  50. /*
  51. * All standard changeLogEntry attributes (initialized in get_cleattrs)
  52. */
  53. static const char *cleattrs[ 10 ] = { NULL, NULL, NULL, NULL, NULL, NULL,
  54. NULL, NULL, NULL };
  55. static int retrocl_trimming = 0;
  56. static Slapi_Eq_Context retrocl_trim_ctx = NULL;
  57. /*
  58. * Function: get_cleattrs
  59. *
  60. * Returns: an array of pointers to attribute names.
  61. *
  62. * Arguments: None.
  63. *
  64. * Description: Initializes, if necessary, and returns an array of char *s
  65. * with attribute names used for retrieving changeLogEntry
  66. * entries from the directory.
  67. */
  68. static const char **get_cleattrs(void)
  69. {
  70. if ( cleattrs[ 0 ] == NULL ) {
  71. cleattrs[ 0 ] = attr_objectclass;
  72. cleattrs[ 1 ] = attr_changenumber;
  73. cleattrs[ 2 ] = attr_targetdn;
  74. cleattrs[ 3 ] = attr_changetype;
  75. cleattrs[ 4 ] = attr_newrdn;
  76. cleattrs[ 5 ] = attr_deleteoldrdn;
  77. cleattrs[ 6 ] = attr_changes;
  78. cleattrs[ 7 ] = attr_newsuperior;
  79. cleattrs[ 8 ] = attr_changetime;
  80. cleattrs[ 9 ] = NULL;
  81. }
  82. return cleattrs;
  83. }
  84. /*
  85. * Function: delete_changerecord
  86. *
  87. * Returns: LDAP_ error code
  88. *
  89. * Arguments: the number of the change to delete
  90. *
  91. * Description:
  92. *
  93. */
  94. static int
  95. delete_changerecord( changeNumber cnum )
  96. {
  97. Slapi_PBlock *pb;
  98. char *dnbuf;
  99. int delrc;
  100. dnbuf = slapi_ch_smprintf("%s=%ld, %s", attr_changenumber, cnum,
  101. RETROCL_CHANGELOG_DN);
  102. pb = slapi_pblock_new ();
  103. slapi_delete_internal_set_pb ( pb, dnbuf, NULL /*controls*/, NULL /* uniqueid */,
  104. g_plg_identity[PLUGIN_RETROCL], 0 /* actions */ );
  105. slapi_delete_internal_pb (pb);
  106. slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_RESULT, &delrc );
  107. slapi_pblock_destroy( pb );
  108. if ( delrc != LDAP_SUCCESS ) {
  109. slapi_log_error( SLAPI_LOG_FATAL, RETROCL_PLUGIN_NAME, "delete_changerecord: could not delete "
  110. "change record %lu\n", cnum );
  111. } else {
  112. slapi_log_error( SLAPI_LOG_PLUGIN, RETROCL_PLUGIN_NAME,
  113. "delete_changerecord: deleted changelog entry \"%s\"\n", dnbuf);
  114. }
  115. slapi_ch_free((void **) &dnbuf );
  116. return delrc;
  117. }
  118. /*
  119. * Function: handle_getchangerecord_result
  120. * Arguments: op - pointer to Operation struct for this operation
  121. * err - error code returned from search
  122. * Returns: nothing
  123. * Description: result handler for get_changerecord(). Sets the crt_err
  124. * field of the cnum_result_t struct to the error returned
  125. * from the backend.
  126. */
  127. static void
  128. handle_getchangerecord_result( int err, void *callback_data )
  129. {
  130. cnum_result_t *crt = callback_data;
  131. if ( crt == NULL ) {
  132. slapi_log_error( SLAPI_LOG_FATAL, RETROCL_PLUGIN_NAME,
  133. "handle_getchangerecord_result: callback_data NULL\n" );
  134. } else {
  135. crt->crt_err = err;
  136. }
  137. }
  138. /*
  139. * Function: handle_getchangerecord_search
  140. * Arguments: op - pointer to Operation struct for this operation
  141. * e - entry returned by backend
  142. * Returns: 0 in all cases
  143. * Description: Search result operation handler for get_changerecord().
  144. * Sets fields in the cnum_result_t struct pointed to by
  145. * op->o_handler_data.
  146. */
  147. static int
  148. handle_getchangerecord_search( Slapi_Entry *e, void *callback_data)
  149. {
  150. cnum_result_t *crt = callback_data;
  151. if ( crt == NULL ) {
  152. slapi_log_error( SLAPI_LOG_FATAL, RETROCL_PLUGIN_NAME,
  153. "handle_getchangerecord_search: op->o_handler_data NULL\n" );
  154. } else if ( crt->crt_nentries > 0 ) {
  155. /* only return the first entry, I guess */
  156. slapi_log_error( SLAPI_LOG_FATAL, RETROCL_PLUGIN_NAME,
  157. "handle_getchangerecord_search: multiple entries returned\n" );
  158. } else {
  159. crt->crt_nentries++;
  160. crt->crt_entry = e;
  161. }
  162. return 0;
  163. }
  164. /*
  165. * Function: get_changerecord
  166. * Arguments: cnum - number of change record to retrieve
  167. * Returns: Pointer to an entry structure. The caller must free the entry.
  168. * If "err" is non-NULL, an error code is returned in the memory
  169. * location it points to.
  170. * Description: Retrieve the change record entry whose number is "cnum".
  171. */
  172. static Slapi_Entry *get_changerecord( changeNumber cnum, int *err )
  173. {
  174. cnum_result_t crt, *crtp = &crt;
  175. char fstr[ 16 + CNUMSTR_LEN + 2 ];
  176. Slapi_PBlock *pb;
  177. if ( cnum == 0UL ) {
  178. if ( err != NULL ) {
  179. *err = LDAP_PARAM_ERROR;
  180. }
  181. return NULL;
  182. }
  183. crtp->crt_nentries = crtp->crt_err = 0; crtp->crt_entry = NULL;
  184. PR_snprintf( fstr, sizeof(fstr), "%s=%ld", attr_changenumber, cnum );
  185. pb = slapi_pblock_new ();
  186. slapi_search_internal_set_pb (pb, RETROCL_CHANGELOG_DN,
  187. LDAP_SCOPE_SUBTREE, fstr,
  188. (char **)get_cleattrs(), /* cast const */
  189. 0 /* attrsonly */,
  190. NULL /* controls */, NULL /* uniqueid */,
  191. g_plg_identity[PLUGIN_RETROCL],
  192. 0 /* actions */);
  193. slapi_search_internal_callback_pb (pb, crtp,
  194. handle_getchangerecord_result,
  195. handle_getchangerecord_search, NULL );
  196. if ( err != NULL ) {
  197. *err = crtp->crt_err;
  198. }
  199. slapi_pblock_destroy (pb);
  200. return( crtp->crt_entry );
  201. }
  202. /*
  203. * Function: trim_changelog
  204. *
  205. * Arguments: none
  206. *
  207. * Returns: 0 on success, -1 on failure
  208. *
  209. * Description: Trims the changelog, according to the constraints
  210. * described by the ts structure.
  211. */
  212. static int trim_changelog(void)
  213. {
  214. int rc = 0, ldrc, done;
  215. time_t now;
  216. changeNumber first_in_log = 0, last_in_log = 0;
  217. Slapi_Entry *e = NULL;
  218. int num_deleted = 0;
  219. int me,lt;
  220. now = current_time();
  221. PR_Lock( ts.ts_s_trim_mutex );
  222. me = ts.ts_c_max_age;
  223. lt = ts.ts_s_last_trim;
  224. PR_Unlock( ts.ts_s_trim_mutex );
  225. if ( now - lt >= (CHANGELOGDB_TRIM_INTERVAL / 1000) ) {
  226. /*
  227. * Trim the changelog. Read sequentially through all the
  228. * entries, deleting any which do not meet the criteria
  229. * described in the ts structure.
  230. */
  231. done = 0;
  232. while ( !done && retrocl_trimming == 1 ) {
  233. int did_delete;
  234. Slapi_Attr *attr;
  235. did_delete = 0;
  236. first_in_log = retrocl_get_first_changenumber();
  237. if ( 0UL == first_in_log ) {
  238. slapi_log_error( SLAPI_LOG_PLUGIN, RETROCL_PLUGIN_NAME,
  239. "trim_changelog: no changelog records "
  240. "to trim\n" );
  241. /* Bail out - we can't do any useful work */
  242. break;
  243. }
  244. last_in_log = retrocl_get_last_changenumber();
  245. if ( last_in_log == first_in_log ) {
  246. /* Always leave at least one entry in the change log */
  247. break;
  248. }
  249. if ( me > 0L ) {
  250. e = get_changerecord( first_in_log, &ldrc );
  251. if ( NULL != e ) {
  252. Slapi_Value *sval = NULL;
  253. const struct berval *val = NULL;
  254. rc = slapi_entry_attr_find( e, attr_changetime, &attr );
  255. /* Bug 624442: Logic checking for lack of timestamp was
  256. reversed. */
  257. if ( 0 != rc || slapi_attr_first_value( attr,&sval ) == -1 ||
  258. (val = slapi_value_get_berval ( sval )) == NULL ||
  259. NULL == val->bv_val ) {
  260. /* What to do if there's no timestamp? Just delete it. */
  261. retrocl_set_first_changenumber( first_in_log + 1 );
  262. ldrc = delete_changerecord( first_in_log );
  263. num_deleted++;
  264. did_delete = 1;
  265. } else {
  266. time_t change_time = parse_localTime( val->bv_val );
  267. if ( change_time + me < now ) {
  268. retrocl_set_first_changenumber( first_in_log + 1 );
  269. ldrc = delete_changerecord( first_in_log );
  270. num_deleted++;
  271. did_delete = 1;
  272. }
  273. /* slapi_entry_free( e ); */ /* XXXggood should we be freeing this? */
  274. }
  275. }
  276. }
  277. if ( !did_delete ) {
  278. done = 1;
  279. }
  280. }
  281. } else {
  282. LDAPDebug(LDAP_DEBUG_PLUGIN, "not yet time to trim: %ld < (%d+%d)\n",
  283. now,lt,(CHANGELOGDB_TRIM_INTERVAL/1000));
  284. }
  285. PR_Lock( ts.ts_s_trim_mutex );
  286. ts.ts_s_trimming = 0;
  287. ts.ts_s_last_trim = now;
  288. PR_Unlock( ts.ts_s_trim_mutex );
  289. if ( num_deleted > 0 ) {
  290. slapi_log_error( SLAPI_LOG_PLUGIN, RETROCL_PLUGIN_NAME,
  291. "trim_changelog: removed %d change records\n",
  292. num_deleted );
  293. }
  294. return rc;
  295. }
  296. static int retrocl_active_threads;
  297. /*
  298. * Function: changelog_trim_thread_fn
  299. *
  300. * Returns: nothing
  301. *
  302. * Arguments: none
  303. *
  304. * Description: the thread creation callback. retrocl_active_threads is
  305. * provided for debugging purposes.
  306. *
  307. */
  308. static void
  309. changelog_trim_thread_fn( void *arg )
  310. {
  311. PR_AtomicIncrement(&retrocl_active_threads);
  312. trim_changelog();
  313. PR_AtomicDecrement(&retrocl_active_threads);
  314. }
  315. /*
  316. * Function: retrocl_housekeeping
  317. * Arguments: cur_time - the current time
  318. * Returns: nothing
  319. * Description: Determines if it is time to trim the changelog database,
  320. * and if so, determines if the changelog database needs to
  321. * be trimmed. If so, a thread is started which will trim
  322. * the database.
  323. */
  324. void retrocl_housekeeping ( time_t cur_time, void *noarg )
  325. {
  326. int ldrc;
  327. if (retrocl_be_changelog == NULL) {
  328. LDAPDebug0Args(LDAP_DEBUG_TRACE,"not housekeeping if no cl be\n");
  329. return;
  330. }
  331. if ( !ts.ts_s_initialized ) {
  332. slapi_log_error( SLAPI_LOG_FATAL, RETROCL_PLUGIN_NAME, "changelog_housekeeping called before "
  333. "trimming constraints set\n" );
  334. return;
  335. }
  336. PR_Lock( ts.ts_s_trim_mutex );
  337. if ( !ts.ts_s_trimming ) {
  338. int must_trim = 0;
  339. /* See if we need to trim */
  340. /* Has enough time elapsed since our last check? */
  341. if ( cur_time - ts.ts_s_last_trim >= (ts.ts_c_max_age) ) {
  342. /* Is the first entry too old? */
  343. time_t first_time;
  344. /*
  345. * good we could avoid going to the database to retrieve
  346. * this time information if we cached the last value we'd read.
  347. * But a client might have deleted it over protocol.
  348. */
  349. first_time = retrocl_getchangetime( SLAPI_SEQ_FIRST, &ldrc );
  350. LDAPDebug(LDAP_DEBUG_PLUGIN,
  351. "cltrim: ldrc=%d, first_time=%ld, cur_time=%ld\n",
  352. ldrc,first_time,cur_time);
  353. if ( LDAP_SUCCESS == ldrc && first_time > (time_t) 0L &&
  354. first_time + ts.ts_c_max_age < cur_time ) {
  355. must_trim = 1;
  356. }
  357. }
  358. if ( must_trim ) {
  359. LDAPDebug0Args(LDAP_DEBUG_TRACE,"changelog about to create thread\n");
  360. /* Start a thread to trim the changelog */
  361. ts.ts_s_trimming = 1;
  362. if ( PR_CreateThread( PR_USER_THREAD,
  363. changelog_trim_thread_fn, NULL,
  364. PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD,
  365. RETROCL_DLL_DEFAULT_THREAD_STACKSIZE ) == NULL ) {
  366. slapi_log_error( SLAPI_LOG_FATAL, RETROCL_PLUGIN_NAME, "unable to create changelog trimming thread\n" );
  367. }
  368. } else {
  369. LDAPDebug0Args(LDAP_DEBUG_PLUGIN,
  370. "changelog does not need to be trimmed\n");
  371. }
  372. }
  373. PR_Unlock( ts.ts_s_trim_mutex );
  374. }
  375. /*
  376. * Function: age_str2time
  377. *
  378. * Returns: time_t
  379. *
  380. * Arguments: string representation of age (digits and unit s,m,h,d or w)
  381. *
  382. * Description:
  383. * convert time from string like 1h (1 hour) to corresponding time in seconds
  384. *
  385. */
  386. static time_t
  387. age_str2time (const char *age)
  388. {
  389. char *maxage;
  390. char unit;
  391. time_t ageval;
  392. if (age == NULL || age[0] == '\0' || strcmp (age, "0") == 0) {
  393. return 0;
  394. }
  395. maxage = slapi_ch_strdup ( age );
  396. if (!maxage) {
  397. slapi_log_error( SLAPI_LOG_PLUGIN, "retrocl",
  398. "age_str2time: Out of memory\n" );
  399. ageval = -1;
  400. goto done;
  401. }
  402. unit = maxage[ strlen( maxage ) - 1 ];
  403. maxage[ strlen( maxage ) - 1 ] = '\0';
  404. ageval = strntoul( maxage, strlen( maxage ), 10 );
  405. switch ( unit ) {
  406. case 's':
  407. break;
  408. case 'm':
  409. ageval *= 60;
  410. break;
  411. case 'h':
  412. ageval *= ( 60 * 60 );
  413. break;
  414. case 'd':
  415. ageval *= ( 24 * 60 * 60 );
  416. break;
  417. case 'w':
  418. ageval *= ( 7 * 24 * 60 * 60 );
  419. break;
  420. default:
  421. slapi_log_error( SLAPI_LOG_PLUGIN, "retrocl",
  422. "age_str2time: unknown unit \"%c\" "
  423. "for maxiumum changelog age\n", unit );
  424. ageval = -1;
  425. }
  426. done:
  427. if ( maxage) {
  428. slapi_ch_free ( (void **) &maxage );
  429. }
  430. return ageval;
  431. }
  432. /*
  433. * Function: retrocl_init_trimming
  434. *
  435. * Returns: none, exits on fatal error
  436. *
  437. * Arguments: none
  438. *
  439. * Description: called during startup
  440. *
  441. */
  442. void retrocl_init_trimming (void)
  443. {
  444. const char *cl_maxage;
  445. time_t ageval;
  446. cl_maxage = retrocl_get_config_str(CONFIG_CHANGELOG_MAXAGE_ATTRIBUTE);
  447. if (cl_maxage == NULL) {
  448. LDAPDebug0Args(LDAP_DEBUG_TRACE,"No maxage, not trimming retro changelog.\n");
  449. return;
  450. }
  451. ageval = age_str2time (cl_maxage);
  452. slapi_ch_free ((void **)&cl_maxage);
  453. ts.ts_c_max_age = ageval;
  454. ts.ts_s_last_trim = (time_t) 0L;
  455. ts.ts_s_trimming = 0;
  456. if (( ts.ts_s_trim_mutex = PR_NewLock()) == NULL ) {
  457. slapi_log_error( SLAPI_LOG_FATAL, RETROCL_PLUGIN_NAME, "set_changelog_trim_constraints: "
  458. "cannot create new lock.\n" );
  459. exit( 1 );
  460. }
  461. ts.ts_s_initialized = 1;
  462. retrocl_trimming = 1;
  463. retrocl_trim_ctx = slapi_eq_repeat(retrocl_housekeeping,
  464. NULL, (time_t)0,
  465. /* in milliseconds */
  466. CHANGELOGDB_TRIM_INTERVAL);
  467. }
  468. /*
  469. * Function: retrocl_stop_trimming
  470. *
  471. * Returns: none
  472. *
  473. * Arguments: none
  474. *
  475. * Description: called when server is shutting down to ensure trimming stops
  476. * eventually.
  477. *
  478. */
  479. void retrocl_stop_trimming(void)
  480. {
  481. retrocl_trimming = 0;
  482. if (retrocl_trim_ctx) {
  483. slapi_eq_cancel(retrocl_trim_ctx);
  484. retrocl_trim_ctx = NULL;
  485. }
  486. }