Example #1
0
        // Helper method to retrieve retrieve the CloudBlobClient object in order to interact with the storage account
        // The method reads an environment variable that is used to store the connection string to the storage account.
        // The retry policy on the CloudBlobClient object is set to an Exponential retry policy with a back off of 2 seconds
        // and a max attempts of 10 times.
        public static CloudBlobClient GetCloudBlobClient()
        {
            // Load the connection string for use with the application. The storage connection string is stored
            // in an environment variable on the machine running the application called storageconnectionstring.
            // If the environment variable is created after the application is launched in a console or with Visual
            // studio the shell needs to be closed and reloaded to take the environment variable into account.
            string storageConnectionString = Environment.GetEnvironmentVariable("storageconnectionstring");

            if (storageConnectionString == null)
            {
                Console.WriteLine(
                    "A connection string has not been defined in the system environment variables. " +
                    "Add a environment variable name 'storageconnectionstring' with the actual storage " +
                    "connection string as a value.");
            }
            try
            {
                CloudStorageAccount storageAccount         = CloudStorageAccount.Parse(storageConnectionString);
                CloudBlobClient     blobClient             = storageAccount.CreateCloudBlobClient();
                IRetryPolicy        exponentialRetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(2), 10);
                blobClient.DefaultRequestOptions.RetryPolicy = exponentialRetryPolicy;
                return(blobClient);
            }
            catch (StorageException ex)
            {
                Console.WriteLine("Error returned from the service: {0}", ex.Message);
                throw;
            }
        }
        private string DownloadSamplePackage(PackageId packageId, NuGetVersion version,
                                             NuGetPackageDownloader nuGetPackageDownloader)
        {
            return(ExponentialRetry.ExecuteWithRetry(
                       DownloadMostRecentSamplePackageFromPublicFeed,
                       result => result != null,
                       3,
                       () => ExponentialRetry.Timer(ExponentialRetry.Intervals),
                       "Run command while retry transient restore error")
                   .ConfigureAwait(false).GetAwaiter().GetResult());

            string DownloadMostRecentSamplePackageFromPublicFeed()
            {
                try
                {
                    return(nuGetPackageDownloader.DownloadPackageAsync(
                               packageId, version, includePreview: true,
                               packageSourceLocation: new PackageSourceLocation(
                                   sourceFeedOverrides: new[] { "https://api.nuget.org/v3/index.json" })).GetAwaiter()
                           .GetResult());
                }
                catch (Exception)
                {
                    return(null);
                }
            }
        }
        public void CreateConsumerInvokesTheTransportClient()
        {
            var transportClient = new ObservableTransportClientMock();
            var client          = new InjectableTransportClientMock(transportClient, "Endpoint=sb://not-real.servicebus.windows.net/;SharedAccessKeyName=DummyKey;SharedAccessKey=[not_real];EntityPath=fake");
            var expectedOptions = new EventHubConsumerOptions {
                Retry = Retry.Default
            };
            var expectedPosition      = EventPosition.FromOffset(65);
            var expectedPartition     = "2123";
            var expectedConsumerGroup = EventHubConsumer.DefaultConsumerGroup;

            client.CreateConsumer(expectedConsumerGroup, expectedPartition, expectedPosition, expectedOptions);
            (var actualConsumerGroup, var actualPartition, var actualPosition, var actualOptions) = transportClient.CreateConsumerCalledWith;

            Assert.That(actualPartition, Is.EqualTo(expectedPartition), "The partition should have been passed.");
            Assert.That(actualConsumerGroup, Is.EqualTo(expectedConsumerGroup), "The consumer groups should match.");
            Assert.That(actualPosition.Offset, Is.EqualTo(expectedPosition.Offset), "The event position to receive should match.");
            Assert.That(actualOptions, Is.Not.Null, "The consumer options should have been set.");
            Assert.That(actualPosition.Offset, Is.EqualTo(expectedPosition.Offset), "The event position to receive should match.");
            Assert.That(actualOptions.OwnerLevel, Is.EqualTo(expectedOptions.OwnerLevel), "The owner levels should match.");
            Assert.That(actualOptions.Identifier, Is.EqualTo(expectedOptions.Identifier), "The identifiers should match.");
            Assert.That(actualOptions.PrefetchCount, Is.EqualTo(expectedOptions.PrefetchCount), "The prefetch counts should match.");
            Assert.That(ExponentialRetry.HaveSameConfiguration((ExponentialRetry)actualOptions.Retry, (ExponentialRetry)expectedOptions.Retry), "The retries should match.");
            Assert.That(actualOptions.MaximumReceiveWaitTimeOrDefault, Is.EqualTo(expectedOptions.MaximumReceiveWaitTimeOrDefault), "The wait times should match.");
        }
