2023-03-20 14:55:57 +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 job
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2023-07-17 14:59:34 +08:00
|
|
|
"github.com/cockroachdb/errors"
|
2023-03-20 14:55:57 +08:00
|
|
|
"github.com/samber/lo"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/querypb"
|
2023-05-30 17:41:28 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/querycoordv2/checkers"
|
2023-03-20 14:55:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/querycoordv2/meta"
|
|
|
|
"github.com/milvus-io/milvus/internal/querycoordv2/observers"
|
|
|
|
"github.com/milvus-io/milvus/internal/querycoordv2/session"
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/log"
|
|
|
|
"github.com/milvus-io/milvus/pkg/metrics"
|
2023-03-20 14:55:57 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type ReleaseCollectionJob struct {
|
|
|
|
*BaseJob
|
2023-05-30 17:41:28 +08:00
|
|
|
req *querypb.ReleaseCollectionRequest
|
|
|
|
dist *meta.DistributionManager
|
|
|
|
meta *meta.Meta
|
|
|
|
broker meta.Broker
|
|
|
|
cluster session.Cluster
|
2024-08-02 17:20:15 +08:00
|
|
|
targetMgr meta.TargetManagerInterface
|
2023-05-30 17:41:28 +08:00
|
|
|
targetObserver *observers.TargetObserver
|
|
|
|
checkerController *checkers.CheckerController
|
2023-03-20 14:55:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewReleaseCollectionJob(ctx context.Context,
|
|
|
|
req *querypb.ReleaseCollectionRequest,
|
|
|
|
dist *meta.DistributionManager,
|
|
|
|
meta *meta.Meta,
|
2023-05-18 09:17:23 +08:00
|
|
|
broker meta.Broker,
|
|
|
|
cluster session.Cluster,
|
2024-08-02 17:20:15 +08:00
|
|
|
targetMgr meta.TargetManagerInterface,
|
2023-03-20 14:55:57 +08:00
|
|
|
targetObserver *observers.TargetObserver,
|
2023-05-30 17:41:28 +08:00
|
|
|
checkerController *checkers.CheckerController,
|
2023-03-20 14:55:57 +08:00
|
|
|
) *ReleaseCollectionJob {
|
|
|
|
return &ReleaseCollectionJob{
|
2023-05-30 17:41:28 +08:00
|
|
|
BaseJob: NewBaseJob(ctx, req.Base.GetMsgID(), req.GetCollectionID()),
|
|
|
|
req: req,
|
|
|
|
dist: dist,
|
|
|
|
meta: meta,
|
|
|
|
broker: broker,
|
|
|
|
cluster: cluster,
|
|
|
|
targetMgr: targetMgr,
|
|
|
|
targetObserver: targetObserver,
|
|
|
|
checkerController: checkerController,
|
2023-03-20 14:55:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (job *ReleaseCollectionJob) Execute() error {
|
|
|
|
req := job.req
|
|
|
|
log := log.Ctx(job.ctx).With(zap.Int64("collectionID", req.GetCollectionID()))
|
|
|
|
|
|
|
|
if !job.meta.CollectionManager.Exist(req.GetCollectionID()) {
|
|
|
|
log.Info("release collection end, the collection has not been loaded into QueryNode")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-18 09:17:23 +08:00
|
|
|
loadedPartitions := job.meta.CollectionManager.GetPartitionsByCollection(req.GetCollectionID())
|
|
|
|
toRelease := lo.Map(loadedPartitions, func(partition *meta.Partition, _ int) int64 {
|
|
|
|
return partition.GetPartitionID()
|
|
|
|
})
|
|
|
|
releasePartitions(job.ctx, job.meta, job.cluster, req.GetCollectionID(), toRelease...)
|
2023-03-20 14:55:57 +08:00
|
|
|
|
|
|
|
err := job.meta.CollectionManager.RemoveCollection(req.GetCollectionID())
|
|
|
|
if err != nil {
|
|
|
|
msg := "failed to remove collection"
|
|
|
|
log.Warn(msg, zap.Error(err))
|
2023-07-17 14:59:34 +08:00
|
|
|
return errors.Wrap(err, msg)
|
2023-03-20 14:55:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
err = job.meta.ReplicaManager.RemoveCollection(req.GetCollectionID())
|
|
|
|
if err != nil {
|
|
|
|
msg := "failed to remove replicas"
|
|
|
|
log.Warn(msg, zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
job.targetObserver.ReleaseCollection(req.GetCollectionID())
|
2023-05-30 17:41:28 +08:00
|
|
|
waitCollectionReleased(job.dist, job.checkerController, req.GetCollectionID())
|
2023-03-20 14:55:57 +08:00
|
|
|
metrics.QueryCoordNumCollections.WithLabelValues().Dec()
|
2023-05-18 09:17:23 +08:00
|
|
|
metrics.QueryCoordNumPartitions.WithLabelValues().Sub(float64(len(toRelease)))
|
2023-05-17 11:17:22 +08:00
|
|
|
metrics.QueryCoordReleaseCount.WithLabelValues(metrics.TotalLabel).Inc()
|
|
|
|
metrics.QueryCoordReleaseCount.WithLabelValues(metrics.SuccessLabel).Inc()
|
2023-03-20 14:55:57 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ReleasePartitionJob struct {
|
|
|
|
*BaseJob
|
|
|
|
releasePartitionsOnly bool
|
|
|
|
|
2023-05-30 17:41:28 +08:00
|
|
|
req *querypb.ReleasePartitionsRequest
|
|
|
|
dist *meta.DistributionManager
|
|
|
|
meta *meta.Meta
|
|
|
|
broker meta.Broker
|
|
|
|
cluster session.Cluster
|
2024-08-02 17:20:15 +08:00
|
|
|
targetMgr meta.TargetManagerInterface
|
2023-05-30 17:41:28 +08:00
|
|
|
targetObserver *observers.TargetObserver
|
|
|
|
checkerController *checkers.CheckerController
|
2023-03-20 14:55:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewReleasePartitionJob(ctx context.Context,
|
|
|
|
req *querypb.ReleasePartitionsRequest,
|
|
|
|
dist *meta.DistributionManager,
|
|
|
|
meta *meta.Meta,
|
2023-05-18 09:17:23 +08:00
|
|
|
broker meta.Broker,
|
2023-03-20 14:55:57 +08:00
|
|
|
cluster session.Cluster,
|
2024-08-02 17:20:15 +08:00
|
|
|
targetMgr meta.TargetManagerInterface,
|
2023-03-20 14:55:57 +08:00
|
|
|
targetObserver *observers.TargetObserver,
|
2023-05-30 17:41:28 +08:00
|
|
|
checkerController *checkers.CheckerController,
|
2023-03-20 14:55:57 +08:00
|
|
|
) *ReleasePartitionJob {
|
|
|
|
return &ReleasePartitionJob{
|
2023-05-30 17:41:28 +08:00
|
|
|
BaseJob: NewBaseJob(ctx, req.Base.GetMsgID(), req.GetCollectionID()),
|
|
|
|
req: req,
|
|
|
|
dist: dist,
|
|
|
|
meta: meta,
|
|
|
|
broker: broker,
|
|
|
|
cluster: cluster,
|
|
|
|
targetMgr: targetMgr,
|
|
|
|
targetObserver: targetObserver,
|
|
|
|
checkerController: checkerController,
|
2023-03-20 14:55:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (job *ReleasePartitionJob) Execute() error {
|
|
|
|
req := job.req
|
|
|
|
log := log.Ctx(job.ctx).With(
|
|
|
|
zap.Int64("collectionID", req.GetCollectionID()),
|
|
|
|
zap.Int64s("partitionIDs", req.GetPartitionIDs()),
|
|
|
|
)
|
|
|
|
|
|
|
|
if !job.meta.CollectionManager.Exist(req.GetCollectionID()) {
|
|
|
|
log.Info("release collection end, the collection has not been loaded into QueryNode")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
loadedPartitions := job.meta.CollectionManager.GetPartitionsByCollection(req.GetCollectionID())
|
|
|
|
toRelease := lo.FilterMap(loadedPartitions, func(partition *meta.Partition, _ int) (int64, bool) {
|
|
|
|
return partition.GetPartitionID(), lo.Contains(req.GetPartitionIDs(), partition.GetPartitionID())
|
|
|
|
})
|
|
|
|
|
2023-04-23 19:16:32 +08:00
|
|
|
if len(toRelease) == 0 {
|
|
|
|
log.Warn("releasing partition(s) not loaded")
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-18 09:17:23 +08:00
|
|
|
releasePartitions(job.ctx, job.meta, job.cluster, req.GetCollectionID(), toRelease...)
|
2023-04-23 19:16:32 +08:00
|
|
|
|
2023-07-19 16:54:58 +08:00
|
|
|
// If all partitions are released, clear all
|
|
|
|
if len(toRelease) == len(loadedPartitions) {
|
2023-03-20 14:55:57 +08:00
|
|
|
log.Info("release partitions covers all partitions, will remove the whole collection")
|
|
|
|
err := job.meta.CollectionManager.RemoveCollection(req.GetCollectionID())
|
|
|
|
if err != nil {
|
|
|
|
msg := "failed to release partitions from store"
|
|
|
|
log.Warn(msg, zap.Error(err))
|
2023-07-17 14:59:34 +08:00
|
|
|
return errors.Wrap(err, msg)
|
2023-03-20 14:55:57 +08:00
|
|
|
}
|
|
|
|
err = job.meta.ReplicaManager.RemoveCollection(req.GetCollectionID())
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("failed to remove replicas", zap.Error(err))
|
|
|
|
}
|
|
|
|
job.targetObserver.ReleaseCollection(req.GetCollectionID())
|
|
|
|
metrics.QueryCoordNumCollections.WithLabelValues().Dec()
|
2023-05-30 17:41:28 +08:00
|
|
|
waitCollectionReleased(job.dist, job.checkerController, req.GetCollectionID())
|
2023-03-20 14:55:57 +08:00
|
|
|
} else {
|
2023-09-02 00:09:01 +08:00
|
|
|
err := job.meta.CollectionManager.RemovePartition(req.GetCollectionID(), toRelease...)
|
2023-03-20 14:55:57 +08:00
|
|
|
if err != nil {
|
|
|
|
msg := "failed to release partitions from store"
|
|
|
|
log.Warn(msg, zap.Error(err))
|
2023-07-17 14:59:34 +08:00
|
|
|
return errors.Wrap(err, msg)
|
2023-03-20 14:55:57 +08:00
|
|
|
}
|
2024-09-05 18:47:03 +08:00
|
|
|
job.targetObserver.ReleasePartition(req.GetCollectionID(), toRelease...)
|
2023-05-30 17:41:28 +08:00
|
|
|
waitCollectionReleased(job.dist, job.checkerController, req.GetCollectionID(), toRelease...)
|
2023-03-20 14:55:57 +08:00
|
|
|
}
|
|
|
|
metrics.QueryCoordNumPartitions.WithLabelValues().Sub(float64(len(toRelease)))
|
|
|
|
return nil
|
|
|
|
}
|