Exemplo n.º 1
0
        public async Task <ServicePartitionList> GetPartitionsAsync(CancellationToken ct)
        {
            ReleaseAssert.AssertIfNull(FabricClientRetryErrors.GetPartitionListFabricErrors.Value, "partition list error code");

            var retryableErrors = new FabricClientRetryErrors();

            retryableErrors.RetryableFabricErrorCodes.AddRangeNullSafe(FabricClientRetryErrors.GetPartitionListFabricErrors.Value.RetryableFabricErrorCodes);
            retryableErrors.RetryableExceptions.AddRangeNullSafe(FabricClientRetryErrors.GetPartitionListFabricErrors.Value.RetryableExceptions);

            retryableErrors.RetryableFabricErrorCodes.Add(FabricErrorCode.PartitionNotFound);

            ServicePartitionList servicePartitionList = new ServicePartitionList();
            string continuationToken = null;

            do
            {
                ServicePartitionList queryResult = await FabricClientRetryHelper.ExecuteFabricActionWithRetryAsync(
                    () =>
                    this.TestContext.FabricClient.QueryManager.GetPartitionListAsync(
                        this.serviceName,
                        null,
                        continuationToken,
                        this.requestTimeout,
                        ct),
                    retryableErrors,
                    this.operationTimeout,
                    ct).ConfigureAwait(false);

                servicePartitionList.AddRangeNullSafe(queryResult);
                continuationToken = queryResult.ContinuationToken;
            } while (!string.IsNullOrEmpty(continuationToken));

            return(servicePartitionList);
        }
        public static async Task ExecuteFabricActionWithRetryAsync(Func <Task> action, FabricClientRetryErrors errors, TimeSpan operationTimeout, CancellationToken cancellationToken)
        {
            TestabilityTrace.TraceSource.WriteInfo(TraceType, "Enter ExecuteFabricActionWithRetryAsync() with action");
            Stopwatch watch = new Stopwatch();

            watch.Start();
            while (true)
            {
                cancellationToken.ThrowIfCancellationRequested();

                try
                {
                    await action().ConfigureAwait(false);

                    return;
                }
                catch (Exception e)
                {
                    bool retryElseSuccess;
                    if (HandleException(e, errors, out retryElseSuccess))
                    {
                        if (retryElseSuccess)
                        {
                            if (watch.Elapsed > operationTimeout)
                            {
                                throw;
                            }
                        }
                        else
                        {
                            // Exception indicates success for API hence return
                            return;
                        }
                    }
                    else
                    {
                        // Could not handle exception hence throwing
                        throw;
                    }
                }

                TestabilityTrace.TraceSource.WriteInfo(TraceType, "ExecuteFabricActionWithRetryAsync() with action: Going to wait for 5 seconds before retry");

                // If we get here it means that we need to retry. Sleep for 5 seconds
                // before next iteration
                await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken).ConfigureAwait(false);
            }
        }
 public static async Task ExecuteFabricActionWithRetryAsync(Func <Task> action, FabricClientRetryErrors errors, CancellationToken cancellationToken)
 {
     await ExecuteFabricActionWithRetryAsync(action, errors, DefaultOperationTimeout, cancellationToken).ConfigureAwait(false);
 }
        private static bool HandleException(Exception e, FabricClientRetryErrors errors, out bool retryElseSuccess)
        {
            TestabilityTrace.TraceSource.WriteInfo(TraceType, "HandleException(): e='{0}', HResult='{0}'", e.ToString(), e.HResult);

            FabricException fabricException = e as FabricException;

            if (fabricException != null)
            {
                TestabilityTrace.TraceSource.WriteInfo(TraceType, "HandleException(): errorCode='{0}'", fabricException.ErrorCode);
            }

            Exception currentEx = e;

            for (int i = 0; i < 5 && currentEx != null; i++)
            {
                if (errors.RetryableExceptions.Contains(currentEx.GetType()))
                {
                    retryElseSuccess = true /*retry*/;
                    return(true);
                }

                currentEx = currentEx.InnerException;
            }

            if (fabricException != null && errors.RetryableFabricErrorCodes.Contains(fabricException.ErrorCode))
            {
                retryElseSuccess = true /*retry*/;
                return(true);
            }

            if (errors.RetrySuccessExceptions.Contains(e.GetType()))
            {
                retryElseSuccess = false /*success*/;
                return(true);
            }

            if (fabricException != null && errors.RetrySuccessFabricErrorCodes.Contains(fabricException.ErrorCode))
            {
                retryElseSuccess = false /*success*/;
                return(true);
            }

            if (e.GetType() == typeof(FabricTransientException))
            {
                retryElseSuccess = true /*retry*/;
                return(true);
            }

            if (fabricException != null && fabricException.InnerException != null)
            {
                var ex = fabricException.InnerException;

                if (errors.InternalRetrySuccessFabricErrorCodes.Contains((uint)ex.HResult))
                {
                    retryElseSuccess = false /*success*/;
                    return(true);
                }
            }

            retryElseSuccess = false;
            return(false);
        }
 public static async Task <T> ExecuteFabricActionWithRetryAsync <T>(Func <Task <T> > function, FabricClientRetryErrors errors, CancellationToken cancellationToken)
 {
     return(await ExecuteFabricActionWithRetryAsync <T>(function, errors, DefaultOperationTimeout).ConfigureAwait(false));
 }
 public static async Task <T> ExecuteFabricActionWithRetryAsync <T>(Func <Task <T> > function, FabricClientRetryErrors errors, TimeSpan operationTimeout)
 {
     return(await ExecuteFabricActionWithRetryAsync <T>(function, errors, operationTimeout, CancellationToken.None).ConfigureAwait(false));
 }