Ejemplo n.º 1
0
        protected override async ValueTask OnFinshTransaction(long transactionId)
        {
            if (transactionId > 0)
            {
                if (!TransactionOptions.RetainTxEvents)
                {
                    //删除最后一个TransactionCommitEvent
                    await EventStorage.DeleteEnd(Snapshot.Base.StateId, Snapshot.Base.Version, Snapshot.Base.LatestMinEventTimestamp);

                    if (Snapshot.Base is TxSnapshotBase <PrimaryKey> snapshotBase &&
                        BackupSnapshot.Base is TxSnapshotBase <PrimaryKey> backupSnapshotBase)
                    {
                        snapshotBase.ClearTransactionInfo(true);
                        backupSnapshotBase.ClearTransactionInfo(true);
                    }
                    else
                    {
                        throw new SnapshotNotSupportTxException(Snapshot.GetType());
                    }
                }
                else
                {
                    await base.RaiseEvent(new TxFinishedEvent(transactionId));
                }
            }
        }
Ejemplo n.º 2
0
        protected override async ValueTask OnFinshTransaction(long transactionId)
        {
            if (transactionId > 0)
            {
                if (!TransactionOptions.RetainTransactionEvents)
                {
                    //删除最后一个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));
                }
            }
        }
Ejemplo n.º 3
0
        public async Task RollbackTransaction(long transactionId)
        {
            if (Logger.IsEnabled(LogLevel.Trace))
            {
                Logger.LogTrace("Start rollback 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 (CurrentTransactionId == transactionId && CurrentTransactionStartVersion != -1 && Snapshot.Base.Version >= CurrentTransactionStartVersion)
            {
                try
                {
                    if (BackupSnapshot.Base.Version == CurrentTransactionStartVersion - 1)
                    {
                        Snapshot = new Snapshot <PrimaryKey, StateType>(GrainId)
                        {
                            Base  = BackupSnapshot.Base.Clone(),
                            State = BackupSnapshot.State.Clone()
                        };
                    }
                    else
                    {
                        if (BackupSnapshot.Base.Version >= CurrentTransactionStartVersion)
                        {
                            await EventStorage.DeleteEnd(Snapshot.Base.StateId, CurrentTransactionStartVersion, Snapshot.Base.LatestMinEventTimestamp);
                        }
                        await RecoverySnapshot();
                    }

                    WaitingForTransactionTransports.Clear();
                    RestoreTransactionTemporaryState();
                    TransactionSemaphore.Release();
                    if (Logger.IsEnabled(LogLevel.Trace))
                    {
                        Logger.LogTrace("Rollback transaction successfully with id = {0},state version = {1}", GrainId.ToString(), Snapshot.Base.Version.ToString());
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogCritical(ex, "Rollback transaction failed with Id = {1}", GrainId.ToString());
                    throw;
                }
            }
        }
Ejemplo n.º 4
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();
            }
        }