forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
worker.go
307 lines (269 loc) · 7.5 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package health
import (
"context"
"errors"
"fmt"
"maps"
"slices"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
"github.com/MetalBlockchain/metalgo/utils"
"github.com/MetalBlockchain/metalgo/utils/logging"
"github.com/MetalBlockchain/metalgo/utils/set"
)
var (
allTags = []string{AllTag}
errRestrictedTag = errors.New("restricted tag")
errDuplicateCheck = errors.New("duplicated check")
)
type worker struct {
log logging.Logger
namespace string
metrics *metrics
checksLock sync.RWMutex
checks map[string]*taggedChecker
resultsLock sync.RWMutex
results map[string]Result
numFailingApplicationChecks int
tags map[string]set.Set[string] // tag -> set of check names
startOnce sync.Once
closeOnce sync.Once
wg sync.WaitGroup
closer chan struct{}
}
type taggedChecker struct {
checker Checker
isApplicationCheck bool
tags []string
}
func newWorker(
log logging.Logger,
namespace string,
registerer prometheus.Registerer,
) (*worker, error) {
metrics, err := newMetrics(namespace, registerer)
return &worker{
log: log,
namespace: namespace,
metrics: metrics,
checks: make(map[string]*taggedChecker),
results: make(map[string]Result),
closer: make(chan struct{}),
tags: make(map[string]set.Set[string]),
}, err
}
func (w *worker) RegisterCheck(name string, check Checker, tags ...string) error {
// We ensure [AllTag] isn't contained in [tags] to prevent metrics from
// double counting.
if slices.Contains(tags, AllTag) {
return fmt.Errorf("%w: %q", errRestrictedTag, AllTag)
}
w.checksLock.Lock()
defer w.checksLock.Unlock()
if _, ok := w.checks[name]; ok {
return fmt.Errorf("%w: %q", errDuplicateCheck, name)
}
w.resultsLock.Lock()
defer w.resultsLock.Unlock()
// Add the check to each tag
for _, tag := range tags {
names := w.tags[tag]
names.Add(name)
w.tags[tag] = names
}
// Add the special AllTag descriptor
names := w.tags[AllTag]
names.Add(name)
w.tags[AllTag] = names
applicationChecks := w.tags[ApplicationTag]
tc := &taggedChecker{
checker: check,
isApplicationCheck: applicationChecks.Contains(name),
tags: tags,
}
w.checks[name] = tc
w.results[name] = notYetRunResult
// Whenever a new check is added - it is failing
w.log.Info("registered new check and initialized its state to failing",
zap.String("namespace", w.namespace),
zap.String("name", name),
zap.Strings("tags", tags),
)
// If this is a new application-wide check, then all of the registered tags
// now have one additional failing check.
w.updateMetrics(tc, false /*=healthy*/, true /*=register*/)
return nil
}
func (w *worker) RegisterMonotonicCheck(name string, checker Checker, tags ...string) error {
var result utils.Atomic[any]
return w.RegisterCheck(name, CheckerFunc(func(ctx context.Context) (any, error) {
details := result.Get()
if details != nil {
return details, nil
}
details, err := checker.HealthCheck(ctx)
if err == nil {
result.Set(details)
}
return details, err
}), tags...)
}
func (w *worker) Results(tags ...string) (map[string]Result, bool) {
w.resultsLock.RLock()
defer w.resultsLock.RUnlock()
// if no tags are specified, return all checks
if len(tags) == 0 {
tags = allTags
}
names := set.Set[string]{}
tagSet := set.Of(tags...)
tagSet.Add(ApplicationTag) // we always want to include the application tag
for tag := range tagSet {
if set, ok := w.tags[tag]; ok {
names.Union(set)
}
}
results := make(map[string]Result, names.Len())
healthy := true
for name := range names {
if result, ok := w.results[name]; ok {
results[name] = result
healthy = healthy && result.Error == nil
}
}
return results, healthy
}
func (w *worker) Start(ctx context.Context, freq time.Duration) {
w.startOnce.Do(func() {
detachedCtx := context.WithoutCancel(ctx)
w.wg.Add(1)
go func() {
ticker := time.NewTicker(freq)
defer func() {
ticker.Stop()
w.wg.Done()
}()
w.runChecks(detachedCtx)
for {
select {
case <-ticker.C:
w.runChecks(detachedCtx)
case <-w.closer:
return
}
}
}()
})
}
func (w *worker) Stop() {
w.closeOnce.Do(func() {
close(w.closer)
w.wg.Wait()
})
}
func (w *worker) runChecks(ctx context.Context) {
w.checksLock.RLock()
// Copy the [w.checks] map to collect the checks that we will be running
// during this iteration. If [w.checks] is modified during this iteration of
// [runChecks], then the added check will not be run until the next
// iteration.
checks := maps.Clone(w.checks)
w.checksLock.RUnlock()
var wg sync.WaitGroup
wg.Add(len(checks))
for name, check := range checks {
go w.runCheck(ctx, &wg, name, check)
}
wg.Wait()
}
func (w *worker) runCheck(ctx context.Context, wg *sync.WaitGroup, name string, check *taggedChecker) {
defer wg.Done()
start := time.Now()
// To avoid any deadlocks when [RegisterCheck] is called with a lock
// that is grabbed by [check.HealthCheck], we ensure that no locks
// are held when [check.HealthCheck] is called.
details, err := check.checker.HealthCheck(ctx)
end := time.Now()
result := Result{
Details: details,
Timestamp: end,
Duration: end.Sub(start),
}
w.resultsLock.Lock()
defer w.resultsLock.Unlock()
prevResult := w.results[name]
if err != nil {
errString := err.Error()
result.Error = &errString
result.ContiguousFailures = prevResult.ContiguousFailures + 1
if prevResult.ContiguousFailures > 0 {
result.TimeOfFirstFailure = prevResult.TimeOfFirstFailure
} else {
result.TimeOfFirstFailure = &end
}
if prevResult.Error == nil {
w.log.Warn("check started failing",
zap.String("namespace", w.namespace),
zap.String("name", name),
zap.Strings("tags", check.tags),
zap.Error(err),
)
w.updateMetrics(check, false /*=healthy*/, false /*=register*/)
}
} else if prevResult.Error != nil {
w.log.Info("check started passing",
zap.String("namespace", w.namespace),
zap.String("name", name),
zap.Strings("tags", check.tags),
)
w.updateMetrics(check, true /*=healthy*/, false /*=register*/)
}
w.results[name] = result
}
// updateMetrics updates the metrics for the given check. If [healthy] is true,
// then the check is considered healthy and the metrics are decremented.
// Otherwise, the check is considered unhealthy and the metrics are incremented.
// [register] must be true only if this is the first time the check is being
// registered.
func (w *worker) updateMetrics(tc *taggedChecker, healthy bool, register bool) {
if tc.isApplicationCheck {
// Note: [w.tags] will include AllTag.
for tag := range w.tags {
gauge := w.metrics.failingChecks.WithLabelValues(tag)
if healthy {
gauge.Dec()
} else {
gauge.Inc()
}
}
if healthy {
w.numFailingApplicationChecks--
} else {
w.numFailingApplicationChecks++
}
} else {
for _, tag := range tc.tags {
gauge := w.metrics.failingChecks.WithLabelValues(tag)
if healthy {
gauge.Dec()
} else {
gauge.Inc()
// If this is the first time this tag was registered, we also need to
// account for the currently failing application-wide checks.
if register && w.tags[tag].Len() == 1 {
gauge.Add(float64(w.numFailingApplicationChecks))
}
}
}
gauge := w.metrics.failingChecks.WithLabelValues(AllTag)
if healthy {
gauge.Dec()
} else {
gauge.Inc()
}
}
}