do not always try to connect to unreachable sqlservers

but instead remember the last reachable server
- some cleanup
- added timeout of 10 seconds to sql-connections
This commit is contained in:
H-M-H 2016-01-28 17:46:51 +01:00
parent 783c2161f1
commit 2bd6d0c381
6 changed files with 36 additions and 27 deletions

View file

@ -42,11 +42,6 @@
#include <windows.h>
#endif
#ifdef CONF_SQL
CSqlServer** CSqlConnector::ms_ppSqlReadServers = 0;
CSqlServer** CSqlConnector::ms_ppSqlWriteServers = 0;
#endif
static const char *StrLtrim(const char *pStr)
{
while(*pStr)
@ -2049,7 +2044,7 @@ void CServer::ConAddSqlServer(IConsole::IResult *pResult, void *pUserData)
{
if (!apSqlServers[i])
{
apSqlServers[i] = new CSqlServer(pResult->GetString(1), pResult->GetString(2), pResult->GetString(3), pResult->GetString(4), pResult->GetString(5), pResult->GetInteger(6), SetUpDb);
apSqlServers[i] = new CSqlServer(pResult->GetString(1), pResult->GetString(2), pResult->GetString(3), pResult->GetString(4), pResult->GetString(5), pResult->GetInteger(6), ReadOnly, SetUpDb);
if(SetUpDb)
{
@ -2063,7 +2058,7 @@ void CServer::ConAddSqlServer(IConsole::IResult *pResult, void *pUserData)
return;
}
}
pSelf->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", "failed to add new sqlmaster: limit of sqlmasters reached");
pSelf->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", "failed to add new sqlserver: limit of sqlservers reached");
}
void CServer::ConDumpSqlServers(IConsole::IResult *pResult, void *pUserData)

View file

@ -4,6 +4,12 @@
#include "sql_connector.h"
CSqlServer** CSqlConnector::ms_ppSqlReadServers = 0;
CSqlServer** CSqlConnector::ms_ppSqlWriteServers = 0;
int CSqlConnector::ms_ReachableReadServer = 0;
int CSqlConnector::ms_ReachableWriteServer = 0;
CSqlConnector::CSqlConnector() :
m_pSqlServer(0),
m_NumReadRetries(0),
@ -13,28 +19,21 @@ m_NumWriteRetries(0)
bool CSqlConnector::ConnectSqlServer(bool ReadOnly)
{
ReadOnly ? ++m_NumReadRetries : ++m_NumWriteRetries;
int& ReachableServer = ReadOnly ? ms_ReachableReadServer : ms_ReachableWriteServer;
int NumServers = ReadOnly ? CSqlServer::ms_NumReadServer : CSqlServer::ms_NumWriteServer;
bool passedOldServer = !m_pSqlServer;
for (int i = 0; i < MAX_SQLSERVERS && SqlServer(i, ReadOnly); i++)
for (int i = ReachableServer, ID = ReachableServer; i < ReachableServer + NumServers && SqlServer(i % NumServers, ReadOnly); i++, ID = i % NumServers)
{
if (!passedOldServer)
if (SqlServer(ID, ReadOnly) && SqlServer(ID, ReadOnly)->Connect())
{
if (m_pSqlServer == SqlServer(i, ReadOnly))
passedOldServer = true;
continue;
}
if (SqlServer(i, ReadOnly) && SqlServer(i, ReadOnly)->Connect())
{
m_pSqlServer = SqlServer(i, ReadOnly);
m_pSqlServer = SqlServer(ID, ReadOnly);
ReachableServer = ID;
return true;
}
if (SqlServer(i, ReadOnly))
dbg_msg("SQL", "Warning: Unable to connect to Sql%sServer %d ('%s'), trying next...", ReadOnly ? "read" : "write", i, SqlServer(i, ReadOnly)->GetIP());
if (SqlServer(ID, ReadOnly))
dbg_msg("SQL", "Warning: Unable to connect to Sql%sServer %d ('%s'), trying next...", ReadOnly ? "Read" : "Write", ID, SqlServer(ID, ReadOnly)->GetIP());
}
dbg_msg("SQL", "ERROR: No Sql%sServers available", ReadOnly ? "Read" : "Write");
dbg_msg("SQL", "FATAL ERROR: No Sql%sServers available", ReadOnly ? "Read" : "Write");
m_pSqlServer = 0;
return false;
}

View file

@ -24,7 +24,7 @@ public:
bool ConnectSqlServer(bool ReadOnly = true);
bool MaxTriesReached(bool ReadOnly = true) { return ReadOnly ? m_NumReadRetries : m_NumWriteRetries >= MAX_SQLSERVERS; }
bool MaxTriesReached(bool ReadOnly = true) { return ReadOnly ? m_NumReadRetries >= CSqlServer::ms_NumReadServer : m_NumWriteRetries >= CSqlServer::ms_NumWriteServer; }
private:
@ -32,6 +32,12 @@ private:
static CSqlServer **ms_ppSqlReadServers;
static CSqlServer **ms_ppSqlWriteServers;
static int ms_NumReadServer;
static int ms_NumWriteServer;
static int ms_ReachableReadServer;
static int ms_ReachableWriteServer;
int m_NumReadRetries;
int m_NumWriteRetries;
};

View file

@ -7,7 +7,10 @@
#include "sql_server.h"
CSqlServer::CSqlServer(const char* pDatabase, const char* pPrefix, const char* pUser, const char* pPass, const char* pIp, int Port, bool SetUpDb) :
int CSqlServer::ms_NumReadServer = 0;
int CSqlServer::ms_NumWriteServer = 0;
CSqlServer::CSqlServer(const char* pDatabase, const char* pPrefix, const char* pUser, const char* pPass, const char* pIp, int Port, bool ReadOnly, bool SetUpDb) :
m_Port(Port),
m_SetUpDB(SetUpDb)
{
@ -22,6 +25,8 @@ CSqlServer::CSqlServer(const char* pDatabase, const char* pPrefix, const char* p
m_pResults = 0;
m_pStatement = 0;
ReadOnly ? ms_NumReadServer++ : ms_NumWriteServer++;
m_SqlLock = lock_create();
}
@ -78,6 +83,7 @@ bool CSqlServer::Connect()
connection_properties["port"] = m_Port;
connection_properties["userName"] = sql::SQLString(m_aUser);
connection_properties["password"] = sql::SQLString(m_aPass);
connection_properties["OPT_CONNECT_TIMEOUT"] = 10;
connection_properties["OPT_RECONNECT"] = true;
// Create connection

View file

@ -10,7 +10,7 @@
class CSqlServer
{
public:
CSqlServer(const char* pDatabase, const char* pPrefix, const char* pUser, const char* pPass, const char* pIp, int Port, bool SetUpDb = false);
CSqlServer(const char* pDatabase, const char* pPrefix, const char* pUser, const char* pPass, const char* pIp, int Port, bool ReadOnly = true, bool SetUpDb = false);
~CSqlServer();
bool Connect();
@ -32,6 +32,8 @@ public:
void Lock() { lock_wait(m_SqlLock); }
void UnLock() { lock_unlock(m_SqlLock); }
static int ms_NumReadServer;
static int ms_NumWriteServer;
private:
sql::Driver *m_pDriver;

View file

@ -861,6 +861,7 @@ bool CSqlScore::ShowTop5Thread(CSqlServer* pSqlServer, CSqlData *pGameData, bool
pData->GameServer()->SendChatTarget(pData->m_ClientID, "-------------------------------");
dbg_msg("SQL", "Showing top5 done");
return true;
}
catch (sql::SQLException &e)
{