From 2b3a5d4229d789985993d843df12a67685e145d8 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Tue, 9 Apr 2024 16:02:15 +0200 Subject: [PATCH] `retry`: Set `attempt`'s initial value to `1` This change simplifies the use of `attempt` as a number for reading in log messages and `if`s. Also before, with `attempt` starting with `0`, the second attempt would have been taken immediately, as our backoff implementation returns `0` in this case. Co-Authored-By: Alvar Penning --- pkg/config/redis.go | 4 ++-- pkg/icingadb/db.go | 4 ++-- pkg/icingadb/driver.go | 4 ++-- pkg/icingadb/ha.go | 8 ++++---- pkg/retry/retry.go | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/pkg/config/redis.go b/pkg/config/redis.go index 201c22a9..ad8b31a6 100644 --- a/pkg/config/redis.go +++ b/pkg/config/redis.go @@ -92,9 +92,9 @@ func dialWithLogging(dialer ctxDialerFunc, logger *logging.Logger) ctxDialerFunc } }, OnSuccess: func(elapsed time.Duration, attempt uint64, _ error) { - if attempt > 0 { + if attempt > 1 { logger.Infow("Reconnected to Redis", - zap.Duration("after", elapsed), zap.Uint64("attempts", attempt+1)) + zap.Duration("after", elapsed), zap.Uint64("attempts", attempt)) } }, }, diff --git a/pkg/icingadb/db.go b/pkg/icingadb/db.go index a5bccca6..4c575e3b 100644 --- a/pkg/icingadb/db.go +++ b/pkg/icingadb/db.go @@ -680,10 +680,10 @@ func (db *DB) getDefaultRetrySettings() retry.Settings { } }, OnSuccess: func(elapsed time.Duration, attempt uint64, lastErr error) { - if attempt > 0 { + if attempt > 1 { db.logger.Infow("Query retried successfully after error", zap.Duration("after", elapsed), - zap.Uint64("attempts", attempt+1), + zap.Uint64("attempts", attempt), zap.NamedError("recovered_error", lastErr)) } }, diff --git a/pkg/icingadb/driver.go b/pkg/icingadb/driver.go index 0f3453db..ac5af7e1 100644 --- a/pkg/icingadb/driver.go +++ b/pkg/icingadb/driver.go @@ -66,9 +66,9 @@ func (c RetryConnector) Connect(ctx context.Context) (driver.Conn, error) { OnSuccess: func(elapsed time.Duration, attempt uint64, _ error) { telemetry.UpdateCurrentDbConnErr(nil) - if attempt > 0 { + if attempt > 1 { c.logger.Infow("Reconnected to database", - zap.Duration("after", elapsed), zap.Uint64("attempts", attempt+1)) + zap.Duration("after", elapsed), zap.Uint64("attempts", attempt)) } }, }, diff --git a/pkg/icingadb/ha.go b/pkg/icingadb/ha.go index a6355f31..68a2a68d 100644 --- a/pkg/icingadb/ha.go +++ b/pkg/icingadb/ha.go @@ -390,7 +390,7 @@ func (h *HA) realize( OnRetryableError: func(_ time.Duration, attempt uint64, err, lastErr error) { if lastErr == nil || err.Error() != lastErr.Error() { log := h.logger.Debugw - if attempt > 2 { + if attempt > 3 { log = h.logger.Infow } @@ -398,10 +398,10 @@ func (h *HA) realize( } }, OnSuccess: func(elapsed time.Duration, attempt uint64, lastErr error) { - if attempt > 0 { + if attempt > 1 { log := h.logger.Debugw - if attempt > 3 { + if attempt > 4 { // We log errors with severity info starting from the fourth attempt, (see above) // so we need to log success with severity info from the fifth attempt. log = h.logger.Infow @@ -409,7 +409,7 @@ func (h *HA) realize( log("Instance updated/inserted successfully after error", zap.Duration("after", elapsed), - zap.Uint64("attempts", attempt+1), + zap.Uint64("attempts", attempt), zap.NamedError("recovered_error", lastErr)) } }, diff --git a/pkg/retry/retry.go b/pkg/retry/retry.go index fb3b20b9..171fcc87 100644 --- a/pkg/retry/retry.go +++ b/pkg/retry/retry.go @@ -50,7 +50,7 @@ func WithBackoff( } start := time.Now() - for attempt := uint64(0); ; /* true */ attempt++ { + for attempt := uint64(1); ; /* true */ attempt++ { prevErr := err if err = retryableFunc(ctx); err == nil {