コード例 #1
0
        private async Task InvokeRollbackOnActors(string transactionName)
        {
            var enumerableActiveTransactions = await ActiveTransactions.GetEnumerableDictionaryAsync();

            if (!enumerableActiveTransactions.ContainsKey(transactionName))
            {
                string errorMessage = $"{baseLogString} InvokeRollbackOnActors => transaction '{transactionName}' not found in '{ReliableDictionaryNames.ActiveTransactions}'.";
                Logger.LogError(errorMessage);
                throw new Exception(errorMessage);
            }

            var result = await TransactionEnlistmentLedger.TryGetValueAsync(transactionName);

            if (!result.HasValue)
            {
                string errorMessage = $"{baseLogString} InvokeRollbackOnActors => Transaction '{transactionName}' not found in '{ReliableDictionaryNames.TransactionEnlistmentLedger}'.";
                Logger.LogError(errorMessage);
                throw new Exception(errorMessage);
            }

            var transactionLedger = result.Value;

            List <Task> tasks = new List <Task>();

            foreach (var transactionActorName in transactionLedger)
            {
                if (enumerableActiveTransactions[transactionName].Contains(transactionActorName))
                {
                    var task = Task.Run(async() =>
                    {
                        ITransactionActorContract transactionActorClient = TransactionActorClient.CreateClient(transactionActorName);
                        await transactionActorClient.Rollback();
                        Logger.LogInformation($"{baseLogString} InvokeRollbackOnActors => Rollback invoked on Transaction actor: {transactionActorName}.");
                    });

                    tasks.Add(task);
                }
            }

            Task.WaitAll(tasks.ToArray());
            Logger.LogInformation($"{baseLogString} InvokeRollbackOnActors => Rollback SUCCESSFULLY invoked on all transaction actors.");
        }
コード例 #2
0
        public async Task <bool> Enlist(string transactionName, string transactionActorName)
        {
            while (!ReliableDictionariesInitialized)
            {
                await Task.Delay(1000);
            }

            var enumerableActiveTransactions = await ActiveTransactions.GetEnumerableDictionaryAsync();

            if (!enumerableActiveTransactions.ContainsKey(transactionName))
            {
                string errorMessage = $"{baseLogString} Enlist => transaction '{transactionName}' not found in '{ReliableDictionaryNames.ActiveTransactions}'.";
                Logger.LogError(errorMessage);
                throw new Exception(errorMessage);
            }

            var result = await TransactionEnlistmentLedger.TryGetValueAsync(transactionName);

            if (!result.HasValue)
            {
                string errorMessage = $"{baseLogString} Enlist => Transaction '{transactionName}' not found in '{ReliableDictionaryNames.TransactionEnlistmentLedger}'.";
                Logger.LogError(errorMessage);
                throw new Exception(errorMessage);
            }

            var transactionLedger = result.Value;

            if (!enumerableActiveTransactions[transactionName].Contains(transactionActorName))
            {
                string errorMessage = $"{baseLogString} InvokePreparationOnActors => Transaction '{transactionName}' not found in '{ReliableDictionaryNames.TransactionEnlistmentLedger}'.";
                Logger.LogError(errorMessage);
                return(false);
            }

            transactionLedger.Add(transactionActorName);
            await TransactionEnlistmentLedger.SetAsync(transactionName, transactionLedger);

            return(true);
        }
コード例 #3
0
        private async Task <bool> InvokePreparationOnActors(string transactionName)
        {
            bool success;

            var enumerableActiveTransactions = await ActiveTransactions.GetEnumerableDictionaryAsync();

            if (!enumerableActiveTransactions.ContainsKey(transactionName))
            {
                string errorMessage = $"{baseLogString} InvokePreparationOnActors => transaction '{transactionName}' not found in '{ReliableDictionaryNames.ActiveTransactions}'.";
                Logger.LogError(errorMessage);
                throw new Exception(errorMessage);
            }

            var result = await TransactionEnlistmentLedger.TryGetValueAsync(transactionName);

            if (!result.HasValue)
            {
                string errorMessage = $"{baseLogString} InvokePreparationOnActors => Transaction '{transactionName}' not found in '{ReliableDictionaryNames.TransactionEnlistmentLedger}'.";
                Logger.LogError(errorMessage);
                throw new Exception(errorMessage);
            }

            var transactionLedger = result.Value;

            if (!enumerableActiveTransactions[transactionName].SetEquals(transactionLedger))
            {
                string errorMessage = $"{baseLogString} InvokePreparationOnActors => not all actors have enlisted for the transaction '{transactionName}'.";
                Logger.LogError(errorMessage);
                throw new NotAllActorsEnlistedException(errorMessage);
            }

            List <Task <Tuple <string, bool> > > tasks = new List <Task <Tuple <string, bool> > >();

            foreach (var transactionActorName in transactionLedger)
            {
                var task = Task.Run(async() =>
                {
                    ITransactionActorContract transactionActorClient = TransactionActorClient.CreateClient(transactionActorName);
                    var prepareSuccess = await transactionActorClient.Prepare();
                    Logger.LogInformation($"{baseLogString} InvokePreparationOnActors => Prepare invoked on Transaction actor: {transactionActorName}, Success: {prepareSuccess}.");

                    return(new Tuple <string, bool>(transactionActorName, prepareSuccess));
                });

                tasks.Add(task);
            }

            var taskResults = await Task.WhenAll(tasks);

            success = true;

            foreach (var taskResult in taskResults)
            {
                var actorUri       = taskResult.Item1;
                var prepareSuccess = taskResult.Item2;

                success = success && prepareSuccess;

                if (success)
                {
                    Logger.LogInformation($"{baseLogString} InvokePreparationOnActors => Preparation on Transaction actor: {actorUri} finsihed SUCCESSFULLY.");
                }
                else
                {
                    Logger.LogInformation($"{baseLogString} InvokePreparationOnActors => Preparation on Transaction actor: {actorUri} finsihed UNSUCCESSFULLY.");
                    break;
                }
            }

            return(success);
        }