Blame SOURCES/0003-rhbz-Retry-XML-RPC-calls-when-uploading-attachments.patch

83467f
From 28a6267c056d43cdbf8cd3e10e69d8ab113e74f8 Mon Sep 17 00:00:00 2001
83467f
From: Michal Srb <michal@redhat.com>
83467f
Date: Tue, 7 Sep 2021 21:31:21 +0200
83467f
Subject: [PATCH] [rhbz] Retry XML-RPC calls when uploading attachments
83467f
83467f
If there is a bot that automatically modifies newly opened bugs, then
83467f
it can lead to "query serialization error" from Bugzilla. Retry should
83467f
help us here.
83467f
83467f
Signed-off-by: Michal Srb <michal@redhat.com>
83467f
---
83467f
 src/lib/abrt_xmlrpc.c     | 34 ++++++++++++++++++++++++++++++++++
83467f
 src/lib/abrt_xmlrpc.h     |  5 +++++
83467f
 src/lib/libreport-web.sym |  1 +
83467f
 src/plugins/rhbz.c        |  4 +++-
83467f
 4 files changed, 43 insertions(+), 1 deletion(-)
83467f
83467f
diff --git a/src/lib/abrt_xmlrpc.c b/src/lib/abrt_xmlrpc.c
83467f
index 7cac9253..4c3b469a 100644
83467f
--- a/src/lib/abrt_xmlrpc.c
83467f
+++ b/src/lib/abrt_xmlrpc.c
83467f
@@ -16,6 +16,7 @@
83467f
     with this program; if not, write to the Free Software Foundation, Inc.,
83467f
     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
83467f
 */
83467f
+#include <unistd.h>
83467f
 #include "internal_libreport.h"
83467f
 #include "abrt_xmlrpc.h"
83467f
 #include "proxies.h"
83467f
@@ -301,3 +302,36 @@ xmlrpc_value *abrt_xmlrpc_call(struct abrt_xmlrpc *ax,
83467f
 
83467f
     return result;
83467f
 }
83467f
+
83467f
+/* die eventually or return expected results; retry up to 5 times if the error is known */
83467f
+xmlrpc_value *abrt_xmlrpc_call_with_retry(const char *fault_substring,
83467f
+                                          struct abrt_xmlrpc *ax,
83467f
+                                          const char *method,
83467f
+                                          const char *format, ...)
83467f
+{
83467f
+    int retry_counter = 0;
83467f
+    xmlrpc_env env;
83467f
+
83467f
+    va_list args;
83467f
+
83467f
+    do {
83467f
+        // sleep, if this is not the first try;
83467f
+        // sleep() can be interrupted, but that's not a big deal here
83467f
+        if (retry_counter)
83467f
+            sleep(retry_counter);
83467f
+
83467f
+        va_start(args, format);
83467f
+        xmlrpc_value *result = abrt_xmlrpc_call_full_va(&env, ax, method, format, args);
83467f
+        va_end(args);
83467f
+
83467f
+        if (!env.fault_occurred)
83467f
+            return result;  // success!
83467f
+
83467f
+        if (env.fault_string && !strstr(env.fault_string, fault_substring)) {
83467f
+            // unknown error, don't bother retrying...
83467f
+            abrt_xmlrpc_die(&env;;
83467f
+        }
83467f
+    } while (++retry_counter <= 5);
83467f
+
83467f
+    abrt_xmlrpc_die(&env;;
83467f
+}
83467f
diff --git a/src/lib/abrt_xmlrpc.h b/src/lib/abrt_xmlrpc.h
83467f
index 31768ffc..8ddcfc54 100644
83467f
--- a/src/lib/abrt_xmlrpc.h
83467f
+++ b/src/lib/abrt_xmlrpc.h
83467f
@@ -63,6 +63,11 @@ xmlrpc_value *abrt_xmlrpc_call_params(xmlrpc_env *env, struct abrt_xmlrpc *ax,
83467f
 xmlrpc_value *abrt_xmlrpc_call_full(xmlrpc_env *enf, struct abrt_xmlrpc *ax,
83467f
                                    const char *method, const char *format, ...);
83467f
 
83467f
+xmlrpc_value *abrt_xmlrpc_call_with_retry(const char *fault_substring,
83467f
+                                          struct abrt_xmlrpc *ax,
83467f
+                                          const char *method,
83467f
+                                          const char *format, ...);
83467f
+
83467f
 #ifdef __cplusplus
83467f
 }
83467f
 #endif
83467f
diff --git a/src/lib/libreport-web.sym b/src/lib/libreport-web.sym
83467f
index 44f5244d..9ab88d3e 100644
83467f
--- a/src/lib/libreport-web.sym
83467f
+++ b/src/lib/libreport-web.sym
83467f
@@ -51,6 +51,7 @@ global:
83467f
     abrt_xmlrpc_call;
83467f
     abrt_xmlrpc_call_params;
83467f
     abrt_xmlrpc_call_full;
83467f
+    abrt_xmlrpc_call_with_retry;
83467f
 
83467f
     /* internal_libreport.h - these symbols are only to be used by libreport developers */
83467f
     libreport_trim_all_whitespace;
83467f
diff --git a/src/plugins/rhbz.c b/src/plugins/rhbz.c
83467f
index 0dae1e93..f252f914 100644
83467f
--- a/src/plugins/rhbz.c
83467f
+++ b/src/plugins/rhbz.c
83467f
@@ -643,8 +643,10 @@ int rhbz_attach_blob(struct abrt_xmlrpc *ax, const char *bug_id,
83467f
      *   i -> integer, single argument (int value)
83467f
      *   6 -> base64,  two arguments (char* plain data which will be encoded by xmlrpc-c to base64,
83467f
      *                                size_t number of bytes to encode)
83467f
+     *
83467f
+     * Retry if another user/bot attempted to change the same data.
83467f
      */
83467f
-    result = abrt_xmlrpc_call(ax, "Bug.add_attachment", "{s:(s),s:s,s:s,s:s,s:6,s:i}",
83467f
+    result = abrt_xmlrpc_call_with_retry("query serialization error", ax, "Bug.add_attachment", "{s:(s),s:s,s:s,s:s,s:6,s:i}",
83467f
                 "ids", bug_id,
83467f
                 "summary", fn,
83467f
                 "file_name", filename,
83467f
-- 
83467f
2.31.1
83467f