Skip to content

Commit

Permalink
fix PR issues
Browse files Browse the repository at this point in the history
  • Loading branch information
asmyasnikov committed Apr 15, 2022
1 parent ec4a82b commit 25b805c
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 70 deletions.
1 change: 1 addition & 0 deletions NEXT_MAJOR_RELEASE.md
Original file line number Diff line number Diff line change
@@ -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
30 changes: 15 additions & 15 deletions internal/backoff/backoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}

Expand All @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions internal/backoff/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions internal/table/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions internal/table/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
}
4 changes: 2 additions & 2 deletions internal/wait/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions internal/xerrors/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions internal/xerrors/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
47 changes: 18 additions & 29 deletions retry/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
22 changes: 11 additions & 11 deletions retry/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 25b805c

Please sign in to comment.