[Ignore]    // REVIEW
        public void TestSqlAzureTransientErrorDetectionStrategyWithRetryableError()
        {
            int[] errors = new int[] { 40197, 40501, 40540, 10053, 10054, 10060, 40549, 40550, 40551, 40552, 40553, 40613, 40143, 233, 64 };
            Type type = typeof(SqlDatabaseTransientErrorDetectionStrategy).GetNestedType("ProcessNetLibErrorCode", BindingFlags.NonPublic);
            errors = errors.AddRange((int[])Enum.GetValues(type));

            Exception[] exceptions = FakeSqlExceptionGenerator.GenerateFakeSqlExceptions(errors);
            exceptions = exceptions.AddRange(new TimeoutException(), new EntityException("Forced Exception"));

            RetryPolicy defaultPolicy = new RetryPolicy<SqlDatabaseTransientErrorDetectionStrategy>(exceptions.Length - 1, RetryStrategy.DefaultRetryInterval);
            int execCount = 0;

            try
            {
                defaultPolicy.ExecuteAction(() =>
                {
                    Exception ex = exceptions[execCount];
                    execCount++;
                    throw ex;
                });
            }
            catch (EntityException ex)
            {
                Assert.AreEqual("Forced Exception", ex.Message);
            }

            Assert.AreEqual<int>(exceptions.Length, execCount, "The action was not executed the expected amount of times");
        }
示例#2
0
        /// <summary>
        /// Executes a query against the database and returns a DataTable
        /// </summary>
        /// <param name="commandText"></param>
        /// <param name="cmdType"></param>
        /// <param name="parms"></param>
        public DataTable ExcecuteDataTable(string commandText, CommandType cmdType, List <SqlParameter> parms)
        {
            DataTable returnValue = new DataTable();

            if (retryPolicy == null)
            {
                SetupRetryManager();
            }

            retryPolicy.ExecuteAction(() =>
            {
                OpenConnectionIfClosedOrNonExistent();

                using (var cmd = new SqlCommand(commandText, TransactionScope.Connection.Current, TransactionScope.Transaction))
                {
                    cmd.CommandTimeout = _commandTimeout;
                    cmd.CommandType    = cmdType;
                    SetupParameters(cmd, ref parms);

                    using (var adapter = new SqlDataAdapter(cmd))
                    {
                        adapter.Fill(returnValue);
                    }
                    CurrentCommand = cmd;
                }

                //Close connection if not in transaction scope
                CloseConnectionNoTransaction();
            });


            return(returnValue);
        }
        // GET api/nextsession
        public Session Get()
        {
            var retryStrategy = new Incremental(5, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2));
            var retryPolicy   = new RetryPolicy <SqlAzureTransientErrorDetectionStrategy>(retryStrategy);

            Session nextSession = new Session();

            using (VidPackEntities db = new VidPackEntities())
            {
                retryPolicy.ExecuteAction(() =>
                {
                    db.Database.Connection.Open();
                });

                ExistingSession dbSession = retryPolicy.ExecuteAction <ExistingSession>(() =>
                                                                                        db.ExistingSession.Where(item => item.IsNextSession == 1).FirstOrDefault()
                                                                                        );

                if (dbSession != null)
                {
                    nextSession = new VidPackModel.Session()
                    {
                        SessionDate         = dbSession.SessionDate.ToString(),
                        SessionDescription  = dbSession.SessionDescription,
                        SessionSubTitle     = dbSession.SessionSubTitle,
                        SessionThumbnailUrl = String.Format("{0}{1}", ThumbnailStorageUrl, dbSession.SessionThumbnailUri),
                        SessionTitle        = dbSession.SessionSubTitle,
                        SessionVideoUrl     = dbSession.SessionVideoUri == null ? "" : dbSession.SessionVideoUri,
                        Speaker             = dbSession.Speaker
                    }
                }
                ;
            }
            return(nextSession);
        }
示例#4
0
        private void HandleBadDataCacheBug(Action action)
        {
            var retryStrategy = new Incremental(5, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2));

            var retryPolicy =
                new RetryPolicy <CacheRetryPolicy>(retryStrategy);

            try
            {
                retryPolicy.ExecuteAction(action);
            }
            catch (DataCacheException ex)
            {
                if (ex.ErrorCode == DataCacheErrorCode.ConnectionTerminated || ex.ErrorCode == DataCacheErrorCode.Timeout ||
                    ex.ErrorCode == DataCacheErrorCode.RetryLater)
                {
                    ErrorLogUtils.AddError(
                        new Exception(
                            "DataCache connection was lost. Trying to reinitialize DataCache through DataCacheFactory and retrying last operation.",
                            ex));
                    InitDataCache();
                    retryPolicy.ExecuteAction(action);
                }
                else
                {
                    throw;
                }
            }
        }
示例#5
0
        public void Initialize()
        {
            var retryStrategy    = new Incremental(3, TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1));
            var retryPolicy      = new RetryPolicy <ServiceBusTransientErrorDetectionStrategy>(retryStrategy);
            var tokenProvider    = TokenProvider.CreateSharedSecretTokenProvider(settings.TokenIssuer, settings.TokenAccessKey);
            var serviceUri       = ServiceBusEnvironment.CreateServiceUri(settings.ServiceUriScheme, settings.ServiceNamespace, settings.ServicePath);
            var namespaceManager = new NamespaceManager(serviceUri, tokenProvider);

            this.settings.Topics.AsParallel().ForAll(topic =>
            {
                retryPolicy.ExecuteAction(() => CreateTopicIfNotExists(namespaceManager, topic));
                topic.Subscriptions.AsParallel().ForAll(subscription =>
                {
                    retryPolicy.ExecuteAction(() => CreateSubscriptionIfNotExists(namespaceManager, topic, subscription));
                    retryPolicy.ExecuteAction(() => UpdateRules(namespaceManager, topic, subscription));
                });
            });

            // Execute migration support actions only after all the previous ones have been completed.
            foreach (var topic in this.settings.Topics)
            {
                foreach (var action in topic.MigrationSupport)
                {
                    retryPolicy.ExecuteAction(() => UpdateSubscriptionIfExists(namespaceManager, topic, action));
                }
            }

            this.initialized = true;
        }
        public void Initialize()
        {
            var retryStrategy = new Incremental(3, TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1));
            var retryPolicy = new RetryPolicy<ServiceBusTransientErrorDetectionStrategy>(retryStrategy);
            var tokenProvider = TokenProvider.CreateSharedSecretTokenProvider(settings.TokenIssuer, settings.TokenAccessKey);
            var serviceUri = ServiceBusEnvironment.CreateServiceUri(settings.ServiceUriScheme, settings.ServiceNamespace, settings.ServicePath);
            var namespaceManager = new NamespaceManager(serviceUri, tokenProvider);

            this.settings.Topics.AsParallel().ForAll(topic =>
            {
                retryPolicy.ExecuteAction(() => CreateTopicIfNotExists(namespaceManager, topic));
                topic.Subscriptions.AsParallel().ForAll(subscription =>
                {
                    retryPolicy.ExecuteAction(() => CreateSubscriptionIfNotExists(namespaceManager, topic, subscription));
                    retryPolicy.ExecuteAction(() => UpdateRules(namespaceManager, topic, subscription));
                });
            });

            // Execute migration support actions only after all the previous ones have been completed.
            foreach (var topic in this.settings.Topics)
            {
                foreach (var action in topic.MigrationSupport)
                {
                    retryPolicy.ExecuteAction(() => UpdateSubscriptionIfExists(namespaceManager, topic, action));
                }
            }

            this.initialized = true;
        }
示例#7
0
        public void Initialize()
        {
            var retryStrategy    = new Incremental(3, TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1));
            var retryPolicy      = new RetryPolicy <ServiceBusTransientErrorDetectionStrategy>(retryStrategy);
            var namespaceManager = NamespaceManager.CreateFromConnectionString(settings.ConnectionString);

            this.settings.Topics.AsParallel().ForAll(topic =>
            {
                retryPolicy.ExecuteAction(() => CreateTopicIfNotExists(namespaceManager, topic));
                topic.Subscriptions.AsParallel().ForAll(subscription =>
                {
                    retryPolicy.ExecuteAction(() => CreateSubscriptionIfNotExists(namespaceManager, topic, subscription));
                    retryPolicy.ExecuteAction(() => UpdateRules(namespaceManager, this.settings.ConnectionString, topic, subscription));
                });
            });

            // Execute migration support actions only after all the previous ones have been completed.
            foreach (var topic in this.settings.Topics)
            {
                foreach (var action in topic.MigrationSupport)
                {
                    retryPolicy.ExecuteAction(() => UpdateSubscriptionIfExists(namespaceManager, this.settings.ConnectionString, topic, action));
                }
            }

            this.initialized = true;
        }
        // GET api/nextsession
        public Session Get()
        {
            var retryStrategy = new Incremental(5, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2));
            var retryPolicy = new RetryPolicy<SqlAzureTransientErrorDetectionStrategy>(retryStrategy);
            
            Session nextSession = new Session();
            using (VidPackEntities db = new VidPackEntities())
            {
                retryPolicy.ExecuteAction(() =>
                {
                    db.Database.Connection.Open();
                });

                ExistingSession dbSession = retryPolicy.ExecuteAction<ExistingSession>(() =>
                    db.ExistingSession.Where(item => item.IsNextSession == 1).FirstOrDefault()
                );

                if (dbSession != null)
                    nextSession = new VidPackModel.Session()
                    {
                        SessionDate = dbSession.SessionDate.ToString(),
                        SessionDescription = dbSession.SessionDescription,
                        SessionSubTitle = dbSession.SessionSubTitle,
                        SessionThumbnailUrl = String.Format("{0}{1}", ThumbnailStorageUrl, dbSession.SessionThumbnailUri),
                        SessionTitle = dbSession.SessionSubTitle,
                        SessionVideoUrl = dbSession.SessionVideoUri == null ? "" : dbSession.SessionVideoUri,
                        Speaker = dbSession.Speaker
                    };
            }
            return nextSession; 
        }
