Skip to content
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

The ExecuteCoreAsync method is now Outcome based #1195

Merged
merged 8 commits into from
May 23, 2023

Conversation

martintmk
Copy link
Contributor

@martintmk martintmk commented May 18, 2023

Details on the issue fix or feature implementation

This change simplifies the implementation of strategies and improves performance in cases when there are many exceptions thrown (i.e. circuit breaker is opened).

The perf impact of this change is non-trivial. It comes from wrapping the user-callback and converting it to outcome-based one. If caller provides the outcome-based compatible callback the impact is minimal (demonstrated in the ResilienceStrategyBenchmark).

Benefits:

  • Simplify the implementation of strategies (do not have to deal with exceptions).
  • Allow to attach additional information to the Outcome in the future.
  • Improves the perf in error cases as no-exception are thrown (for example when circuit is opened). Note that the caller needs to use the Outcome-based API to take advantage of this. By default, the perf will be around the same if the normal Execute methods are used.

Downsides:

  • Perf hit for success cases.

Benchmark for error cases:

Method Mean Error StdDev Ratio RatioSD Gen0 Allocated Alloc Ratio
ExecuteAsync_Exception_V7 29,569.5 ns 70.74 ns 101.46 ns 91.72 1.72 0.0916 2888 B 12.89
ExecuteAsync_Exception_V8 31,845.8 ns 158.22 ns 221.81 ns 98.72 1.85 0.0610 2824 B 12.61
ExecuteAsync_Outcome_V8 322.5 ns 4.09 ns 5.87 ns 1.00 0.00 0.0086 224 B 1.00

Confirm the following

  • I started this PR by branching from the head of the default branch
  • I have targeted the PR to merge into the default branch
  • I have included unit tests for the issue/feature
  • I have successfully run a local build

@martintmk martintmk added the v8 Issues related to the new version 8 of the Polly library. label May 18, 2023
@martintmk martintmk added this to the v8.0.0 milestone May 18, 2023
Copy link
Member

@martincostello martincostello left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

src/Polly.Core.Tests/Polly.Core.Tests.csproj Outdated Show resolved Hide resolved
src/Polly.Core/Strategy/Outcome.TResult.cs Outdated Show resolved Hide resolved
src/Polly.Core/Strategy/Outcome.cs Outdated Show resolved Hide resolved
@martintmk martintmk force-pushed the mtomka/execute-core-returns-outcome branch from d7bbafb to 67b58b6 Compare May 18, 2023 14:02
@geeknoid
Copy link
Contributor

I'm not crazy about the perf hit, but I think the improvements are warranted.

@martintmk
Copy link
Contributor Author

Ok, I'll go ahead, polish this and prepare for merging.

@martintmk martintmk force-pushed the mtomka/execute-core-returns-outcome branch 5 times, most recently from 2aa54de to 8a79609 Compare May 23, 2023 06:35
@codecov-commenter
Copy link

codecov-commenter commented May 23, 2023

Codecov Report

Merging #1195 (41074c3) into main (71b760a) will increase coverage by 0.39%.
The diff coverage is 100.00%.

❗ Current head 41074c3 differs from pull request most recent head d8d0558. Consider uploading reports for the commit d8d0558 to get more accurate results

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

@@            Coverage Diff             @@
##             main    #1195      +/-   ##
==========================================
+ Coverage   83.31%   83.70%   +0.39%     
==========================================
  Files         277      278       +1     
  Lines        6286     6439     +153     
  Branches     1023     1023              
==========================================
+ Hits         5237     5390     +153     
  Misses        844      844              
  Partials      205      205              
Flag Coverage Δ
linux ?
macos 83.70% <100.00%> (+0.39%) ⬆️
windows 83.70% <100.00%> (+0.39%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...Core/Hedging/Controller/HedgingExecutionContext.cs 100.00% <ø> (ø)
src/Polly.Core/NullResilienceStrategy.cs 100.00% <ø> (ø)
.../Polly.Core/Strategy/ResilienceStrategyPipeline.cs 100.00% <ø> (ø)
...CircuitBreaker/CircuitBreakerResilienceStrategy.cs 100.00% <100.00%> (ø)
...ircuitBreaker/Controller/CircuitStateController.cs 100.00% <100.00%> (ø)
.../Polly.Core/Fallback/FallbackResilienceStrategy.cs 100.00% <100.00%> (ø)
src/Polly.Core/Hedging/Controller/TaskExecution.cs 100.00% <100.00%> (ø)
...rc/Polly.Core/Hedging/HedgingResilienceStrategy.cs 100.00% <100.00%> (ø)
...c/Polly.Core/ResilienceStrategy.Async.ValueTask.cs 100.00% <100.00%> (ø)
.../Polly.Core/ResilienceStrategy.Async.ValueTaskT.cs 100.00% <100.00%> (ø)
... and 10 more

/// <summary>
/// Gets the <see cref="ExceptionDispatchInfo"/> associated with the exception, if any.
/// </summary>
internal ExceptionDispatchInfo? ExceptionDispatchInfo { get; }
Copy link
Contributor

@geeknoid geeknoid May 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given this new property, then you should change the Exception property to just

public Exception? => ExceptionDispatchInfo?.SourceException;

which would shrink the Outcome and Outcome<T> structs by 1/3, speeding up all that struct copying.

@martintmk martintmk force-pushed the mtomka/execute-core-returns-outcome branch from c727b70 to dacf762 Compare May 23, 2023 11:08
@martintmk martintmk enabled auto-merge (squash) May 23, 2023 12:17
@martintmk martintmk merged commit 34f63dd into main May 23, 2023
@martintmk martintmk deleted the mtomka/execute-core-returns-outcome branch May 23, 2023 12:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v8 Issues related to the new version 8 of the Polly library.
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

5 participants