Polly icon indicating copy to clipboard operation
Polly copied to clipboard

Contrib Package for Hedging Policy

Open laura-mi opened this issue 4 years ago • 5 comments

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);

laura-mi avatar Jul 30 '21 15:07 laura-mi

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.

martincostello avatar Aug 10 '21 12:08 martincostello

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!

joelhulen avatar Aug 10 '21 14:08 joelhulen

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

alexhcheng avatar Sep 28 '21 16:09 alexhcheng

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

laura-mi avatar Sep 30 '21 11:09 laura-mi

Hey, everyone. Sorry I missed this thread. I have created the new Polly.Contrib project here: https://github.com/Polly-Contrib/Polly.Contrib.RequestHedging and added both Alex and Laura. I will work with both of them to make sure they can successfully publish official Polly.Contrib NuGet packages.

Thanks! Joel

On Thu, Sep 30, 2021 at 7:22 AM Laura @.***> wrote:

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

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/App-vNext/Polly/issues/876#issuecomment-931232245, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABNMWYGMXWITMVFXE244LETUERB63ANCNFSM5BIWPLRQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

joelhulen avatar Oct 13 '21 16:10 joelhulen

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.

joelhulen avatar Jan 12 '23 12:01 joelhulen

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?

juraj-blazek avatar Apr 21 '23 09:04 juraj-blazek

No objections from me.

martincostello avatar Apr 21 '23 10:04 martincostello

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

joelhulen avatar Apr 25 '23 16:04 joelhulen

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

martintmk avatar Apr 26 '23 07:04 martintmk

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 !

johnib avatar Feb 20 '24 08:02 johnib

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

martintmk avatar Feb 20 '24 09:02 martintmk

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?

johnib avatar Feb 21 '24 12:02 johnib

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.

martincostello avatar Feb 21 '24 12:02 martincostello