-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
check.go
202 lines (174 loc) · 6.91 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
/*
Copyright 2017 GitHub Inc.
Licensed under MIT License. See https://github.com/github/freno/blob/master/LICENSE
*/
package throttle
import (
"context"
"fmt"
"net/http"
"strings"
"sync/atomic"
"time"
"vitess.io/vitess/go/stats"
"vitess.io/vitess/go/textutil"
"vitess.io/vitess/go/vt/vttablet/tabletserver/throttle/base"
"vitess.io/vitess/go/vt/vttablet/tabletserver/throttle/throttlerapp"
)
const (
selfCheckInterval = 250 * time.Millisecond
)
// CheckFlags provide hints for a check
type CheckFlags struct {
ReadCheck bool
OverrideThreshold float64
LowPriority bool
OKIfNotExists bool
SkipRequestHeartbeats bool
}
// StandardCheckFlags have no special hints
var StandardCheckFlags = &CheckFlags{}
// ThrottlerCheck provides methods for an app checking on metrics
type ThrottlerCheck struct {
throttler *Throttler
}
// NewThrottlerCheck creates a ThrottlerCheck
func NewThrottlerCheck(throttler *Throttler) *ThrottlerCheck {
return &ThrottlerCheck{
throttler: throttler,
}
}
// checkAppMetricResult allows an app to check on a metric
func (check *ThrottlerCheck) checkAppMetricResult(ctx context.Context, appName string, storeType string, storeName string, metricResultFunc base.MetricResultFunc, flags *CheckFlags) (checkResult *CheckResult) {
// Handle deprioritized app logic
denyApp := false
metricName := fmt.Sprintf("%s/%s", storeType, storeName)
if flags.LowPriority {
if _, exists := check.throttler.nonLowPriorityAppRequestsThrottled.Get(metricName); exists {
// a non-deprioritized app, ie a "normal" app, has recently been throttled.
// This is now a deprioritized app. Deny access to this request.
denyApp = true
}
}
//
metricResult, threshold := check.throttler.AppRequestMetricResult(ctx, appName, metricResultFunc, denyApp)
if flags.OverrideThreshold > 0 {
threshold = flags.OverrideThreshold
}
value, err := metricResult.Get()
if appName == "" {
return NewCheckResult(http.StatusExpectationFailed, value, threshold, fmt.Errorf("no app indicated"))
}
var statusCode int
switch {
case err == base.ErrAppDenied:
// app specifically not allowed to get metrics
statusCode = http.StatusExpectationFailed // 417
case err == base.ErrNoSuchMetric:
// not collected yet, or metric does not exist
statusCode = http.StatusNotFound // 404
case err != nil:
// any error
statusCode = http.StatusInternalServerError // 500
case value > threshold:
// casual throttling
statusCode = http.StatusTooManyRequests // 429
err = base.ErrThresholdExceeded
if !flags.LowPriority && !flags.ReadCheck && throttlerapp.VitessName.Equals(appName) {
// low priority requests will henceforth be denied
go check.throttler.nonLowPriorityAppRequestsThrottled.SetDefault(metricName, true)
}
default:
// all good!
statusCode = http.StatusOK // 200
}
return NewCheckResult(statusCode, value, threshold, err)
}
// Check is the core function that runs when a user wants to check a metric
func (check *ThrottlerCheck) Check(ctx context.Context, appName string, storeType string, storeName string, remoteAddr string, flags *CheckFlags) (checkResult *CheckResult) {
var metricResultFunc base.MetricResultFunc
switch storeType {
case "mysql":
{
metricResultFunc = func() (metricResult base.MetricResult, threshold float64) {
return check.throttler.getMySQLClusterMetrics(ctx, storeName)
}
}
}
if metricResultFunc == nil {
return NoSuchMetricCheckResult
}
checkResult = check.checkAppMetricResult(ctx, appName, storeType, storeName, metricResultFunc, flags)
atomic.StoreInt64(&check.throttler.lastCheckTimeNano, time.Now().UnixNano())
go func(statusCode int) {
stats.GetOrNewCounter("ThrottlerCheckAnyTotal", "total number of checks").Add(1)
stats.GetOrNewCounter(fmt.Sprintf("ThrottlerCheckAny%s%sTotal", textutil.SingleWordCamel(storeType), textutil.SingleWordCamel(storeName)), "").Add(1)
if statusCode != http.StatusOK {
stats.GetOrNewCounter("ThrottlerCheckAnyError", "total number of failed checks").Add(1)
stats.GetOrNewCounter(fmt.Sprintf("ThrottlerCheckAny%s%sError", textutil.SingleWordCamel(storeType), textutil.SingleWordCamel(storeName)), "").Add(1)
}
check.throttler.markRecentApp(appName, remoteAddr)
}(checkResult.StatusCode)
return checkResult
}
func (check *ThrottlerCheck) splitMetricTokens(metricName string) (storeType string, storeName string, err error) {
metricTokens := strings.Split(metricName, "/")
if len(metricTokens) != 2 {
return storeType, storeName, base.ErrNoSuchMetric
}
storeType = metricTokens[0]
storeName = metricTokens[1]
return storeType, storeName, nil
}
// localCheck
func (check *ThrottlerCheck) localCheck(ctx context.Context, metricName string) (checkResult *CheckResult) {
storeType, storeName, err := check.splitMetricTokens(metricName)
if err != nil {
return NoSuchMetricCheckResult
}
checkResult = check.Check(ctx, throttlerapp.VitessName.String(), storeType, storeName, "local", StandardCheckFlags)
if checkResult.StatusCode == http.StatusOK {
check.throttler.markMetricHealthy(metricName)
}
if timeSinceHealthy, found := check.throttler.timeSinceMetricHealthy(metricName); found {
stats.GetOrNewGauge(fmt.Sprintf("ThrottlerCheck%s%sSecondsSinceHealthy", textutil.SingleWordCamel(storeType), textutil.SingleWordCamel(storeName)), fmt.Sprintf("seconds since last healthy cehck for %s.%s", storeType, storeName)).Set(int64(timeSinceHealthy.Seconds()))
}
return checkResult
}
func (check *ThrottlerCheck) reportAggregated(metricName string, metricResult base.MetricResult) {
storeType, storeName, err := check.splitMetricTokens(metricName)
if err != nil {
return
}
if value, err := metricResult.Get(); err == nil {
stats.GetOrNewGaugeFloat64(fmt.Sprintf("ThrottlerAggregated%s%s", textutil.SingleWordCamel(storeType), textutil.SingleWordCamel(storeName)), fmt.Sprintf("aggregated value for %s.%s", storeType, storeName)).Set(value)
}
}
// AggregatedMetrics is a convenience access method into throttler's `aggregatedMetricsSnapshot`
func (check *ThrottlerCheck) AggregatedMetrics(ctx context.Context) map[string]base.MetricResult {
return check.throttler.aggregatedMetricsSnapshot()
}
// MetricsHealth is a convenience access method into throttler's `metricsHealthSnapshot`
func (check *ThrottlerCheck) MetricsHealth() map[string](*base.MetricHealth) {
return check.throttler.metricsHealthSnapshot()
}
// SelfChecks runs checks on all known metrics as if we were an app.
// This runs asynchronously, continuously, and independently of any user interaction
func (check *ThrottlerCheck) SelfChecks(ctx context.Context) {
selfCheckTicker := time.NewTicker(selfCheckInterval)
go func() {
for {
select {
case <-ctx.Done():
return
case <-selfCheckTicker.C:
for metricName, metricResult := range check.AggregatedMetrics(ctx) {
metricName := metricName
metricResult := metricResult
go check.localCheck(ctx, metricName)
go check.reportAggregated(metricName, metricResult)
}
}
}
}()
}