Skip to content

Commit 426e76c

Browse files
authored
Merge pull request #276 from sy-c/master
v2.23.1
2 parents 51dd321 + a77dca2 commit 426e76c

File tree

4 files changed

+53
-10
lines changed

4 files changed

+53
-10
lines changed

doc/releaseNotes.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,3 +615,6 @@ This file describes the main feature changes for each readout.exe released versi
615615

616616
## v2.23.0 - 18/03/2024
617617
- 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.
618+
619+
## v2.23.1 - 06/05/2024
620+
- Fix MySQL 8.0.34 depracation warning for auto-reconnect feature. Functionnality re-implemented locally.

src/ReadoutDatabase.cxx

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "ReadoutDatabase.h"
22
#include <mysql.h>
3+
#include <mysql/errmsg.h>
34
#include <string.h>
45
#include <stdlib.h>
56
#include <stdarg.h>
@@ -8,18 +9,39 @@
89
#include <inttypes.h>
910
#include <string>
1011
#include <map>
12+
#include <time.h>
1113

1214
#if LIBMYSQL_VERSION_ID >= 80000
1315
typedef bool my_bool;
1416
#endif
1517

18+
int ReadoutDatabase::connect() {
19+
time_t now = time(NULL);
20+
if ((lastConnectTime != 0) && (now < lastConnectTime + reconnectTimeout)) return -2;
21+
lastConnectTime = now;
22+
if (db != nullptr) {
23+
mysql_close(db);
24+
db = nullptr;
25+
}
26+
db = mysql_init(nullptr);
27+
if (db == nullptr) {
28+
return -1;
29+
}
30+
if (mysql_real_connect(db,cxDbHost.c_str(),cxDbUser.c_str(),cxDbPwd.c_str(),cxDbName.c_str(),0,nullptr,0) == nullptr) {
31+
log(std::string("DB connect error :") + mysql_error(db));
32+
return -1;
33+
}
34+
log("DB connected");
35+
lastConnectTime = 0;
36+
return 0;
37+
}
38+
1639
ReadoutDatabase::ReadoutDatabase(const char* cx, int v, const LogCallback& cb) {
1740
verbose = v;
1841
theLogCallback = cb;
1942

2043
char *db_db=nullptr, *db_user=nullptr, *db_pwd=nullptr, *db_host=nullptr;
2144
char *p=nullptr,*ptr,*lptr;
22-
my_bool reconnect = 1;
2345

2446
if (cx == nullptr) {
2547
throw __LINE__;
@@ -50,6 +72,7 @@ ReadoutDatabase::ReadoutDatabase(const char* cx, int v, const LogCallback& cb) {
5072
break;
5173
}
5274
}
75+
cxDbUser = db_user;
5376

5477
// pwd
5578
for (lptr=ptr;*ptr!=0;ptr++) {
@@ -60,6 +83,7 @@ ReadoutDatabase::ReadoutDatabase(const char* cx, int v, const LogCallback& cb) {
6083
break;
6184
}
6285
}
86+
cxDbPwd = db_pwd;
6387

6488
// host
6589
for (lptr=ptr;*ptr!=0;ptr++) {
@@ -70,6 +94,7 @@ ReadoutDatabase::ReadoutDatabase(const char* cx, int v, const LogCallback& cb) {
7094
break;
7195
}
7296
}
97+
cxDbHost = db_host;
7398

7499
// db name
75100
db_db=ptr;
@@ -82,12 +107,7 @@ ReadoutDatabase::ReadoutDatabase(const char* cx, int v, const LogCallback& cb) {
82107
log("Using database " + std::string(db_db) + "@" + std::string(db_host));
83108

84109
// try to connect
85-
if (mysql_real_connect(db,db_host,db_user,db_pwd,db_db,0,nullptr,0)==nullptr) {
86-
goto open_failed;
87-
}
88-
89-
90-
if (mysql_options(db, MYSQL_OPT_RECONNECT, &reconnect)) {
110+
if (this->connect()) {
91111
goto open_failed;
92112
}
93113

@@ -167,14 +187,27 @@ int ReadoutDatabase::query(int maxRetry, const char *inQuery,...) {
167187

168188
int i;
169189
for (i=1; i<=maxRetry; i++) {
170-
if (mysql_query(db,query)==0) break;
190+
if (mysql_query(db,query) == 0) break;
171191
lastError = std::string("DB query error :") + mysql_error(db);
192+
log("DB error: " + std::to_string(mysql_errno(db)) + " = " + lastError);
193+
if (mysql_errno(db) == CR_SERVER_LOST) {
194+
log("DB trying to reconnect");
195+
int err = connect();
196+
if (err == 0) {
197+
continue;
198+
} else {
199+
if (err == -2) {
200+
log("DB reconnect - need to wait a bit before retry");
201+
}
202+
return -1;
203+
}
204+
}
172205
usleep(retryTimeout);
173206
}
174207
if (i > maxRetry) {
175208
return -1;
176209
}
177-
210+
log("DB query success");
178211
return 0;
179212
}
180213

src/ReadoutDatabase.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,20 @@ class ReadoutDatabase {
4242
uint64_t vRun;
4343
std::string vRole;
4444
std::string cxDbName; // name of the database used
45+
std::string cxDbUser;
46+
std::string cxDbPwd;
47+
std::string cxDbHost;
48+
time_t lastConnectTime = 0;
49+
int reconnectTimeout = 10;
4550

4651
int maxRetry = 20; // number of query retries
4752
int retryTimeout = 50; // retry interval (milliseconds)
4853

4954
LogCallback theLogCallback;
5055
void log(const std::string &log);
5156

57+
int connect();
58+
5259
std::string lastError = ""; // error string of last query, if any
5360
std::string lastQuery = ""; // last query executed
5461
};

src/ReadoutVersion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
// granted to it by virtue of its status as an Intergovernmental Organization
1010
// or submit itself to any jurisdiction.
1111

12-
#define READOUT_VERSION "2.23.0"
12+
#define READOUT_VERSION "2.23.1"
1313

0 commit comments

Comments
 (0)