Skip to content

Commit

Permalink
Consolidate default retry timeout and settings
Browse files Browse the repository at this point in the history
  • Loading branch information
lippserd committed Apr 8, 2024
1 parent 8adefda commit 487fa58
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 69 deletions.
2 changes: 1 addition & 1 deletion pkg/config/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func dialWithLogging(dialer ctxDialerFunc, logger *logging.Logger) ctxDialerFunc
retry.Retryable,
backoff.NewExponentialWithJitter(1*time.Millisecond, 1*time.Second),
retry.Settings{
Timeout: 5 * time.Minute,
Timeout: retry.DefaultTimeout,
OnError: func(_ time.Duration, _ uint64, err, lastErr error) {
if lastErr == nil || err.Error() != lastErr.Error() {
logger.Warnw("Can't connect to Redis. Retrying", zap.Error(err))
Expand Down
18 changes: 1 addition & 17 deletions pkg/icingadb/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/icinga/icingadb/pkg/com"
"github.com/icinga/icingadb/pkg/retry"
"github.com/icinga/icingadb/pkg/types"
"go.uber.org/zap"
"time"
)

Expand Down Expand Up @@ -68,22 +67,7 @@ func (db *DB) CleanupOlderThan(
},
retry.Retryable,
backoff.NewExponentialWithJitter(1*time.Millisecond, 1*time.Second),
retry.Settings{
Timeout: 5 * time.Minute,
OnError: func(_ time.Duration, _ uint64, err, lastErr error) {
if lastErr == nil || err.Error() != lastErr.Error() {
db.logger.Warnw("Can't execute query. Retrying", zap.Error(err))
}
},
OnSuccess: func(elapsed time.Duration, attempt uint64, lastErr error) {
if attempt > 0 {
db.logger.Infow("Query retried successfully after error",
zap.Duration("after", elapsed),
zap.Uint64("attempts", attempt+1),
zap.NamedError("recovered_error", lastErr))
}
},
},
db.getDefaultRetrySettings(),
)
if err != nil {
return 0, err
Expand Down
70 changes: 22 additions & 48 deletions pkg/icingadb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,22 +347,7 @@ func (db *DB) BulkExec(
},
retry.Retryable,
backoff.NewExponentialWithJitter(1*time.Millisecond, 1*time.Second),
retry.Settings{
Timeout: 5 * time.Minute,
OnError: func(_ time.Duration, _ uint64, err, lastErr error) {
if lastErr == nil || err.Error() != lastErr.Error() {
db.logger.Warnw("Can't execute query. Retrying", zap.Error(err))
}
},
OnSuccess: func(elapsed time.Duration, attempt uint64, lastErr error) {
if attempt > 0 {
db.logger.Infow("Query retried successfully after error",
zap.Duration("after", elapsed),
zap.Uint64("attempts", attempt+1),
zap.NamedError("recovered_error", lastErr))
}
},
},
db.getDefaultRetrySettings(),
)
}
}(b))
Expand Down Expand Up @@ -427,22 +412,7 @@ func (db *DB) NamedBulkExec(
},
retry.Retryable,
backoff.NewExponentialWithJitter(1*time.Millisecond, 1*time.Second),
retry.Settings{
Timeout: 5 * time.Minute,
OnError: func(_ time.Duration, _ uint64, err, lastErr error) {
if lastErr == nil || err.Error() != lastErr.Error() {
db.logger.Warnw("Can't execute query. Retrying", zap.Error(err))
}
},
OnSuccess: func(elapsed time.Duration, attempt uint64, lastErr error) {
if attempt > 0 {
db.logger.Infow("Query retried successfully after error",
zap.Duration("after", elapsed),
zap.Uint64("attempts", attempt+1),
zap.NamedError("recovered_error", lastErr))
}
},
},
db.getDefaultRetrySettings(),
)
}
}(b))
Expand Down Expand Up @@ -515,22 +485,7 @@ func (db *DB) NamedBulkExecTx(
},
retry.Retryable,
backoff.NewExponentialWithJitter(1*time.Millisecond, 1*time.Second),
retry.Settings{
Timeout: 5 * time.Minute,
OnError: func(_ time.Duration, _ uint64, err, lastErr error) {
if lastErr == nil || err.Error() != lastErr.Error() {
db.logger.Warnw("Can't execute query. Retrying", zap.Error(err))
}
},
OnSuccess: func(elapsed time.Duration, attempt uint64, lastErr error) {
if attempt > 0 {
db.logger.Infow("Query retried successfully after error",
zap.Duration("after", elapsed),
zap.Uint64("attempts", attempt+1),
zap.NamedError("recovered_error", lastErr))
}
},
},
db.getDefaultRetrySettings(),
)
}
}(b))
Expand Down Expand Up @@ -716,6 +671,25 @@ func (db *DB) GetSemaphoreForTable(table string) *semaphore.Weighted {
}
}

func (db *DB) getDefaultRetrySettings() retry.Settings {
return retry.Settings{
Timeout: retry.DefaultTimeout,
OnError: func(_ time.Duration, _ uint64, err, lastErr error) {
if lastErr == nil || err.Error() != lastErr.Error() {
db.logger.Warnw("Can't execute query. Retrying", zap.Error(err))
}
},
OnSuccess: func(elapsed time.Duration, attempt uint64, lastErr error) {
if attempt > 0 {
db.logger.Infow("Query retried successfully after error",
zap.Duration("after", elapsed),
zap.Uint64("attempts", attempt+1),
zap.NamedError("recovered_error", lastErr))
}
},
}
}

func (db *DB) log(ctx context.Context, query string, counter *com.Counter) periodic.Stopper {
return periodic.Start(ctx, db.logger.Interval(), func(tick periodic.Tick) {
if count := counter.Reset(); count > 0 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/icingadb/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (c RetryConnector) Connect(ctx context.Context) (driver.Conn, error) {
shouldRetry,
backoff.NewExponentialWithJitter(time.Millisecond*128, time.Minute*1),
retry.Settings{
Timeout: time.Minute * 5,
Timeout: retry.DefaultTimeout,
OnError: func(_ time.Duration, _ uint64, err, lastErr error) {
telemetry.UpdateCurrentDbConnErr(err)

Expand Down
4 changes: 2 additions & 2 deletions pkg/icingadb/ha.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (h *HA) controller() {
// expiration time. Therefore, we use a deadline ctx to retry.WithBackoff() in realize() which expires earlier
// than our default timeout.
// 2) Since we do not want to exit before our default timeout expires, we have to repeat step 1 until it does.
retryTimeout := time.NewTimer(5 * time.Minute)
retryTimeout := time.NewTimer(retry.DefaultTimeout)
defer retryTimeout.Stop()

for {
Expand Down Expand Up @@ -246,7 +246,7 @@ func (h *HA) controller() {
// But this is the best place to catch all scenarios where the timeout needs to be reset.
// And since HA needs quite a bit of refactoring anyway to e.g. return immediately after calling h.abort(),
// it's fine to have it here for now.
retryTimeout.Reset(5 * time.Minute)
retryTimeout.Reset(retry.DefaultTimeout)
case <-h.heartbeat.Done():
if err := h.heartbeat.Err(); err != nil {
h.abort(err)
Expand Down
3 changes: 3 additions & 0 deletions pkg/retry/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
"time"
)

// DefaultTimeout is our opinionated default timeout for retrying database and Redis operations.
const DefaultTimeout = 5 * time.Minute

// RetryableFunc is a retryable function.
type RetryableFunc func(context.Context) error

Expand Down

0 comments on commit 487fa58

Please sign in to comment.