diff --git a/src/engine/server/server.cpp b/src/engine/server/server.cpp index 877144294..cb74f024f 100644 --- a/src/engine/server/server.cpp +++ b/src/engine/server/server.cpp @@ -42,11 +42,6 @@ #include #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) diff --git a/src/engine/server/sql_connector.cpp b/src/engine/server/sql_connector.cpp index 0eb262380..30c739e8a 100644 --- a/src/engine/server/sql_connector.cpp +++ b/src/engine/server/sql_connector.cpp @@ -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; } diff --git a/src/engine/server/sql_connector.h b/src/engine/server/sql_connector.h index a51785ed7..6d1d8a036 100644 --- a/src/engine/server/sql_connector.h +++ b/src/engine/server/sql_connector.h @@ -24,14 +24,20 @@ 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: CSqlServer *m_pSqlServer; 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; }; diff --git a/src/engine/server/sql_server.cpp b/src/engine/server/sql_server.cpp index 062f4162a..9dbce7849 100644 --- a/src/engine/server/sql_server.cpp +++ b/src/engine/server/sql_server.cpp @@ -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 diff --git a/src/engine/server/sql_server.h b/src/engine/server/sql_server.h index cf1b55206..775f7cd88 100644 --- a/src/engine/server/sql_server.h +++ b/src/engine/server/sql_server.h @@ -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; diff --git a/src/game/server/score/sql_score.cpp b/src/game/server/score/sql_score.cpp index 84de39575..159904ebb 100644 --- a/src/game/server/score/sql_score.cpp +++ b/src/game/server/score/sql_score.cpp @@ -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) {