2022-09-16 09:56:47 +08:00
|
|
|
// Licensed to the LF AI & Data foundation under one
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
// distributed with this work for additional information
|
|
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
|
|
// to you 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 proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-02-01 16:03:51 +08:00
|
|
|
"strconv"
|
2022-12-30 18:35:32 +08:00
|
|
|
"sync"
|
2022-09-16 09:56:47 +08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2023-01-06 14:31:37 +08:00
|
|
|
"github.com/milvus-io/milvus-proto/go-api/commonpb"
|
2022-12-30 18:35:32 +08:00
|
|
|
"github.com/milvus-io/milvus-proto/go-api/milvuspb"
|
2023-02-01 16:03:51 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/config"
|
2022-09-16 09:56:47 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
|
|
"github.com/milvus-io/milvus/internal/metrics"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/internalpb"
|
2022-11-04 14:25:38 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/paramtable"
|
2022-09-16 09:56:47 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/ratelimitutil"
|
2023-02-01 16:03:51 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/typeutil"
|
2022-09-16 09:56:47 +08:00
|
|
|
)
|
|
|
|
|
2023-01-06 14:31:37 +08:00
|
|
|
var QuotaErrorString = map[commonpb.ErrorCode]string{
|
|
|
|
commonpb.ErrorCode_ForceDeny: "manually force deny",
|
|
|
|
commonpb.ErrorCode_MemoryQuotaExhausted: "memory quota exhausted, please allocate more resources",
|
|
|
|
commonpb.ErrorCode_DiskQuotaExhausted: "disk quota exhausted, please allocate more resources",
|
|
|
|
commonpb.ErrorCode_TimeTickLongDelay: "time tick long delay",
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetQuotaErrorString(errCode commonpb.ErrorCode) string {
|
|
|
|
return QuotaErrorString[errCode]
|
|
|
|
}
|
|
|
|
|
2022-09-16 09:56:47 +08:00
|
|
|
// MultiRateLimiter includes multilevel rate limiters, such as global rateLimiter,
|
|
|
|
// collection level rateLimiter and so on. It also implements Limiter interface.
|
|
|
|
type MultiRateLimiter struct {
|
|
|
|
globalRateLimiter *rateLimiter
|
|
|
|
// TODO: add collection level rateLimiter
|
2022-12-30 18:35:32 +08:00
|
|
|
quotaStatesMu sync.RWMutex
|
2023-01-06 14:31:37 +08:00
|
|
|
quotaStates map[milvuspb.QuotaState]commonpb.ErrorCode
|
2022-09-16 09:56:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewMultiRateLimiter returns a new MultiRateLimiter.
|
|
|
|
func NewMultiRateLimiter() *MultiRateLimiter {
|
|
|
|
m := &MultiRateLimiter{}
|
|
|
|
m.globalRateLimiter = newRateLimiter()
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2022-12-30 18:35:32 +08:00
|
|
|
// Check checks if request would be limited or denied.
|
2023-01-06 14:31:37 +08:00
|
|
|
func (m *MultiRateLimiter) Check(rt internalpb.RateType, n int) commonpb.ErrorCode {
|
2022-12-07 18:01:19 +08:00
|
|
|
if !Params.QuotaConfig.QuotaAndLimitsEnabled.GetAsBool() {
|
2023-01-06 14:31:37 +08:00
|
|
|
return commonpb.ErrorCode_Success
|
2022-12-30 18:35:32 +08:00
|
|
|
}
|
|
|
|
limit, rate := m.globalRateLimiter.limit(rt, n)
|
|
|
|
if rate == 0 {
|
2023-01-06 14:31:37 +08:00
|
|
|
return m.GetErrorCode(rt)
|
2022-12-30 18:35:32 +08:00
|
|
|
}
|
|
|
|
if limit {
|
2023-01-06 14:31:37 +08:00
|
|
|
return commonpb.ErrorCode_RateLimit
|
2022-12-30 18:35:32 +08:00
|
|
|
}
|
2023-01-06 14:31:37 +08:00
|
|
|
return commonpb.ErrorCode_Success
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MultiRateLimiter) GetErrorCode(rt internalpb.RateType) commonpb.ErrorCode {
|
|
|
|
switch rt {
|
|
|
|
case internalpb.RateType_DMLInsert, internalpb.RateType_DMLDelete, internalpb.RateType_DMLBulkLoad:
|
|
|
|
m.quotaStatesMu.RLock()
|
|
|
|
defer m.quotaStatesMu.RUnlock()
|
|
|
|
return m.quotaStates[milvuspb.QuotaState_DenyToWrite]
|
|
|
|
case internalpb.RateType_DQLSearch, internalpb.RateType_DQLQuery:
|
|
|
|
m.quotaStatesMu.RLock()
|
|
|
|
defer m.quotaStatesMu.RUnlock()
|
|
|
|
return m.quotaStates[milvuspb.QuotaState_DenyToRead]
|
|
|
|
}
|
|
|
|
return commonpb.ErrorCode_Success
|
2022-12-30 18:35:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetQuotaStates returns quota states.
|
|
|
|
func (m *MultiRateLimiter) GetQuotaStates() ([]milvuspb.QuotaState, []string) {
|
|
|
|
m.quotaStatesMu.RLock()
|
|
|
|
defer m.quotaStatesMu.RUnlock()
|
|
|
|
states := make([]milvuspb.QuotaState, 0, len(m.quotaStates))
|
|
|
|
reasons := make([]string, 0, len(m.quotaStates))
|
|
|
|
for k, v := range m.quotaStates {
|
|
|
|
states = append(states, k)
|
2023-01-06 14:31:37 +08:00
|
|
|
reasons = append(reasons, GetQuotaErrorString(v))
|
2022-12-30 18:35:32 +08:00
|
|
|
}
|
|
|
|
return states, reasons
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetQuotaStates sets quota states for MultiRateLimiter.
|
2023-01-06 14:31:37 +08:00
|
|
|
func (m *MultiRateLimiter) SetQuotaStates(states []milvuspb.QuotaState, codes []commonpb.ErrorCode) {
|
2022-12-30 18:35:32 +08:00
|
|
|
m.quotaStatesMu.Lock()
|
|
|
|
defer m.quotaStatesMu.Unlock()
|
2023-01-06 14:31:37 +08:00
|
|
|
m.quotaStates = make(map[milvuspb.QuotaState]commonpb.ErrorCode, len(states))
|
2022-12-30 18:35:32 +08:00
|
|
|
for i := 0; i < len(states); i++ {
|
2023-01-06 14:31:37 +08:00
|
|
|
m.quotaStates[states[i]] = codes[i]
|
2022-09-16 09:56:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// rateLimiter implements Limiter.
|
|
|
|
type rateLimiter struct {
|
2023-02-01 16:03:51 +08:00
|
|
|
limiters *typeutil.ConcurrentMap[internalpb.RateType, *ratelimitutil.Limiter]
|
2022-09-16 09:56:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// newRateLimiter returns a new RateLimiter.
|
|
|
|
func newRateLimiter() *rateLimiter {
|
|
|
|
rl := &rateLimiter{
|
2023-02-01 16:03:51 +08:00
|
|
|
limiters: typeutil.NewConcurrentMap[internalpb.RateType, *ratelimitutil.Limiter](),
|
2022-09-16 09:56:47 +08:00
|
|
|
}
|
|
|
|
rl.registerLimiters()
|
|
|
|
return rl
|
|
|
|
}
|
|
|
|
|
|
|
|
// limit returns true, the request will be rejected.
|
|
|
|
// Otherwise, the request will pass.
|
|
|
|
func (rl *rateLimiter) limit(rt internalpb.RateType, n int) (bool, float64) {
|
2023-02-01 16:03:51 +08:00
|
|
|
limit, ok := rl.limiters.Get(rt)
|
|
|
|
if !ok {
|
|
|
|
return false, -1
|
|
|
|
}
|
|
|
|
return !limit.AllowN(time.Now(), n), float64(limit.Limit())
|
2022-09-16 09:56:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// setRates sets new rates for the limiters.
|
|
|
|
func (rl *rateLimiter) setRates(rates []*internalpb.Rate) error {
|
|
|
|
for _, r := range rates {
|
2023-02-01 16:03:51 +08:00
|
|
|
if limit, ok := rl.limiters.Get(r.GetRt()); ok {
|
|
|
|
limit.SetLimit(ratelimitutil.Limit(r.GetR()))
|
2022-11-04 14:25:38 +08:00
|
|
|
metrics.SetRateGaugeByRateType(r.GetRt(), paramtable.GetNodeID(), r.GetR())
|
2022-09-16 09:56:47 +08:00
|
|
|
} else {
|
|
|
|
return fmt.Errorf("unregister rateLimiter for rateType %s", r.GetRt().String())
|
|
|
|
}
|
|
|
|
}
|
2022-12-30 18:35:32 +08:00
|
|
|
// rl.printRates(rates)
|
2022-09-16 09:56:47 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// printRates logs the rate info.
|
|
|
|
func (rl *rateLimiter) printRates(rates []*internalpb.Rate) {
|
|
|
|
//fmt.Printf("RateLimiter set rates:\n---------------------------------\n")
|
|
|
|
//for _, r := range rates {
|
2022-10-27 17:37:32 +08:00
|
|
|
// fmt.Printf("%s -> %v\n", r.GetRt().String(), r.GetR())
|
2022-09-16 09:56:47 +08:00
|
|
|
//}
|
|
|
|
//fmt.Printf("---------------------------------\n")
|
|
|
|
log.Debug("RateLimiter setRates", zap.Any("rates", rates))
|
|
|
|
}
|
|
|
|
|
|
|
|
// registerLimiters register limiter for all rate types.
|
|
|
|
func (rl *rateLimiter) registerLimiters() {
|
2023-02-01 16:03:51 +08:00
|
|
|
quotaConfig := &Params.QuotaConfig
|
2022-09-16 09:56:47 +08:00
|
|
|
for rt := range internalpb.RateType_name {
|
2023-02-01 16:03:51 +08:00
|
|
|
var r *paramtable.ParamItem
|
2022-09-16 09:56:47 +08:00
|
|
|
switch internalpb.RateType(rt) {
|
|
|
|
case internalpb.RateType_DDLCollection:
|
2023-02-01 16:03:51 +08:00
|
|
|
r = "aConfig.DDLCollectionRate
|
2022-09-16 09:56:47 +08:00
|
|
|
case internalpb.RateType_DDLPartition:
|
2023-02-01 16:03:51 +08:00
|
|
|
r = "aConfig.DDLPartitionRate
|
2022-09-16 09:56:47 +08:00
|
|
|
case internalpb.RateType_DDLIndex:
|
2023-02-01 16:03:51 +08:00
|
|
|
r = "aConfig.MaxIndexRate
|
2022-09-16 09:56:47 +08:00
|
|
|
case internalpb.RateType_DDLFlush:
|
2023-02-01 16:03:51 +08:00
|
|
|
r = "aConfig.MaxFlushRate
|
2022-09-16 09:56:47 +08:00
|
|
|
case internalpb.RateType_DDLCompaction:
|
2023-02-01 16:03:51 +08:00
|
|
|
r = "aConfig.MaxCompactionRate
|
2022-09-16 09:56:47 +08:00
|
|
|
case internalpb.RateType_DMLInsert:
|
2023-02-01 16:03:51 +08:00
|
|
|
r = "aConfig.DMLMaxInsertRate
|
2022-09-16 09:56:47 +08:00
|
|
|
case internalpb.RateType_DMLDelete:
|
2023-02-01 16:03:51 +08:00
|
|
|
r = "aConfig.DMLMaxDeleteRate
|
2022-09-16 09:56:47 +08:00
|
|
|
case internalpb.RateType_DMLBulkLoad:
|
2023-02-01 16:03:51 +08:00
|
|
|
r = "aConfig.DMLMaxBulkLoadRate
|
2022-09-16 09:56:47 +08:00
|
|
|
case internalpb.RateType_DQLSearch:
|
2023-02-01 16:03:51 +08:00
|
|
|
r = "aConfig.DQLMaxSearchRate
|
2022-09-16 09:56:47 +08:00
|
|
|
case internalpb.RateType_DQLQuery:
|
2023-02-01 16:03:51 +08:00
|
|
|
r = "aConfig.DQLMaxQueryRate
|
2022-09-16 09:56:47 +08:00
|
|
|
}
|
2023-02-01 16:03:51 +08:00
|
|
|
limit := ratelimitutil.Limit(r.GetAsFloat())
|
|
|
|
burst := r.GetAsFloat() // use rate as burst, because Limiter is with punishment mechanism, burst is insignificant.
|
|
|
|
rl.limiters.InsertIfNotPresent(internalpb.RateType(rt), ratelimitutil.NewLimiter(limit, burst))
|
|
|
|
onEvent := func(rateType internalpb.RateType) func(*config.Event) {
|
|
|
|
return func(event *config.Event) {
|
|
|
|
f, err := strconv.ParseFloat(event.Value, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Info("Error format for rateLimit",
|
|
|
|
zap.String("rateType", rateType.String()),
|
|
|
|
zap.String("key", event.Key),
|
|
|
|
zap.String("value", event.Value),
|
|
|
|
zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
limit, ok := rl.limiters.Get(rateType)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
limit.SetLimit(ratelimitutil.Limit(f))
|
|
|
|
}
|
|
|
|
}(internalpb.RateType(rt))
|
|
|
|
paramtable.Get().Watch(r.Key, config.NewHandler(fmt.Sprintf("rateLimiter-%d", rt), onEvent))
|
2022-10-28 19:05:32 +08:00
|
|
|
log.Info("RateLimiter register for rateType",
|
|
|
|
zap.String("rateType", internalpb.RateType_name[rt]),
|
2023-02-01 16:03:51 +08:00
|
|
|
zap.String("rate", ratelimitutil.Limit(r.GetAsFloat()).String()),
|
2022-10-28 19:05:32 +08:00
|
|
|
zap.String("burst", fmt.Sprintf("%v", burst)))
|
2022-09-16 09:56:47 +08:00
|
|
|
}
|
|
|
|
}
|