Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/Ydb.Sdk/src/Ado/Retry/DefaultRetryPolicy.cs
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 src/Ydb.Sdk/src/Ado/Retry/Delay/DefaultStatusDelayProfile.cs
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;
}
}
}
6 changes: 6 additions & 0 deletions src/Ydb.Sdk/src/Ado/Retry/Delay/IStatusDelayProfile.cs
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);
}
10 changes: 10 additions & 0 deletions src/Ydb.Sdk/src/Ado/Retry/IRetryPolicy.cs
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);
}
29 changes: 29 additions & 0 deletions src/Ydb.Sdk/src/Ado/Retry/RetryConfig.cs
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 src/Ydb.Sdk/test/Ydb.Sdk.Ado.Tests/DefaultRetryPolicyTests.cs
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));
}
}
Loading