Fix stats collector spinning CPU if no stats are collected#36609
Conversation
Commit fd0e24b changed the stats collection loop to use a `sleep()` instead of `time.Tick()` in the for-loop. This change caused a regression in situations where no stats are being collected, or an error is hit in the loop (in which case the loop would `continue`, and the `sleep()` is not hit). This patch puts the sleep at the start of the loop to guarantee it's always hit. This will delay the sampling, which is similar to the behavior before fd0e24b. Signed-off-by: Sebastiaan van Stijn <[email protected]>
|
ping @stevvooe @jhowardmsft PTAL |
cpuguy83
left a comment
There was a problem hiding this comment.
Not necessarily horrible, but this delays the initial collection.
What about a goto Sleep?
|
@cpuguy83 I'm not a big fan of The loop is started when the daemon starts, so it would be a 1 second delay once, after the daemon starts. Situation before #36519 was applied (using diff --git a/daemon/stats/collector.go b/daemon/stats/collector.go
index 88e20984b..d8045710a 100644
--- a/daemon/stats/collector.go
+++ b/daemon/stats/collector.go
@@ -90,10 +90,12 @@ func (s *Collector) Run() {
// it will grow enough in first iteration
var pairs []publishersPair
- for {
+ logrus.Debugf("starting to collect stats")
+ for range time.Tick(s.interval) {
// Put sleep at the start so that it will always be hit,
// preventing a tight loop if no stats are collected.
- time.Sleep(s.interval)
+ //time.Sleep(s.interval)
+ logrus.Debugf("collecting stats")
// it does not make sense in the first iteration,
// but saves allocations in further iterationsSituation in this PR ( diff --git a/daemon/stats/collector.go b/daemon/stats/collector.go
index 88e20984b..7f9735293 100644
--- a/daemon/stats/collector.go
+++ b/daemon/stats/collector.go
@@ -90,10 +90,12 @@ func (s *Collector) Run() {
// it will grow enough in first iteration
var pairs []publishersPair
+ logrus.Debugf("starting to collect stats")
for {
// Put sleep at the start so that it will always be hit,
// preventing a tight loop if no stats are collected.
time.Sleep(s.interval)
+ logrus.Debugf("collecting stats")
// it does not make sense in the first iteration,
// but saves allocations in further iterations |
|
The |
Codecov Report
@@ Coverage Diff @@
## master #36609 +/- ##
=========================================
Coverage ? 34.69%
=========================================
Files ? 612
Lines ? 45424
Branches ? 0
=========================================
Hits ? 15759
Misses ? 27603
Partials ? 2062 |
cpuguy83
left a comment
There was a problem hiding this comment.
LGTM since this is happening at daemon start it seems fine.
fixes #36608
Commit fd0e24b (#36519) changed the stats collection loop to use a
sleep()instead oftime.Tick()in the for-loop.This change caused a regression in situations where no stats are being collected, or an error is hit in the loop (in which case the loop would
continue, and thesleep()is not hit).This patch puts the sleep at the start of the loop to guarantee it's always hit.
This will delay the sampling, which is similar to the behavior before fd0e24b.
- Description for the changelog
- A picture of a cute animal (not mandatory but encouraged)