mirror of
https://gitee.com/milvus-io/milvus.git
synced 2024-12-02 20:09:57 +08:00
941dc755df
flush rate control at collection level to avoid generate too much segment. 0.1 qps by default. issue: #29477 Signed-off-by: chyezh <ye.zhen@zilliz.com>
228 lines
9.1 KiB
Go
228 lines
9.1 KiB
Go
// 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 (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/mock"
|
|
"google.golang.org/grpc"
|
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/milvuspb"
|
|
"github.com/milvus-io/milvus/internal/proto/internalpb"
|
|
"github.com/milvus-io/milvus/pkg/util/merr"
|
|
)
|
|
|
|
type limiterMock struct {
|
|
limit bool
|
|
rate float64
|
|
quotaStates []milvuspb.QuotaState
|
|
quotaStateReasons []commonpb.ErrorCode
|
|
}
|
|
|
|
func (l *limiterMock) Check(collection []int64, rt internalpb.RateType, n int) error {
|
|
if l.rate == 0 {
|
|
return merr.ErrServiceQuotaExceeded
|
|
}
|
|
if l.limit {
|
|
return merr.ErrServiceRateLimit
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func TestRateLimitInterceptor(t *testing.T) {
|
|
t.Run("test getRequestInfo", func(t *testing.T) {
|
|
mockCache := NewMockCache(t)
|
|
mockCache.On("GetCollectionID",
|
|
mock.Anything, // context.Context
|
|
mock.AnythingOfType("string"),
|
|
mock.AnythingOfType("string"),
|
|
).Return(int64(0), nil)
|
|
globalMetaCache = mockCache
|
|
collection, rt, size, err := getRequestInfo(&milvuspb.InsertRequest{})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, proto.Size(&milvuspb.InsertRequest{}), size)
|
|
assert.Equal(t, internalpb.RateType_DMLInsert, rt)
|
|
assert.ElementsMatch(t, collection, []int64{int64(0)})
|
|
|
|
collection, rt, size, err = getRequestInfo(&milvuspb.UpsertRequest{})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, proto.Size(&milvuspb.InsertRequest{}), size)
|
|
assert.Equal(t, internalpb.RateType_DMLUpsert, rt)
|
|
assert.ElementsMatch(t, collection, []int64{int64(0)})
|
|
|
|
collection, rt, size, err = getRequestInfo(&milvuspb.DeleteRequest{})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, proto.Size(&milvuspb.DeleteRequest{}), size)
|
|
assert.Equal(t, internalpb.RateType_DMLDelete, rt)
|
|
assert.ElementsMatch(t, collection, []int64{int64(0)})
|
|
|
|
collection, rt, size, err = getRequestInfo(&milvuspb.ImportRequest{})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, proto.Size(&milvuspb.ImportRequest{}), size)
|
|
assert.Equal(t, internalpb.RateType_DMLBulkLoad, rt)
|
|
assert.ElementsMatch(t, collection, []int64{int64(0)})
|
|
|
|
collection, rt, size, err = getRequestInfo(&milvuspb.SearchRequest{Nq: 5})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 5, size)
|
|
assert.Equal(t, internalpb.RateType_DQLSearch, rt)
|
|
assert.ElementsMatch(t, collection, []int64{int64(0)})
|
|
|
|
collection, rt, size, err = getRequestInfo(&milvuspb.QueryRequest{})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 1, size)
|
|
assert.Equal(t, internalpb.RateType_DQLQuery, rt)
|
|
assert.ElementsMatch(t, collection, []int64{int64(0)})
|
|
|
|
collection, rt, size, err = getRequestInfo(&milvuspb.CreateCollectionRequest{})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 1, size)
|
|
assert.Equal(t, internalpb.RateType_DDLCollection, rt)
|
|
assert.ElementsMatch(t, collection, []int64{int64(0)})
|
|
|
|
collection, rt, size, err = getRequestInfo(&milvuspb.LoadCollectionRequest{})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 1, size)
|
|
assert.Equal(t, internalpb.RateType_DDLCollection, rt)
|
|
assert.ElementsMatch(t, collection, []int64{int64(0)})
|
|
|
|
collection, rt, size, err = getRequestInfo(&milvuspb.ReleaseCollectionRequest{})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 1, size)
|
|
assert.Equal(t, internalpb.RateType_DDLCollection, rt)
|
|
assert.ElementsMatch(t, collection, []int64{int64(0)})
|
|
|
|
collection, rt, size, err = getRequestInfo(&milvuspb.DropCollectionRequest{})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 1, size)
|
|
assert.Equal(t, internalpb.RateType_DDLCollection, rt)
|
|
assert.ElementsMatch(t, collection, []int64{int64(0)})
|
|
|
|
collection, rt, size, err = getRequestInfo(&milvuspb.CreatePartitionRequest{})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 1, size)
|
|
assert.Equal(t, internalpb.RateType_DDLPartition, rt)
|
|
assert.ElementsMatch(t, collection, []int64{int64(0)})
|
|
|
|
collection, rt, size, err = getRequestInfo(&milvuspb.LoadPartitionsRequest{})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 1, size)
|
|
assert.Equal(t, internalpb.RateType_DDLPartition, rt)
|
|
assert.ElementsMatch(t, collection, []int64{int64(0)})
|
|
|
|
collection, rt, size, err = getRequestInfo(&milvuspb.ReleasePartitionsRequest{})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 1, size)
|
|
assert.Equal(t, internalpb.RateType_DDLPartition, rt)
|
|
assert.ElementsMatch(t, collection, []int64{int64(0)})
|
|
|
|
collection, rt, size, err = getRequestInfo(&milvuspb.DropPartitionRequest{})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 1, size)
|
|
assert.Equal(t, internalpb.RateType_DDLPartition, rt)
|
|
assert.ElementsMatch(t, collection, []int64{int64(0)})
|
|
|
|
collection, rt, size, err = getRequestInfo(&milvuspb.CreateIndexRequest{})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 1, size)
|
|
assert.Equal(t, internalpb.RateType_DDLIndex, rt)
|
|
assert.ElementsMatch(t, collection, []int64{int64(0)})
|
|
|
|
collection, rt, size, err = getRequestInfo(&milvuspb.DropIndexRequest{})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 1, size)
|
|
assert.Equal(t, internalpb.RateType_DDLIndex, rt)
|
|
assert.ElementsMatch(t, collection, []int64{int64(0)})
|
|
|
|
collection, rt, size, err = getRequestInfo(&milvuspb.FlushRequest{})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 1, size)
|
|
assert.Equal(t, internalpb.RateType_DDLFlush, rt)
|
|
assert.Len(t, collection, 0)
|
|
|
|
collection, rt, size, err = getRequestInfo(&milvuspb.ManualCompactionRequest{})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 1, size)
|
|
assert.Equal(t, internalpb.RateType_DDLCompaction, rt)
|
|
assert.Len(t, collection, 0)
|
|
})
|
|
|
|
t.Run("test getFailedResponse", func(t *testing.T) {
|
|
testGetFailedResponse := func(req interface{}, rt internalpb.RateType, err error, fullMethod string) {
|
|
rsp := getFailedResponse(req, err)
|
|
assert.NotNil(t, rsp)
|
|
}
|
|
|
|
testGetFailedResponse(&milvuspb.DeleteRequest{}, internalpb.RateType_DMLDelete, merr.ErrServiceQuotaExceeded, "delete")
|
|
testGetFailedResponse(&milvuspb.UpsertRequest{}, internalpb.RateType_DMLUpsert, merr.ErrServiceQuotaExceeded, "upsert")
|
|
testGetFailedResponse(&milvuspb.ImportRequest{}, internalpb.RateType_DMLBulkLoad, merr.ErrServiceMemoryLimitExceeded, "import")
|
|
testGetFailedResponse(&milvuspb.SearchRequest{}, internalpb.RateType_DQLSearch, merr.ErrServiceDiskLimitExceeded, "search")
|
|
testGetFailedResponse(&milvuspb.QueryRequest{}, internalpb.RateType_DQLQuery, merr.ErrServiceQuotaExceeded, "query")
|
|
testGetFailedResponse(&milvuspb.CreateCollectionRequest{}, internalpb.RateType_DDLCollection, merr.ErrServiceRateLimit, "createCollection")
|
|
testGetFailedResponse(&milvuspb.FlushRequest{}, internalpb.RateType_DDLFlush, merr.ErrServiceRateLimit, "flush")
|
|
testGetFailedResponse(&milvuspb.ManualCompactionRequest{}, internalpb.RateType_DDLCompaction, merr.ErrServiceRateLimit, "compaction")
|
|
|
|
// test illegal
|
|
rsp := getFailedResponse(&milvuspb.SearchResults{}, merr.OldCodeToMerr(commonpb.ErrorCode_UnexpectedError))
|
|
assert.Nil(t, rsp)
|
|
rsp = getFailedResponse(nil, merr.OldCodeToMerr(commonpb.ErrorCode_UnexpectedError))
|
|
assert.Nil(t, rsp)
|
|
})
|
|
|
|
t.Run("test RateLimitInterceptor", func(t *testing.T) {
|
|
mockCache := NewMockCache(t)
|
|
mockCache.On("GetCollectionID",
|
|
mock.Anything, // context.Context
|
|
mock.AnythingOfType("string"),
|
|
mock.AnythingOfType("string"),
|
|
).Return(int64(0), nil)
|
|
globalMetaCache = mockCache
|
|
|
|
limiter := limiterMock{rate: 100}
|
|
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
|
return &milvuspb.MutationResult{
|
|
Status: merr.Success(),
|
|
}, nil
|
|
}
|
|
serverInfo := &grpc.UnaryServerInfo{FullMethod: "MockFullMethod"}
|
|
|
|
limiter.limit = true
|
|
interceptorFun := RateLimitInterceptor(&limiter)
|
|
rsp, err := interceptorFun(context.Background(), &milvuspb.InsertRequest{}, serverInfo, handler)
|
|
assert.Equal(t, commonpb.ErrorCode_RateLimit, rsp.(*milvuspb.MutationResult).GetStatus().GetErrorCode())
|
|
assert.NoError(t, err)
|
|
|
|
limiter.limit = false
|
|
interceptorFun = RateLimitInterceptor(&limiter)
|
|
rsp, err = interceptorFun(context.Background(), &milvuspb.InsertRequest{}, serverInfo, handler)
|
|
assert.Equal(t, commonpb.ErrorCode_Success, rsp.(*milvuspb.MutationResult).GetStatus().GetErrorCode())
|
|
assert.NoError(t, err)
|
|
|
|
// test 0 rate, force deny
|
|
limiter.rate = 0
|
|
interceptorFun = RateLimitInterceptor(&limiter)
|
|
rsp, err = interceptorFun(context.Background(), &milvuspb.InsertRequest{}, serverInfo, handler)
|
|
assert.Equal(t, commonpb.ErrorCode_ForceDeny, rsp.(*milvuspb.MutationResult).GetStatus().GetErrorCode())
|
|
assert.NoError(t, err)
|
|
})
|
|
}
|