コード例 #1
0
ファイル: TxGrain.cs プロジェクト: microservicekit/Ray
        public async Task FinishTransaction(long transactionId)
        {
            if (CurrentTransactionId == transactionId)
            {
                //如果副本快照没有更新,则更新副本集
                foreach (var transport in WaitingForTransactionTransports)
                {
                    var task = OnRaiseSuccessed(transport.FullyEvent, transport.BytesTransport);
                    if (!task.IsCompletedSuccessfully)
                    {
                        await task;
                    }
                }
                var onFinishTask = OnFinshTransaction(transactionId);
                if (!onFinishTask.IsCompletedSuccessfully)
                {
                    await onFinishTask;
                }
                var saveSnapshotTask = SaveSnapshotAsync();
                if (!saveSnapshotTask.IsCompletedSuccessfully)
                {
                    await saveSnapshotTask;
                }
                var handlers = ObserverUnit.GetAllEventHandlers();
                if (handlers.Count > 0)
                {
                    try
                    {
                        foreach (var transport in WaitingForTransactionTransports)
                        {
                            if (CoreOptions.PriorityAsyncEventBus)
                            {
                                try
                                {
                                    var publishTask = EventBusProducer.Publish(transport.BytesTransport.GetBytes(), transport.HashKey);
                                    if (!publishTask.IsCompletedSuccessfully)
                                    {
                                        await publishTask;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Logger.LogError(ex, ex.Message);

                                    //当消息队列出现问题的时候同步推送
                                    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, ex.Message);
                                    //当消息队列出现问题的时候异步推送
                                    var publishTask = EventBusProducer.Publish(transport.BytesTransport.GetBytes(), transport.HashKey);
                                    if (!publishTask.IsCompletedSuccessfully)
                                    {
                                        await publishTask;
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.LogError(ex, ex.Message);
                    }
                }
                WaitingForTransactionTransports.Clear();
                RestoreTransactionTemporaryState();
                TransactionSemaphore.Release();
            }
        }
コード例 #2
0
        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;
                }
            }
        }
コード例 #3
0
        public async Task FinishTransaction(long transactionId)
        {
            if (CurrentTransactionId == transactionId)
            {
                //如果副本快照没有更新,则更新副本集
                foreach (var transport in WaitingForTransactionTransports)
                {
                    var task = OnRaiseSuccessed(transport.FullyEvent, transport.BytesTransport);
                    if (!task.IsCompletedSuccessfully)
                    {
                        await task;
                    }
                }
                if (transactionId > 0)
                {
                    if (CoreOptions.ClearTransactionEvents)
                    {
                        //删除最后一个TransactionCommitEvent
                        await EventStorage.DeleteEnd(Snapshot.Base.StateId, Snapshot.Base.Version, Snapshot.Base.LatestMinEventTimestamp);

                        Snapshot.Base.ClearTransactionInfo(true);
                        BackupSnapshot.Base.ClearTransactionInfo(true);
                    }
                    else
                    {
                        await base.RaiseEvent(new TransactionFinishEvent(transactionId));
                    }
                }
                var saveSnapshotTask = SaveSnapshotAsync();
                if (!saveSnapshotTask.IsCompletedSuccessfully)
                {
                    await saveSnapshotTask;
                }
                var handlers = FollowUnit.GetAllEventHandlers();
                if (handlers.Count > 0)
                {
                    try
                    {
                        foreach (var transport in WaitingForTransactionTransports)
                        {
                            if (CoreOptions.PriorityAsyncEventBus)
                            {
                                try
                                {
                                    var publishTask = EventBusProducer.Publish(transport.BytesTransport.GetBytes(), transport.HashKey);
                                    if (!publishTask.IsCompletedSuccessfully)
                                    {
                                        await publishTask;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Logger.LogError(ex, ex.Message);

                                    //当消息队列出现问题的时候同步推送
                                    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, ex.Message);
                                    //当消息队列出现问题的时候异步推送
                                    var publishTask = EventBusProducer.Publish(transport.BytesTransport.GetBytes(), transport.HashKey);
                                    if (!publishTask.IsCompletedSuccessfully)
                                    {
                                        await publishTask;
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.LogError(ex, ex.Message);
                    }
                }
                WaitingForTransactionTransports.Clear();
                RestoreTransactionTemporaryState();
                TransactionSemaphore.Release();
            }
        }