|
| 1 | +using Ydb.Sdk.Ado.Internal; |
| 2 | + |
| 3 | +namespace Ydb.Sdk.Ado.RetryPolicy; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// See <a href="https://aws.amazon.com/ru/blogs/architecture/exponential-backoff-and-jitter/">AWS paper</a> |
| 7 | +/// </summary> |
| 8 | +public class YdbRetryPolicy : IRetryPolicy |
| 9 | +{ |
| 10 | + public static readonly YdbRetryPolicy Default = new(YdbRetryPolicyConfig.Default); |
| 11 | + |
| 12 | + private readonly int _maxAttempt; |
| 13 | + private readonly int _fastBackoffBaseMs; |
| 14 | + private readonly int _slowBackoffBaseMs; |
| 15 | + private readonly int _fastCeiling; |
| 16 | + private readonly int _slowCeiling; |
| 17 | + private readonly int _fastCapBackoffMs; |
| 18 | + private readonly int _slowCapBackoffMs; |
| 19 | + private readonly bool _enableRetryIdempotence; |
| 20 | + private readonly IRandom _random; |
| 21 | + |
| 22 | + public YdbRetryPolicy(YdbRetryPolicyConfig config) |
| 23 | + { |
| 24 | + _maxAttempt = config.MaxAttempt; |
| 25 | + _fastBackoffBaseMs = config.FastBackoffBaseMs; |
| 26 | + _slowBackoffBaseMs = config.SlowBackoffBaseMs; |
| 27 | + _fastCeiling = (int)Math.Ceiling(Math.Log(config.FastCapBackoffMs + 1, 2)); |
| 28 | + _slowCeiling = (int)Math.Ceiling(Math.Log(config.SlowCapBackoffMs + 1, 2)); |
| 29 | + _fastCapBackoffMs = config.FastCapBackoffMs; |
| 30 | + _slowCapBackoffMs = config.SlowCapBackoffMs; |
| 31 | + _enableRetryIdempotence = config.EnableRetryIdempotence; |
| 32 | + _random = ThreadLocalRandom.Instance; |
| 33 | + } |
| 34 | + |
| 35 | + internal YdbRetryPolicy(YdbRetryPolicyConfig config, IRandom random) : this(config) |
| 36 | + { |
| 37 | + _random = random; |
| 38 | + } |
| 39 | + |
| 40 | + public TimeSpan? GetNextDelay(YdbException ydbException, int attempt) |
| 41 | + { |
| 42 | + if (attempt >= _maxAttempt || (!_enableRetryIdempotence && !ydbException.IsTransient)) |
| 43 | + return null; |
| 44 | + |
| 45 | + return ydbException.Code switch |
| 46 | + { |
| 47 | + StatusCode.BadSession or StatusCode.SessionBusy => TimeSpan.Zero, |
| 48 | + StatusCode.Aborted or StatusCode.Undetermined => |
| 49 | + FullJitter(_fastBackoffBaseMs, _fastCapBackoffMs, _fastCeiling, attempt, _random), |
| 50 | + StatusCode.Unavailable or StatusCode.ClientTransportUnknown or StatusCode.ClientTransportUnavailable => |
| 51 | + EqualJitter(_fastBackoffBaseMs, _fastCapBackoffMs, _fastCeiling, attempt, _random), |
| 52 | + StatusCode.Overloaded or StatusCode.ClientTransportResourceExhausted => |
| 53 | + EqualJitter(_slowBackoffBaseMs, _slowCapBackoffMs, _slowCeiling, attempt, _random), |
| 54 | + _ => null |
| 55 | + }; |
| 56 | + } |
| 57 | + |
| 58 | + private static TimeSpan FullJitter(int backoffBaseMs, int capMs, int ceiling, int attempt, IRandom random) => |
| 59 | + TimeSpan.FromMilliseconds(random.Next(CalculateBackoff(backoffBaseMs, capMs, ceiling, attempt) + 1)); |
| 60 | + |
| 61 | + private static TimeSpan EqualJitter(int backoffBaseMs, int capMs, int ceiling, int attempt, IRandom random) |
| 62 | + { |
| 63 | + var calculatedBackoff = CalculateBackoff(backoffBaseMs, capMs, ceiling, attempt); |
| 64 | + var temp = calculatedBackoff / 2; |
| 65 | + |
| 66 | + return TimeSpan.FromMilliseconds(temp + calculatedBackoff % 2 + random.Next(temp + 1)); |
| 67 | + } |
| 68 | + |
| 69 | + private static int CalculateBackoff(int backoffBaseMs, int capMs, int ceiling, int attempt) => |
| 70 | + Math.Min(backoffBaseMs * (1 << Math.Min(ceiling, attempt)), capMs); |
| 71 | +} |
0 commit comments