-
Notifications
You must be signed in to change notification settings - Fork 796
/
worker.go
67 lines (60 loc) · 1.75 KB
/
worker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package ruler
import (
"time"
"github.com/cortexproject/cortex/pkg/util"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
)
var (
blockedWorkers = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "cortex",
Name: "blocked_workers",
Help: "How many workers are waiting on an item to be ready.",
})
workerIdleTime = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "cortex",
Name: "worker_idle_seconds_total",
Help: "How long workers have spent waiting for work.",
})
evalLatency = prometheus.NewHistogram(prometheus.HistogramOpts{
Namespace: "cortex",
Name: "group_evaluation_latency_seconds",
Help: "How far behind the target time each rule group executed.",
Buckets: []float64{.1, .25, .5, 1, 2.5, 5, 10, 25},
})
)
// Worker does a thing until it's told to stop.
type Worker interface {
Run()
Stop()
}
type worker struct {
scheduler *scheduler
ruler *Ruler
}
func newWorker(ruler *Ruler) worker {
return worker{
scheduler: ruler.scheduler,
ruler: ruler,
}
}
func (w *worker) Run() {
for {
waitStart := time.Now()
blockedWorkers.Inc()
level.Debug(util.Logger).Log("msg", "waiting for next work item")
item := w.scheduler.nextWorkItem()
blockedWorkers.Dec()
waitElapsed := time.Now().Sub(waitStart)
if item == nil {
level.Debug(util.Logger).Log("msg", "queue closed and empty; terminating worker")
return
}
evalLatency.Observe(time.Since(item.scheduled).Seconds())
workerIdleTime.Add(waitElapsed.Seconds())
level.Debug(util.Logger).Log("msg", "processing item", "item", item)
w.ruler.Evaluate(item.userID, item)
w.scheduler.workItemDone(*item)
level.Debug(util.Logger).Log("msg", "item handed back to queue", "item", item)
}
}