-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathapi.go
More file actions
462 lines (399 loc) · 16.6 KB
/
api.go
File metadata and controls
462 lines (399 loc) · 16.6 KB
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
package http
import (
"encoding/json"
"fmt"
"net/http"
"net/http/pprof"
"os"
"strconv"
"strings"
"time"
"github.com/github/freno/pkg/config"
"github.com/github/freno/pkg/group"
"github.com/github/freno/pkg/throttle"
metrics "github.com/rcrowley/go-metrics"
"github.com/rcrowley/go-metrics/exp"
"github.com/julienschmidt/httprouter"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// API exposes the contract for the throttler's web API
type API interface {
LbCheck(w http.ResponseWriter, _ *http.Request, _ httprouter.Params)
LeaderCheck(w http.ResponseWriter, _ *http.Request, _ httprouter.Params)
ConsensusLeader(w http.ResponseWriter, _ *http.Request, _ httprouter.Params)
ConsensusState(w http.ResponseWriter, _ *http.Request, _ httprouter.Params)
ConsensusStatus(w http.ResponseWriter, _ *http.Request, _ httprouter.Params)
Hostname(w http.ResponseWriter, _ *http.Request, _ httprouter.Params)
WriteCheck(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
WriteCheckIfExists(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
ReadCheck(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
ReadCheckIfExists(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
AggregatedMetrics(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
MetricsHealth(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
PrometheusMetrics(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
ThrottleApp(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
UnthrottleApp(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
ThrottledApps(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
SkipHost(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
SkippedHosts(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
RecoverHost(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
RecentApps(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
Help(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
MemcacheConfig(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
}
var endpoints = []string{} // known API URIs
var okIfNotExistsFlags = &throttle.CheckFlags{OKIfNotExists: true}
type GeneralResponse struct {
StatusCode int
Message string
}
func NewGeneralResponse(statusCode int, message string) *GeneralResponse {
return &GeneralResponse{StatusCode: statusCode, Message: message}
}
// APIImpl implements the API
type APIImpl struct {
throttlerCheck *throttle.ThrottlerCheck
consensusService group.ConsensusService
hostname string
}
// NewAPIImpl creates a new instance of the API implementation
func NewAPIImpl(throttlerCheck *throttle.ThrottlerCheck, consensusService group.ConsensusService) *APIImpl {
api := &APIImpl{
throttlerCheck: throttlerCheck,
consensusService: consensusService,
}
if hostname, err := os.Hostname(); err == nil {
api.hostname = hostname
}
return api
}
// respondGeneric will generate a generic response in the form of {status, message}
// It will set response based on whether request is HEAD/GET and based on given error
func (api *APIImpl) respondGeneric(w http.ResponseWriter, r *http.Request, e error) {
if r.Method == http.MethodGet {
w.Header().Set("Content-Type", "application/json")
}
var generalRespnse *GeneralResponse
if e == nil {
generalRespnse = NewGeneralResponse(http.StatusOK, "OK")
} else {
generalRespnse = NewGeneralResponse(http.StatusInternalServerError, e.Error())
}
w.WriteHeader(generalRespnse.StatusCode)
if r.Method == http.MethodGet {
json.NewEncoder(w).Encode(generalRespnse)
}
}
// LbCheck responds to LbCheck with HTTP 200
func (api *APIImpl) LbCheck(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
api.respondGeneric(w, r, nil)
}
// LeaderCheck responds with HTTP 200 when this node is a raft leader, otherwise 404
// This is a useful check for HAProxy routing
func (api *APIImpl) LeaderCheck(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
statusCode := http.StatusNotFound
if api.consensusService.IsLeader() {
statusCode = http.StatusOK
}
w.WriteHeader(statusCode)
if r.Method == http.MethodGet {
json.NewEncoder(w).Encode(fmt.Sprintf("HTTP %d", statusCode))
}
}
// ConsensusLeader returns the identity of the leader
func (api *APIImpl) ConsensusLeader(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
if leader := api.consensusService.GetLeader(); leader != "" {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(leader)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
}
// ConsensusLeader returns the consensus state of this node
func (api *APIImpl) ConsensusState(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
json.NewEncoder(w).Encode(api.consensusService.GetStateDescription())
}
// ConsensusLeader returns the consensus state of this node
func (api *APIImpl) ConsensusStatus(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(api.consensusService.GetStatus())
}
// Hostname returns the hostname this process executes on
func (api *APIImpl) Hostname(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
if api.hostname != "" {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(api.hostname)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
}
func (api *APIImpl) respondToCheckRequest(w http.ResponseWriter, r *http.Request, checkResult *throttle.CheckResult) {
if r.Method == http.MethodGet {
w.Header().Set("Content-Type", "application/json")
}
w.WriteHeader(checkResult.StatusCode)
if r.Method == http.MethodGet {
json.NewEncoder(w).Encode(checkResult)
}
}
// Check checks whether a collected metric is within its threshold
func (api *APIImpl) check(w http.ResponseWriter, r *http.Request, ps httprouter.Params, flags *throttle.CheckFlags) {
appName := ps.ByName("app")
storeType := ps.ByName("storeType")
storeName := ps.ByName("storeName")
remoteAddr := r.Header.Get("X-Forwarded-For")
if remoteAddr == "" {
remoteAddr = r.RemoteAddr
remoteAddr = strings.Split(remoteAddr, ":")[0]
}
flags.LowPriority = (r.URL.Query().Get("p") == "low")
checkResult := api.throttlerCheck.Check(appName, storeType, storeName, remoteAddr, flags)
if checkResult.StatusCode == http.StatusNotFound && flags.OKIfNotExists {
checkResult.StatusCode = http.StatusOK // 200
}
api.respondToCheckRequest(w, r, checkResult)
}
// WriteCheck
func (api *APIImpl) WriteCheck(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
api.check(w, r, ps, throttle.StandardCheckFlags)
}
// WriteCheckIfExists checks for a metric, but reports an OK if the metric does not exist.
// If the metric does exist, then all usual checks are made.
func (api *APIImpl) WriteCheckIfExists(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
api.check(w, r, ps, okIfNotExistsFlags)
}
func (api *APIImpl) readCheck(w http.ResponseWriter, r *http.Request, ps httprouter.Params, flags *throttle.CheckFlags) {
if overrideThreshold, err := strconv.ParseFloat(ps.ByName("threshold"), 64); err != nil {
api.respondGeneric(w, r, err)
} else {
flags.ReadCheck = true
flags.OverrideThreshold = overrideThreshold
api.check(w, r, ps, flags)
}
}
// ReadCheck
func (api *APIImpl) ReadCheck(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
api.readCheck(w, r, ps, &throttle.CheckFlags{})
}
// ReadCheckIfExists checks for a metric, but reports an OK if the metric does not exist.
// If the metric does exist, then all usual checks are made.
func (api *APIImpl) ReadCheckIfExists(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
api.readCheck(w, r, ps, &throttle.CheckFlags{OKIfNotExists: true})
}
// AggregatedMetrics returns a snapshot of all current aggregated metrics
func (api *APIImpl) AggregatedMetrics(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
brief := (r.URL.Query().Get("brief") == "true")
w.Header().Set("Content-Type", "application/json")
aggregatedMetrics := api.throttlerCheck.AggregatedMetrics()
responseMap := map[string]string{}
for metricName, metric := range aggregatedMetrics {
value, err := metric.Get()
description := ""
if err == nil {
if brief {
description = fmt.Sprintf("%.3f", value)
} else {
description = fmt.Sprintf("%f", value)
}
} else {
description = fmt.Sprintf("error: %s", err.Error())
}
responseMap[metricName] = description
}
json.NewEncoder(w).Encode(responseMap)
}
// MetricsHealth returns the time since last "OK" check per-metric
func (api *APIImpl) MetricsHealth(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Content-Type", "application/json")
metricsHealth := api.throttlerCheck.MetricsHealth()
json.NewEncoder(w).Encode(metricsHealth)
}
func (api *APIImpl) PrometheusMetrics(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
handler := promhttp.Handler()
handler.ServeHTTP(w, r)
}
// ThrottleApp forcibly marks given app as throttled. Future requests by this app may be denied.
func (api *APIImpl) ThrottleApp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
storeName := r.URL.Query().Get("store_name")
var appName string
if storeName != "" {
// limit throttling to this store
appName = fmt.Sprintf("%s/%s", ps.ByName("app"), storeName)
} else {
// default is throttle app globally
appName = ps.ByName("app")
}
var expireAt time.Time // default zero
var ttlMinutes int64
var ratio float64
var err error
if ps.ByName("ttlMinutes") == "" {
ttlMinutes = 0
} else if ttlMinutes, err = strconv.ParseInt(ps.ByName("ttlMinutes"), 10, 64); err != nil {
goto response
}
if ttlMinutes != 0 {
expireAt = time.Now().Add(time.Duration(ttlMinutes) * time.Minute)
}
// if ttlMinutes is zero, we keep expireAt as zero, which is handled in a special way
if ps.ByName("ratio") == "" {
ratio = -1
} else if ratio, err = strconv.ParseFloat(ps.ByName("ratio"), 64); err != nil {
goto response
} else if ratio < 0 || ratio > 1 {
err = fmt.Errorf("ratio must be in [0..1] range; got %+v", ratio)
goto response
}
err = api.consensusService.ThrottleApp(appName, ttlMinutes, expireAt, ratio)
response:
api.respondGeneric(w, r, err)
}
// UnthrottleApp unthrottles given app.
func (api *APIImpl) UnthrottleApp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
storeName := r.URL.Query().Get("store_name")
appName := ps.ByName("app")
if storeName != "" {
appName += "/" + storeName
}
for app := range api.consensusService.ThrottledAppsMap() {
if app == appName {
err := api.consensusService.UnthrottleApp(app)
if err != nil {
api.respondGeneric(w, r, err)
return
}
}
}
api.respondGeneric(w, r, nil)
}
// ThrottledApps returns a snapshot of all currently throttled apps
func (api *APIImpl) ThrottledApps(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Content-Type", "application/json")
throttledApps := api.consensusService.ThrottledAppsMap()
json.NewEncoder(w).Encode(throttledApps)
}
// ThrottledApps returns a snapshot of all currently throttled apps
func (api *APIImpl) RecentApps(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var err error
var lastMinutes int64
if ps.ByName("lastMinutes") != "" {
if lastMinutes, err = strconv.ParseInt(ps.ByName("lastMinutes"), 10, 64); err != nil {
api.respondGeneric(w, r, err)
return
}
}
w.Header().Set("Content-Type", "application/json")
recentApps := api.consensusService.RecentAppsMap()
if lastMinutes > 0 {
for key, recentApp := range recentApps {
if recentApp.MinutesSinceChecked > lastMinutes {
delete(recentApps, key)
}
}
}
json.NewEncoder(w).Encode(recentApps)
}
// ThrottledApps returns a snapshot of all currently throttled apps
func (api *APIImpl) Help(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(endpoints)
}
// MemcacheConfig outputs the memcache configuration being used, so clients can
// implement more efficient access strategies
func (api *APIImpl) MemcacheConfig(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
memcacheConfig := struct {
MemcacheServers []string
MemcachePath string
}{
config.Settings().MemcacheServers,
config.Settings().MemcachePath,
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(memcacheConfig)
}
// register is a wrapper function for accepting both GET and HEAD requests
func register(router *httprouter.Router, path string, f httprouter.Handle) {
router.HEAD(path, f)
router.GET(path, f)
endpoints = append(endpoints, path)
}
func metricsHandle(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
handler := exp.ExpHandler(metrics.DefaultRegistry)
handler.ServeHTTP(w, r)
}
func (api *APIImpl) SkipHost(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var ttlMinutes int64
var expireAt time.Time
var err error
hostName := ps.ByName("hostName")
ttlString := ps.ByName("ttlMinutes")
if ttlString == "" {
ttlMinutes = 0
} else if ttlMinutes, err = strconv.ParseInt(ttlString, 10, 64); err != nil {
api.respondGeneric(w, r, err)
}
if ttlMinutes != 0 {
expireAt = time.Now().Add(time.Duration(ttlMinutes) * time.Minute)
}
err = api.consensusService.SkipHost(hostName, ttlMinutes, expireAt)
api.respondGeneric(w, r, err)
}
func (api *APIImpl) RecoverHost(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
hostName := ps.ByName("hostName")
err := api.consensusService.RecoverHost(hostName)
api.respondGeneric(w, r, err)
}
func (api *APIImpl) SkippedHosts(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(api.consensusService.SkippedHostsMap())
}
// ConfigureRoutes configures a set of HTTP routes to be actions dispatched by the
// given api's methods.
func ConfigureRoutes(api API) *httprouter.Router {
router := httprouter.New()
register(router, "/lb-check", api.LbCheck)
register(router, "/_ping", api.LbCheck)
register(router, "/status", api.LbCheck)
register(router, "/leader-check", api.LeaderCheck)
register(router, "/raft/leader", api.ConsensusLeader)
register(router, "/raft/state", api.ConsensusState)
register(router, "/consensus/leader", api.ConsensusLeader)
register(router, "/consensus/state", api.ConsensusState)
register(router, "/consensus/status", api.ConsensusStatus)
register(router, "/hostname", api.Hostname)
register(router, "/check/:app/:storeType/:storeName", api.WriteCheck)
register(router, "/check-if-exists/:app/:storeType/:storeName", api.WriteCheckIfExists)
register(router, "/check-read/:app/:storeType/:storeName/:threshold", api.ReadCheck)
register(router, "/check-read-if-exists/:app/:storeType/:storeName/:threshold", api.ReadCheckIfExists)
register(router, "/aggregated-metrics", api.AggregatedMetrics)
register(router, "/metrics-health", api.MetricsHealth)
register(router, "/metrics", api.PrometheusMetrics)
register(router, "/throttle-app/:app", api.ThrottleApp)
register(router, "/throttle-app/:app/ratio/:ratio", api.ThrottleApp)
register(router, "/throttle-app/:app/ttl/:ttlMinutes", api.ThrottleApp)
register(router, "/throttle-app/:app/ttl/:ttlMinutes/ratio/:ratio", api.ThrottleApp)
register(router, "/unthrottle-app/:app", api.UnthrottleApp)
register(router, "/throttled-apps", api.ThrottledApps)
register(router, "/recent-apps", api.RecentApps)
register(router, "/recent-apps/:lastMinutes", api.RecentApps)
register(router, "/skip-host/:hostName", api.SkipHost)
register(router, "/skip-host/:hostName/ttl/:ttlMinutes", api.SkipHost)
register(router, "/skipped-hosts", api.SkippedHosts)
register(router, "/recover-host/:hostName", api.RecoverHost)
register(router, "/debug/vars", metricsHandle)
register(router, "/debug/metrics", metricsHandle)
if config.Settings().EnableProfiling {
router.HandlerFunc(http.MethodGet, "/debug/pprof/", pprof.Index)
router.HandlerFunc(http.MethodGet, "/debug/pprof/cmdline", pprof.Cmdline)
router.HandlerFunc(http.MethodGet, "/debug/pprof/profile", pprof.Profile)
router.HandlerFunc(http.MethodGet, "/debug/pprof/symbol", pprof.Symbol)
router.HandlerFunc(http.MethodGet, "/debug/pprof/trace", pprof.Trace)
router.Handler(http.MethodGet, "/debug/pprof/goroutine", pprof.Handler("goroutine"))
router.Handler(http.MethodGet, "/debug/pprof/heap", pprof.Handler("heap"))
router.Handler(http.MethodGet, "/debug/pprof/threadcreate", pprof.Handler("threadcreate"))
router.Handler(http.MethodGet, "/debug/pprof/block", pprof.Handler("block"))
}
register(router, "/help", api.Help)
router.GET("/config/memcache", api.MemcacheConfig)
return router
}