From 34fc6663484924466171f46dc320382cf02f360b Mon Sep 17 00:00:00 2001 From: Eugene Burkov Date: Mon, 22 Aug 2022 12:49:43 +0300 Subject: [PATCH] stats: imp code, logging --- CHANGELOG.md | 5 +++++ internal/stats/stats.go | 27 +++++++++++++++------------ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56360e30151..41cfd64739b 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 + +- Excessive logging of a non-critical statistics error ([#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 2504bb336e5..8c2bba766be 100644 --- a/internal/stats/stats.go +++ b/internal/stats/stats.go @@ -385,35 +385,38 @@ 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: warning: %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 { + delErr := tx.DeleteBucket(idToUnitName(id - limit)) + if delErr != nil { logFunc := log.Error - if !errors.Is(derr, bbolt.ErrBucketNotFound) { + // TODO(e.burkov): Improve the algorithm of deleting the oldest bucket + // to avoid the error. + if !errors.Is(delErr, bbolt.ErrBucketNotFound) { isCommitable = false logFunc = log.Debug } - logFunc("stats: deleting unit: %s", derr) - } - err = finishTxn(tx, isCommitable) - if err != nil { - log.Error("stats: %s", err) + logFunc("stats: warning: deleting unit: %s", delErr) } return true, 0