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

Introduce Hedging Strategy #876

Closed
laura-mi opened this issue Jul 30, 2021 · 14 comments · Fixed by #1161
Closed

Introduce Hedging Strategy #876

laura-mi opened this issue Jul 30, 2021 · 14 comments · Fixed by #1161
Assignees
Labels
v8 Issues related to the new version 8 of the Polly library.
Milestone

Comments

@laura-mi
Copy link

laura-mi commented Jul 30, 2021

We are working on the development of a generic HedgingPolicy to use as part of our HttpClient Resilience pipeline.
Since the hedging as a policy is not yet supported by Polly we consider setting up a new package dedicated to the generic implementation of such policy. Please let us know if you are interested in having this and how we could proceed further.

Summary
The hedging policy will allow the sequential and delayed execution of a set of tasks, until one of them completes successfully or until all of them are completed.
First task that is successfully completed triggers the cancellation and disposal of all other tasks and/or adjacent allocated resources.

General structure of polly policy:

  • The Async<PolicyName>Syntax exposes extension method for the PolicyBuilder which builds new instances of the Async<PolicyName>:
    • e.g. for Hedging: AsyncHedgingSyntax exposes methods to create AsyncHedgingPolicy instances.
  • The generic Async<PolicyName>Policy is the actual policy, which:
    • Inherits the AsyncPolicy<TResult> base class
    • Exposes internal constructor and it is created only via the Async<PolicyName>Syntax extensions.
    • Implements the abstract method of the base class, Async<PolicyName>Syntax which contains the actual implementation, i.e.
      Task<TResult> ImplementationAsync(
          Func<Context, CancellationToken, Task<TResult>> action,
          Context context, 
          CancellationToken cancellationToken,
          bool continueOnCapturedContext)
      
      • e.g. for Hedging: AsyncHedgingPolicy

Hedging policy configuration

The base implementation will take:

  • HedgingDelay - how long shall one wait before spawning a new hedged request;
  • OnHedgingAsync - task performed after every spawning a hedged request; [can be used for logging & telemetry]
  • A list of hedgedTaskFunctions - the tasks performed via hedging if the primary execution delegate fails.

Hedging logic

Steps:

  1. Start executing task [first one first]
  2. Wait up to HedgingDelay ms
  3. If the request is:
  • A. Completed:
    • If result is an exception
      • If ShouldHandleException - GOTO [B]
      • else - GOTO [4.]
    • If result is not exception
      • If ShouldHandleResult - GOTO [B]
      • else - GOTO [4.] (e.g. result is successful, so return result).
  • B. Not Completed:
    • Next task will be executed - GOTO [1.]
  1. The result of the execution can be:
  • [Success]
    • First request to complete successfully triggers the cancellation of all others
    • Return successful result.
  • [Failure]
    • No request completed successfully, and all requests have been executed.
    • Return the failure of the first task.

Exposed API for policy creation:

 var hedgingPolicy = Policy
                .Handle<Exception>()
                .OrResult<string>(_ => false)
                .AsyncHedgingPolicy(
                    hedgedTaskFunctions,
                    defaultHedgingDelay,
                    onHedgingAsync);

@martincostello
Copy link
Member

Sorry for the delayed reply.

This sounds interesting, but at this time I don't think this is something we'd have the bandwidth to add to a shipping version of Polly any time soon, but does sound like a candidate for something over in https://github.com/Polly-Contrib.

I don't know what the policies etc. are for dealing with that, I don't have access to that.

Maybe @joelhulen, @DreamOfTheRedChamber, @mebjas or @vany0114 could help you out.

@joelhulen
Copy link
Member

I also apologize for the late reply. I just saw this issue.

I can help you get up and running under Polly-Contrib, which is our officially recognized community projects that extend Polly's core functionality. Any Polly.Contrib NuGet packages are recognized as part of the Polly organization as well. Please reach out to me at joelhulen at gmail.com

Thanks!

@alexhcheng
Copy link

@joelhulen : sent you an email, we are interested in this feature as well and can contribute.

@laura-mi
Copy link
Author

@joelhulen I sent you an email last month, not sure if you got it. Please let me know :).

@joelhulen
Copy link
Member

joelhulen commented Oct 13, 2021 via email

@joelhulen
Copy link
Member

I have created a new repo generically for Hedging vs. the more specific RequestHedging I had previously created (and archived). @laura-mi, I've added you as an admin to this repo: https://github.com/Polly-Contrib/Polly.Contrib.Hedging per your request.

@juraj-blazek
Copy link

Since the hedging strategy will be implemented by us as maintainers and it doesn't have any additional dependencies, we were discussing to include hedging directly in the new Polly.Core package. We wouldn't need to setup all the build and test infra and it will also simplify future maintenance.
@joelhulen, @martincostello, @laura-mi any objections?

@martincostello
Copy link
Member

No objections from me.

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

I have no objections, either. In fact, I find that any contrib project migrating to core is a win :)

@martintmk martintmk changed the title Contrib Package for Hedging Policy Introduce Hedging Strategy Apr 26, 2023
@martintmk
Copy link
Contributor

The hedging will be adopted directly in Polly.Core for V8. The API will be adjusted to conform to v8 shape.

  • Options based
  • Hedging extensions for ResilienceStrategyBuilder

@johnib
Copy link

johnib commented Feb 20, 2024

The hedging will be adopted directly in Polly.Core for V8. The API will be adjusted to conform to v8 shape.

  • Options based
  • Hedging extensions for ResilienceStrategyBuilder

Is it still possible to use the Contrib.Hedging nuget somehow? I recall it had the ability to issue the hedging request if the duration crossed the duration of some percentile.

I.e. the hedging policy would record what is the pXX and use it as the threshold for issuing the hedging request.

I don't see this in the v8 docs anymore.

Thank you !

@martintmk
Copy link
Contributor

I don't think there is any hedging available for Polly V7.
It was considered but never published.

@johnib
Copy link

johnib commented Feb 21, 2024

I don't think there is any hedging available for Polly V7. It was considered but never published.

are you sure? I remember a repo Polly.Contrib.Hedging, which is actually mentioned here, and I recall the code was implemented there, long before Polly 8 was released.

Is it possible to get access this code somehow?

@martincostello
Copy link
Member

martincostello commented Feb 21, 2024

The repo is here, and it's been archived: https://github.com/Polly-Contrib/Polly.Contrib.RequestHedging

The contrib stuff is maintained independently from the core Polly library.

AFAIK, no contrib libraries have been updated to be compatible with the new APIs in Polly.Core for v8.

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 a pull request may close this issue.

7 participants