repl5_schedule.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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. /* repl5_schedule.c */
  13. /*
  14. The schedule object implements the scheduling policy for a DS 5.0 replication
  15. supplier.
  16. Methods:
  17. schedule_set() - sets the schedule
  18. schedule_get() - gets the schedule
  19. schedule_in_window_now() - returns TRUE if a replication session
  20. should commence.
  21. schedule_next() - returns the next time that replication is
  22. scheduled to occur.
  23. schedule_notify() - called to inform the scheduler when entries
  24. have been updated.
  25. schedule_set_priority_attributes() - sets the attributes that are
  26. considered "high priority". A modification to one of these attributes
  27. will cause replication to commence asap, overriding the startup
  28. delay and maximum backlog. Also includes an additional parameter
  29. that controls whether priority attributes are propagated regardless
  30. of the scheduling window, e.g. it's possible to configure things
  31. so that password changes get propagated even if we're not in a
  32. replication window.
  33. schedule_set_startup_delay() - sets the time that replication should
  34. wait before commencing replication sessions.
  35. schedule_set_maximum_backlog() - sets the maximum number of updates
  36. which can occur before replication will commence. If the backlog
  37. threshhold is exceeded, then replication will commence ASAP,
  38. overriding the startup delay.
  39. */
  40. /* ONREPL - I made a simplifying assumption that a schedule item does not
  41. cross day boundaries. Implementing this is hard because we search
  42. for the items for a particular day only based on the item's staring time.
  43. For instance if the current time is tuesday morning, we would not consider
  44. the item that started on monday and continued through tuesday.
  45. To simulate an item that crosses day boundaries, you can create 2 items -
  46. one for the time in the first day and one for the time in the second.
  47. We could do this internally by allowing items do span 2 days and
  48. splitting them ourselves. This, however, is not currently implemented */
  49. #include "slapi-plugin.h"
  50. #include "repl5.h"
  51. #include <ctype.h> /* For isdigit() */
  52. /* from proto-slap.h */
  53. char *get_timestring(time_t *t);
  54. void free_timestring(char *timestr);
  55. typedef struct schedule_item
  56. {
  57. struct schedule_item *next;
  58. PRUint32 start; /* Start time, given as seconds after midnight */
  59. PRUint32 end; /* End time */
  60. unsigned char dow; /* Days of week, LSB = Sunday */
  61. } schedule_item;
  62. typedef struct schedule
  63. {
  64. const char *session_id;
  65. size_t max_backlog;
  66. size_t startup_delay;
  67. schedule_item *schedule_list; /* Linked list of schedule windows */
  68. char **prio_attrs; /* Priority attributes - start replication now */
  69. int prio_attrs_override_schedule;
  70. PRTime last_session_end;
  71. int last_session_status;
  72. PRTime last_successful_session_end;
  73. window_state_change_callback callback_fn; /* function to call when window opens/closes */
  74. void *callback_arg; /* argument to pass to the window state change callback */
  75. Slapi_Eq_Context pending_event; /* event scheduled with the event queue */
  76. PRLock *lock;
  77. } schedule;
  78. /* Forward declarations */
  79. static schedule_item *parse_schedule_value(const Slapi_Value *v);
  80. static void schedule_window_state_change_event(Schedule *sch);
  81. static void unschedule_window_state_change_event(Schedule *sch);
  82. static void window_state_changed(time_t when, void *arg);
  83. static int schedule_in_window_now_nolock(Schedule *sch);
  84. static time_t PRTime2time_t(PRTime tm);
  85. static PRTime schedule_next_nolock(Schedule *sch, PRBool start);
  86. static void free_schedule_list(schedule_item **schedule_list);
  87. #define SECONDS_PER_MINUTE 60
  88. #define SECONDS_PER_HOUR (60 * SECONDS_PER_MINUTE)
  89. #define SECONDS_PER_DAY (24 * SECONDS_PER_HOUR)
  90. #define DAYS_PER_WEEK 7
  91. #define ALL_DAYS 0x7F /* Bit mask */
  92. /*
  93. * Create a new schedule object and return a pointer to it.
  94. */
  95. Schedule *
  96. schedule_new(window_state_change_callback callback_fn, void *callback_arg, const char *session_id)
  97. {
  98. Schedule *sch = NULL;
  99. sch = (Schedule *)slapi_ch_calloc(1, sizeof(struct schedule));
  100. sch->session_id = session_id ? session_id : "";
  101. sch->callback_fn = callback_fn;
  102. sch->callback_arg = callback_arg;
  103. if ((sch->lock = PR_NewLock()) == NULL) {
  104. slapi_ch_free((void **)&sch);
  105. }
  106. return sch;
  107. }
  108. void
  109. schedule_destroy(Schedule *s)
  110. {
  111. int i;
  112. /* unschedule update window event if exists */
  113. unschedule_window_state_change_event(s);
  114. if (s->schedule_list) {
  115. free_schedule_list(&s->schedule_list);
  116. }
  117. if (NULL != s->prio_attrs) {
  118. for (i = 0; NULL != s->prio_attrs[i]; i++) {
  119. slapi_ch_free((void **)&(s->prio_attrs[i]));
  120. }
  121. slapi_ch_free((void **)&(s->prio_attrs));
  122. }
  123. PR_DestroyLock(s->lock);
  124. s->lock = NULL;
  125. slapi_ch_free((void **)&s);
  126. }
  127. static void
  128. free_schedule_list(schedule_item **schedule_list)
  129. {
  130. schedule_item *si = *schedule_list;
  131. schedule_item *tmp_si;
  132. while (NULL != si) {
  133. tmp_si = si->next;
  134. slapi_ch_free((void **)&si);
  135. si = tmp_si;
  136. }
  137. *schedule_list = NULL;
  138. }
  139. /*
  140. * Sets the schedule. Returns 0 if all of the schedule lines were
  141. * correctly parsed and the new schedule has been put into effect.
  142. * Returns -1 if one or more of the schedule items could not be
  143. * parsed. If -1 is returned, then no changes have been made to the
  144. * current schedule.
  145. */
  146. int
  147. schedule_set(Schedule *sch, Slapi_Attr *attr)
  148. {
  149. int return_value;
  150. schedule_item *si = NULL;
  151. schedule_item *new_schedule_list = NULL;
  152. int valid = 1;
  153. if (NULL != attr) {
  154. int ind;
  155. Slapi_Value *sval;
  156. ind = slapi_attr_first_value(attr, &sval);
  157. while (ind >= 0) {
  158. si = parse_schedule_value(sval);
  159. if (NULL == si) {
  160. valid = 0;
  161. break;
  162. }
  163. /* Put at head of linked list */
  164. si->next = new_schedule_list;
  165. new_schedule_list = si;
  166. ind = slapi_attr_next_value(attr, ind, &sval);
  167. }
  168. }
  169. if (!valid) {
  170. /* deallocate any new schedule items */
  171. free_schedule_list(&new_schedule_list);
  172. return_value = -1;
  173. } else {
  174. PR_Lock(sch->lock);
  175. /* if there is an update window event scheduled - unschedule it */
  176. unschedule_window_state_change_event(sch);
  177. free_schedule_list(&sch->schedule_list);
  178. sch->schedule_list = new_schedule_list;
  179. /* schedule an event to notify the caller about openning/closing of the update window */
  180. schedule_window_state_change_event(sch);
  181. PR_Unlock(sch->lock);
  182. return_value = 0;
  183. }
  184. return return_value;
  185. }
  186. /*
  187. * Returns the schedule.
  188. */
  189. char **
  190. schedule_get(Schedule *sch __attribute__((unused)))
  191. {
  192. char **return_value = NULL;
  193. return return_value;
  194. }
  195. /*
  196. * Return an integer corresponding to the day of the week for
  197. * "when".
  198. */
  199. static PRInt32
  200. day_of_week(PRTime when)
  201. {
  202. PRExplodedTime exp;
  203. PR_ExplodeTime(when, PR_LocalTimeParameters, &exp);
  204. return (exp.tm_wday);
  205. }
  206. /*
  207. * Return the number of seconds between "when" and the
  208. * most recent midnight.
  209. */
  210. static PRUint32
  211. seconds_since_midnight(PRTime when)
  212. {
  213. PRExplodedTime exp;
  214. PR_ExplodeTime(when, PR_LocalTimeParameters, &exp);
  215. return (exp.tm_hour * 3600 + exp.tm_min * 60 + exp.tm_sec);
  216. }
  217. /*
  218. * Return 1 if "now" is within the schedule window
  219. * specified by "si", 0 otherwise.
  220. */
  221. static int
  222. time_in_window(PRTime now, schedule_item *si)
  223. {
  224. unsigned char dow = 1 << day_of_week(now);
  225. int return_value = 0;
  226. if (dow & si->dow) {
  227. PRUint32 nowsec = seconds_since_midnight(now);
  228. return_value = (nowsec >= si->start) && (nowsec <= si->end);
  229. }
  230. return return_value;
  231. }
  232. /*
  233. * Returns a non-zero value if the current time is within a
  234. * replication window and if scheduling constraints are all met.
  235. * Otherwise, returns zero.
  236. */
  237. int
  238. schedule_in_window_now(Schedule *sch)
  239. {
  240. int rc;
  241. PR_ASSERT(NULL != sch);
  242. PR_Lock(sch->lock);
  243. rc = schedule_in_window_now_nolock(sch);
  244. PR_Unlock(sch->lock);
  245. return rc;
  246. }
  247. /* Must be called under sch->lock */
  248. static int
  249. schedule_in_window_now_nolock(Schedule *sch)
  250. {
  251. int return_value = 0;
  252. if (NULL == sch->schedule_list) {
  253. /* Absence of a schedule is the same as 0000-2359 0123456 */
  254. return_value = 1;
  255. } else {
  256. schedule_item *si = sch->schedule_list;
  257. PRTime now;
  258. now = PR_Now();
  259. while (NULL != si) {
  260. if (time_in_window(now, si)) {
  261. /* XXX check backoff timers??? */
  262. return_value = 1;
  263. break;
  264. }
  265. si = si->next;
  266. }
  267. }
  268. return return_value;
  269. }
  270. /*
  271. * Calculate the next time (expressed as a PRTime) when this
  272. * schedule item will change state (from open to close or vice versa).
  273. */
  274. static PRTime
  275. next_change_time(schedule_item *si, PRTime now, PRBool start)
  276. {
  277. PRUint32 nowsec = seconds_since_midnight(now);
  278. PRUint32 sec_til_change;
  279. PRUint32 change_time;
  280. PRExplodedTime exp;
  281. PRInt32 dow = day_of_week(now);
  282. unsigned char dow_bit = 1 << dow;
  283. unsigned char next_dow;
  284. if (start) /* we are looking for the next window opening */
  285. {
  286. change_time = si->start;
  287. } else /* we are looking for the next window closing */
  288. {
  289. /* open range is inclusive - so we need to add a minute if we are looking for close time */
  290. change_time = si->end + SECONDS_PER_MINUTE;
  291. }
  292. /* we are replicating today and next change is also today */
  293. if ((dow_bit & si->dow) && (nowsec < change_time)) {
  294. sec_til_change = change_time - nowsec;
  295. } else /* not replicating today or the change already occured today */
  296. {
  297. int i;
  298. /* find next day when we replicate */
  299. for (i = 1; i <= DAYS_PER_WEEK; i++) {
  300. next_dow = 1 << ((dow + i) % DAYS_PER_WEEK);
  301. if (next_dow & si->dow)
  302. break;
  303. }
  304. sec_til_change = change_time + i * SECONDS_PER_DAY - nowsec;
  305. }
  306. PR_ExplodeTime(now, PR_LocalTimeParameters, &exp);
  307. exp.tm_sec += sec_til_change;
  308. PR_NormalizeTime(&exp, PR_LocalTimeParameters);
  309. return PR_ImplodeTime(&exp);
  310. }
  311. /*
  312. * Returns the next time that replication is scheduled to occur.
  313. * Returns 0 if there is no time in the future that replication
  314. * will begin (e.g. there's no schedule at all).
  315. */
  316. PRTime
  317. schedule_next(Schedule *sch)
  318. {
  319. PRTime tm;
  320. PR_ASSERT(NULL != sch);
  321. PR_Lock(sch->lock);
  322. tm = schedule_next_nolock(sch, PR_TRUE);
  323. PR_Unlock(sch->lock);
  324. return tm;
  325. }
  326. /* Must be called under sch->lock */
  327. static PRTime
  328. schedule_next_nolock(Schedule *sch, PRBool start)
  329. {
  330. PRTime closest_time = LL_Zero();
  331. if (NULL != sch->schedule_list) {
  332. schedule_item *si = sch->schedule_list;
  333. PRTime now = PR_Now();
  334. while (NULL != si) {
  335. PRTime tmp_time;
  336. /* Check if this item's change time is sooner than the others */
  337. tmp_time = next_change_time(si, now, start);
  338. if (LL_IS_ZERO(closest_time)) {
  339. LL_ADD(closest_time, tmp_time, LL_Zero()); /* Really just an asignment */
  340. } else if (LL_CMP(tmp_time, <, closest_time)) {
  341. LL_ADD(closest_time, tmp_time, LL_Zero()); /* Really just an asignment */
  342. }
  343. si = si->next;
  344. }
  345. }
  346. return closest_time;
  347. }
  348. /*
  349. * Called by the enclosing object (replsupplier) when a change within the
  350. * replicated area has occurred. This allows the scheduler to update its
  351. * internal counters, timers, etc. Returns a non-zero value if replication
  352. * should commence, zero if it should not.
  353. */
  354. int
  355. schedule_notify(Schedule *sch __attribute__((unused)), Slapi_PBlock *pb __attribute__((unused)))
  356. {
  357. int return_value = 0;
  358. return return_value;
  359. }
  360. /*
  361. * Provide a list of attributes which, if changed,
  362. * will cause replication to commence as soon as possible. There
  363. * is also a flag that tells the scheduler if the update of a
  364. * priority attribute should cause the schedule to be overridden,
  365. * e.g. if the administrator wants password changes to propagate
  366. * even if not in a replication window.
  367. *
  368. * This function consumes "prio_attrs" and assumes management
  369. * of the memory.
  370. */
  371. void
  372. schedule_set_priority_attributes(Schedule *sch, char **prio_attrs, int override_schedule)
  373. {
  374. PR_ASSERT(NULL != sch);
  375. PR_Lock(sch->lock);
  376. if (NULL != sch->prio_attrs) {
  377. int i;
  378. for (i = 0; NULL != prio_attrs[i]; i++) {
  379. slapi_ch_free((void **)&sch->prio_attrs[i]);
  380. }
  381. slapi_ch_free((void **)&sch->prio_attrs);
  382. }
  383. sch->prio_attrs = prio_attrs;
  384. sch->prio_attrs_override_schedule = override_schedule;
  385. PR_Unlock(sch->lock);
  386. }
  387. /*
  388. * Set the time, in seconds, that replication will wait after a change is
  389. * available before propagating it. This capability will allow multiple
  390. * updates to be coalesced into a single replication session.
  391. */
  392. void
  393. schedule_set_startup_delay(Schedule *sch, size_t startup_delay)
  394. {
  395. PR_ASSERT(NULL != sch);
  396. PR_Lock(sch->lock);
  397. sch->startup_delay = startup_delay;
  398. PR_Unlock(sch->lock);
  399. }
  400. /*
  401. * Set the maximum number of pending changes allowed to accumulate
  402. * before a replication session is begun.
  403. */
  404. void
  405. schedule_set_maximum_backlog(Schedule *sch, size_t max_backlog)
  406. {
  407. PR_ASSERT(NULL != sch);
  408. PR_Lock(sch->lock);
  409. sch->max_backlog = max_backlog;
  410. PR_Unlock(sch->lock);
  411. }
  412. /*
  413. * Notify the scheduler that a replication session completed at a certain
  414. * time. There is also a status argument that says more about the session's
  415. * termination (normal, abnormal), which the scheduler uses in determining
  416. * the backoff strategy.
  417. */
  418. void
  419. schedule_notify_session(Schedule *sch, PRTime session_end_time, unsigned int status)
  420. {
  421. PR_ASSERT(NULL != sch);
  422. PR_Lock(sch->lock);
  423. sch->last_session_end = session_end_time;
  424. sch->last_session_status = status;
  425. if (REPLICATION_SESSION_SUCCESS == status) {
  426. sch->last_successful_session_end = session_end_time;
  427. }
  428. PR_Unlock(sch->lock);
  429. }
  430. /* schedule an event that will fire the next time the update window state
  431. changes from open to closed or vice versa */
  432. static void
  433. schedule_window_state_change_event(Schedule *sch)
  434. {
  435. time_t wakeup_time;
  436. PRTime tm;
  437. int window_opened;
  438. char *timestr = NULL;
  439. /* if we have a schedule and a callback function is registerd -
  440. register an event with the event queue */
  441. if (sch->schedule_list && sch->callback_fn) {
  442. /* ONREPL what if the window is really small and by the time we are done
  443. with the computation - we cross window boundary.
  444. I think we should put some constrains on schedule to avoid that */
  445. window_opened = schedule_in_window_now_nolock(sch);
  446. tm = schedule_next_nolock(sch, !window_opened);
  447. wakeup_time = PRTime2time_t(tm);
  448. /* schedule the event */
  449. sch->pending_event = slapi_eq_once(window_state_changed, sch, wakeup_time);
  450. timestr = get_timestring(&wakeup_time);
  451. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "%s: Update window will %s at %s\n",
  452. sch->session_id,
  453. window_opened ? "close" : "open", timestr);
  454. free_timestring(timestr);
  455. timestr = NULL;
  456. }
  457. }
  458. /* this function is called by the even queue the next time
  459. the window is opened or closed */
  460. static void
  461. window_state_changed(time_t when __attribute__((unused)), void *arg)
  462. {
  463. Schedule *sch = (Schedule *)arg;
  464. int open;
  465. PR_ASSERT(sch);
  466. PR_Lock(sch->lock);
  467. open = schedule_in_window_now_nolock(sch);
  468. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "%s: Update window is now %s\n",
  469. sch->session_id,
  470. open ? "open" : "closed");
  471. /* schedule next event */
  472. schedule_window_state_change_event(sch);
  473. /* notify the agreement */
  474. sch->callback_fn(sch->callback_arg, open);
  475. PR_Unlock(sch->lock);
  476. }
  477. /* cancel the event registered with the event queue */
  478. static void
  479. unschedule_window_state_change_event(Schedule *sch)
  480. {
  481. if (sch->pending_event) {
  482. slapi_eq_cancel(sch->pending_event);
  483. sch->pending_event = NULL;
  484. }
  485. }
  486. static time_t
  487. PRTime2time_t(PRTime tm)
  488. {
  489. PRInt64 rt;
  490. PR_ASSERT(tm);
  491. LL_DIV(rt, tm, PR_USEC_PER_SEC);
  492. return (time_t)rt;
  493. }
  494. /*
  495. * Parse a schedule line.
  496. * The format is:
  497. * <start>-<end> <day_of_week>
  498. * <start> and <end> are in 24-hour time
  499. * <day_of_week> is like cron(5): 0 = Sunday, 1 = Monday, etc.
  500. *
  501. * The schedule item "*" is equivalen to 0000-2359 0123456
  502. *
  503. * Returns a pointer to a schedule item on success, NULL if the
  504. * schedule item cannot be parsed.
  505. */
  506. static schedule_item *
  507. parse_schedule_value(const Slapi_Value *v)
  508. {
  509. #define RANGE_VALID(p, limit) \
  510. ((p + 9) < limit && \
  511. isdigit(p[0]) && \
  512. isdigit(p[1]) && \
  513. isdigit(p[2]) && \
  514. isdigit(p[3]) && \
  515. ('-' == p[4]) && \
  516. isdigit(p[5]) && \
  517. isdigit(p[6]) && \
  518. isdigit(p[7]) && \
  519. isdigit(p[8]))
  520. schedule_item *si = NULL;
  521. int valid = 0;
  522. const struct berval *sch_bval;
  523. if (NULL != v && (sch_bval = slapi_value_get_berval(v)) != NULL &&
  524. NULL != sch_bval && sch_bval->bv_len > 0 && NULL != sch_bval->bv_val) {
  525. char *p = sch_bval->bv_val;
  526. char *limit = p + sch_bval->bv_len;
  527. si = (schedule_item *)slapi_ch_malloc(sizeof(schedule_item));
  528. si->next = NULL;
  529. si->start = 0UL;
  530. si->end = SECONDS_PER_DAY;
  531. si->dow = ALL_DAYS;
  532. if (*p == '*') {
  533. valid = 1;
  534. goto done;
  535. } else {
  536. if (RANGE_VALID(p, limit)) {
  537. si->start = ((strntoul(p, 2, 10) * 60) +
  538. strntoul(p + 2, 2, 10)) *
  539. 60;
  540. p += 5;
  541. si->end = ((strntoul(p, 2, 10) * 60) +
  542. strntoul(p + 2, 2, 10)) *
  543. 60;
  544. p += 4;
  545. /* ONREPL - for now wi don't allow items that span multiple days.
  546. See note in the beginning of the file for more details. */
  547. /* ONREPL - we should also decide on the minimum of the item size */
  548. if (si->start > si->end) {
  549. valid = 0;
  550. goto done;
  551. }
  552. if (p < limit && ' ' == *p) {
  553. /* Specific days of week */
  554. si->dow = 0;
  555. while (++p < limit) {
  556. if (!isdigit(*p)) {
  557. valid = 0;
  558. goto done;
  559. }
  560. si->dow |= (1 << strntoul(p, 1, 10));
  561. }
  562. }
  563. valid = 1;
  564. }
  565. }
  566. }
  567. done:
  568. if (!valid) {
  569. slapi_ch_free((void **)&si);
  570. }
  571. return si;
  572. }