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