forked from hashicorp/consul-esm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.go
480 lines (408 loc) · 13.3 KB
/
check.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
package main
import (
"crypto/tls"
"errors"
"fmt"
"reflect"
"strings"
"sync"
"time"
"github.com/armon/go-metrics"
consulchecks "github.com/hashicorp/consul/agent/checks"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/lib"
"github.com/hashicorp/consul/types"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-hclog"
)
const externalCheckName = "externalNodeHealth"
var (
// defaultInterval is the check interval to use if one is not set.
defaultInterval = 30 * time.Second
)
type checkIDSet map[types.CheckID]bool
type CheckRunner struct {
sync.RWMutex
logger hclog.Logger
client *api.Client
// checks are unmodified checks as retrieved from Consul Catalog
checks map[types.CheckID]*esmHealthCheck
// checksHTTP & checksTCP are HTTP/TCP checks that are run by ESM.
// They have potentially modified values from the Consul Catalog checks
checksHTTP map[types.CheckID]*consulchecks.CheckHTTP
checksTCP map[types.CheckID]*consulchecks.CheckTCP
checksCritical map[types.CheckID]time.Time
// Used to track checks that are being deferred
deferCheck map[types.CheckID]*time.Timer
CheckUpdateInterval time.Duration
MinimumInterval time.Duration
tlsConfig *tls.Config
PassingThreshold int
CriticalThreshold int
}
type esmHealthCheck struct {
api.HealthCheck
failureCounter int
successCounter int
}
func NewCheckRunner(logger hclog.Logger, client *api.Client, updateInterval,
minimumInterval time.Duration, tlsConfig *tls.Config, passingThreshold int,
criticalThreshold int) *CheckRunner {
return &CheckRunner{
logger: logger,
client: client,
checks: make(map[types.CheckID]*esmHealthCheck),
checksHTTP: make(map[types.CheckID]*consulchecks.CheckHTTP),
checksTCP: make(map[types.CheckID]*consulchecks.CheckTCP),
checksCritical: make(map[types.CheckID]time.Time),
deferCheck: make(map[types.CheckID]*time.Timer),
CheckUpdateInterval: updateInterval,
MinimumInterval: minimumInterval,
tlsConfig: tlsConfig,
PassingThreshold: passingThreshold,
CriticalThreshold: criticalThreshold,
}
}
func (c *CheckRunner) Stop() {
c.Lock()
defer c.Unlock()
for _, check := range c.checksHTTP {
check.Stop()
}
for _, check := range c.checksTCP {
check.Stop()
}
}
// Update an HTTP check
func (c *CheckRunner) updateCheckHTTP(latestCheck *api.HealthCheck, checkHash types.CheckID,
definition *api.HealthCheckDefinition, updated, added checkIDSet) bool {
tlsConfig := c.tlsConfig.Clone()
tlsConfig.InsecureSkipVerify = definition.TLSSkipVerify
http := &consulchecks.CheckHTTP{
Notify: c,
CheckID: checkHash,
HTTP: definition.HTTP,
Header: definition.Header,
Method: definition.Method,
Interval: definition.IntervalDuration,
Timeout: definition.TimeoutDuration,
Logger: c.logger.StandardLogger(&hclog.StandardLoggerOptions{
InferLevels: true,
}),
TLSClientConfig: tlsConfig,
}
if check, checkExists := c.checks[checkHash]; checkExists {
httpCheck, httpCheckExists := c.checksHTTP[checkHash]
if httpCheckExists &&
httpCheck.HTTP == http.HTTP &&
reflect.DeepEqual(httpCheck.Header, http.Header) &&
httpCheck.Method == http.Method &&
httpCheck.TLSClientConfig.InsecureSkipVerify == http.TLSClientConfig.InsecureSkipVerify &&
httpCheck.Interval == http.Interval &&
httpCheck.Timeout == http.Timeout &&
check.Definition.DeregisterCriticalServiceAfter == definition.DeregisterCriticalServiceAfter {
return false
}
c.logger.Info("Updating HTTP check", "checkHash", checkHash)
if httpCheckExists {
httpCheck.Stop()
} else {
tcpCheck, tcpCheckExists := c.checksTCP[checkHash]
if !tcpCheckExists {
c.logger.Warn("Inconsistency check is not TCP and HTTP", "checkHash", checkHash)
return false
}
tcpCheck.Stop()
delete(c.checksTCP, checkHash)
}
updated[checkHash] = true
} else {
c.logger.Debug("Added HTTP check", "checkHash", checkHash)
added[checkHash] = true
}
http.Start()
c.checksHTTP[checkHash] = http
return true
}
func (c *CheckRunner) updateCheckTCP(latestCheck *api.HealthCheck, checkHash types.CheckID,
definition *api.HealthCheckDefinition, updated, added checkIDSet) bool {
tcp := &consulchecks.CheckTCP{
Notify: c,
CheckID: checkHash,
TCP: definition.TCP,
Interval: definition.IntervalDuration,
Timeout: definition.TimeoutDuration,
Logger: c.logger.StandardLogger(&hclog.StandardLoggerOptions{
InferLevels: true,
}),
}
if check, checkExists := c.checks[checkHash]; checkExists {
tcpCheck, tcpCheckExists := c.checksTCP[checkHash]
if tcpCheckExists &&
tcpCheck.TCP == tcp.TCP &&
tcpCheck.Interval == tcp.Interval &&
tcpCheck.Timeout == tcp.Timeout &&
check.Definition.DeregisterCriticalServiceAfter == definition.DeregisterCriticalServiceAfter {
return false
}
c.logger.Info("Updating TCP check", "checkHash", checkHash)
if tcpCheckExists {
tcpCheck.Stop()
} else {
httpCheck, httpCheckExists := c.checksHTTP[checkHash]
if !httpCheckExists {
c.logger.Warn("Inconsistency check is not TCP and HTTP", "checkHash", checkHash)
return false
}
httpCheck.Stop()
delete(c.checksHTTP, checkHash)
}
updated[checkHash] = true
} else {
c.logger.Debug("Added TCP check", "checkHash", checkHash)
added[checkHash] = true
}
tcp.Start()
c.checksTCP[checkHash] = tcp
return true
}
// UpdateChecks takes a list of checks from the catalog and updates
// our list of running checks to match.
func (c *CheckRunner) UpdateChecks(checks api.HealthChecks) {
defer metrics.MeasureSince([]string{"checks", "update"}, time.Now())
c.Lock()
defer c.Unlock()
found := make(checkIDSet)
added := make(checkIDSet)
updated := make(checkIDSet)
removed := make(checkIDSet)
for _, check := range checks {
// Skip the ping-based node check since we're managing that separately
if check.CheckID == externalCheckName {
continue
}
checkHash := checkHash(check)
// create a copy of the definition that will be modified
definition := check.Definition
if definition.IntervalDuration == 0 {
definition.IntervalDuration = defaultInterval
}
// here we verify that the interval is not less then the minimum
if definition.IntervalDuration < c.MinimumInterval {
definition.IntervalDuration = c.MinimumInterval
}
anyUpdates := false
if definition.HTTP != "" {
anyUpdates = c.updateCheckHTTP(check, checkHash, &definition, updated, added)
} else if definition.TCP != "" {
anyUpdates = c.updateCheckTCP(check, checkHash, &definition, updated, added)
} else {
c.logger.Warn("check is not a valid HTTP or TCP check", "checkHash", checkHash)
continue
}
// if we had to fix the interval and we had to update the service, put some trace out
unmodifiedDef := check.Definition
if anyUpdates && unmodifiedDef.IntervalDuration < c.MinimumInterval {
c.logger.Warn("Check interval too low", "interval", unmodifiedDef.Interval, "check", check.Name)
}
found[checkHash] = true
updatedCheck := &esmHealthCheck{
*check,
0,
0,
}
if previousCheck, ok := c.checks[checkHash]; ok {
updatedCheck.failureCounter = previousCheck.failureCounter
updatedCheck.successCounter = previousCheck.successCounter
}
c.checks[checkHash] = updatedCheck
}
// Look for removed checks
for _, check := range c.checks {
checkHash := checkHash(&check.HealthCheck)
if _, ok := found[checkHash]; !ok {
c.logger.Debug("Deleting check %q", "checkHash", checkHash)
delete(c.checks, checkHash)
delete(c.checksCritical, checkHash)
if httpCheck, httpCheckExists := c.checksHTTP[checkHash]; httpCheckExists {
httpCheck.Stop()
delete(c.checksHTTP, checkHash)
}
if tcpCheck, tcpCheckExists := c.checksTCP[checkHash]; tcpCheckExists {
tcpCheck.Stop()
delete(c.checksTCP, checkHash)
}
removed[checkHash] = true
}
}
if len(added) > 0 || len(updated) > 0 || len(removed) > 0 {
c.logger.Info("Updated checks", "count",
len(checks), "found", len(found), "added", len(added), "updated", len(updated), "removed", len(removed))
}
}
// UpdateCheck handles the output of an HTTP/TCP check and decides whether or not
// to push an update to the catalog.
func (c *CheckRunner) UpdateCheck(checkID types.CheckID, status, output string) {
c.Lock()
defer c.Unlock()
check, ok := c.checks[checkID]
if !ok {
return
}
// Do nothing if update is idempotent
if check.Status == status && check.Output == output {
check.failureCounter = decrementCounter(check.failureCounter)
check.successCounter = decrementCounter(check.successCounter)
return
}
if status == api.HealthCritical {
if check.failureCounter < c.CriticalThreshold {
check.failureCounter++
return
}
check.failureCounter = 0
} else {
if check.successCounter < c.PassingThreshold {
check.successCounter++
return
}
check.successCounter = 0
}
// Update the critical time tracking
if status == api.HealthCritical {
if _, ok := c.checksCritical[checkID]; !ok {
c.checksCritical[checkID] = time.Now()
}
} else {
delete(c.checksCritical, checkID)
}
// Defer a sync if the output has changed. This is an optimization around
// frequent updates of output. Instead, we update the output internally,
// and periodically do a write-back to the servers. If there is a status
// change we do the write immediately.
if c.CheckUpdateInterval > 0 && check.Status == status {
check.Output = output
if _, ok := c.deferCheck[checkID]; !ok {
intv := time.Duration(uint64(c.CheckUpdateInterval)/2) + lib.RandomStagger(c.CheckUpdateInterval)
deferSync := time.AfterFunc(intv, func() {
c.Lock()
c.handleCheckUpdate(&check.HealthCheck, status, output)
delete(c.deferCheck, checkID)
c.Unlock()
})
c.deferCheck[checkID] = deferSync
}
return
}
c.handleCheckUpdate(&check.HealthCheck, status, output)
}
// handleCheckUpdate writes a check's status to the catalog and updates the local check state.
// Should only be called when the lock is held.
func (c *CheckRunner) handleCheckUpdate(check *api.HealthCheck, status, output string) {
// Exit early if the check or node have been deregistered.
// consistent mode reduces convergency time particularly when services have many updates in a short time
checks, _, err := c.client.Health().Node(check.Node, &api.QueryOptions{RequireConsistent: true})
if err != nil {
c.logger.Warn("error retrieving existing node entry", "error", err)
return
}
var existing *api.HealthCheck
checkID := strings.TrimPrefix(string(check.CheckID), check.Node+"/")
for _, check := range checks {
if check.CheckID == checkID {
existing = check
break
}
}
if existing == nil {
return
}
existing.Status = status
existing.Output = output
c.logger.Info("Updating output and status for", "checkID", existing.CheckID)
ops := api.TxnOps{
&api.TxnOp{
Check: &api.CheckTxnOp{
Verb: api.CheckCAS,
Check: *existing,
},
},
}
metrics.IncrCounter([]string{"check", "txn"}, 1)
ok, resp, _, err := c.client.Txn().Txn(ops, nil)
if err != nil {
c.logger.Warn("Error updating check status in Consul", "error", err)
return
}
if len(resp.Errors) > 0 {
var errs error
for _, e := range resp.Errors {
errs = multierror.Append(errs, errors.New(e.What))
}
c.logger.Warn("Error(s) returned from txn when updating check status in Consul", "error", errs)
return
}
if !ok {
c.logger.Warn("Failed to atomically update check status in Consul")
return
}
c.logger.Trace("Registered check status to the catalog with ID", "checkId", strings.TrimPrefix(string(check.CheckID), check.Node+"/"))
// Only update the local check state if we successfully updated the catalog
check.Status = status
check.Output = output
}
// reapServices is a long running goroutine that looks for checks that have been
// critical too long and deregisters their associated services.
func (c *CheckRunner) reapServices(shutdownCh <-chan struct{}) {
for {
select {
case <-time.After(30 * time.Second):
c.reapServicesInternal()
case <-shutdownCh:
return
}
}
}
// reapServicesInternal does a single pass, looking for services to reap.
func (c *CheckRunner) reapServicesInternal() {
c.Lock()
defer c.Unlock()
reaped := make(map[string]bool)
for checkID, criticalTime := range c.checksCritical {
check := c.checks[checkID]
serviceID := check.ServiceID
// There's nothing to do if there's no service.
if serviceID == "" {
continue
}
// There might be multiple checks for one service, so
// we don't need to reap multiple times.
if reaped[serviceID] {
continue
}
timeout := check.Definition.DeregisterCriticalServiceAfterDuration
if timeout > 0 && timeout < time.Since(criticalTime) {
c.client.Catalog().Deregister(&api.CatalogDeregistration{
Node: check.Node,
ServiceID: serviceID,
}, nil)
c.logger.Info("agent has been critical for too long, deregistered service", "checkID", checkID,
"serviceID", serviceID,
"duration", time.Since(criticalTime),
"timeout", timeout)
reaped[serviceID] = true
}
}
}
func checkHash(check *api.HealthCheck) types.CheckID {
if check.ServiceID != "" {
return types.CheckID(fmt.Sprintf("%s/%s/%s", check.Node, check.ServiceID, check.CheckID))
}
return types.CheckID(fmt.Sprintf("%s/%s", check.Node, check.CheckID))
}
func decrementCounter(count int) int {
if count == 0 {
return 0
}
return count - 1
}