Retry policy within ITargetBlock

前端 未结 3 1796
梦如初夏
梦如初夏 2020-12-05 15:58

I need to introduce a retry policy to the workflow. Let\'s say there are 3 blocks that are connected in such a way:

var executionOptions = new ExecutionDataf         


        
3条回答
  •  一向
    一向 (楼主)
    2020-12-05 16:27

    Here are two methods CreateRetryTransformBlock and CreateRetryActionBlock that operate under these assumptions:

    1. The caller wants all items to be processed, even if some of them have repeatedly failed.
    2. The caller is interested to know about all occured exceptions, even for items that finally succeeded (not applicable for the CreateRetryActionBlock).
    3. The caller may want to set an upper limit to the number of total retries, after which the block should transition to a faulted state.
    4. The caller wants to be able to set all available options of a normal block, including the MaxDegreeOfParallelism, BoundedCapacity, CancellationToken and EnsureOrdered, on top of the options related to the retry functionality.

    The implementation below uses a SemaphoreSlim to control the level of concurrency between operations that are attempted for the first time, and previously faulted operations that are retried after their delay duration has elapsed.

    public class RetryExecutionDataflowBlockOptions : ExecutionDataflowBlockOptions
    {
        /// The limit after which an item is returned as failed.
        public int MaxAttemptsPerItem { get; set; } = 1;
        /// The delay duration before retrying an item.
        public TimeSpan RetryDelay { get; set; } = TimeSpan.Zero;
        /// The limit after which the block transitions to a faulted
        /// state (unlimited is the default).
        public int MaxRetriesTotal { get; set; } = -1;
    }
    
    public readonly struct RetryResult
    {
        public readonly TInput Input { get; }
        public readonly TOutput Output { get; }
        public readonly bool Success { get; }
        public readonly Exception[] Exceptions { get; }
    
        public bool Failed => !Success;
        public Exception FirstException => Exceptions != null ? Exceptions[0] : null;
        public int Attempts =>
            Exceptions != null ? Exceptions.Length + (Success ? 1 : 0) : 1;
    
        public RetryResult(TInput input, TOutput output, bool success,
            Exception[] exceptions)
        {
            Input = input;
            Output = output;
            Success = success;
            Exceptions = exceptions;
        }
    }
    
    public class RetryLimitException : Exception
    {
        public RetryLimitException(string message, Exception innerException)
            : base(message, innerException) { }
    }
    
    public static IPropagatorBlock>
        CreateRetryTransformBlock(
        Func> transform,
        RetryExecutionDataflowBlockOptions dataflowBlockOptions)
    {
        if (transform == null) throw new ArgumentNullException(nameof(transform));
        if (dataflowBlockOptions == null)
            throw new ArgumentNullException(nameof(dataflowBlockOptions));
        int maxAttemptsPerItem = dataflowBlockOptions.MaxAttemptsPerItem;
        int maxRetriesTotal = dataflowBlockOptions.MaxRetriesTotal;
        TimeSpan retryDelay = dataflowBlockOptions.RetryDelay;
        if (maxAttemptsPerItem < 1) throw new ArgumentOutOfRangeException(
            nameof(dataflowBlockOptions.MaxAttemptsPerItem));
        if (maxRetriesTotal < -1) throw new ArgumentOutOfRangeException(
            nameof(dataflowBlockOptions.MaxRetriesTotal));
        if (retryDelay < TimeSpan.Zero) throw new ArgumentOutOfRangeException(
            nameof(dataflowBlockOptions.RetryDelay));
        var cancellationToken = dataflowBlockOptions.CancellationToken;
    
        var exceptionsCount = 0;
        var semaphore = new SemaphoreSlim(
            dataflowBlockOptions.MaxDegreeOfParallelism);
    
        async Task<(TOutput, Exception)> ProcessOnceAsync(TInput item)
        {
            await semaphore.WaitAsync(); // Preserve the SynchronizationContext
            try
            {
                var result = await transform(item).ConfigureAwait(false);
                return (result, null);
            }
            catch (Exception ex)
            {
                if (maxRetriesTotal != -1)
                {
                    if (Interlocked.Increment(ref exceptionsCount) > maxRetriesTotal)
                    {
                        throw new RetryLimitException($"The max retry limit " +
                            $"({maxRetriesTotal}) has been reached.", ex);
                    }
                }
                return (default, ex);
            }
            finally
            {
                semaphore.Release();
            }
        }
    
        async Task>> ProcessWithRetryAsync(
            TInput item)
        {
            // Creates a two-stages operation. Preserves the context on every await.
            var (result, firstException) = await ProcessOnceAsync(item);
            if (firstException == null) return Task.FromResult(
                new RetryResult(item, result, true, null));
            return RetryStageAsync();
    
            async Task> RetryStageAsync()
            {
                var exceptions = new List();
                exceptions.Add(firstException);
                for (int i = 2; i <= maxAttemptsPerItem; i++)
                {
                    await Task.Delay(retryDelay, cancellationToken);
                    var (result, exception) = await ProcessOnceAsync(item);
                    if (exception != null)
                        exceptions.Add(exception);
                    else
                        return new RetryResult(item, result,
                            true, exceptions.ToArray());
                }
                return new RetryResult(item, default, false,
                    exceptions.ToArray());
            };
        }
    
        // The input block awaits the first stage of each operation
        var input = new TransformBlock>>(
            item => ProcessWithRetryAsync(item), dataflowBlockOptions);
    
        // The output block awaits the second (and final) stage of each operation
        var output = new TransformBlock>,
            RetryResult>(t => t, dataflowBlockOptions);
    
        input.LinkTo(output, new DataflowLinkOptions { PropagateCompletion = true });
    
        // In case of failure ensure that the input block is faulted too,
        // so that its input/output queues are emptied, and any pending
        // SendAsync operations are aborted
        PropagateFailure(output, input);
    
        return DataflowBlock.Encapsulate(input, output);
    
        async void PropagateFailure(IDataflowBlock block1, IDataflowBlock block2)
        {
            try { await block1.Completion.ConfigureAwait(false); }
            catch (Exception ex) { block2.Fault(ex); }
        }
    }
    
    public static ITargetBlock CreateRetryActionBlock(
        Func action,
        RetryExecutionDataflowBlockOptions dataflowBlockOptions)
    {
        if (action == null) throw new ArgumentNullException(nameof(action));
        var block = CreateRetryTransformBlock(async input =>
        {
            await action(input).ConfigureAwait(false); return null;
        }, dataflowBlockOptions);
        var nullTarget = DataflowBlock.NullTarget>();
        block.LinkTo(nullTarget);
        return block;
    }
    

提交回复
热议问题