enhance: add retry termination log (#37894)

/kind improvement

Signed-off-by: SimFG <bang.fu@zilliz.com>
This commit is contained in:
SimFG 2024-11-22 11:40:33 +08:00 committed by GitHub
parent bc131a911c
commit cef55eb093
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -47,19 +47,34 @@ func Do(ctx context.Context, fn func() error, opts ...Option) error {
}
if !IsRecoverable(err) {
if errors.IsAny(err, context.Canceled, context.DeadlineExceeded) && lastErr != nil {
isContextErr := errors.IsAny(err, context.Canceled, context.DeadlineExceeded)
log.Warn("retry func failed, not be recoverable",
zap.Uint("retried", i),
zap.Uint("attempt", c.attempts),
zap.Bool("isContextErr", isContextErr),
)
if isContextErr && lastErr != nil {
return lastErr
}
return err
}
if c.isRetryErr != nil && !c.isRetryErr(err) {
log.Warn("retry func failed, not be retryable",
zap.Uint("retried", i),
zap.Uint("attempt", c.attempts),
)
return err
}
deadline, ok := ctx.Deadline()
if ok && time.Until(deadline) < c.sleep {
// to avoid sleep until ctx done
if errors.IsAny(err, context.Canceled, context.DeadlineExceeded) && lastErr != nil {
isContextErr := errors.IsAny(err, context.Canceled, context.DeadlineExceeded)
log.Warn("retry func failed, deadline",
zap.Uint("retried", i),
zap.Uint("attempt", c.attempts),
zap.Bool("isContextErr", isContextErr),
)
if isContextErr && lastErr != nil {
return lastErr
}
return err
@ -70,6 +85,10 @@ func Do(ctx context.Context, fn func() error, opts ...Option) error {
select {
case <-time.After(c.sleep):
case <-ctx.Done():
log.Warn("retry func failed, ctx done",
zap.Uint("retried", i),
zap.Uint("attempt", c.attempts),
)
return lastErr
}
@ -81,6 +100,11 @@ func Do(ctx context.Context, fn func() error, opts ...Option) error {
return nil
}
}
if lastErr != nil {
log.Warn("retry func failed, reach max retry",
zap.Uint("attempt", c.attempts),
)
}
return lastErr
}