Skip to content

Commit

Permalink
retry, client: append errors for backoffer (#7896)
Browse files Browse the repository at this point in the history
ref #7894

Signed-off-by: Cabinfever_B <[email protected]>

Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
  • Loading branch information
CabinfeverB and ti-chi-bot[bot] authored Mar 28, 2024
1 parent bc92c13 commit 2c20897
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion client/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/stretchr/testify v1.8.2
go.uber.org/atomic v1.10.0
go.uber.org/goleak v1.1.11
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.24.0
golang.org/x/exp v0.0.0-20230711005742-c3f37128e5a4
google.golang.org/grpc v1.59.0
Expand All @@ -33,7 +34,6 @@ require (
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.46.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
Expand Down
21 changes: 16 additions & 5 deletions client/retry/backoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ import (

"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"go.uber.org/multierr"
)

const maxRecordErrorCount = 20

// Backoffer is a backoff policy for retrying operations.
type Backoffer struct {
// base defines the initial time interval to wait before each retry.
Expand All @@ -34,6 +37,7 @@ type Backoffer struct {
// By default, all errors are retryable.
retryableChecker func(err error) bool

attempt int
next time.Duration
currentTotal time.Duration
}
Expand All @@ -45,11 +49,16 @@ func (bo *Backoffer) Exec(
) error {
defer bo.resetBackoff()
var (
err error
after *time.Timer
allErrors error
after *time.Timer
)
for {
err = fn()
err := fn()
bo.attempt++
if bo.attempt < maxRecordErrorCount {
// multierr.Append will ignore nil error.
allErrors = multierr.Append(allErrors, err)
}
if !bo.isRetryable(err) {
break
}
Expand All @@ -62,7 +71,7 @@ func (bo *Backoffer) Exec(
select {
case <-ctx.Done():
after.Stop()
return errors.Trace(ctx.Err())
return multierr.Append(allErrors, errors.Trace(ctx.Err()))
case <-after.C:
failpoint.Inject("backOffExecute", func() {
testBackOffExecuteFlag = true
Expand All @@ -77,7 +86,7 @@ func (bo *Backoffer) Exec(
}
}
}
return err
return allErrors
}

// InitialBackoffer make the initial state for retrying.
Expand All @@ -102,6 +111,7 @@ func InitialBackoffer(base, max, total time.Duration) *Backoffer {
},
next: base,
currentTotal: 0,
attempt: 0,
}
}

Expand Down Expand Up @@ -141,6 +151,7 @@ func (bo *Backoffer) exponentialInterval() time.Duration {
func (bo *Backoffer) resetBackoff() {
bo.next = bo.base
bo.currentTotal = 0
bo.attempt = 0
}

// Only used for test.
Expand Down
1 change: 1 addition & 0 deletions client/retry/backoff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func TestBackoffer(t *testing.T) {
return expectedErr
})
re.InDelta(total, time.Since(start), float64(250*time.Millisecond))
re.ErrorContains(err, "test; test; test; test")
re.ErrorIs(err, expectedErr)
re.Equal(4, execCount)
re.True(isBackofferReset(bo))
Expand Down

0 comments on commit 2c20897

Please sign in to comment.