2023-06-13 10:20:37 +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"
|
2023-06-25 19:46:44 +08:00
|
|
|
"reflect"
|
2023-06-13 10:20:37 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/cockroachdb/errors"
|
|
|
|
"github.com/golang/protobuf/proto"
|
2023-06-25 17:20:43 +08:00
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
"go.uber.org/atomic"
|
|
|
|
|
2023-06-13 10:20:37 +08:00
|
|
|
"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-proto/go-api/v2/schemapb"
|
2023-07-14 10:12:31 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/mocks"
|
2023-06-16 18:38:39 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/internalpb"
|
2023-06-13 10:20:37 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/querypb"
|
|
|
|
"github.com/milvus-io/milvus/internal/types"
|
|
|
|
"github.com/milvus-io/milvus/pkg/common"
|
|
|
|
"github.com/milvus-io/milvus/pkg/util/merr"
|
|
|
|
"github.com/milvus-io/milvus/pkg/util/paramtable"
|
|
|
|
"github.com/milvus-io/milvus/pkg/util/typeutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LBPolicySuite struct {
|
|
|
|
suite.Suite
|
2023-09-26 09:57:25 +08:00
|
|
|
rc types.RootCoordClient
|
|
|
|
qc *mocks.MockQueryCoordClient
|
|
|
|
qn *mocks.MockQueryNodeClient
|
2023-06-13 10:20:37 +08:00
|
|
|
|
|
|
|
mgr *MockShardClientManager
|
|
|
|
lbBalancer *MockLBBalancer
|
|
|
|
lbPolicy *LBPolicyImpl
|
|
|
|
|
|
|
|
nodes []int64
|
|
|
|
channels []string
|
2023-07-14 10:12:31 +08:00
|
|
|
qnList []*mocks.MockQueryNode
|
2023-06-13 10:20:37 +08:00
|
|
|
|
2023-08-01 17:33:06 +08:00
|
|
|
collectionName string
|
|
|
|
collectionID int64
|
2023-06-13 10:20:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LBPolicySuite) SetupSuite() {
|
2023-09-05 10:31:48 +08:00
|
|
|
paramtable.Init()
|
2023-06-13 10:20:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LBPolicySuite) SetupTest() {
|
|
|
|
s.nodes = []int64{1, 2, 3, 4, 5}
|
|
|
|
s.channels = []string{"channel1", "channel2"}
|
|
|
|
successStatus := commonpb.Status{ErrorCode: commonpb.ErrorCode_Success}
|
2023-09-26 09:57:25 +08:00
|
|
|
qc := mocks.NewMockQueryCoordClient(s.T())
|
2023-06-13 10:20:37 +08:00
|
|
|
qc.EXPECT().LoadCollection(mock.Anything, mock.Anything).Return(&successStatus, nil)
|
|
|
|
|
|
|
|
qc.EXPECT().GetShardLeaders(mock.Anything, mock.Anything).Return(&querypb.GetShardLeadersResponse{
|
|
|
|
Status: &successStatus,
|
|
|
|
Shards: []*querypb.ShardLeadersList{
|
|
|
|
{
|
|
|
|
ChannelName: s.channels[0],
|
|
|
|
NodeIds: s.nodes,
|
|
|
|
NodeAddrs: []string{"localhost:9000", "localhost:9001", "localhost:9002", "localhost:9003", "localhost:9004"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ChannelName: s.channels[1],
|
|
|
|
NodeIds: s.nodes,
|
|
|
|
NodeAddrs: []string{"localhost:9000", "localhost:9001", "localhost:9002", "localhost:9003", "localhost:9004"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil).Maybe()
|
|
|
|
qc.EXPECT().ShowPartitions(mock.Anything, mock.Anything).Return(&querypb.ShowPartitionsResponse{
|
2023-10-11 21:01:35 +08:00
|
|
|
Status: merr.Success(),
|
2023-06-13 10:20:37 +08:00
|
|
|
PartitionIDs: []int64{1, 2, 3},
|
|
|
|
}, nil).Maybe()
|
|
|
|
|
|
|
|
s.qc = qc
|
|
|
|
s.rc = NewRootCoordMock()
|
|
|
|
|
2023-09-26 09:57:25 +08:00
|
|
|
s.qn = mocks.NewMockQueryNodeClient(s.T())
|
|
|
|
s.qn.EXPECT().GetComponentStates(mock.Anything, mock.Anything).Return(nil, nil).Maybe()
|
2023-06-13 10:20:37 +08:00
|
|
|
|
|
|
|
s.mgr = NewMockShardClientManager(s.T())
|
|
|
|
s.mgr.EXPECT().UpdateShardLeaders(mock.Anything, mock.Anything).Return(nil).Maybe()
|
|
|
|
s.lbBalancer = NewMockLBBalancer(s.T())
|
2023-06-27 09:52:44 +08:00
|
|
|
s.lbBalancer.EXPECT().Start(context.Background()).Maybe()
|
2023-06-16 18:38:39 +08:00
|
|
|
s.lbPolicy = NewLBPolicyImpl(s.mgr)
|
2023-06-27 09:52:44 +08:00
|
|
|
s.lbPolicy.Start(context.Background())
|
2023-06-16 18:38:39 +08:00
|
|
|
s.lbPolicy.balancer = s.lbBalancer
|
2023-06-13 10:20:37 +08:00
|
|
|
|
|
|
|
err := InitMetaCache(context.Background(), s.rc, s.qc, s.mgr)
|
|
|
|
s.NoError(err)
|
|
|
|
|
2023-08-01 17:33:06 +08:00
|
|
|
s.collectionName = "test_lb_policy"
|
2023-06-13 10:20:37 +08:00
|
|
|
s.loadCollection()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LBPolicySuite) loadCollection() {
|
|
|
|
fieldName2Types := map[string]schemapb.DataType{
|
|
|
|
testBoolField: schemapb.DataType_Bool,
|
|
|
|
testInt32Field: schemapb.DataType_Int32,
|
|
|
|
testInt64Field: schemapb.DataType_Int64,
|
|
|
|
testFloatField: schemapb.DataType_Float,
|
|
|
|
testDoubleField: schemapb.DataType_Double,
|
|
|
|
testFloatVecField: schemapb.DataType_FloatVector,
|
|
|
|
}
|
|
|
|
if enableMultipleVectorFields {
|
|
|
|
fieldName2Types[testBinaryVecField] = schemapb.DataType_BinaryVector
|
|
|
|
}
|
|
|
|
|
2023-08-01 17:33:06 +08:00
|
|
|
schema := constructCollectionSchemaByDataType(s.collectionName, fieldName2Types, testInt64Field, false)
|
2023-06-13 10:20:37 +08:00
|
|
|
marshaledSchema, err := proto.Marshal(schema)
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
createColT := &createCollectionTask{
|
|
|
|
Condition: NewTaskCondition(ctx),
|
|
|
|
CreateCollectionRequest: &milvuspb.CreateCollectionRequest{
|
2023-08-01 17:33:06 +08:00
|
|
|
CollectionName: s.collectionName,
|
2023-06-25 17:20:43 +08:00
|
|
|
DbName: dbName,
|
2023-06-13 10:20:37 +08:00
|
|
|
Schema: marshaledSchema,
|
|
|
|
ShardsNum: common.DefaultShardsNum,
|
|
|
|
},
|
|
|
|
ctx: ctx,
|
|
|
|
rootCoord: s.rc,
|
|
|
|
}
|
|
|
|
|
|
|
|
s.NoError(createColT.OnEnqueue())
|
|
|
|
s.NoError(createColT.PreExecute(ctx))
|
|
|
|
s.NoError(createColT.Execute(ctx))
|
|
|
|
s.NoError(createColT.PostExecute(ctx))
|
|
|
|
|
2023-08-01 17:33:06 +08:00
|
|
|
collectionID, err := globalMetaCache.GetCollectionID(ctx, dbName, s.collectionName)
|
2023-06-13 10:20:37 +08:00
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
status, err := s.qc.LoadCollection(ctx, &querypb.LoadCollectionRequest{
|
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_LoadCollection,
|
|
|
|
SourceID: paramtable.GetNodeID(),
|
|
|
|
},
|
|
|
|
CollectionID: collectionID,
|
|
|
|
})
|
|
|
|
s.NoError(err)
|
|
|
|
s.Equal(commonpb.ErrorCode_Success, status.ErrorCode)
|
2023-08-01 17:33:06 +08:00
|
|
|
s.collectionID = collectionID
|
2023-06-13 10:20:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LBPolicySuite) TestSelectNode() {
|
|
|
|
ctx := context.Background()
|
2023-06-27 09:52:44 +08:00
|
|
|
s.lbBalancer.EXPECT().SelectNode(mock.Anything, mock.Anything, mock.Anything).Return(5, nil)
|
2023-06-13 10:20:37 +08:00
|
|
|
targetNode, err := s.lbPolicy.selectNode(ctx, ChannelWorkload{
|
2023-08-01 17:33:06 +08:00
|
|
|
db: dbName,
|
|
|
|
collectionName: s.collectionName,
|
|
|
|
collectionID: s.collectionID,
|
|
|
|
channel: s.channels[0],
|
|
|
|
shardLeaders: s.nodes,
|
|
|
|
nq: 1,
|
2023-06-13 10:20:37 +08:00
|
|
|
}, typeutil.NewUniqueSet())
|
|
|
|
s.NoError(err)
|
|
|
|
s.Equal(int64(5), targetNode)
|
|
|
|
|
|
|
|
// test select node failed, then update shard leader cache and retry, expect success
|
|
|
|
s.lbBalancer.ExpectedCalls = nil
|
2023-06-27 09:52:44 +08:00
|
|
|
s.lbBalancer.EXPECT().SelectNode(mock.Anything, mock.Anything, mock.Anything).Return(-1, errors.New("fake err")).Times(1)
|
|
|
|
s.lbBalancer.EXPECT().SelectNode(mock.Anything, mock.Anything, mock.Anything).Return(3, nil)
|
2023-06-13 10:20:37 +08:00
|
|
|
targetNode, err = s.lbPolicy.selectNode(ctx, ChannelWorkload{
|
2023-08-01 17:33:06 +08:00
|
|
|
db: dbName,
|
|
|
|
collectionName: s.collectionName,
|
|
|
|
collectionID: s.collectionID,
|
|
|
|
channel: s.channels[0],
|
|
|
|
shardLeaders: []int64{},
|
|
|
|
nq: 1,
|
2023-06-13 10:20:37 +08:00
|
|
|
}, typeutil.NewUniqueSet())
|
|
|
|
s.NoError(err)
|
|
|
|
s.Equal(int64(3), targetNode)
|
|
|
|
|
|
|
|
// test select node always fails, expected failure
|
|
|
|
s.lbBalancer.ExpectedCalls = nil
|
2023-07-17 14:59:34 +08:00
|
|
|
s.lbBalancer.EXPECT().SelectNode(mock.Anything, mock.Anything, mock.Anything).Return(-1, merr.ErrNodeNotAvailable)
|
2023-06-13 10:20:37 +08:00
|
|
|
targetNode, err = s.lbPolicy.selectNode(ctx, ChannelWorkload{
|
2023-08-01 17:33:06 +08:00
|
|
|
db: dbName,
|
|
|
|
collectionName: s.collectionName,
|
|
|
|
collectionID: s.collectionID,
|
|
|
|
channel: s.channels[0],
|
|
|
|
shardLeaders: []int64{},
|
|
|
|
nq: 1,
|
2023-06-13 10:20:37 +08:00
|
|
|
}, typeutil.NewUniqueSet())
|
2023-07-17 14:59:34 +08:00
|
|
|
s.ErrorIs(err, merr.ErrNodeNotAvailable)
|
2023-06-13 10:20:37 +08:00
|
|
|
s.Equal(int64(-1), targetNode)
|
|
|
|
|
|
|
|
// test all nodes has been excluded, expected failure
|
|
|
|
s.lbBalancer.ExpectedCalls = nil
|
2023-07-17 14:59:34 +08:00
|
|
|
s.lbBalancer.EXPECT().SelectNode(mock.Anything, mock.Anything, mock.Anything).Return(-1, merr.ErrNodeNotAvailable)
|
2023-06-13 10:20:37 +08:00
|
|
|
targetNode, err = s.lbPolicy.selectNode(ctx, ChannelWorkload{
|
2023-08-01 17:33:06 +08:00
|
|
|
db: dbName,
|
|
|
|
collectionName: s.collectionName,
|
|
|
|
collectionID: s.collectionID,
|
|
|
|
channel: s.channels[0],
|
|
|
|
shardLeaders: s.nodes,
|
|
|
|
nq: 1,
|
2023-06-13 10:20:37 +08:00
|
|
|
}, typeutil.NewUniqueSet(s.nodes...))
|
2023-11-30 18:34:32 +08:00
|
|
|
s.ErrorIs(err, merr.ErrChannelNotAvailable)
|
2023-06-13 10:20:37 +08:00
|
|
|
s.Equal(int64(-1), targetNode)
|
|
|
|
|
|
|
|
// test get shard leaders failed, retry to select node failed
|
|
|
|
s.lbBalancer.ExpectedCalls = nil
|
2023-07-17 14:59:34 +08:00
|
|
|
s.lbBalancer.EXPECT().SelectNode(mock.Anything, mock.Anything, mock.Anything).Return(-1, merr.ErrNodeNotAvailable)
|
2023-06-13 10:20:37 +08:00
|
|
|
s.qc.ExpectedCalls = nil
|
2023-07-17 14:59:34 +08:00
|
|
|
s.qc.EXPECT().GetShardLeaders(mock.Anything, mock.Anything).Return(nil, merr.ErrServiceUnavailable)
|
2023-06-13 10:20:37 +08:00
|
|
|
targetNode, err = s.lbPolicy.selectNode(ctx, ChannelWorkload{
|
2023-08-01 17:33:06 +08:00
|
|
|
db: dbName,
|
|
|
|
collectionName: s.collectionName,
|
|
|
|
collectionID: s.collectionID,
|
|
|
|
channel: s.channels[0],
|
|
|
|
shardLeaders: s.nodes,
|
|
|
|
nq: 1,
|
2023-06-13 10:20:37 +08:00
|
|
|
}, typeutil.NewUniqueSet())
|
2023-07-17 14:59:34 +08:00
|
|
|
s.ErrorIs(err, merr.ErrServiceUnavailable)
|
2023-06-13 10:20:37 +08:00
|
|
|
s.Equal(int64(-1), targetNode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LBPolicySuite) TestExecuteWithRetry() {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
// test execute success
|
|
|
|
s.lbBalancer.ExpectedCalls = nil
|
|
|
|
s.mgr.EXPECT().GetClient(mock.Anything, mock.Anything).Return(s.qn, nil)
|
2023-06-27 09:52:44 +08:00
|
|
|
s.lbBalancer.EXPECT().SelectNode(mock.Anything, mock.Anything, mock.Anything).Return(1, nil)
|
2023-06-16 18:38:39 +08:00
|
|
|
s.lbBalancer.EXPECT().CancelWorkload(mock.Anything, mock.Anything)
|
2023-06-13 10:20:37 +08:00
|
|
|
err := s.lbPolicy.ExecuteWithRetry(ctx, ChannelWorkload{
|
2023-08-01 17:33:06 +08:00
|
|
|
db: dbName,
|
|
|
|
collectionName: s.collectionName,
|
|
|
|
collectionID: s.collectionID,
|
|
|
|
channel: s.channels[0],
|
|
|
|
shardLeaders: s.nodes,
|
|
|
|
nq: 1,
|
2024-01-09 11:38:48 +08:00
|
|
|
exec: func(ctx context.Context, ui UniqueID, qn types.QueryNodeClient, channel string) error {
|
2023-06-13 10:20:37 +08:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
retryTimes: 1,
|
|
|
|
})
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
// test select node failed, expected error
|
|
|
|
s.lbBalancer.ExpectedCalls = nil
|
2023-07-17 14:59:34 +08:00
|
|
|
s.lbBalancer.EXPECT().SelectNode(mock.Anything, mock.Anything, mock.Anything).Return(-1, merr.ErrNodeNotAvailable)
|
2023-06-13 10:20:37 +08:00
|
|
|
err = s.lbPolicy.ExecuteWithRetry(ctx, ChannelWorkload{
|
2023-08-01 17:33:06 +08:00
|
|
|
db: dbName,
|
|
|
|
collectionName: s.collectionName,
|
|
|
|
collectionID: s.collectionID,
|
|
|
|
channel: s.channels[0],
|
|
|
|
shardLeaders: s.nodes,
|
|
|
|
nq: 1,
|
2024-01-09 11:38:48 +08:00
|
|
|
exec: func(ctx context.Context, ui UniqueID, qn types.QueryNodeClient, channel string) error {
|
2023-06-13 10:20:37 +08:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
retryTimes: 1,
|
|
|
|
})
|
2023-07-17 14:59:34 +08:00
|
|
|
s.ErrorIs(err, merr.ErrNodeNotAvailable)
|
2023-06-13 10:20:37 +08:00
|
|
|
|
|
|
|
// test get client failed, and retry failed, expected success
|
|
|
|
s.mgr.ExpectedCalls = nil
|
|
|
|
s.mgr.EXPECT().GetClient(mock.Anything, mock.Anything).Return(nil, errors.New("fake error")).Times(1)
|
|
|
|
s.lbBalancer.ExpectedCalls = nil
|
2023-06-27 09:52:44 +08:00
|
|
|
s.lbBalancer.EXPECT().SelectNode(mock.Anything, mock.Anything, mock.Anything).Return(1, nil)
|
2023-06-16 18:38:39 +08:00
|
|
|
s.lbBalancer.EXPECT().CancelWorkload(mock.Anything, mock.Anything)
|
2023-06-13 10:20:37 +08:00
|
|
|
err = s.lbPolicy.ExecuteWithRetry(ctx, ChannelWorkload{
|
2023-08-01 17:33:06 +08:00
|
|
|
db: dbName,
|
|
|
|
collectionName: s.collectionName,
|
|
|
|
collectionID: s.collectionID,
|
|
|
|
channel: s.channels[0],
|
|
|
|
shardLeaders: s.nodes,
|
|
|
|
nq: 1,
|
2024-01-09 11:38:48 +08:00
|
|
|
exec: func(ctx context.Context, ui UniqueID, qn types.QueryNodeClient, channel string) error {
|
2023-06-13 10:20:37 +08:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
retryTimes: 1,
|
|
|
|
})
|
|
|
|
s.Error(err)
|
|
|
|
|
|
|
|
s.mgr.ExpectedCalls = nil
|
|
|
|
s.mgr.EXPECT().GetClient(mock.Anything, mock.Anything).Return(nil, errors.New("fake error")).Times(1)
|
|
|
|
s.mgr.EXPECT().GetClient(mock.Anything, mock.Anything).Return(s.qn, nil)
|
2023-06-16 18:38:39 +08:00
|
|
|
s.lbBalancer.EXPECT().CancelWorkload(mock.Anything, mock.Anything)
|
2023-06-13 10:20:37 +08:00
|
|
|
err = s.lbPolicy.ExecuteWithRetry(ctx, ChannelWorkload{
|
2023-08-01 17:33:06 +08:00
|
|
|
db: dbName,
|
|
|
|
collectionName: s.collectionName,
|
|
|
|
collectionID: s.collectionID,
|
|
|
|
channel: s.channels[0],
|
|
|
|
shardLeaders: s.nodes,
|
|
|
|
nq: 1,
|
2024-01-09 11:38:48 +08:00
|
|
|
exec: func(ctx context.Context, ui UniqueID, qn types.QueryNodeClient, channel string) error {
|
2023-06-13 10:20:37 +08:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
retryTimes: 2,
|
|
|
|
})
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
// test exec failed, then retry success
|
|
|
|
s.mgr.ExpectedCalls = nil
|
|
|
|
s.mgr.EXPECT().GetClient(mock.Anything, mock.Anything).Return(s.qn, nil)
|
|
|
|
s.lbBalancer.ExpectedCalls = nil
|
2023-06-27 09:52:44 +08:00
|
|
|
s.lbBalancer.EXPECT().SelectNode(mock.Anything, mock.Anything, mock.Anything).Return(1, nil)
|
2023-06-16 18:38:39 +08:00
|
|
|
s.lbBalancer.EXPECT().CancelWorkload(mock.Anything, mock.Anything)
|
2023-06-13 10:20:37 +08:00
|
|
|
counter := 0
|
|
|
|
err = s.lbPolicy.ExecuteWithRetry(ctx, ChannelWorkload{
|
2023-08-01 17:33:06 +08:00
|
|
|
db: dbName,
|
|
|
|
collectionName: s.collectionName,
|
|
|
|
collectionID: s.collectionID,
|
|
|
|
channel: s.channels[0],
|
|
|
|
shardLeaders: s.nodes,
|
|
|
|
nq: 1,
|
2024-01-09 11:38:48 +08:00
|
|
|
exec: func(ctx context.Context, ui UniqueID, qn types.QueryNodeClient, channel string) error {
|
2023-06-13 10:20:37 +08:00
|
|
|
counter++
|
|
|
|
if counter == 1 {
|
|
|
|
return errors.New("fake error")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
retryTimes: 2,
|
|
|
|
})
|
|
|
|
s.NoError(err)
|
2023-08-16 19:38:18 +08:00
|
|
|
|
|
|
|
// test exec timeout
|
|
|
|
s.mgr.ExpectedCalls = nil
|
|
|
|
s.mgr.EXPECT().GetClient(mock.Anything, mock.Anything).Return(s.qn, nil)
|
|
|
|
s.lbBalancer.EXPECT().CancelWorkload(mock.Anything, mock.Anything)
|
2023-09-26 09:57:25 +08:00
|
|
|
s.qn.EXPECT().GetComponentStates(mock.Anything, mock.Anything).Return(nil, nil).Maybe()
|
2023-08-16 19:38:18 +08:00
|
|
|
s.qn.EXPECT().Search(mock.Anything, mock.Anything).Return(nil, context.Canceled).Times(1)
|
|
|
|
s.qn.EXPECT().Search(mock.Anything, mock.Anything).Return(nil, context.DeadlineExceeded)
|
|
|
|
err = s.lbPolicy.ExecuteWithRetry(ctx, ChannelWorkload{
|
|
|
|
db: dbName,
|
|
|
|
collectionName: s.collectionName,
|
|
|
|
collectionID: s.collectionID,
|
|
|
|
channel: s.channels[0],
|
|
|
|
shardLeaders: s.nodes,
|
|
|
|
nq: 1,
|
2024-01-09 11:38:48 +08:00
|
|
|
exec: func(ctx context.Context, ui UniqueID, qn types.QueryNodeClient, channel string) error {
|
2023-08-16 19:38:18 +08:00
|
|
|
_, err := qn.Search(ctx, nil)
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
retryTimes: 2,
|
|
|
|
})
|
2023-09-04 09:57:09 +08:00
|
|
|
s.True(merr.IsCanceledOrTimeout(err))
|
2023-06-13 10:20:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LBPolicySuite) TestExecute() {
|
|
|
|
ctx := context.Background()
|
2023-07-17 14:59:34 +08:00
|
|
|
mockErr := errors.New("mock error")
|
2023-06-13 10:20:37 +08:00
|
|
|
// test all channel success
|
|
|
|
s.mgr.EXPECT().GetClient(mock.Anything, mock.Anything).Return(s.qn, nil)
|
2023-06-27 09:52:44 +08:00
|
|
|
s.lbBalancer.EXPECT().SelectNode(mock.Anything, mock.Anything, mock.Anything).Return(1, nil)
|
2023-06-16 18:38:39 +08:00
|
|
|
s.lbBalancer.EXPECT().CancelWorkload(mock.Anything, mock.Anything)
|
2023-06-13 10:20:37 +08:00
|
|
|
err := s.lbPolicy.Execute(ctx, CollectionWorkLoad{
|
2023-08-01 17:33:06 +08:00
|
|
|
db: dbName,
|
|
|
|
collectionName: s.collectionName,
|
|
|
|
collectionID: s.collectionID,
|
|
|
|
nq: 1,
|
2024-01-09 11:38:48 +08:00
|
|
|
exec: func(ctx context.Context, ui UniqueID, qn types.QueryNodeClient, channel string) error {
|
2023-06-13 10:20:37 +08:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
// test some channel failed
|
|
|
|
counter := atomic.NewInt64(0)
|
|
|
|
err = s.lbPolicy.Execute(ctx, CollectionWorkLoad{
|
2023-08-01 17:33:06 +08:00
|
|
|
db: dbName,
|
|
|
|
collectionName: s.collectionName,
|
|
|
|
collectionID: s.collectionID,
|
|
|
|
nq: 1,
|
2024-01-09 11:38:48 +08:00
|
|
|
exec: func(ctx context.Context, ui UniqueID, qn types.QueryNodeClient, channel string) error {
|
2023-08-03 15:55:09 +08:00
|
|
|
// succeed in first execute
|
2023-06-13 10:20:37 +08:00
|
|
|
if counter.Add(1) == 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-17 14:59:34 +08:00
|
|
|
return mockErr
|
2023-06-13 10:20:37 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
s.Error(err)
|
2023-08-03 15:55:09 +08:00
|
|
|
s.Equal(int64(11), counter.Load())
|
2023-06-13 10:20:37 +08:00
|
|
|
|
|
|
|
// test get shard leader failed
|
|
|
|
s.qc.ExpectedCalls = nil
|
2023-08-01 17:33:06 +08:00
|
|
|
globalMetaCache.DeprecateShardCache(dbName, s.collectionName)
|
2023-07-17 14:59:34 +08:00
|
|
|
s.qc.EXPECT().GetShardLeaders(mock.Anything, mock.Anything).Return(nil, mockErr)
|
2023-06-13 10:20:37 +08:00
|
|
|
err = s.lbPolicy.Execute(ctx, CollectionWorkLoad{
|
2023-08-01 17:33:06 +08:00
|
|
|
db: dbName,
|
|
|
|
collectionName: s.collectionName,
|
|
|
|
collectionID: s.collectionID,
|
|
|
|
nq: 1,
|
2024-01-09 11:38:48 +08:00
|
|
|
exec: func(ctx context.Context, ui UniqueID, qn types.QueryNodeClient, channel string) error {
|
2023-06-13 10:20:37 +08:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
2023-07-17 14:59:34 +08:00
|
|
|
s.ErrorIs(err, mockErr)
|
2023-06-13 10:20:37 +08:00
|
|
|
}
|
|
|
|
|
2023-06-16 18:38:39 +08:00
|
|
|
func (s *LBPolicySuite) TestUpdateCostMetrics() {
|
|
|
|
s.lbBalancer.EXPECT().UpdateCostMetrics(mock.Anything, mock.Anything)
|
|
|
|
s.lbPolicy.UpdateCostMetrics(1, &internalpb.CostAggregation{})
|
|
|
|
}
|
|
|
|
|
2023-06-25 19:46:44 +08:00
|
|
|
func (s *LBPolicySuite) TestNewLBPolicy() {
|
|
|
|
policy := NewLBPolicyImpl(s.mgr)
|
|
|
|
s.Equal(reflect.TypeOf(policy.balancer).String(), "*proxy.LookAsideBalancer")
|
|
|
|
policy.Close()
|
|
|
|
|
|
|
|
Params.Save(Params.ProxyCfg.ReplicaSelectionPolicy.Key, "round_robin")
|
|
|
|
policy = NewLBPolicyImpl(s.mgr)
|
|
|
|
s.Equal(reflect.TypeOf(policy.balancer).String(), "*proxy.RoundRobinBalancer")
|
|
|
|
policy.Close()
|
|
|
|
|
|
|
|
Params.Save(Params.ProxyCfg.ReplicaSelectionPolicy.Key, "look_aside")
|
|
|
|
policy = NewLBPolicyImpl(s.mgr)
|
|
|
|
s.Equal(reflect.TypeOf(policy.balancer).String(), "*proxy.LookAsideBalancer")
|
|
|
|
policy.Close()
|
|
|
|
}
|
|
|
|
|
2023-06-13 10:20:37 +08:00
|
|
|
func TestLBPolicySuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(LBPolicySuite))
|
|
|
|
}
|