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"
2024-04-12 16:01:19 +08:00
"github.com/cockroachdb/errors"
2022-09-16 09:56:47 +08:00
"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"
2024-07-29 11:31:51 +08:00
"google.golang.org/protobuf/proto"
2022-09-16 09:56:47 +08:00
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"
2024-05-22 15:57:39 +08:00
"github.com/milvus-io/milvus/pkg/util"
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
}
2024-04-12 16:01:19 +08:00
func ( l * limiterMock ) Check ( dbID int64 , collectionIDToPartIDs map [ int64 ] [ ] 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
}
2024-05-23 20:03:40 +08:00
func ( l * limiterMock ) Alloc ( ctx context . Context , dbID int64 , collectionIDToPartIDs map [ int64 ] [ ] int64 , rt internalpb . RateType , n int ) error {
return l . Check ( dbID , collectionIDToPartIDs , rt , n )
}
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 )
2024-04-12 16:01:19 +08:00
mockCache . EXPECT ( ) . GetCollectionID ( mock . Anything , mock . Anything , mock . Anything ) . Return ( int64 ( 1 ) , nil )
mockCache . EXPECT ( ) . GetPartitionInfo ( mock . Anything , mock . Anything , mock . Anything , mock . Anything ) . Return ( & partitionInfo {
name : "p1" ,
partitionID : 10 ,
createdTimestamp : 10001 ,
createdUtcTimestamp : 10002 ,
} , nil )
mockCache . EXPECT ( ) . GetDatabaseInfo ( mock . Anything , mock . Anything ) . Return ( & databaseInfo {
dbID : 100 ,
createdTimestamp : 1 ,
} , nil )
2023-06-25 17:20:43 +08:00
globalMetaCache = mockCache
2024-08-20 16:16:55 +08:00
database , col2part , rt , size , err := GetRequestInfo ( context . Background ( ) , & milvuspb . InsertRequest {
2024-04-28 11:01:28 +08:00
CollectionName : "foo" ,
PartitionName : "p1" ,
DbName : "db1" ,
} )
2022-09-16 09:56:47 +08:00
assert . NoError ( t , err )
2024-04-28 11:01:28 +08:00
assert . Equal ( t , proto . Size ( & milvuspb . InsertRequest {
CollectionName : "foo" ,
PartitionName : "p1" ,
DbName : "db1" ,
} ) , size )
2022-09-16 09:56:47 +08:00
assert . Equal ( t , internalpb . RateType_DMLInsert , rt )
2024-04-12 16:01:19 +08:00
assert . Equal ( t , database , int64 ( 100 ) )
assert . True ( t , len ( col2part ) == 1 )
assert . Equal ( t , int64 ( 10 ) , col2part [ 1 ] [ 0 ] )
2022-09-16 09:56:47 +08:00
2024-08-20 16:16:55 +08:00
database , col2part , rt , size , err = GetRequestInfo ( context . Background ( ) , & milvuspb . UpsertRequest {
2024-04-28 11:01:28 +08:00
CollectionName : "foo" ,
PartitionName : "p1" ,
DbName : "db1" ,
} )
2023-07-11 11:20:34 +08:00
assert . NoError ( t , err )
2024-04-28 11:01:28 +08:00
assert . Equal ( t , proto . Size ( & milvuspb . InsertRequest {
CollectionName : "foo" ,
PartitionName : "p1" ,
DbName : "db1" ,
} ) , size )
2024-07-12 16:17:35 +08:00
assert . Equal ( t , internalpb . RateType_DMLInsert , rt )
2024-04-12 16:01:19 +08:00
assert . Equal ( t , database , int64 ( 100 ) )
assert . True ( t , len ( col2part ) == 1 )
assert . Equal ( t , int64 ( 10 ) , col2part [ 1 ] [ 0 ] )
2023-07-11 11:20:34 +08:00
2024-08-20 16:16:55 +08:00
database , col2part , rt , size , err = GetRequestInfo ( context . Background ( ) , & milvuspb . DeleteRequest {
2024-04-28 11:01:28 +08:00
CollectionName : "foo" ,
PartitionName : "p1" ,
DbName : "db1" ,
} )
2022-09-16 09:56:47 +08:00
assert . NoError ( t , err )
2024-04-28 11:01:28 +08:00
assert . Equal ( t , proto . Size ( & milvuspb . DeleteRequest {
CollectionName : "foo" ,
PartitionName : "p1" ,
DbName : "db1" ,
} ) , size )
2022-09-16 09:56:47 +08:00
assert . Equal ( t , internalpb . RateType_DMLDelete , rt )
2024-04-12 16:01:19 +08:00
assert . Equal ( t , database , int64 ( 100 ) )
assert . True ( t , len ( col2part ) == 1 )
assert . Equal ( t , int64 ( 10 ) , col2part [ 1 ] [ 0 ] )
2022-09-16 09:56:47 +08:00
2024-08-20 16:16:55 +08:00
database , col2part , rt , size , err = GetRequestInfo ( context . Background ( ) , & milvuspb . ImportRequest {
2024-04-28 11:01:28 +08:00
CollectionName : "foo" ,
PartitionName : "p1" ,
DbName : "db1" ,
} )
2022-10-18 16:53:26 +08:00
assert . NoError ( t , err )
2024-04-28 11:01:28 +08:00
assert . Equal ( t , proto . Size ( & milvuspb . ImportRequest {
CollectionName : "foo" ,
PartitionName : "p1" ,
DbName : "db1" ,
} ) , size )
2022-10-18 16:53:26 +08:00
assert . Equal ( t , internalpb . RateType_DMLBulkLoad , rt )
2024-04-12 16:01:19 +08:00
assert . Equal ( t , database , int64 ( 100 ) )
assert . True ( t , len ( col2part ) == 1 )
assert . Equal ( t , int64 ( 10 ) , col2part [ 1 ] [ 0 ] )
2024-08-20 16:16:55 +08:00
database , col2part , rt , size , err = GetRequestInfo ( context . Background ( ) , & milvuspb . SearchRequest {
2024-04-12 16:01:19 +08:00
Nq : 5 ,
PartitionNames : [ ] string {
"p1" ,
} ,
} )
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 )
2024-04-12 16:01:19 +08:00
assert . Equal ( t , database , int64 ( 100 ) )
assert . Equal ( t , 1 , len ( col2part ) )
assert . Equal ( t , 1 , len ( col2part [ 1 ] ) )
2022-09-16 09:56:47 +08:00
2024-08-20 16:16:55 +08:00
database , col2part , rt , size , err = GetRequestInfo ( context . Background ( ) , & milvuspb . QueryRequest {
2024-04-28 11:01:28 +08:00
CollectionName : "foo" ,
PartitionNames : [ ] string {
"p1" ,
} ,
DbName : "db1" ,
} )
2022-09-16 09:56:47 +08:00
assert . NoError ( t , err )
assert . Equal ( t , 1 , size )
assert . Equal ( t , internalpb . RateType_DQLQuery , rt )
2024-04-12 16:01:19 +08:00
assert . Equal ( t , database , int64 ( 100 ) )
assert . Equal ( t , 1 , len ( col2part ) )
2024-04-28 11:01:28 +08:00
assert . Equal ( t , 1 , len ( col2part [ 1 ] ) )
2022-09-16 09:56:47 +08:00
2024-08-20 16:16:55 +08:00
database , col2part , rt , size , err = GetRequestInfo ( context . Background ( ) , & 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 )
2024-04-12 16:01:19 +08:00
assert . Equal ( t , database , int64 ( 100 ) )
assert . Equal ( t , 1 , len ( col2part ) )
assert . Equal ( t , 0 , len ( col2part [ 1 ] ) )
2022-09-16 09:56:47 +08:00
2024-08-20 16:16:55 +08:00
database , col2part , rt , size , err = GetRequestInfo ( context . Background ( ) , & milvuspb . LoadCollectionRequest { } )
2023-04-25 15:54:35 +08:00
assert . NoError ( t , err )
assert . Equal ( t , 1 , size )
assert . Equal ( t , internalpb . RateType_DDLCollection , rt )
2024-04-12 16:01:19 +08:00
assert . Equal ( t , database , int64 ( 100 ) )
assert . Equal ( t , 1 , len ( col2part ) )
assert . Equal ( t , 0 , len ( col2part [ 1 ] ) )
2023-04-25 15:54:35 +08:00
2024-08-20 16:16:55 +08:00
database , col2part , rt , size , err = GetRequestInfo ( context . Background ( ) , & milvuspb . ReleaseCollectionRequest { } )
2023-04-25 15:54:35 +08:00
assert . NoError ( t , err )
assert . Equal ( t , 1 , size )
assert . Equal ( t , internalpb . RateType_DDLCollection , rt )
2024-04-12 16:01:19 +08:00
assert . Equal ( t , database , int64 ( 100 ) )
assert . Equal ( t , 1 , len ( col2part ) )
assert . Equal ( t , 0 , len ( col2part [ 1 ] ) )
2023-04-25 15:54:35 +08:00
2024-08-20 16:16:55 +08:00
database , col2part , rt , size , err = GetRequestInfo ( context . Background ( ) , & milvuspb . DropCollectionRequest { } )
2023-04-25 15:54:35 +08:00
assert . NoError ( t , err )
assert . Equal ( t , 1 , size )
assert . Equal ( t , internalpb . RateType_DDLCollection , rt )
2024-04-12 16:01:19 +08:00
assert . Equal ( t , database , int64 ( 100 ) )
assert . Equal ( t , 1 , len ( col2part ) )
assert . Equal ( t , 0 , len ( col2part [ 1 ] ) )
2023-04-25 15:54:35 +08:00
2024-08-20 16:16:55 +08:00
database , col2part , rt , size , err = GetRequestInfo ( context . Background ( ) , & milvuspb . CreatePartitionRequest { } )
2023-04-25 15:54:35 +08:00
assert . NoError ( t , err )
assert . Equal ( t , 1 , size )
assert . Equal ( t , internalpb . RateType_DDLPartition , rt )
2024-04-12 16:01:19 +08:00
assert . Equal ( t , database , int64 ( 100 ) )
assert . Equal ( t , 1 , len ( col2part ) )
assert . Equal ( t , 0 , len ( col2part [ 1 ] ) )
2023-04-25 15:54:35 +08:00
2024-08-20 16:16:55 +08:00
database , col2part , rt , size , err = GetRequestInfo ( context . Background ( ) , & milvuspb . LoadPartitionsRequest { } )
2023-04-25 15:54:35 +08:00
assert . NoError ( t , err )
assert . Equal ( t , 1 , size )
assert . Equal ( t , internalpb . RateType_DDLPartition , rt )
2024-04-12 16:01:19 +08:00
assert . Equal ( t , database , int64 ( 100 ) )
assert . Equal ( t , 1 , len ( col2part ) )
assert . Equal ( t , 0 , len ( col2part [ 1 ] ) )
2023-04-25 15:54:35 +08:00
2024-08-20 16:16:55 +08:00
database , col2part , rt , size , err = GetRequestInfo ( context . Background ( ) , & milvuspb . ReleasePartitionsRequest { } )
2023-04-25 15:54:35 +08:00
assert . NoError ( t , err )
assert . Equal ( t , 1 , size )
assert . Equal ( t , internalpb . RateType_DDLPartition , rt )
2024-04-12 16:01:19 +08:00
assert . Equal ( t , database , int64 ( 100 ) )
assert . Equal ( t , 1 , len ( col2part ) )
assert . Equal ( t , 0 , len ( col2part [ 1 ] ) )
2023-04-25 15:54:35 +08:00
2024-08-20 16:16:55 +08:00
database , col2part , rt , size , err = GetRequestInfo ( context . Background ( ) , & 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 )
2024-04-12 16:01:19 +08:00
assert . Equal ( t , database , int64 ( 100 ) )
assert . Equal ( t , 1 , len ( col2part ) )
assert . Equal ( t , 0 , len ( col2part [ 1 ] ) )
2023-04-25 15:54:35 +08:00
2024-08-20 16:16:55 +08:00
database , col2part , rt , size , err = GetRequestInfo ( context . Background ( ) , & milvuspb . CreateIndexRequest { } )
2023-04-25 15:54:35 +08:00
assert . NoError ( t , err )
assert . Equal ( t , 1 , size )
assert . Equal ( t , internalpb . RateType_DDLIndex , rt )
2024-04-12 16:01:19 +08:00
assert . Equal ( t , database , int64 ( 100 ) )
assert . Equal ( t , 1 , len ( col2part ) )
assert . Equal ( t , 0 , len ( col2part [ 1 ] ) )
2022-09-16 09:56:47 +08:00
2024-08-20 16:16:55 +08:00
database , col2part , rt , size , err = GetRequestInfo ( context . Background ( ) , & 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 )
2024-04-12 16:01:19 +08:00
assert . Equal ( t , database , int64 ( 100 ) )
assert . Equal ( t , 1 , len ( col2part ) )
assert . Equal ( t , 0 , len ( col2part [ 1 ] ) )
2024-08-20 16:16:55 +08:00
database , col2part , rt , size , err = GetRequestInfo ( context . Background ( ) , & milvuspb . FlushRequest {
2024-04-12 16:01:19 +08:00
CollectionNames : [ ] string {
"col1" ,
} ,
} )
2022-09-16 09:56:47 +08:00
assert . NoError ( t , err )
assert . Equal ( t , 1 , size )
assert . Equal ( t , internalpb . RateType_DDLFlush , rt )
2024-04-12 16:01:19 +08:00
assert . Equal ( t , database , int64 ( 100 ) )
assert . Equal ( t , 1 , len ( col2part ) )
2022-09-16 09:56:47 +08:00
2024-08-20 16:16:55 +08:00
database , _ , rt , size , err = GetRequestInfo ( context . Background ( ) , & 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 )
2024-04-12 16:01:19 +08:00
assert . Equal ( t , database , int64 ( 100 ) )
2024-08-20 16:16:55 +08:00
_ , _ , _ , _ , err = GetRequestInfo ( context . Background ( ) , nil )
2024-04-12 16:01:19 +08:00
assert . Error ( t , err )
2024-08-20 16:16:55 +08:00
_ , _ , _ , _ , err = GetRequestInfo ( context . Background ( ) , & milvuspb . CalcDistanceRequest { } )
2024-05-27 14:27:41 +08:00
assert . NoError ( t , err )
2022-09-16 09:56:47 +08:00
} )
2024-08-20 16:16:55 +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 ) {
2024-08-20 16:16:55 +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
2024-08-20 16:16:55 +08:00
rsp := GetFailedResponse ( & milvuspb . SearchResults { } , merr . OldCodeToMerr ( commonpb . ErrorCode_UnexpectedError ) )
2022-12-30 18:35:32 +08:00
assert . Nil ( t , rsp )
2024-08-20 16:16:55 +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 )
2024-04-12 16:01:19 +08:00
mockCache . EXPECT ( ) . GetCollectionID ( mock . Anything , mock . Anything , mock . Anything ) . Return ( int64 ( 1 ) , nil )
mockCache . EXPECT ( ) . GetPartitionInfo ( mock . Anything , mock . Anything , mock . Anything , mock . Anything ) . Return ( & partitionInfo {
name : "p1" ,
partitionID : 10 ,
createdTimestamp : 10001 ,
createdUtcTimestamp : 10002 ,
} , nil )
mockCache . EXPECT ( ) . GetDatabaseInfo ( mock . Anything , mock . Anything ) . Return ( & databaseInfo {
dbID : 100 ,
createdTimestamp : 1 ,
} , nil )
2023-06-25 17:20:43 +08:00
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 )
2024-04-28 11:01:28 +08:00
rsp , err := interceptorFun ( context . Background ( ) , & milvuspb . InsertRequest {
CollectionName : "foo" ,
PartitionName : "p1" ,
DbName : "db1" ,
} , serverInfo , handler )
2022-09-16 09:56:47 +08:00
assert . Equal ( t , commonpb . ErrorCode_RateLimit , rsp . ( * milvuspb . MutationResult ) . GetStatus ( ) . GetErrorCode ( ) )
assert . NoError ( t , err )
limiter . limit = false
interceptorFun = RateLimitInterceptor ( & limiter )
2024-04-28 11:01:28 +08:00
rsp , err = interceptorFun ( context . Background ( ) , & milvuspb . InsertRequest {
CollectionName : "foo" ,
PartitionName : "p1" ,
DbName : "db1" ,
} , serverInfo , handler )
2022-09-16 09:56:47 +08:00
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 )
} )
2024-04-12 16:01:19 +08:00
t . Run ( "request info fail" , func ( t * testing . T ) {
mockCache := NewMockCache ( t )
mockCache . EXPECT ( ) . GetDatabaseInfo ( mock . Anything , mock . Anything ) . Return ( nil , errors . New ( "mock error: get database info" ) )
originCache := globalMetaCache
globalMetaCache = mockCache
defer func ( ) {
globalMetaCache = originCache
} ( )
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_Success , rsp . ( * milvuspb . MutationResult ) . GetStatus ( ) . GetErrorCode ( ) )
assert . NoError ( t , err )
} )
}
func TestGetInfo ( t * testing . T ) {
mockCache := NewMockCache ( t )
ctx := context . Background ( )
originCache := globalMetaCache
globalMetaCache = mockCache
defer func ( ) {
globalMetaCache = originCache
} ( )
t . Run ( "fail to get database" , func ( t * testing . T ) {
2024-05-22 15:57:39 +08:00
mockCache . EXPECT ( ) . GetDatabaseInfo ( mock . Anything , mock . Anything ) . Return ( nil , errors . New ( "mock error: get database info" ) ) . Times ( 5 )
2024-04-12 16:01:19 +08:00
{
_ , _ , err := getCollectionAndPartitionID ( ctx , & milvuspb . InsertRequest {
DbName : "foo" ,
CollectionName : "coo" ,
PartitionName : "p1" ,
} )
assert . Error ( t , err )
}
{
_ , _ , err := getCollectionAndPartitionIDs ( ctx , & milvuspb . SearchRequest {
DbName : "foo" ,
CollectionName : "coo" ,
PartitionNames : [ ] string { "p1" } ,
} )
assert . Error ( t , err )
}
{
2024-08-20 16:16:55 +08:00
_ , _ , _ , _ , err := GetRequestInfo ( ctx , & milvuspb . FlushRequest {
2024-04-12 16:01:19 +08:00
DbName : "foo" ,
} )
assert . Error ( t , err )
}
{
2024-08-20 16:16:55 +08:00
_ , _ , _ , _ , err := GetRequestInfo ( ctx , & milvuspb . ManualCompactionRequest { } )
2024-04-12 16:01:19 +08:00
assert . Error ( t , err )
}
2024-05-22 15:57:39 +08:00
{
dbID , collectionIDInfos := getCollectionID ( & milvuspb . CreateCollectionRequest { } )
assert . Equal ( t , util . InvalidDBID , dbID )
assert . Equal ( t , 0 , len ( collectionIDInfos ) )
}
2024-04-12 16:01:19 +08:00
} )
t . Run ( "fail to get collection" , func ( t * testing . T ) {
mockCache . EXPECT ( ) . GetDatabaseInfo ( mock . Anything , mock . Anything ) . Return ( & databaseInfo {
dbID : 100 ,
createdTimestamp : 1 ,
} , nil ) . Times ( 3 )
mockCache . EXPECT ( ) . GetCollectionID ( mock . Anything , mock . Anything , mock . Anything ) . Return ( int64 ( 0 ) , errors . New ( "mock error: get collection id" ) ) . Times ( 3 )
{
_ , _ , err := getCollectionAndPartitionID ( ctx , & milvuspb . InsertRequest {
DbName : "foo" ,
CollectionName : "coo" ,
PartitionName : "p1" ,
} )
assert . Error ( t , err )
}
{
_ , _ , err := getCollectionAndPartitionIDs ( ctx , & milvuspb . SearchRequest {
DbName : "foo" ,
CollectionName : "coo" ,
PartitionNames : [ ] string { "p1" } ,
} )
assert . Error ( t , err )
}
{
2024-08-20 16:16:55 +08:00
_ , _ , _ , _ , err := GetRequestInfo ( ctx , & milvuspb . FlushRequest {
2024-04-12 16:01:19 +08:00
DbName : "foo" ,
CollectionNames : [ ] string { "coo" } ,
} )
assert . Error ( t , err )
}
} )
t . Run ( "fail to get partition" , func ( t * testing . T ) {
mockCache . EXPECT ( ) . GetDatabaseInfo ( mock . Anything , mock . Anything ) . Return ( & databaseInfo {
dbID : 100 ,
createdTimestamp : 1 ,
} , nil ) . Twice ( )
mockCache . EXPECT ( ) . GetCollectionID ( mock . Anything , mock . Anything , mock . Anything ) . Return ( int64 ( 1 ) , nil ) . Twice ( )
mockCache . EXPECT ( ) . GetPartitionInfo ( mock . Anything , mock . Anything , mock . Anything , mock . Anything ) . Return ( nil , errors . New ( "mock error: get partition info" ) ) . Twice ( )
{
_ , _ , err := getCollectionAndPartitionID ( ctx , & milvuspb . InsertRequest {
DbName : "foo" ,
CollectionName : "coo" ,
PartitionName : "p1" ,
} )
assert . Error ( t , err )
}
{
_ , _ , err := getCollectionAndPartitionIDs ( ctx , & milvuspb . SearchRequest {
DbName : "foo" ,
CollectionName : "coo" ,
PartitionNames : [ ] string { "p1" } ,
} )
assert . Error ( t , err )
}
} )
t . Run ( "success" , func ( t * testing . T ) {
mockCache . EXPECT ( ) . GetDatabaseInfo ( mock . Anything , mock . Anything ) . Return ( & databaseInfo {
dbID : 100 ,
createdTimestamp : 1 ,
2024-04-28 11:01:28 +08:00
} , nil ) . Times ( 3 )
mockCache . EXPECT ( ) . GetCollectionID ( mock . Anything , mock . Anything , mock . Anything ) . Return ( int64 ( 10 ) , nil ) . Times ( 3 )
2024-04-12 16:01:19 +08:00
mockCache . EXPECT ( ) . GetPartitionInfo ( mock . Anything , mock . Anything , mock . Anything , mock . Anything ) . Return ( & partitionInfo {
name : "p1" ,
partitionID : 100 ,
2024-04-28 11:01:28 +08:00
} , nil ) . Twice ( )
2024-04-12 16:01:19 +08:00
{
db , col2par , err := getCollectionAndPartitionID ( ctx , & milvuspb . InsertRequest {
DbName : "foo" ,
CollectionName : "coo" ,
PartitionName : "p1" ,
} )
assert . NoError ( t , err )
assert . Equal ( t , int64 ( 100 ) , db )
assert . NotNil ( t , col2par [ 10 ] )
assert . Equal ( t , int64 ( 100 ) , col2par [ 10 ] [ 0 ] )
}
2024-04-28 11:01:28 +08:00
{
db , col2par , err := getCollectionAndPartitionID ( ctx , & milvuspb . InsertRequest {
DbName : "foo" ,
CollectionName : "coo" ,
} )
assert . NoError ( t , err )
assert . Equal ( t , int64 ( 100 ) , db )
assert . NotNil ( t , col2par [ 10 ] )
assert . Equal ( t , 0 , len ( col2par [ 10 ] ) )
}
2024-04-12 16:01:19 +08:00
{
db , col2par , err := getCollectionAndPartitionIDs ( ctx , & milvuspb . SearchRequest {
DbName : "foo" ,
CollectionName : "coo" ,
PartitionNames : [ ] string { "p1" } ,
} )
assert . NoError ( t , err )
assert . Equal ( t , int64 ( 100 ) , db )
assert . NotNil ( t , col2par [ 10 ] )
assert . Equal ( t , int64 ( 100 ) , col2par [ 10 ] [ 0 ] )
}
} )
2022-09-16 09:56:47 +08:00
}