From 321774fa8338abf0d9ca87be075c603b7935d3cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Canna=C3=B2?= Date: Tue, 30 Apr 2024 17:05:45 +0000 Subject: [PATCH] Comments and refactoring of handler_ProcessingQueryError_CheckBackendConnectionStatus() --- lib/MySQL_Session.cpp | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/lib/MySQL_Session.cpp b/lib/MySQL_Session.cpp index e47fd386e..648f31a03 100644 --- a/lib/MySQL_Session.cpp +++ b/lib/MySQL_Session.cpp @@ -4609,20 +4609,32 @@ __get_pkts_from_client: // 0 : no action // -1 : the calling function will return // 1 : call to NEXT_IMMEDIATE + + +/** + * @brief Handler for processing query errors and checking backend connection status. + * + * This function handles query errors and checks the status of the backend connection. + * It evaluates the server status and takes appropriate actions based on specific conditions. + * + * @param myds Pointer to the MySQL data stream associated with the session. + * + * @return Returns an integer status code indicating the result of the error handling: + * - 0: No action required, query processing can continue. + * - 1: Retry required due to backend connection status, query processing needs to be retried. + * - -1: Error encountered, query processing cannot continue. + */ + int MySQL_Session::handler_ProcessingQueryError_CheckBackendConnectionStatus(MySQL_Data_Stream *myds) { MySQL_Connection *myconn = myds->myconn; // the query failed - if ( - // due to #774 , we now read myconn->server_status instead of myconn->parent->status - (myconn->server_status==MYSQL_SERVER_STATUS_OFFLINE_HARD) // the query failed because the server is offline hard - || - (myconn->server_status==MYSQL_SERVER_STATUS_SHUNNED && myconn->parent->shunned_automatic==true && myconn->parent->shunned_and_kill_all_connections==true) // the query failed because the server is shunned due to a serious failure - || - (myconn->server_status==MYSQL_SERVER_STATUS_SHUNNED_REPLICATION_LAG) // slave is lagging! see #774 - ) { + if (myconn->IsServerOffline()) { + // Set maximum connect time if connect timeout is configured if (mysql_thread___connect_timeout_server_max) { myds->max_connect_time=thread->curtime+mysql_thread___connect_timeout_server_max*1000; } + + // Variables to track retry and error conditions bool retry_conn=false; if (myconn->server_status==MYSQL_SERVER_STATUS_SHUNNED_REPLICATION_LAG) { thread->status_variables.stvar[st_var_backend_lagging_during_query]++; @@ -4633,6 +4645,8 @@ int MySQL_Session::handler_ProcessingQueryError_CheckBackendConnectionStatus(MyS proxy_error("Detected an offline server during query: %s, %d, session_id:%u\n", myconn->parent->address, myconn->parent->port, this->thread_session_id); MyHGM->p_update_mysql_error_counter(p_mysql_error_type::proxysql, myconn->parent->myhgc->hid, myconn->parent->address, myconn->parent->port, ER_PROXYSQL_OFFLINE_SRV); } + + // Retry the query if retries are allowed and conditions permit if (myds->query_retries_on_failure > 0) { myds->query_retries_on_failure--; if ((myds->myconn->reusable==true) && myds->myconn->IsActiveTransaction()==false && myds->myconn->MultiplexDisabled()==false) { @@ -4644,6 +4658,8 @@ int MySQL_Session::handler_ProcessingQueryError_CheckBackendConnectionStatus(MyS } } } + + // Destroy MySQL connection from pool and reset file descriptor myds->destroy_MySQL_Connection_From_Pool(false); myds->fd=0; if (retry_conn) {