-
Notifications
You must be signed in to change notification settings - Fork 28
Retry config/default retry policy #510
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
Closed
LiamHamsters
wants to merge
7
commits into
ydb-platform:main
from
LiamHamsters:RetryConfig/DefaultRetryPolicy
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b748dfc
Add: IRetryConfig + DefaultRetryPolicy
LiamHamsters ef0d4f5
delete comment
LiamHamsters d0b9dbc
feat: add StatusDelayProfile with status-based backoff logic
LiamHamsters 6fbfe75
fix tests
LiamHamsters 2323965
fix lint
LiamHamsters 1d4794f
fix lint
LiamHamsters f762253
last fix lint
LiamHamsters 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 |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| namespace Ydb.Sdk.Ado.Retry; | ||
|
|
||
| public sealed class DefaultRetryPolicy : IRetryPolicy | ||
| { | ||
| private readonly RetryConfig _cfg; | ||
|
|
||
| public DefaultRetryPolicy(RetryConfig? config = null) | ||
| { | ||
| _cfg = config ?? new RetryConfig(); | ||
| } | ||
|
|
||
| public int MaxAttempts => _cfg.MaxAttempts; | ||
|
|
||
| public bool CanRetry(Exception ex, bool isIdempotent) | ||
| { | ||
| if (ex is YdbException ydb) | ||
| return isIdempotent ? ydb.IsTransientWhenIdempotent : ydb.IsTransient; | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| public TimeSpan? GetDelay(Exception ex, int attempt) | ||
| { | ||
| if (attempt <= 0) attempt = 1; | ||
|
|
||
| if (ex is YdbException ydb) | ||
| { | ||
| if (_cfg.PerStatusDelay.TryGetValue(ydb.Code, out var calc)) | ||
| return Cap(calc(attempt)); | ||
|
|
||
| var profileDelay = _cfg.StatusDelayProfile.GetDelay(ydb.Code, attempt); | ||
| if (profileDelay is not null) | ||
| return Cap(profileDelay); | ||
| } | ||
|
|
||
| return Cap(_cfg.DefaultDelay(ex, attempt)); | ||
| } | ||
|
|
||
| private TimeSpan? Cap(TimeSpan? delay) | ||
| { | ||
| if (delay is null) return null; | ||
| if (delay.Value <= TimeSpan.Zero) return TimeSpan.Zero; | ||
| return delay.Value <= _cfg.MaxDelay ? delay : _cfg.MaxDelay; | ||
| } | ||
| } |
34 changes: 34 additions & 0 deletions
34
src/Ydb.Sdk/src/Ado/Retry/Delay/DefaultStatusDelayProfile.cs
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 |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| namespace Ydb.Sdk.Ado.Retry.Delay; | ||
|
|
||
| public sealed class DefaultStatusDelayProfile : IStatusDelayProfile | ||
| { | ||
| public TimeSpan? GetDelay(StatusCode code, int attempt) | ||
| { | ||
| TimeSpan Calc(TimeSpan baseDelay) | ||
| { | ||
| var baseMs = baseDelay.TotalMilliseconds * Math.Pow(2.0, attempt - 1); | ||
| var jitter = 1.0 + Random.Shared.NextDouble() * 0.5; | ||
| var ms = Math.Min(baseMs * jitter, TimeSpan.FromSeconds(10).TotalMilliseconds); | ||
| return TimeSpan.FromMilliseconds(ms); | ||
| } | ||
|
|
||
| switch (code) | ||
| { | ||
| case StatusCode.BadSession: | ||
| return TimeSpan.Zero; | ||
| case StatusCode.Aborted: | ||
| case StatusCode.Unavailable: | ||
| case StatusCode.SessionBusy: | ||
| case StatusCode.ClientInternalError: | ||
| case StatusCode.ClientTransportUnavailable: | ||
| case StatusCode.ClientTransportTimeout: | ||
| return Calc(TimeSpan.FromMilliseconds(100)); | ||
| case StatusCode.Overloaded: | ||
| case StatusCode.ClientResourceExhausted: | ||
| case StatusCode.ClientTransportResourceExhausted: | ||
| return Calc(TimeSpan.FromMilliseconds(500)); | ||
| default: | ||
| return null; | ||
| } | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| namespace Ydb.Sdk.Ado.Retry.Delay; | ||
|
|
||
| public interface IStatusDelayProfile | ||
| { | ||
| TimeSpan? GetDelay(StatusCode code, int attempt); | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| namespace Ydb.Sdk.Ado.Retry; | ||
|
|
||
| public interface IRetryPolicy | ||
| { | ||
| int MaxAttempts { get; } | ||
|
|
||
| bool CanRetry(Exception ex, bool isIdempotent); | ||
|
|
||
| TimeSpan? GetDelay(Exception ex, int attempt); | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| using Ydb.Sdk.Ado.Retry.Delay; | ||
|
|
||
| namespace Ydb.Sdk.Ado.Retry; | ||
|
|
||
| public sealed class RetryConfig | ||
| { | ||
| public int MaxAttempts { get; set; } = 10; | ||
|
|
||
| public TimeSpan BaseDelay { get; set; } = TimeSpan.FromMilliseconds(100); | ||
|
|
||
| public double Exponent { get; set; } = 2.0; | ||
|
|
||
| public TimeSpan MaxDelay { get; set; } = TimeSpan.FromSeconds(10); | ||
|
|
||
| public double JitterFraction { get; set; } = 0.5; | ||
|
|
||
| public Dictionary<StatusCode, Func<int, TimeSpan?>> PerStatusDelay { get; } = new(); | ||
|
|
||
| public IStatusDelayProfile StatusDelayProfile { get; set; } = new DefaultStatusDelayProfile(); | ||
|
|
||
| public Func<Exception, int, TimeSpan?> DefaultDelay { get; set; } = | ||
| static (_, attempt) => | ||
| { | ||
| var baseMs = 100.0 * Math.Pow(2.0, Math.Max(0, attempt - 1)); | ||
| var jitter = 1.0 + Random.Shared.NextDouble() * 0.5; | ||
| var ms = Math.Min(baseMs * jitter, 10_000.0); | ||
| return TimeSpan.FromMilliseconds(ms); | ||
| }; | ||
| } | ||
135 changes: 135 additions & 0 deletions
135
src/Ydb.Sdk/test/Ydb.Sdk.Ado.Tests/DefaultRetryPolicyTests.cs
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 |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| using Xunit; | ||
| using Ydb.Sdk.Ado.Retry; | ||
|
|
||
| namespace Ydb.Sdk.Ado.Tests; | ||
|
|
||
| public class DefaultRetryPolicyTests : TestBase | ||
| { | ||
| [Fact] | ||
| public void GetDelay_WhenAttemptOne_UsesDefaultDelayDelegate() | ||
| { | ||
| var config = new RetryConfig | ||
| { | ||
| DefaultDelay = (ex, attempt) => | ||
| { | ||
| _ = ex; | ||
| _ = attempt; | ||
| return TimeSpan.FromMilliseconds(100); | ||
| }, | ||
| MaxDelay = TimeSpan.FromMilliseconds(500) | ||
| }; | ||
|
|
||
| var policy = new DefaultRetryPolicy(config); | ||
|
|
||
| var delay = policy.GetDelay(new Exception("test"), 1); | ||
| Assert.Equal(TimeSpan.FromMilliseconds(100), delay); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetDelay_WhenAttemptOne_ReturnsBaseDelay_NoJitter() | ||
| { | ||
| var config = new RetryConfig | ||
| { | ||
| BaseDelay = TimeSpan.FromMilliseconds(100), | ||
| Exponent = 2.0, | ||
| JitterFraction = 0.0, | ||
| MaxDelay = TimeSpan.FromMilliseconds(500), | ||
| DefaultDelay = (ex, attempt) => | ||
| { | ||
| _ = ex; | ||
| attempt = Math.Max(1, attempt); | ||
| var baseMs = 100.0 * Math.Pow(2.0, attempt - 1); | ||
| var ms = Math.Min(baseMs, 500.0); | ||
| return TimeSpan.FromMilliseconds(ms); | ||
| } | ||
| }; | ||
|
|
||
| var policy = new DefaultRetryPolicy(config); | ||
|
|
||
| var delay = policy.GetDelay(new Exception("test"), 1); | ||
| Assert.Equal(TimeSpan.FromMilliseconds(100), delay); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetDelay_WhenStatusHasOverride_ReturnsPerStatusDelay() | ||
| { | ||
| var config = new RetryConfig(); | ||
| config.PerStatusDelay[StatusCode.Unavailable] = attempt => | ||
| { | ||
| _ = attempt; | ||
| return TimeSpan.FromMilliseconds(123); | ||
| }; | ||
| var policy = new DefaultRetryPolicy(config); | ||
|
|
||
| var ex = new YdbException(StatusCode.Unavailable, "unavailable"); | ||
| var delay = policy.GetDelay(ex, 2); | ||
|
|
||
| Assert.Equal(TimeSpan.FromMilliseconds(123), delay); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CanRetry_WhenTransientYdbException_ReturnsTrue() | ||
| { | ||
| var config = new RetryConfig(); | ||
| var policy = new DefaultRetryPolicy(config); | ||
| var ex = new YdbException(StatusCode.Aborted, "transient"); | ||
|
|
||
| Assert.True(policy.CanRetry(ex, isIdempotent: false)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CanRetry_WhenOverloaded_RetriesRegardlessOfIdempotency() | ||
| { | ||
| var policy = new DefaultRetryPolicy(new RetryConfig()); | ||
| var ex = new YdbException(StatusCode.Overloaded, "overloaded"); | ||
|
|
||
| Assert.True(policy.CanRetry(ex, isIdempotent: false)); | ||
| Assert.True(policy.CanRetry(ex, isIdempotent: true)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CanRetry_WhenTimeoutException_ReturnsFalse() | ||
| { | ||
| var config = new RetryConfig(); | ||
| var policy = new DefaultRetryPolicy(config); | ||
|
|
||
| Assert.False(policy.CanRetry(new TimeoutException(), isIdempotent: true)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CanRetry_WhenUserCancelled_ReturnsFalse() | ||
| { | ||
| using var cts = new CancellationTokenSource(); | ||
| cts.Cancel(); | ||
| var config = new RetryConfig(); | ||
| var policy = new DefaultRetryPolicy(config); | ||
|
|
||
| var ex = new OperationCanceledException(cts.Token); | ||
| Assert.False(policy.CanRetry(ex, isIdempotent: true)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetDelay_WhenDelayExceedsMaxDelay_IsCappedToMaxDelay() | ||
| { | ||
| var config = new RetryConfig | ||
| { | ||
| MaxDelay = TimeSpan.FromMilliseconds(500), | ||
| DefaultDelay = (_, _) => TimeSpan.FromMilliseconds(1000) | ||
| }; | ||
| var policy = new DefaultRetryPolicy(config); | ||
|
|
||
| var delay = policy.GetDelay(new Exception("test"), 1); | ||
|
|
||
| Assert.Equal(TimeSpan.FromMilliseconds(500), delay); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CanRetry_WhenOperationCanceledWithoutToken_ReturnsFalse() | ||
| { | ||
| var config = new RetryConfig(); | ||
| var policy = new DefaultRetryPolicy(config); | ||
|
|
||
| var ex = new OperationCanceledException(); | ||
| Assert.False(policy.CanRetry(ex, isIdempotent: true)); | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.