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

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