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

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