Merge pull request #2623 from Zwelf/pr-mysql-enhance-unreachable-server-situation

MySQL enhance unreachable server situation
This commit is contained in:
Dennis Felsing 2020-08-21 20:15:49 +02:00 committed by GitHub
commit b2ebcb4f3c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -133,6 +133,9 @@ void CDbConnectionPool::Worker(void *pUser)
void CDbConnectionPool::Worker() void CDbConnectionPool::Worker()
{ {
// remember last working server and try to connect to it first
int ReadServer = 0;
int WriteServer = 0;
while(1) while(1)
{ {
m_NumElem.wait(); m_NumElem.wait();
@ -151,8 +154,11 @@ void CDbConnectionPool::Worker()
{ {
for(int i = 0; i < (int)m_aapDbConnections[Mode::READ].size(); i++) for(int i = 0; i < (int)m_aapDbConnections[Mode::READ].size(); i++)
{ {
if(ExecSqlFunc(m_aapDbConnections[Mode::READ][i].get(), pThreadData.get(), false)) int CurServer = (ReadServer + i) % (int)m_aapDbConnections[Mode::READ].size();
if(ExecSqlFunc(m_aapDbConnections[Mode::READ][CurServer].get(), pThreadData.get(), false))
{ {
ReadServer = CurServer;
dbg_msg("sql", "%s done on read database %d", pThreadData->m_pName, CurServer);
Success = true; Success = true;
break; break;
} }
@ -162,8 +168,11 @@ void CDbConnectionPool::Worker()
{ {
for(int i = 0; i < (int)m_aapDbConnections[Mode::WRITE].size(); i++) for(int i = 0; i < (int)m_aapDbConnections[Mode::WRITE].size(); i++)
{ {
int CurServer = (WriteServer + i) % (int)m_aapDbConnections[Mode::READ].size();
if(ExecSqlFunc(m_aapDbConnections[Mode::WRITE][i].get(), pThreadData.get(), false)) if(ExecSqlFunc(m_aapDbConnections[Mode::WRITE][i].get(), pThreadData.get(), false))
{ {
WriteServer = CurServer;
dbg_msg("sql", "%s done on write database %d", pThreadData->m_pName, CurServer);
Success = true; Success = true;
break; break;
} }
@ -174,6 +183,7 @@ void CDbConnectionPool::Worker()
{ {
if(ExecSqlFunc(m_aapDbConnections[Mode::WRITE_BACKUP][i].get(), pThreadData.get(), true)) if(ExecSqlFunc(m_aapDbConnections[Mode::WRITE_BACKUP][i].get(), pThreadData.get(), true))
{ {
dbg_msg("sql", "%s done on write backup database %d", pThreadData->m_pName, i);
Success = true; Success = true;
break; break;
} }
@ -181,8 +191,8 @@ void CDbConnectionPool::Worker()
} }
} break; } break;
} }
if(Success) if(!Success)
dbg_msg("sql", "%s done", pThreadData->m_pName); dbg_msg("sql", "%s failed on all databases", pThreadData->m_pName);
} }
} }
@ -208,18 +218,35 @@ bool CDbConnectionPool::ExecSqlFunc(IDbConnection *pConnection, CSqlExecData *pD
#if defined(CONF_SQL) #if defined(CONF_SQL)
catch (sql::SQLException &e) catch (sql::SQLException &e)
{ {
dbg_msg("sql", "MySQL Error: %s", e.what()); dbg_msg("sql", "%s MySQL Error: %s", pData->m_pName, e.what());
} }
#endif #endif
catch (std::runtime_error &e) catch (std::runtime_error &e)
{ {
dbg_msg("sql", "SQLite Error: %s", e.what()); dbg_msg("sql", "%s SQLite Error: %s", pData->m_pName, e.what());
} }
catch (...) catch (...)
{ {
dbg_msg("sql", "Unexpected exception caught"); dbg_msg("sql", "%s Unexpected exception caught", pData->m_pName);
}
try
{
pConnection->Unlock();
}
#if defined(CONF_SQL)
catch (sql::SQLException &e)
{
dbg_msg("sql", "%s MySQL Error during unlock: %s", pData->m_pName, e.what());
}
#endif
catch (std::runtime_error &e)
{
dbg_msg("sql", "%s SQLite Error during unlock: %s", pData->m_pName, e.what());
}
catch (...)
{
dbg_msg("sql", "%s Unexpected exception caught during unlock", pData->m_pName);
} }
pConnection->Unlock();
pConnection->Disconnect(); pConnection->Disconnect();
if(!Success) if(!Success)
dbg_msg("sql", "%s failed", pData->m_pName); dbg_msg("sql", "%s failed", pData->m_pName);