示例#9
0
        public void Save(string id, string contentType, byte[] blob)
        {
            var client             = account.CreateCloudBlobClient();
            var containerReference = client.GetContainerReference(rootContainerName);

            var blobReference = containerReference.GetBlobReference(id);

            writeRetryPolicy.ExecuteAction(() => { blobReference.UploadByteArray(blob); });
        }
示例#10
0
        protected async Task <T> GetDataAsync <T>()
        {
            var response = await RetryPolicy.ExecuteAction(() =>
            {
                var message = new HttpRequestMessage(HttpMethod.Get, BaseUri);
                return(ExecuteRequest <T>(message));
            }).ConfigureAwait(false);

            return(response.Data);
        }
示例#11
0
 /// <summary>
 /// Insert a new record containing data from the specified object. The primary key in the specified record must be zero.
 /// Very similar to Dapper.Contrib.SqlMapperExtensions Insert method except:
 /// <list type="bullet">
 ///     <item><description>It manages (obtains, opens, closes and returns) the database connection itself</description></item>
 ///     <item><description>Transient errors are automatically retried</description></item>
 ///     <item><description>Transactions are not supported (in order to support retries)</description></item>
 ///     <item><description>Table / primary key naming conventions are &lt;EntityName>.&lt;EntityName>Id rather than &lt;EntityName>s.Id</description></item>
 /// </list>
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="goc"></param>
 /// <param name="entity"></param>
 /// <param name="commandTimeout"></param>
 /// <returns>The primary key of the </returns>
 public static long Insert <T>(this IGetOpenConnection goc, T entity, int?commandTimeout = null) where T : class
 {
     // TODO: Log that user did this query
     return(RetryPolicy.ExecuteAction <long>(() =>
     {
         using (var conn = goc.GetOpenConnection())
         {
             return conn.Insert <T>(entity, null, commandTimeout);
         }
     }));
 }
示例#12
0
 private void ProcessMessagesForSubscriptionAzure()
 {
     logger.Info("ProcessMessagesForSubscription Message Start {0} Declared {1} MessageTytpe {2}, IsReusable {3}", data.EndPointData.SubscriptionName,
                 data.EndPointData.DeclaredType.ToString(), data.EndPointData.MessageType.ToString(), data.EndPointData.IsReusable);
     retryPolicy.ExecuteAction(() => {
         try {
             data.Client.OnMessage(OnMessageHandler, options);
         }
         catch (Exception ex) {
             logger.Error("Error Calling OnMessage {0}", ex.ToString());
         }
     });
 }
示例#13
0
        /// <summary>
        /// Executes a Transact-SQL statement against the connection and returns the number of rows affected.
        /// </summary>
        public int ExecuteNonQuery()
        {
            Tracer.TraceEvent(TraceEventType.Verbose, 0, "ReliableSqlCommand => ExecuteNonQuery");

            return(RetryPolicy.ExecuteAction(() =>
            {
                if (Connection.State != ConnectionState.Open)
                {
                    Connection.Open();
                }

                return _sqlCommandToWrap.ExecuteNonQuery();
            }));
        }
示例#14
0
        public void OnMessageHandler(IBrokeredMessage msg)
        {
            //receive loop begin
            bool failed = false;

            if (msg != null)
            {
                // Make sure we are not told to stop receiving while we were waiting for a new message.
                if (!data.CancelToken.IsCancellationRequested)
                {
                    // Process the received message.
                    logger.Debug("ProcessMessagesForSubscription Start received new message: {0}", data.EndPointData.SubscriptionNameDebug);
                    var receiveState = new AzureReceiveState(data, methodInfo, serializer, msg);
                    failed = OnReceiveProcess(receiveState);
                    logger.Debug("ProcessMessagesForSubscription End received new message: {0}", data.EndPointData.SubscriptionNameDebug);
                }
            }

            if (data.CancelToken.IsCancellationRequested)
            {
                //Cancel the message pump.
                data.SetMessageLoopCompleted();
                try {
                    if (!data.Client.IsClosed)
                    {
                        data.Client.Close();
                    }
                }
                catch {
                }
            }
            else if (failed)
            {
                if (data.EndPointData.AttributeData.PauseTimeIfErrorWasThrown > 0)
                {
                    lock (lockObject) {
                        if (DateTime.Now.AddMilliseconds(-data.EndPointData.AttributeData.PauseTimeIfErrorWasThrown) >= lastResetTime)
                        {
                            retryPolicy.ExecuteAction(() => {
                                if (!data.Client.IsClosed)
                                {
                                    data.Client.Close();
                                }
                            });
                            Thread.Sleep(data.EndPointData.AttributeData.PauseTimeIfErrorWasThrown);
                            lastResetTime = DateTime.Now;
                            retryPolicy.ExecuteAction(() => {
                                Configure(endpoint);
                                data.Client.OnMessage(OnMessageHandler, options);
                            });
                        }
                    }
                }
                else
                {
                    Thread.Sleep(1000); //This has zero impact if the thread count is > 1
                }
            }
        }
        public void LoadTestAddPointMapping()
        {
            try
            {
                ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                    Globals.ShardMapManagerConnectionString,
                    ShardMapManagerLoadPolicy.Lazy);

                ListShardMap <int> lsm = smm.GetListShardMap <int>(ShardMapManagerLoadTests.s_listShardMapName);

                Assert.IsNotNull(lsm);
                do
                {
                    // Chose a random shard to add mapping.
                    Shard s = GetRandomOnlineShardFromShardMap((ShardMap)lsm);
                    if (s == null)
                    {
                        continue;
                    }

                    // Create a random integer key for a new mapping and verify that its not already present in this shard map.
                    int key = _r.Next(MinMappingPoint, MaxMappingPoint);
                    PointMapping <int> pExisting = null;

                    // choose different mapping if this one already exists.
                    if (lsm.TryGetMappingForKey(key, out pExisting))
                    {
                        continue;
                    }

                    Debug.WriteLine("Trying to add point mapping for key {0} to shard location {1}", key, s.Location);

                    PointMapping <int> p1 = lsm.CreatePointMapping(key, s);

                    Assert.IsNotNull(p1);

                    // Validate mapping by trying to connect
                    s_retryPolicy.ExecuteAction(
                        () => ValidateImpl(
                            (ShardMap)lsm,
                            key));
                }while (false);
            }
            catch (ShardManagementException sme)
            {
                Debug.WriteLine("Exception caught: {0}", sme.Message);
            }
        }
        protected void EnsureTopic(string topicName, bool enablePartitioning)
        {
            Guard.ArgumentNotNull(topicName, "topicName");
            bool createNew = false;

            try {
                logger.Info("EnsureTopic Try {0} ", topicName);
                // First, let's see if a topic with the specified name already exists.
                defaultTopic = verifyRetryPolicy.ExecuteAction <TopicDescription>(() => {
                    return(configurationFactory.NamespaceManager.GetTopic(topicName));
                });

                createNew = (defaultTopic == null);
            }
            catch (MessagingEntityNotFoundException) {
                logger.Info("EnsureTopic Does Not Exist {0} ", topicName);
                // Looks like the topic does not exist. We should create a new one.
                createNew = true;
            }

            if (!createNew && defaultTopic != null && !defaultTopic.EnablePartitioning && enablePartitioning)
            {
                throw new Exception("EnablePartitioning may not be changed on an existing Topic.");
            }

            // If a topic with the specified name doesn't exist, it will be auto-created.
            if (createNew)
            {
                try {
                    logger.Info("EnsureTopic CreateTopic {0} ", topicName);
                    var newTopic = new TopicDescription(topicName)
                    {
                        EnablePartitioning = enablePartitioning
                    };

                    defaultTopic = retryPolicy.ExecuteAction <TopicDescription>(() => {
                        return(configurationFactory.NamespaceManager.CreateTopic(newTopic));
                    });
                }
                catch (MessagingEntityAlreadyExistsException) {
                    logger.Info("EnsureTopic GetTopic {0} ", topicName);
                    // A topic under the same name was already created by someone else, perhaps by another instance. Let's just use it.
                    defaultTopic = retryPolicy.ExecuteAction <TopicDescription>(() => {
                        return(configurationFactory.NamespaceManager.GetTopic(topicName));
                    });
                }
            }
        }
