Skip to content

Commit

Permalink
Merge pull request #34 from sy-c/master
Browse files Browse the repository at this point in the history
fix in server dispatch threads init
  • Loading branch information
sy-c authored Oct 28, 2019
2 parents 154b9de + c49935d commit fabe43b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
1 change: 1 addition & 0 deletions doc/releaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ This file describes the main feature changes for each InfoLogger released versio

## next version
- 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.
- fix in infoLoggerServer threads initialization.
8 changes: 6 additions & 2 deletions src/InfoLoggerDispatch.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

InfoLoggerDispatch::InfoLoggerDispatch(ConfigInfoLoggerServer* vConfig, SimpleLog* vLog)
{
dispatchThread = std::make_unique<Thread>(InfoLoggerDispatch::threadCallback, this);
input = std::make_unique<AliceO2::Common::Fifo<std::shared_ptr<InfoLoggerMessageList>>>(vConfig->dbDispatchQueueSize);
dispatchThread->start();
if (vLog != NULL) {
theLog = vLog;
} else {
theLog = &defaultLog;
}
theConfig = vConfig;
dispatchThread = std::make_unique<Thread>(InfoLoggerDispatch::threadCallback, this, "InfoLoggerDispatch", 50000);
dispatchThread->start();
}

InfoLoggerDispatch::~InfoLoggerDispatch()
Expand Down Expand Up @@ -54,6 +54,10 @@ Thread::CallbackResult InfoLoggerDispatch::threadCallback(void* arg)
return Thread::CallbackResult::Error;
}

if (!dPtr->isReady) {
return Thread::CallbackResult::Idle;
}

int nMsgProcessed = 0;

if (dPtr->customLoop()) {
Expand Down
3 changes: 3 additions & 0 deletions src/InfoLoggerDispatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class InfoLoggerDispatch
SimpleLog defaultLog;

ConfigInfoLoggerServer* theConfig;

bool isReady = false; // this flag must be set to true by derived instances when ready.
// customloop will not be called unless set.
};

class InfoLoggerDispatchPrint : public InfoLoggerDispatch
Expand Down
3 changes: 3 additions & 0 deletions src/InfoLoggerDispatchBrowser.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ InfoLoggerDispatchOnlineBrowser::InfoLoggerDispatchOnlineBrowser(ConfigInfoLogge
throw __LINE__;
}
//theLog.info("%s() success\n",__FUNCTION__);

// enable customloop callback
isReady = true;
}
InfoLoggerDispatchOnlineBrowser::~InfoLoggerDispatchOnlineBrowser()
{
Expand Down
28 changes: 14 additions & 14 deletions src/InfoLoggerDispatchSQL.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ typedef bool my_bool;
#endif

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

class InfoLoggerDispatchSQLImpl
{
Expand Down Expand Up @@ -103,14 +103,7 @@ void InfoLoggerDispatchSQLImpl::start()
}

// try to connect DB
int maxRetry = 10;
for (int n = 0; n < maxRetry; n++) {
InfoLoggerDispatchSQLImpl::connectDB();
if (dbIsConnected) {
break;
}
sleep(SQL_RETRY_CONNECT);
}
// done automatically in customloop
}

InfoLoggerDispatchSQL::InfoLoggerDispatchSQL(ConfigInfoLoggerServer* config, SimpleLog* log) : InfoLoggerDispatch(config, log)
Expand All @@ -119,6 +112,9 @@ InfoLoggerDispatchSQL::InfoLoggerDispatchSQL(ConfigInfoLoggerServer* config, Sim
dPtr->theLog = log;
dPtr->theConfig = config;
dPtr->start();

// enable customloop callback
isReady = true;
}

void InfoLoggerDispatchSQLImpl::stop()
Expand Down Expand Up @@ -152,19 +148,19 @@ int InfoLoggerDispatchSQLImpl::connectDB()
time_t now = time(NULL);
if (now < dbLastConnectTry + SQL_RETRY_CONNECT) {
// wait before reconnecting
return 0;
return 1;
}
dbLastConnectTry = now;
if (mysql_real_connect(&db, theConfig->dbHost.c_str(), theConfig->dbUser.c_str(), theConfig->dbPassword.c_str(), theConfig->dbName.c_str(), 0, NULL, 0)) {
theLog->info("DB connected");
dbIsConnected = 1;
dbConnectTrials = 0;
} else {
if (dbConnectTrials == 1) { // the first attempt always fails, hide it
if (dbConnectTrials == 0) { // log only first attempt
theLog->error("DB connection failed: %s", mysql_error(&db));
}
dbConnectTrials++;
return 0;
return 1;
}

// create prepared insert statement
Expand Down Expand Up @@ -209,8 +205,12 @@ int InfoLoggerDispatchSQLImpl::connectDB()

int InfoLoggerDispatchSQLImpl::customLoop()
{

return connectDB();
int err = connectDB();
if (err) {
// temporization to avoid immediate retry
sleep(SQL_RETRY_CONNECT);
}
return err;
}

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

0 comments on commit fabe43b

Please sign in to comment.