2022-04-29 13:35:49 +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"
|
2022-10-19 23:17:44 +08:00
|
|
|
"errors"
|
2022-11-04 14:25:38 +08:00
|
|
|
"os"
|
2022-04-29 13:35:49 +08:00
|
|
|
"testing"
|
|
|
|
|
2022-10-27 13:05:31 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-10-25 11:29:30 +08:00
|
|
|
|
2022-10-16 20:49:27 +08:00
|
|
|
"github.com/milvus-io/milvus-proto/go-api/commonpb"
|
|
|
|
"github.com/milvus-io/milvus-proto/go-api/milvuspb"
|
2022-10-27 13:05:31 +08:00
|
|
|
"github.com/milvus-io/milvus-proto/go-api/schemapb"
|
2023-01-04 19:37:36 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/datapb"
|
2022-10-19 23:17:44 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/querypb"
|
2022-04-29 13:35:49 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/funcutil"
|
2022-11-04 14:25:38 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/paramtable"
|
2022-10-27 13:05:31 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/typeutil"
|
2022-04-29 13:35:49 +08:00
|
|
|
)
|
|
|
|
|
2022-11-04 14:25:38 +08:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
paramtable.Init()
|
|
|
|
code := m.Run()
|
|
|
|
os.Exit(code)
|
|
|
|
}
|
|
|
|
|
2022-04-29 13:35:49 +08:00
|
|
|
func TestGetIndexStateTask_Execute(t *testing.T) {
|
|
|
|
dbName := funcutil.GenRandomStr()
|
|
|
|
collectionName := funcutil.GenRandomStr()
|
|
|
|
collectionID := UniqueID(1)
|
|
|
|
fieldName := funcutil.GenRandomStr()
|
|
|
|
indexName := ""
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
rootCoord := newMockRootCoord()
|
2022-05-19 10:13:56 +08:00
|
|
|
queryCoord := NewQueryCoordMock()
|
2023-01-04 19:37:36 +08:00
|
|
|
datacoord := NewDataCoordMock()
|
2022-04-29 13:35:49 +08:00
|
|
|
|
|
|
|
gist := &getIndexStateTask{
|
|
|
|
GetIndexStateRequest: &milvuspb.GetIndexStateRequest{
|
|
|
|
Base: &commonpb.MsgBase{},
|
|
|
|
DbName: dbName,
|
|
|
|
CollectionName: collectionName,
|
|
|
|
FieldName: fieldName,
|
|
|
|
IndexName: indexName,
|
|
|
|
},
|
2023-01-04 19:37:36 +08:00
|
|
|
ctx: ctx,
|
|
|
|
rootCoord: rootCoord,
|
|
|
|
dataCoord: datacoord,
|
2022-04-29 13:35:49 +08:00
|
|
|
result: &milvuspb.GetIndexStateResponse{
|
|
|
|
Status: &commonpb.Status{ErrorCode: commonpb.ErrorCode_UnexpectedError, Reason: "mock"},
|
|
|
|
State: commonpb.IndexState_Unissued,
|
|
|
|
},
|
|
|
|
collectionID: collectionID,
|
|
|
|
}
|
|
|
|
|
2022-06-02 12:16:03 +08:00
|
|
|
shardMgr := newShardClientMgr()
|
2022-04-29 13:35:49 +08:00
|
|
|
// failed to get collection id.
|
2022-08-04 11:04:34 +08:00
|
|
|
_ = InitMetaCache(ctx, rootCoord, queryCoord, shardMgr)
|
2022-04-29 13:35:49 +08:00
|
|
|
assert.Error(t, gist.Execute(ctx))
|
2022-08-25 15:48:54 +08:00
|
|
|
|
|
|
|
rootCoord.DescribeCollectionFunc = func(ctx context.Context, request *milvuspb.DescribeCollectionRequest) (*milvuspb.DescribeCollectionResponse, error) {
|
|
|
|
return &milvuspb.DescribeCollectionResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
|
|
|
},
|
|
|
|
Schema: newTestSchema(),
|
|
|
|
CollectionID: collectionID,
|
|
|
|
CollectionName: request.CollectionName,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-01-04 19:37:36 +08:00
|
|
|
datacoord.GetIndexStateFunc = func(ctx context.Context, request *datapb.GetIndexStateRequest) (*datapb.GetIndexStateResponse, error) {
|
|
|
|
return &datapb.GetIndexStateResponse{
|
2022-08-25 15:48:54 +08:00
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
2022-04-29 13:35:49 +08:00
|
|
|
},
|
2022-08-25 15:48:54 +08:00
|
|
|
State: commonpb.IndexState_Finished,
|
|
|
|
FailReason: "",
|
2022-04-29 13:35:49 +08:00
|
|
|
}, nil
|
|
|
|
}
|
2022-06-16 20:12:11 +08:00
|
|
|
|
2022-04-29 13:35:49 +08:00
|
|
|
assert.NoError(t, gist.Execute(ctx))
|
|
|
|
assert.Equal(t, commonpb.IndexState_Finished, gist.result.GetState())
|
|
|
|
}
|
2022-10-19 23:17:44 +08:00
|
|
|
|
|
|
|
func TestDropIndexTask_PreExecute(t *testing.T) {
|
|
|
|
collectionName := "collection1"
|
|
|
|
collectionID := UniqueID(1)
|
|
|
|
fieldName := "field1"
|
2022-10-27 20:23:38 +08:00
|
|
|
indexName := "_default_idx_101"
|
2022-10-19 23:17:44 +08:00
|
|
|
|
|
|
|
Params.Init()
|
|
|
|
showCollectionMock := func(ctx context.Context, request *querypb.ShowCollectionsRequest) (*querypb.ShowCollectionsResponse, error) {
|
|
|
|
return &querypb.ShowCollectionsResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
|
|
|
},
|
|
|
|
CollectionIDs: nil,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
qc := NewQueryCoordMock(withValidShardLeaders(), SetQueryCoordShowCollectionsFunc(showCollectionMock))
|
2023-01-04 19:37:36 +08:00
|
|
|
dc := NewDataCoordMock()
|
2022-10-19 23:17:44 +08:00
|
|
|
ctx := context.Background()
|
|
|
|
qc.updateState(commonpb.StateCode_Healthy)
|
|
|
|
|
2022-10-25 11:29:30 +08:00
|
|
|
mockCache := newMockCache()
|
|
|
|
mockCache.setGetIDFunc(func(ctx context.Context, collectionName string) (typeutil.UniqueID, error) {
|
|
|
|
return collectionID, nil
|
|
|
|
})
|
|
|
|
globalMetaCache = mockCache
|
2022-10-19 23:17:44 +08:00
|
|
|
|
|
|
|
dit := dropIndexTask{
|
|
|
|
ctx: ctx,
|
|
|
|
DropIndexRequest: &milvuspb.DropIndexRequest{
|
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: 0,
|
|
|
|
MsgID: 0,
|
|
|
|
Timestamp: 0,
|
|
|
|
SourceID: 0,
|
|
|
|
TargetID: 0,
|
|
|
|
},
|
|
|
|
CollectionName: collectionName,
|
|
|
|
FieldName: fieldName,
|
|
|
|
IndexName: indexName,
|
|
|
|
},
|
2023-01-04 19:37:36 +08:00
|
|
|
dataCoord: dc,
|
2022-10-19 23:17:44 +08:00
|
|
|
queryCoord: qc,
|
|
|
|
result: nil,
|
|
|
|
collectionID: collectionID,
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("normal", func(t *testing.T) {
|
|
|
|
err := dit.PreExecute(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
2022-10-25 11:29:30 +08:00
|
|
|
t.Run("get collectionID error", func(t *testing.T) {
|
|
|
|
mockCache := newMockCache()
|
|
|
|
mockCache.setGetIDFunc(func(ctx context.Context, collectionName string) (typeutil.UniqueID, error) {
|
|
|
|
return 0, errors.New("error")
|
|
|
|
})
|
|
|
|
globalMetaCache = mockCache
|
|
|
|
err := dit.PreExecute(ctx)
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
mockCache.setGetIDFunc(func(ctx context.Context, collectionName string) (typeutil.UniqueID, error) {
|
|
|
|
return collectionID, nil
|
|
|
|
})
|
|
|
|
globalMetaCache = mockCache
|
|
|
|
|
2022-10-19 23:17:44 +08:00
|
|
|
t.Run("coll has been loaded", func(t *testing.T) {
|
|
|
|
showCollectionMock := func(ctx context.Context, request *querypb.ShowCollectionsRequest) (*querypb.ShowCollectionsResponse, error) {
|
|
|
|
return &querypb.ShowCollectionsResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
|
|
|
},
|
|
|
|
CollectionIDs: []int64{collectionID},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
qc := NewQueryCoordMock(withValidShardLeaders(), SetQueryCoordShowCollectionsFunc(showCollectionMock))
|
|
|
|
qc.updateState(commonpb.StateCode_Healthy)
|
|
|
|
dit.queryCoord = qc
|
|
|
|
|
|
|
|
err := dit.PreExecute(ctx)
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("show collection error", func(t *testing.T) {
|
|
|
|
showCollectionMock := func(ctx context.Context, request *querypb.ShowCollectionsRequest) (*querypb.ShowCollectionsResponse, error) {
|
|
|
|
return nil, errors.New("error")
|
|
|
|
}
|
|
|
|
qc := NewQueryCoordMock(withValidShardLeaders(), SetQueryCoordShowCollectionsFunc(showCollectionMock))
|
|
|
|
qc.updateState(commonpb.StateCode_Healthy)
|
|
|
|
dit.queryCoord = qc
|
|
|
|
|
|
|
|
err := dit.PreExecute(ctx)
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("show collection fail", func(t *testing.T) {
|
|
|
|
showCollectionMock := func(ctx context.Context, request *querypb.ShowCollectionsRequest) (*querypb.ShowCollectionsResponse, error) {
|
|
|
|
return &querypb.ShowCollectionsResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_UnexpectedError,
|
|
|
|
Reason: "fail reason",
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
qc := NewQueryCoordMock(withValidShardLeaders(), SetQueryCoordShowCollectionsFunc(showCollectionMock))
|
|
|
|
qc.updateState(commonpb.StateCode_Healthy)
|
|
|
|
dit.queryCoord = qc
|
|
|
|
|
|
|
|
err := dit.PreExecute(ctx)
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|
2022-10-27 13:05:31 +08:00
|
|
|
|
|
|
|
func TestCreateIndexTask_PreExecute(t *testing.T) {
|
|
|
|
collectionName := "collection1"
|
|
|
|
collectionID := UniqueID(1)
|
|
|
|
fieldName := newTestSchema().Fields[0].Name
|
|
|
|
|
2023-01-04 19:37:36 +08:00
|
|
|
dc := NewDataCoordMock()
|
2022-10-27 13:05:31 +08:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
mockCache := newMockCache()
|
|
|
|
mockCache.setGetIDFunc(func(ctx context.Context, collectionName string) (typeutil.UniqueID, error) {
|
|
|
|
return collectionID, nil
|
|
|
|
})
|
|
|
|
mockCache.setGetSchemaFunc(func(ctx context.Context, collectionName string) (*schemapb.CollectionSchema, error) {
|
|
|
|
return newTestSchema(), nil
|
|
|
|
})
|
|
|
|
globalMetaCache = mockCache
|
|
|
|
|
|
|
|
cit := createIndexTask{
|
|
|
|
ctx: ctx,
|
|
|
|
req: &milvuspb.CreateIndexRequest{
|
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_CreateIndex,
|
|
|
|
},
|
|
|
|
CollectionName: collectionName,
|
|
|
|
FieldName: fieldName,
|
|
|
|
},
|
2023-01-04 19:37:36 +08:00
|
|
|
datacoord: dc,
|
2022-10-27 13:05:31 +08:00
|
|
|
queryCoord: nil,
|
|
|
|
result: nil,
|
|
|
|
collectionID: collectionID,
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("normal", func(t *testing.T) {
|
|
|
|
showCollectionMock := func(ctx context.Context, request *querypb.ShowCollectionsRequest) (*querypb.ShowCollectionsResponse, error) {
|
|
|
|
return &querypb.ShowCollectionsResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
|
|
|
},
|
|
|
|
CollectionIDs: []int64{},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
qc := NewQueryCoordMock(withValidShardLeaders(), SetQueryCoordShowCollectionsFunc(showCollectionMock))
|
|
|
|
qc.updateState(commonpb.StateCode_Healthy)
|
|
|
|
cit.queryCoord = qc
|
|
|
|
|
|
|
|
err := cit.PreExecute(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
}
|