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: support to log after execing some times #7895

Merged
merged 8 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 12 additions & 6 deletions client/retry/backoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ import (
// Option is used to customize the backoffer.
type Option func(*Backoffer)

// WithLogTimes sets the number of retries required to print a log.
func WithLogTimes(logTimes int) Option {
// withMinLogInterval sets the mininum log interval for retrying.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// withMinLogInterval sets the mininum log interval for retrying.
// withMinLogInterval sets the minimum log interval for retrying.

Copy link
Member

Choose a reason for hiding this comment

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

maybe can add more comments/change this function name, It seems unrelated to the minimum?

func withMinLogInterval(interval time.Duration) Option {
return func(bo *Backoffer) {
bo.logTimes = logTimes
bo.logInterval = interval
}
}

Expand All @@ -48,8 +48,10 @@ type Backoffer struct {
// retryableChecker is used to check if the error is retryable.
// By default, all errors are retryable.
retryableChecker func(err error) bool
// logTimes defines the number of retries required to print a log
logTimes int
// logInterval defines the log interval for retrying.
logInterval time.Duration
// nextLogTime is used to record the next log time.
nextLogTime time.Duration

attempt int
next time.Duration
Expand All @@ -71,14 +73,16 @@ func (bo *Backoffer) Exec(
err = fn()
bo.attempt++
if err != nil {
if bo.logTimes > 0 && bo.attempt%bo.logTimes == 0 {
if bo.logInterval > 0 && bo.nextLogTime >= bo.logInterval {
bo.nextLogTime %= bo.logInterval
log.Warn("call PD API failed and retrying", zap.String("api", fnName), zap.Int("retry-time", bo.attempt), zap.Error(err))
}
}
if !bo.isRetryable(err) {
break
}
currentInterval := bo.nextInterval()
bo.nextLogTime += currentInterval
if after == nil {
after = time.NewTimer(currentInterval)
} else {
Expand Down Expand Up @@ -171,6 +175,8 @@ func (bo *Backoffer) exponentialInterval() time.Duration {
func (bo *Backoffer) resetBackoff() {
bo.next = bo.base
bo.currentTotal = 0
bo.attempt = 0
bo.nextLogTime = 0
}

// Only used for test.
Expand Down
30 changes: 26 additions & 4 deletions client/retry/backoff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,28 @@ func TestBackofferWithLog(t *testing.T) {
lg := newZapTestLogger(conf)
log.ReplaceGlobals(lg.Logger, nil)

bo := InitialBackoffer(time.Millisecond*10, time.Millisecond*100, time.Millisecond*1000, WithLogTimes(10))
bo := InitialBackoffer(time.Millisecond*10, time.Millisecond*100, time.Millisecond*1000, withMinLogInterval(time.Millisecond*100))
err := bo.Exec(ctx, testFn)
re.ErrorIs(err, errTest)

m1 := lg.Message()
rfc := `["call PD API failed and retrying"] [api=testFn] [retry-time=10] [error=test]`
re.Contains(m1, rfc)
ms := lg.Messages()
len1 := len(ms)
re.Len(ms, 9)
HuSharp marked this conversation as resolved.
Show resolved Hide resolved
rfc := `["call PD API failed and retrying"] [api=testFn] [retry-time=13] [error=test]`
re.Contains(ms[len(ms)-1], rfc)
rfc = `["call PD API failed and retrying"] [api=testFn] [retry-time=5] [error=test]`
re.Contains(ms[0], rfc)

bo.resetBackoff()
err = bo.Exec(ctx, testFn)
re.ErrorIs(err, errTest)

ms = lg.Messages()
re.Len(ms, 18)
rfc = `["call PD API failed and retrying"] [api=testFn] [retry-time=13] [error=test]`
re.Contains(ms[len(ms)-1], rfc)
rfc = `["call PD API failed and retrying"] [api=testFn] [retry-time=5] [error=test]`
re.Contains(ms[len1], rfc)
}

var errTest = errors.New("test")
Expand Down Expand Up @@ -165,6 +180,13 @@ func (logger *verifyLogger) Message() string {
return logger.w.messages[len(logger.w.messages)-1]
}

func (logger *verifyLogger) Messages() []string {
if logger.w.messages == nil {
return nil
}
return logger.w.messages
}

func newZapTestLogger(cfg *log.Config, opts ...zap.Option) verifyLogger {
// TestingWriter is used to write to memory.
// Used in the verify logger.
Expand Down
Loading