]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
[mod_mariadb] Fix "DeadLock. The retries are over." message.
authorSergei Rozhkov <119299827+ghzserg@users.noreply.github.com>
Fri, 11 Aug 2023 16:53:32 +0000 (21:53 +0500)
committerGitHub <noreply@github.com>
Fri, 11 Aug 2023 16:53:32 +0000 (19:53 +0300)
Co-authored-by: Sergei Rozhkov <git@zserg.ru>
src/mod/databases/mod_mariadb/mod_mariadb.c

index 5f32e8e90d61c7214993e0963feea28d9235ad1a..09b67468bb3311dbaa01278fa143ce00c7549360 100644 (file)
@@ -627,13 +627,27 @@ switch_status_t mariadb_send_query(mariadb_handle_t *handle, const char* sql)
 {
        char *err_str;
        int ret;
+       unsigned retries = 60; /* 60 tries, will take 30 to 60 seconds at worst */
 
        switch_safe_free(handle->sql);
        handle->sql = strdup(sql);
+    again:
        handle->stored_results = 0;
        ret = mysql_real_query(&handle->con, sql, (unsigned long)strlen(sql));  
        if (ret) {
                err_str = mariadb_handle_get_error(handle);
+               if (strstr(err_str, "Deadlock found when trying to get lock; try restarting transaction")) {
+                       if (--retries > 0) {
+                               switch_safe_free(err_str);
+                               /* We are waiting for 500 ms and random time is not more than 500 ms.
+                                 This is necessary so that the delay on the primary and secondary servers does not coincide and deadlock does not occur again. */
+                               switch_yield(500 + (rand() & 511));
+                               goto again;
+                       }
+
+                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "DeadLock. The retries are over.\n");
+               }
+
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Failed to send query (%s) to database: %s\n", sql, err_str);
                switch_safe_free(err_str);
                mariadb_finish_results(handle);