-
Notifications
You must be signed in to change notification settings - Fork 726
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
*: add API concurrency metrics #7541
Merged
ti-chi-bot
merged 28 commits into
tikv:master
from
CabinfeverB:rate_limit/concurrency_limit
Dec 28, 2023
Merged
Changes from 11 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
b8be11c
refactor ratelimit pkg
CabinfeverB 5c242b8
refactor ratelimit pkg
CabinfeverB b8cde6f
refactor test
CabinfeverB 753c9d4
address comment
CabinfeverB 9fb345f
Merge branch 'master' into rate_limit/refactor
CabinfeverB 8c6a990
address comment
CabinfeverB 12d9529
address comment
CabinfeverB a815282
address comment
CabinfeverB 4748cf5
address comment
CabinfeverB 5514e9b
concurrency metrics
CabinfeverB 4d70445
fix conflict
CabinfeverB 8da0b38
fix conflict
CabinfeverB ed2de59
address comment
CabinfeverB c620463
address comment
CabinfeverB 31d416d
Merge branch 'master' into rate_limit/concurrency_limit
CabinfeverB 12faadd
address comment
CabinfeverB d76914e
address comment
CabinfeverB ce33540
fix test
CabinfeverB b52c4f6
fix test
CabinfeverB e1ef4d2
Merge branch 'master' into rate_limit/concurrency_limit
CabinfeverB 469005b
fix
CabinfeverB 170acf1
merge master
CabinfeverB a1f8fd0
merge master
CabinfeverB bf14f9a
address comment
CabinfeverB 62bc1d8
Merge branch 'master' into rate_limit/concurrency_limit
CabinfeverB b3311b7
fix
CabinfeverB d8cab6c
fix
CabinfeverB c34d2b1
address comment
CabinfeverB File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright 2023 TiKV Project 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. | ||
|
||
package ratelimit | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"golang.org/x/time/rate" | ||
) | ||
|
||
var emptyFunc = func() {} | ||
|
||
// Controller is a controller which holds multiple limiters to manage the request rate of different objects. | ||
type Controller struct { | ||
limiters sync.Map | ||
// the label which is in labelAllowList won't be limited, and only inited by hard code. | ||
labelAllowList map[string]struct{} | ||
|
||
ctx context.Context | ||
cancel context.CancelFunc | ||
apiType string | ||
concurrencyGauge *prometheus.GaugeVec | ||
} | ||
|
||
// NewController returns a global limiter which can be updated in the later. | ||
func NewController(ctx context.Context, typ string, concurrencyGauge *prometheus.GaugeVec) *Controller { | ||
ctx, cancel := context.WithCancel(ctx) | ||
l := &Controller{ | ||
ctx: ctx, | ||
cancel: cancel, | ||
labelAllowList: make(map[string]struct{}), | ||
apiType: typ, | ||
concurrencyGauge: concurrencyGauge, | ||
} | ||
if concurrencyGauge != nil { | ||
go l.collectMetrics() | ||
} | ||
return l | ||
} | ||
|
||
func (l *Controller) Close() { | ||
l.cancel() | ||
} | ||
|
||
func (l *Controller) collectMetrics() { | ||
tricker := time.NewTicker(time.Second * 5) | ||
defer tricker.Stop() | ||
for { | ||
select { | ||
case <-l.ctx.Done(): | ||
return | ||
case <-tricker.C: | ||
l.limiters.Range(func(key, value any) bool { | ||
limiter := value.(*limiter) | ||
label := key.(string) | ||
// Due to not in hot path, no need to save sub Gauge. | ||
if con := limiter.getConcurrencyLimiter(); con != nil { | ||
l.concurrencyGauge.WithLabelValues(l.apiType, label).Set(float64(con.getCurrent())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we can find another way to show the statistics, the current implementation only captures the instant value every 15s. |
||
} | ||
return true | ||
}) | ||
} | ||
} | ||
} | ||
|
||
// Allow is used to check whether it has enough token. | ||
func (l *Controller) Allow(label string) (DoneFunc, error) { | ||
var ok bool | ||
lim, ok := l.limiters.Load(label) | ||
if ok { | ||
return lim.(*limiter).allow() | ||
} | ||
return emptyFunc, nil | ||
} | ||
|
||
// Update is used to update Ratelimiter with Options | ||
func (l *Controller) Update(label string, opts ...Option) UpdateStatus { | ||
var status UpdateStatus | ||
for _, opt := range opts { | ||
status |= opt(label, l) | ||
} | ||
return status | ||
} | ||
|
||
// GetQPSLimiterStatus returns the status of a given label's QPS limiter. | ||
func (l *Controller) GetQPSLimiterStatus(label string) (limit rate.Limit, burst int) { | ||
if limit, exist := l.limiters.Load(label); exist { | ||
return limit.(*limiter).getQPSLimiterStatus() | ||
} | ||
return 0, 0 | ||
} | ||
|
||
// GetConcurrencyLimiterStatus returns the status of a given label's concurrency limiter. | ||
func (l *Controller) GetConcurrencyLimiterStatus(label string) (limit uint64, current uint64) { | ||
if limit, exist := l.limiters.Load(label); exist { | ||
return limit.(*limiter).getConcurrencyLimiterStatus() | ||
} | ||
return 0, 0 | ||
} | ||
|
||
// IsInAllowList returns whether this label is in allow list. | ||
// If returns true, the given label won't be limited | ||
func (l *Controller) IsInAllowList(label string) bool { | ||
_, allow := l.labelAllowList[label] | ||
return allow | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.