From 73e52dddace9fe4a72507cc977948d207dc0a2b0 Mon Sep 17 00:00:00 2001 From: Sylvain Chapeland Date: Mon, 6 May 2024 17:00:10 +0200 Subject: [PATCH 1/2] fix mysql auto-reconnect --- doc/releaseNotes.md | 3 +++ src/ReadoutDatabase.cxx | 51 +++++++++++++++++++++++++++++++++-------- src/ReadoutDatabase.h | 7 ++++++ 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/doc/releaseNotes.md b/doc/releaseNotes.md index 9dc9aa98..5d2dcb81 100644 --- a/doc/releaseNotes.md +++ b/doc/releaseNotes.md @@ -615,3 +615,6 @@ This file describes the main feature changes for each readout.exe released versi ## v2.23.0 - 18/03/2024 - Added o2-readout-rawmerger: a utility to concatenate multiple raw files in a single one, e.g. to replay full detector data from a single FLP. + +## next version +- Fix MySQL 8.0.34 depracation warning for auto-reconnect feature. Functionnality re-implemented locally. diff --git a/src/ReadoutDatabase.cxx b/src/ReadoutDatabase.cxx index 79d9b9a4..93cb3d7e 100644 --- a/src/ReadoutDatabase.cxx +++ b/src/ReadoutDatabase.cxx @@ -1,5 +1,6 @@ #include "ReadoutDatabase.h" #include +#include #include #include #include @@ -8,18 +9,39 @@ #include #include #include +#include #if LIBMYSQL_VERSION_ID >= 80000 typedef bool my_bool; #endif +int ReadoutDatabase::connect() { + time_t now = time(NULL); + if ((lastConnectTime != 0) && (now < lastConnectTime + reconnectTimeout)) return -2; + lastConnectTime = now; + if (db != nullptr) { + mysql_close(db); + db = nullptr; + } + db = mysql_init(nullptr); + if (db == nullptr) { + return -1; + } + if (mysql_real_connect(db,cxDbHost.c_str(),cxDbUser.c_str(),cxDbPwd.c_str(),cxDbName.c_str(),0,nullptr,0) == nullptr) { + log(std::string("DB connect error :") + mysql_error(db)); + return -1; + } + log("DB connected"); + lastConnectTime = 0; + return 0; +} + ReadoutDatabase::ReadoutDatabase(const char* cx, int v, const LogCallback& cb) { verbose = v; theLogCallback = cb; char *db_db=nullptr, *db_user=nullptr, *db_pwd=nullptr, *db_host=nullptr; char *p=nullptr,*ptr,*lptr; - my_bool reconnect = 1; if (cx == nullptr) { throw __LINE__; @@ -50,6 +72,7 @@ ReadoutDatabase::ReadoutDatabase(const char* cx, int v, const LogCallback& cb) { break; } } + cxDbUser = db_user; // pwd for (lptr=ptr;*ptr!=0;ptr++) { @@ -60,6 +83,7 @@ ReadoutDatabase::ReadoutDatabase(const char* cx, int v, const LogCallback& cb) { break; } } + cxDbPwd = db_pwd; // host for (lptr=ptr;*ptr!=0;ptr++) { @@ -70,6 +94,7 @@ ReadoutDatabase::ReadoutDatabase(const char* cx, int v, const LogCallback& cb) { break; } } + cxDbHost = db_host; // db name db_db=ptr; @@ -82,12 +107,7 @@ ReadoutDatabase::ReadoutDatabase(const char* cx, int v, const LogCallback& cb) { log("Using database " + std::string(db_db) + "@" + std::string(db_host)); // try to connect - if (mysql_real_connect(db,db_host,db_user,db_pwd,db_db,0,nullptr,0)==nullptr) { - goto open_failed; - } - - - if (mysql_options(db, MYSQL_OPT_RECONNECT, &reconnect)) { + if (this->connect()) { goto open_failed; } @@ -167,14 +187,27 @@ int ReadoutDatabase::query(int maxRetry, const char *inQuery,...) { int i; for (i=1; i<=maxRetry; i++) { - if (mysql_query(db,query)==0) break; + if (mysql_query(db,query) == 0) break; lastError = std::string("DB query error :") + mysql_error(db); + log("DB error: " + std::to_string(mysql_errno(db)) + " = " + lastError); + if (mysql_errno(db) == CR_SERVER_LOST) { + log("DB trying to reconnect"); + int err = connect(); + if (err == 0) { + continue; + } else { + if (err == -2) { + log("DB reconnect - need to wait a bit before retry"); + } + return -1; + } + } usleep(retryTimeout); } if (i > maxRetry) { return -1; } - + log("DB query success"); return 0; } diff --git a/src/ReadoutDatabase.h b/src/ReadoutDatabase.h index 3edb811a..d4186ae0 100644 --- a/src/ReadoutDatabase.h +++ b/src/ReadoutDatabase.h @@ -42,6 +42,11 @@ class ReadoutDatabase { uint64_t vRun; std::string vRole; std::string cxDbName; // name of the database used + std::string cxDbUser; + std::string cxDbPwd; + std::string cxDbHost; + time_t lastConnectTime = 0; + int reconnectTimeout = 10; int maxRetry = 20; // number of query retries int retryTimeout = 50; // retry interval (milliseconds) @@ -49,6 +54,8 @@ class ReadoutDatabase { LogCallback theLogCallback; void log(const std::string &log); + int connect(); + std::string lastError = ""; // error string of last query, if any std::string lastQuery = ""; // last query executed }; From a77dca24fc4a269b65368b541c4e330dc1a18d18 Mon Sep 17 00:00:00 2001 From: Sylvain Chapeland Date: Mon, 6 May 2024 17:00:37 +0200 Subject: [PATCH 2/2] v2.23.1 --- doc/releaseNotes.md | 2 +- src/ReadoutVersion.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/releaseNotes.md b/doc/releaseNotes.md index 5d2dcb81..9e934f8e 100644 --- a/doc/releaseNotes.md +++ b/doc/releaseNotes.md @@ -616,5 +616,5 @@ This file describes the main feature changes for each readout.exe released versi ## v2.23.0 - 18/03/2024 - Added o2-readout-rawmerger: a utility to concatenate multiple raw files in a single one, e.g. to replay full detector data from a single FLP. -## next version +## v2.23.1 - 06/05/2024 - Fix MySQL 8.0.34 depracation warning for auto-reconnect feature. Functionnality re-implemented locally. diff --git a/src/ReadoutVersion.h b/src/ReadoutVersion.h index 3e23dd8a..2664121d 100644 --- a/src/ReadoutVersion.h +++ b/src/ReadoutVersion.h @@ -9,5 +9,5 @@ // granted to it by virtue of its status as an Intergovernmental Organization // or submit itself to any jurisdiction. -#define READOUT_VERSION "2.23.0" +#define READOUT_VERSION "2.23.1"