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 (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-06-25 17:20:43 +08:00
|
|
|
"github.com/stretchr/testify/mock"
|
2022-09-16 09:56:47 +08:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2023-06-09 01:28:37 +08:00
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
|
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/milvuspb"
|
2022-09-16 09:56:47 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/internalpb"
|
2023-09-19 10:37:23 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/merr"
|
2022-09-16 09:56:47 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type limiterMock struct {
|
2022-12-30 18:35:32 +08:00
|
|
|
limit bool
|
|
|
|
rate float64
|
|
|
|
quotaStates []milvuspb.QuotaState
|
2023-01-06 14:31:37 +08:00
|
|
|
quotaStateReasons []commonpb.ErrorCode
|
2022-09-16 09:56:47 +08:00
|
|
|
}
|
|
|
|
|
2023-09-19 10:37:23 +08:00
|
|
|
func (l *limiterMock) Check(collection int64, rt internalpb.RateType, n int) error {
|
2022-12-30 18:35:32 +08:00
|
|
|
if l.rate == 0 {
|
2023-12-20 22:52:44 +08:00
|
|
|
return merr.ErrServiceQuotaExceeded
|
2022-12-30 18:35:32 +08:00
|
|
|
}
|
|
|
|
if l.limit {
|
2023-09-19 10:37:23 +08:00
|
|
|
return merr.ErrServiceRateLimit
|
2022-12-30 18:35:32 +08:00
|
|
|
}
|
2023-09-19 10:37:23 +08:00
|
|
|
return nil
|
2022-09-16 09:56:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRateLimitInterceptor(t *testing.T) {
|
|
|
|
t.Run("test getRequestInfo", func(t *testing.T) {
|
2023-06-25 17:20:43 +08:00
|
|
|
mockCache := NewMockCache(t)
|
|
|
|
mockCache.On("GetCollectionID",
|
|
|
|
mock.Anything, // context.Context
|
|
|
|
mock.AnythingOfType("string"),
|
|
|
|
mock.AnythingOfType("string"),
|
|
|
|
).Return(int64(0), nil)
|
|
|
|
globalMetaCache = mockCache
|
2023-04-25 15:54:35 +08:00
|
|
|
collection, rt, size, err := getRequestInfo(&milvuspb.InsertRequest{})
|
2022-09-16 09:56:47 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, proto.Size(&milvuspb.InsertRequest{}), size)
|
|
|
|
assert.Equal(t, internalpb.RateType_DMLInsert, rt)
|
2023-04-25 15:54:35 +08:00
|
|
|
assert.Equal(t, collection, int64(0))
|
2022-09-16 09:56:47 +08:00
|
|
|
|
2023-07-11 11:20:34 +08:00
|
|
|
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.Equal(t, collection, int64(0))
|
|
|
|
|
2023-04-25 15:54:35 +08:00
|
|
|
collection, rt, size, err = getRequestInfo(&milvuspb.DeleteRequest{})
|
2022-09-16 09:56:47 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, proto.Size(&milvuspb.DeleteRequest{}), size)
|
|
|
|
assert.Equal(t, internalpb.RateType_DMLDelete, rt)
|
2023-04-25 15:54:35 +08:00
|
|
|
assert.Equal(t, collection, int64(0))
|
2022-09-16 09:56:47 +08:00
|
|
|
|
2023-04-25 15:54:35 +08:00
|
|
|
collection, rt, size, err = getRequestInfo(&milvuspb.ImportRequest{})
|
2022-10-18 16:53:26 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, proto.Size(&milvuspb.ImportRequest{}), size)
|
|
|
|
assert.Equal(t, internalpb.RateType_DMLBulkLoad, rt)
|
2023-04-25 15:54:35 +08:00
|
|
|
assert.Equal(t, collection, int64(0))
|
2022-10-18 16:53:26 +08:00
|
|
|
|
2023-04-25 15:54:35 +08:00
|
|
|
collection, rt, size, err = getRequestInfo(&milvuspb.SearchRequest{Nq: 5})
|
2022-09-16 09:56:47 +08:00
|
|
|
assert.NoError(t, err)
|
2023-04-25 15:54:35 +08:00
|
|
|
assert.Equal(t, 5, size)
|
2022-09-16 09:56:47 +08:00
|
|
|
assert.Equal(t, internalpb.RateType_DQLSearch, rt)
|
2023-04-25 15:54:35 +08:00
|
|
|
assert.Equal(t, collection, int64(0))
|
2022-09-16 09:56:47 +08:00
|
|
|
|
2023-04-25 15:54:35 +08:00
|
|
|
collection, rt, size, err = getRequestInfo(&milvuspb.QueryRequest{})
|
2022-09-16 09:56:47 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, size)
|
|
|
|
assert.Equal(t, internalpb.RateType_DQLQuery, rt)
|
2023-04-25 15:54:35 +08:00
|
|
|
assert.Equal(t, collection, int64(0))
|
2022-09-16 09:56:47 +08:00
|
|
|
|
2023-04-25 15:54:35 +08:00
|
|
|
collection, rt, size, err = getRequestInfo(&milvuspb.CreateCollectionRequest{})
|
2022-09-16 09:56:47 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, size)
|
|
|
|
assert.Equal(t, internalpb.RateType_DDLCollection, rt)
|
2023-04-25 15:54:35 +08:00
|
|
|
assert.Equal(t, collection, int64(0))
|
2022-09-16 09:56:47 +08:00
|
|
|
|
2023-04-25 15:54:35 +08:00
|
|
|
collection, rt, size, err = getRequestInfo(&milvuspb.LoadCollectionRequest{})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, size)
|
|
|
|
assert.Equal(t, internalpb.RateType_DDLCollection, rt)
|
|
|
|
assert.Equal(t, collection, 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.Equal(t, collection, 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.Equal(t, collection, 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.Equal(t, collection, 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.Equal(t, collection, 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.Equal(t, collection, int64(0))
|
|
|
|
|
|
|
|
collection, rt, size, err = getRequestInfo(&milvuspb.DropPartitionRequest{})
|
2022-09-16 09:56:47 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, size)
|
|
|
|
assert.Equal(t, internalpb.RateType_DDLPartition, rt)
|
2023-04-25 15:54:35 +08:00
|
|
|
assert.Equal(t, collection, 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.Equal(t, collection, int64(0))
|
2022-09-16 09:56:47 +08:00
|
|
|
|
2023-04-25 15:54:35 +08:00
|
|
|
collection, rt, size, err = getRequestInfo(&milvuspb.DropIndexRequest{})
|
2022-09-16 09:56:47 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, size)
|
|
|
|
assert.Equal(t, internalpb.RateType_DDLIndex, rt)
|
2023-04-25 15:54:35 +08:00
|
|
|
assert.Equal(t, collection, int64(0))
|
2022-09-16 09:56:47 +08:00
|
|
|
|
2023-04-25 15:54:35 +08:00
|
|
|
_, rt, size, err = getRequestInfo(&milvuspb.FlushRequest{})
|
2022-09-16 09:56:47 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, size)
|
|
|
|
assert.Equal(t, internalpb.RateType_DDLFlush, rt)
|
|
|
|
|
2023-04-25 15:54:35 +08:00
|
|
|
collection, rt, size, err = getRequestInfo(&milvuspb.ManualCompactionRequest{})
|
2022-09-16 09:56:47 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, size)
|
|
|
|
assert.Equal(t, internalpb.RateType_DDLCompaction, rt)
|
2023-04-25 15:54:35 +08:00
|
|
|
assert.Equal(t, collection, int64(0))
|
2022-09-16 09:56:47 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("test getFailedResponse", func(t *testing.T) {
|
2023-09-19 10:37:23 +08:00
|
|
|
testGetFailedResponse := func(req interface{}, rt internalpb.RateType, err error, fullMethod string) {
|
2023-12-20 22:52:44 +08:00
|
|
|
rsp := getFailedResponse(req, err)
|
2022-12-30 18:35:32 +08:00
|
|
|
assert.NotNil(t, rsp)
|
2022-09-16 09:56:47 +08:00
|
|
|
}
|
|
|
|
|
2023-12-20 22:52:44 +08:00
|
|
|
testGetFailedResponse(&milvuspb.DeleteRequest{}, internalpb.RateType_DMLDelete, merr.ErrServiceQuotaExceeded, "delete")
|
|
|
|
testGetFailedResponse(&milvuspb.UpsertRequest{}, internalpb.RateType_DMLUpsert, merr.ErrServiceQuotaExceeded, "upsert")
|
2023-09-19 10:37:23 +08:00
|
|
|
testGetFailedResponse(&milvuspb.ImportRequest{}, internalpb.RateType_DMLBulkLoad, merr.ErrServiceMemoryLimitExceeded, "import")
|
|
|
|
testGetFailedResponse(&milvuspb.SearchRequest{}, internalpb.RateType_DQLSearch, merr.ErrServiceDiskLimitExceeded, "search")
|
2023-12-20 22:52:44 +08:00
|
|
|
testGetFailedResponse(&milvuspb.QueryRequest{}, internalpb.RateType_DQLQuery, merr.ErrServiceQuotaExceeded, "query")
|
2023-09-19 10:37:23 +08:00
|
|
|
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")
|
2022-09-16 09:56:47 +08:00
|
|
|
|
|
|
|
// test illegal
|
2023-12-20 22:52:44 +08:00
|
|
|
rsp := getFailedResponse(&milvuspb.SearchResults{}, merr.OldCodeToMerr(commonpb.ErrorCode_UnexpectedError))
|
2022-12-30 18:35:32 +08:00
|
|
|
assert.Nil(t, rsp)
|
2023-12-20 22:52:44 +08:00
|
|
|
rsp = getFailedResponse(nil, merr.OldCodeToMerr(commonpb.ErrorCode_UnexpectedError))
|
2022-12-30 18:35:32 +08:00
|
|
|
assert.Nil(t, rsp)
|
2022-09-16 09:56:47 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("test RateLimitInterceptor", func(t *testing.T) {
|
2023-06-25 17:20:43 +08:00
|
|
|
mockCache := NewMockCache(t)
|
|
|
|
mockCache.On("GetCollectionID",
|
|
|
|
mock.Anything, // context.Context
|
|
|
|
mock.AnythingOfType("string"),
|
|
|
|
mock.AnythingOfType("string"),
|
|
|
|
).Return(int64(0), nil)
|
|
|
|
globalMetaCache = mockCache
|
|
|
|
|
2022-09-16 09:56:47 +08:00
|
|
|
limiter := limiterMock{rate: 100}
|
|
|
|
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
|
|
|
return &milvuspb.MutationResult{
|
2023-10-11 21:01:35 +08:00
|
|
|
Status: merr.Success(),
|
2022-09-16 09:56:47 +08:00
|
|
|
}, 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)
|
|
|
|
})
|
|
|
|
}
|