repl5_schedule.c 18 KB

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