示例#17
0
            /// <summary>
            /// Executes a stored procedure.
            /// </summary>
            /// <param name="procedureName">The procedure name.</param>
            /// <param name="parameters">The set of parameters.</param>
            /// <param name="outputParameters">The set of output parameters.</param>
            /// <param name="resultCallback">The result callback.</param>
            /// <param name="returnValue">The procedure result.</param>
            public void ExecuteStoredProcedure(string procedureName, ParameterSet parameters, ParameterSet outputParameters, Action <IDatabaseResult> resultCallback, out int returnValue)
            {
                ThrowIf.NullOrWhiteSpace(procedureName, "procedureName");
                ThrowIf.Null(parameters, "parameters");

                int?procedureReturnValue = null;

                if (this.settings != null)
                {
                    var settingsParameter = new QueryResultSettingsTableType(this.settings);
                    parameters.Add(QueryResultSettingsTableType.ParameterName, settingsParameter.DataTable);
                }

                RetryPolicy retryPolicy = this.DatabaseProvider.GetRetryPolicy();

                try
                {
                    retryPolicy.ExecuteAction(
                        () => this.DatabaseProvider.ExecuteStoredProcedure(this.ConnectionManager.Connection, procedureName, parameters, outputParameters, resultCallback, out procedureReturnValue),
                        (policy, retryCount, ex) => RetailLogger.Log.CrtDataAccessTransientExceptionOccurred(policy.RetryCount, policy.RetryInterval, retryCount, ex));
                }
                catch (DatabaseException ex)
                {
                    throw new StorageException(
                              StorageErrors.Microsoft_Dynamics_Commerce_Runtime_CriticalStorageError,
                              (int)ex.ErrorCode,
                              ex,
                              "Failed to read from the database. See inner exception for details");
                }

                returnValue = procedureReturnValue.GetValueOrDefault(0);
            }
        /// <summary>
        ///     Sets the specified key and data.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">The key.</param>
        /// <param name="data">The data.</param>
        /// <param name="returnIfExists">if set to <c>true</c> [return if exists].</param>
        public T Set <T>(string key, T data, bool returnIfExists)
        {
            // Define your retry strategy: retry 3 times, 1 second apart.
            var retryStrategy = new Incremental(5, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2));

            // Define your retry policy using the retry strategy and the Windows Azure storage
            // transient fault detection strategy.
            var retryPolicy = new RetryPolicy <StorageTransientErrorDetectionStrategy>(retryStrategy);

            // Do some work that may result in a transient fault.
            try
            {
                // Call a method that uses Windows Azure storage and which may
                // throw a transient exception.
                retryPolicy.ExecuteAction(
                    () =>
                {
                    var database = _connectionMultiplexer.GetDatabase();
                    database.StringSet(key, JsonSerializer.Serialize(data), ConfigurationHelper.SessionPoolTimeOut);
                });
            }
            catch (Exception ex)
            {
                //Logger.Error(ex, $"{GetType().FullName} / {MethodBase.GetCurrentMethod().Name}");
            }

            return(returnIfExists ? Get <T>(key) : data);
        }
        /// <summary>
        ///     Gets the specified key.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public T Get <T>(string key)
        {
            var cachedData    = default(T);
            var retryStrategy = new Incremental(5, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2));
            var retryPolicy   = new RetryPolicy <StorageTransientErrorDetectionStrategy>(retryStrategy);

            // Do some work that may result in a transient fault.
            try
            {
                retryPolicy.ExecuteAction(() =>
                {
                    var database = _connectionMultiplexer.GetDatabase();
                    var data     = database.StringGet(key);
                    if (data.HasValue)
                    {
                        cachedData = JsonSerializer.Deserialize <T>(database.StringGet(key));
                    }
                });
            }
            catch (Exception ex)
            {
                //Logger.Error(ex, $"{GetType().FullName} / {MethodBase.GetCurrentMethod().Name}");
            }
            return(cachedData);
        }
示例#20
0
    [Ignore]    // REVIEW
    public void TestSqlAzureTransientErrorDetectionStrategyWithRetryableError()
    {
        int[] errors = { 40197, 40501, 40540, 10053, 10054, 10060, 40549, 40550, 40551, 40552, 40553, 40613, 40143, 233, 64 };
        Type  type   = typeof(SqlDatabaseTransientErrorDetectionStrategy).GetNestedType("ProcessNetLibErrorCode", BindingFlags.NonPublic) !;

        errors = errors.AddRange((int[])Enum.GetValues(type));

        Exception[] exceptions = FakeSqlExceptionGenerator.GenerateFakeSqlExceptions(errors);
        exceptions = exceptions.AddRange(new TimeoutException(), new EntityException("Forced Exception"));

        RetryPolicy defaultPolicy = new RetryPolicy <SqlDatabaseTransientErrorDetectionStrategy>(exceptions.Length - 1, RetryStrategy.DefaultRetryInterval);
        int         execCount     = 0;

        try
        {
            defaultPolicy.ExecuteAction(() =>
            {
                Exception ex = exceptions[execCount];
                execCount++;
                throw ex;
            });
        }
        catch (EntityException ex)
        {
            Assert.AreEqual("Forced Exception", ex.Message);
        }

        Assert.AreEqual(exceptions.Length, execCount, "The action was not executed the expected amount of times");
    }
示例#21
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        private IRestResponse CallApi(IRestRequest request)
        {
            Func <IRestResponse> func = () =>
            {
                RestClient client = new RestClient(_apiHost);
                var        res    = client.Execute(request);
                if (res.StatusCode != HttpStatusCode.OK)
                {
                    throw new Exception("Fetch Fail");
                }
                return(res);
            };
            IRestResponse response = null;

            //如果CallApi失败,抛出的异常由外部调用方,即ConfigManager来处理,该部分不与异常处理产生交集
            if (_policy != null)
            {
                response = _policy.ExecuteAction(func);
            }
            else
            {
                response = func();
            }
            return(response);
        }
示例#22
0
        void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                // Build the Queries
                var rootQuery  = DatabaseHelper.BuildInsertQuery();
                var batchQuery = DatabaseHelper.BuildBatchQuery(_model.BulkPurchaseQty, rootQuery);

                // Create the Retry Policy
                var retryPolicy = new RetryPolicy <ErrorDetectionStrategy>(_backoffStrategy);

                retryPolicy.ExecuteAction(() =>
                {
                    do
                    {
                        if (_worker.CancellationPending)
                        {
                            e.Cancel = true;
                            break;
                        }

                        // Load the Database & Sleep for x amount of time
                        LoadDatabase(retryPolicy, batchQuery);
                        Sleep();

                        // Switch Databases
                        _loadingPrimaryDatabase = !_loadingPrimaryDatabase;
                    }while (_totalElapsedSeconds < ConfigHelper.Runtime);
                });
            }
            catch (Exception ex)
            {
                e.Result = ex.Message;
            }
        }
        protected RetriableTransformBlock(ExecutionDataflowBlockOptions options, RetryPolicy retryPolicy, bool throwOnException = true)
        {
            if (retryPolicy == null)
            {
                throw new ArgumentNullException("retryPolicy");
            }

            retryPolicy.Retrying += (sender, args) => OnRetry(args);

            _block = new TransformManyBlock <TInput, TOutput>(input =>
            {
                try
                {
                    return(retryPolicy.ExecuteAction(() => new[] { Handle(input) }));
                }
                catch
                {
                    if (throwOnException)
                    {
                        throw;
                    }

                    return(Enumerable.Empty <TOutput>());
                }
            }, options);
        }
