Core/Database: Replace DatabaseWorker with asio io_context

This commit is contained in:
Shauren
2023-12-15 12:06:59 +01:00
parent ee95a5e00f
commit d958bfd0f3
19 changed files with 236 additions and 402 deletions

View File

@@ -70,9 +70,9 @@ void TransactionBase::Cleanup()
_cleanedUp = true;
}
bool TransactionTask::Execute()
bool TransactionTask::Execute(MySQLConnection* conn, std::shared_ptr<TransactionBase> trans)
{
int errorCode = TryExecute();
int errorCode = TryExecute(conn, trans);
if (!errorCode)
return true;
@@ -91,7 +91,7 @@ bool TransactionTask::Execute()
for (uint32 loopDuration = 0, startMSTime = getMSTime(); loopDuration <= DEADLOCK_MAX_RETRY_TIME_MS; loopDuration = GetMSTimeDiffToNow(startMSTime))
{
if (!TryExecute())
if (!TryExecute(conn, trans))
return true;
TC_LOG_WARN("sql.sql", "Deadlocked SQL Transaction, retrying. Loop timer: {} ms, Thread Id: {}", loopDuration, threadId);
@@ -101,61 +101,14 @@ bool TransactionTask::Execute()
}
// Clean up now.
CleanupOnFailure();
trans->Cleanup();
return false;
}
int TransactionTask::TryExecute()
int TransactionTask::TryExecute(MySQLConnection* conn, std::shared_ptr<TransactionBase> trans)
{
return m_conn->ExecuteTransaction(m_trans);
}
void TransactionTask::CleanupOnFailure()
{
m_trans->Cleanup();
}
bool TransactionWithResultTask::Execute()
{
int errorCode = TryExecute();
if (!errorCode)
{
m_result.set_value(true);
return true;
}
if (errorCode == ER_LOCK_DEADLOCK)
{
std::string threadId = []()
{
// wrapped in lambda to fix false positive analysis warning C26115
std::ostringstream threadIdStream;
threadIdStream << std::this_thread::get_id();
return threadIdStream.str();
}();
// Make sure only 1 async thread retries a transaction so they don't keep dead-locking each other
std::lock_guard<std::mutex> lock(_deadlockLock);
for (uint32 loopDuration = 0, startMSTime = getMSTime(); loopDuration <= DEADLOCK_MAX_RETRY_TIME_MS; loopDuration = GetMSTimeDiffToNow(startMSTime))
{
if (!TryExecute())
{
m_result.set_value(true);
return true;
}
TC_LOG_WARN("sql.sql", "Deadlocked SQL Transaction, retrying. Loop timer: {} ms, Thread Id: {}", loopDuration, threadId);
}
TC_LOG_ERROR("sql.sql", "Fatal deadlocked SQL Transaction, it will not be retried anymore. Thread Id: {}", threadId);
}
// Clean up now.
CleanupOnFailure();
m_result.set_value(false);
return false;
return conn->ExecuteTransaction(trans);
}
bool TransactionCallback::InvokeIfReady()