1
0

repl5_schedule.c 18 KB

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