示例#24
0
        private async Task <string> PostAsync(DataMessage requestMessage)
        {
            WebRequest wRequest = WebRequest.Create(baseUri);

            wRequest.ContentType = "application/json";
            wRequest.Method      = WebRequestMethods.Http.Post;

            wRequest.Headers.Add($"Authorization:key={apiKey}");

            using (var reqStream = await wRequest.GetRequestStreamAsync())
                using (var streamWriter = new StreamWriter(reqStream))
                {
                    string json = JSON.SerializeDynamic(requestMessage, Options.IncludeInherited);

                    await streamWriter.WriteAsync(json);
                }

            var retryStrategy = new ExponentialBackoff(3, TimeSpan.FromSeconds(2),
                                                       TimeSpan.FromSeconds(20), TimeSpan.FromSeconds(1));

            var retryPolicy = new RetryPolicy <WebExceptionDetectionStrategy>(retryStrategy);

            return(await retryPolicy.ExecuteAction(async() =>
            {
                using (var httpResponse = await wRequest.GetResponseAsync())
                    using (var responseStream = httpResponse.GetResponseStream())
                        using (var streamReader = new StreamReader(responseStream))
                        {
                            return await streamReader.ReadToEndAsync();
                        }
            }));
        }
        public void Execute(int?timeout, IsolationLevel isolationLevel, Action action)
        {
            RetryPolicy policy = new RetryPolicy <SqlDatabaseTransientErrorDetectionStrategy>(3, TimeSpan.FromMilliseconds(750));

            try
            {
                policy.ExecuteAction(() =>
                {
                    var context = _unitOfWork.GetContext();

                    if (timeout.HasValue)
                    {
                        context.Database.SetCommandTimeout(timeout.Value);
                    }

                    using (IDbContextTransaction transaction = context.Database.BeginTransaction(isolationLevel))
                    {
                        try
                        {
                            action();
                            transaction.Commit();
                        }
                        catch
                        {
                            transaction.Rollback();
                            throw;
                        }
                    }
                });
            }
            catch { }
        }
示例#26
0
        public Telegram Process(TelegramBodyType type, Telegram telegram)
        {
            try
            {
                var policy = new RetryPolicy(new TelegramProcessingErrorDetectionStrategy(telegram),
                                             new FixedInterval(_retryPolicyCount, TimeSpan.FromSeconds(_retryPolicyTime)));

                return(policy.ExecuteAction(() => ProcessTelegram(telegram)));
            }
            catch (Exception ex)
            {
                //                if (ex is UnauthorizedAccessException)
                //                    throw;
                if (ex is TimeoutException)
                {
                    _log.Error(ex);
                }
                else
                {
                    IsConnected = false;
                    _log.Error("Ошибка отправки телеграммы", ex);
                }
                throw;
            }
        }
示例#27
0
        /// <summary>
        /// Uses a reliable connection retry policy and transaction
        /// scoping to add and submit a set of EntityObjects.
        /// </summary>
        /// <typeparam name="T">Must derive from <typeparamref name="EntityObject"/>.</typeparam>
        /// <param name="context">Any ObjectContext</param>
        /// <param name="objects">A set of <typeparamref name="EntityObject"/>s to save.</param>
        public static void AddObjectsAndSave <T>(this ObjectContext context, IEnumerable <T> objects)
            where T : EntityObject
        {
            if (!objects.Any())
            {
                return;
            }

            var policy = new RetryPolicy <SqlAzureTransientErrorDetectionStrategy>
                             (10, TimeSpan.FromSeconds(10));
            var tso = new TransactionOptions();

            tso.IsolationLevel = IsolationLevel.ReadCommitted;

            var name = context.GetTableName <T>();

            foreach (var item in objects)
            {
                context.AddObject(name, item);
            }


            policy.ExecuteAction(() =>
            {
                using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required, tso))
                {
                    context.SaveChanges();

                    ts.Complete();
                }
            });
        }
示例#28
0
        /// <summary>
        /// Returns an opened connection to the caller
        /// </summary>
        /// <returns></returns>
        public static SqlConnection GetSqlConnection()
        {
            RetryPolicy rp = new RetryPolicy(new ConnectionExceptionDetectionStrategy(), 3);

            rp.Retrying += (sender, args) =>
            {
                // This code runs when a retry is taking place...
                System.Diagnostics.Debug.WriteLine("Retrying... count=" + args.CurrentRetryCount.ToString());
            };

            // Open the database connection with retries
            SqlConnection conn = new SqlConnection(ConnectionString);

            //conn.OpenWithRetry(rp);

            rp.ExecuteAction(() =>
            {
                // The action to perform with retry
                conn.Open();
            });


            conn.StatisticsEnabled = EnableStatistics;
            conn.StateChange      += conn_StateChange;
            return(conn);
        }
示例#29
0
        public string GetContainerSasUri(string containerDirectory)
        {
            var connectionString  = _applicationConfiguration.GetSetting("storage_connection_string");
            var retryStrategy     = new FixedInterval(3, TimeSpan.FromSeconds(1));
            var retryPolicy       = new RetryPolicy <StorageTransientErrorDetectionStrategy>(retryStrategy);
            var sasContainerToken = Empty;

            try
            {
                retryPolicy.ExecuteAction(
                    () =>
                {
                    var storageAccount     = CloudStorageAccount.Parse(connectionString);
                    var cloudBlobClient    = storageAccount.CreateCloudBlobClient();
                    var cloudBlobContainer = cloudBlobClient.GetContainerReference(containerDirectory);
                    cloudBlobContainer.CreateIfNotExists();
                    cloudBlobContainer.SetPermissions(new BlobContainerPermissions {
                        PublicAccess = BlobContainerPublicAccessType.Off
                    });
                    var adHocPolicy = new SharedAccessBlobPolicy()
                    {
                        SharedAccessExpiryTime = DateTime.UtcNow.AddHours(3),
                        Permissions            = SharedAccessBlobPermissions.Read
                    };
                    sasContainerToken = cloudBlobContainer.GetSharedAccessSignature(adHocPolicy, null);
                });
            }
            catch (Exception exception)
            {
                var e = exception.Message;
                return(sasContainerToken);
            }
            return(sasContainerToken);
        }
示例#30
0
        internal static void TestRetryPolicy(RetryPolicy retryPolicy, out int retryCount, out TimeSpan totalDelay)
        {
            int    callbackCount  = 0;
            double totalDelayInMs = 0;

            retryPolicy.Retrying += (sender, args) =>
            {
                callbackCount++;
                totalDelayInMs += args.Delay.TotalMilliseconds;
            };

            try
            {
                retryPolicy.ExecuteAction(() =>
                {
                    throw new TimeoutException("Forced Exception");
                });
            }
            catch (TimeoutException ex)
            {
                Assert.AreEqual("Forced Exception", ex.Message);
            }

            retryCount = callbackCount;
            totalDelay = TimeSpan.FromMilliseconds(totalDelayInMs);
        }
示例#31
0
        public void Sample3_Level300_AutomaticRetry_ManuallyUsingMicrosoftTransientFaultHandling_UsingOperationsProperty()
        {
            using (var service = ServiceConsumerFactory.Create <IFakeService>(() => new FakeServiceClient()))
            {
                try
                {
                    int actual = 0;

                    var microsoftRetryStrategy = new Incremental(5, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(2));
                    var microsoftRetryPolicy   = new RetryPolicy <SoapFaultWebServiceTransientErrorDetectionStrategy>(microsoftRetryStrategy);

                    microsoftRetryPolicy.ExecuteAction(() =>
                    {
                        actual = service.Operations.AddIntegers(1, 1);
                    });

                    Console.WriteLine("Actual: " + actual);
                    Assert.AreEqual(2, actual);

                    return;
                }
                catch (FaultException fe)
                {
                    Console.WriteLine("Service operation threw a fault: " + fe.ToString());
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Technical error occurred while calling the service operation: " + ex.ToString());
                }

                Assert.Fail("Service operation was not successfully called");
            }
        }
        private void GetToken()
        {
            using (WebClient client = new WebClient())
            {
                client.BaseAddress = this._acsBaseAddress;

                var oauthRequestValues = new NameValueCollection
                {
                    { "grant_type", GrantType },
                    { "client_id", this._clientId },
                    { "client_secret", this._clientSecret },
                    { "scope", this._scope },
                };

                RetryPolicy retryPolicy = new RetryPolicy(
                    new WebRequestTransientErrorDetectionStrategy(),
                    RetryStrategyFactory.DefaultStrategy());

                retryPolicy.ExecuteAction(
                    () =>
                {
                    byte[] responseBytes = client.UploadValues("/v2/OAuth2-13", "POST", oauthRequestValues);

                    using (var responseStream = new MemoryStream(responseBytes))
                    {
                        OAuth2TokenResponse tokenResponse = (OAuth2TokenResponse) new DataContractJsonSerializer(typeof(OAuth2TokenResponse)).ReadObject(responseStream);
                        this.AccessToken      = tokenResponse.AccessToken;
                        this._tokenExpiration = DateTime.Now.AddSeconds(tokenResponse.ExpirationInSeconds - ExpirationTimeBufferInSeconds);
                    }
                });
            }
        }
