From 53ce87d0f7db456231b662d298d1c2cc7add37f9 Mon Sep 17 00:00:00 2001 From: Kargatum Date: Mon, 12 Apr 2021 15:09:13 +0700 Subject: feat(Core/Database): implement db loader (#4431) --- src/common/Database/DatabaseWorkerPool.cpp | 141 +++++++++++++++++++++++------ 1 file changed, 111 insertions(+), 30 deletions(-) (limited to 'src/common/Database/DatabaseWorkerPool.cpp') diff --git a/src/common/Database/DatabaseWorkerPool.cpp b/src/common/Database/DatabaseWorkerPool.cpp index 30ef85e0e0..1b5add5469 100644 --- a/src/common/Database/DatabaseWorkerPool.cpp +++ b/src/common/Database/DatabaseWorkerPool.cpp @@ -12,7 +12,9 @@ template DatabaseWorkerPool::DatabaseWorkerPool() : _mqueue(new ACE_Message_Queue(2 * 1024 * 1024, 2 * 1024 * 1024)), - _queue(new ACE_Activation_Queue(_mqueue)) + _queue(new ACE_Activation_Queue(_mqueue)), + _async_threads(0), + _synch_threads(0) { memset(_connectionCount, 0, sizeof(_connectionCount)); _connections.resize(IDX_SIZE); @@ -22,45 +24,39 @@ template DatabaseWorkerPool::DatabaseWorkerPool() : } template -bool DatabaseWorkerPool::Open(const std::string& infoString, uint8 async_threads, uint8 synch_threads) +void DatabaseWorkerPool::SetConnectionInfo(std::string const& infoString, + uint8 const asyncThreads, uint8 const synchThreads) { - bool res = true; - _connectionInfo = MySQLConnectionInfo(infoString); + _connectionInfo = std::make_unique(infoString); + + _async_threads = asyncThreads; + _synch_threads = synchThreads; +} + +template +uint32 DatabaseWorkerPool::Open() +{ + WPFatal(_connectionInfo.get(), "Connection info was not set!"); sLog->outSQLDriver("Opening DatabasePool '%s'. Asynchronous connections: %u, synchronous connections: %u.", - GetDatabaseName(), async_threads, synch_threads); + GetDatabaseName(), _async_threads, _synch_threads); - //! Open asynchronous connections (delayed operations) - _connections[IDX_ASYNC].resize(async_threads); - for (uint8 i = 0; i < async_threads; ++i) - { - T* t = new T(_queue, _connectionInfo); - res &= t->Open(); - if (res) // only check mysql version if connection is valid - WPFatal(mysql_get_server_version(t->GetHandle()) >= MIN_MYSQL_SERVER_VERSION, "AzerothCore does not support MySQL versions below 5.7"); + uint32 error = OpenConnections(IDX_ASYNC, _async_threads); - _connections[IDX_ASYNC][i] = t; - ++_connectionCount[IDX_ASYNC]; + if (error) + { + return error; } - //! Open synchronous connections (direct, blocking operations) - _connections[IDX_SYNCH].resize(synch_threads); - for (uint8 i = 0; i < synch_threads; ++i) + error = OpenConnections(IDX_SYNCH, _synch_threads); + + if (!error) { - T* t = new T(_connectionInfo); - res &= t->Open(); - _connections[IDX_SYNCH][i] = t; - ++_connectionCount[IDX_SYNCH]; + sLog->outSQLDriver("DatabasePool '%s' opened successfully. %u total connections running.", + GetDatabaseName(), (_connectionCount[IDX_SYNCH] + _connectionCount[IDX_ASYNC])); } - if (res) - sLog->outSQLDriver("DatabasePool '%s' opened successfully. %u total connections running.", GetDatabaseName(), - (_connectionCount[IDX_SYNCH] + _connectionCount[IDX_ASYNC])); - else - sLog->outError("DatabasePool %s NOT opened. There were errors opening the MySQL connections. Check your SQLDriverLogFile " - "for specific errors.", GetDatabaseName()); - - return res; + return error; } template @@ -99,6 +95,91 @@ void DatabaseWorkerPool::Close() sLog->outSQLDriver("All connections on DatabasePool '%s' closed.", GetDatabaseName()); } +template +uint32 DatabaseWorkerPool::OpenConnections(InternalIndex type, uint8 numConnections) +{ + _connections[type].resize(numConnections); + for (uint8 i = 0; i < numConnections; ++i) + { + T* t; + + if (type == IDX_ASYNC) + { + t = new T(_queue, *_connectionInfo); + } + else if (type == IDX_SYNCH) + { + t = new T(*_connectionInfo); + } + else + { + ASSERT(false, "> Incorrect InternalIndex (%u)", static_cast(type)); + } + + _connections[type][i] = t; + ++_connectionCount[type]; + + uint32 error = t->Open(); + + if (!error) + { + if (mysql_get_server_version(t->GetHandle()) < MIN_MYSQL_SERVER_VERSION) + { + sLog->outSQLDriver("Not support MySQL versions below 5.7"); + error = 1; + } + } + + // Failed to open a connection or invalid version, abort and cleanup + if (error) + { + while (_connectionCount[type] != 0) + { + T* t = _connections[type][i--]; + delete t; + --_connectionCount[type]; + } + + return error; + } + } + + // Everything is fine + return 0; +} + +template +bool DatabaseWorkerPool::PrepareStatements() +{ + for (uint8 i = 0; i < IDX_SIZE; ++i) + { + for (uint32 c = 0; c < _connectionCount[i]; ++c) + { + T* t = _connections[i][c]; + t->LockIfReady(); + + if (!t->PrepareStatements()) + { + t->Unlock(); + Close(); + return false; + } + else + { + t->Unlock(); + } + } + } + + return true; +} + +template +char const* DatabaseWorkerPool::GetDatabaseName() const +{ + return _connectionInfo->database.c_str(); +} + template void DatabaseWorkerPool::Execute(const char* sql) { -- cgit v1.2.3