Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

retry, client: append errors for backoffer #7896

Merged
merged 5 commits into from
Mar 28, 2024
Merged
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
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, will the log become too long?

}
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
Loading