2022-10-11 11:39:22 +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.
|
|
|
|
|
2022-09-15 18:48:32 +08:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2023-06-27 11:48:45 +08:00
|
|
|
"time"
|
2022-09-15 18:48:32 +08:00
|
|
|
|
2023-04-27 17:46:34 +08:00
|
|
|
"github.com/cockroachdb/errors"
|
2022-11-07 19:37:04 +08:00
|
|
|
"github.com/samber/lo"
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
"github.com/stretchr/testify/suite"
|
2023-04-18 18:30:32 +08:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2022-11-07 19:37:04 +08:00
|
|
|
|
|
|
|
etcdkv "github.com/milvus-io/milvus/internal/kv/etcd"
|
2024-03-15 14:19:03 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/metastore"
|
2023-07-31 13:57:04 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/metastore/kv/querycoord"
|
2022-09-15 18:48:32 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/datapb"
|
2022-11-07 19:37:04 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/querypb"
|
|
|
|
. "github.com/milvus-io/milvus/internal/querycoordv2/params"
|
2023-01-30 10:19:48 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/querycoordv2/session"
|
2024-06-25 21:18:15 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/kv"
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/etcd"
|
2023-10-18 15:32:11 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/merr"
|
2023-09-05 10:31:48 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/paramtable"
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/typeutil"
|
2022-09-15 18:48:32 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type TargetManagerSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
// Data
|
2024-01-16 14:40:53 +08:00
|
|
|
collections []int64
|
|
|
|
partitions map[int64][]int64
|
|
|
|
channels map[int64][]string
|
|
|
|
segments map[int64]map[int64][]int64 // CollectionID, PartitionID -> Segments
|
|
|
|
level0Segments []int64
|
2022-09-15 18:48:32 +08:00
|
|
|
// Derived data
|
|
|
|
allChannels []string
|
|
|
|
allSegments []int64
|
|
|
|
|
2024-03-15 14:19:03 +08:00
|
|
|
kv kv.MetaKv
|
|
|
|
catalog metastore.QueryCoordCatalog
|
|
|
|
meta *Meta
|
|
|
|
broker *MockBroker
|
2022-09-15 18:48:32 +08:00
|
|
|
// Test object
|
|
|
|
mgr *TargetManager
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *TargetManagerSuite) SetupSuite() {
|
2023-09-05 10:31:48 +08:00
|
|
|
paramtable.Init()
|
2022-09-15 18:48:32 +08:00
|
|
|
suite.collections = []int64{1000, 1001}
|
|
|
|
suite.partitions = map[int64][]int64{
|
|
|
|
1000: {100, 101},
|
|
|
|
1001: {102, 103},
|
|
|
|
}
|
|
|
|
suite.channels = map[int64][]string{
|
|
|
|
1000: {"1000-dmc0", "1000-dmc1"},
|
|
|
|
1001: {"1001-dmc0", "1001-dmc1"},
|
|
|
|
}
|
|
|
|
suite.segments = map[int64]map[int64][]int64{
|
|
|
|
1000: {
|
|
|
|
100: {1, 2},
|
|
|
|
101: {3, 4},
|
|
|
|
},
|
|
|
|
1001: {
|
|
|
|
102: {5, 6},
|
|
|
|
103: {7, 8},
|
|
|
|
},
|
|
|
|
}
|
2024-01-16 14:40:53 +08:00
|
|
|
suite.level0Segments = []int64{10000, 10001}
|
2022-09-15 18:48:32 +08:00
|
|
|
|
|
|
|
suite.allChannels = make([]string, 0)
|
|
|
|
suite.allSegments = make([]int64, 0)
|
|
|
|
for _, channels := range suite.channels {
|
|
|
|
suite.allChannels = append(suite.allChannels, channels...)
|
|
|
|
}
|
|
|
|
for _, partitions := range suite.segments {
|
|
|
|
for _, segments := range partitions {
|
|
|
|
suite.allSegments = append(suite.allSegments, segments...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *TargetManagerSuite) SetupTest() {
|
2022-11-07 19:37:04 +08:00
|
|
|
var err error
|
|
|
|
config := GenerateEtcdConfig()
|
2022-11-30 18:23:15 +08:00
|
|
|
cli, err := etcd.GetEtcdClient(
|
|
|
|
config.UseEmbedEtcd.GetAsBool(),
|
|
|
|
config.EtcdUseSSL.GetAsBool(),
|
|
|
|
config.Endpoints.GetAsStrings(),
|
|
|
|
config.EtcdTLSCert.GetValue(),
|
|
|
|
config.EtcdTLSKey.GetValue(),
|
|
|
|
config.EtcdTLSCACert.GetValue(),
|
|
|
|
config.EtcdTLSMinVersion.GetValue())
|
2022-11-07 19:37:04 +08:00
|
|
|
suite.Require().NoError(err)
|
2022-11-17 18:59:09 +08:00
|
|
|
suite.kv = etcdkv.NewEtcdKV(cli, config.MetaRootPath.GetValue())
|
2022-11-07 19:37:04 +08:00
|
|
|
|
|
|
|
// meta
|
2024-03-15 14:19:03 +08:00
|
|
|
suite.catalog = querycoord.NewCatalog(suite.kv)
|
2022-11-07 19:37:04 +08:00
|
|
|
idAllocator := RandomIncrementIDAllocator()
|
2024-03-15 14:19:03 +08:00
|
|
|
suite.meta = NewMeta(idAllocator, suite.catalog, session.NewNodeManager())
|
2022-11-07 19:37:04 +08:00
|
|
|
suite.broker = NewMockBroker(suite.T())
|
|
|
|
suite.mgr = NewTargetManager(suite.broker, suite.meta)
|
|
|
|
|
|
|
|
for _, collection := range suite.collections {
|
|
|
|
dmChannels := make([]*datapb.VchannelInfo, 0)
|
|
|
|
for _, channel := range suite.channels[collection] {
|
|
|
|
dmChannels = append(dmChannels, &datapb.VchannelInfo{
|
2024-01-16 14:40:53 +08:00
|
|
|
CollectionID: collection,
|
|
|
|
ChannelName: channel,
|
|
|
|
LevelZeroSegmentIds: suite.level0Segments,
|
2022-11-07 19:37:04 +08:00
|
|
|
})
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
2022-11-07 19:37:04 +08:00
|
|
|
|
2023-04-18 18:30:32 +08:00
|
|
|
allSegments := make([]*datapb.SegmentInfo, 0)
|
|
|
|
for partitionID, segments := range suite.segments[collection] {
|
2022-09-15 18:48:32 +08:00
|
|
|
for _, segment := range segments {
|
2023-04-18 18:30:32 +08:00
|
|
|
allSegments = append(allSegments, &datapb.SegmentInfo{
|
|
|
|
ID: segment,
|
2022-11-07 19:37:04 +08:00
|
|
|
InsertChannel: suite.channels[collection][0],
|
2023-04-18 18:30:32 +08:00
|
|
|
CollectionID: collection,
|
|
|
|
PartitionID: partitionID,
|
2022-09-15 18:48:32 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-04-18 18:30:32 +08:00
|
|
|
suite.broker.EXPECT().GetRecoveryInfoV2(mock.Anything, collection).Return(dmChannels, allSegments, nil)
|
|
|
|
|
2023-08-04 10:31:06 +08:00
|
|
|
suite.meta.PutCollection(&Collection{
|
|
|
|
CollectionLoadInfo: &querypb.CollectionLoadInfo{
|
|
|
|
CollectionID: collection,
|
2023-09-21 09:45:27 +08:00
|
|
|
ReplicaNumber: 1,
|
|
|
|
},
|
2023-08-04 10:31:06 +08:00
|
|
|
})
|
|
|
|
for _, partition := range suite.partitions[collection] {
|
|
|
|
suite.meta.PutPartition(&Partition{
|
|
|
|
PartitionLoadInfo: &querypb.PartitionLoadInfo{
|
|
|
|
CollectionID: collection,
|
|
|
|
PartitionID: partition,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
suite.mgr.UpdateCollectionNextTarget(collection)
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-07 19:37:04 +08:00
|
|
|
func (suite *TargetManagerSuite) TearDownSuite() {
|
|
|
|
suite.kv.Close()
|
|
|
|
}
|
2022-09-15 18:48:32 +08:00
|
|
|
|
2022-11-07 19:37:04 +08:00
|
|
|
func (suite *TargetManagerSuite) TestUpdateCurrentTarget() {
|
|
|
|
collectionID := int64(1000)
|
|
|
|
suite.assertSegments(suite.getAllSegment(collectionID, suite.partitions[collectionID]),
|
2023-10-25 00:44:12 +08:00
|
|
|
suite.mgr.GetSealedSegmentsByCollection(collectionID, NextTarget))
|
2022-11-07 19:37:04 +08:00
|
|
|
suite.assertChannels(suite.channels[collectionID], suite.mgr.GetDmChannelsByCollection(collectionID, NextTarget))
|
2023-10-25 00:44:12 +08:00
|
|
|
suite.assertSegments([]int64{}, suite.mgr.GetSealedSegmentsByCollection(collectionID, CurrentTarget))
|
2022-11-07 19:37:04 +08:00
|
|
|
suite.assertChannels([]string{}, suite.mgr.GetDmChannelsByCollection(collectionID, CurrentTarget))
|
|
|
|
|
|
|
|
suite.mgr.UpdateCollectionCurrentTarget(collectionID)
|
2023-10-25 00:44:12 +08:00
|
|
|
suite.assertSegments([]int64{}, suite.mgr.GetSealedSegmentsByCollection(collectionID, NextTarget))
|
2022-11-07 19:37:04 +08:00
|
|
|
suite.assertChannels([]string{}, suite.mgr.GetDmChannelsByCollection(collectionID, NextTarget))
|
|
|
|
suite.assertSegments(suite.getAllSegment(collectionID, suite.partitions[collectionID]),
|
2023-10-25 00:44:12 +08:00
|
|
|
suite.mgr.GetSealedSegmentsByCollection(collectionID, CurrentTarget))
|
2022-11-07 19:37:04 +08:00
|
|
|
suite.assertChannels(suite.channels[collectionID], suite.mgr.GetDmChannelsByCollection(collectionID, CurrentTarget))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *TargetManagerSuite) TestUpdateNextTarget() {
|
|
|
|
collectionID := int64(1003)
|
2023-10-25 00:44:12 +08:00
|
|
|
suite.assertSegments([]int64{}, suite.mgr.GetSealedSegmentsByCollection(collectionID, NextTarget))
|
2022-11-07 19:37:04 +08:00
|
|
|
suite.assertChannels([]string{}, suite.mgr.GetDmChannelsByCollection(collectionID, NextTarget))
|
2023-10-25 00:44:12 +08:00
|
|
|
suite.assertSegments([]int64{}, suite.mgr.GetSealedSegmentsByCollection(collectionID, CurrentTarget))
|
2022-11-07 19:37:04 +08:00
|
|
|
suite.assertChannels([]string{}, suite.mgr.GetDmChannelsByCollection(collectionID, CurrentTarget))
|
|
|
|
|
|
|
|
suite.meta.PutCollection(&Collection{
|
|
|
|
CollectionLoadInfo: &querypb.CollectionLoadInfo{
|
|
|
|
CollectionID: collectionID,
|
2023-09-21 09:45:27 +08:00
|
|
|
ReplicaNumber: 1,
|
|
|
|
},
|
2022-11-07 19:37:04 +08:00
|
|
|
})
|
|
|
|
suite.meta.PutPartition(&Partition{
|
|
|
|
PartitionLoadInfo: &querypb.PartitionLoadInfo{
|
|
|
|
CollectionID: collectionID,
|
|
|
|
PartitionID: 1,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
nextTargetChannels := []*datapb.VchannelInfo{
|
|
|
|
{
|
|
|
|
CollectionID: collectionID,
|
|
|
|
ChannelName: "channel-1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CollectionID: collectionID,
|
|
|
|
ChannelName: "channel-2",
|
|
|
|
},
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
|
|
|
|
2023-04-18 18:30:32 +08:00
|
|
|
nextTargetSegments := []*datapb.SegmentInfo{
|
|
|
|
{
|
|
|
|
ID: 11,
|
|
|
|
PartitionID: 1,
|
|
|
|
InsertChannel: "channel-1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 12,
|
|
|
|
PartitionID: 1,
|
|
|
|
InsertChannel: "channel-2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
nextTargetBinlogs := []*datapb.SegmentBinlogs{
|
2022-11-07 19:37:04 +08:00
|
|
|
{
|
|
|
|
SegmentID: 11,
|
|
|
|
InsertChannel: "channel-1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SegmentID: 12,
|
|
|
|
InsertChannel: "channel-2",
|
|
|
|
},
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
2022-11-07 19:37:04 +08:00
|
|
|
|
2023-04-18 18:30:32 +08:00
|
|
|
suite.broker.EXPECT().GetRecoveryInfoV2(mock.Anything, collectionID).Return(nextTargetChannels, nextTargetSegments, nil)
|
2023-08-04 10:31:06 +08:00
|
|
|
suite.mgr.UpdateCollectionNextTarget(collectionID)
|
2023-10-25 00:44:12 +08:00
|
|
|
suite.assertSegments([]int64{11, 12}, suite.mgr.GetSealedSegmentsByCollection(collectionID, NextTarget))
|
2022-11-07 19:37:04 +08:00
|
|
|
suite.assertChannels([]string{"channel-1", "channel-2"}, suite.mgr.GetDmChannelsByCollection(collectionID, NextTarget))
|
2023-10-25 00:44:12 +08:00
|
|
|
suite.assertSegments([]int64{}, suite.mgr.GetSealedSegmentsByCollection(collectionID, CurrentTarget))
|
2022-11-07 19:37:04 +08:00
|
|
|
suite.assertChannels([]string{}, suite.mgr.GetDmChannelsByCollection(collectionID, CurrentTarget))
|
2023-04-18 18:30:32 +08:00
|
|
|
|
|
|
|
suite.broker.ExpectedCalls = nil
|
2023-04-27 17:46:34 +08:00
|
|
|
// test getRecoveryInfoV2 failed , then back to getRecoveryInfo succeed
|
2023-10-18 15:32:11 +08:00
|
|
|
suite.broker.EXPECT().GetRecoveryInfoV2(mock.Anything, collectionID).Return(
|
|
|
|
nil, nil, merr.WrapErrServiceUnimplemented(status.Errorf(codes.Unimplemented, "fake not found")))
|
2023-04-18 18:30:32 +08:00
|
|
|
suite.broker.EXPECT().GetPartitions(mock.Anything, mock.Anything).Return([]int64{1}, nil)
|
|
|
|
suite.broker.EXPECT().GetRecoveryInfo(mock.Anything, collectionID, int64(1)).Return(nextTargetChannels, nextTargetBinlogs, nil)
|
2023-08-04 10:31:06 +08:00
|
|
|
err := suite.mgr.UpdateCollectionNextTarget(collectionID)
|
2023-04-18 18:30:32 +08:00
|
|
|
suite.NoError(err)
|
|
|
|
|
2023-04-27 17:46:34 +08:00
|
|
|
suite.broker.ExpectedCalls = nil
|
|
|
|
// test getRecoveryInfoV2 failed , then retry getRecoveryInfoV2 succeed
|
|
|
|
suite.broker.EXPECT().GetRecoveryInfoV2(mock.Anything, collectionID).Return(nil, nil, errors.New("fake error")).Times(1)
|
|
|
|
suite.broker.EXPECT().GetRecoveryInfoV2(mock.Anything, collectionID).Return(nextTargetChannels, nextTargetSegments, nil)
|
2023-08-04 10:31:06 +08:00
|
|
|
err = suite.mgr.UpdateCollectionNextTarget(collectionID)
|
2023-04-27 17:46:34 +08:00
|
|
|
suite.NoError(err)
|
|
|
|
|
2023-04-18 18:30:32 +08:00
|
|
|
err = suite.mgr.UpdateCollectionNextTarget(collectionID)
|
|
|
|
suite.NoError(err)
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
|
|
|
|
2022-11-07 19:37:04 +08:00
|
|
|
func (suite *TargetManagerSuite) TestRemovePartition() {
|
|
|
|
collectionID := int64(1000)
|
2023-10-25 00:44:12 +08:00
|
|
|
suite.assertSegments(suite.getAllSegment(collectionID, suite.partitions[collectionID]), suite.mgr.GetSealedSegmentsByCollection(collectionID, NextTarget))
|
2022-11-07 19:37:04 +08:00
|
|
|
suite.assertChannels(suite.channels[collectionID], suite.mgr.GetDmChannelsByCollection(collectionID, NextTarget))
|
2023-10-25 00:44:12 +08:00
|
|
|
suite.assertSegments([]int64{}, suite.mgr.GetSealedSegmentsByCollection(collectionID, CurrentTarget))
|
2022-11-07 19:37:04 +08:00
|
|
|
suite.assertChannels([]string{}, suite.mgr.GetDmChannelsByCollection(collectionID, CurrentTarget))
|
2022-09-15 18:48:32 +08:00
|
|
|
|
2022-11-07 19:37:04 +08:00
|
|
|
suite.mgr.RemovePartition(collectionID, 100)
|
2024-01-16 14:40:53 +08:00
|
|
|
suite.assertSegments(append([]int64{3, 4}, suite.level0Segments...), suite.mgr.GetSealedSegmentsByCollection(collectionID, NextTarget))
|
2022-11-07 19:37:04 +08:00
|
|
|
suite.assertChannels(suite.channels[collectionID], suite.mgr.GetDmChannelsByCollection(collectionID, NextTarget))
|
2023-10-25 00:44:12 +08:00
|
|
|
suite.assertSegments([]int64{}, suite.mgr.GetSealedSegmentsByCollection(collectionID, CurrentTarget))
|
2022-11-07 19:37:04 +08:00
|
|
|
suite.assertChannels([]string{}, suite.mgr.GetDmChannelsByCollection(collectionID, CurrentTarget))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *TargetManagerSuite) TestRemoveCollection() {
|
|
|
|
collectionID := int64(1000)
|
2023-10-25 00:44:12 +08:00
|
|
|
suite.assertSegments(suite.getAllSegment(collectionID, suite.partitions[collectionID]), suite.mgr.GetSealedSegmentsByCollection(collectionID, NextTarget))
|
2022-11-07 19:37:04 +08:00
|
|
|
suite.assertChannels(suite.channels[collectionID], suite.mgr.GetDmChannelsByCollection(collectionID, NextTarget))
|
2023-10-25 00:44:12 +08:00
|
|
|
suite.assertSegments([]int64{}, suite.mgr.GetSealedSegmentsByCollection(collectionID, CurrentTarget))
|
2022-11-07 19:37:04 +08:00
|
|
|
suite.assertChannels([]string{}, suite.mgr.GetDmChannelsByCollection(collectionID, CurrentTarget))
|
|
|
|
|
|
|
|
suite.mgr.RemoveCollection(collectionID)
|
2023-10-25 00:44:12 +08:00
|
|
|
suite.assertSegments([]int64{}, suite.mgr.GetSealedSegmentsByCollection(collectionID, NextTarget))
|
2022-11-07 19:37:04 +08:00
|
|
|
suite.assertChannels([]string{}, suite.mgr.GetDmChannelsByCollection(collectionID, NextTarget))
|
2023-10-25 00:44:12 +08:00
|
|
|
suite.assertSegments([]int64{}, suite.mgr.GetSealedSegmentsByCollection(collectionID, CurrentTarget))
|
2022-11-07 19:37:04 +08:00
|
|
|
suite.assertChannels([]string{}, suite.mgr.GetDmChannelsByCollection(collectionID, CurrentTarget))
|
|
|
|
|
|
|
|
collectionID = int64(1001)
|
|
|
|
suite.mgr.UpdateCollectionCurrentTarget(collectionID)
|
2023-10-25 00:44:12 +08:00
|
|
|
suite.assertSegments([]int64{}, suite.mgr.GetSealedSegmentsByCollection(collectionID, NextTarget))
|
2022-11-07 19:37:04 +08:00
|
|
|
suite.assertChannels([]string{}, suite.mgr.GetDmChannelsByCollection(collectionID, NextTarget))
|
2023-10-25 00:44:12 +08:00
|
|
|
suite.assertSegments(suite.getAllSegment(collectionID, suite.partitions[collectionID]), suite.mgr.GetSealedSegmentsByCollection(collectionID, CurrentTarget))
|
2022-11-07 19:37:04 +08:00
|
|
|
suite.assertChannels(suite.channels[collectionID], suite.mgr.GetDmChannelsByCollection(collectionID, CurrentTarget))
|
|
|
|
|
|
|
|
suite.mgr.RemoveCollection(collectionID)
|
2023-10-25 00:44:12 +08:00
|
|
|
suite.assertSegments([]int64{}, suite.mgr.GetSealedSegmentsByCollection(collectionID, NextTarget))
|
2022-11-07 19:37:04 +08:00
|
|
|
suite.assertChannels([]string{}, suite.mgr.GetDmChannelsByCollection(collectionID, NextTarget))
|
2023-10-25 00:44:12 +08:00
|
|
|
suite.assertSegments([]int64{}, suite.mgr.GetSealedSegmentsByCollection(collectionID, CurrentTarget))
|
2022-11-07 19:37:04 +08:00
|
|
|
suite.assertChannels([]string{}, suite.mgr.GetDmChannelsByCollection(collectionID, CurrentTarget))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *TargetManagerSuite) getAllSegment(collectionID int64, partitionIDs []int64) []int64 {
|
|
|
|
allSegments := make([]int64, 0)
|
2022-09-15 18:48:32 +08:00
|
|
|
for collection, partitions := range suite.segments {
|
2022-11-07 19:37:04 +08:00
|
|
|
if collectionID == collection {
|
|
|
|
for partition, segments := range partitions {
|
|
|
|
if lo.Contains(partitionIDs, partition) {
|
|
|
|
allSegments = append(allSegments, segments...)
|
|
|
|
}
|
|
|
|
}
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-16 14:40:53 +08:00
|
|
|
return append(allSegments, suite.level0Segments...)
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
|
|
|
|
2022-11-07 19:37:04 +08:00
|
|
|
func (suite *TargetManagerSuite) assertChannels(expected []string, actual map[string]*DmChannel) bool {
|
2022-09-15 18:48:32 +08:00
|
|
|
if !suite.Equal(len(expected), len(actual)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
set := typeutil.NewSet(expected...)
|
|
|
|
for _, channel := range actual {
|
|
|
|
set.Remove(channel.ChannelName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return suite.Len(set, 0)
|
|
|
|
}
|
|
|
|
|
2022-11-07 19:37:04 +08:00
|
|
|
func (suite *TargetManagerSuite) assertSegments(expected []int64, actual map[int64]*datapb.SegmentInfo) bool {
|
2022-09-15 18:48:32 +08:00
|
|
|
if !suite.Equal(len(expected), len(actual)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
set := typeutil.NewUniqueSet(expected...)
|
|
|
|
for _, segment := range actual {
|
|
|
|
set.Remove(segment.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return suite.Len(set, 0)
|
|
|
|
}
|
|
|
|
|
2023-06-27 11:48:45 +08:00
|
|
|
func (suite *TargetManagerSuite) TestGetCollectionTargetVersion() {
|
|
|
|
t1 := time.Now().UnixNano()
|
2024-07-01 17:40:07 +08:00
|
|
|
target := NewCollectionTarget(nil, nil, nil)
|
2023-06-27 11:48:45 +08:00
|
|
|
t2 := time.Now().UnixNano()
|
|
|
|
|
|
|
|
version := target.GetTargetVersion()
|
|
|
|
suite.True(t1 <= version)
|
|
|
|
suite.True(t2 >= version)
|
|
|
|
|
2023-08-04 10:31:06 +08:00
|
|
|
collectionID := suite.collections[0]
|
2023-06-27 11:48:45 +08:00
|
|
|
t3 := time.Now().UnixNano()
|
2023-08-04 10:31:06 +08:00
|
|
|
suite.mgr.UpdateCollectionNextTarget(collectionID)
|
2023-06-27 11:48:45 +08:00
|
|
|
t4 := time.Now().UnixNano()
|
|
|
|
|
|
|
|
collectionVersion := suite.mgr.GetCollectionTargetVersion(collectionID, NextTarget)
|
|
|
|
suite.True(t3 <= collectionVersion)
|
|
|
|
suite.True(t4 >= collectionVersion)
|
|
|
|
}
|
|
|
|
|
2023-07-06 10:30:26 +08:00
|
|
|
func (suite *TargetManagerSuite) TestGetSegmentByChannel() {
|
|
|
|
collectionID := int64(1003)
|
2023-10-25 00:44:12 +08:00
|
|
|
suite.assertSegments([]int64{}, suite.mgr.GetSealedSegmentsByCollection(collectionID, NextTarget))
|
2023-07-06 10:30:26 +08:00
|
|
|
suite.assertChannels([]string{}, suite.mgr.GetDmChannelsByCollection(collectionID, NextTarget))
|
2023-10-25 00:44:12 +08:00
|
|
|
suite.assertSegments([]int64{}, suite.mgr.GetSealedSegmentsByCollection(collectionID, CurrentTarget))
|
2023-07-06 10:30:26 +08:00
|
|
|
suite.assertChannels([]string{}, suite.mgr.GetDmChannelsByCollection(collectionID, CurrentTarget))
|
|
|
|
|
|
|
|
suite.meta.PutCollection(&Collection{
|
|
|
|
CollectionLoadInfo: &querypb.CollectionLoadInfo{
|
|
|
|
CollectionID: collectionID,
|
2023-09-21 09:45:27 +08:00
|
|
|
ReplicaNumber: 1,
|
|
|
|
},
|
2023-07-06 10:30:26 +08:00
|
|
|
})
|
|
|
|
suite.meta.PutPartition(&Partition{
|
|
|
|
PartitionLoadInfo: &querypb.PartitionLoadInfo{
|
|
|
|
CollectionID: collectionID,
|
|
|
|
PartitionID: 1,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
nextTargetChannels := []*datapb.VchannelInfo{
|
|
|
|
{
|
|
|
|
CollectionID: collectionID,
|
|
|
|
ChannelName: "channel-1",
|
|
|
|
UnflushedSegmentIds: []int64{1, 2, 3, 4},
|
2023-07-24 14:09:00 +08:00
|
|
|
DroppedSegmentIds: []int64{11, 22, 33},
|
2023-07-06 10:30:26 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
CollectionID: collectionID,
|
|
|
|
ChannelName: "channel-2",
|
|
|
|
UnflushedSegmentIds: []int64{5},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
nextTargetSegments := []*datapb.SegmentInfo{
|
|
|
|
{
|
|
|
|
ID: 11,
|
|
|
|
PartitionID: 1,
|
|
|
|
InsertChannel: "channel-1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 12,
|
|
|
|
PartitionID: 1,
|
|
|
|
InsertChannel: "channel-2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
suite.broker.EXPECT().GetRecoveryInfoV2(mock.Anything, collectionID).Return(nextTargetChannels, nextTargetSegments, nil)
|
2023-08-04 10:31:06 +08:00
|
|
|
suite.mgr.UpdateCollectionNextTarget(collectionID)
|
2023-10-25 00:44:12 +08:00
|
|
|
suite.Len(suite.mgr.GetSealedSegmentsByCollection(collectionID, NextTarget), 2)
|
|
|
|
suite.Len(suite.mgr.GetSealedSegmentsByChannel(collectionID, "channel-1", NextTarget), 1)
|
|
|
|
suite.Len(suite.mgr.GetSealedSegmentsByChannel(collectionID, "channel-2", NextTarget), 1)
|
|
|
|
suite.Len(suite.mgr.GetGrowingSegmentsByChannel(collectionID, "channel-1", NextTarget), 4)
|
|
|
|
suite.Len(suite.mgr.GetGrowingSegmentsByChannel(collectionID, "channel-2", NextTarget), 1)
|
2023-07-24 14:09:00 +08:00
|
|
|
suite.Len(suite.mgr.GetDroppedSegmentsByChannel(collectionID, "channel-1", NextTarget), 3)
|
2024-03-19 11:49:05 +08:00
|
|
|
suite.Len(suite.mgr.GetGrowingSegmentsByCollection(collectionID, NextTarget), 5)
|
|
|
|
suite.Len(suite.mgr.GetSealedSegmentsByPartition(collectionID, 1, NextTarget), 2)
|
|
|
|
suite.NotNil(suite.mgr.GetSealedSegment(collectionID, 11, NextTarget))
|
|
|
|
suite.NotNil(suite.mgr.GetDmChannel(collectionID, "channel-1", NextTarget))
|
2023-07-06 10:30:26 +08:00
|
|
|
}
|
|
|
|
|
2023-12-29 09:04:46 +08:00
|
|
|
func (suite *TargetManagerSuite) TestGetTarget() {
|
|
|
|
type testCase struct {
|
|
|
|
tag string
|
|
|
|
mgr *TargetManager
|
|
|
|
scope TargetScope
|
2024-03-19 11:49:05 +08:00
|
|
|
expectTarget int
|
2023-12-29 09:04:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
current := &CollectionTarget{}
|
|
|
|
next := &CollectionTarget{}
|
|
|
|
|
|
|
|
bothMgr := &TargetManager{
|
|
|
|
current: &target{
|
|
|
|
collectionTargetMap: map[int64]*CollectionTarget{
|
|
|
|
1000: current,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
next: &target{
|
|
|
|
collectionTargetMap: map[int64]*CollectionTarget{
|
2024-03-19 11:49:05 +08:00
|
|
|
1000: next,
|
2023-12-29 09:04:46 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
currentMgr := &TargetManager{
|
|
|
|
current: &target{
|
|
|
|
collectionTargetMap: map[int64]*CollectionTarget{
|
|
|
|
1000: current,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
next: &target{},
|
|
|
|
}
|
|
|
|
nextMgr := &TargetManager{
|
|
|
|
next: &target{
|
|
|
|
collectionTargetMap: map[int64]*CollectionTarget{
|
|
|
|
1000: current,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
current: &target{},
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []testCase{
|
|
|
|
{
|
2024-03-19 11:49:05 +08:00
|
|
|
tag: "both_scope_unknown",
|
|
|
|
mgr: bothMgr,
|
|
|
|
scope: -1,
|
|
|
|
|
|
|
|
expectTarget: 0,
|
2023-12-29 09:04:46 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: "both_scope_current",
|
|
|
|
mgr: bothMgr,
|
|
|
|
scope: CurrentTarget,
|
2024-03-19 11:49:05 +08:00
|
|
|
expectTarget: 1,
|
2023-12-29 09:04:46 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: "both_scope_next",
|
|
|
|
mgr: bothMgr,
|
|
|
|
scope: NextTarget,
|
2024-03-19 11:49:05 +08:00
|
|
|
expectTarget: 1,
|
2023-12-29 09:04:46 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: "both_scope_current_first",
|
|
|
|
mgr: bothMgr,
|
|
|
|
scope: CurrentTargetFirst,
|
2024-03-19 11:49:05 +08:00
|
|
|
expectTarget: 2,
|
2023-12-29 09:04:46 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: "both_scope_next_first",
|
|
|
|
mgr: bothMgr,
|
|
|
|
scope: NextTargetFirst,
|
2024-03-19 11:49:05 +08:00
|
|
|
expectTarget: 2,
|
2023-12-29 09:04:46 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: "next_scope_current",
|
|
|
|
mgr: nextMgr,
|
|
|
|
scope: CurrentTarget,
|
2024-03-19 11:49:05 +08:00
|
|
|
expectTarget: 0,
|
2023-12-29 09:04:46 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: "next_scope_next",
|
|
|
|
mgr: nextMgr,
|
|
|
|
scope: NextTarget,
|
2024-03-19 11:49:05 +08:00
|
|
|
expectTarget: 1,
|
2023-12-29 09:04:46 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: "next_scope_current_first",
|
|
|
|
mgr: nextMgr,
|
|
|
|
scope: CurrentTargetFirst,
|
2024-03-19 11:49:05 +08:00
|
|
|
expectTarget: 1,
|
2023-12-29 09:04:46 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: "next_scope_next_first",
|
|
|
|
mgr: nextMgr,
|
|
|
|
scope: NextTargetFirst,
|
2024-03-19 11:49:05 +08:00
|
|
|
expectTarget: 1,
|
2023-12-29 09:04:46 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: "current_scope_current",
|
|
|
|
mgr: currentMgr,
|
|
|
|
scope: CurrentTarget,
|
2024-03-19 11:49:05 +08:00
|
|
|
expectTarget: 1,
|
2023-12-29 09:04:46 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: "current_scope_next",
|
|
|
|
mgr: currentMgr,
|
|
|
|
scope: NextTarget,
|
2024-03-19 11:49:05 +08:00
|
|
|
expectTarget: 0,
|
2023-12-29 09:04:46 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: "current_scope_current_first",
|
|
|
|
mgr: currentMgr,
|
|
|
|
scope: CurrentTargetFirst,
|
2024-03-19 11:49:05 +08:00
|
|
|
expectTarget: 1,
|
2023-12-29 09:04:46 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: "current_scope_next_first",
|
|
|
|
mgr: currentMgr,
|
|
|
|
scope: NextTargetFirst,
|
2024-03-19 11:49:05 +08:00
|
|
|
expectTarget: 1,
|
2023-12-29 09:04:46 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
suite.Run(tc.tag, func() {
|
2024-03-19 11:49:05 +08:00
|
|
|
targets := tc.mgr.getCollectionTarget(tc.scope, 1000)
|
|
|
|
suite.Equal(tc.expectTarget, len(targets))
|
2023-12-29 09:04:46 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-15 14:19:03 +08:00
|
|
|
func (suite *TargetManagerSuite) TestRecover() {
|
|
|
|
collectionID := int64(1003)
|
|
|
|
suite.assertSegments([]int64{}, suite.mgr.GetSealedSegmentsByCollection(collectionID, NextTarget))
|
|
|
|
suite.assertChannels([]string{}, suite.mgr.GetDmChannelsByCollection(collectionID, NextTarget))
|
|
|
|
suite.assertSegments([]int64{}, suite.mgr.GetSealedSegmentsByCollection(collectionID, CurrentTarget))
|
|
|
|
suite.assertChannels([]string{}, suite.mgr.GetDmChannelsByCollection(collectionID, CurrentTarget))
|
|
|
|
|
|
|
|
suite.meta.PutCollection(&Collection{
|
|
|
|
CollectionLoadInfo: &querypb.CollectionLoadInfo{
|
|
|
|
CollectionID: collectionID,
|
|
|
|
ReplicaNumber: 1,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
suite.meta.PutPartition(&Partition{
|
|
|
|
PartitionLoadInfo: &querypb.PartitionLoadInfo{
|
|
|
|
CollectionID: collectionID,
|
|
|
|
PartitionID: 1,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
nextTargetChannels := []*datapb.VchannelInfo{
|
|
|
|
{
|
|
|
|
CollectionID: collectionID,
|
|
|
|
ChannelName: "channel-1",
|
|
|
|
UnflushedSegmentIds: []int64{1, 2, 3, 4},
|
|
|
|
DroppedSegmentIds: []int64{11, 22, 33},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CollectionID: collectionID,
|
|
|
|
ChannelName: "channel-2",
|
|
|
|
UnflushedSegmentIds: []int64{5},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
nextTargetSegments := []*datapb.SegmentInfo{
|
|
|
|
{
|
|
|
|
ID: 11,
|
|
|
|
PartitionID: 1,
|
|
|
|
InsertChannel: "channel-1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 12,
|
|
|
|
PartitionID: 1,
|
|
|
|
InsertChannel: "channel-2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
suite.broker.EXPECT().GetRecoveryInfoV2(mock.Anything, collectionID).Return(nextTargetChannels, nextTargetSegments, nil)
|
|
|
|
suite.mgr.UpdateCollectionNextTarget(collectionID)
|
|
|
|
suite.mgr.UpdateCollectionCurrentTarget(collectionID)
|
|
|
|
|
|
|
|
suite.mgr.SaveCurrentTarget(suite.catalog)
|
|
|
|
|
|
|
|
// clear target in memory
|
2024-08-02 14:46:13 +08:00
|
|
|
version := suite.mgr.current.getCollectionTarget(collectionID).GetTargetVersion()
|
2024-03-15 14:19:03 +08:00
|
|
|
suite.mgr.current.removeCollectionTarget(collectionID)
|
|
|
|
// try to recover
|
|
|
|
suite.mgr.Recover(suite.catalog)
|
|
|
|
|
|
|
|
target := suite.mgr.current.getCollectionTarget(collectionID)
|
|
|
|
suite.NotNil(target)
|
|
|
|
suite.Len(target.GetAllDmChannelNames(), 2)
|
|
|
|
suite.Len(target.GetAllSegmentIDs(), 2)
|
2024-08-02 14:46:13 +08:00
|
|
|
suite.Equal(target.GetTargetVersion(), version)
|
2024-03-15 14:19:03 +08:00
|
|
|
|
|
|
|
// after recover, target info should be cleaned up
|
|
|
|
targets, err := suite.catalog.GetCollectionTargets()
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.Len(targets, 0)
|
|
|
|
}
|
|
|
|
|
2022-09-15 18:48:32 +08:00
|
|
|
func TestTargetManager(t *testing.T) {
|
|
|
|
suite.Run(t, new(TargetManagerSuite))
|
|
|
|
}
|