-
Notifications
You must be signed in to change notification settings - Fork 12
Adding Transient error support #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cgoconseils
wants to merge
2
commits into
seanmcne:master
Choose a base branch
from
cgoconseils:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -126,7 +126,7 @@ public IDictionary<string, TResponse> Execute<TRequest, TResponse>(IDictionary<s | |
| errorHandler) | ||
| .ToDictionary(r => r.Key, r => r.Value); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Performs data parallelism on a collection of type <see cref="DiscoveryRequest"/> to execute <see cref="IDiscoveryService"/>.Execute() requests concurrently | ||
| /// </summary> | ||
|
|
@@ -205,7 +205,7 @@ public IEnumerable<TResponse> Execute<TRequest, TResponse>(IEnumerable<TRequest> | |
| #endregion | ||
|
|
||
| #region Core Parallel Execution Method <TRequest, TResponse> | ||
|
|
||
| /// <summary> | ||
| /// Core implementation of the parallel pattern for service operations that should collect responses to each request | ||
| /// </summary> | ||
|
|
@@ -226,12 +226,12 @@ private IEnumerable<TResponse> ExecuteOperationWithResponse<TRequest, TResponse> | |
|
|
||
| // Inline method for initializing a new discovery service channel | ||
| Func<ManagedTokenDiscoveryServiceProxy> proxyInit = () => | ||
| { | ||
| var proxy = this.ServiceManager.GetProxy(); | ||
| proxy.SetProxyOptions(options); | ||
| { | ||
| var proxy = this.ServiceManager.GetProxy(); | ||
| proxy.SetProxyOptions(options); | ||
|
|
||
| return proxy; | ||
| }; | ||
| return proxy; | ||
| }; | ||
|
|
||
| using (var threadLocalProxy = new ThreadLocal<ManagedTokenDiscoveryServiceProxy>(proxyInit, true)) | ||
| { | ||
|
|
@@ -240,7 +240,7 @@ private IEnumerable<TResponse> ExecuteOperationWithResponse<TRequest, TResponse> | |
| Parallel.ForEach<TRequest, ParallelDiscoveryOperationContext<TRequest, TResponse>>(requests, | ||
| new ParallelOptions() { MaxDegreeOfParallelism = this.MaxDegreeOfParallelism }, | ||
| () => new ParallelDiscoveryOperationContext<TRequest, TResponse>(threadLocalProxy.Value), | ||
| (request, loopState, index, context) => | ||
| (request, loopState, index, context) => | ||
| { | ||
| try | ||
| { | ||
|
|
@@ -301,13 +301,19 @@ private IEnumerable<TResponse> ExecuteOperationWithResponse<TRequest, TResponse> | |
| /// </remarks> | ||
| public class ParallelOrganizationServiceProxy : ParallelServiceProxy<OrganizationServiceManager> | ||
| { | ||
| private const int RateLimitExceededErrorCode = -2147015902; | ||
| private const int TimeLimitExceededErrorCode = -2147015903; | ||
| private const int ConcurrencyLimitExceededErrorCode = -2147015898; | ||
|
|
||
| private const int MaxRetries = 3; | ||
|
|
||
| #region Constructor(s) | ||
|
|
||
| public ParallelOrganizationServiceProxy(OrganizationServiceManager serviceManager) | ||
| : base(serviceManager) { } | ||
| : base(serviceManager) { } | ||
|
|
||
| public ParallelOrganizationServiceProxy(OrganizationServiceManager serviceManager, int maxDegreeOfParallelism) | ||
| : base(serviceManager, maxDegreeOfParallelism) { } | ||
| : base(serviceManager, maxDegreeOfParallelism) { } | ||
|
|
||
| #endregion | ||
|
|
||
|
|
@@ -397,9 +403,9 @@ public IEnumerable<Entity> Create(IEnumerable<Entity> targets, OrganizationServi | |
| { | ||
| return this.ExecuteOperationWithResponse<Entity, Entity>(targets, options, | ||
| (target, context) => | ||
| { | ||
| { | ||
| target.Id = context.Local.Create(target); //Hydrate target with response Id | ||
|
|
||
| //Collect the result from each iteration in this partition | ||
| context.Results.Add(target); | ||
| }, | ||
|
|
@@ -513,7 +519,7 @@ public void Associate(IEnumerable<AssociateRequest> requests, OrganizationServic | |
| /// <param name="errorHandler">An optional error handling operation. Handler will be passed the request that failed along with the corresponding <see cref="FaultException{OrganizationServiceFault}"/></param> | ||
| /// <exception cref="AggregateException">callers should catch <see cref="AggregateException"/> to handle exceptions raised by individual requests</exception> | ||
| public void Disassociate(IEnumerable<DisassociateRequest> requests, Action<DisassociateRequest, FaultException<OrganizationServiceFault>> errorHandler = null) | ||
| { | ||
| { | ||
| this.Disassociate(requests, new OrganizationServiceProxyOptions(), errorHandler); | ||
| } | ||
|
|
||
|
|
@@ -664,7 +670,7 @@ public IDictionary<string, EntityCollection> RetrieveMultiple(IDictionary<string | |
| }, | ||
| errorHandler) | ||
| .ToDictionary(r => r.Key, r => r.Value); | ||
| } | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
|
|
@@ -836,16 +842,16 @@ private void ExecuteOperation<TRequest>(IEnumerable<TRequest> requests, Organiza | |
| Action<TRequest, ManagedTokenOrganizationServiceProxy> operation, Action<TRequest, FaultException<OrganizationServiceFault>> errorHandler) | ||
| { | ||
| var allFailures = new ConcurrentBag<ParallelOrganizationOperationFailure<TRequest>>(); | ||
|
|
||
| // Inline method for initializing a new organization service channel | ||
| Func<ManagedTokenOrganizationServiceProxy> proxyInit = () => | ||
| { | ||
| var proxy = this.ServiceManager.GetProxy(); | ||
| proxy.SetProxyOptions(options); | ||
| { | ||
| var proxy = this.ServiceManager.GetProxy(); | ||
| proxy.SetProxyOptions(options); | ||
|
|
||
| return proxy; | ||
| }; | ||
|
|
||
| return proxy; | ||
| }; | ||
|
|
||
| using (var threadLocalProxy = new ThreadLocal<ManagedTokenOrganizationServiceProxy>(proxyInit, true)) | ||
| { | ||
| try | ||
|
|
@@ -855,20 +861,22 @@ private void ExecuteOperation<TRequest>(IEnumerable<TRequest> requests, Organiza | |
| () => new ParallelOrganizationOperationContext<TRequest, bool>(), | ||
| (request, loopState, index, context) => | ||
| { | ||
| try | ||
| { | ||
| operation(request, threadLocalProxy.Value); | ||
| } | ||
| catch (FaultException<OrganizationServiceFault> fault) | ||
| var retryCount = 0; | ||
| while (true) | ||
| { | ||
| // Track faults locally | ||
| if (errorHandler != null) | ||
| try | ||
| { | ||
| context.Failures.Add(new ParallelOrganizationOperationFailure<TRequest>(request, fault)); | ||
| operation(request, threadLocalProxy.Value); | ||
| } | ||
| else | ||
| catch (FaultException<OrganizationServiceFault> e) when (IsTransientError(e) && ++retryCount < MaxRetries) | ||
| { | ||
| throw; | ||
| ApplyDelay(e, retryCount); | ||
| } | ||
| catch (FaultException<OrganizationServiceFault> fault) when (errorHandler != null) | ||
| { | ||
| // Track faults locally | ||
| context.Failures.Add(new ParallelOrganizationOperationFailure<TRequest>(request, fault)); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -891,7 +899,7 @@ private void ExecuteOperation<TRequest>(IEnumerable<TRequest> requests, Organiza | |
| { | ||
| foreach (var failure in allFailures) | ||
| { | ||
| errorHandler(failure.Request, failure.Exception); | ||
| errorHandler(failure.Request, failure.Exception); | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -917,13 +925,13 @@ private IEnumerable<TResponse> ExecuteOperationWithResponse<TRequest, TResponse> | |
|
|
||
| // Inline method for initializing a new organization service channel | ||
| Func<ManagedTokenOrganizationServiceProxy> proxyInit = () => | ||
| { | ||
| var proxy = this.ServiceManager.GetProxy(); | ||
| proxy.SetProxyOptions(options); | ||
| { | ||
| var proxy = this.ServiceManager.GetProxy(); | ||
| proxy.SetProxyOptions(options); | ||
|
|
||
| return proxy; | ||
| }; | ||
|
|
||
| return proxy; | ||
| }; | ||
|
|
||
| using (var threadLocalProxy = new ThreadLocal<ManagedTokenOrganizationServiceProxy>(proxyInit, true)) | ||
| { | ||
| try | ||
|
|
@@ -933,30 +941,32 @@ private IEnumerable<TResponse> ExecuteOperationWithResponse<TRequest, TResponse> | |
| () => new ParallelOrganizationOperationContext<TRequest, TResponse>(threadLocalProxy.Value), | ||
| (request, loopState, index, context) => | ||
| { | ||
| try | ||
| { | ||
| coreOperation(request, context); | ||
| } | ||
| catch (FaultException<OrganizationServiceFault> fault) | ||
| var retryCount = 0; | ||
| while (true) | ||
| { | ||
| // Track faults locally | ||
| if (errorHandler != null) | ||
| try | ||
| { | ||
| context.Failures.Add(new ParallelOrganizationOperationFailure<TRequest>(request, fault)); | ||
| coreOperation(request, context); | ||
| } | ||
| else | ||
| catch (FaultException<OrganizationServiceFault> e) when (IsTransientError(e) && ++retryCount < MaxRetries) | ||
| { | ||
| throw; | ||
| ApplyDelay(e, retryCount); | ||
| } | ||
| catch (FaultException<OrganizationServiceFault> fault) when (errorHandler != null) | ||
| { | ||
| // Track faults locally | ||
| context.Failures.Add(new ParallelOrganizationOperationFailure<TRequest>(request, fault)); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return context; | ||
| }, | ||
| (context) => | ||
| { | ||
| { | ||
| // Join results and faults together | ||
| Array.ForEach(context.Results.ToArray(), r => allResponses.Add(r)); | ||
| Array.ForEach(context.Failures.ToArray(), f => allFailures.Add(f)); | ||
| Array.ForEach(context.Failures.ToArray(), f => allFailures.Add(f)); | ||
|
|
||
| // Remove temporary reference to ThreadLocal proxy | ||
| context.Local = null; | ||
|
|
@@ -971,15 +981,41 @@ private IEnumerable<TResponse> ExecuteOperationWithResponse<TRequest, TResponse> | |
| // Handle faults | ||
| if (errorHandler != null) | ||
| { | ||
| foreach(var failure in allFailures) | ||
| foreach (var failure in allFailures) | ||
| { | ||
| errorHandler(failure.Request, failure.Exception); | ||
| errorHandler(failure.Request, failure.Exception); | ||
| } | ||
| } | ||
|
|
||
| return allResponses; | ||
| } | ||
|
|
||
|
|
||
| private static void ApplyDelay(FaultException<OrganizationServiceFault> e, int retryCount) | ||
| { | ||
| TimeSpan delay; | ||
| if (e.Detail.ErrorCode == RateLimitExceededErrorCode) | ||
| { | ||
| // Use Retry-After delay when specified | ||
| delay = (TimeSpan)e.Detail.ErrorDetails["Retry-After"]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to add a check here to see if "Retry-After" exists in the ErrorDetails, as this isn't always the case. There are different types of RateLimits represented by the same RateLimitExceededErrorCode, so don't assume. I already ran into this issue. |
||
| } | ||
| else | ||
| { | ||
| // else use exponential backoff delay | ||
| delay = TimeSpan.FromSeconds(Math.Pow(2, retryCount)); | ||
| } | ||
|
|
||
| Thread.Sleep(delay); | ||
| } | ||
|
|
||
| private static bool IsTransientError(FaultException<OrganizationServiceFault> ex) | ||
| { | ||
| // You can add more transient fault codes to retry here | ||
| return ex.Detail.ErrorCode == RateLimitExceededErrorCode || | ||
| ex.Detail.ErrorCode == TimeLimitExceededErrorCode || | ||
| ex.Detail.ErrorCode == ConcurrencyLimitExceededErrorCode; | ||
| } | ||
|
|
||
| #endregion | ||
| } | ||
|
|
||
|
|
@@ -990,7 +1026,7 @@ private IEnumerable<TResponse> ExecuteOperationWithResponse<TRequest, TResponse> | |
| public abstract class ParallelServiceProxy<T> : ParallelServiceProxy | ||
| where T : XrmServiceManagerBase | ||
| { | ||
| protected static object syncRoot = new Object(); | ||
| protected static object syncRoot = new Object(); | ||
|
|
||
| #region Constructor(s) | ||
|
|
||
|
|
@@ -1014,7 +1050,7 @@ protected ParallelServiceProxy(T serviceManager, int maxDegreeOfParallelism) | |
| #endregion | ||
|
|
||
| #region Properties | ||
|
|
||
| protected T ServiceManager { get; set; } | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make this configurable, part of OrganizationServiceProxyOptions, set default to 3 if none provided