Skip to content

Commit fabe43b

Browse files
authored
Merge pull request #34 from sy-c/master
fix in server dispatch threads init
2 parents 154b9de + c49935d commit fabe43b

File tree

5 files changed

+27
-16
lines changed

5 files changed

+27
-16
lines changed

doc/releaseNotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ This file describes the main feature changes for each InfoLogger released versio
2626

2727
## next version
2828
- added header file InfoLoggerErrorCodes.h to reference centrally the definition of error code ranges reserved specifically for each o2 module, and possibly the individual description / documentation of each error code.
29+
- fix in infoLoggerServer threads initialization.

src/InfoLoggerDispatch.cxx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616

1717
InfoLoggerDispatch::InfoLoggerDispatch(ConfigInfoLoggerServer* vConfig, SimpleLog* vLog)
1818
{
19-
dispatchThread = std::make_unique<Thread>(InfoLoggerDispatch::threadCallback, this);
2019
input = std::make_unique<AliceO2::Common::Fifo<std::shared_ptr<InfoLoggerMessageList>>>(vConfig->dbDispatchQueueSize);
21-
dispatchThread->start();
2220
if (vLog != NULL) {
2321
theLog = vLog;
2422
} else {
2523
theLog = &defaultLog;
2624
}
2725
theConfig = vConfig;
26+
dispatchThread = std::make_unique<Thread>(InfoLoggerDispatch::threadCallback, this, "InfoLoggerDispatch", 50000);
27+
dispatchThread->start();
2828
}
2929

3030
InfoLoggerDispatch::~InfoLoggerDispatch()
@@ -54,6 +54,10 @@ Thread::CallbackResult InfoLoggerDispatch::threadCallback(void* arg)
5454
return Thread::CallbackResult::Error;
5555
}
5656

57+
if (!dPtr->isReady) {
58+
return Thread::CallbackResult::Idle;
59+
}
60+
5761
int nMsgProcessed = 0;
5862

5963
if (dPtr->customLoop()) {

src/InfoLoggerDispatch.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ class InfoLoggerDispatch
5252
SimpleLog defaultLog;
5353

5454
ConfigInfoLoggerServer* theConfig;
55+
56+
bool isReady = false; // this flag must be set to true by derived instances when ready.
57+
// customloop will not be called unless set.
5558
};
5659

5760
class InfoLoggerDispatchPrint : public InfoLoggerDispatch

src/InfoLoggerDispatchBrowser.cxx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ InfoLoggerDispatchOnlineBrowser::InfoLoggerDispatchOnlineBrowser(ConfigInfoLogge
8181
throw __LINE__;
8282
}
8383
//theLog.info("%s() success\n",__FUNCTION__);
84+
85+
// enable customloop callback
86+
isReady = true;
8487
}
8588
InfoLoggerDispatchOnlineBrowser::~InfoLoggerDispatchOnlineBrowser()
8689
{

src/InfoLoggerDispatchSQL.cxx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ typedef bool my_bool;
2121
#endif
2222

2323
// some constants
24-
#define SQL_RETRY_CONNECT 1 // base retry time, will be sleeping up to 10x this value
24+
#define SQL_RETRY_CONNECT 1 // SQL database connect retry time
2525

2626
class InfoLoggerDispatchSQLImpl
2727
{
@@ -103,14 +103,7 @@ void InfoLoggerDispatchSQLImpl::start()
103103
}
104104

105105
// try to connect DB
106-
int maxRetry = 10;
107-
for (int n = 0; n < maxRetry; n++) {
108-
InfoLoggerDispatchSQLImpl::connectDB();
109-
if (dbIsConnected) {
110-
break;
111-
}
112-
sleep(SQL_RETRY_CONNECT);
113-
}
106+
// done automatically in customloop
114107
}
115108

116109
InfoLoggerDispatchSQL::InfoLoggerDispatchSQL(ConfigInfoLoggerServer* config, SimpleLog* log) : InfoLoggerDispatch(config, log)
@@ -119,6 +112,9 @@ InfoLoggerDispatchSQL::InfoLoggerDispatchSQL(ConfigInfoLoggerServer* config, Sim
119112
dPtr->theLog = log;
120113
dPtr->theConfig = config;
121114
dPtr->start();
115+
116+
// enable customloop callback
117+
isReady = true;
122118
}
123119

124120
void InfoLoggerDispatchSQLImpl::stop()
@@ -152,19 +148,19 @@ int InfoLoggerDispatchSQLImpl::connectDB()
152148
time_t now = time(NULL);
153149
if (now < dbLastConnectTry + SQL_RETRY_CONNECT) {
154150
// wait before reconnecting
155-
return 0;
151+
return 1;
156152
}
157153
dbLastConnectTry = now;
158154
if (mysql_real_connect(&db, theConfig->dbHost.c_str(), theConfig->dbUser.c_str(), theConfig->dbPassword.c_str(), theConfig->dbName.c_str(), 0, NULL, 0)) {
159155
theLog->info("DB connected");
160156
dbIsConnected = 1;
161157
dbConnectTrials = 0;
162158
} else {
163-
if (dbConnectTrials == 1) { // the first attempt always fails, hide it
159+
if (dbConnectTrials == 0) { // log only first attempt
164160
theLog->error("DB connection failed: %s", mysql_error(&db));
165161
}
166162
dbConnectTrials++;
167-
return 0;
163+
return 1;
168164
}
169165

170166
// create prepared insert statement
@@ -209,8 +205,12 @@ int InfoLoggerDispatchSQLImpl::connectDB()
209205

210206
int InfoLoggerDispatchSQLImpl::customLoop()
211207
{
212-
213-
return connectDB();
208+
int err = connectDB();
209+
if (err) {
210+
// temporization to avoid immediate retry
211+
sleep(SQL_RETRY_CONNECT);
212+
}
213+
return err;
214214
}
215215

216216
int InfoLoggerDispatchSQLImpl::customMessageProcess(std::shared_ptr<InfoLoggerMessageList> lmsg)

0 commit comments

Comments
 (0)