2022-11-07 19:37:04 +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 observers
|
|
|
|
|
|
|
|
import (
|
2023-10-09 11:07:33 +08:00
|
|
|
"context"
|
2022-11-07 19:37:04 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-09-05 18:47:03 +08:00
|
|
|
"github.com/samber/lo"
|
2022-11-07 19:37:04 +08:00
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
|
|
|
etcdkv "github.com/milvus-io/milvus/internal/kv/etcd"
|
2023-07-31 13:57:04 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/metastore/kv/querycoord"
|
2022-11-07 19:37:04 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/datapb"
|
2022-12-12 15:15:22 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/querypb"
|
2022-11-07 19:37:04 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/querycoordv2/meta"
|
|
|
|
. "github.com/milvus-io/milvus/internal/querycoordv2/params"
|
2023-01-30 10:19:48 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/querycoordv2/session"
|
2022-11-07 19:37:04 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/querycoordv2/utils"
|
2024-07-01 17:40:07 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/common"
|
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"
|
|
|
|
"github.com/milvus-io/milvus/pkg/util/paramtable"
|
2022-11-07 19:37:04 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type TargetObserverSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
2023-06-13 10:52:38 +08:00
|
|
|
kv kv.MetaKv
|
2023-09-21 09:45:27 +08:00
|
|
|
// dependency
|
2022-11-07 19:37:04 +08:00
|
|
|
meta *meta.Meta
|
|
|
|
targetMgr *meta.TargetManager
|
|
|
|
distMgr *meta.DistributionManager
|
|
|
|
broker *meta.MockBroker
|
2023-11-08 11:36:22 +08:00
|
|
|
cluster *session.MockCluster
|
2022-11-07 19:37:04 +08:00
|
|
|
|
|
|
|
observer *TargetObserver
|
|
|
|
|
|
|
|
collectionID int64
|
|
|
|
partitionID int64
|
2023-04-18 18:30:32 +08:00
|
|
|
nextTargetSegments []*datapb.SegmentInfo
|
2022-11-07 19:37:04 +08:00
|
|
|
nextTargetChannels []*datapb.VchannelInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *TargetObserverSuite) SetupSuite() {
|
2022-12-07 18:01:19 +08:00
|
|
|
paramtable.Init()
|
|
|
|
paramtable.Get().Save(Params.QueryCoordCfg.UpdateNextTargetInterval.Key, "3")
|
2022-11-07 19:37:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *TargetObserverSuite) SetupTest() {
|
|
|
|
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
|
2023-07-31 13:57:04 +08:00
|
|
|
store := querycoord.NewCatalog(suite.kv)
|
2022-11-07 19:37:04 +08:00
|
|
|
idAllocator := RandomIncrementIDAllocator()
|
2023-01-30 10:19:48 +08:00
|
|
|
suite.meta = meta.NewMeta(idAllocator, store, session.NewNodeManager())
|
2022-11-07 19:37:04 +08:00
|
|
|
|
|
|
|
suite.broker = meta.NewMockBroker(suite.T())
|
|
|
|
suite.targetMgr = meta.NewTargetManager(suite.broker, suite.meta)
|
|
|
|
suite.distMgr = meta.NewDistributionManager()
|
2023-11-08 11:36:22 +08:00
|
|
|
suite.cluster = session.NewMockCluster(suite.T())
|
|
|
|
suite.observer = NewTargetObserver(suite.meta, suite.targetMgr, suite.distMgr, suite.broker, suite.cluster)
|
2022-11-07 19:37:04 +08:00
|
|
|
suite.collectionID = int64(1000)
|
|
|
|
suite.partitionID = int64(100)
|
|
|
|
|
|
|
|
err = suite.meta.CollectionManager.PutCollection(utils.CreateTestCollection(suite.collectionID, 1))
|
|
|
|
suite.NoError(err)
|
2023-03-20 14:55:57 +08:00
|
|
|
err = suite.meta.CollectionManager.PutPartition(utils.CreateTestPartition(suite.collectionID, suite.partitionID))
|
|
|
|
suite.NoError(err)
|
2024-05-10 17:27:31 +08:00
|
|
|
replicas, err := suite.meta.ReplicaManager.Spawn(suite.collectionID, map[string]int{meta.DefaultResourceGroupName: 1}, nil)
|
2022-12-12 15:15:22 +08:00
|
|
|
suite.NoError(err)
|
2024-04-05 04:57:16 +08:00
|
|
|
replicas[0].AddRWNode(2)
|
2022-12-12 15:15:22 +08:00
|
|
|
err = suite.meta.ReplicaManager.Put(replicas...)
|
2022-11-07 19:37:04 +08:00
|
|
|
suite.NoError(err)
|
|
|
|
|
|
|
|
suite.nextTargetChannels = []*datapb.VchannelInfo{
|
|
|
|
{
|
|
|
|
CollectionID: suite.collectionID,
|
|
|
|
ChannelName: "channel-1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CollectionID: suite.collectionID,
|
|
|
|
ChannelName: "channel-2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-04-18 18:30:32 +08:00
|
|
|
suite.nextTargetSegments = []*datapb.SegmentInfo{
|
2022-11-07 19:37:04 +08:00
|
|
|
{
|
2023-04-18 18:30:32 +08:00
|
|
|
ID: 11,
|
|
|
|
PartitionID: suite.partitionID,
|
2022-11-07 19:37:04 +08:00
|
|
|
InsertChannel: "channel-1",
|
|
|
|
},
|
|
|
|
{
|
2023-04-18 18:30:32 +08:00
|
|
|
ID: 12,
|
|
|
|
PartitionID: suite.partitionID,
|
2022-11-07 19:37:04 +08:00
|
|
|
InsertChannel: "channel-2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-04-18 18:30:32 +08:00
|
|
|
suite.broker.EXPECT().GetRecoveryInfoV2(mock.Anything, mock.Anything).Return(suite.nextTargetChannels, suite.nextTargetSegments, nil)
|
2023-10-19 21:52:10 +08:00
|
|
|
suite.observer.Start()
|
2022-11-07 19:37:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *TargetObserverSuite) TestTriggerUpdateTarget() {
|
|
|
|
suite.Eventually(func() bool {
|
2023-10-25 00:44:12 +08:00
|
|
|
return len(suite.targetMgr.GetSealedSegmentsByCollection(suite.collectionID, meta.NextTarget)) == 2 &&
|
2022-12-12 15:15:22 +08:00
|
|
|
len(suite.targetMgr.GetDmChannelsByCollection(suite.collectionID, meta.NextTarget)) == 2
|
2022-11-07 19:37:04 +08:00
|
|
|
}, 5*time.Second, 1*time.Second)
|
|
|
|
|
2022-12-12 15:15:22 +08:00
|
|
|
suite.distMgr.LeaderViewManager.Update(2,
|
|
|
|
&meta.LeaderView{
|
|
|
|
ID: 2,
|
|
|
|
CollectionID: suite.collectionID,
|
|
|
|
Channel: "channel-1",
|
|
|
|
Segments: map[int64]*querypb.SegmentDist{
|
|
|
|
11: {NodeID: 2},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&meta.LeaderView{
|
|
|
|
ID: 2,
|
|
|
|
CollectionID: suite.collectionID,
|
|
|
|
Channel: "channel-2",
|
|
|
|
Segments: map[int64]*querypb.SegmentDist{
|
|
|
|
12: {NodeID: 2},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2022-11-07 19:37:04 +08:00
|
|
|
|
2022-12-12 15:15:22 +08:00
|
|
|
// Never update current target if it's empty, even the next target is ready
|
|
|
|
suite.Eventually(func() bool {
|
|
|
|
return len(suite.targetMgr.GetDmChannelsByCollection(suite.collectionID, meta.CurrentTarget)) == 0
|
|
|
|
}, 3*time.Second, 1*time.Second)
|
|
|
|
|
|
|
|
suite.broker.AssertExpectations(suite.T())
|
|
|
|
suite.broker.ExpectedCalls = suite.broker.ExpectedCalls[:0]
|
2023-04-18 18:30:32 +08:00
|
|
|
suite.nextTargetSegments = append(suite.nextTargetSegments, &datapb.SegmentInfo{
|
|
|
|
ID: 13,
|
|
|
|
PartitionID: suite.partitionID,
|
2022-12-12 15:15:22 +08:00
|
|
|
InsertChannel: "channel-1",
|
|
|
|
})
|
|
|
|
suite.targetMgr.UpdateCollectionCurrentTarget(suite.collectionID)
|
2022-11-07 19:37:04 +08:00
|
|
|
|
2022-12-12 15:15:22 +08:00
|
|
|
// Pull next again
|
2023-01-13 17:11:41 +08:00
|
|
|
suite.broker.EXPECT().
|
2023-04-18 18:30:32 +08:00
|
|
|
GetRecoveryInfoV2(mock.Anything, mock.Anything).
|
2023-01-13 17:11:41 +08:00
|
|
|
Return(suite.nextTargetChannels, suite.nextTargetSegments, nil)
|
2022-11-07 19:37:04 +08:00
|
|
|
suite.Eventually(func() bool {
|
2023-10-25 00:44:12 +08:00
|
|
|
return len(suite.targetMgr.GetSealedSegmentsByCollection(suite.collectionID, meta.NextTarget)) == 3 &&
|
2022-12-12 15:15:22 +08:00
|
|
|
len(suite.targetMgr.GetDmChannelsByCollection(suite.collectionID, meta.NextTarget)) == 2
|
|
|
|
}, 7*time.Second, 1*time.Second)
|
2023-01-13 17:11:41 +08:00
|
|
|
suite.broker.AssertExpectations(suite.T())
|
|
|
|
|
|
|
|
// Manually update next target
|
|
|
|
ready, err := suite.observer.UpdateNextTarget(suite.collectionID)
|
|
|
|
suite.NoError(err)
|
2022-11-07 19:37:04 +08:00
|
|
|
|
2022-12-12 15:15:22 +08:00
|
|
|
suite.distMgr.LeaderViewManager.Update(2,
|
|
|
|
&meta.LeaderView{
|
|
|
|
ID: 2,
|
|
|
|
CollectionID: suite.collectionID,
|
|
|
|
Channel: "channel-1",
|
|
|
|
Segments: map[int64]*querypb.SegmentDist{
|
|
|
|
11: {NodeID: 2},
|
|
|
|
13: {NodeID: 2},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&meta.LeaderView{
|
|
|
|
ID: 2,
|
|
|
|
CollectionID: suite.collectionID,
|
|
|
|
Channel: "channel-2",
|
|
|
|
Segments: map[int64]*querypb.SegmentDist{
|
|
|
|
12: {NodeID: 2},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// Able to update current if it's not empty
|
2022-11-07 19:37:04 +08:00
|
|
|
suite.Eventually(func() bool {
|
2023-01-13 17:11:41 +08:00
|
|
|
isReady := false
|
|
|
|
select {
|
|
|
|
case <-ready:
|
|
|
|
isReady = true
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return isReady &&
|
2023-10-25 00:44:12 +08:00
|
|
|
len(suite.targetMgr.GetSealedSegmentsByCollection(suite.collectionID, meta.CurrentTarget)) == 3 &&
|
2022-12-12 15:15:22 +08:00
|
|
|
len(suite.targetMgr.GetDmChannelsByCollection(suite.collectionID, meta.CurrentTarget)) == 2
|
|
|
|
}, 7*time.Second, 1*time.Second)
|
2022-11-07 19:37:04 +08:00
|
|
|
}
|
|
|
|
|
2024-09-05 18:47:03 +08:00
|
|
|
func (suite *TargetObserverSuite) TestTriggerRelease() {
|
|
|
|
// Manually update next target
|
|
|
|
_, err := suite.observer.UpdateNextTarget(suite.collectionID)
|
|
|
|
suite.NoError(err)
|
|
|
|
|
|
|
|
// manually release partition
|
|
|
|
partitions := suite.meta.CollectionManager.GetPartitionsByCollection(suite.collectionID)
|
|
|
|
partitionIDs := lo.Map(partitions, func(partition *meta.Partition, _ int) int64 { return partition.PartitionID })
|
|
|
|
suite.observer.ReleasePartition(suite.collectionID, partitionIDs[0])
|
|
|
|
|
|
|
|
// manually release collection
|
|
|
|
suite.observer.ReleaseCollection(suite.collectionID)
|
|
|
|
}
|
|
|
|
|
2023-12-04 18:14:34 +08:00
|
|
|
func (suite *TargetObserverSuite) TearDownTest() {
|
2022-11-07 19:37:04 +08:00
|
|
|
suite.kv.Close()
|
|
|
|
suite.observer.Stop()
|
|
|
|
}
|
|
|
|
|
2023-10-09 11:07:33 +08:00
|
|
|
type TargetObserverCheckSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
kv kv.MetaKv
|
|
|
|
// dependency
|
|
|
|
meta *meta.Meta
|
|
|
|
targetMgr *meta.TargetManager
|
|
|
|
distMgr *meta.DistributionManager
|
|
|
|
broker *meta.MockBroker
|
2023-11-08 11:36:22 +08:00
|
|
|
cluster *session.MockCluster
|
2023-10-09 11:07:33 +08:00
|
|
|
|
|
|
|
observer *TargetObserver
|
|
|
|
|
|
|
|
collectionID int64
|
|
|
|
partitionID int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *TargetObserverCheckSuite) SetupSuite() {
|
|
|
|
paramtable.Init()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *TargetObserverCheckSuite) SetupTest() {
|
|
|
|
var err error
|
|
|
|
config := GenerateEtcdConfig()
|
|
|
|
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())
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.kv = etcdkv.NewEtcdKV(cli, config.MetaRootPath.GetValue())
|
|
|
|
|
|
|
|
// meta
|
|
|
|
store := querycoord.NewCatalog(suite.kv)
|
|
|
|
idAllocator := RandomIncrementIDAllocator()
|
|
|
|
suite.meta = meta.NewMeta(idAllocator, store, session.NewNodeManager())
|
|
|
|
|
|
|
|
suite.broker = meta.NewMockBroker(suite.T())
|
|
|
|
suite.targetMgr = meta.NewTargetManager(suite.broker, suite.meta)
|
|
|
|
suite.distMgr = meta.NewDistributionManager()
|
2023-11-08 11:36:22 +08:00
|
|
|
suite.cluster = session.NewMockCluster(suite.T())
|
|
|
|
suite.observer = NewTargetObserver(
|
|
|
|
suite.meta,
|
|
|
|
suite.targetMgr,
|
|
|
|
suite.distMgr,
|
|
|
|
suite.broker,
|
|
|
|
suite.cluster,
|
|
|
|
)
|
2023-10-09 11:07:33 +08:00
|
|
|
suite.collectionID = int64(1000)
|
|
|
|
suite.partitionID = int64(100)
|
|
|
|
|
|
|
|
err = suite.meta.CollectionManager.PutCollection(utils.CreateTestCollection(suite.collectionID, 1))
|
|
|
|
suite.NoError(err)
|
|
|
|
err = suite.meta.CollectionManager.PutPartition(utils.CreateTestPartition(suite.collectionID, suite.partitionID))
|
|
|
|
suite.NoError(err)
|
2024-05-10 17:27:31 +08:00
|
|
|
replicas, err := suite.meta.ReplicaManager.Spawn(suite.collectionID, map[string]int{meta.DefaultResourceGroupName: 1}, nil)
|
2023-10-09 11:07:33 +08:00
|
|
|
suite.NoError(err)
|
2024-04-05 04:57:16 +08:00
|
|
|
replicas[0].AddRWNode(2)
|
2023-10-09 11:07:33 +08:00
|
|
|
err = suite.meta.ReplicaManager.Put(replicas...)
|
|
|
|
suite.NoError(err)
|
|
|
|
}
|
|
|
|
|
2023-10-24 10:14:11 +08:00
|
|
|
func (s *TargetObserverCheckSuite) TestCheck() {
|
2024-07-01 17:40:07 +08:00
|
|
|
r := s.observer.Check(context.Background(), s.collectionID, common.AllPartitionsID)
|
2023-10-24 10:14:11 +08:00
|
|
|
s.False(r)
|
|
|
|
s.True(s.observer.dispatcher.tasks.Contain(s.collectionID))
|
2023-10-09 11:07:33 +08:00
|
|
|
}
|
|
|
|
|
2023-01-30 10:19:48 +08:00
|
|
|
func TestTargetObserver(t *testing.T) {
|
2022-11-07 19:37:04 +08:00
|
|
|
suite.Run(t, new(TargetObserverSuite))
|
2023-10-09 11:07:33 +08:00
|
|
|
suite.Run(t, new(TargetObserverCheckSuite))
|
2022-11-07 19:37:04 +08:00
|
|
|
}
|