diff options
author | Giacomo Pozzoni <giacomopoz@gmail.com> | 2020-05-30 15:43:54 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-30 17:43:54 +0200 |
commit | 141ada19b00fb9d0dd1a260d86d97bbb709a0f5e (patch) | |
tree | 03c8559a245f404d7d84655fe5c5066ec22e2ee7 /src/server/database/Database/Transaction.cpp | |
parent | 00bc6e7d54e924dabaf4636a4b8084f0db067194 (diff) |
Cherry pick some db commits (#24713)
* Core/DBLayer: Implement async transaction completion callbacks
(cherry picked from commit 0f0ca3a9194d76afa0227943e86469ad8368c5e2)
# Conflicts:
# src/server/bnetserver/REST/LoginRESTService.cpp
# src/server/bnetserver/Server/Session.cpp
# src/server/bnetserver/Server/Session.h
# src/server/database/Database/Transaction.cpp
# src/server/game/Handlers/CharacterHandler.cpp
# src/server/game/Handlers/SpellHandler.cpp
# src/server/game/Server/WorldSession.cpp
# src/server/game/Server/WorldSocket.cpp
# src/server/game/Server/WorldSocket.h
* Core/Misc: Fixed char enum packet sometimes not showing newly created character when client latency is too low
(cherry picked from commit fc9d2e728e5c21bd2a1645a29be60d0e280ad940)
# Conflicts:
# src/server/game/Handlers/CharacterHandler.cpp
* Fix no-pch build
* Core/Player: Wait for correct transaction on character creation
(cherry picked from commit 01c68b2aa2ed25ea9b4c007f238a0e958f6525b1)
# Conflicts:
# src/server/game/Entities/Player/Player.cpp
* Fix warning
* Remove unused login transaction
Co-authored-by: Shauren <shauren.trinity@gmail.com>
Diffstat (limited to 'src/server/database/Database/Transaction.cpp')
-rw-r--r-- | src/server/database/Database/Transaction.cpp | 56 |
1 files changed, 54 insertions, 2 deletions
diff --git a/src/server/database/Database/Transaction.cpp b/src/server/database/Database/Transaction.cpp index be5c28bceb3..9b2178d2c4b 100644 --- a/src/server/database/Database/Transaction.cpp +++ b/src/server/database/Database/Transaction.cpp @@ -71,7 +71,7 @@ void TransactionBase::Cleanup() bool TransactionTask::Execute() { - int errorCode = m_conn->ExecuteTransaction(m_trans); + int errorCode = TryExecute(); if (!errorCode) return true; @@ -86,7 +86,7 @@ bool TransactionTask::Execute() for (uint32 loopDuration = 0, startMSTime = getMSTime(); loopDuration <= DEADLOCK_MAX_RETRY_TIME_MS; loopDuration = GetMSTimeDiffToNow(startMSTime)) { - if (!m_conn->ExecuteTransaction(m_trans)) + if (!TryExecute()) return true; TC_LOG_WARN("sql.sql", "Deadlocked SQL Transaction, retrying. Loop timer: %u ms, Thread Id: %s", loopDuration, threadId.c_str()); @@ -96,7 +96,59 @@ bool TransactionTask::Execute() } // Clean up now. + CleanupOnFailure(); + + return false; +} + +int TransactionTask::TryExecute() +{ + 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) + { + // 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); + uint8 loopBreaker = 5; // Handle MySQL Errno 1213 without extending deadlock to the core itself + for (uint8 i = 0; i < loopBreaker; ++i) + { + if (!TryExecute()) + { + m_result.set_value(true); + return true; + } + } + } + + // Clean up now. + CleanupOnFailure(); + m_result.set_value(false); + + return false; +} + +bool TransactionCallback::InvokeIfReady() +{ + if (m_future.valid() && m_future.wait_for(std::chrono::seconds(0)) == std::future_status::ready) + { + m_callback(m_future.get()); + return true; + } return false; } |