diff --git a/CHANGELOG.md b/CHANGELOG.md index 56360e30151..65cc777c8a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,7 +28,12 @@ and this project adheres to future release. - Go 1.18 support. v0.109.0 will require at least Go 1.19 to build. +### Fixed + +- Unnecessary logging of non-critical statistics errors ([#4850]). + [#2993]: https://github.com/AdguardTeam/AdGuardHome/issues/2993 +[#4850]: https://github.com/AdguardTeam/AdGuardHome/issues/4850 diff --git a/internal/stats/stats.go b/internal/stats/stats.go index e483dbba41f..aea6d92dcb1 100644 --- a/internal/stats/stats.go +++ b/internal/stats/stats.go @@ -385,35 +385,39 @@ func (s *StatsCtx) flush() (cont bool, sleepFor time.Duration) { return true, 0 } + isCommitable := true tx, err := db.Begin(true) if err != nil { log.Error("stats: opening transaction: %s", err) return true, 0 } + defer func() { + if err = finishTxn(tx, isCommitable); err != nil { + log.Error("stats: %s", err) + } + }() s.curr = newUnit(id) - isCommitable := true - ferr := ptr.serialize().flushUnitToDB(tx, ptr.id) - if ferr != nil { - log.Error("stats: flushing unit: %s", ferr) + flushErr := ptr.serialize().flushUnitToDB(tx, ptr.id) + if flushErr != nil { + log.Error("stats: flushing unit: %s", flushErr) isCommitable = false } - derr := tx.DeleteBucket(idToUnitName(id - limit)) - if derr != nil { - log.Error("stats: deleting unit: %s", derr) - if !errors.Is(derr, bbolt.ErrBucketNotFound) { + delErr := tx.DeleteBucket(idToUnitName(id - limit)) + if delErr != nil { + // TODO(e.burkov): Improve the algorithm of deleting the oldest bucket + // to avoid the error. + if errors.Is(delErr, bbolt.ErrBucketNotFound) { + log.Debug("stats: warning: deleting unit: %s", delErr) + } else { isCommitable = false + log.Error("stats: deleting unit: %s", delErr) } } - err = finishTxn(tx, isCommitable) - if err != nil { - log.Error("stats: %s", err) - } - return true, 0 }