示例#33
0
        public bool DeleteFile(string filename, string personFolder, string containerDirectory)
        {
            //var targetFilename = Path.Combine(@"C:\Development\MediClinic\BlobStorage", personFolder, filename);
            //File.Delete(targetFilename);
            //return true;

            var destinationBlobName = IsNullOrEmpty(personFolder) ? filename : Format("{0}/{1}", personFolder, filename);
            var connectionString    = _applicationConfiguration.GetSetting("storage_connection_string");
            var retryStrategy       = new FixedInterval(3, TimeSpan.FromSeconds(1));
            var retryPolicy         = new RetryPolicy <StorageTransientErrorDetectionStrategy>(retryStrategy);

            try
            {
                retryPolicy.ExecuteAction(
                    () =>
                {
                    var account   = CloudStorageAccount.Parse(connectionString);
                    var client    = account.CreateCloudBlobClient();
                    var container = client.GetContainerReference(containerDirectory);
                    var blob      = container.GetBlockBlobReference(destinationBlobName);
                    blob.Delete();
                    return(true);
                });
            }
            catch (Exception exception)
            {
                //We will catch deletes as it just means it doesnt exist
                var e = exception.Message;
                //_logger.Error(string.Format("Blob storage exception {0}", e.Message), e);
            }
            return(false);
        }
        public void Initialize()
        {
            var retryStrategy = new Incremental(3, TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1));
            var retryPolicy = new RetryPolicy<ServiceBusTransientErrorDetectionStrategy>(retryStrategy);
            var tokenProvider = TokenProvider.CreateSharedSecretTokenProvider(settings.TokenIssuer, settings.TokenAccessKey);
            var serviceUri = ServiceBusEnvironment.CreateServiceUri(settings.ServiceUriScheme, settings.ServiceNamespace, settings.ServicePath);
            var namespaceManager = new NamespaceManager(serviceUri, tokenProvider);

            foreach (var topic in this.settings.Topics)
            {
                retryPolicy.ExecuteAction(() => CreateTopicIfNotExists(namespaceManager, topic));
                foreach (var subscription in topic.Subscriptions)
                {
                    retryPolicy.ExecuteAction(() => CreateSubscriptionIfNotExists(namespaceManager, topic, subscription));
                }
            }

            this.initialized = true;
        }
        // GET api/notification
        public IEnumerable<NotificationInfo> Get()
        {

            var retryStrategy = new Incremental(5, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2));
            var retryPolicy = new RetryPolicy<SqlAzureTransientErrorDetectionStrategy>(retryStrategy);

            List<NotificationInfo> notifications = new List<NotificationInfo>(); 
            using (VidPackEntities db = new VidPackEntities())
            {

                retryPolicy.ExecuteAction(() =>
                {
                    db.Database.Connection.Open();
                });

                notifications = retryPolicy.ExecuteAction<List<NotificationInfo>>(() =>
                        db.Notification.Select(item => new NotificationInfo() { 
                        NotificationTag = item.NotificationTag,
                    }).ToList<NotificationInfo>()
                ); 
            }
            return notifications; 
        }
示例#36
0
 public DataSet GetBulkRows()
 {
     var ds = new DataSet(TableName);
     using (var con = new SqlConnection(Connection.ConnectionString))
     {
         using (var cmd = SelectBulkRows)
         {
             var policy = new RetryPolicy<SqlTransientErrorDetectionStrategy>(2, TimeSpan.FromSeconds(5));
             cmd.Connection = con;
             con.Open();
             var adapter = new SqlDataAdapter(cmd);
             policy.ExecuteAction(() => adapter.Fill(ds, TableName));
         }
     }
     return ds;
 }
        protected override void RunDatabaseLoad(DoWorkEventArgs e)
        {
            try
            {
                // Create the Retry Policy
                var retryPolicy = new RetryPolicy<ErrorDetectionStrategy>(BackoffStrategy);

                retryPolicy.ExecuteAction(() => LoadDatabase(retryPolicy, _data));

                if (Worker.CancellationPending)
                {
                    e.Cancel = true;
                }
            }
            catch (Exception ex)
            {
                e.Result = ex.Message;
            }
        }
示例#38
0
        public void TestSqlAzureTransientErrorDetectionStrategyWithNonRetryableError()
        {
            RetryPolicy defaultPolicy = new RetryPolicy<SqlAzureTransientErrorDetectionStrategy>(1, RetryStrategy.DefaultRetryInterval);
            int execCount = 0;

            try
            {
                defaultPolicy.ExecuteAction(() =>
                {
                    execCount++;
                    throw new ApplicationException("Forced Exception");
                });
            }
            catch (ApplicationException ex)
            {
                Assert.AreEqual("Forced Exception", ex.Message);
            }

            Assert.AreEqual<int>(1, execCount, "The action was not executed the expected amount of times");
        }
        public void TestBackoffRetryPolicyWithRetryableError()
        {
            RetryPolicy retryPolicy = new RetryPolicy<SqlAzureTransientErrorDetectionStrategy>(RetryStrategy.DefaultClientRetryCount, RetryStrategy.DefaultMinBackoff, RetryStrategy.DefaultMaxBackoff, RetryStrategy.DefaultClientBackoff);
            int execCount = 0;

            try
            {
                retryPolicy.ExecuteAction(() =>
                {
                    execCount++;

                    throw new TimeoutException("Forced Exception");
                });
            }
            catch (TimeoutException ex)
            {
                Assert.AreEqual("Forced Exception", ex.Message);
            }

            Assert.AreEqual<int>(RetryStrategy.DefaultClientRetryCount, execCount - 1, "The action was not retried using the expected amount of times");
        }
        [Ignore]    // REVIEW - Test will continue throwing the exception so the action will eventually run out of retry attempts
        public void TestThrottlingConditionSimulation()
        {
            // Instantiate a retry policy capable of detecting transient faults that are specific to SQL Database. Use the default retry count.
            RetryPolicy retryPolicy = new RetryPolicy<SqlDatabaseTransientErrorDetectionStrategy>(RetryStrategy.DefaultClientRetryCount);

            // Register a custom event handler that will be invoked by the policy whenever a retry condition is encountered.
            retryPolicy.Retrying += (sender, args) =>
            {
                // Log a warning message to indicate that a retry loop kicked in.
                Trace.TraceWarning("Retry condition encountered. Reason: {0} (retry count: {1}, retry delay: {2})", args.LastException.Message, args.CurrentRetryCount, args.Delay);

                // Make sure we are dealing with a SQL exception.
                if (args.LastException is SqlException)
                {
                    // Parse the exception to find out whether it is indicative of any throttling conditions.
                    ThrottlingCondition throttlingCondition = ThrottlingCondition.FromException(args.LastException as SqlException);

                    // Verify whether throttling condition were detected.
                    if (!throttlingCondition.IsUnknown)
                    {
                        // Log a further warning message with details on throttling conditions encountered.
                        Trace.TraceWarning("Throttling condition detected. Details: {0}", throttlingCondition);
                    }
                }
            };

            // Invoke a database operation or set of database operations against SQL Database.
            retryPolicy.ExecuteAction(() =>
            {
                // Open a connection, execute a SQL query or stored procedure, perform any kind of database operations.
                // ...

                var sqlEx = FakeSqlExceptionGenerator.GenerateFakeSqlException(ThrottlingCondition.ThrottlingErrorNumber, Resources.SampleThrottlingErrorMsg);
                throw sqlEx;
            });
        }
示例#41
0
        private static void RetryPolityUsingCode(IUnityContainer container, IService service, OutputWriterService writer)
        {
            writer.WriteLine("Begin sample: RetryPolityUsingCode");
            // Define your retry strategy: retry 5 times, starting 1 second apart
            // and adding 2 seconds to the interval each retry.
            var retryStrategy = new Incremental(5, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2));

            // Define your retry policy using the retry strategy and the Windows Azure storage
            // transient fault detection strategy.
            var retryPolicy = new RetryPolicy<FileSystemTransientErrorDetectionStrategy>(retryStrategy);


            try
            {
                // Do some work that may result in a transient fault.
                retryPolicy.ExecuteAction(service.DoSlowAndImportantTask);
            }
            catch (Exception exception)
            {
                // All the retries failed.
                writer.WriteLine("An Exception has been thrown:\n{0}", exception);
            }
            writer.WriteLine("End sample: RetryPolityUsingCode");
        }
        internal static void TestRetryPolicy(RetryPolicy retryPolicy, out int retryCount, out TimeSpan totalDelay)
        {
            var callbackCount = 0;
            double totalDelayInMs = 0;

            retryPolicy.Retrying += (sender, args) =>
            {
                callbackCount++;
                totalDelayInMs += args.Delay.TotalMilliseconds;
            };

            try
            {
                retryPolicy.ExecuteAction(() => { throw new TimeoutException("Forced Exception"); });
            }
            catch (TimeoutException ex)
            {
                Assert.Equal("Forced Exception", ex.Message);
            }

            retryCount = callbackCount;
            totalDelay = TimeSpan.FromMilliseconds(totalDelayInMs);
        }
