multi_rate_limiter_test.go 4.1 KB
Newer Older
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
// 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 (
	"math"
	"testing"

	"github.com/milvus-io/milvus/internal/proto/internalpb"
	"github.com/milvus-io/milvus/internal/util/ratelimitutil"
	"github.com/stretchr/testify/assert"
)

func TestMultiRateLimiter(t *testing.T) {
	t.Run("test multiRateLimiter", func(t *testing.T) {
B
bigsheeper 已提交
30 31
		bak := Params.QuotaConfig.QuotaAndLimitsEnabled
		Params.QuotaConfig.QuotaAndLimitsEnabled = true
32 33 34 35 36 37 38 39 40 41 42 43
		multiLimiter := NewMultiRateLimiter()
		for _, rt := range internalpb.RateType_value {
			multiLimiter.globalRateLimiter.limiters[internalpb.RateType(rt)] = ratelimitutil.NewLimiter(ratelimitutil.Limit(1000), 1)
		}
		for _, rt := range internalpb.RateType_value {
			ok, _ := multiLimiter.Limit(internalpb.RateType(rt), 1)
			assert.False(t, ok)
			ok, _ = multiLimiter.Limit(internalpb.RateType(rt), math.MaxInt)
			assert.False(t, ok)
			ok, _ = multiLimiter.Limit(internalpb.RateType(rt), math.MaxInt)
			assert.True(t, ok)
		}
B
bigsheeper 已提交
44
		Params.QuotaConfig.QuotaAndLimitsEnabled = bak
45 46 47 48
	})

	t.Run("not enable quotaAndLimit", func(t *testing.T) {
		multiLimiter := NewMultiRateLimiter()
B
bigsheeper 已提交
49 50
		bak := Params.QuotaConfig.QuotaAndLimitsEnabled
		Params.QuotaConfig.QuotaAndLimitsEnabled = false
51 52 53 54 55
		for _, rt := range internalpb.RateType_value {
			ok, r := multiLimiter.Limit(internalpb.RateType(rt), 1)
			assert.False(t, ok)
			assert.NotEqual(t, float64(0), r)
		}
B
bigsheeper 已提交
56
		Params.QuotaConfig.QuotaAndLimitsEnabled = bak
57
	})
58 59 60 61 62 63 64 65 66 67 68 69 70 71

	t.Run("test limit", func(t *testing.T) {
		run := func(insertRate float64) {
			bakInsertRate := Params.QuotaConfig.DMLMaxInsertRate
			Params.QuotaConfig.DMLMaxInsertRate = insertRate
			multiLimiter := NewMultiRateLimiter()
			bak := Params.QuotaConfig.QuotaAndLimitsEnabled
			Params.QuotaConfig.QuotaAndLimitsEnabled = true
			ok, r := multiLimiter.Limit(internalpb.RateType_DMLInsert, 1*1024*1024)
			assert.False(t, ok)
			assert.NotEqual(t, float64(0), r)
			Params.QuotaConfig.QuotaAndLimitsEnabled = bak
			Params.QuotaConfig.DMLMaxInsertRate = bakInsertRate
		}
72 73 74 75 76
		run(math.MaxFloat64)
		run(math.MaxFloat64 / 1.2)
		run(math.MaxFloat64 / 2)
		run(math.MaxFloat64 / 3)
		run(math.MaxFloat64 / 10000)
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
}

func TestRateLimiter(t *testing.T) {
	t.Run("test limit", func(t *testing.T) {
		limiter := newRateLimiter()
		limiter.registerLimiters()
		for _, rt := range internalpb.RateType_value {
			limiter.limiters[internalpb.RateType(rt)] = ratelimitutil.NewLimiter(ratelimitutil.Limit(1000), 1)
		}
		for _, rt := range internalpb.RateType_value {
			ok, _ := limiter.limit(internalpb.RateType(rt), 1)
			assert.False(t, ok)
			ok, _ = limiter.limit(internalpb.RateType(rt), math.MaxInt)
			assert.False(t, ok)
			ok, _ = limiter.limit(internalpb.RateType(rt), math.MaxInt)
			assert.True(t, ok)
		}
	})

	t.Run("test setRates", func(t *testing.T) {
		limiter := newRateLimiter()
		for _, rt := range internalpb.RateType_value {
			limiter.limiters[internalpb.RateType(rt)] = ratelimitutil.NewLimiter(ratelimitutil.Limit(1000), 1)
		}

		zeroRates := make([]*internalpb.Rate, 0, len(internalpb.RateType_value))
		for _, rt := range internalpb.RateType_value {
			zeroRates = append(zeroRates, &internalpb.Rate{
				Rt: internalpb.RateType(rt), R: 0,
			})
		}
		err := limiter.setRates(zeroRates)
		assert.NoError(t, err)
		for _, rt := range internalpb.RateType_value {
			for i := 0; i < 100; i++ {
113 114
				ok, _ := limiter.limit(internalpb.RateType(rt), 1)
				assert.True(t, ok)
115 116 117 118
			}
		}
	})
}