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"
|
2022-10-19 12:13:28 +08:00
|
|
|
"sync"
|
2022-09-15 18:48:32 +08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/querypb"
|
|
|
|
"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-01-18 16:41:44 +08:00
|
|
|
"go.uber.org/zap"
|
2022-09-15 18:48:32 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type CollectionObserver struct {
|
|
|
|
stopCh chan struct{}
|
|
|
|
|
2023-03-20 14:55:57 +08:00
|
|
|
dist *meta.DistributionManager
|
|
|
|
meta *meta.Meta
|
|
|
|
targetMgr *meta.TargetManager
|
|
|
|
targetObserver *TargetObserver
|
|
|
|
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,
|
2022-09-15 18:48:32 +08:00
|
|
|
) *CollectionObserver {
|
|
|
|
return &CollectionObserver{
|
2023-03-20 14:55:57 +08:00
|
|
|
stopCh: make(chan struct{}),
|
|
|
|
dist: dist,
|
|
|
|
meta: meta,
|
|
|
|
targetMgr: targetMgr,
|
|
|
|
targetObserver: targetObserver,
|
|
|
|
partitionLoadedCount: make(map[int64]int),
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ob *CollectionObserver) Start(ctx context.Context) {
|
|
|
|
const observePeriod = time.Second
|
|
|
|
go func() {
|
|
|
|
ticker := time.NewTicker(observePeriod)
|
|
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
log.Info("CollectionObserver stopped due to context canceled")
|
|
|
|
return
|
|
|
|
|
|
|
|
case <-ob.stopCh:
|
|
|
|
log.Info("CollectionObserver stopped")
|
|
|
|
return
|
|
|
|
|
|
|
|
case <-ticker.C:
|
|
|
|
ob.Observe()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ob *CollectionObserver) Stop() {
|
2022-10-19 12:13:28 +08:00
|
|
|
ob.stopOnce.Do(func() {
|
|
|
|
close(ob.stopCh)
|
|
|
|
})
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ob *CollectionObserver) Observe() {
|
|
|
|
ob.observeTimeout()
|
|
|
|
ob.observeLoadStatus()
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
partitions := utils.GroupPartitionsByCollection(
|
|
|
|
ob.meta.CollectionManager.GetAllPartitions())
|
|
|
|
if len(partitions) > 0 {
|
|
|
|
log.Info("observes partitions timeout", zap.Int("partitionNum", len(partitions)))
|
|
|
|
}
|
|
|
|
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-03-20 14:55:57 +08:00
|
|
|
ob.meta.CollectionManager.RemovePartition(partition.GetPartitionID())
|
|
|
|
ob.targetMgr.RemovePartition(partition.GetCollectionID(), partition.GetPartitionID())
|
2022-09-15 18:48:32 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ob *CollectionObserver) observeLoadStatus() {
|
|
|
|
partitions := ob.meta.CollectionManager.GetAllPartitions()
|
|
|
|
if len(partitions) > 0 {
|
|
|
|
log.Info("observe partitions status", zap.Int("partitionNum", len(partitions)))
|
|
|
|
}
|
|
|
|
for _, partition := range partitions {
|
|
|
|
if partition.LoadPercentage == 100 {
|
|
|
|
continue
|
|
|
|
}
|
2023-03-20 14:55:57 +08:00
|
|
|
replicaNum := ob.meta.GetReplicaNumber(partition.GetCollectionID())
|
|
|
|
ob.observePartitionLoadStatus(partition, replicaNum)
|
|
|
|
}
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
|
|
|
|
2023-03-20 14:55:57 +08:00
|
|
|
func (ob *CollectionObserver) observePartitionLoadStatus(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()),
|
|
|
|
)
|
|
|
|
|
2022-11-07 19:37:04 +08:00
|
|
|
segmentTargets := ob.targetMgr.GetHistoricalSegmentsByPartition(partition.GetCollectionID(), partition.GetPartitionID(), meta.NextTarget)
|
|
|
|
channelTargets := ob.targetMgr.GetDmChannelsByCollection(partition.GetCollectionID(), meta.NextTarget)
|
2022-09-15 18:48:32 +08:00
|
|
|
targetNum := len(segmentTargets) + len(channelTargets)
|
|
|
|
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)
|
2022-11-07 19:37:04 +08:00
|
|
|
if targetNum == 0 {
|
|
|
|
log.Info("No segment/channel in target need to be loaded!")
|
2023-03-30 10:48:23 +08:00
|
|
|
loadPercentage = 100
|
2022-11-07 19:37:04 +08:00
|
|
|
} else {
|
|
|
|
for _, channel := range channelTargets {
|
|
|
|
group := utils.GroupNodesByReplica(ob.meta.ReplicaManager,
|
|
|
|
partition.GetCollectionID(),
|
|
|
|
ob.dist.LeaderViewManager.GetChannelDist(channel.GetChannelName()))
|
2022-12-06 18:29:19 +08:00
|
|
|
loadedCount += len(group)
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
2022-11-07 19:37:04 +08:00
|
|
|
subChannelCount := loadedCount
|
|
|
|
for _, segment := range segmentTargets {
|
|
|
|
group := utils.GroupNodesByReplica(ob.meta.ReplicaManager,
|
|
|
|
partition.GetCollectionID(),
|
|
|
|
ob.dist.LeaderViewManager.GetSealedSegmentDist(segment.GetID()))
|
2022-12-06 18:29:19 +08:00
|
|
|
loadedCount += len(group)
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
2022-11-07 19:37:04 +08:00
|
|
|
if loadedCount > 0 {
|
|
|
|
log.Info("partition load progress",
|
2022-11-15 10:11:07 +08:00
|
|
|
zap.Int("subChannelCount", subChannelCount),
|
|
|
|
zap.Int("loadSegmentCount", loadedCount-subChannelCount))
|
2022-11-07 19:37:04 +08:00
|
|
|
}
|
2023-03-30 10:48:23 +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-03-30 10:48:23 +08:00
|
|
|
if loadPercentage == 100 && ob.targetObserver.Check(partition.GetCollectionID()) {
|
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),
|
|
|
|
)
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|