-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
throttler.go
1048 lines (920 loc) · 36.4 KB
/
throttler.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
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
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2017 GitHub Inc.
Licensed under MIT License. See https://github.com/github/freno/blob/master/LICENSE
*/
package throttle
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"math"
"math/rand"
"net/http"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/patrickmn/go-cache"
"github.com/spf13/pflag"
"vitess.io/vitess/go/sync2"
"vitess.io/vitess/go/textutil"
"vitess.io/vitess/go/timer"
"vitess.io/vitess/go/vt/log"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/srvtopo"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/vttablet/tabletserver/connpool"
"vitess.io/vitess/go/vt/vttablet/tabletserver/heartbeat"
"vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv"
"vitess.io/vitess/go/vt/vttablet/tabletserver/throttle/base"
"vitess.io/vitess/go/vt/vttablet/tabletserver/throttle/config"
"vitess.io/vitess/go/vt/vttablet/tabletserver/throttle/mysql"
)
const (
leaderCheckInterval = 5 * time.Second
mysqlCollectInterval = 250 * time.Millisecond
mysqlDormantCollectInterval = 5 * time.Second
mysqlRefreshInterval = 10 * time.Second
mysqlAggregateInterval = 125 * time.Millisecond
aggregatedMetricsExpiration = 5 * time.Second
throttledAppsSnapshotInterval = 5 * time.Second
recentAppsExpiration = time.Hour * 24
nonDeprioritizedAppMapExpiration = time.Second
dormantPeriod = time.Minute
defaultThrottleTTLMinutes = 60
defaultThrottleRatio = 1.0
shardStoreName = "shard"
selfStoreName = "self"
)
var (
// flag vars
throttleThreshold = 1 * time.Second
throttleTabletTypes = "replica"
throttleMetricQuery string
throttleMetricThreshold = math.MaxFloat64
throttlerCheckAsCheckSelf = false
throttlerConfigViaTopo = false
)
func init() {
servenv.OnParseFor("vtcombo", registerThrottlerFlags)
servenv.OnParseFor("vttablet", registerThrottlerFlags)
}
func registerThrottlerFlags(fs *pflag.FlagSet) {
fs.StringVar(&throttleTabletTypes, "throttle_tablet_types", throttleTabletTypes, "Comma separated VTTablet types to be considered by the throttler. default: 'replica'. example: 'replica,rdonly'. 'replica' aways implicitly included")
fs.DurationVar(&throttleThreshold, "throttle_threshold", throttleThreshold, "Replication lag threshold for default lag throttling")
fs.StringVar(&throttleMetricQuery, "throttle_metrics_query", throttleMetricQuery, "Override default heartbeat/lag metric. Use either `SELECT` (must return single row, single value) or `SHOW GLOBAL ... LIKE ...` queries. Set -throttle_metrics_threshold respectively.")
fs.Float64Var(&throttleMetricThreshold, "throttle_metrics_threshold", throttleMetricThreshold, "Override default throttle threshold, respective to --throttle_metrics_query")
fs.BoolVar(&throttlerCheckAsCheckSelf, "throttle_check_as_check_self", throttlerCheckAsCheckSelf, "Should throttler/check return a throttler/check-self result (changes throttler behavior for writes)")
fs.BoolVar(&throttlerConfigViaTopo, "throttler-config-via-topo", throttlerConfigViaTopo, "When 'true', read config from topo service and ignore throttle_threshold, throttle_metrics_threshold, throttle_metrics_query, throttle_check_as_check_self")
}
var (
replicationLagQuery = `select unix_timestamp(now(6))-max(ts/1000000000) as replication_lag from _vt.heartbeat`
ErrThrottlerNotReady = errors.New("throttler not enabled/ready")
)
// ThrottleCheckType allows a client to indicate what type of check it wants to issue. See available types below.
type ThrottleCheckType int // nolint:revive
const (
// ThrottleCheckPrimaryWrite indicates a check before making a write on a primary server
ThrottleCheckPrimaryWrite ThrottleCheckType = iota
// ThrottleCheckSelf indicates a check on a specific server health
ThrottleCheckSelf
)
func init() {
rand.Seed(time.Now().UnixNano())
}
// Throttler is the main entity in the throttling mechanism. This service runs, probes, collects data,
// aggregates, reads inventory, provides information, etc.
type Throttler struct {
keyspace string
shard string
cell string
check *ThrottlerCheck
isEnabled int64
isLeader int64
isOpen int64
env tabletenv.Env
pool *connpool.Pool
tabletTypeFunc func() topodatapb.TabletType
ts *topo.Server
srvTopoServer srvtopo.Server
heartbeatWriter heartbeat.HeartbeatWriter
throttleTabletTypesMap map[topodatapb.TabletType]bool
mysqlThrottleMetricChan chan *mysql.MySQLThrottleMetric
mysqlInventoryChan chan *mysql.Inventory
mysqlClusterProbesChan chan *mysql.ClusterProbes
throttlerConfigChan chan *topodatapb.ThrottlerConfig
mysqlInventory *mysql.Inventory
metricsQuery atomic.Value
MetricsThreshold sync2.AtomicFloat64
mysqlClusterThresholds *cache.Cache
aggregatedMetrics *cache.Cache
throttledApps *cache.Cache
recentApps *cache.Cache
metricsHealth *cache.Cache
lastCheckTimeNano int64
initMutex sync.Mutex
enableMutex sync.Mutex
cancelEnableContext context.CancelFunc
throttledAppsMutex sync.Mutex
nonLowPriorityAppRequestsThrottled *cache.Cache
httpClient *http.Client
}
// ThrottlerStatus published some status values from the throttler
type ThrottlerStatus struct {
Keyspace string
Shard string
IsLeader bool
IsOpen bool
IsEnabled bool
IsDormant bool
Query string
Threshold float64
AggregatedMetrics map[string]base.MetricResult
MetricsHealth base.MetricHealthMap
}
// NewThrottler creates a Throttler
func NewThrottler(env tabletenv.Env, srvTopoServer srvtopo.Server, ts *topo.Server, cell string, heartbeatWriter heartbeat.HeartbeatWriter, tabletTypeFunc func() topodatapb.TabletType) *Throttler {
throttler := &Throttler{
isLeader: 0,
isOpen: 0,
cell: cell,
env: env,
tabletTypeFunc: tabletTypeFunc,
srvTopoServer: srvTopoServer,
ts: ts,
heartbeatWriter: heartbeatWriter,
pool: connpool.NewPool(env, "ThrottlerPool", tabletenv.ConnPoolConfig{
Size: 2,
IdleTimeoutSeconds: env.Config().OltpReadPool.IdleTimeoutSeconds,
}),
}
throttler.mysqlThrottleMetricChan = make(chan *mysql.MySQLThrottleMetric)
throttler.mysqlInventoryChan = make(chan *mysql.Inventory, 1)
throttler.mysqlClusterProbesChan = make(chan *mysql.ClusterProbes)
throttler.throttlerConfigChan = make(chan *topodatapb.ThrottlerConfig)
throttler.mysqlInventory = mysql.NewInventory()
throttler.throttledApps = cache.New(cache.NoExpiration, 0)
throttler.mysqlClusterThresholds = cache.New(cache.NoExpiration, 0)
throttler.aggregatedMetrics = cache.New(aggregatedMetricsExpiration, 0)
throttler.recentApps = cache.New(recentAppsExpiration, 0)
throttler.metricsHealth = cache.New(cache.NoExpiration, 0)
throttler.nonLowPriorityAppRequestsThrottled = cache.New(nonDeprioritizedAppMapExpiration, 0)
throttler.httpClient = base.SetupHTTPClient(2 * mysqlCollectInterval)
throttler.initThrottleTabletTypes()
throttler.check = NewThrottlerCheck(throttler)
throttler.metricsQuery.Store(replicationLagQuery) // default
if throttleMetricQuery != "" {
throttler.metricsQuery.Store(throttleMetricQuery) // override
}
throttler.MetricsThreshold = sync2.NewAtomicFloat64(throttleThreshold.Seconds()) //default
if throttleMetricThreshold != math.MaxFloat64 {
throttler.MetricsThreshold.Set(throttleMetricThreshold) // override
}
throttler.initConfig()
return throttler
}
// CheckIsReady checks if this throttler is ready to serve. If not, it returns an error
func (throttler *Throttler) CheckIsReady() error {
if throttler.IsEnabled() {
// all good
return nil
}
return ErrThrottlerNotReady
}
// initThrottleTabletTypes reads the user supplied throttle_tablet_types and sets these
// for the duration of this tablet's lifetime
func (throttler *Throttler) initThrottleTabletTypes() {
throttler.throttleTabletTypesMap = make(map[topodatapb.TabletType]bool)
tokens := textutil.SplitDelimitedList(throttleTabletTypes)
for _, token := range tokens {
token = strings.ToUpper(token)
if value, ok := topodatapb.TabletType_value[token]; ok {
throttler.throttleTabletTypesMap[topodatapb.TabletType(value)] = true
}
}
// always on:
throttler.throttleTabletTypesMap[topodatapb.TabletType_REPLICA] = true
}
// InitDBConfig initializes keyspace and shard
func (throttler *Throttler) InitDBConfig(keyspace, shard string) {
throttler.keyspace = keyspace
throttler.shard = shard
if throttlerConfigViaTopo {
throttler.srvTopoServer.WatchSrvKeyspace(context.Background(), throttler.cell, throttler.keyspace, throttler.WatchSrvKeyspaceCallback)
}
}
func (throttler *Throttler) GetMetricsQuery() string {
return throttler.metricsQuery.Load().(string)
}
func (throttler *Throttler) GetMetricsThreshold() float64 {
return throttler.MetricsThreshold.Get()
}
// initThrottler initializes config
func (throttler *Throttler) initConfig() {
log.Infof("Throttler: initializing config")
config.Instance = &config.ConfigurationSettings{
Stores: config.StoresSettings{
MySQL: config.MySQLConfigurationSettings{
IgnoreDialTCPErrors: true,
Clusters: map[string](*config.MySQLClusterConfigurationSettings){},
},
},
}
config.Instance.Stores.MySQL.Clusters[selfStoreName] = &config.MySQLClusterConfigurationSettings{
MetricQuery: throttler.GetMetricsQuery(),
ThrottleThreshold: &throttler.MetricsThreshold,
IgnoreHostsCount: 0,
}
config.Instance.Stores.MySQL.Clusters[shardStoreName] = &config.MySQLClusterConfigurationSettings{
MetricQuery: throttler.GetMetricsQuery(),
ThrottleThreshold: &throttler.MetricsThreshold,
IgnoreHostsCount: 0,
}
}
// readThrottlerConfig proactively reads the throttler's config from SrvKeyspace in local topo
func (throttler *Throttler) readThrottlerConfig(ctx context.Context) (*topodatapb.ThrottlerConfig, error) {
srvks, err := throttler.ts.GetSrvKeyspace(ctx, throttler.cell, throttler.keyspace)
if err != nil {
return nil, err
}
return throttler.normalizeThrottlerConfig(srvks.ThrottlerConfig), nil
}
// normalizeThrottlerConfig noramlizes missing throttler config information, as needed.
func (throttler *Throttler) normalizeThrottlerConfig(thottlerConfig *topodatapb.ThrottlerConfig) *topodatapb.ThrottlerConfig {
if thottlerConfig == nil {
thottlerConfig = &topodatapb.ThrottlerConfig{}
}
if thottlerConfig.CustomQuery == "" {
// no custom query; we check replication lag
if thottlerConfig.Threshold == 0 {
thottlerConfig.Threshold = throttleThreshold.Seconds()
}
}
return thottlerConfig
}
func (throttler *Throttler) WatchSrvKeyspaceCallback(srvks *topodatapb.SrvKeyspace, err error) bool {
if err != nil {
log.Errorf("WatchSrvKeyspaceCallback error: %v", err)
return false
}
throttlerConfig := throttler.normalizeThrottlerConfig(srvks.ThrottlerConfig)
if throttler.IsEnabled() {
// Throttler is running and we should apply the config change through Operate()
// or else we get into race conditions.
go func() {
throttler.throttlerConfigChan <- throttlerConfig
}()
} else {
// throttler is not running, we should apply directly
throttler.applyThrottlerConfig(context.Background(), throttlerConfig)
}
return true
}
// applyThrottlerConfig receives a Throttlerconfig as read from SrvKeyspace, and applies the configuration. This may cause
// the throttler to be enabled/disabled, and of course it affects the throttling query/threshold.
func (throttler *Throttler) applyThrottlerConfig(ctx context.Context, throttlerConfig *topodatapb.ThrottlerConfig) {
if !throttlerConfigViaTopo {
return
}
if throttlerConfig.CustomQuery == "" {
throttler.metricsQuery.Store(replicationLagQuery)
} else {
throttler.metricsQuery.Store(throttlerConfig.CustomQuery)
}
throttler.MetricsThreshold.Set(throttlerConfig.Threshold)
throttlerCheckAsCheckSelf = throttlerConfig.CheckAsCheckSelf
if throttlerConfig.Enabled {
go throttler.Enable(ctx)
} else {
go throttler.Disable(ctx)
}
}
func (throttler *Throttler) IsEnabled() bool {
return atomic.LoadInt64(&throttler.isEnabled) > 0
}
// Enable activates the throttler probes; when enabled, the throttler responds to check queries based on
// the collected metrics.
func (throttler *Throttler) Enable(ctx context.Context) bool {
throttler.enableMutex.Lock()
defer throttler.enableMutex.Unlock()
if throttler.isEnabled > 0 {
return false
}
atomic.StoreInt64(&throttler.isEnabled, 1)
ctx, throttler.cancelEnableContext = context.WithCancel(ctx)
throttler.check.SelfChecks(ctx)
throttler.Operate(ctx)
// Make a one-time request for a lease of heartbeats
go throttler.heartbeatWriter.RequestHeartbeats()
return true
}
// Disable deactivates the probes and associated operations. When disabled, the throttler reponds to check
// queries with "200 OK" irrespective of lag or any other metrics.
func (throttler *Throttler) Disable(ctx context.Context) bool {
throttler.enableMutex.Lock()
defer throttler.enableMutex.Unlock()
if throttler.isEnabled == 0 {
return false
}
// _ = throttler.updateConfig(ctx, false, throttler.MetricsThreshold.Get()) // TODO(shlomi)
atomic.StoreInt64(&throttler.isEnabled, 0)
throttler.aggregatedMetrics.Flush()
throttler.recentApps.Flush()
throttler.nonLowPriorityAppRequestsThrottled.Flush()
// we do not flush throttler.throttledApps because this is data submitted by the user; the user expects the data to survive a disable+enable
throttler.cancelEnableContext()
return true
}
// Open opens database pool and initializes the schema
func (throttler *Throttler) Open() error {
throttler.initMutex.Lock()
defer throttler.initMutex.Unlock()
if atomic.LoadInt64(&throttler.isOpen) > 0 {
// already open
return nil
}
ctx := context.Background()
throttler.pool.Open(throttler.env.Config().DB.AppWithDB(), throttler.env.Config().DB.DbaWithDB(), throttler.env.Config().DB.AppDebugWithDB())
atomic.StoreInt64(&throttler.isOpen, 1)
throttler.ThrottleApp("always-throttled-app", time.Now().Add(time.Hour*24*365*10), defaultThrottleRatio)
if throttlerConfigViaTopo {
// We want to read throttler config from topo and apply it.
// But also, we're in an Open() function, which blocks state manager's operation, and affects
// opening of all other components. We thus read the throttler config in the background.
// However, we want to handle a situation where the read errors out.
// So we kick a loop that keeps retrying reading the config, for as long as this throttler is open.
go func() {
retryTicker := time.NewTicker(time.Minute)
defer retryTicker.Stop()
for {
if atomic.LoadInt64(&throttler.isOpen) == 0 {
// closed down. No need to keep retrying
return
}
throttlerConfig, err := throttler.readThrottlerConfig(ctx)
if err == nil {
// it's possible that during a retry-sleep, the throttler is closed and opened again, leading
// to two (or more) instances of this goroutine. That's not a big problem; it's fine if all
// attempt to read the throttler config; but we just want to ensure they don't step on each other
// while applying the changes.
throttler.initMutex.Lock()
defer throttler.initMutex.Unlock()
throttler.applyThrottlerConfig(ctx, throttlerConfig) // may issue an Enable
return
}
log.Errorf("Throttler.Open(): error reading throttler config. Will retry in 1 minute. Err=%+v", err)
<-retryTicker.C
}
}()
} else {
// backwards-cmpatible: check for --enable-lag-throttler flag in vttablet
// this will be removed in a future version
if throttler.env.Config().EnableLagThrottler {
go throttler.Enable(ctx)
}
}
return nil
}
// Close frees resources
func (throttler *Throttler) Close() {
log.Infof("Throttler: started execution of Close. Acquiring initMutex lock")
throttler.initMutex.Lock()
log.Infof("Throttler: acquired initMutex lock")
defer throttler.initMutex.Unlock()
if atomic.LoadInt64(&throttler.isOpen) == 0 {
log.Infof("Throttler: throttler is not open")
return
}
ctx := context.Background()
throttler.Disable(ctx)
atomic.StoreInt64(&throttler.isLeader, 0)
log.Infof("Throttler: closing pool")
throttler.pool.Close()
atomic.StoreInt64(&throttler.isOpen, 0)
log.Infof("Throttler: finished execution of Close")
}
func (throttler *Throttler) generateSelfMySQLThrottleMetricFunc(ctx context.Context, probe *mysql.Probe) func() *mysql.MySQLThrottleMetric {
f := func() *mysql.MySQLThrottleMetric {
return throttler.readSelfMySQLThrottleMetric(ctx, probe)
}
return f
}
// readSelfMySQLThrottleMetric reads the mysql metric from thi very tablet's backend mysql.
func (throttler *Throttler) readSelfMySQLThrottleMetric(ctx context.Context, probe *mysql.Probe) *mysql.MySQLThrottleMetric {
metric := &mysql.MySQLThrottleMetric{
ClusterName: selfStoreName,
Key: *mysql.SelfInstanceKey,
Value: 0,
Err: nil,
}
conn, err := throttler.pool.Get(ctx, nil)
if err != nil {
metric.Err = err
return metric
}
defer conn.Recycle()
tm, err := conn.Exec(ctx, probe.MetricQuery, 1, true)
if err != nil {
metric.Err = err
return metric
}
row := tm.Named().Row()
if row == nil {
metric.Err = fmt.Errorf("no results for readSelfMySQLThrottleMetric")
return metric
}
metricsQueryType := mysql.GetMetricsQueryType(throttler.GetMetricsQuery())
switch metricsQueryType {
case mysql.MetricsQueryTypeSelect:
// We expect a single row, single column result.
// The "for" iteration below is just a way to get first result without knowning column name
for k := range row {
metric.Value, metric.Err = row.ToFloat64(k)
}
case mysql.MetricsQueryTypeShowGlobal:
metric.Value, metric.Err = strconv.ParseFloat(row["Value"].ToString(), 64)
default:
metric.Err = fmt.Errorf("Unsupported metrics query type for query: %s", throttler.GetMetricsQuery())
}
return metric
}
// throttledAppsSnapshot returns a snapshot (a copy) of current throttled apps
func (throttler *Throttler) throttledAppsSnapshot() map[string]cache.Item {
return throttler.throttledApps.Items()
}
// ThrottledAppsSnapshot returns a snapshot (a copy) of current throttled apps
func (throttler *Throttler) ThrottledApps() (result []base.AppThrottle) {
for _, item := range throttler.throttledAppsSnapshot() {
appThrottle, _ := item.Object.(*base.AppThrottle)
result = append(result, *appThrottle)
}
return result
}
// isDormant returns true when the last check was more than dormantPeriod ago
func (throttler *Throttler) isDormant() bool {
lastCheckTime := time.Unix(0, atomic.LoadInt64(&throttler.lastCheckTimeNano))
return time.Since(lastCheckTime) > dormantPeriod
}
// Operate is the main entry point for the throttler operation and logic. It will
// run the probes, collect metrics, refresh inventory, etc.
func (throttler *Throttler) Operate(ctx context.Context) {
tickers := [](*timer.SuspendableTicker){}
addTicker := func(d time.Duration) *timer.SuspendableTicker {
t := timer.NewSuspendableTicker(d, false)
tickers = append(tickers, t)
return t
}
leaderCheckTicker := addTicker(leaderCheckInterval)
mysqlCollectTicker := addTicker(mysqlCollectInterval)
mysqlDormantCollectTicker := addTicker(mysqlDormantCollectInterval)
mysqlRefreshTicker := addTicker(mysqlRefreshInterval)
mysqlAggregateTicker := addTicker(mysqlAggregateInterval)
throttledAppsTicker := addTicker(throttledAppsSnapshotInterval)
go func() {
defer log.Infof("Throttler: Operate terminated, tickers stopped")
for _, t := range tickers {
defer t.Stop()
// since we just started the tickers now, speed up the ticks by forcing an immediate tick
go t.TickNow()
}
for {
select {
case <-ctx.Done():
return
case <-leaderCheckTicker.C:
{
func() {
throttler.initMutex.Lock()
defer throttler.initMutex.Unlock()
// sparse
shouldBeLeader := int64(0)
if atomic.LoadInt64(&throttler.isOpen) > 0 {
if throttler.tabletTypeFunc() == topodatapb.TabletType_PRIMARY {
shouldBeLeader = 1
}
}
transitionedIntoLeader := false
if shouldBeLeader > throttler.isLeader {
log.Infof("Throttler: transition into leadership")
transitionedIntoLeader = true
}
if shouldBeLeader < throttler.isLeader {
log.Infof("Throttler: transition out of leadership")
}
atomic.StoreInt64(&throttler.isLeader, shouldBeLeader)
if transitionedIntoLeader {
// transitioned into leadership, let's speed up the next 'refresh' and 'collect' ticks
go mysqlRefreshTicker.TickNow()
go throttler.heartbeatWriter.RequestHeartbeats()
}
}()
}
case <-mysqlCollectTicker.C:
{
if atomic.LoadInt64(&throttler.isOpen) > 0 {
// frequent
if !throttler.isDormant() {
throttler.collectMySQLMetrics(ctx)
}
}
}
case <-mysqlDormantCollectTicker.C:
{
if atomic.LoadInt64(&throttler.isOpen) > 0 {
// infrequent
if throttler.isDormant() {
throttler.collectMySQLMetrics(ctx)
}
}
}
case metric := <-throttler.mysqlThrottleMetricChan:
{
// incoming MySQL metric, frequent, as result of collectMySQLMetrics()
throttler.mysqlInventory.InstanceKeyMetrics[metric.GetClusterInstanceKey()] = metric
}
case <-mysqlRefreshTicker.C:
{
// sparse
if atomic.LoadInt64(&throttler.isOpen) > 0 {
go throttler.refreshMySQLInventory(ctx)
}
}
case probes := <-throttler.mysqlClusterProbesChan:
{
// incoming structural update, sparse, as result of refreshMySQLInventory()
throttler.updateMySQLClusterProbes(ctx, probes)
}
case <-mysqlAggregateTicker.C:
{
if atomic.LoadInt64(&throttler.isOpen) > 0 {
throttler.aggregateMySQLMetrics(ctx)
}
}
case <-throttledAppsTicker.C:
{
if atomic.LoadInt64(&throttler.isOpen) > 0 {
go throttler.expireThrottledApps()
}
}
case throttlerConfig := <-throttler.throttlerConfigChan:
throttler.applyThrottlerConfig(ctx, throttlerConfig)
}
}
}()
}
func (throttler *Throttler) generateTabletHTTPProbeFunction(ctx context.Context, clusterName string, probe *mysql.Probe) (probeFunc func() *mysql.MySQLThrottleMetric) {
return func() *mysql.MySQLThrottleMetric {
// Hit a tablet's `check-self` via HTTP, and convert its CheckResult JSON output into a MySQLThrottleMetric
mySQLThrottleMetric := mysql.NewMySQLThrottleMetric()
mySQLThrottleMetric.ClusterName = clusterName
mySQLThrottleMetric.Key = probe.Key
tabletCheckSelfURL := fmt.Sprintf("http://%s:%d/throttler/check-self?app=vitess", probe.TabletHost, probe.TabletPort)
resp, err := throttler.httpClient.Get(tabletCheckSelfURL)
if err != nil {
mySQLThrottleMetric.Err = err
return mySQLThrottleMetric
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
mySQLThrottleMetric.Err = err
return mySQLThrottleMetric
}
checkResult := &CheckResult{}
if err := json.Unmarshal(b, checkResult); err != nil {
mySQLThrottleMetric.Err = err
return mySQLThrottleMetric
}
mySQLThrottleMetric.Value = checkResult.Value
if checkResult.StatusCode == http.StatusInternalServerError {
mySQLThrottleMetric.Err = fmt.Errorf("Status code: %d", checkResult.StatusCode)
}
return mySQLThrottleMetric
}
}
func (throttler *Throttler) collectMySQLMetrics(ctx context.Context) error {
// 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)
var throttleMetricFunc func() *mysql.MySQLThrottleMetric
if clusterName == selfStoreName {
throttleMetricFunc = throttler.generateSelfMySQLThrottleMetricFunc(ctx, probe)
} else {
throttleMetricFunc = throttler.generateTabletHTTPProbeFunction(ctx, clusterName, probe)
}
throttleMetrics := mysql.ReadThrottleMetric(probe, clusterName, throttleMetricFunc)
throttler.mysqlThrottleMetricChan <- throttleMetrics
}()
}
}()
}
return nil
}
// refreshMySQLInventory will re-structure the inventory based on reading config settings
func (throttler *Throttler) refreshMySQLInventory(ctx context.Context) error {
// distribute the query/threshold from the throttler down to the cluster settings and from there to the probes
metricsQuery := throttler.GetMetricsQuery()
metricsThreshold := throttler.MetricsThreshold.Get()
addInstanceKey := func(tabletHost string, tabletPort int, key *mysql.InstanceKey, clusterName string, clusterSettings *config.MySQLClusterConfigurationSettings, probes *mysql.Probes) {
for _, ignore := range clusterSettings.IgnoreHosts {
if strings.Contains(key.StringCode(), ignore) {
log.Infof("Throttler: instance key ignored: %+v", key)
return
}
}
if !key.IsValid() && !key.IsSelf() {
log.Infof("Throttler: read invalid instance key: [%+v] for cluster %+v", key, clusterName)
return
}
probe := &mysql.Probe{
Key: *key,
TabletHost: tabletHost,
TabletPort: tabletPort,
MetricQuery: clusterSettings.MetricQuery,
CacheMillis: clusterSettings.CacheMillis,
}
(*probes)[*key] = probe
}
for clusterName, clusterSettings := range config.Settings().Stores.MySQL.Clusters {
clusterName := clusterName
clusterSettings := clusterSettings
clusterSettings.MetricQuery = metricsQuery
clusterSettings.ThrottleThreshold.Set(metricsThreshold)
// 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() {
throttler.mysqlClusterThresholds.Set(clusterName, clusterSettings.ThrottleThreshold.Get(), cache.DefaultExpiration)
clusterProbes := &mysql.ClusterProbes{
ClusterName: clusterName,
IgnoreHostsCount: clusterSettings.IgnoreHostsCount,
InstanceProbes: mysql.NewProbes(),
}
if clusterName == selfStoreName {
// special case: just looking at this tablet's MySQL server
// We will probe this "cluster" (of one server) is a special way.
addInstanceKey("", 0, mysql.SelfInstanceKey, clusterName, clusterSettings, clusterProbes.InstanceProbes)
throttler.mysqlClusterProbesChan <- clusterProbes
return
}
if atomic.LoadInt64(&throttler.isLeader) == 0 {
// not the leader (primary tablet)? Then no more work for us.
return
}
// The primary tablet is also in charge of collecting the shard's metrics
err := func() error {
tabletAliases, err := throttler.ts.FindAllTabletAliasesInShard(ctx, throttler.keyspace, throttler.shard)
if err != nil {
return err
}
for _, tabletAlias := range tabletAliases {
tablet, err := throttler.ts.GetTablet(ctx, tabletAlias)
if err != nil {
return err
}
if throttler.throttleTabletTypesMap[tablet.Type] {
key := mysql.InstanceKey{Hostname: tablet.MysqlHostname, Port: int(tablet.MysqlPort)}
addInstanceKey(tablet.Hostname, int(tablet.PortMap["vt"]), &key, clusterName, clusterSettings, clusterProbes.InstanceProbes)
}
}
throttler.mysqlClusterProbesChan <- clusterProbes
return nil
}()
if err != nil {
log.Errorf("refreshMySQLInventory: %+v", err)
}
}()
}
return nil
}
// synchronous update of inventory
func (throttler *Throttler) updateMySQLClusterProbes(ctx context.Context, clusterProbes *mysql.ClusterProbes) error {
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(ctx context.Context) error {
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(ctx, probes, clusterName, throttler.mysqlInventory.InstanceKeyMetrics, ignoreHostsCount, config.Settings().Stores.MySQL.IgnoreDialTCPErrors, ignoreHostsThreshold)
throttler.aggregatedMetrics.Set(metricName, aggregatedMetric, cache.DefaultExpiration)
}
return nil
}
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(ctx context.Context, 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)
}
}
}
// ThrottleApp instructs the throttler to begin throttling an app, to som eperiod and with some ratio.
func (throttler *Throttler) ThrottleApp(appName string, expireAt time.Time, ratio float64) (appThrottle *base.AppThrottle) {
throttler.throttledAppsMutex.Lock()
defer throttler.throttledAppsMutex.Unlock()
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(appName, expireAt, ratio)
}
if now.Before(appThrottle.ExpireAt) {
throttler.throttledApps.Set(appName, appThrottle, cache.DefaultExpiration)
} else {
throttler.UnthrottleApp(appName)
}
return appThrottle
}
// UnthrottleApp cancels any throttling, if any, for a given app
func (throttler *Throttler) UnthrottleApp(appName string) (appThrottle *base.AppThrottle) {
throttler.throttledApps.Delete(appName)
// the app is likely to check
go throttler.heartbeatWriter.RequestHeartbeats()
return base.NewAppThrottle(appName, time.Now(), 0)
}
// IsAppThrottled tells whether some app should be throttled.
// Assuming an app is throttled to some extend, it will randomize the result based
// on the throttle ratio
func (throttler *Throttler) IsAppThrottled(appName string) bool {
isSingleAppNameThrottled := func(singleAppName string) bool {
if object, found := throttler.throttledApps.Get(singleAppName); found {
appThrottle := object.(*base.AppThrottle)
if appThrottle.ExpireAt.Before(time.Now()) {
// throttling cleanup hasn't purged yet, but it is expired
return false
}
// handle ratio
if rand.Float64() < appThrottle.Ratio {
return true
}
}
return false
}
if isSingleAppNameThrottled(appName) {
return true
}
for _, singleAppName := range strings.Split(appName, ":") {
if singleAppName == "" {
continue
}
if isSingleAppNameThrottled(singleAppName) {
return true
}
}
return false
}
// ThrottledAppsMap returns a (copy) map of currently throttled apps
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
}
// markRecentApp takes note that an app has just asked about throttling, making it "recent"
func (throttler *Throttler) markRecentApp(appName string, remoteAddr string) {
recentAppKey := fmt.Sprintf("%s/%s", appName, remoteAddr)
throttler.recentApps.Set(recentAppKey, time.Now(), cache.DefaultExpiration)
}
// RecentAppsMap returns a (copy) map of apps which checked for throttling recently
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
}
// AppRequestMetricResult gets a metric result in the context of a specific app
func (throttler *Throttler) AppRequestMetricResult(ctx context.Context, appName string, metricResultFunc base.MetricResultFunc, denyApp bool) (metricResult base.MetricResult, threshold float64) {
if denyApp {
return base.AppDeniedMetric, 0
}
if throttler.IsAppThrottled(appName) {
return base.AppDeniedMetric, 0
}
return metricResultFunc()
}
// checkStore checks the aggregated value of given MySQL store
func (throttler *Throttler) checkStore(ctx context.Context, appName string, storeName string, remoteAddr string, flags *CheckFlags) (checkResult *CheckResult) {
if !throttler.IsEnabled() {
return okMetricCheckResult
}
return throttler.check.Check(ctx, appName, "mysql", storeName, remoteAddr, flags)