Core/DBLayer: Implement async transaction completion callbacks
Port From (https://github.com/TrinityCore/TrinityCore/commit/0f0ca3a9194d76afa0227943e86469ad8368c5e2)
This commit is contained in:
+12
-6
@@ -19,21 +19,27 @@ using System.Collections.Generic;
|
||||
|
||||
namespace Framework.Database
|
||||
{
|
||||
public class QueryCallbackProcessor
|
||||
public interface ISqlCallback
|
||||
{
|
||||
public void AddQuery(QueryCallback query)
|
||||
bool InvokeIfReady();
|
||||
}
|
||||
|
||||
public class AsyncCallbackProcessor<T> where T : ISqlCallback
|
||||
{
|
||||
List<T> _callbacks = new List<T>();
|
||||
|
||||
public T AddCallback(T query)
|
||||
{
|
||||
_callbacks.Add(query);
|
||||
return query;
|
||||
}
|
||||
|
||||
public void ProcessReadyQueries()
|
||||
public void ProcessReadyCallbacks()
|
||||
{
|
||||
if (_callbacks.Empty())
|
||||
return;
|
||||
|
||||
_callbacks.RemoveAll(callback => callback.InvokeIfReady() == QueryCallbackStatus.Completed);
|
||||
_callbacks.RemoveAll(callback => callback.InvokeIfReady());
|
||||
}
|
||||
|
||||
List<QueryCallback> _callbacks = new List<QueryCallback>();
|
||||
}
|
||||
}
|
||||
@@ -253,7 +253,15 @@ namespace Framework.Database
|
||||
_queue.Push(new TransactionTask(transaction));
|
||||
}
|
||||
|
||||
public bool DirectCommitTransaction(SQLTransaction transaction)
|
||||
public TransactionCallback AsyncCommitTransaction(SQLTransaction transaction)
|
||||
{
|
||||
TransactionWithResultTask task = new TransactionWithResultTask(transaction);
|
||||
Task<bool> result = task.GetFuture();
|
||||
_queue.Push(task);
|
||||
return new TransactionCallback(result);
|
||||
}
|
||||
|
||||
public MySqlErrorCode DirectCommitTransaction(SQLTransaction transaction)
|
||||
{
|
||||
using (var Connection = _connectionInfo.GetConnection())
|
||||
{
|
||||
@@ -277,13 +285,12 @@ namespace Framework.Database
|
||||
trans.Commit();
|
||||
scope.Complete();
|
||||
}
|
||||
return true;
|
||||
return MySqlErrorCode.None;
|
||||
}
|
||||
catch (MySqlException ex) //error occurred
|
||||
{
|
||||
HandleMySQLException(ex, query);
|
||||
trans.Rollback();
|
||||
return false;
|
||||
return HandleMySQLException(ex, query);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Framework.Database
|
||||
{
|
||||
public class QueryCallback
|
||||
public class QueryCallback : ISqlCallback
|
||||
{
|
||||
public QueryCallback(Task<SQLResult> result)
|
||||
{
|
||||
@@ -49,7 +49,7 @@ namespace Framework.Database
|
||||
_result = next._result;
|
||||
}
|
||||
|
||||
public QueryCallbackStatus InvokeIfReady()
|
||||
public bool InvokeIfReady()
|
||||
{
|
||||
QueryCallbackData callback = _callbacks.Peek();
|
||||
|
||||
@@ -68,17 +68,17 @@ namespace Framework.Database
|
||||
if (_callbacks.Count == 0)
|
||||
{
|
||||
Cypher.Assert(!hasNext);
|
||||
return QueryCallbackStatus.Completed;
|
||||
return true;
|
||||
}
|
||||
|
||||
// abort chain
|
||||
if (!hasNext)
|
||||
return QueryCallbackStatus.Completed;
|
||||
return true;
|
||||
|
||||
callback = _callbacks.Peek();
|
||||
}
|
||||
else
|
||||
return QueryCallbackStatus.NotReady;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,11 +100,4 @@ namespace Framework.Database
|
||||
|
||||
public Action<QueryCallback, SQLResult> _result;
|
||||
}
|
||||
|
||||
public enum QueryCallbackStatus
|
||||
{
|
||||
NotReady,
|
||||
NextStep,
|
||||
Completed
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
|
||||
using MySql.Data.MySqlClient;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Framework.Database
|
||||
{
|
||||
@@ -52,10 +54,98 @@ namespace Framework.Database
|
||||
}
|
||||
|
||||
public bool Execute<T>(MySqlBase<T> mySqlBase)
|
||||
{
|
||||
MySqlErrorCode errorCode = TryExecute(mySqlBase);
|
||||
if (errorCode == MySqlErrorCode.None)
|
||||
return true;
|
||||
|
||||
if (errorCode == MySqlErrorCode.LockDeadlock)
|
||||
{
|
||||
// Make sure only 1 async thread retries a transaction so they don't keep dead-locking each other
|
||||
lock (_deadlockLock)
|
||||
{
|
||||
byte loopBreaker = 5; // Handle MySQL Errno 1213 without extending deadlock to the core itself
|
||||
for (byte i = 0; i < loopBreaker; ++i)
|
||||
if (TryExecute(mySqlBase) == MySqlErrorCode.None)
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public MySqlErrorCode TryExecute<T>(MySqlBase<T> mySqlBase)
|
||||
{
|
||||
return mySqlBase.DirectCommitTransaction(m_trans);
|
||||
}
|
||||
|
||||
SQLTransaction m_trans;
|
||||
public static object _deadlockLock = new object();
|
||||
}
|
||||
|
||||
class TransactionWithResultTask : TransactionTask
|
||||
{
|
||||
public TransactionWithResultTask(SQLTransaction trans) : base(trans) { }
|
||||
|
||||
public new bool Execute<T>(MySqlBase<T> mySqlBase)
|
||||
{
|
||||
MySqlErrorCode errorCode = TryExecute(mySqlBase);
|
||||
if (errorCode == MySqlErrorCode.None)
|
||||
{
|
||||
m_result.SetResult(true);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (errorCode == MySqlErrorCode.LockDeadlock)
|
||||
{
|
||||
// Make sure only 1 async thread retries a transaction so they don't keep dead-locking each other
|
||||
lock (_deadlockLock)
|
||||
{
|
||||
byte loopBreaker = 5; // Handle MySQL Errno 1213 without extending deadlock to the core itself
|
||||
for (byte i = 0; i < loopBreaker; ++i)
|
||||
{
|
||||
if (TryExecute(mySqlBase) == MySqlErrorCode.None)
|
||||
{
|
||||
m_result.SetResult(true);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m_result.SetResult(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
public Task<bool> GetFuture() { return m_result.Task; }
|
||||
|
||||
TaskCompletionSource<bool> m_result = new TaskCompletionSource<bool>();
|
||||
}
|
||||
|
||||
public class TransactionCallback : ISqlCallback
|
||||
{
|
||||
public TransactionCallback(Task<bool> future)
|
||||
{
|
||||
m_future = future;
|
||||
}
|
||||
|
||||
public void AfterComplete(Action<bool> callback)
|
||||
{
|
||||
m_callback = callback;
|
||||
}
|
||||
|
||||
public bool InvokeIfReady()
|
||||
{
|
||||
if (m_future != null && m_future.Wait(0))
|
||||
{
|
||||
m_callback(m_future.Result);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Task<bool> m_future;
|
||||
Action<bool> m_callback;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user