-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
throttler.go
1230 lines (1085 loc) · 45 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 2023 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// This codebase originates from https://github.com/github/freno, See https://github.com/github/freno/blob/master/LICENSE
/*
MIT License
Copyright (c) 2017 GitHub
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package throttle
import (
"context"
"errors"
"fmt"
"math"
"math/rand"
"net/http"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/patrickmn/go-cache"
"github.com/spf13/pflag"
"vitess.io/vitess/go/constants/sidecar"
"vitess.io/vitess/go/protoutil"
"vitess.io/vitess/go/textutil"
"vitess.io/vitess/go/timer"
"vitess.io/vitess/go/vt/log"
tabletmanagerdatapb "vitess.io/vitess/go/vt/proto/tabletmanagerdata"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/srvtopo"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/topo/topoproto"
"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"
"vitess.io/vitess/go/vt/vttablet/tabletserver/throttle/throttlerapp"
"vitess.io/vitess/go/vt/vttablet/tmclient"
)
const (
leaderCheckInterval = 5 * time.Second
mysqlCollectInterval = 250 * time.Millisecond
mysqlDormantCollectInterval = 5 * time.Second
mysqlRefreshInterval = 10 * time.Second
mysqlAggregateInterval = 125 * time.Millisecond
throttledAppsSnapshotInterval = 5 * time.Second
aggregatedMetricsExpiration = 5 * time.Second
recentAppsExpiration = time.Hour * 24
nonDeprioritizedAppMapExpiration = time.Second
dormantPeriod = time.Minute
DefaultAppThrottleDuration = time.Hour
DefaultThrottleRatio = 1.0
shardStoreName = "shard"
selfStoreName = "self"
defaultReplicationLagQuery = "select unix_timestamp(now(6))-max(ts/1000000000) as replication_lag from %s.heartbeat"
)
var (
// flag vars
defaultThrottleLagThreshold = 5 * time.Second
throttleTabletTypes = "replica"
)
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' always implicitly included")
}
var (
ErrThrottlerNotOpen = errors.New("throttler not open")
)
// 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
)
// throttlerTopoService represents the functionality we expect from a TopoServer, abstracted so that
// it can be mocked in unit tests
type throttlerTopoService interface {
GetTablet(ctx context.Context, alias *topodatapb.TabletAlias) (*topo.TabletInfo, error)
FindAllTabletAliasesInShard(ctx context.Context, keyspace, shard string) ([]*topodatapb.TabletAlias, error)
GetSrvKeyspace(ctx context.Context, cell, keyspace string) (*topodatapb.SrvKeyspace, error)
}
// 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 atomic.Bool
isLeader atomic.Bool
isOpen atomic.Bool
leaderCheckInterval time.Duration
mysqlCollectInterval time.Duration
mysqlDormantCollectInterval time.Duration
mysqlRefreshInterval time.Duration
mysqlAggregateInterval time.Duration
throttledAppsSnapshotInterval time.Duration
configSettings *config.ConfigurationSettings
env tabletenv.Env
pool *connpool.Pool
tabletTypeFunc func() topodatapb.TabletType
ts throttlerTopoService
srvTopoServer srvtopo.Server
heartbeatWriter heartbeat.HeartbeatWriter
overrideTmClient tmclient.TabletManagerClient
// recentCheckTickerValue is an ever increasing number, incrementing once per second.
recentCheckTickerValue atomic.Int64
// recentCheckValue is set to match or exceed recentCheckTickerValue whenever a "check" was made (other than by the throttler itself).
// when recentCheckValue < recentCheckTickerValue that means there hasn't been a recent check.
recentCheckValue atomic.Int64
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 atomic.Uint64
checkAsCheckSelf atomic.Bool
mysqlClusterThresholds *cache.Cache
aggregatedMetrics *cache.Cache
throttledApps *cache.Cache
recentApps *cache.Cache
metricsHealth *cache.Cache
lastCheckTimeNano atomic.Int64
initMutex sync.Mutex
enableMutex sync.Mutex
cancelOpenContext context.CancelFunc
cancelEnableContext context.CancelFunc
throttledAppsMutex sync.Mutex
readSelfThrottleMetric func(context.Context, *mysql.Probe) *mysql.MySQLThrottleMetric // overwritten by unit test
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{
cell: cell,
env: env,
tabletTypeFunc: tabletTypeFunc,
srvTopoServer: srvTopoServer,
ts: ts,
heartbeatWriter: heartbeatWriter,
pool: connpool.NewPool(env, "ThrottlerPool", tabletenv.ConnPoolConfig{
Size: 2,
IdleTimeout: env.Config().OltpReadPool.IdleTimeout,
}),
}
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.leaderCheckInterval = leaderCheckInterval
throttler.mysqlCollectInterval = mysqlCollectInterval
throttler.mysqlDormantCollectInterval = mysqlDormantCollectInterval
throttler.mysqlRefreshInterval = mysqlRefreshInterval
throttler.mysqlAggregateInterval = mysqlAggregateInterval
throttler.throttledAppsSnapshotInterval = throttledAppsSnapshotInterval
throttler.StoreMetricsThreshold(defaultThrottleLagThreshold.Seconds()) //default
throttler.readSelfThrottleMetric = func(ctx context.Context, p *mysql.Probe) *mysql.MySQLThrottleMetric {
return throttler.readSelfMySQLThrottleMetric(ctx, p)
}
return throttler
}
func (throttler *Throttler) StoreMetricsThreshold(threshold float64) {
throttler.MetricsThreshold.Store(math.Float64bits(threshold))
}
// 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
}
func (throttler *Throttler) GetMetricsQuery() string {
return throttler.metricsQuery.Load().(string)
}
func (throttler *Throttler) GetMetricsThreshold() float64 {
return math.Float64frombits(throttler.MetricsThreshold.Load())
}
// initThrottler initializes config
func (throttler *Throttler) initConfig() {
log.Infof("Throttler: initializing config")
throttler.configSettings = &config.ConfigurationSettings{
Stores: config.StoresSettings{
MySQL: config.MySQLConfigurationSettings{
IgnoreDialTCPErrors: true,
Clusters: map[string](*config.MySQLClusterConfigurationSettings){},
},
},
}
throttler.configSettings.Stores.MySQL.Clusters[selfStoreName] = &config.MySQLClusterConfigurationSettings{
MetricQuery: throttler.GetMetricsQuery(),
ThrottleThreshold: &throttler.MetricsThreshold,
IgnoreHostsCount: 0,
}
throttler.configSettings.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 normalizes missing throttler config information, as needed.
func (throttler *Throttler) normalizeThrottlerConfig(throttlerConfig *topodatapb.ThrottlerConfig) *topodatapb.ThrottlerConfig {
if throttlerConfig == nil {
throttlerConfig = &topodatapb.ThrottlerConfig{}
}
if throttlerConfig.ThrottledApps == nil {
throttlerConfig.ThrottledApps = make(map[string]*topodatapb.ThrottledAppRule)
}
if throttlerConfig.CustomQuery == "" {
// no custom query; we check replication lag
if throttlerConfig.Threshold == 0 {
throttlerConfig.Threshold = defaultThrottleLagThreshold.Seconds()
}
}
return throttlerConfig
}
func (throttler *Throttler) WatchSrvKeyspaceCallback(srvks *topodatapb.SrvKeyspace, err error) bool {
if err != nil {
if !topo.IsErrType(err, topo.Interrupted) && !errors.Is(err, context.Canceled) {
log.Errorf("WatchSrvKeyspaceCallback error: %v", err)
}
return false
}
throttlerConfig := throttler.normalizeThrottlerConfig(srvks.ThrottlerConfig)
if throttler.IsEnabled() {
// Throttler is enabled and we should apply the config change
// through Operate() or else we get into race conditions.
go func() {
throttler.throttlerConfigChan <- throttlerConfig
}()
} else {
throttler.initMutex.Lock()
defer throttler.initMutex.Unlock()
// Throttler is not enabled, 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.
// Note: you should be holding the initMutex when calling this function.
func (throttler *Throttler) applyThrottlerConfig(ctx context.Context, throttlerConfig *topodatapb.ThrottlerConfig) {
log.Infof("Throttler: applying topo config: %+v", throttlerConfig)
if throttlerConfig.CustomQuery == "" {
throttler.metricsQuery.Store(sqlparser.BuildParsedQuery(defaultReplicationLagQuery, sidecar.GetIdentifier()).Query)
} else {
throttler.metricsQuery.Store(throttlerConfig.CustomQuery)
}
throttler.StoreMetricsThreshold(throttlerConfig.Threshold)
throttler.checkAsCheckSelf.Store(throttlerConfig.CheckAsCheckSelf)
for _, appRule := range throttlerConfig.ThrottledApps {
throttler.ThrottleApp(appRule.Name, protoutil.TimeFromProto(appRule.ExpiresAt).UTC(), appRule.Ratio, appRule.Exempt)
}
if throttlerConfig.Enabled {
go throttler.Enable()
} else {
go throttler.Disable()
}
}
func (throttler *Throttler) IsEnabled() bool {
return throttler.isEnabled.Load()
}
func (throttler *Throttler) IsOpen() bool {
return throttler.isOpen.Load()
}
// CheckIsOpen checks if this throttler is ready to serve. If not, it
// returns an error.
func (throttler *Throttler) CheckIsOpen() error {
if throttler.IsOpen() {
// all good
return nil
}
return ErrThrottlerNotOpen
}
func (throttler *Throttler) IsRunning() bool {
return throttler.IsOpen() && throttler.IsEnabled()
}
// Enable activates the throttler probes; when enabled, the throttler responds to check queries based on
// the collected metrics.
// The function returns a WaitGroup that can be used to wait for the throttler to be fully disabled, ie when
// the Operate() goroutine function terminates and caches are invalidated.
func (throttler *Throttler) Enable() *sync.WaitGroup {
throttler.enableMutex.Lock()
defer throttler.enableMutex.Unlock()
if wasEnabled := throttler.isEnabled.Swap(true); wasEnabled {
log.Infof("Throttler: already enabled")
return nil
}
log.Infof("Throttler: enabling")
wg := &sync.WaitGroup{}
var ctx context.Context
ctx, throttler.cancelEnableContext = context.WithCancel(context.Background())
throttler.check.SelfChecks(ctx)
throttler.Operate(ctx, wg)
// Make a one-time request for a lease of heartbeats
go throttler.heartbeatWriter.RequestHeartbeats()
return wg
}
// Disable deactivates the probes and associated operations. When disabled, the throttler responds to check
// queries with "200 OK" irrespective of lag or any other metrics.
func (throttler *Throttler) Disable() bool {
throttler.enableMutex.Lock()
defer throttler.enableMutex.Unlock()
if wasEnabled := throttler.isEnabled.Swap(false); !wasEnabled {
log.Infof("Throttler: already disabled")
return false
}
log.Infof("Throttler: disabling")
// _ = throttler.updateConfig(ctx, false, throttler.MetricsThreshold.Get()) // TODO(shlomi)
throttler.cancelEnableContext()
return true
}
// retryReadAndApplyThrottlerConfig() is called by Open(), read throttler config from topo, applies it, and starts watching
// for topo changes.
// 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.
func (throttler *Throttler) retryReadAndApplyThrottlerConfig(ctx context.Context) {
var watchSrvKeyspaceOnce sync.Once
retryInterval := 10 * time.Second
retryTicker := time.NewTicker(retryInterval)
defer retryTicker.Stop()
for {
if !throttler.IsOpen() {
// Throttler is not open so no need to keep retrying.
log.Warningf("Throttler.retryReadAndApplyThrottlerConfig(): throttler no longer seems to be open, exiting")
return
}
requestCtx, requestCancel := context.WithTimeout(ctx, 5*time.Second)
defer requestCancel()
throttlerConfig, err := throttler.readThrottlerConfig(requestCtx)
if err == nil {
log.Infof("Throttler.retryReadAndApplyThrottlerConfig(): success reading throttler config: %+v", throttlerConfig)
// 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
go watchSrvKeyspaceOnce.Do(func() {
// We start watching SrvKeyspace only after we know it's been created. Now is that time!
// We watch using the given ctx, which is cancelled when the throttler is Close()d.
throttler.srvTopoServer.WatchSrvKeyspace(ctx, throttler.cell, throttler.keyspace, throttler.WatchSrvKeyspaceCallback)
})
return
}
log.Errorf("Throttler.retryReadAndApplyThrottlerConfig(): error reading throttler config. Will retry in %v. Err=%+v", retryInterval, err)
select {
case <-ctx.Done():
// Throttler is not open so no need to keep retrying.
log.Infof("Throttler.retryReadAndApplyThrottlerConfig(): throttler no longer seems to be open, exiting")
return
case <-retryTicker.C:
}
}
}
// Open opens database pool and initializes the schema
func (throttler *Throttler) Open() error {
log.Infof("Throttler: started execution of Open. Acquiring initMutex lock")
throttler.initMutex.Lock()
defer throttler.initMutex.Unlock()
isOpen := throttler.isOpen.Swap(true)
if isOpen {
// already open
log.Infof("Throttler: throttler is already open")
return nil
}
log.Infof("Throttler: opening")
var ctx context.Context
ctx, throttler.cancelOpenContext = context.WithCancel(context.Background())
// The query needs to be dynamically built because the sidecar database name
// is not known when the TabletServer is created, which in turn creates the
// Throttler.
throttler.metricsQuery.Store(sqlparser.BuildParsedQuery(defaultReplicationLagQuery, sidecar.GetIdentifier()).Query) // default
throttler.initConfig()
throttler.pool.Open(throttler.env.Config().DB.AppWithDB(), throttler.env.Config().DB.DbaWithDB(), throttler.env.Config().DB.AppDebugWithDB())
throttler.ThrottleApp("always-throttled-app", time.Now().Add(time.Hour*24*365*10), DefaultThrottleRatio, false)
go throttler.retryReadAndApplyThrottlerConfig(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()
isOpen := throttler.isOpen.Swap(false)
if !isOpen {
log.Infof("Throttler: throttler is not open")
return
}
throttler.Disable()
throttler.isLeader.Store(false)
// The below " != nil " checks are relevant to unit tests, where perhaps not all
// fields are supplied.
if throttler.pool != nil {
log.Infof("Throttler: closing pool")
throttler.pool.Close()
}
if throttler.cancelOpenContext != nil {
throttler.cancelOpenContext()
}
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.readSelfThrottleMetric(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,
Alias: "",
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.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 knowing 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, throttler.lastCheckTimeNano.Load())
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, wg *sync.WaitGroup) {
tickers := [](*timer.SuspendableTicker){}
addTicker := func(d time.Duration) *timer.SuspendableTicker {
t := timer.NewSuspendableTicker(d, false)
tickers = append(tickers, t)
return t
}
leaderCheckTicker := addTicker(throttler.leaderCheckInterval)
mysqlCollectTicker := addTicker(throttler.mysqlCollectInterval)
mysqlDormantCollectTicker := addTicker(throttler.mysqlDormantCollectInterval)
mysqlRefreshTicker := addTicker(throttler.mysqlRefreshInterval)
mysqlAggregateTicker := addTicker(throttler.mysqlAggregateInterval)
throttledAppsTicker := addTicker(throttler.throttledAppsSnapshotInterval)
recentCheckTicker := addTicker(time.Second)
wg.Add(1)
go func() {
defer func() {
throttler.aggregatedMetrics.Flush()
throttler.recentApps.Flush()
throttler.nonLowPriorityAppRequestsThrottled.Flush()
wg.Done()
}()
// we do not flush throttler.throttledApps because this is data submitted by the user; the user expects the data to survive a disable+enable
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()
}
tmClient := throttler.overrideTmClient
if tmClient == nil {
// This is the normal production behavior.
// throttler.overrideTmClient != nil only in unit testing
tmClient = tmclient.NewTabletManagerClient()
defer tmClient.Close()
}
for {
select {
case <-ctx.Done():
return
case <-leaderCheckTicker.C:
func() {
throttler.initMutex.Lock()
defer throttler.initMutex.Unlock()
// sparse
shouldBeLeader := false
if throttler.IsOpen() && throttler.tabletTypeFunc() == topodatapb.TabletType_PRIMARY {
shouldBeLeader = true
}
isLeader := throttler.isLeader.Swap(shouldBeLeader)
transitionedIntoLeader := false
if shouldBeLeader && !isLeader {
log.Infof("Throttler: transition into leadership")
transitionedIntoLeader = true
}
if !shouldBeLeader && isLeader {
log.Infof("Throttler: transition out of leadership")
}
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 throttler.IsOpen() {
// frequent
if !throttler.isDormant() {
throttler.collectMySQLMetrics(ctx, tmClient)
}
}
case <-mysqlDormantCollectTicker.C:
if throttler.IsOpen() {
// infrequent
if throttler.isDormant() {
throttler.collectMySQLMetrics(ctx, tmClient)
}
}
case metric := <-throttler.mysqlThrottleMetricChan:
// incoming MySQL metric, frequent, as result of collectMySQLMetrics()
throttler.mysqlInventory.TabletMetrics[metric.GetClusterTablet()] = metric
case <-mysqlRefreshTicker.C:
// sparse
if throttler.IsOpen() {
throttler.refreshMySQLInventory(ctx)
}
case probes := <-throttler.mysqlClusterProbesChan:
// incoming structural update, sparse, as result of refreshMySQLInventory()
throttler.updateMySQLClusterProbes(ctx, probes)
case <-mysqlAggregateTicker.C:
if throttler.IsOpen() {
throttler.aggregateMySQLMetrics(ctx)
}
case <-throttledAppsTicker.C:
if throttler.IsOpen() {
go throttler.expireThrottledApps()
}
case throttlerConfig := <-throttler.throttlerConfigChan:
throttler.applyThrottlerConfig(ctx, throttlerConfig)
case <-recentCheckTicker.C:
// Increment recentCheckTickerValue by one.
throttler.recentCheckTickerValue.Add(1)
}
}
}()
}
func (throttler *Throttler) generateTabletProbeFunction(ctx context.Context, clusterName string, tmClient tmclient.TabletManagerClient, probe *mysql.Probe) (probeFunc func() *mysql.MySQLThrottleMetric) {
return func() *mysql.MySQLThrottleMetric {
// Some reasonable timeout, to ensure we release connections even if they're hanging (otherwise grpc-go keeps polling those connections forever)
ctx, cancel := context.WithTimeout(ctx, 4*mysqlCollectInterval)
defer cancel()
// Hit a tablet's `check-self` via HTTP, and convert its CheckResult JSON output into a MySQLThrottleMetric
mySQLThrottleMetric := mysql.NewMySQLThrottleMetric()
mySQLThrottleMetric.ClusterName = clusterName
mySQLThrottleMetric.Alias = probe.Alias
if probe.Tablet == nil {
mySQLThrottleMetric.Err = fmt.Errorf("found nil tablet reference for alias %v, hostname %v", probe.Alias, probe.Tablet.Hostname)
return mySQLThrottleMetric
}
req := &tabletmanagerdatapb.CheckThrottlerRequest{} // We leave AppName empty; it will default to VitessName anyway, and we can save some proto space
resp, gRPCErr := tmClient.CheckThrottler(ctx, probe.Tablet, req)
if gRPCErr != nil {
mySQLThrottleMetric.Err = fmt.Errorf("gRPC error accessing tablet %v. Err=%v", probe.Alias, gRPCErr)
return mySQLThrottleMetric
}
mySQLThrottleMetric.Value = resp.Value
if resp.StatusCode == http.StatusInternalServerError {
mySQLThrottleMetric.Err = fmt.Errorf("Status code: %d", resp.StatusCode)
}
if resp.RecentlyChecked {
// We have just probed a tablet, and it reported back that someone just recently "check"ed it.
// We therefore renew the heartbeats lease.
go throttler.heartbeatWriter.RequestHeartbeats()
}
return mySQLThrottleMetric
}
}
func (throttler *Throttler) collectMySQLMetrics(ctx context.Context, tmClient tmclient.TabletManagerClient) error {
// synchronously, get lists of probes
for clusterName, probes := range throttler.mysqlInventory.ClustersProbes {
clusterName := clusterName
// probes is known not to change. It can be *replaced*, but not changed.
// so it's safe to iterate it
for _, probe := range probes {
go func(probe *mysql.Probe) {
// 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 {
// Throttler is probing its own tablet's metrics:
throttleMetricFunc = throttler.generateSelfMySQLThrottleMetricFunc(ctx, probe)
} else {
// Throttler probing other tablets:
throttleMetricFunc = throttler.generateTabletProbeFunction(ctx, clusterName, tmClient, probe)
}
throttleMetrics := mysql.ReadThrottleMetric(probe, clusterName, throttleMetricFunc)
select {
case <-ctx.Done():
return
case throttler.mysqlThrottleMetricChan <- throttleMetrics:
}
}(probe)
}
}
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.Load()
addProbe := func(alias string, tablet *topodatapb.Tablet, clusterName string, clusterSettings *config.MySQLClusterConfigurationSettings, probes mysql.Probes) bool {
for _, ignore := range clusterSettings.IgnoreHosts {
if strings.Contains(alias, ignore) {
log.Infof("Throttler: tablet ignored: %+v", alias)
return false
}
}
if clusterName != selfStoreName {
if alias == "" {
log.Errorf("Throttler: got empty alias for cluster: %+v", clusterName)
return false
}
if tablet == nil {
log.Errorf("Throttler: got nil tablet for alias: %v in cluster: %+v", alias, clusterName)
return false
}
}
probe := &mysql.Probe{
Alias: alias,
Tablet: tablet,
MetricQuery: clusterSettings.MetricQuery,
CacheMillis: clusterSettings.CacheMillis,
}
probes[alias] = probe
return true
}
attemptWriteProbes := func(clusterProbes *mysql.ClusterProbes) error {
select {
case <-ctx.Done():
return ctx.Err()
case throttler.mysqlClusterProbesChan <- clusterProbes:
return nil
}
}
for clusterName, clusterSettings := range throttler.configSettings.Stores.MySQL.Clusters {
clusterName := clusterName
clusterSettings.MetricQuery = metricsQuery
clusterSettings.ThrottleThreshold.Store(metricsThreshold)
clusterSettingsCopy := *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:
collect := func() error {
throttler.mysqlClusterThresholds.Set(clusterName, math.Float64frombits(clusterSettingsCopy.ThrottleThreshold.Load()), cache.DefaultExpiration)
clusterProbes := &mysql.ClusterProbes{
ClusterName: clusterName,
IgnoreHostsCount: clusterSettingsCopy.IgnoreHostsCount,
TabletProbes: 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.
addProbe("", nil, clusterName, &clusterSettingsCopy, clusterProbes.TabletProbes)
return attemptWriteProbes(clusterProbes)
}
if !throttler.isLeader.Load() {
// This tablet may have used to be the primary, but it isn't now. It may have a recollection
// of previous clusters it used to probe. It may have recollection of specific probes for such clusters.
// This now ensures any existing cluster probes are overridden with an empty list of probes.
// `clusterProbes` was created above as empty, and identifiable via `clusterName`. This will in turn
// be used to overwrite throttler.mysqlInventory.ClustersProbes[clusterProbes.ClusterName] in
// updateMySQLClusterProbes().
return attemptWriteProbes(clusterProbes)
// not the leader (primary tablet)? Then no more work for us.
}
// The primary tablet is also in charge of collecting the shard's metrics
ctx, cancel := context.WithTimeout(ctx, mysqlRefreshInterval)
defer cancel()
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] {
addProbe(topoproto.TabletAliasString(tabletAlias), tablet.Tablet, clusterName, &clusterSettingsCopy, clusterProbes.TabletProbes)
}
}
return attemptWriteProbes(clusterProbes)
}
go func() {
if err := collect(); 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.TabletProbes
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.TabletMetrics, ignoreHostsCount, throttler.configSettings.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 some period and with some ratio.
func (throttler *Throttler) ThrottleApp(appName string, expireAt time.Time, ratio float64, exempt bool) (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
}
appThrottle.Exempt = exempt