示例#43
0
        public void TestBackoffRetryPolicyWithRetryableError()
        {
            const int MaxRetryCount = 4;
            RetryPolicy retryPolicy = new RetryPolicy<SqlDatabaseTransientErrorDetectionStrategy>(MaxRetryCount, TimeSpan.FromMilliseconds(500), TimeSpan.FromSeconds(1));
            int execCount = 0;

            try
            {
                retryPolicy.ExecuteAction(() =>
                {
                    execCount++;

                    throw new TimeoutException("Forced Exception");
                });
            }
            catch (TimeoutException ex)
            {
                Assert.AreEqual("Forced Exception", ex.Message);
            }

            Assert.AreEqual<int>(MaxRetryCount, execCount - 1, "The action was not retried using the expected amount of times");
        }
        void purchaseWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                object[] args = e.Argument as object[];
                if (args.Length != 8 || Convert.ToInt32(args[3]) <= 0 || Convert.ToInt32(args[4]) <= 0 || Convert.ToInt32(args[5]) <= 0 || Convert.ToInt32(args[6]) <= 0 || Convert.ToInt32(args[7]) <= 0)
                {
                    e.Result = "Please ensure you have selected a concert, ticket level, and customer, as well as supplied the ticket count and bulk purchase quantity.";
                    return;
                }
                string conn = ConstructConn(args[0].ToString(), TenantDbName, args[1].ToString(), args[2].ToString());
                string rootQuery =
                    string.Format("Insert Into Tickets (CustomerId, Name, TicketLevelId, ConcertId, PurchaseDate) Values ({0}, '', {1}, {2}, GETDATE())",
                        Convert.ToInt32(args[5]).ToString(), Convert.ToInt32(args[4]).ToString(), Convert.ToInt32(args[3]).ToString());
                string runtimeQuery = string.Empty;
                int bulkPurchase = Convert.ToInt32(args[7]);
                for (int i = 0; i < bulkPurchase; i++)
                    runtimeQuery += rootQuery + "; ";
                int ticketCount = Convert.ToInt32(args[6]);
                int purchaseCounter = 0;

                var retryPolicy = new RetryPolicy<CustomTransientErrorDetectionStrategy>(exponentialBackoffStrategy);

                retryPolicy.ExecuteAction(() =>
                {
                    using (ReliableSqlConnection reliableSqlConnection = new ReliableSqlConnection(conn, retryPolicy))
                    {
                        reliableSqlConnection.Open(retryPolicy);
                        IDbTransaction transaction = reliableSqlConnection.BeginTransaction();
                        using (var cmd = new SqlCommand(runtimeQuery, reliableSqlConnection.Current, (SqlTransaction)transaction))
                        {
                            while (purchaseCounter < ticketCount)
                            {
                                if (purchaseWorker.CancellationPending)
                                    break;
                                if (ticketCount - purchaseCounter < bulkPurchase)
                                {
                                    runtimeQuery = string.Empty;
                                    bulkPurchase = ticketCount - purchaseCounter;
                                    for (int i = 0; i < bulkPurchase; i++)
                                        runtimeQuery += rootQuery;
                                    cmd.CommandText = runtimeQuery;
                                }

                                cmd.ExecuteNonQueryWithRetry(retryPolicy);

                                purchaseWorker.ReportProgress(Convert.ToInt32(((purchaseCounter * 1.0) / ticketCount) * 100), purchaseCounter);
                                purchaseCounter = purchaseCounter + bulkPurchase;
                            }
                            transaction.Commit();
                        }
                    }
                });
                e.Result = purchaseCounter + " tickets purchased";
            }
            catch (Exception ex) { e.Result = ex.Message; }
        }
        private Uri GetAccountApiEndpoint(OAuthDataServiceAdapter dataServiceAdapter, ServiceVersionAdapter versionAdapter, Uri apiServer)
        {
            RetryPolicy retryPolicy = new RetryPolicy(
                GetWebRequestTransientErrorDetectionStrategy(),
                RetryStrategyFactory.DefaultStrategy());

            Uri apiEndpoint = null;
            retryPolicy.ExecuteAction(
                    () =>
                        {
                            HttpWebRequest request = (HttpWebRequest) WebRequest.Create(apiServer);
                            request.AllowAutoRedirect = false;
                            dataServiceAdapter.AddAccessTokenToRequest(request);
                            versionAdapter.AddVersionToRequest(request);

                            using (WebResponse response = request.GetResponse())
                            {
                                apiEndpoint = GetAccountApiEndpointFromResponse(response);
                            }
                        }
                );

            return apiEndpoint;
        }
        private void GetToken()
        {
            using (WebClient client = new WebClient())
            {
                client.BaseAddress = this._acsBaseAddress;

                var oauthRequestValues = new NameValueCollection
                {
                    {"grant_type", GrantType},
                    {"client_id", this._clientId},
                    {"client_secret", this._clientSecret},
                    {"scope", this._scope},
                };

                RetryPolicy retryPolicy = new RetryPolicy(
                    new WebRequestTransientErrorDetectionStrategy(),
                    RetryStrategyFactory.DefaultStrategy());

                retryPolicy.ExecuteAction(
                    () =>
                        {
                            byte[] responseBytes = client.UploadValues("/v2/OAuth2-13", "POST", oauthRequestValues);

                            using (var responseStream = new MemoryStream(responseBytes))
                            {
                                OAuth2TokenResponse tokenResponse = (OAuth2TokenResponse)new DataContractJsonSerializer(typeof (OAuth2TokenResponse)).ReadObject(responseStream);
                                this.AccessToken = tokenResponse.AccessToken;
                                this._tokenExpiration = DateTime.Now.AddSeconds(tokenResponse.ExpirationInSeconds - ExpirationTimeBufferInSeconds);
                            }
                        });
            }
        }
        public void TestFastFirstRetry()
        {
            RetryPolicy retryPolicy =
                new RetryPolicy<SqlAzureTransientErrorDetectionStrategy>(new FixedInterval("", 1, TimeSpan.FromMinutes(10), true));

            Stopwatch stopwatch = Stopwatch.StartNew();

            try
            {
                retryPolicy.ExecuteAction(() =>
                {
                    SimulateFailure(retryPolicy);
                });
            }
            catch (TimeoutException ex)
            {
                Assert.AreEqual("Forced Exception", ex.Message);
            }

            stopwatch.Stop();
            Assert.IsFalse(stopwatch.Elapsed.TotalSeconds > 5, "FastFirstRetry does not seem to work correctly");

            retryPolicy = new RetryPolicy<SqlAzureTransientErrorDetectionStrategy>(2, TimeSpan.FromSeconds(1));
            stopwatch.Start();

            try
            {
                retryPolicy.ExecuteAction(() =>
                {
                    SimulateFailure(retryPolicy);
                });
            }
            catch (TimeoutException ex)
            {
                Assert.AreEqual("Forced Exception", ex.Message);
            }

            stopwatch.Stop();
            Assert.IsTrue(stopwatch.Elapsed.TotalSeconds >= 1 && stopwatch.Elapsed.TotalSeconds < 2, "FastFirstRetry does not seem to work correctly");
        }
