Blame SOURCES/0001-Providers-Delay-online-check-on-startup.patch

5cd47f
From dab55626ce859dd519fe108b89fa723a38fb21d1 Mon Sep 17 00:00:00 2001
5cd47f
From: Tomas Halman <thalman@redhat.com>
5cd47f
Date: Wed, 20 Mar 2019 15:44:02 +0100
5cd47f
Subject: [PATCH] Providers: Delay online check on startup
5cd47f
5cd47f
Typical usecase is system startup or network restart. In such
5cd47f
cases SSSD receives several messages from the system about
5cd47f
network change and immediately starts connecting.
5cd47f
With multiple addresses on interface or multiple interfaces
5cd47f
SSSD receives even more messages.
5cd47f
5cd47f
This patch introduces 1s delay for online check after first
5cd47f
message.
5cd47f
5cd47f
Online callback tries 3 times to go online. There is increasing
5cd47f
delay between online checks up to 4s.
5cd47f
5cd47f
Resolves: https://pagure.io/SSSD/sssd/issue/3467
5cd47f
5cd47f
Reviewed-by: Alexey Tikhonov <atikhono@redhat.com>
5cd47f
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
5cd47f
(cherry picked from commit fe4288088e6cccf7650e5c5def3bd67be90756be)
5cd47f
---
5cd47f
 src/providers/backend.h          |  1 +
5cd47f
 src/providers/data_provider_be.c | 94 ++++++++++++++++++++++++--------
5cd47f
 2 files changed, 72 insertions(+), 23 deletions(-)
5cd47f
5cd47f
diff --git a/src/providers/backend.h b/src/providers/backend.h
5cd47f
index 1fe1c2313..5ab47b29a 100644
5cd47f
--- a/src/providers/backend.h
5cd47f
+++ b/src/providers/backend.h
5cd47f
@@ -112,6 +112,7 @@ struct be_ctx {
5cd47f
     struct be_refresh_ctx *refresh_ctx;
5cd47f
 
5cd47f
     size_t check_online_ref_count;
5cd47f
+    int check_online_retry_delay;
5cd47f
 
5cd47f
     struct data_provider *provider;
5cd47f
 
5cd47f
diff --git a/src/providers/data_provider_be.c b/src/providers/data_provider_be.c
5cd47f
index fad6f2801..17513111c 100644
5cd47f
--- a/src/providers/data_provider_be.c
5cd47f
+++ b/src/providers/data_provider_be.c
5cd47f
@@ -50,6 +50,9 @@
5cd47f
 #include "resolv/async_resolv.h"
5cd47f
 #include "monitor/monitor_interfaces.h"
5cd47f
 
5cd47f
+#define ONLINE_CB_RETRY 3
5cd47f
+#define ONLINE_CB_RETRY_MAX_DELAY 4
5cd47f
+
5cd47f
 static int data_provider_res_init(struct sbus_request *dbus_req, void *data);
5cd47f
 static int data_provider_go_offline(struct sbus_request *dbus_req, void *data);
5cd47f
 static int data_provider_reset_offline(struct sbus_request *dbus_req, void *data);
5cd47f
@@ -71,7 +74,7 @@ bool be_is_offline(struct be_ctx *ctx)
5cd47f
     return ctx->offstat.offline;
5cd47f
 }
5cd47f
 
5cd47f
-static void check_if_online(struct be_ctx *be_ctx);
5cd47f
+static void check_if_online(struct be_ctx *be_ctx, int delay);
5cd47f
 
5cd47f
 static errno_t
