>From 01df1ff0c473915bdea274d7fb3665b2cbce207e Mon Sep 17 00:00:00 2001
From: Michal Zidek <mzidek@redhat.com>
Date: Mon, 7 Jul 2014 16:46:18 +0200
Subject: [PATCH 1/3] ptask: Allow adding random_offset to scheduled execution
 time

---
 src/providers/data_provider_be.c     |  2 +-
 src/providers/dp_ptask.c             | 12 +++++++++++-
 src/providers/dp_ptask.h             |  5 +++++
 src/providers/ldap/ldap_id_cleanup.c |  7 ++++---
 src/providers/ldap/ldap_id_enum.c    |  1 +
 5 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/src/providers/data_provider_be.c b/src/providers/data_provider_be.c
index faa9f7a..a01bb06 100644
--- a/src/providers/data_provider_be.c
+++ b/src/providers/data_provider_be.c
@@ -2580,7 +2580,7 @@ int be_process_init(TALLOC_CTX *mem_ctx,
 
     if (ctx->domain->refresh_expired_interval > 0) {
         ret = be_ptask_create(ctx, ctx, ctx->domain->refresh_expired_interval,
-                              30, 5, ctx->domain->refresh_expired_interval,
+                              30, 5, 0, ctx->domain->refresh_expired_interval,
                               BE_PTASK_OFFLINE_SKIP,
                               be_refresh_send, be_refresh_recv,
                               ctx->refresh_ctx, "Refresh Records", NULL);
diff --git a/src/providers/dp_ptask.c b/src/providers/dp_ptask.c
index 8214112..4d0fec4 100644
--- a/src/providers/dp_ptask.c
+++ b/src/providers/dp_ptask.c
@@ -37,6 +37,8 @@ struct be_ptask {
     struct be_ctx *be_ctx;
     time_t period;
     time_t enabled_delay;
+    time_t random_offset;
+    unsigned int ro_seed;
     time_t timeout;
     enum be_ptask_offline offline;
     be_ptask_send_t send_fn;
@@ -208,6 +210,10 @@ static void be_ptask_schedule(struct be_ptask *task,
         return;
     }
 
+    if (task->random_offset != 0) {
+        delay = delay + (rand_r(&task->ro_seed) % task->random_offset);
+    }
+
     switch (from) {
     case BE_PTASK_SCHEDULE_FROM_NOW:
         tv = tevent_timeval_current_ofs(delay, 0);
@@ -244,6 +250,7 @@ errno_t be_ptask_create(TALLOC_CTX *mem_ctx,
                         time_t period,
                         time_t first_delay,
                         time_t enabled_delay,
+                        time_t random_offset,
                         time_t timeout,
                         enum be_ptask_offline offline,
                         be_ptask_send_t send_fn,
@@ -270,6 +277,8 @@ errno_t be_ptask_create(TALLOC_CTX *mem_ctx,
     task->be_ctx = be_ctx;
     task->period = period;
     task->enabled_delay = enabled_delay;
+    task->random_offset = random_offset;
+    task->ro_seed = time(NULL) * getpid();
     task->timeout = timeout;
     task->offline = offline;
     task->send_fn = send_fn;
@@ -406,6 +415,7 @@ errno_t be_ptask_create_sync(TALLOC_CTX *mem_ctx,
                              time_t period,
                              time_t first_delay,
                              time_t enabled_delay,
+                             time_t random_offset,
                              time_t timeout,
                              enum be_ptask_offline offline,
                              be_ptask_sync_t fn,
@@ -426,7 +436,7 @@ errno_t be_ptask_create_sync(TALLOC_CTX *mem_ctx,
     ctx->pvt = pvt;
 
     ret = be_ptask_create(mem_ctx, be_ctx, period, first_delay,
-                          enabled_delay, timeout, offline,
+                          enabled_delay, random_offset, timeout, offline,
                           be_ptask_sync_send, be_ptask_sync_recv,
                           ctx, name, _task);
     if (ret != EOK) {
diff --git a/src/providers/dp_ptask.h b/src/providers/dp_ptask.h
index 11324db..0f98738 100644
--- a/src/providers/dp_ptask.h
+++ b/src/providers/dp_ptask.h
@@ -81,6 +81,9 @@ typedef errno_t
  * If the task is reenabled, it will be scheduled again to
  * 'now + enabled_delay'.
  *
+ * The random_offset is maximum number of seconds added to the
+ * expected delay. Set to 0 if no randomization is needed.
+ *
  * If an internal error occurred, the task is automatically disabled.
  */
 errno_t be_ptask_create(TALLOC_CTX *mem_ctx,
@@ -88,6 +91,7 @@ errno_t be_ptask_create(TALLOC_CTX *mem_ctx,
                         time_t period,
                         time_t first_delay,
                         time_t enabled_delay,
+                        time_t random_offset,
                         time_t timeout,
                         enum be_ptask_offline offline,
                         be_ptask_send_t send_fn,
@@ -101,6 +105,7 @@ errno_t be_ptask_create_sync(TALLOC_CTX *mem_ctx,
                              time_t period,
                              time_t first_delay,
                              time_t enabled_delay,
+                             time_t random_offset,
                              time_t timeout,
                              enum be_ptask_offline offline,
                              be_ptask_sync_t fn,
diff --git a/src/providers/ldap/ldap_id_cleanup.c b/src/providers/ldap/ldap_id_cleanup.c
index 6b0bead..0c473da 100644
--- a/src/providers/ldap/ldap_id_cleanup.c
+++ b/src/providers/ldap/ldap_id_cleanup.c
@@ -86,9 +86,10 @@ errno_t ldap_setup_cleanup(struct sdap_id_ctx *id_ctx,
     }
 
     ret = be_ptask_create_sync(sdom, id_ctx->be, period, first_delay,
-                               5 /* enabled delay */, period /* timeout */,
-                               BE_PTASK_OFFLINE_SKIP, ldap_cleanup_task,
-                               cleanup_ctx, name, &sdom->cleanup_task);
+                               5 /* enabled delay */, 0 /* random offset */,
+                               period /* timeout */, BE_PTASK_OFFLINE_SKIP,
+                               ldap_cleanup_task, cleanup_ctx, name,
+                               &sdom->cleanup_task);
     if (ret != EOK) {
         DEBUG(SSSDBG_FATAL_FAILURE, "Unable to initialize cleanup periodic "
                                      "task for %s\n", sdom->dom->name);
diff --git a/src/providers/ldap/ldap_id_enum.c b/src/providers/ldap/ldap_id_enum.c
index 62db1c1..25ee5f6 100644
--- a/src/providers/ldap/ldap_id_enum.c
+++ b/src/providers/ldap/ldap_id_enum.c
@@ -74,6 +74,7 @@ errno_t ldap_setup_enumeration(struct be_ctx *be_ctx,
                           period,                   /* period */
                           first_delay,              /* first_delay */
                           5,                        /* enabled delay */
+                          0,                        /* random offset */
                           period,                   /* timeout */
                           BE_PTASK_OFFLINE_SKIP,
                           send_fn, recv_fn,
-- 
1.9.3