示例#48
0
        private static void RunLoop()
        {
            MessagingFactory factory = MessagingFactory.Create();
            SubscriptionClient auditSubscriptionClient = factory.CreateSubscriptionClient("IssueTrackingTopic", "AuditSubscription", ReceiveMode.ReceiveAndDelete);

            var retryPolicy = new RetryPolicy<ServiceBusTransientErrorDetectionStrategy>(RetryStrategy.DefaultClientRetryCount);

            var waitTimeout = TimeSpan.FromSeconds(10);

            // Declare an action acting as a callback whenever a message arrives on a queue.
            AsyncCallback completeReceive = null;

            // Declare an action acting as a callback whenever a non-transient exception occurs while receiving or processing messages.
            Action<Exception> recoverReceive = null;

            // Declare a cancellation token that is used to signal an exit from the receive loop.
            var cts = new CancellationTokenSource();

            // Declare an action implementing the main processing logic for received messages.
            Action<BrokeredMessage> processMessage = ((msg) =>
            {
                // Put your custom processing logic here. DO NOT swallow any exceptions.

                Console.WriteLine(msg.Properties["HorseId"]);

            });

            // Declare an action responsible for the core operations in the message receive loop.
            Action receiveMessage = (() =>
            {
                // Use a retry policy to execute the Receive action in an asynchronous and reliable fashion.
                retryPolicy.ExecuteAction
                (
                    (cb) =>
                    {
                        // Start receiving a new message asynchronously.
                        auditSubscriptionClient.BeginReceive(waitTimeout, cb, null);
                    },
                    (ar) =>
                    {
                        // Make sure we are not told to stop receiving while we were waiting for a new message.
                        if (!cts.IsCancellationRequested)
                        {
                            // Complete the asynchronous operation. This may throw an exception that will be handled internally by retry policy.
                            BrokeredMessage msg = auditSubscriptionClient.EndReceive(ar);

                            // Check if we actually received any messages.
                            if (msg != null)
                            {
                                // Make sure we are not told to stop receiving while we were waiting for a new message.
                                if (!cts.IsCancellationRequested)
                                {
                                    try
                                    {
                                        // Process the received message.
                                        processMessage(msg);

                                        // With PeekLock mode, we should mark the processed message as completed.
                                        if (auditSubscriptionClient.Mode == ReceiveMode.PeekLock)
                                        {
                                            // Mark brokered message as completed at which point it's removed from the queue.
                                            msg.Complete();
                                        }
                                    }
                                    catch
                                    {
                                        // With PeekLock mode, we should mark the failed message as abandoned.
                                        if (auditSubscriptionClient.Mode == ReceiveMode.PeekLock)
                                        {
                                            // Abandons a brokered message. This will cause Service Bus to unlock the message and make it available
                                            // to be received again, either by the same consumer or by another completing consumer.
                                            msg.Abandon();
                                        }

                                        // Re-throw the exception so that we can report it in the fault handler.
                                        throw;
                                    }
                                    finally
                                    {
                                        // Ensure that any resources allocated by a BrokeredMessage instance are released.
                                        msg.Dispose();
                                    }
                                }
                                else
                                {
                                    // If we were told to stop processing, the current message needs to be unlocked and return back to the queue.
                                    if (auditSubscriptionClient.Mode == ReceiveMode.PeekLock)
                                    {
                                        msg.Abandon();
                                    }
                                }
                            }
                        }

                        // Invoke a custom callback method to indicate that we have completed an iteration in the message receive loop.
                        completeReceive(ar);
                    },
                    () =>
                    {
                        Console.WriteLine("Success Handler");
                    },
                    (ex) =>
                    {
                        // Invoke a custom action to indicate that we have encountered an exception and
                        // need further decision as to whether to continue receiving messages.
                        recoverReceive(ex);
                    });
            });

            // Initialize a custom action acting as a callback whenever a message arrives on a queue.
            completeReceive = ((ar) =>
            {
                if (!cts.IsCancellationRequested)
                {
                    // Continue receiving and processing new messages until we are told to stop.
                    receiveMessage();
                }
            });

            // Initialize a custom action acting as a callback whenever a non-transient exception occurs while receiving or processing messages.
            recoverReceive = ((ex) =>
            {
                // Just log an exception. Do not allow an unhandled exception to terminate the message receive loop abnormally.
                Console.WriteLine(ex.Message);

                if (!cts.IsCancellationRequested)
                {
                    // Continue receiving and processing new messages until we are told to stop regardless of any exceptions.
                    receiveMessage();
                }
            });

            // Start receiving messages asynchronously.
            receiveMessage();

            // Perform any other work. Message will keep arriving asynchronously while we are busy doing something else.

            // Stop the message receive loop gracefully.
            cts.Cancel();
        }
示例#49
0
        public EventProcessor CreateEventProcessor(string topic, string subscription, SubscriptionSettings subscriptionSettings, IEventHandler handler, ITextSerializer serializer, bool instrumentationEnabled = false)
        {
            var retryStrategy = new Incremental(3, TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1));
            var retryPolicy = new RetryPolicy<ServiceBusTransientErrorDetectionStrategy>(retryStrategy);
            var namespaceManager = settings.GetNamespaceManager();
            var topicSettings = new TopicSettings
            {
                IsEventBus = true,
                DuplicateDetectionHistoryTimeWindow = new TimeSpan(0, 30, 0),
                Path = topic
            };
            retryPolicy.ExecuteAction(() => CreateSubscriptionIfNotExists(namespaceManager, topicSettings, subscriptionSettings));
            retryPolicy.ExecuteAction(() => UpdateRules(namespaceManager, topicSettings, subscriptionSettings, settings.GetMessagingFactory()));

            IMessageReceiver receiver;

            if (subscriptionSettings.RequiresSession)
            {
                var instrumentation = new SessionSubscriptionReceiverInstrumentation(subscription, instrumentationEnabled);
                try
                {
                    receiver = (IMessageReceiver)new SessionSubscriptionReceiver(this.settings, topic, subscription, true, instrumentation);
                }
                catch
                {
                    instrumentation.Dispose();
                    throw;
                }
            }
            else
            {
                var instrumentation = new SubscriptionReceiverInstrumentation(subscription, instrumentationEnabled);
                try
                {
                    receiver = (IMessageReceiver)new SubscriptionReceiver(this.settings, topic, subscription, true, instrumentation);
                }
                catch
                {
                    instrumentation.Dispose();
                    throw;
                }
            }

            EventProcessor processor;
            try
            {
                processor = new EventProcessor(receiver, serializer);
            }
            catch
            {
                using (receiver as IDisposable) { }
                throw;
            }

            try
            {
                processor.Register(handler);

                return processor;
            }
            catch
            {
                processor.Dispose();
                throw;
            }
        }
示例#50
0
        public void RegisterTopicsAndSubscriptions(List<TopicSettings> topics)
        {
            var namespaceManager = settings.GetNamespaceManager();

            var retryStrategy = new Incremental(3, TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1));
            var retryPolicy = new RetryPolicy<ServiceBusTransientErrorDetectionStrategy>(retryStrategy);
            topics.AsParallel().ForAll(topic =>
            {
                retryPolicy.ExecuteAction(() => CreateTopicIfNotExists(namespaceManager, topic));
                topic.Subscriptions.AsParallel().ForAll(subscription =>
                {
                    retryPolicy.ExecuteAction(() => CreateSubscriptionIfNotExists(namespaceManager, topic, subscription));
                    retryPolicy.ExecuteAction(() => UpdateRules(namespaceManager, topic, subscription, settings.GetMessagingFactory()));
                });
            });

            // Execute migration support actions only after all the previous ones have been completed.
            foreach (var topic in topics)
            {
                foreach (var action in topic.MigrationSupport)
                {
                    retryPolicy.ExecuteAction(() => UpdateSubscriptionIfExists(namespaceManager, topic, action, settings.GetMessagingFactory()));
                }
            }
        }
 private static void SimulateFailure(RetryPolicy retryPolicy)
 {
     try
     {
         // Action #2
         retryPolicy.ExecuteAction(() =>
         {
             throw new TimeoutException("Forced Exception");
         });
     }
     catch (Exception ex)
     {
         // By now, we exhausted all retry attempts when establishing a connection. This means we should also
         // stop retrying the SQL command. Should we not do that, we will continue retrying substantially
         // increasing the command execution time. The strategy here is that we wrap the transient error into an
         // RetryLimitExceededException and re-throw. This will ensure that the command will not be retried.
         throw new RetryLimitExceededException(ex);
     }
 }
        /// <summary>
        /// Requests ACS token from the server and stores it for future use.
        /// </summary>
        public void RefreshToken()
        {
            using (WebClient client = new WebClient())
            {
                client.BaseAddress = this.AcsBaseAddress;

                var oauthRequestValues = new NameValueCollection
                {
                    {"grant_type", GrantType},
                    {"client_id", this.ClientId},
                    {"client_secret", this.ClientSecret},
                    {"scope", this.Scope},
                };

                RetryPolicy retryPolicy = new RetryPolicy(new WebRequestTransientErrorDetectionStrategy(), RetryStrategyFactory.DefaultStrategy());

                retryPolicy.ExecuteAction(
                    () =>
                    {
                        byte[] responseBytes = client.UploadValues("/v2/OAuth2-13", "POST", oauthRequestValues);
                        SetAcsToken(responseBytes);
                    });
            }
        }
 /// <summary>
 /// Kills all precess by names with retries. Ignores exceptions.
 /// </summary>
 /// <param name="processNames"></param>
 public void Kill(params string[] processNames)
 {
     Contract.Requires(processNames != null);
     var retry = new RetryPolicy(RetryPolicy.DefaultFixed.ErrorDetectionStrategy, 3, TimeSpan.FromSeconds(10));
     retry.ExecuteAction(() => KillInternal(processNames));
 }
        protected override void RunDatabaseLoad(DoWorkEventArgs e)
        {
            try
            {
                // Create the Retry Policy
                var retryPolicy = new RetryPolicy<ErrorDetectionStrategy>(BackoffStrategy);

                retryPolicy.ExecuteAction(() =>
                {
                    do
                    {
                        if (Worker.CancellationPending)
                        {
                            e.Cancel = true;
                            break;
                        }

                        // Load the Database & Sleep for x amount of time
                        if (CanLoadDatabase)
                        {
                            LoadDatabase(retryPolicy, _data);
                            Sleep();

                            // Switch Databases
                            IsLoadingPrimaryDatabase = !IsLoadingPrimaryDatabase;
                        }
                        else
                        {
                            Thread.Sleep(500);
                        }
                    }
                    while (TotalElapsedSeconds < ConfigHelper.Runtime);
                });
            }
            catch (Exception ex)
            {
                e.Result = ex.Message;
            }
        }
        internal static void SafeMessagingActionAsync(Action<AsyncCallback> begin, Action<IAsyncResult> end, BrokeredMessage message, Action<bool> callback, string actionErrorDescription, string messageId, string subscription, long processingElapsedMilliseconds, long schedulingElapsedMilliseconds, Stopwatch roundtripStopwatch)
        {
            var retryPolicy = new RetryPolicy<ServiceBusTransientErrorDetectionStrategy>(retryStrategy);
            retryPolicy.Retrying +=
                (s, e) =>
                {
                    Trace.TraceWarning("An error occurred in attempt number {1} to release message {3} in subscription {2}: {0}",
                    e.LastException.GetType().Name + " - " + e.LastException.Message,
                    e.CurrentRetryCount,
                    subscription,
                    message.MessageId);
                };

            long messagingActionStart = 0;

            retryPolicy.ExecuteAction(
                ac => { messagingActionStart = roundtripStopwatch.ElapsedMilliseconds; begin(ac); },
                end,
                () =>
                {
                    roundtripStopwatch.Stop();
                    callback(true);
                },
                e =>
                {
                    roundtripStopwatch.Stop();

                    if (e is MessageLockLostException || e is MessagingException || e is TimeoutException)
                    {
                        Trace.TraceWarning(actionErrorDescription, messageId, subscription, e.GetType().Name + " - " + e.Message, processingElapsedMilliseconds, schedulingElapsedMilliseconds, messagingActionStart, roundtripStopwatch.ElapsedMilliseconds);
                    }
                    else
                    {
                        Trace.TraceError("Unexpected error releasing message in subscription {1}:\r\n{0}", e, subscription);
                    }

                    callback(false);
                });
        }
