public async Task CommitTransaction(long transactionId) { if (WaitingForTransactionTransports.Count > 0) { if (CurrentTransactionId != transactionId) { throw new TxCommitException(); } try { var onCommitTask = OnCommitTransaction(transactionId); if (!onCommitTask.IsCompletedSuccessfully) { await onCommitTask; } foreach (var transport in WaitingForTransactionTransports) { var startTask = OnRaiseStart(transport.FullyEvent); if (!startTask.IsCompletedSuccessfully) { await startTask; } var evtType = transport.FullyEvent.Event.GetType(); transport.BytesTransport = new EventBytesTransport( TypeContainer.GetTypeCode(evtType), GrainId, transport.FullyEvent.Base.GetBytes(), Serializer.SerializeToUtf8Bytes(transport.FullyEvent.Event, evtType) ); } await EventStorage.TransactionBatchAppend(WaitingForTransactionTransports); if (Logger.IsEnabled(LogLevel.Trace)) { Logger.LogTrace("Transaction Commited: {0}->{1}->{2}", GrainType.FullName, GrainId.ToString(), transactionId); } } catch (Exception ex) { Logger.LogCritical(ex, "Transaction failed: {0}->{1}->{2}", GrainType.FullName, GrainId.ToString(), transactionId); throw; } } }
public async Task CommitTransaction(long transactionId) { if (Logger.IsEnabled(LogLevel.Trace)) { Logger.LogTrace("Commit transaction with id = {0},event counts = {1}, from version {2} to version {3}", GrainId.ToString(), WaitingForTransactionTransports.Count.ToString(), CurrentTransactionStartVersion.ToString(), Snapshot.Base.Version.ToString()); } if (WaitingForTransactionTransports.Count > 0) { if (CurrentTransactionId != transactionId) { throw new TxCommitException(); } try { var onCommitTask = OnCommitTransaction(transactionId); if (!onCommitTask.IsCompletedSuccessfully) { await onCommitTask; } foreach (var transport in WaitingForTransactionTransports) { var startTask = OnRaiseStart(transport.FullyEvent); if (!startTask.IsCompletedSuccessfully) { await startTask; } var evtType = transport.FullyEvent.Event.GetType(); transport.BytesTransport = new EventBytesTransport( TypeContainer.GetTypeCode(evtType), GrainId, transport.FullyEvent.Base.GetBytes(), Serializer.SerializeToUtf8Bytes(transport.FullyEvent.Event, evtType) ); } await EventStorage.TransactionBatchAppend(WaitingForTransactionTransports); } catch (Exception ex) { Logger.LogError(ex, "Commit transaction failed, grain Id = {1}", GrainId.ToString()); throw; } } }
public async Task CommitTransaction(long transactionId) { if (Logger.IsEnabled(LogLevel.Trace)) { Logger.LogTrace("Commit transaction with id = {0},event counts = {1}, from version {2} to version {3}", GrainId.ToString(), WaitingForTransactionTransports.Count.ToString(), CurrentTransactionStartVersion.ToString(), Snapshot.Base.Version.ToString()); } if (WaitingForTransactionTransports.Count > 0) { if (CurrentTransactionId != transactionId) { throw new TransactionCommitException(); } try { //如果是带Id的事务,则加入事务事件,等待Complete if (transactionId > 0) { TransactionRaiseEvent(new TransactionCommitEvent(CurrentTransactionId, CurrentTransactionStartVersion + 1, WaitingForTransactionTransports.Min(t => t.FullyEvent.Base.Timestamp))); } foreach (var transport in WaitingForTransactionTransports) { var startTask = OnRaiseStart(transport.FullyEvent); if (!startTask.IsCompletedSuccessfully) { await startTask; } transport.BytesTransport = new EventBytesTransport { EventType = transport.FullyEvent.Event.GetType().FullName, GrainId = GrainId, EventBytes = Serializer.SerializeToBytes(transport.FullyEvent.Event), BaseBytes = transport.FullyEvent.Base.GetBytes() }; } await EventStorage.TransactionBatchAppend(WaitingForTransactionTransports); } catch (Exception ex) { Logger.LogError(ex, "Commit transaction failed, grain Id = {1}", GrainId.ToString()); throw; } } }
protected async Task CommitTransaction() { if (Logger.IsEnabled(LogLevel.Trace)) { Logger.LogTrace("Commit transaction with id = {0},event counts = {1}, from version {2} to version {3}", GrainId.ToString(), WaitingForTransactionEvents.Count.ToString(), TransactionStartVersion.ToString(), Snapshot.Base.Version.ToString()); } if (WaitingForTransactionEvents.Count > 0) { try { foreach (var transport in WaitingForTransactionEvents) { var startTask = OnRaiseStart(transport.FullyEvent); if (!startTask.IsCompletedSuccessfully) { await startTask; } transport.BytesTransport = new EventBytesTransport { EventType = transport.FullyEvent.Event.GetType().FullName, ActorId = GrainId, EventBytes = Serializer.SerializeToBytes(transport.FullyEvent.Event), BaseBytes = transport.FullyEvent.Base.GetBytes() }; } await EventStorage.TransactionBatchAppend(WaitingForTransactionEvents); foreach (var transport in WaitingForTransactionEvents) { var task = OnRaiseSuccessed(transport.FullyEvent, transport.BytesTransport); if (!task.IsCompletedSuccessfully) { await task; } } var saveSnapshotTask = SaveSnapshotAsync(); if (!saveSnapshotTask.IsCompletedSuccessfully) { await saveSnapshotTask; } var handlers = FollowUnit.GetAllEventHandlers(); if (handlers.Count > 0) { try { foreach (var transport in WaitingForTransactionEvents) { if (CoreOptions.PriorityAsyncEventBus) { try { var publishTask = EventBusProducer.Publish(transport.BytesTransport.GetBytes(), transport.HashKey); if (!publishTask.IsCompletedSuccessfully) { await publishTask; } } catch (Exception ex) { Logger.LogError(ex, "EventBus error,state Id ={0}, version ={1}", GrainId.ToString(), Snapshot.Base.Version); //当消息队列出现问题的时候同步推送 await Task.WhenAll(handlers.Select(func => func(transport.BytesTransport.GetBytes()))); } } else { try { await Task.WhenAll(handlers.Select(func => func(transport.BytesTransport.GetBytes()))); } catch (Exception ex) { Logger.LogError(ex, "EventBus error,state Id ={0}, version ={1}", GrainId.ToString(), Snapshot.Base.Version); //当消息队列出现问题的时候异步推送 var publishTask = EventBusProducer.Publish(transport.BytesTransport.GetBytes(), transport.HashKey); if (!publishTask.IsCompletedSuccessfully) { await publishTask; } } } } } catch (Exception ex) { Logger.LogError(ex, "EventBus error,state Id ={0}, version ={1}", GrainId.ToString(), Snapshot.Base.Version); } } if (Logger.IsEnabled(LogLevel.Trace)) { Logger.LogTrace("Commit transaction with id {0},event counts = {1}, from version {2} to version {3}", GrainId.ToString(), WaitingForTransactionEvents.Count.ToString(), TransactionStartVersion.ToString(), Snapshot.Base.Version.ToString()); } } catch (Exception ex) { Logger.LogError(ex, "Commit transaction failed, grain Id = {1}", GrainId.ToString()); throw; } finally { WaitingForTransactionEvents.Clear(); TransactionPending = false; } } }