5cd47f
 try_to_go_online(TALLOC_CTX *mem_ctx,
5cd47f
@@ -82,7 +85,7 @@ try_to_go_online(TALLOC_CTX *mem_ctx,
5cd47f
 {
5cd47f
     struct be_ctx *ctx = (struct be_ctx*) be_ctx_void;
5cd47f
 
5cd47f
-    check_if_online(ctx);
5cd47f
+    check_if_online(ctx, 0);
5cd47f
     return EOK;
5cd47f
 }
5cd47f
 
5cd47f
@@ -247,10 +250,39 @@ static errno_t be_check_online_request(struct be_ctx *be_ctx)
5cd47f
     return EOK;
5cd47f
 }
5cd47f
 
5cd47f
+static void check_if_online_delayed(struct tevent_context *ev,
5cd47f
+                                    struct tevent_timer *tim,
5cd47f
+                                    struct timeval current_time,
5cd47f
+                                    void *private_data)
5cd47f
+{
5cd47f
+    errno_t ret;
5cd47f
+    struct be_ctx *be_ctx = talloc_get_type(private_data, struct be_ctx);
5cd47f
+
5cd47f
+    be_run_unconditional_online_cb(be_ctx);
5cd47f
+
5cd47f
+    if (!be_is_offline(be_ctx)) {
5cd47f
+        DEBUG(SSSDBG_TRACE_INTERNAL,
5cd47f
+              "Backend is already online, nothing to do.\n");
5cd47f
+        be_ctx->check_online_ref_count = 0;
5cd47f
+        return;
5cd47f
+    }
5cd47f
+
5cd47f
+    DEBUG(SSSDBG_TRACE_INTERNAL, "Trying to go back online!\n");
5cd47f
+
5cd47f
+    ret = be_check_online_request(be_ctx);
5cd47f
+    if (ret != EOK) {
5cd47f
+        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create check online req.\n");
5cd47f
+    } else {
5cd47f
+        DEBUG(SSSDBG_TRACE_INTERNAL, "Check online req created.\n");
5cd47f
+    }
5cd47f
+}
5cd47f
+
5cd47f
 static void be_check_online_done(struct tevent_req *req)
5cd47f
 {
5cd47f
     struct be_ctx *be_ctx;
5cd47f
     struct dp_reply_std *reply;
5cd47f
+    struct tevent_timer *time_event;
5cd47f
+    struct timeval schedule;
5cd47f
     errno_t ret;
5cd47f
 
5cd47f
     be_ctx = tevent_req_callback_data(req, struct be_ctx);
5cd47f
@@ -285,11 +317,24 @@ static void be_check_online_done(struct tevent_req *req)
5cd47f
     be_ctx->check_online_ref_count--;
5cd47f
 
5cd47f
     if (reply->dp_error != DP_ERR_OK && be_ctx->check_online_ref_count > 0) {
5cd47f
-        ret = be_check_online_request(be_ctx);
5cd47f
-        if (ret != EOK) {
5cd47f
-            DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create check online req.\n");
5cd47f
+        be_ctx->check_online_retry_delay *= 2;
5cd47f
+        if (be_ctx->check_online_retry_delay > ONLINE_CB_RETRY_MAX_DELAY) {
5cd47f
+            be_ctx->check_online_retry_delay = ONLINE_CB_RETRY_MAX_DELAY;
5cd47f
+        }
5cd47f
+
5cd47f
+        schedule = tevent_timeval_current_ofs(be_ctx->check_online_retry_delay,
5cd47f
+                                              0);
5cd47f
+        time_event = tevent_add_timer(be_ctx->ev, be_ctx, schedule,
5cd47f
+                                      check_if_online_delayed, be_ctx);
5cd47f
+
5cd47f
+        if (time_event == NULL) {
5cd47f
+            DEBUG(SSSDBG_OP_FAILURE, "Failed to schedule online check\n");
5cd47f
             goto done;
5cd47f
         }
5cd47f
+
5cd47f
+        DEBUG(SSSDBG_TRACE_INTERNAL,
5cd47f
+              "Schedule check_if_online_delayed in %ds.\n",
5cd47f
+              be_ctx->check_online_retry_delay);
5cd47f
         return;
5cd47f
     }
5cd47f
 
5cd47f
@@ -303,28 +348,23 @@ done:
5cd47f
     }
5cd47f
 }
5cd47f
 
5cd47f
-static void check_if_online(struct be_ctx *be_ctx)
5cd47f
+static void check_if_online(struct be_ctx *be_ctx, int delay)
5cd47f
 {
5cd47f
-    errno_t ret;
5cd47f
-
5cd47f
-    be_run_unconditional_online_cb(be_ctx);
5cd47f
-
5cd47f
-    if (!be_is_offline(be_ctx)) {
5cd47f
-        DEBUG(SSSDBG_TRACE_INTERNAL,
5cd47f
-              "Backend is already online, nothing to do.\n");
5cd47f
-        return;
5cd47f
-    }
5cd47f
+    struct tevent_timer *time_event;
5cd47f
+    struct timeval schedule;
5cd47f
 
5cd47f
     /* Make sure nobody tries to go online while we are checking */
5cd47f
     be_ctx->offstat.went_offline = time(NULL);
5cd47f
 
5cd47f
-    DEBUG(SSSDBG_TRACE_INTERNAL, "Trying to go back online!\n");
5cd47f
-
5cd47f
     be_ctx->check_online_ref_count++;
5cd47f
 
5cd47f
     if (be_ctx->check_online_ref_count != 1) {
5cd47f
         DEBUG(SSSDBG_TRACE_INTERNAL,
5cd47f
               "There is an online check already running.\n");
5cd47f
+        /* Do not have more than ONLINE_CB_RETRY retries in the queue */
5cd47f
+        if (be_ctx->check_online_ref_count > ONLINE_CB_RETRY) {
5cd47f
+            be_ctx->check_online_ref_count--;
5cd47f
+        }
5cd47f
         return;
5cd47f
     }
5cd47f
 
5cd47f
@@ -334,12 +374,20 @@ static void check_if_online(struct be_ctx *be_ctx)
5cd47f
         goto failed;
5cd47f
     }
5cd47f
 
5cd47f
-    ret = be_check_online_request(be_ctx);
5cd47f
-    if (ret != EOK) {
5cd47f
-        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create check online req.\n");
5cd47f
+    schedule = tevent_timeval_current_ofs(delay, 0);
5cd47f
+    time_event = tevent_add_timer(be_ctx->ev, be_ctx, schedule,
5cd47f
+                                  check_if_online_delayed, be_ctx);
5cd47f
+
5cd47f
+    if (time_event == NULL) {
5cd47f
+        DEBUG(SSSDBG_OP_FAILURE,
5cd47f
+              "Scheduling check_if_online_delayed failed.\n");
5cd47f
         goto failed;
5cd47f
     }
5cd47f
 
5cd47f
+    be_ctx->check_online_ref_count = ONLINE_CB_RETRY;
5cd47f
+    be_ctx->check_online_retry_delay = 1;
5cd47f
+    DEBUG(SSSDBG_TRACE_INTERNAL,
5cd47f
+          "Schedule check_if_online_delayed in %ds.\n", delay);
5cd47f
     return;
5cd47f
 
5cd47f
 failed:
5cd47f
@@ -373,7 +421,7 @@ static void signal_be_reset_offline(struct tevent_context *ev,
5cd47f
                                     void *private_data)
5cd47f
 {
5cd47f
     struct be_ctx *ctx = talloc_get_type(private_data, struct be_ctx);
5cd47f
-    check_if_online(ctx);
5cd47f
+    check_if_online(ctx, 0);
5cd47f
 }
5cd47f
 
5cd47f
 errno_t be_process_init(TALLOC_CTX *mem_ctx,
5cd47f
@@ -649,7 +697,7 @@ static int data_provider_res_init(struct sbus_request *dbus_req, void *data)
5cd47f
     be_ctx = talloc_get_type(data, struct be_ctx);
5cd47f
 
5cd47f
     resolv_reread_configuration(be_ctx->be_res->resolv);
5cd47f
-    check_if_online(be_ctx);
5cd47f
+    check_if_online(be_ctx, 1);
5cd47f
 
5cd47f
     return monitor_common_res_init(dbus_req, data);
5cd47f
 }
5cd47f
@@ -666,7 +714,7 @@ static int data_provider_reset_offline(struct sbus_request *dbus_req, void *data
5cd47f
 {
5cd47f
     struct be_ctx *be_ctx;
5cd47f
     be_ctx = talloc_get_type(data, struct be_ctx);
5cd47f
-    check_if_online(be_ctx);
5cd47f
+    check_if_online(be_ctx, 1);
5cd47f
     return sbus_request_return_and_finish(dbus_req, DBUS_TYPE_INVALID);
5cd47f
 }
5cd47f
 
5cd47f
-- 
5cd47f
2.19.1
5cd47f