diff --git a/NEXT_MAJOR_RELEASE.md b/NEXT_MAJOR_RELEASE.md index 78bd010c3..5a2e38800 100644 --- a/NEXT_MAJOR_RELEASE.md +++ b/NEXT_MAJOR_RELEASE.md @@ -1,3 +1,4 @@ # Breaking changes for the next major release - [ ] Make connection string as required param to ydb.New call (instead optional params ydb.WithConnectionString, ydb.WithEndpoint, ydb.WithDatabase, ydb.WithInsecure) - [ ] Change signature of `closer.Closer.Close(ctx) error` to `closer.Closer.Close()` +- [ ] Remove `retry.FastBackoff` and `retry.SlowBackoff` public variables diff --git a/internal/backoff/backoff.go b/internal/backoff/backoff.go index ecf6c6078..716f6d173 100644 --- a/internal/backoff/backoff.go +++ b/internal/backoff/backoff.go @@ -39,23 +39,23 @@ var ( // logBackoff contains logarithmic Backoff policy. type logBackoff struct { - // SlotDuration is a size of a single time slot used in Backoff Delay + // slotDuration is a size of a single time slot used in Backoff Delay // calculation. - // If SlotDuration is less or equal to zero, then the time.Second value is + // If slotDuration is less or equal to zero, then the time.Second value is // used. - SlotDuration time.Duration + slotDuration time.Duration - // Ceiling is a maximum degree of Backoff Delay growth. - // If Ceiling is less or equal to zero, then the default ceiling of 1 is + // ceiling is a maximum degree of Backoff Delay growth. + // If ceiling is less or equal to zero, then the default ceiling of 1 is // used. - Ceiling uint + ceiling uint - // JitterLimit controls fixed and random portions of Backoff Delay. + // jitterLimit controls fixed and random portions of Backoff Delay. // Its value can be in range [0, 1]. - // If JitterLimit is non zero, then the Backoff Delay will be equal to (F + R), + // If jitterLimit is non zero, then the Backoff Delay will be equal to (F + R), // where F is a result of multiplication of this value and calculated Delay // duration D; and R is a random sized part from [0,(D - F)]. - JitterLimit float64 + jitterLimit float64 // generator of jitter r xrand.Rand @@ -65,19 +65,19 @@ type option func(b *logBackoff) func WithSlotDuration(slotDuration time.Duration) option { return func(b *logBackoff) { - b.SlotDuration = slotDuration + b.slotDuration = slotDuration } } func WithCeiling(ceiling uint) option { return func(b *logBackoff) { - b.Ceiling = ceiling + b.ceiling = ceiling } } func WithJitterLimit(jitterLimit float64) option { return func(b *logBackoff) { - b.JitterLimit = jitterLimit + b.jitterLimit = jitterLimit } } @@ -98,13 +98,13 @@ func (b logBackoff) Wait(n int) <-chan time.Time { // Delay returns mapping of i to Delay. func (b logBackoff) Delay(i int) time.Duration { - s := b.SlotDuration + s := b.slotDuration if s <= 0 { s = time.Second } - n := 1 << min(uint(i), max(1, b.Ceiling)) + n := 1 << min(uint(i), max(1, b.ceiling)) d := s * time.Duration(n) - f := time.Duration(math.Min(1, math.Abs(b.JitterLimit)) * float64(d)) + f := time.Duration(math.Min(1, math.Abs(b.jitterLimit)) * float64(d)) if f == d { return f } diff --git a/internal/backoff/type.go b/internal/backoff/type.go index 425fab6c5..7555cd44f 100644 --- a/internal/backoff/type.go +++ b/internal/backoff/type.go @@ -9,19 +9,19 @@ type Type uint8 const ( TypeNoBackoff Type = 1 << iota >> 1 - TypeFastBackoff - TypeSlowBackoff + TypeFast + TypeSlow - TypeAny = TypeFastBackoff | TypeSlowBackoff + TypeAny = TypeFast | TypeSlow ) func (b Type) String() string { switch b { case TypeNoBackoff: return "immediately" - case TypeFastBackoff: + case TypeFast: return "fast backoff" - case TypeSlowBackoff: + case TypeSlow: return "slow backoff" case TypeAny: return "any backoff" diff --git a/internal/table/client.go b/internal/table/client.go index ac8978e60..3e144b022 100644 --- a/internal/table/client.go +++ b/internal/table/client.go @@ -132,8 +132,8 @@ func (c *client) CreateSession(ctx context.Context, opts ...table.Option) (table }, retry.WithIdempotent(true), retry.WithID("CreateSession"), - retry.WithInternalFastBackoff(options.FastBackoff), - retry.WithInternalSlowBackoff(options.SlowBackoff), + retry.WithFastBackoff(options.FastBackoff), + retry.WithSlowBackoff(options.SlowBackoff), retry.WithTrace(trace.Retry{ OnRetry: func(info trace.RetryLoopStartInfo) func(trace.RetryLoopIntermediateInfo) func(trace.RetryLoopDoneInfo) { onIntermediate := trace.TableOnCreateSession(c.config.Trace(), info.Context) diff --git a/internal/table/retry.go b/internal/table/retry.go index 4cdce67f3..c3ab04fbd 100644 --- a/internal/table/retry.go +++ b/internal/table/retry.go @@ -262,8 +262,8 @@ func retryBackoff( return xerrors.WithStackTrace(err) }, - retry.WithInternalFastBackoff(fastBackoff), - retry.WithInternalSlowBackoff(slowBackoff), + retry.WithFastBackoff(fastBackoff), + retry.WithSlowBackoff(slowBackoff), retry.WithIdempotent(isOperationIdempotent), ) } diff --git a/internal/wait/wait.go b/internal/wait/wait.go index 2698aa715..15a07869d 100644 --- a/internal/wait/wait.go +++ b/internal/wait/wait.go @@ -33,9 +33,9 @@ func Wait(ctx context.Context, fastBackoff backoff.Backoff, slowBackoff backoff. switch t { case backoff.TypeNoBackoff: return nil - case backoff.TypeFastBackoff: + case backoff.TypeFast: b = fastBackoff - case backoff.TypeSlowBackoff: + case backoff.TypeSlow: b = slowBackoff } return waitBackoff(ctx, b, i) diff --git a/internal/xerrors/operation.go b/internal/xerrors/operation.go index e711b9456..17dc8f6e0 100644 --- a/internal/xerrors/operation.go +++ b/internal/xerrors/operation.go @@ -122,14 +122,14 @@ func (e *operationError) BackoffType() backoff.Type { switch e.code { case Ydb.StatusIds_OVERLOADED: - return backoff.TypeSlowBackoff + return backoff.TypeSlow case Ydb.StatusIds_ABORTED, Ydb.StatusIds_UNAVAILABLE, Ydb.StatusIds_CANCELLED, Ydb.StatusIds_SESSION_BUSY, Ydb.StatusIds_UNDETERMINED: - return backoff.TypeFastBackoff + return backoff.TypeFast default: return backoff.TypeNoBackoff } diff --git a/internal/xerrors/transport.go b/internal/xerrors/transport.go index e899ec08c..612197f8f 100644 --- a/internal/xerrors/transport.go +++ b/internal/xerrors/transport.go @@ -127,10 +127,10 @@ func (e *transportError) BackoffType() backoff.Type { grpcCodes.Internal, grpcCodes.Canceled, grpcCodes.Unavailable: - return backoff.TypeFastBackoff + return backoff.TypeFast case grpcCodes.ResourceExhausted: - return backoff.TypeSlowBackoff + return backoff.TypeSlow default: return backoff.TypeNoBackoff } diff --git a/retry/retry.go b/retry/retry.go index ebede31ce..25437270a 100644 --- a/retry/retry.go +++ b/retry/retry.go @@ -13,13 +13,15 @@ import ( var ( // FastBackoff is a default fast backoff object + // // Deprecated: don't use explicit it, will be removed at next major release. - // Use retry.WithFastBackoff retry option for redefine default fast backoff instead + // Use retry.Backoff constructor instead FastBackoff = backoff.Fast // SlowBackoff is a default fast backoff object + // // Deprecated: don't use explicit it, will be removed at next major release. - // Use retry.WithSlowBackoff retry option for redefine default slow backoff instead + // Use retry.Backoff constructor instead SlowBackoff = backoff.Slow ) @@ -32,8 +34,8 @@ type retryableErrorOption xerrors.RetryableErrorOption const ( BackoffTypeNoBackoff = backoff.TypeNoBackoff - BackoffTypeFastBackoff = backoff.TypeFastBackoff - BackoffTypeSlowBackoff = backoff.TypeSlowBackoff + BackoffTypeFastBackoff = backoff.TypeFast + BackoffTypeSlowBackoff = backoff.TypeSlow ) // WithBackoff makes retryable error option with custom backoff type @@ -86,44 +88,31 @@ func WithTrace(trace trace.Retry) retryOption { } } -// WithIdempotent returns idempotent option +// WithIdempotent applies idempotent flag to retry operation func WithIdempotent(idempotent bool) retryOption { return func(h *retryOptions) { h.idempotent = idempotent } } -// WithFastBackoff replaces default fast backoff -func WithFastBackoff(slotDuration time.Duration, ceiling uint, jitterLimit float64) retryOption { - return func(h *retryOptions) { - h.fastBackoff = backoff.New( - backoff.WithSlotDuration(slotDuration), - backoff.WithCeiling(ceiling), - backoff.WithJitterLimit(jitterLimit), - ) - } -} - -// WithSlowBackoff replaces default slow backoff -func WithSlowBackoff(slotDuration time.Duration, ceiling uint, jitterLimit float64) retryOption { - return func(h *retryOptions) { - h.slowBackoff = backoff.New( - backoff.WithSlotDuration(slotDuration), - backoff.WithCeiling(ceiling), - backoff.WithJitterLimit(jitterLimit), - ) - } +// Backoff makes backoff with custom params +func Backoff(slotDuration time.Duration, ceiling uint, jitterLimit float64) backoff.Backoff { + return backoff.New( + backoff.WithSlotDuration(slotDuration), + backoff.WithCeiling(ceiling), + backoff.WithJitterLimit(jitterLimit), + ) } -// WithInternalFastBackoff replaces default fast backoff -func WithInternalFastBackoff(b backoff.Backoff) retryOption { +// WithFastBackoff replaces default fast backoff +func WithFastBackoff(b backoff.Backoff) retryOption { return func(h *retryOptions) { h.fastBackoff = b } } -// WithInternalSlowBackoff replaces default slow backoff -func WithInternalSlowBackoff(b backoff.Backoff) retryOption { +// WithSlowBackoff replaces default slow backoff +func WithSlowBackoff(b backoff.Backoff) retryOption { return func(h *retryOptions) { h.slowBackoff = b } diff --git a/retry/retry_test.go b/retry/retry_test.go index 573950faf..ea893a4a4 100644 --- a/retry/retry_test.go +++ b/retry/retry_test.go @@ -164,7 +164,7 @@ func TestRetryModes(t *testing.T) { // We want to check internal grpc error on chaos monkey testing // nolint:nolintlint err: xerrors.FromGRPCError(grpc.ErrClientConnClosing), - backoff: backoff.TypeFastBackoff, + backoff: backoff.TypeFast, deleteSession: true, canRetry: CanRetry{ idempotentOperation: true, @@ -184,7 +184,7 @@ func TestRetryModes(t *testing.T) { err: xerrors.Transport( xerrors.WithCode(grpcCodes.Canceled), ), - backoff: backoff.TypeFastBackoff, + backoff: backoff.TypeFast, deleteSession: true, canRetry: CanRetry{ idempotentOperation: true, @@ -261,7 +261,7 @@ func TestRetryModes(t *testing.T) { err: xerrors.Transport( xerrors.WithCode(grpcCodes.ResourceExhausted), ), - backoff: backoff.TypeSlowBackoff, + backoff: backoff.TypeSlow, deleteSession: false, canRetry: CanRetry{ idempotentOperation: true, @@ -316,7 +316,7 @@ func TestRetryModes(t *testing.T) { err: xerrors.Transport( xerrors.WithCode(grpcCodes.Internal), ), - backoff: backoff.TypeFastBackoff, + backoff: backoff.TypeFast, deleteSession: true, canRetry: CanRetry{ idempotentOperation: true, @@ -327,7 +327,7 @@ func TestRetryModes(t *testing.T) { err: xerrors.Transport( xerrors.WithCode(grpcCodes.Unavailable), ), - backoff: backoff.TypeFastBackoff, + backoff: backoff.TypeFast, deleteSession: true, canRetry: CanRetry{ idempotentOperation: true, @@ -404,7 +404,7 @@ func TestRetryModes(t *testing.T) { err: xerrors.Operation( xerrors.WithStatusCode(Ydb.StatusIds_ABORTED), ), - backoff: backoff.TypeFastBackoff, + backoff: backoff.TypeFast, deleteSession: false, canRetry: CanRetry{ idempotentOperation: true, @@ -415,7 +415,7 @@ func TestRetryModes(t *testing.T) { err: xerrors.Operation( xerrors.WithStatusCode(Ydb.StatusIds_UNAVAILABLE), ), - backoff: backoff.TypeFastBackoff, + backoff: backoff.TypeFast, deleteSession: false, canRetry: CanRetry{ idempotentOperation: true, @@ -426,7 +426,7 @@ func TestRetryModes(t *testing.T) { err: xerrors.Operation( xerrors.WithStatusCode(Ydb.StatusIds_OVERLOADED), ), - backoff: backoff.TypeSlowBackoff, + backoff: backoff.TypeSlow, deleteSession: false, canRetry: CanRetry{ idempotentOperation: true, @@ -525,7 +525,7 @@ func TestRetryModes(t *testing.T) { err: xerrors.Operation( xerrors.WithStatusCode(Ydb.StatusIds_CANCELLED), ), - backoff: backoff.TypeFastBackoff, + backoff: backoff.TypeFast, deleteSession: false, canRetry: CanRetry{ idempotentOperation: false, @@ -536,7 +536,7 @@ func TestRetryModes(t *testing.T) { err: xerrors.Operation( xerrors.WithStatusCode(Ydb.StatusIds_UNDETERMINED), ), - backoff: backoff.TypeFastBackoff, + backoff: backoff.TypeFast, deleteSession: false, canRetry: CanRetry{ idempotentOperation: true, @@ -558,7 +558,7 @@ func TestRetryModes(t *testing.T) { err: xerrors.Operation( xerrors.WithStatusCode(Ydb.StatusIds_SESSION_BUSY), ), - backoff: backoff.TypeFastBackoff, + backoff: backoff.TypeFast, deleteSession: true, canRetry: CanRetry{ idempotentOperation: true,