Example #4
0
        /// <summary>
        /// Saves the document's content to the given file path.
        /// </summary>
        public void Save(string path)
        {
            //On Windows:
            // Readers will prevent the file from being overwritten due to FileShare.Read.
            // Readers can read in parallel, but when there's contention with a writer, the retrying will kick in to resolve it.
            //On Unix:
            // FileShare.Read does nothing, but...
            // File.Replace is implemented with rename system call that will mean that even though writers can overwrite while
            // reading is happening, each reader will see file before or after overwrite, not in between

            EnsureContent();
            var tempPath = Path.Combine(Path.GetDirectoryName(path), Path.GetRandomFileName());

            using (var stream = File.Open(tempPath, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
            {
                Save(stream);
            }

            ExponentialRetry.ExecuteWithRetryOnIOException(() =>
            {
                if (File.Exists(path))
                {
                    File.Replace(
                        sourceFileName: tempPath,
                        destinationFileName: path,
                        destinationBackupFileName: null,
                        ignoreMetadataErrors: true);
                }
                else
                {
                    File.Move(sourceFileName: tempPath, destFileName: path);
                }
            }, maxRetryCount: 3);
        }
Example #5
0
        internal static MemoryStream GetAssetFromIntermediaryStorage(string containerName, string fileName)
        {
            //Bitmap sourceBitmap;

            CloudBlobClient blobClient = Sahara.Core.Settings.Azure.Storage.StorageConnections.IntermediateStorage.CreateCloudBlobClient();

            //Create and set retry policy
            IRetryPolicy exponentialRetryPolicy = new ExponentialRetry(TimeSpan.FromMilliseconds(500), 8);

            blobClient.DefaultRequestOptions.RetryPolicy = exponentialRetryPolicy;


            //Creat/Connect to the Blob Container
            blobClient.GetContainerReference(containerName).CreateIfNotExists(BlobContainerPublicAccessType.Blob); //<-- Create and make public
            CloudBlobContainer blobContainer = blobClient.GetContainerReference(containerName);

            //Get reference to the picture blob or create if not exists.
            CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(fileName);

            //using(var ms = new MemoryStream())
            //{
            var ms = new MemoryStream();

            blockBlob.DownloadToStream(ms);

            return(ms);

            //sourceBitmap = new Bitmap(ms);
            //}

            //return sourceBitmap;
        }
Example #6
0
        public void CreateEventSenderCreatesDefaultWhenNoOptionsArePassed()
        {
            var clientOptions = new EventHubClientOptions
            {
                Retry          = new ExponentialRetry(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(3), 5),
                DefaultTimeout = TimeSpan.FromHours(24)
            };

            var expected = new SenderOptions
            {
                Retry   = clientOptions.Retry,
                Timeout = clientOptions.DefaultTimeout
            };

            var actual           = default(SenderOptions);
            var connectionString = "Endpoint=value.com;SharedAccessKeyName=[value];SharedAccessKey=[value];EntityPath=[value]";

            var mockClient = new Mock <EventHubClient>(connectionString, clientOptions)
            {
                CallBase = true
            };

            mockClient
            .Protected()
            .Setup <EventSender>("BuildEventSender", ItExpr.IsAny <ConnectionType>(), ItExpr.IsAny <string>(), ItExpr.IsAny <SenderOptions>())
            .Returns(Mock.Of <EventSender>())
            .Callback <ConnectionType, string, SenderOptions>((type, path, options) => actual = options);

            mockClient.Object.CreateSender();

            Assert.That(actual, Is.Not.Null, "The sender options should have been set.");
            Assert.That(actual.PartitionId, Is.EqualTo(expected.PartitionId), "The partition identifiers should match.");
            Assert.That(ExponentialRetry.HaveSameConfiguration((ExponentialRetry)actual.Retry, (ExponentialRetry)expected.Retry), "The retries should match.");
            Assert.That(actual.TimeoutOrDefault, Is.EqualTo(expected.TimeoutOrDefault), "The timeouts should match.");
        }
Example #7
0
        private void BackupFile(CloudPageBlob fileInfo, ExponentialRetry retryPolicy)
        {
            var backupBlobName = fileInfo.Name + ".bak";
            var backupBlob     = this._container.GetPageBlobReference(backupBlobName);

            backupBlob.StartCopyFromBlob(fileInfo, options: new BlobRequestOptions {
                RetryPolicy = retryPolicy
            });

            while (true)              // wait for copy operation to complete
            {
                backupBlob.FetchAttributes();
                switch (backupBlob.CopyState.Status)
                {
                case CopyStatus.Pending:
                    Thread.Sleep(500);
                    break;

                case CopyStatus.Success:
                    return;

                case CopyStatus.Aborted:
                    this.BackupFile(fileInfo, retryPolicy);                               // try again
                    return;

                case CopyStatus.Invalid:
                case CopyStatus.Failed:
                    throw new InvalidOperationException(string.Format("Failed to backup {0} to {1}. Status: {2}. Description: {3}",
                                                                      fileInfo.Name, backupBlobName, backupBlob.CopyState.Status, backupBlob.CopyState.StatusDescription));

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
        }
Example #8
0
        private string DownloadSamplePackage(PackageId packageId)
        {
            NuGetPackageDownloader nuGetPackageDownloader = new NuGetPackageDownloader(_tempDirectory, null,
                                                                                       new MockFirstPartyNuGetPackageSigningVerifier(),
                                                                                       _logger, restoreActionConfig: new RestoreActionConfig(NoCache: true));

            return(ExponentialRetry.ExecuteWithRetry <string>(
                       action: DownloadMostRecentSamplePackageFromPublicFeed,
                       shouldStopRetry: result => result != null,
                       maxRetryCount: 3,
                       timer: () => ExponentialRetry.Timer(ExponentialRetry.Intervals),
                       taskDescription: "Run command while retry transient restore error")
                   .ConfigureAwait(false).GetAwaiter().GetResult());

            string DownloadMostRecentSamplePackageFromPublicFeed()
            {
                try
                {
                    return(nuGetPackageDownloader.DownloadPackageAsync(
                               new PackageId("Microsoft.iOS.Ref"), null, includePreview: true,
                               packageSourceLocation: new PackageSourceLocation(
                                   sourceFeedOverrides: new[] { "https://api.nuget.org/v3/index.json" })).GetAwaiter()
                           .GetResult());
                }
                catch (Exception)
                {
                    return(null);
                }
            }
        }
Example #9
0
        public void CloneProducesACopy()
        {
            var options = new ReceiverOptions
            {
                ConsumerGroup             = "custom$consumer",
                BeginReceivingAt          = EventPosition.FromOffset(65),
                ExclusiveReceiverPriority = 99,
                Retry = new ExponentialRetry(TimeSpan.FromSeconds(4), TimeSpan.FromSeconds(5), 6),
                DefaultMaximumReceiveWaitTime = TimeSpan.FromMinutes(65),
                Identifier = "an_event_receiver"
            };

            var clone = options.Clone();

            Assert.That(clone, Is.Not.Null, "The clone should not be null.");

            Assert.That(clone.ConsumerGroup, Is.EqualTo(options.ConsumerGroup), "The consumer group of the clone should match.");
            Assert.That(clone.BeginReceivingAt, Is.EqualTo(options.BeginReceivingAt), "The position to begin reading events of the clone should match.");
            Assert.That(clone.ExclusiveReceiverPriority, Is.EqualTo(options.ExclusiveReceiverPriority), "The exclusive priority of the clone should match.");
            Assert.That(clone.DefaultMaximumReceiveWaitTime, Is.EqualTo(options.DefaultMaximumReceiveWaitTime), "The default maximum wait time of the clone should match.");
            Assert.That(clone.Identifier, Is.EqualTo(options.Identifier), "The identifier of the clone should match.");

            Assert.That(ExponentialRetry.HaveSameConfiguration((ExponentialRetry)clone.Retry, (ExponentialRetry)options.Retry), "The retry of the clone should be considered equal.");
            Assert.That(clone.Retry, Is.Not.SameAs(options.Retry), "The retry of the clone should be a copy, not the same instance.");
        }
        public void CreateProducerCreatesDefaultWhenOptionsAreNotSet()
        {
            var clientOptions = new EventHubClientOptions
            {
                Retry          = new ExponentialRetry(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(3), 5),
                DefaultTimeout = TimeSpan.FromHours(24)
            };

            var producerOptions = new EventHubProducerOptions
            {
                PartitionId = "123",
                Retry       = null,
                Timeout     = TimeSpan.Zero
            };

            var expected = new EventHubProducerOptions
            {
                PartitionId = producerOptions.PartitionId,
                Retry       = clientOptions.Retry,
                Timeout     = clientOptions.DefaultTimeout
            };

            var connectionString = "Endpoint=value.com;SharedAccessKeyName=[value];SharedAccessKey=[value];EntityPath=[value]";
            var mockClient       = new ReadableOptionsMock(connectionString, clientOptions);

            mockClient.CreateProducer(producerOptions);

            Assert.That(mockClient.ProducerOptions, Is.Not.Null, "The producer options should have been set.");
            Assert.That(mockClient.ProducerOptions, Is.Not.SameAs(producerOptions), "The options should have been cloned.");
            Assert.That(mockClient.ProducerOptions.PartitionId, Is.EqualTo(expected.PartitionId), "The partition identifiers should match.");
            Assert.That(ExponentialRetry.HaveSameConfiguration((ExponentialRetry)mockClient.ProducerOptions.Retry, (ExponentialRetry)expected.Retry), "The retries should match.");
            Assert.That(mockClient.ProducerOptions.TimeoutOrDefault, Is.EqualTo(expected.TimeoutOrDefault), "The timeouts should match.");
        }
Example #11
0
        private static void ClearPlatformStorageAccounts()
        {
            //var response = new DataAccessResponseType { isSuccess = false };

            //Loop through all tables named by schema for this account and delete
            CloudTableClient cloudTableClient = Sahara.Core.Settings.Azure.Storage.StorageConnections.PlatformStorage.CreateCloudTableClient();

            //Create and set retry policy
            IRetryPolicy exponentialRetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(1), 16);

            //IRetryPolicy linearRetryPolicy = new LinearRetry(TimeSpan.FromSeconds(1), 3);
            cloudTableClient.DefaultRequestOptions.RetryPolicy = exponentialRetryPolicy;

            IEnumerable <CloudTable> tables = cloudTableClient.ListTables();

            foreach (CloudTable table in tables)
            {
                try
                {
                    table.Delete();
                }
                catch
                {
                    //response.isSuccess = false;
                }
            }
        }
Example #12
0
        public async Task BatchRequestRetryPolicyNoExceptionNoRetries()
        {
            TimeSpan  deltaBackoff = TimeSpan.FromSeconds(0);
            const int maxRetries   = 5;
            int       serviceRequestFuncCallCount = 0;

            var request = new BatchRequest <
                JobScheduleListOptions,
                AzureOperationResponse <IPage <ProxyModels.CloudJobSchedule> > >(null, CancellationToken.None);

            request.ServiceRequestFunc = (token) =>
            {
                ++serviceRequestFuncCallCount;
                return(Task.FromResult(default(AzureOperationResponse <IPage <ProxyModels.CloudJobSchedule> >)));
            };

            IRetryPolicy policy = new ExponentialRetry(deltaBackoff, maxRetries);

            request.RetryPolicy = policy;

            AzureOperationResponse <IPage <ProxyModels.CloudJobSchedule> > result = await request.ExecuteRequestAsync();

            Assert.Null(result);
            Assert.Equal(1, serviceRequestFuncCallCount);
        }
Example #13
0
        protected void Application_Start()
        {
            GlobalConfiguration.Configure(WebApiConfig.Register);

            try
            {
                /// On start up, CreateIfNotExists CloudBreadAdminLog table on Azure Table Storage
                /// On start up, CreateIfNotExists messagestoadminlog table on Azure Queue Service
                if (globalVal.StorageConnectionString != "")
                {
                    /// this table is used for CloudBread admin log saving
                    /// Azure Storage connection retry policy
                    var retryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(2), 10);
                    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(globalVal.StorageConnectionString);
                    CloudTableClient    tableClient    = storageAccount.CreateCloudTableClient();
                    tableClient.DefaultRequestOptions.RetryPolicy = retryPolicy;
                    var cloudTable = tableClient.GetTableReference("CloudBreadAdminLog");
                    cloudTable.CreateIfNotExists();
                    cloudTable = tableClient.GetTableReference("CloudBreadAdminErrorLog");
                    cloudTable.CreateIfNotExists();

                    /// this queue is used for CloudBread queue method admin log saving
                    CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
                    queueClient.DefaultRequestOptions.RetryPolicy = retryPolicy;
                    CloudQueue queue = queueClient.GetQueueReference("messagestoadminlog");      /// must be lowercase
                    queue.CreateIfNotExists();
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
Example #14
0
        public async Task ExponentialRetryAbortsRetriesAfterMaxRetryCount()
        {
            const int        delayInSeconds   = 3;
            TimeSpan         interval         = TimeSpan.FromSeconds(delayInSeconds);
            const int        maxRetries       = 5;
            ExponentialRetry exponentialRetry = new ExponentialRetry(interval, maxRetries);

            TimeoutException timeoutException = new TimeoutException();

            OperationContext context       = new OperationContext();
            RetryDecision    retryDecision = null;
            int requestCount = 0;

            while (retryDecision == null || retryDecision.ShouldRetry)
            {
                context.RequestResults.Add(new RequestResult(new RequestInformation(), timeoutException));
                retryDecision = await exponentialRetry.ShouldRetryAsync(timeoutException, context);

                Assert.NotNull(retryDecision);

                if (retryDecision.ShouldRetry)
                {
                    TimeSpan expectedDelay = TimeSpan.FromSeconds(Math.Pow(2, requestCount) * delayInSeconds);
                    Assert.Equal(expectedDelay, retryDecision.RetryDelay);
                }

                ++requestCount;
            }

            Assert.Equal(maxRetries, requestCount - 1); //request count is retry count + 1
        }
        public void CreateConsumerCreatesDefaultWhenNoOptionsArePassed()
        {
            var clientOptions = new EventHubClientOptions
            {
                Retry          = new ExponentialRetry(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(3), 5),
                DefaultTimeout = TimeSpan.FromHours(24)
            };

            var expectedOptions = new EventHubConsumerOptions
            {
                Retry = clientOptions.Retry,
                DefaultMaximumReceiveWaitTime = clientOptions.DefaultTimeout
            };

            var expectedConsumerGroup = EventHubConsumer.DefaultConsumerGroup;
            var expectedPartition     = "56767";
            var expectedPosition      = EventPosition.FromEnqueuedTime(DateTime.Parse("2015-10-27T12:00:00Z"));
            var connectionString      = "Endpoint=value.com;SharedAccessKeyName=[value];SharedAccessKey=[value];EntityPath=[value]";
            var mockClient            = new ReadableOptionsMock(connectionString, clientOptions);

            mockClient.CreateConsumer(expectedConsumerGroup, expectedPartition, expectedPosition);
            var actualOptions = mockClient.ConsumerOptions;

            Assert.That(actualOptions, Is.Not.Null, "The consumer options should have been set.");
            Assert.That(actualOptions.OwnerLevel, Is.EqualTo(expectedOptions.OwnerLevel), "The owner levels should match.");
            Assert.That(actualOptions.Identifier, Is.EqualTo(expectedOptions.Identifier), "The identifiers should match.");
            Assert.That(actualOptions.PrefetchCount, Is.EqualTo(expectedOptions.PrefetchCount), "The prefetch counts should match.");
            Assert.That(ExponentialRetry.HaveSameConfiguration((ExponentialRetry)actualOptions.Retry, (ExponentialRetry)expectedOptions.Retry), "The retries should match.");
            Assert.That(actualOptions.MaximumReceiveWaitTimeOrDefault, Is.EqualTo(expectedOptions.MaximumReceiveWaitTimeOrDefault), "The wait times should match.");
        }
Example #16
0
        public static DataAccessResponseType DeleteImageBlobs(string storagePartition, string containerName, string blobPath)
        {
            //CloudBlobClient blobClient = Sahara.Core.Settings.Azure.Storage.StorageConnections.AccountsStorage.CreateCloudBlobClient();
            CloudBlobClient blobClient = Settings.Azure.Storage.GetStoragePartitionAccount(storagePartition).CreateCloudBlobClient();

            //Create and set retry policy
            IRetryPolicy exponentialRetryPolicy = new ExponentialRetry(TimeSpan.FromMilliseconds(400), 6);

            blobClient.DefaultRequestOptions.RetryPolicy = exponentialRetryPolicy;

            //Creat/Connect to the Blob Container
            blobClient.GetContainerReference(containerName).CreateIfNotExists(BlobContainerPublicAccessType.Blob); //<-- Create and make public
            CloudBlobContainer blobContainer = blobClient.GetContainerReference(containerName);

            var blockBlobMainUri = blobPath;
            var blockBlobSMUri   = blobPath.Replace(".jpg", "_sm.jpg").Replace(".gif", "_sm.gif").Replace(".png", "_sm.png");
            var blockBlobXSUri   = blobPath.Replace(".jpg", "_xs.jpg").Replace(".gif", "_xs.gif").Replace(".png", "_xs.png");

            //Get references to the blobs.
            CloudBlockBlob blockBlobMain = blobContainer.GetBlockBlobReference(blockBlobMainUri);
            CloudBlockBlob blockBlobSM   = blobContainer.GetBlockBlobReference(blockBlobSMUri);
            CloudBlockBlob blockBlobXS   = blobContainer.GetBlockBlobReference(blockBlobXSUri);

            try { blockBlobMain.Delete(); } catch { }
            try { blockBlobSM.Delete(); } catch { }
            try { blockBlobXS.Delete(); } catch { }

            return(new DataAccessResponseType {
                isSuccess = true
            });
        }
        public void CreateConsumerCreatesDefaultWhenOptionsAreNotSet()
        {
            var clientOptions = new EventHubClientOptions
            {
                Retry          = new ExponentialRetry(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(3), 5),
                DefaultTimeout = TimeSpan.FromHours(24)
            };

            var expectedOptions = new EventHubConsumerOptions
            {
                OwnerLevel    = 251,
                Identifier    = "Bob",
                PrefetchCount = 600,
                Retry         = clientOptions.Retry,
                DefaultMaximumReceiveWaitTime = clientOptions.DefaultTimeout
            };

            var expectedConsumerGroup = "SomeGroup";
            var expectedPartition     = "56767";
            var expectedPosition      = EventPosition.FromSequenceNumber(123);
            var connectionString      = "Endpoint=value.com;SharedAccessKeyName=[value];SharedAccessKey=[value];EntityPath=[value]";
            var mockClient            = new ReadableOptionsMock(connectionString, clientOptions);

            mockClient.CreateConsumer(expectedConsumerGroup, expectedPartition, expectedPosition, expectedOptions);
            var actualOptions = mockClient.ConsumerOptions;

            Assert.That(actualOptions, Is.Not.Null, "The consumer options should have been set.");
            Assert.That(actualOptions, Is.Not.SameAs(expectedOptions), "A clone of the options should have been made.");
            Assert.That(actualOptions.OwnerLevel, Is.EqualTo(expectedOptions.OwnerLevel), "The owner levels should match.");
            Assert.That(actualOptions.Identifier, Is.EqualTo(expectedOptions.Identifier), "The identifiers should match.");
            Assert.That(actualOptions.PrefetchCount, Is.EqualTo(expectedOptions.PrefetchCount), "The prefetch counts should match.");
            Assert.That(ExponentialRetry.HaveSameConfiguration((ExponentialRetry)actualOptions.Retry, (ExponentialRetry)expectedOptions.Retry), "The retries should match.");
            Assert.That(actualOptions.MaximumReceiveWaitTimeOrDefault, Is.EqualTo(expectedOptions.MaximumReceiveWaitTimeOrDefault), "The wait times should match.");
        }
Example #18
0
        public AzureStorageConfigurationBuilder(CloudStorageAccount account)
        {
            // defaults
            // Retry up to 450 times with max 2 seconds delay between each retry (so wait up to 15 mins)
            var retry = new ExponentialRetry(TimeSpan.FromSeconds(0.2), 15);

            this._queueConfig = client =>
            {
                client.RetryPolicy          = retry;
                client.MaximumExecutionTime = TimeSpan.FromMinutes(15);
                client.ServerTimeout        = TimeSpan.FromMinutes(15);
            };
            this._blobConfig = client =>
            {
                client.RetryPolicy          = retry;
                client.MaximumExecutionTime = TimeSpan.FromMinutes(15);
                client.ServerTimeout        = TimeSpan.FromMinutes(15);
            };
            this._tableConfig = client => client.RetryPolicy = retry;

            if (account.Credentials.AccountName == "devstoreaccount1")
            {
                this._blobConfig += client =>
                {
                    // http://stackoverflow.com/questions/4897826/
                    // local dev store works poorly with multi-thread uploads
                    client.ParallelOperationThreadCount = 1;
                };
            }

            this._account   = account;
            this._accountId = account.Credentials.AccountName;
        }
        public async Task <string> GetTokenForInstallation(long installationId)
        {
            if (TryGetCachedToken(installationId, out AccessToken cachedToken))
            {
                _logger.LogInformation($"Cached token obtained for GitHub installation {installationId}. Expires at {cachedToken.ExpiresAt}.");
                return(cachedToken.Token);
            }

            return(await ExponentialRetry.RetryAsync(
                       async() =>
            {
                string jwt = GetAppToken();
                var product = new ProductHeaderValue(Options.ApplicationName, Options.ApplicationVersion);
                var appClient = new Octokit.GitHubClient(product)
                {
                    Credentials = new Credentials(jwt, AuthenticationType.Bearer)
                };
                AccessToken token = await appClient.GitHubApps.CreateInstallationToken(installationId);
                _logger.LogInformation($"New token obtained for GitHub installation {installationId}. Expires at {token.ExpiresAt}.");
                UpdateTokenCache(installationId, token);
                return token.Token;
            },
                       ex => _logger.LogError(ex, $"Failed to get a github token for installation id {installationId}, retrying"),
                       ex => ex is ApiException && ((ApiException)ex).StatusCode == HttpStatusCode.InternalServerError));
        }
Example #20
0
        public void CreatePartitionReceiverCreatesDefaultWhenNoOptionsArePassed()
        {
            var clientOptions = new EventHubClientOptions
            {
                Retry = new ExponentialRetry(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(3), 5),
                DefaultTimeout = TimeSpan.FromHours(24)
            };

            var expectedOptions = new ReceiverOptions
            {
                Retry = clientOptions.Retry,
                DefaultMaximumReceiveWaitTime = clientOptions.DefaultTimeout

            };

            var expectedPartition = "56767";
            var connectionString = "Endpoint=value.com;SharedAccessKeyName=[value];SharedAccessKey=[value];EntityPath=[value]";
            var mockClient = new ReadableOptionsMock(connectionString, clientOptions);
            var receiver = mockClient.CreateReceiver(expectedPartition);
            var actualOptions = mockClient.ReceiverOptions;

            Assert.That(receiver.PartitionId, Is.EqualTo(expectedPartition), "The partition should match.");
            Assert.That(actualOptions, Is.Not.Null, "The receiver options should have been set.");
            Assert.That(actualOptions.BeginReceivingAt.Offset, Is.EqualTo(expectedOptions.BeginReceivingAt.Offset), "The beginning position to receive should match.");
            Assert.That(actualOptions.ConsumerGroup, Is.EqualTo(expectedOptions.ConsumerGroup), "The consumer groups should match.");
            Assert.That(actualOptions.ExclusiveReceiverPriority, Is.EqualTo(expectedOptions.ExclusiveReceiverPriority), "The exclusive priorities should match.");
            Assert.That(actualOptions.Identifier, Is.EqualTo(expectedOptions.Identifier), "The identifiers should match.");
            Assert.That(actualOptions.PrefetchCount, Is.EqualTo(expectedOptions.PrefetchCount), "The prefetch counts should match.");
            Assert.That(ExponentialRetry.HaveSameConfiguration((ExponentialRetry)actualOptions.Retry, (ExponentialRetry)expectedOptions.Retry), "The retries should match.");
            Assert.That(actualOptions.MaximumReceiveWaitTimeOrDefault, Is.EqualTo(expectedOptions.MaximumReceiveWaitTimeOrDefault), "The wait times should match.");
        }
Example #21
0
        internal static IRetryPolicy GetRetryPolicy(string policyName, TimeSpan deltaBackoff, int maxAttempts)
        {
            IRetryPolicy policy;

            switch (policyName)
            {
            case "Exponential":
                policy = new ExponentialRetry(deltaBackoff, maxAttempts);
                break;

            case "Linear":
                policy = new LinearRetry(deltaBackoff, maxAttempts);
                break;

            case "NoRetry":
                policy = new NoRetry();
                break;

            default:
                policy = new NoRetry();
                break;
            }

            return(policy);
        }
Example #22
0
        /// Timer trigger of CBProcessBPITrigger
        public static void CBProcessBPITrigger([TimerTrigger("0 5 12 * * *")] TimerInfo timer) // every day 12:05
        {
            try
            {
                Console.WriteLine("CB task timer starting at CBProcessBPITrigger");

                // send message to cloudbread-batch queue
                /// Azure Queue Storage connection retry policy
                var queueStorageRetryPolicy        = new ExponentialRetry(TimeSpan.FromSeconds(2), 10);
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["AzureWebJobsStorage"].ConnectionString);
                CloudQueueClient    queueClient    = storageAccount.CreateCloudQueueClient();
                queueClient.DefaultRequestOptions.RetryPolicy = queueStorageRetryPolicy;
                CloudQueue queue = queueClient.GetQueueReference("cloudbread-batch");

                /// send message to queue - Dormant
                CBBatchJob bj = new CBBatchJob();
                bj.JobID        = "CDBatch-BPI";
                bj.JobTitle     = "Best Purchased Item Batch";
                bj.JobTriggerDT = DateTimeOffset.UtcNow.ToString();
                bj.JobTrackID   = Guid.NewGuid().ToString();
                queue.AddMessage(new CloudQueueMessage(JsonConvert.SerializeObject(bj)));

                Console.WriteLine("CB task timer done at CBProcessBPITrigger");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #23
0
        //////////////////////////////////////////////////////////////
        /// @brief Timer trigger of testProcess. one time test on startup and every two min(set it "0 */2 * * * *") . trigger
        //////////////////////////////////////////////////////////////
        public static void testProcess([TimerTrigger("0 */5 * * * *", RunOnStartup = true)] TimerInfo timer)
        {
            try
            {
                Console.WriteLine("CB test timer starting");

                CBBatchJob bj = new CBBatchJob();
                bj.JobID        = "test";
                bj.JobTitle     = "this is developer test job";
                bj.JobTriggerDT = DateTimeOffset.UtcNow.ToString();
                bj.JobTrackID   = Guid.NewGuid().ToString();

                /// send message to cloudbread-batch queue
                /// Azure Queue Storage connection retry policy
                var queueStorageRetryPolicy        = new ExponentialRetry(TimeSpan.FromSeconds(2), 10);
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["AzureWebJobsStorage"].ConnectionString);
                CloudQueueClient    queueClient    = storageAccount.CreateCloudQueueClient();
                queueClient.DefaultRequestOptions.RetryPolicy = queueStorageRetryPolicy;
                CloudQueue queue = queueClient.GetQueueReference("cloudbread-batch");
                queue.AddMessage(new CloudQueueMessage(JsonConvert.SerializeObject(bj)));

                Console.WriteLine("CB test timer done");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #24
0
        public BlobStorageDataBus(CloudBlobContainer container, DataBusSettings settings, IAsyncTimer timer)
        {
            this.container = container;
            this.settings  = settings;
            this.timer     = timer;

            retryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(settings.BackOffInterval), settings.MaxRetries);
        }
        public void ShouldRetry_ShouldReturn_ExponentiallyIncreasingRetryInterval(uint currentRetryCount, double min, double max)
        {
            var retry = new ExponentialRetry(TimeSpan.FromSeconds(4), 3);

            retry.ShouldRetry(currentRetryCount, HttpStatusCode.RequestTimeout, out var retryInterval);

            Assert.InRange(retryInterval.TotalSeconds, min, max);
        }
Example #26
0
        public void CloneProducesACopy()
        {
            var retry = new ExponentialRetry(TimeSpan.FromSeconds(1), TimeSpan.FromMilliseconds(2), 123);
            var clone = retry.Clone() as ExponentialRetry;

            Assert.That(clone, Is.Not.Null, "The clone should not be null.");
            Assert.That(ExponentialRetry.HaveSameConfiguration(clone, retry), Is.True, "The clone should be considered equal.");
            Assert.That(clone, Is.Not.SameAs(retry), "The clone should be a copy, not the same instance.");
        }
Example #27
0
        public void ExponentialRetryPropertiesSetCorrect()
        {
            TimeSpan         interval         = TimeSpan.FromSeconds(5);
            const int        maxRetries       = 10;
            ExponentialRetry exponentialRetry = new ExponentialRetry(interval, maxRetries);

            Assert.Equal(interval, exponentialRetry.DeltaBackoff);
            Assert.Equal(maxRetries, exponentialRetry.MaximumRetries);
        }
        public void TestExponentialRetry()
        {
            IReconnectRetryPolicy exponentialRetry = new ExponentialRetry(5000);

            Assert.False(exponentialRetry.ShouldRetry(0, 0));
            Assert.True(exponentialRetry.ShouldRetry(1, 5600));
            Assert.True(exponentialRetry.ShouldRetry(2, 6050));
            Assert.False(exponentialRetry.ShouldRetry(2, 4050));
        }
Example #29
0
        public async Task ExponentialRetryDoesNotRetryOnValidationExceptions()
        {
            TimeSpan  deltaBackoff = TimeSpan.FromSeconds(1);
            const int maxRetries   = 3;

            IRetryPolicy policy = new ExponentialRetry(deltaBackoff, maxRetries);

            await this.AssertPolicyDoesNotRetryOnValidationExceptions(policy);
        }
Example #30
0
            /// <summary>
            /// Initializes a new instance of the <see cref="TransferRetryPolicy"/> class.
            /// </summary>
            /// <param name="deltaBackoff">Back-off in ExponentialRetry retry policy.</param>
            /// <param name="maxAttemptsXMsError">Max retry count when meets x-ms error.</param>
            /// <param name="maxAttemptsOtherError">Max retry count when meets non x-ms error.</param>
            public TransferRetryPolicy(TimeSpan deltaBackoff, int maxAttemptsXMsError, int maxAttemptsOtherError)
            {
                Debug.Assert(
                    maxAttemptsXMsError >= maxAttemptsOtherError,
                    "We should retry more times when meets x-ms errors than the other errors.");

                this.retryPolicy           = new ExponentialRetry(deltaBackoff, maxAttemptsXMsError);
                this.maxAttemptsOtherError = maxAttemptsOtherError;
            }