Laser table storage: track repeats

I am using a standard example from microsoft to insert new entities into a table. Is there a way to track if a retry was made?

code:

CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); CloudTable table = tableClient.GetTableReference("people"); CustomerEntity customer1 = new CustomerEntity("Harp", "Walter"); TableOperation insertOperation = TableOperation.Insert(customer1); table.Execute(insertOperation); 

With TransientFaultHandlingFramework, this was easy to do:

 var retryPol = new RetryPolicy<SqlAzureTransientErrorDetectionStrategy>(retryStrategy); retryPol.Retrying += (obj, eventArgs) => { var msg = String.Format("Retrying, CurrentRetryCount = {0} , Delay = {1}, Exception = {2}", eventArgs.CurrentRetryCount, eventArgs.Delay, eventArgs.LastException.Message); System.Diagnostics.Debug.WriteLine(msg); }; 
+4
source share
2 answers

You can use the Application Block to process Enterprise transition applications , as described in Rob's answer :

 var retryPol = new RetryPolicy<StorageTransientErrorDetectionStrategy>(retryStrategy); retryPol.Retrying += (obj, eventArgs) => { var msg = String.Format("Retrying, CurrentRetryCount = {0} , Delay = {1}, Exception = {2}", eventArgs.CurrentRetryCount, eventArgs.Delay, eventArgs.LastException.Message); System.Diagnostics.Debug.WriteLine(msg); }; var options = new TableRequestOptions { RetryPolicy = new RetryPolicies.NoRetry() }; CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); CloudTable table = tableClient.GetTableReference("people"); CustomerEntity customer1 = new CustomerEntity("Harp", "Walter"); TableOperation insertOperation = TableOperation.Insert(customer1); retryPol.ExecuteAction<TableResult>(() => { return table.Execute(insertOperation, options); }); 

If you prefer to use exclusively the Windows Azure Storage client library, you can create a custom retry policy that raises events, for example:

 public class EventExponentialRetry : IRetryPolicy { private static readonly TimeSpan DefaultClientBackoff = TimeSpan.FromSeconds(4.0); private const int DefaultClientRetryCount = 3; private TimeSpan deltaBackoff; private int maximumAttempts; private ExponentialRetry retry; public event EventHandler<RetryEventArgs> RaiseRetryEvent; public EventExponentialRetry() { Initialize(DefaultClientBackoff, DefaultClientRetryCount); } public EventExponentialRetry(TimeSpan deltaBackoff, int maxAttempts) { Initialize(deltaBackoff, maxAttempts); } private void Initialize(TimeSpan deltaBackoff, int maxAttempts) { this.deltaBackoff = deltaBackoff; this.maximumAttempts = maxAttempts; retry = new ExponentialRetry(this.deltaBackoff, this.maximumAttempts); } public IRetryPolicy CreateInstance() { EventExponentialRetry newInstance = new EventExponentialRetry(this.deltaBackoff, this.maximumAttempts); newInstance.RaiseRetryEvent = this.RaiseRetryEvent; return newInstance; } public bool ShouldRetry(int currentRetryCount, int statusCode, Exception lastException, out TimeSpan retryInterval, OperationContext operationContext) { bool shouldRetry = retry.ShouldRetry(currentRetryCount, statusCode, lastException, out retryInterval, operationContext); if (shouldRetry) { OnRaiseRetryEvent(new RetryEventArgs(currentRetryCount, statusCode, lastException, retryInterval, operationContext)); } return shouldRetry; } protected virtual void OnRaiseRetryEvent(RetryEventArgs e) { // Make a temporary copy of the event to avoid possibility of // a race condition if the last subscriber unsubscribes // immediately after the null check and before the event is raised. EventHandler<RetryEventArgs> handler = RaiseRetryEvent; // Event will be null if there are no subscribers. if (handler != null) { // Use the () operator to raise the event. handler(this, e); } } } 

For a complete example, see the CustomAzureStorageRetryPolicySample GitHub Project .

+5
source

You can use the same platform for transaction processing with enterprise transients with table storage (and the associated retry notification).

To do this, you disable the default table table overflow error handling and use enterprise data warehouse error handling instead.

 var retryPol = new RetryPolicy<StorageTransientErrorDetectionStrategy>(retryStrategy); retryPol.Retrying += (obj, eventArgs) => { var msg = String.Format("Retrying, CurrentRetryCount = {0} , Delay = {1}, Exception = {2}", eventArgs.CurrentRetryCount, eventArgs.Delay, eventArgs.LastException.Message); System.Diagnostics.Debug.WriteLine(msg); }; var options = new TableRequestOptions { RetryPolicy = new RetryPolicies.NoRetry() }; CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); CloudTable table = tableClient.GetTableReference("people"); CustomerEntity customer1 = new CustomerEntity("Harp", "Walter"); TableOperation insertOperation = TableOperation.Insert(customer1); retryPol.ExecuteAction<TableResult>(() => { return table.Execute(insertOperation, options); }); 
+1
source

Source: https://habr.com/ru/post/1489740/


All Articles