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 observers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-07-25 17:23:01 +08:00
|
|
|
"fmt"
|
2022-10-19 12:13:28 +08:00
|
|
|
"sync"
|
2022-09-15 18:48:32 +08:00
|
|
|
"time"
|
|
|
|
|
2023-04-06 19:14:32 +08:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2022-09-15 18:48:32 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/querypb"
|
2023-05-08 14:06:41 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/querycoordv2/checkers"
|
2022-09-15 18:48:32 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/querycoordv2/meta"
|
|
|
|
. "github.com/milvus-io/milvus/internal/querycoordv2/params"
|
|
|
|
"github.com/milvus-io/milvus/internal/querycoordv2/utils"
|
2023-07-25 17:23:01 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/eventlog"
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/log"
|
2022-09-15 18:48:32 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type CollectionObserver struct {
|
2023-09-27 16:27:27 +08:00
|
|
|
cancel context.CancelFunc
|
|
|
|
wg sync.WaitGroup
|
2022-09-15 18:48:32 +08:00
|
|
|
|
2023-03-20 14:55:57 +08:00
|
|
|
dist *meta.DistributionManager
|
|
|
|
meta *meta.Meta
|
|
|
|
targetMgr *meta.TargetManager
|
|
|
|
targetObserver *TargetObserver
|
2023-06-27 11:48:45 +08:00
|
|
|
leaderObserver *LeaderObserver
|
2023-05-08 14:06:41 +08:00
|
|
|
checkerController *checkers.CheckerController
|
2023-03-20 14:55:57 +08:00
|
|
|
partitionLoadedCount map[int64]int
|
2022-10-19 12:13:28 +08:00
|
|
|
|
|
|
|
stopOnce sync.Once
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewCollectionObserver(
|
|
|
|
dist *meta.DistributionManager,
|
|
|
|
meta *meta.Meta,
|
|
|
|
targetMgr *meta.TargetManager,
|
2023-01-17 11:41:51 +08:00
|
|
|
targetObserver *TargetObserver,
|
2023-06-27 11:48:45 +08:00
|
|
|
leaderObserver *LeaderObserver,
|
2023-05-08 14:06:41 +08:00
|
|
|
checherController *checkers.CheckerController,
|
2022-09-15 18:48:32 +08:00
|
|
|
) *CollectionObserver {
|
|
|
|
return &CollectionObserver{
|
2023-03-20 14:55:57 +08:00
|
|
|
dist: dist,
|
|
|
|
meta: meta,
|
|
|
|
targetMgr: targetMgr,
|
|
|
|
targetObserver: targetObserver,
|
2023-06-27 11:48:45 +08:00
|
|
|
leaderObserver: leaderObserver,
|
2023-05-08 14:06:41 +08:00
|
|
|
checkerController: checherController,
|
2023-03-20 14:55:57 +08:00
|
|
|
partitionLoadedCount: make(map[int64]int),
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-27 16:27:27 +08:00
|
|
|
func (ob *CollectionObserver) Start() {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
ob.cancel = cancel
|
|
|
|
|
2022-09-15 18:48:32 +08:00
|
|
|
const observePeriod = time.Second
|
2023-09-27 16:27:27 +08:00
|
|
|
ob.wg.Add(1)
|
2022-09-15 18:48:32 +08:00
|
|
|
go func() {
|
2023-09-27 16:27:27 +08:00
|
|
|
defer ob.wg.Done()
|
|
|
|
|
2022-09-15 18:48:32 +08:00
|
|
|
ticker := time.NewTicker(observePeriod)
|
|
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
log.Info("CollectionObserver stopped")
|
|
|
|
return
|
|
|
|
|
|
|
|
case <-ticker.C:
|
2023-09-27 16:27:27 +08:00
|
|
|
ob.Observe(ctx)
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ob *CollectionObserver) Stop() {
|
2022-10-19 12:13:28 +08:00
|
|
|
ob.stopOnce.Do(func() {
|
2023-09-27 16:27:27 +08:00
|
|
|
if ob.cancel != nil {
|
|
|
|
ob.cancel()
|
|
|
|
}
|
|
|
|
ob.wg.Wait()
|
2022-10-19 12:13:28 +08:00
|
|
|
})
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
|
|
|
|
2023-09-27 16:27:27 +08:00
|
|
|
func (ob *CollectionObserver) Observe(ctx context.Context) {
|
2022-09-15 18:48:32 +08:00
|
|
|
ob.observeTimeout()
|
2023-10-09 11:07:33 +08:00
|
|
|
ob.observeLoadStatus(ctx)
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ob *CollectionObserver) observeTimeout() {
|
|
|
|
collections := ob.meta.CollectionManager.GetAllCollections()
|
|
|
|
for _, collection := range collections {
|
|
|
|
if collection.GetStatus() != querypb.LoadStatus_Loading ||
|
2022-12-07 18:01:19 +08:00
|
|
|
time.Now().Before(collection.UpdatedAt.Add(Params.QueryCoordCfg.LoadTimeoutSeconds.GetAsDuration(time.Second))) {
|
2022-09-15 18:48:32 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("load collection timeout, cancel it",
|
|
|
|
zap.Int64("collectionID", collection.GetCollectionID()),
|
|
|
|
zap.Duration("loadTime", time.Since(collection.CreatedAt)))
|
|
|
|
ob.meta.CollectionManager.RemoveCollection(collection.GetCollectionID())
|
|
|
|
ob.meta.ReplicaManager.RemoveCollection(collection.GetCollectionID())
|
|
|
|
ob.targetMgr.RemoveCollection(collection.GetCollectionID())
|
|
|
|
}
|
|
|
|
|
2023-10-11 10:13:34 +08:00
|
|
|
partitions := utils.GroupPartitionsByCollection(ob.meta.CollectionManager.GetAllPartitions())
|
2022-09-15 18:48:32 +08:00
|
|
|
for collection, partitions := range partitions {
|
|
|
|
for _, partition := range partitions {
|
|
|
|
if partition.GetStatus() != querypb.LoadStatus_Loading ||
|
2023-01-18 17:29:43 +08:00
|
|
|
time.Now().Before(partition.UpdatedAt.Add(Params.QueryCoordCfg.LoadTimeoutSeconds.GetAsDuration(time.Second))) {
|
2022-09-15 18:48:32 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-03-20 14:55:57 +08:00
|
|
|
log.Info("load partition timeout, cancel it",
|
|
|
|
zap.Int64("collectionID", collection),
|
2022-09-15 18:48:32 +08:00
|
|
|
zap.Int64("partitionID", partition.GetPartitionID()),
|
|
|
|
zap.Duration("loadTime", time.Since(partition.CreatedAt)))
|
2023-09-02 00:09:01 +08:00
|
|
|
ob.meta.CollectionManager.RemovePartition(collection, partition.GetPartitionID())
|
2023-03-20 14:55:57 +08:00
|
|
|
ob.targetMgr.RemovePartition(partition.GetCollectionID(), partition.GetPartitionID())
|
2023-07-06 19:10:25 +08:00
|
|
|
}
|
|
|
|
// all partition timeout, remove collection
|
|
|
|
if len(ob.meta.CollectionManager.GetPartitionsByCollection(collection)) == 0 {
|
|
|
|
log.Info("collection timeout due to all partition removed", zap.Int64("collection", collection))
|
|
|
|
|
|
|
|
ob.meta.CollectionManager.RemoveCollection(collection)
|
|
|
|
ob.meta.ReplicaManager.RemoveCollection(collection)
|
|
|
|
ob.targetMgr.RemoveCollection(collection)
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-01 11:17:01 +08:00
|
|
|
func (ob *CollectionObserver) readyToObserve(collectionID int64) bool {
|
|
|
|
metaExist := (ob.meta.GetCollection(collectionID) != nil)
|
|
|
|
targetExist := ob.targetMgr.IsNextTargetExist(collectionID) || ob.targetMgr.IsCurrentTargetExist(collectionID)
|
|
|
|
|
|
|
|
return metaExist && targetExist
|
|
|
|
}
|
|
|
|
|
2023-10-09 11:07:33 +08:00
|
|
|
func (ob *CollectionObserver) observeLoadStatus(ctx context.Context) {
|
2022-09-15 18:48:32 +08:00
|
|
|
partitions := ob.meta.CollectionManager.GetAllPartitions()
|
|
|
|
if len(partitions) > 0 {
|
|
|
|
log.Info("observe partitions status", zap.Int("partitionNum", len(partitions)))
|
|
|
|
}
|
2023-05-08 14:06:41 +08:00
|
|
|
loading := false
|
2022-09-15 18:48:32 +08:00
|
|
|
for _, partition := range partitions {
|
|
|
|
if partition.LoadPercentage == 100 {
|
|
|
|
continue
|
|
|
|
}
|
2023-09-01 11:17:01 +08:00
|
|
|
if ob.readyToObserve(partition.CollectionID) {
|
|
|
|
replicaNum := ob.meta.GetReplicaNumber(partition.GetCollectionID())
|
2023-10-09 11:07:33 +08:00
|
|
|
ob.observePartitionLoadStatus(ctx, partition, replicaNum)
|
2023-09-01 11:17:01 +08:00
|
|
|
loading = true
|
|
|
|
}
|
2023-05-08 14:06:41 +08:00
|
|
|
}
|
|
|
|
// trigger check logic when loading collections/partitions
|
|
|
|
if loading {
|
|
|
|
ob.checkerController.Check()
|
2023-03-20 14:55:57 +08:00
|
|
|
}
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
|
|
|
|
2023-10-09 11:07:33 +08:00
|
|
|
func (ob *CollectionObserver) observePartitionLoadStatus(ctx context.Context, partition *meta.Partition, replicaNum int32) {
|
2022-09-15 18:48:32 +08:00
|
|
|
log := log.With(
|
|
|
|
zap.Int64("collectionID", partition.GetCollectionID()),
|
|
|
|
zap.Int64("partitionID", partition.GetPartitionID()),
|
|
|
|
)
|
|
|
|
|
2023-10-25 00:44:12 +08:00
|
|
|
segmentTargets := ob.targetMgr.GetSealedSegmentsByPartition(partition.GetCollectionID(), partition.GetPartitionID(), meta.NextTarget)
|
2022-11-07 19:37:04 +08:00
|
|
|
channelTargets := ob.targetMgr.GetDmChannelsByCollection(partition.GetCollectionID(), meta.NextTarget)
|
2023-08-04 10:31:06 +08:00
|
|
|
|
2022-09-15 18:48:32 +08:00
|
|
|
targetNum := len(segmentTargets) + len(channelTargets)
|
2023-08-04 10:31:06 +08:00
|
|
|
if targetNum == 0 {
|
|
|
|
log.Info("segments and channels in target are both empty, waiting for new target content")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-15 18:48:32 +08:00
|
|
|
log.Info("partition targets",
|
2022-12-06 18:29:19 +08:00
|
|
|
zap.Int("segmentTargetNum", len(segmentTargets)),
|
|
|
|
zap.Int("channelTargetNum", len(channelTargets)),
|
|
|
|
zap.Int("totalTargetNum", targetNum),
|
2023-03-20 14:55:57 +08:00
|
|
|
zap.Int32("replicaNum", replicaNum),
|
2022-12-06 18:29:19 +08:00
|
|
|
)
|
2022-09-15 18:48:32 +08:00
|
|
|
loadedCount := 0
|
2023-03-30 10:48:23 +08:00
|
|
|
loadPercentage := int32(0)
|
2023-08-04 10:31:06 +08:00
|
|
|
|
|
|
|
for _, channel := range channelTargets {
|
|
|
|
group := utils.GroupNodesByReplica(ob.meta.ReplicaManager,
|
|
|
|
partition.GetCollectionID(),
|
|
|
|
ob.dist.LeaderViewManager.GetChannelDist(channel.GetChannelName()))
|
|
|
|
loadedCount += len(group)
|
|
|
|
}
|
|
|
|
subChannelCount := loadedCount
|
|
|
|
for _, segment := range segmentTargets {
|
|
|
|
group := utils.GroupNodesByReplica(ob.meta.ReplicaManager,
|
|
|
|
partition.GetCollectionID(),
|
|
|
|
ob.dist.LeaderViewManager.GetSealedSegmentDist(segment.GetID()))
|
|
|
|
loadedCount += len(group)
|
|
|
|
}
|
|
|
|
if loadedCount > 0 {
|
|
|
|
log.Info("partition load progress",
|
|
|
|
zap.Int("subChannelCount", subChannelCount),
|
|
|
|
zap.Int("loadSegmentCount", loadedCount-subChannelCount))
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
2023-08-04 10:31:06 +08:00
|
|
|
loadPercentage = int32(loadedCount * 100 / (targetNum * int(replicaNum)))
|
2022-09-15 18:48:32 +08:00
|
|
|
|
2023-03-30 10:48:23 +08:00
|
|
|
if loadedCount <= ob.partitionLoadedCount[partition.GetPartitionID()] && loadPercentage != 100 {
|
2023-02-06 16:07:53 +08:00
|
|
|
ob.partitionLoadedCount[partition.GetPartitionID()] = loadedCount
|
2022-09-30 19:48:55 +08:00
|
|
|
return
|
|
|
|
}
|
2023-03-30 10:48:23 +08:00
|
|
|
|
2022-12-06 17:13:18 +08:00
|
|
|
ob.partitionLoadedCount[partition.GetPartitionID()] = loadedCount
|
2023-10-24 10:14:11 +08:00
|
|
|
if loadPercentage == 100 {
|
|
|
|
if !ob.targetObserver.Check(ctx, partition.GetCollectionID()) {
|
|
|
|
log.Warn("failed to manual check current target, skip update load status")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !ob.leaderObserver.CheckTargetVersion(ctx, partition.GetCollectionID()) {
|
|
|
|
log.Warn("failed to manual check leader target version ,skip update load status")
|
|
|
|
return
|
|
|
|
}
|
2022-12-12 18:43:23 +08:00
|
|
|
delete(ob.partitionLoadedCount, partition.GetPartitionID())
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
2023-03-30 10:48:23 +08:00
|
|
|
collectionPercentage, err := ob.meta.CollectionManager.UpdateLoadPercent(partition.PartitionID, loadPercentage)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("failed to update load percentage")
|
|
|
|
}
|
|
|
|
log.Info("load status updated",
|
|
|
|
zap.Int32("partitionLoadPercentage", loadPercentage),
|
|
|
|
zap.Int32("collectionLoadPercentage", collectionPercentage),
|
|
|
|
)
|
2023-07-25 17:23:01 +08:00
|
|
|
eventlog.Record(eventlog.NewRawEvt(eventlog.Level_Info, fmt.Sprintf("collection %d load percentage update: %d", partition.CollectionID, loadPercentage)))
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|