-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy paththrottler.go
More file actions
768 lines (686 loc) · 25.5 KB
/
Copy paththrottler.go
File metadata and controls
768 lines (686 loc) · 25.5 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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
package throttle
import (
"encoding/json"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"io/ioutil"
"math/rand"
"net/http"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/github/freno/pkg/base"
"github.com/github/freno/pkg/config"
"github.com/github/freno/pkg/haproxy"
"github.com/github/freno/pkg/mysql"
"github.com/github/freno/pkg/proxysql"
"github.com/github/freno/pkg/vitess"
"github.com/outbrain/golib/log"
"github.com/patrickmn/go-cache"
"github.com/bradfitz/gomemcache/memcache"
metrics "github.com/rcrowley/go-metrics"
)
const leaderCheckInterval = 1 * time.Second
const mysqlCollectInterval = 50 * time.Millisecond
const mysqlRefreshInterval = 10 * time.Second
const mysqlAggreateInterval = 25 * time.Millisecond
const mysqlHttpCheckInterval = 5 * time.Second
const sharedDomainCollectInterval = 1 * time.Second
const aggregatedMetricsExpiration = 5 * time.Second
const aggregatedMetricsCleanup = 1 * time.Second
const throttledAppsSnapshotInterval = 5 * time.Second
const recentAppsExpiration = time.Hour * 24
const skippedHostsSnapshotInterval = 5 * time.Second
const nonDeprioritizedAppMapExpiration = time.Second
const nonDeprioritizedAppMapInterval = 100 * time.Millisecond
const DefaultThrottleTTLMinutes = 60
const DefaultSkipTTLMinutes = 60
const DefaultThrottleRatio = 1.0
func init() {
rand.Seed(time.Now().UnixNano())
}
type PrometheusMetrics struct {
value *prometheus.GaugeVec
timestamp *prometheus.GaugeVec
throttleThreshold *prometheus.GaugeVec
}
type Throttler struct {
isLeader bool
isLeaderFunc func() bool
sharedDomainServicesFunc func() (map[string]string, error)
mysqlThrottleMetricChan chan *mysql.MySQLThrottleMetric
mysqlHttpCheckChan chan *mysql.MySQLHttpCheck
mysqlInventoryChan chan *mysql.MySQLInventory
mysqlClusterProbesChan chan *mysql.ClusterProbes
mysqlInventory *mysql.MySQLInventory
mysqlClusterThresholds *cache.Cache
aggregatedMetrics *cache.Cache
throttledApps *cache.Cache
skippedHosts *cache.Cache
recentApps *cache.Cache
metricsHealth *cache.Cache
shareDomainMetricHealth *cache.Cache
prometheusMetrics *PrometheusMetrics
memcacheClient *memcache.Client
memcachePath string
proxysqlClient *proxysql.Client
throttledAppsMutex sync.Mutex
skippedHostsMutex sync.Mutex
nonLowPriorityAppRequestsThrottled *cache.Cache
httpClient *http.Client
}
func NewPrometheusMetrics(namespace string, subsystem string) *PrometheusMetrics {
promMetrics := &PrometheusMetrics{
value: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "value",
Help: "Result value for the cluster",
},
[]string{"store_type", "store_name"}),
timestamp: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "last_update_timestamp",
Help: "Timestamp of the last update",
},
[]string{"store_type", "store_name"}),
throttleThreshold: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "throttle_threshold",
Help: "Throtthe Threshold",
}, []string{"store_type", "store_name"}),
}
prometheus.MustRegister(promMetrics.value)
prometheus.MustRegister(promMetrics.timestamp)
prometheus.MustRegister(promMetrics.throttleThreshold)
return promMetrics
}
func NewThrottler() *Throttler {
throttler := &Throttler{
isLeader: false,
mysqlThrottleMetricChan: make(chan *mysql.MySQLThrottleMetric),
mysqlHttpCheckChan: make(chan *mysql.MySQLHttpCheck),
mysqlInventoryChan: make(chan *mysql.MySQLInventory, 1),
mysqlClusterProbesChan: make(chan *mysql.ClusterProbes),
mysqlInventory: mysql.NewMySQLInventory(),
throttledApps: cache.New(cache.NoExpiration, 10*time.Second),
skippedHosts: cache.New(cache.NoExpiration, 10*time.Second),
mysqlClusterThresholds: cache.New(cache.NoExpiration, 0),
aggregatedMetrics: cache.New(aggregatedMetricsExpiration, aggregatedMetricsCleanup),
recentApps: cache.New(recentAppsExpiration, time.Minute),
metricsHealth: cache.New(cache.NoExpiration, 0),
shareDomainMetricHealth: cache.New(5*sharedDomainCollectInterval, sharedDomainCollectInterval),
nonLowPriorityAppRequestsThrottled: cache.New(nonDeprioritizedAppMapExpiration, nonDeprioritizedAppMapInterval),
httpClient: base.SetupHttpClient(0),
}
throttler.ThrottleApp("abusing-app", time.Now().Add(time.Hour*24*365*10), DefaultThrottleRatio)
if memcacheServers := config.Settings().MemcacheServers; len(memcacheServers) > 0 {
throttler.memcacheClient = memcache.New(memcacheServers...)
}
throttler.memcachePath = config.Settings().MemcachePath
if throttler.hasProxySQLStores() {
throttler.proxysqlClient = proxysql.NewClient(mysqlRefreshInterval)
}
throttler.prometheusMetrics = NewPrometheusMetrics(
config.Settings().PrometheusNamespace,
config.Settings().PrometheusSubsystem,
)
return throttler
}
func (throttler *Throttler) SetLeaderFunc(isLeaderFunc func() bool) {
throttler.isLeaderFunc = isLeaderFunc
}
func (throttler *Throttler) SetSharedDomainServicesFunc(sharedDomainServicesFunc func() (map[string]string, error)) {
throttler.sharedDomainServicesFunc = sharedDomainServicesFunc
}
func (throttler *Throttler) ThrottledAppsSnapshot() map[string]cache.Item {
return throttler.throttledApps.Items()
}
func (throttler *Throttler) Operate() {
leaderCheckTick := time.Tick(leaderCheckInterval)
mysqlCollectTick := time.Tick(mysqlCollectInterval)
mysqlRefreshTick := time.Tick(mysqlRefreshInterval)
mysqlAggregateTick := time.Tick(mysqlAggreateInterval)
mysqlHttpCheckTick := time.Tick(mysqlHttpCheckInterval)
throttledAppsTick := time.Tick(throttledAppsSnapshotInterval)
sharedDomainTick := time.Tick(sharedDomainCollectInterval)
skippedHostsTick := time.Tick(skippedHostsSnapshotInterval)
// initial read of inventory:
go throttler.refreshMySQLInventory()
for {
select {
case <-leaderCheckTick:
{
// sparse
throttler.isLeader = throttler.isLeaderFunc()
}
case <-mysqlCollectTick:
{
// frequent
throttler.collectMySQLMetrics()
}
case <-mysqlHttpCheckTick:
{
throttler.collectMySQLHttpChecks()
}
case metric := <-throttler.mysqlThrottleMetricChan:
{
// incoming MySQL metric, frequent, as result of collectMySQLMetrics()
throttler.mysqlInventory.InstanceKeyMetrics[metric.GetClusterInstanceKey()] = metric
}
case httpCheckResult := <-throttler.mysqlHttpCheckChan:
{
// incoming MySQL metric, frequent, as result of collectMySQLMetrics()
throttler.mysqlInventory.ClusterInstanceHttpChecks[httpCheckResult.HashKey()] = httpCheckResult.CheckResult
}
case <-mysqlRefreshTick:
{
// sparse
go throttler.refreshMySQLInventory()
}
case <-sharedDomainTick:
{
go throttler.collectShareDomainMetricHealth()
}
case probes := <-throttler.mysqlClusterProbesChan:
{
// incoming structural update, sparse, as result of refreshMySQLInventory()
throttler.updateMySQLClusterProbes(probes)
}
case <-mysqlAggregateTick:
{
throttler.aggregateMySQLMetrics()
}
case <-throttledAppsTick:
{
go throttler.expireThrottledApps()
go throttler.pushStatusToExpVar()
}
case <-skippedHostsTick:
{
go throttler.expireSkippedHosts()
}
}
if !throttler.isLeader {
time.Sleep(1 * time.Second)
}
}
}
func (throttler *Throttler) hasProxySQLStores() bool {
for _, clusterSettings := range config.Settings().Stores.MySQL.Clusters {
if !clusterSettings.ProxySQLSettings.IsEmpty() {
return true
}
}
return false
}
func (throttler *Throttler) collectMySQLMetrics() error {
if !throttler.isLeader {
return nil
}
// synchronously, get lists of probes
for clusterName, probes := range throttler.mysqlInventory.ClustersProbes {
clusterName := clusterName
probes := probes
go func() {
// probes is known not to change. It can be *replaced*, but not changed.
// so it's safe to iterate it
for _, probe := range *probes {
probe := probe
go func() {
// Avoid querying the same server twice at the same time. If previous read is still there,
// we avoid re-reading it.
if !atomic.CompareAndSwapInt64(&probe.QueryInProgress, 0, 1) {
return
}
defer atomic.StoreInt64(&probe.QueryInProgress, 0)
throttleMetrics := mysql.ReadThrottleMetric(probe, clusterName)
throttler.mysqlThrottleMetricChan <- throttleMetrics
}()
}
}()
}
return nil
}
func (throttler *Throttler) collectMySQLHttpChecks() error {
if !throttler.isLeader {
return nil
}
// synchronously, get lists of probes
for clusterName, probes := range throttler.mysqlInventory.ClustersProbes {
clusterName := clusterName
probes := probes
go func() {
// probes is known not to change. It can be *replaced*, but not changed.
// so it's safe to iterate it
for _, probe := range *probes {
probe := probe
go func() {
// Avoid querying the same server twice at the same time. If previous read is still there,
// we avoid re-reading it.
if !atomic.CompareAndSwapInt64(&probe.HttpCheckInProgress, 0, 1) {
return
}
defer atomic.StoreInt64(&probe.HttpCheckInProgress, 0)
httpCheckResult := mysql.CheckHttp(clusterName, probe)
throttler.mysqlHttpCheckChan <- httpCheckResult
}()
}
}()
}
return nil
}
// refreshMySQLInventory will re-structure the inventory based on reading config settings, and potentially
// re-querying dynamic data such as HAProxy list of hosts
func (throttler *Throttler) refreshMySQLInventory() error {
if !throttler.isLeader {
return nil
}
log.Debugf("refreshing MySQL inventory")
addInstanceKey := func(key *mysql.InstanceKey, clusterName string, clusterSettings *config.MySQLClusterConfigurationSettings, probes *mysql.Probes) {
for _, ignore := range clusterSettings.IgnoreHosts {
if strings.Contains(key.StringCode(), ignore) {
log.Debugf("instance key ignored: %+v", key)
return
}
}
if _, skipped := throttler.skippedHosts.Get(key.Hostname); skipped {
log.Debugf("host skipped: %+v", key.Hostname)
return
}
if !key.IsValid() {
log.Debugf("read invalid instance key: [%+v] for cluster %+v", key, clusterName)
return
}
log.Debugf("read instance key: %+v", key)
probe := &mysql.Probe{
Key: *key,
User: clusterSettings.User,
Password: clusterSettings.Password,
MetricQuery: clusterSettings.MetricQuery,
CacheMillis: clusterSettings.CacheMillis,
HttpCheckPath: clusterSettings.HttpCheckPath,
HttpCheckPort: clusterSettings.HttpCheckPort,
}
(*probes)[*key] = probe
}
for clusterName, clusterSettings := range config.Settings().Stores.MySQL.Clusters {
clusterName := clusterName
clusterSettings := clusterSettings
// config may dynamically change, but internal structure (config.Settings().Stores.MySQL.Clusters in our case)
// is immutable and can only be _replaced_. Hence, it's safe to read in a goroutine:
go func() error {
throttler.mysqlClusterThresholds.Set(clusterName, clusterSettings.ThrottleThreshold, cache.DefaultExpiration)
throttler.prometheusMetrics.throttleThreshold.WithLabelValues("mysql", clusterName).Set(clusterSettings.ThrottleThreshold)
if !clusterSettings.HAProxySettings.IsEmpty() {
poolName := clusterSettings.HAProxySettings.PoolName
totalHosts := []string{}
addresses, _ := clusterSettings.HAProxySettings.GetProxyAddresses()
for _, u := range addresses {
log.Debugf("getting haproxy data from %s", u.String())
csv, err := haproxy.Read(u)
if err != nil {
return log.Errorf("Unable to get HAproxy data from %s: %+v", u.String(), err)
}
if backendHosts, err := haproxy.ParseCsvHosts(csv, poolName); err == nil {
hosts := haproxy.FilterThrotllerHosts(backendHosts)
totalHosts = append(totalHosts, hosts...)
log.Debugf("Read %+v hosts from haproxy %s/#%s", len(hosts), u.String(), poolName)
} else {
log.Errorf("Unable to get HAproxy hosts from %s/#%s: %+v", u.String(), poolName, err)
}
}
if len(totalHosts) == 0 {
return log.Errorf("Unable to get any HAproxy hosts for pool: %+v", poolName)
}
clusterProbes := &mysql.ClusterProbes{
ClusterName: clusterName,
IgnoreHostsCount: clusterSettings.IgnoreHostsCount,
IgnoreHostsThreshold: clusterSettings.IgnoreHostsThreshold,
InstanceProbes: mysql.NewProbes(),
}
for _, host := range totalHosts {
key := mysql.InstanceKey{Hostname: host, Port: clusterSettings.Port}
addInstanceKey(&key, clusterName, clusterSettings, clusterProbes.InstanceProbes)
}
throttler.mysqlClusterProbesChan <- clusterProbes
return nil
}
if !clusterSettings.ProxySQLSettings.IsEmpty() {
db, addr, err := throttler.proxysqlClient.GetDB(clusterSettings.ProxySQLSettings)
if err != nil {
log.Debugf("Unable to connect to ProxySQL: %v", err)
return err
}
dsn := clusterSettings.ProxySQLSettings.AddressToDSN(addr)
log.Debugf("getting ProxySQL data from %s, hostgroup id: %d (%s)", dsn, clusterSettings.ProxySQLSettings.HostgroupID, clusterName)
servers, err := throttler.proxysqlClient.GetServers(db, clusterSettings.ProxySQLSettings)
if err != nil {
throttler.proxysqlClient.CloseDB(addr)
return log.Errorf("Unable to get hosts from ProxySQL %s: %+v", dsn, err)
}
log.Debugf("Read %+v hosts from ProxySQL %s, hostgroup id: %d (%s)", len(servers), dsn, clusterSettings.ProxySQLSettings.HostgroupID, clusterName)
clusterProbes := &mysql.ClusterProbes{
ClusterName: clusterName,
IgnoreHostsCount: clusterSettings.IgnoreHostsCount,
IgnoreHostsThreshold: clusterSettings.IgnoreHostsThreshold,
InstanceProbes: mysql.NewProbes(),
}
for _, server := range servers {
key := mysql.InstanceKey{Hostname: server.Host, Port: int(server.Port)}
addInstanceKey(&key, clusterName, clusterSettings, clusterProbes.InstanceProbes)
}
throttler.mysqlClusterProbesChan <- clusterProbes
return nil
}
if !clusterSettings.VitessSettings.IsEmpty() {
log.Debugf("getting vitess data from %s", clusterSettings.VitessSettings.API)
keyspace := clusterSettings.VitessSettings.Keyspace
shard := clusterSettings.VitessSettings.Shard
tablets, err := vitess.ParseTablets(clusterSettings.VitessSettings)
if err != nil {
return log.Errorf("Unable to get vitess hosts from %s, %s/%s: %+v", clusterSettings.VitessSettings.API, keyspace, shard, err)
}
log.Debugf("Read %+v hosts from vitess %s, %s/%s, cells=%s", len(tablets), clusterSettings.VitessSettings.API,
keyspace, shard, strings.Join(vitess.ParseCells(clusterSettings.VitessSettings), ","),
)
clusterProbes := &mysql.ClusterProbes{
ClusterName: clusterName,
IgnoreHostsCount: clusterSettings.IgnoreHostsCount,
IgnoreHostsThreshold: clusterSettings.IgnoreHostsThreshold,
InstanceProbes: mysql.NewProbes(),
}
for _, tablet := range tablets {
key := mysql.InstanceKey{Hostname: tablet.MysqlHostname, Port: int(tablet.MysqlPort)}
addInstanceKey(&key, clusterName, clusterSettings, clusterProbes.InstanceProbes)
}
throttler.mysqlClusterProbesChan <- clusterProbes
return nil
}
if !clusterSettings.StaticHostsSettings.IsEmpty() {
clusterProbes := &mysql.ClusterProbes{
ClusterName: clusterName,
InstanceProbes: mysql.NewProbes(),
}
for _, host := range clusterSettings.StaticHostsSettings.Hosts {
key, err := mysql.ParseInstanceKey(host, clusterSettings.Port)
if err != nil {
return log.Errore(err)
}
addInstanceKey(key, clusterName, clusterSettings, clusterProbes.InstanceProbes)
}
throttler.mysqlClusterProbesChan <- clusterProbes
return nil
}
return log.Errorf("Could not find any hosts definition for cluster %s", clusterName)
}()
}
return nil
}
// synchronous update of inventory
func (throttler *Throttler) updateMySQLClusterProbes(clusterProbes *mysql.ClusterProbes) error {
log.Debugf("updating MySQLClusterProbes: %s", clusterProbes.ClusterName)
throttler.mysqlInventory.ClustersProbes[clusterProbes.ClusterName] = clusterProbes.InstanceProbes
throttler.mysqlInventory.IgnoreHostsCount[clusterProbes.ClusterName] = clusterProbes.IgnoreHostsCount
throttler.mysqlInventory.IgnoreHostsThreshold[clusterProbes.ClusterName] = clusterProbes.IgnoreHostsThreshold
return nil
}
// synchronous aggregation of collected data
func (throttler *Throttler) aggregateMySQLMetrics() error {
if !throttler.isLeader {
return nil
}
for clusterName, probes := range throttler.mysqlInventory.ClustersProbes {
metricName := fmt.Sprintf("mysql/%s", clusterName)
ignoreHostsCount := throttler.mysqlInventory.IgnoreHostsCount[clusterName]
ignoreHostsThreshold := throttler.mysqlInventory.IgnoreHostsThreshold[clusterName]
aggregatedMetric := aggregateMySQLProbes(probes, clusterName, throttler.mysqlInventory.InstanceKeyMetrics, throttler.mysqlInventory.ClusterInstanceHttpChecks, ignoreHostsCount, config.Settings().Stores.MySQL.IgnoreDialTcpErrors, ignoreHostsThreshold)
updateMySQLMetrics(throttler.prometheusMetrics, clusterName, aggregatedMetric)
go throttler.aggregatedMetrics.Set(metricName, aggregatedMetric, cache.DefaultExpiration)
if throttler.memcacheClient != nil {
go func() {
memcacheKey := fmt.Sprintf("%s/%s", throttler.memcachePath, metricName)
value, err := aggregatedMetric.Get()
if err != nil {
throttler.memcacheClient.Delete(memcacheKey)
} else {
epochMillis := time.Now().UnixNano() / 1000000
entryVal := fmt.Sprintf("%d:%.6f", epochMillis, value)
throttler.memcacheClient.Set(&memcache.Item{Key: memcacheKey, Value: []byte(entryVal), Expiration: 1})
}
}()
}
}
return nil
}
func updateMySQLMetrics(prometheusMetrics *PrometheusMetrics, clusterName string, metric base.MetricResult) {
const storeType = "mysql"
prometheusMetrics.timestamp.WithLabelValues(storeType, clusterName).Set(float64(time.Now().Unix()))
value, err := metric.Get()
if err != nil {
prometheusMetrics.value.DeleteLabelValues(storeType, clusterName)
} else {
prometheusMetrics.value.WithLabelValues(storeType, clusterName).Set(value)
}
}
func (throttler *Throttler) pushStatusToExpVar() {
metrics.DefaultRegistry.Each(func(metricName string, _ interface{}) {
if strings.HasPrefix(metricName, "throttled_states.") {
metrics.Get(metricName).(metrics.Gauge).Update(0)
}
})
for appName := range throttler.ThrottledAppsSnapshot() {
metrics.GetOrRegisterGauge(fmt.Sprintf("throttled_states.%s", appName), nil).Update(1)
}
}
func (throttler *Throttler) getNamedMetric(metricName string) base.MetricResult {
if metricResultVal, found := throttler.aggregatedMetrics.Get(metricName); found {
return metricResultVal.(base.MetricResult)
}
return base.NoSuchMetric
}
func (throttler *Throttler) getMySQLClusterMetrics(clusterName string) (base.MetricResult, float64) {
if thresholdVal, found := throttler.mysqlClusterThresholds.Get(clusterName); found {
threshold, _ := thresholdVal.(float64)
metricName := fmt.Sprintf("mysql/%s", clusterName)
return throttler.getNamedMetric(metricName), threshold
}
return base.NoSuchMetric, 0
}
func (throttler *Throttler) aggregatedMetricsSnapshot() map[string]base.MetricResult {
snapshot := make(map[string]base.MetricResult)
for key, value := range throttler.aggregatedMetrics.Items() {
metricResult, _ := value.Object.(base.MetricResult)
snapshot[key] = metricResult
}
return snapshot
}
func (throttler *Throttler) expireThrottledApps() {
now := time.Now()
for appName, item := range throttler.throttledApps.Items() {
appThrottle := item.Object.(*base.AppThrottle)
if appThrottle.ExpireAt.Before(now) {
throttler.UnthrottleApp(appName)
}
}
}
func (throttler *Throttler) ThrottleApp(appName string, expireAt time.Time, ratio float64) {
throttler.throttledAppsMutex.Lock()
defer throttler.throttledAppsMutex.Unlock()
var appThrottle *base.AppThrottle
now := time.Now()
if object, found := throttler.throttledApps.Get(appName); found {
appThrottle = object.(*base.AppThrottle)
if !expireAt.IsZero() {
appThrottle.ExpireAt = expireAt
}
if ratio >= 0 {
appThrottle.Ratio = ratio
}
} else {
if expireAt.IsZero() {
expireAt = now.Add(DefaultThrottleTTLMinutes * time.Minute)
}
if ratio < 0 {
ratio = DefaultThrottleRatio
}
appThrottle = base.NewAppThrottle(expireAt, ratio)
}
if now.Before(appThrottle.ExpireAt) {
throttler.throttledApps.Set(appName, appThrottle, cache.DefaultExpiration)
} else {
throttler.UnthrottleApp(appName)
}
}
func (throttler *Throttler) UnthrottleApp(appName string) {
throttler.throttledApps.Delete(appName)
}
func (throttler *Throttler) IsAppThrottled(appName, storeName string) bool {
appWithStore := fmt.Sprintf("%s/%s", appName, storeName)
keys := []string{appWithStore, appName}
// check if app is throttled for this store or globally
for _, key := range keys {
if object, found := throttler.throttledApps.Get(key); found {
appThrottle := object.(*base.AppThrottle)
if appThrottle.ExpireAt.Before(time.Now()) {
// throttling cleanup hasn't purged yet, but it is expired
continue
}
// handle ratio
if rand.Float64() < appThrottle.Ratio {
return true
}
}
}
return false
}
func (throttler *Throttler) ThrottledAppsMap() (result map[string](*base.AppThrottle)) {
result = make(map[string](*base.AppThrottle))
for appName, item := range throttler.throttledApps.Items() {
appThrottle := item.Object.(*base.AppThrottle)
result[appName] = appThrottle
}
return result
}
func (throttler *Throttler) expireSkippedHosts() {
now := time.Now()
for hostName, item := range throttler.skippedHosts.Items() {
expireAt := item.Object.(time.Time)
if expireAt.Before(now) {
throttler.RecoverHost(hostName)
}
}
}
func (throttler *Throttler) SkipHost(hostName string, expireAt time.Time) {
throttler.skippedHostsMutex.Lock()
defer throttler.skippedHostsMutex.Unlock()
now := time.Now()
if expireAt.IsZero() {
expireAt = now.Add(DefaultSkipTTLMinutes * time.Minute)
}
if now.Before(expireAt) {
throttler.skippedHosts.Set(hostName, expireAt, cache.DefaultExpiration)
} else {
throttler.RecoverHost(hostName)
}
}
func (throttler *Throttler) RecoverHost(hostName string) {
throttler.skippedHosts.Delete(hostName)
}
func (throttler *Throttler) SkippedHostsMap() map[string]time.Time {
result := make(map[string]time.Time)
for hostName, item := range throttler.skippedHosts.Items() {
expireAt := item.Object.(time.Time)
result[hostName] = expireAt
}
return result
}
func (throttler *Throttler) markRecentApp(appName string, remoteAddr string) {
recentAppKey := fmt.Sprintf("%s/%s", appName, remoteAddr)
throttler.recentApps.Set(recentAppKey, time.Now(), cache.DefaultExpiration)
}
func (throttler *Throttler) RecentAppsMap() (result map[string](*base.RecentApp)) {
result = make(map[string](*base.RecentApp))
for recentAppKey, item := range throttler.recentApps.Items() {
recentApp := base.NewRecentApp(item.Object.(time.Time))
result[recentAppKey] = recentApp
}
return result
}
// markMetricHealthy will mark the time "now" as the last time a given metric was checked to be "OK"
func (throttler *Throttler) markMetricHealthy(metricName string) {
throttler.metricsHealth.Set(metricName, time.Now(), cache.DefaultExpiration)
}
// timeSinceMetricHealthy returns time elapsed since the last time a metric checked "OK"
func (throttler *Throttler) timeSinceMetricHealthy(metricName string) (timeSinceHealthy time.Duration, found bool) {
if lastOKTime, found := throttler.metricsHealth.Get(metricName); found {
return time.Since(lastOKTime.(time.Time)), true
}
return 0, false
}
func (throttler *Throttler) metricsHealthSnapshot() base.MetricHealthMap {
snapshot := make(base.MetricHealthMap)
for key, value := range throttler.metricsHealth.Items() {
lastHealthyAt, _ := value.Object.(time.Time)
snapshot[key] = base.NewMetricHealth(lastHealthyAt)
}
return snapshot
}
func (throttler *Throttler) AppRequestMetricResult(appName string, storeName string, metricResultFunc base.MetricResultFunc, denyApp bool) (metricResult base.MetricResult, threshold float64) {
if denyApp {
return base.AppDeniedMetric, 0
}
if throttler.IsAppThrottled(appName, storeName) {
return base.AppDeniedMetric, 0
}
return metricResultFunc()
}
func (throttler *Throttler) collectShareDomainMetricHealth() error {
if !throttler.isLeader {
return nil
}
services, err := throttler.sharedDomainServicesFunc()
if err != nil {
return log.Errore(err)
}
if len(services) == 0 {
return nil
}
aggregatedMetricHealth := make(base.MetricHealthMap)
for _, service := range services {
err := func() error {
uri := fmt.Sprintf("http://%s/metrics-health", service)
resp, err := throttler.httpClient.Get(uri)
if err != nil {
return err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
m := make(base.MetricHealthMap)
if err = json.Unmarshal(b, &m); err != nil {
return err
}
log.Debugf("share domain url: %+v", uri)
aggregatedMetricHealth.Aggregate(m)
return nil
}()
log.Errore(err)
}
for metricName, metricHealth := range aggregatedMetricHealth {
throttler.shareDomainMetricHealth.SetDefault(metricName, metricHealth)
}
return nil
}
func (throttler *Throttler) getShareDomainSecondsSinceHealth(metricName string) int64 {
if object, found := throttler.shareDomainMetricHealth.Get(metricName); found {
metricHealth := object.(*base.MetricHealth)
return metricHealth.SecondsSinceLastHealthy
}
return 0
}