retrocl_trim.c 17 KB

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