示例#56
0
文件: Program.cs 项目: JackBao/MyLab
        private static void SpecifyRetryStrategiesInCode()
        {
            // Define your retry strategy: retry 5 times, starting 1 second apart
            // and adding 2 seconds to the interval each retry.
            var retryStrategy = new Incremental(5, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2));

            // Define your retry policy using the retry strategy and the Windows Azure storage
            // transient fault detection strategy.
            var retryPolicy = new RetryPolicy<StorageTransientErrorDetectionStrategy>(retryStrategy);

            // Receive notifications about retries.
            retryPolicy.Retrying += (sender, args) =>
            {
                // Log details of the retry.
                var msg = String.Format("Retry - Count:{0}, Delay:{1}, Exception:{2}",
                    args.CurrentRetryCount, args.Delay, args.LastException);
                Trace.WriteLine(msg, "Information");
            };

            try
            {
                var queue = Queue.GetQueueReference("orders");

                // Do some work that may result in a transient fault.
                retryPolicy.ExecuteAction(
                  () =>
                  {
                      // Call a method that uses Windows Azure storage and which may
                      // throw a transient exception.
                      queue.CreateIfNotExists();
                  });
            }
            catch (Exception)
            {
                // All the retries failed.
            }
        }
        // GET api/session
        public IEnumerable<Session> Get(ODataQueryOptions<Session> queryOptions)
        {

            var retryStrategy = new Incremental(5, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2));
            var retryPolicy = new RetryPolicy<SqlAzureTransientErrorDetectionStrategy>(retryStrategy);

            List<Session> session = new List<Session>();
            try
            {
                using (VidPackEntities db = new VidPackEntities())
                {
                    retryPolicy.ExecuteAction(() =>
                    {
                        db.Database.Connection.Open();
                    });


                    var listSession = retryPolicy.ExecuteAction(() =>
                        db.ExistingSession.Include("DownloadItem").Select(item => new
                        {
                            SessionDate = item.SessionDate,
                            SessionDescription = item.SessionDescription,
                            SessionSubTitle = item.SessionSubTitle,
                            SessionThumbnailUrl = item.SessionThumbnailUri,
                            SessionTitle = item.SessionSubTitle,
                            SessionVideoUrl = item.SessionVideoUri == null ? "" : item.SessionVideoUri,
                            Speaker = item.Speaker,
                            isActual = item.IsActualSession,
                            DownloadItem = item.DownloadItem.Select(_ => new
                            {
                                Caption = _.Caption,
                                Description = _.Description,
                                Url = _.Url,
                            })
                        }).ToList()
                    );

                    //ToString() / String.Format() is not know within projection
                    session = listSession.Select(item => new Session() {
                        SessionDate = Convert.ToString(item.SessionDate),
                        SessionDescription = item.SessionDescription,
                        SessionSubTitle = item.SessionSubTitle,
                        SessionThumbnailUrl = String.Format("{0}{1}", ThumbnailStorageUrl, item.SessionThumbnailUrl),
                        SessionTitle = item.SessionSubTitle,
                        SessionVideoUrl = item.SessionVideoUrl,
                        Speaker = item.Speaker,
                        SessionDownloadItem = item.DownloadItem.Select(_ => new DownloadItemInfo() {
                            Caption = _.Caption,
                            Description = _.Description,
                            Url = _.Url,
                        }).ToList<DownloadItemInfo>(),
                    }).ToList<Session>();

                    return session;
                }
            }
            catch (Exception ex)
            {
                //TODO: Implement Error Handling
                return new List<Session>();
            }
        }
        public void TestRetryStrategyInExtensionMethods()
        {
            int maxRetryCount = 5;
            RetryPolicy retryPolicy = new RetryPolicy<SqlAzureTransientErrorDetectionStrategy>(maxRetryCount, TimeSpan.FromMilliseconds(500));
            int callbackCount = 0;

            retryPolicy.Retrying += (sender, args) =>
            {
                callbackCount++;

                Trace.WriteLine(String.Format("Current Retry Count: {0}", args.CurrentRetryCount));
                Trace.WriteLine(String.Format("Last Exception: {0}", args.LastException.Message));
                Trace.WriteLine(String.Format("Delay (ms): {0}", args.Delay.TotalMilliseconds));
            };

            try
            {
                // Action #1
                retryPolicy.ExecuteAction(() =>
                {
                    SimulateFailure(retryPolicy);
                });
            }
            catch (TimeoutException ex)
            {
                Assert.AreEqual("Forced Exception", ex.Message);
            }

            // NO LONGER VALID STRATEGY (initial implementation)
            // We expect to call the SimulateFailure method (maxRetryCount + 1) number of times (first pass + maxRetryCount attempts).
            // Each time we call SimulateFailure, we are going to retry for maxRetryCount times. The callback method is shared, hence it will be invoked
            // whenever we retry inside SimulateFailure as well as outside. The outer retry will impose extra hits on the callback method.
            // Therefore we should account for these extra calls and add further maxRetryCount number of attempts. Hence, the formula below.
            // int expectedRetryCount = (maxRetryCount + 1) * maxRetryCount + maxRetryCount;

            int expectedRetryCount = maxRetryCount;

            Assert.AreEqual<int>(expectedRetryCount, callbackCount, "The action was not retried using the expected amount of times");
        }
        public void TestRetryCallback()
        {
            RetryPolicy retryPolicy = new RetryPolicy<SqlAzureTransientErrorDetectionStrategy>(RetryStrategy.DefaultClientRetryCount);
            int callbackCount = 0;

            retryPolicy.Retrying += (sender, args) =>
            {
                callbackCount++;

                Trace.WriteLine(String.Format("Current Retry Count: {0}", args.CurrentRetryCount));
                Trace.WriteLine(String.Format("Last Exception: {0}", args.LastException.Message));
                Trace.WriteLine(String.Format("Delay (ms): {0}", args.Delay.TotalMilliseconds));
            };

            try
            {
                retryPolicy.ExecuteAction(() =>
                {
                    throw new TimeoutException("Forced Exception");
                });
            }
            catch (TimeoutException ex)
            {
                Assert.AreEqual("Forced Exception", ex.Message);
            }

            Assert.AreEqual<int>(RetryStrategy.DefaultClientRetryCount, callbackCount, "The callback has not been made using the expected amount of times");
        }
示例#60
0
        private static void RetryPolityUsingCode(IUnityContainer container, IBlockService service)
        {
            System.Diagnostics.Trace.WriteLine("Begin sample: RetryPolityUsingCode");
            // Define your retry strategy: retry 5 times, starting 1 second apart
            // and adding 2 seconds to the interval each retry.
            var retryStrategy = new Incremental(5, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2));

            // Define your retry policy using the retry strategy and the Windows Azure storage
            // transient fault detection strategy.
            var retryPolicy = new RetryPolicy<BlockServiceExceptionDetectionStrategy>(retryStrategy);

            // Do some work that may result in a transient fault.
            System.Threading.Tasks.Parallel.For(0, 100, index =>
            {
                try
                {
                    retryPolicy.Retrying += OnRetryPolicyRetrying;
                    retryPolicy.ExecuteAction(() =>
                    {
                        _blockService.PutBlock(index.ToString(), index);
                    });
                    retryPolicy.Retrying -= OnRetryPolicyRetrying;
                }
                catch (Exception exception)
                {
                    // All the retries failed.
                    System.Diagnostics.Trace.WriteLine(string.Format("An Exception has been thrown:\n{0}", exception));
                }
            });

            System.Diagnostics.Trace.WriteLine("End sample: RetryPolityUsingCode");
        }