2023-01-04 19:37:36 +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 datacoord
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2023-12-21 18:07:24 +08:00
|
|
|
"github.com/samber/lo"
|
2023-01-04 19:37:36 +08:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2023-06-09 01:28:37 +08:00
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
|
2023-01-04 19:37:36 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/metastore/model"
|
2024-09-02 14:19:03 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/datapb"
|
2023-01-11 14:35:40 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/indexpb"
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/log"
|
|
|
|
"github.com/milvus-io/milvus/pkg/metrics"
|
2024-08-21 00:22:54 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/funcutil"
|
2024-01-17 16:40:54 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/indexparamcheck"
|
2023-05-06 10:34:39 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/merr"
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/metautil"
|
|
|
|
"github.com/milvus-io/milvus/pkg/util/paramtable"
|
2023-06-01 18:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/typeutil"
|
2023-01-04 19:37:36 +08:00
|
|
|
)
|
|
|
|
|
2023-05-06 10:34:39 +08:00
|
|
|
// serverID return the session serverID
|
|
|
|
func (s *Server) serverID() int64 {
|
|
|
|
if s.session != nil {
|
2023-11-07 14:02:20 +08:00
|
|
|
return s.session.GetServerID()
|
2023-05-06 10:34:39 +08:00
|
|
|
}
|
|
|
|
// return 0 if no session exist, only for UT
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2023-01-04 19:37:36 +08:00
|
|
|
func (s *Server) startIndexService(ctx context.Context) {
|
|
|
|
s.serverLoopWg.Add(1)
|
|
|
|
go s.createIndexForSegmentLoop(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) createIndexForSegment(segment *SegmentInfo, indexID UniqueID) error {
|
2024-09-02 14:19:03 +08:00
|
|
|
if !segment.GetIsSorted() && !segment.GetIsImporting() && segment.Level != datapb.SegmentLevel_L0 {
|
|
|
|
log.Info("segment not sorted, skip create index", zap.Int64("segmentID", segment.GetID()))
|
|
|
|
return nil
|
|
|
|
}
|
2023-09-28 18:03:28 +08:00
|
|
|
log.Info("create index for segment", zap.Int64("segmentID", segment.ID), zap.Int64("indexID", indexID))
|
2024-08-22 10:06:56 +08:00
|
|
|
buildID, err := s.allocator.AllocID(context.Background())
|
2023-01-04 19:37:36 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
segIndex := &model.SegmentIndex{
|
2023-09-28 18:03:28 +08:00
|
|
|
SegmentID: segment.ID,
|
|
|
|
CollectionID: segment.CollectionID,
|
|
|
|
PartitionID: segment.PartitionID,
|
|
|
|
NumRows: segment.NumOfRows,
|
|
|
|
IndexID: indexID,
|
|
|
|
BuildID: buildID,
|
|
|
|
CreateTime: uint64(segment.ID),
|
|
|
|
WriteHandoff: false,
|
2023-01-04 19:37:36 +08:00
|
|
|
}
|
2024-03-04 16:56:59 +08:00
|
|
|
if err = s.meta.indexMeta.AddSegmentIndex(segIndex); err != nil {
|
2023-01-04 19:37:36 +08:00
|
|
|
return err
|
|
|
|
}
|
2024-09-02 14:19:03 +08:00
|
|
|
s.taskScheduler.enqueue(newIndexBuildTask(buildID))
|
2023-01-04 19:37:36 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) createIndexesForSegment(segment *SegmentInfo) error {
|
2024-09-02 14:19:03 +08:00
|
|
|
if !segment.GetIsSorted() && !segment.GetIsImporting() && segment.GetLevel() != datapb.SegmentLevel_L0 {
|
|
|
|
log.Debug("segment is not sorted by pk, skip create index", zap.Int64("segmentID", segment.ID))
|
|
|
|
return nil
|
|
|
|
}
|
2024-03-04 16:56:59 +08:00
|
|
|
indexes := s.meta.indexMeta.GetIndexesForCollection(segment.CollectionID, "")
|
|
|
|
indexIDToSegIndexes := s.meta.indexMeta.GetSegmentIndexes(segment.CollectionID, segment.ID)
|
2023-01-04 19:37:36 +08:00
|
|
|
for _, index := range indexes {
|
2024-03-04 16:56:59 +08:00
|
|
|
if _, ok := indexIDToSegIndexes[index.IndexID]; !ok {
|
2023-01-04 19:37:36 +08:00
|
|
|
if err := s.createIndexForSegment(segment, index.IndexID); err != nil {
|
2023-07-14 15:56:31 +08:00
|
|
|
log.Warn("create index for segment fail", zap.Int64("segmentID", segment.ID),
|
2023-01-04 19:37:36 +08:00
|
|
|
zap.Int64("indexID", index.IndexID))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-04 16:56:59 +08:00
|
|
|
func (s *Server) getUnIndexTaskSegments() []*SegmentInfo {
|
2024-05-08 21:37:29 +08:00
|
|
|
flushedSegments := s.meta.SelectSegments(SegmentFilterFunc(func(seg *SegmentInfo) bool {
|
2024-03-04 16:56:59 +08:00
|
|
|
return isFlush(seg)
|
2024-05-08 21:37:29 +08:00
|
|
|
}))
|
2024-03-04 16:56:59 +08:00
|
|
|
|
|
|
|
unindexedSegments := make([]*SegmentInfo, 0)
|
|
|
|
for _, segment := range flushedSegments {
|
|
|
|
if s.meta.indexMeta.IsUnIndexedSegment(segment.CollectionID, segment.GetID()) {
|
|
|
|
unindexedSegments = append(unindexedSegments, segment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return unindexedSegments
|
|
|
|
}
|
|
|
|
|
2023-01-04 19:37:36 +08:00
|
|
|
func (s *Server) createIndexForSegmentLoop(ctx context.Context) {
|
|
|
|
log.Info("start create index for segment loop...")
|
|
|
|
defer s.serverLoopWg.Done()
|
|
|
|
|
2024-09-02 14:19:03 +08:00
|
|
|
ticker := time.NewTicker(Params.DataCoordCfg.TaskCheckInterval.GetAsDuration(time.Second))
|
2023-01-04 19:37:36 +08:00
|
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
log.Warn("DataCoord context done, exit...")
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
2024-03-04 16:56:59 +08:00
|
|
|
segments := s.getUnIndexTaskSegments()
|
2023-01-04 19:37:36 +08:00
|
|
|
for _, segment := range segments {
|
|
|
|
if err := s.createIndexesForSegment(segment); err != nil {
|
2023-07-14 15:56:31 +08:00
|
|
|
log.Warn("create index for segment fail, wait for retry", zap.Int64("segmentID", segment.ID))
|
2023-01-04 19:37:36 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2023-07-14 15:56:31 +08:00
|
|
|
case collectionID := <-s.notifyIndexChan:
|
|
|
|
log.Info("receive create index notify", zap.Int64("collectionID", collectionID))
|
2024-05-08 21:37:29 +08:00
|
|
|
segments := s.meta.SelectSegments(WithCollection(collectionID), SegmentFilterFunc(func(info *SegmentInfo) bool {
|
2024-09-02 14:19:03 +08:00
|
|
|
return isFlush(info) && info.GetIsSorted()
|
2024-05-08 21:37:29 +08:00
|
|
|
}))
|
2023-01-04 19:37:36 +08:00
|
|
|
for _, segment := range segments {
|
|
|
|
if err := s.createIndexesForSegment(segment); err != nil {
|
2023-07-14 15:56:31 +08:00
|
|
|
log.Warn("create index for segment fail, wait for retry", zap.Int64("segmentID", segment.ID))
|
2023-01-04 19:37:36 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case segID := <-s.buildIndexCh:
|
2023-07-14 15:56:31 +08:00
|
|
|
log.Info("receive new flushed segment", zap.Int64("segmentID", segID))
|
2023-03-03 14:13:49 +08:00
|
|
|
segment := s.meta.GetSegment(segID)
|
2023-01-04 19:37:36 +08:00
|
|
|
if segment == nil {
|
2023-07-14 15:56:31 +08:00
|
|
|
log.Warn("segment is not exist, no need to build index", zap.Int64("segmentID", segID))
|
2023-01-04 19:37:36 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := s.createIndexesForSegment(segment); err != nil {
|
2023-07-14 15:56:31 +08:00
|
|
|
log.Warn("create index for segment fail, wait for retry", zap.Int64("segmentID", segment.ID))
|
2023-01-04 19:37:36 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-20 17:22:41 +08:00
|
|
|
func (s *Server) getFieldNameByID(ctx context.Context, collID, fieldID int64) (string, error) {
|
|
|
|
resp, err := s.broker.DescribeCollectionInternal(ctx, collID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, field := range resp.GetSchema().GetFields() {
|
|
|
|
if field.FieldID == fieldID {
|
|
|
|
return field.Name, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2023-01-04 19:37:36 +08:00
|
|
|
// CreateIndex create an index on collection.
|
|
|
|
// Index building is asynchronous, so when an index building request comes, an IndexID is assigned to the task and
|
|
|
|
// will get all flushed segments from DataCoord and record tasks with these segments. The background process
|
|
|
|
// indexBuilder will find this task and assign it to IndexNode for execution.
|
2023-01-11 14:35:40 +08:00
|
|
|
func (s *Server) CreateIndex(ctx context.Context, req *indexpb.CreateIndexRequest) (*commonpb.Status, error) {
|
2023-07-14 15:56:31 +08:00
|
|
|
log := log.Ctx(ctx).With(
|
2023-09-15 10:07:19 +08:00
|
|
|
zap.Int64("collectionID", req.GetCollectionID()),
|
2023-07-14 15:56:31 +08:00
|
|
|
)
|
|
|
|
log.Info("receive CreateIndex request",
|
2023-01-04 19:37:36 +08:00
|
|
|
zap.String("IndexName", req.GetIndexName()), zap.Int64("fieldID", req.GetFieldID()),
|
|
|
|
zap.Any("TypeParams", req.GetTypeParams()),
|
2023-09-26 17:15:27 +08:00
|
|
|
zap.Any("IndexParams", req.GetIndexParams()),
|
2024-09-08 18:23:05 +08:00
|
|
|
zap.Any("UserIndexParams", req.GetUserIndexParams()),
|
2023-09-26 17:15:27 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
if err := merr.CheckHealthy(s.GetStateCode()); err != nil {
|
|
|
|
log.Warn(msgDataCoordIsUnhealthy(paramtable.GetNodeID()), zap.Error(err))
|
|
|
|
return merr.Status(err), nil
|
2023-01-04 19:37:36 +08:00
|
|
|
}
|
2023-01-06 14:21:37 +08:00
|
|
|
metrics.IndexRequestCounter.WithLabelValues(metrics.TotalLabel).Inc()
|
2023-01-04 19:37:36 +08:00
|
|
|
|
2023-12-20 17:22:41 +08:00
|
|
|
if req.GetIndexName() == "" {
|
2024-03-04 16:56:59 +08:00
|
|
|
indexes := s.meta.indexMeta.GetFieldIndexes(req.GetCollectionID(), req.GetFieldID(), req.GetIndexName())
|
2023-12-20 17:22:41 +08:00
|
|
|
if len(indexes) == 0 {
|
|
|
|
fieldName, err := s.getFieldNameByID(ctx, req.GetCollectionID(), req.GetFieldID())
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("get field name from schema failed", zap.Int64("fieldID", req.GetFieldID()))
|
|
|
|
return merr.Status(err), nil
|
|
|
|
}
|
|
|
|
req.IndexName = fieldName
|
|
|
|
} else if len(indexes) == 1 {
|
|
|
|
req.IndexName = indexes[0].IndexName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-04 16:56:59 +08:00
|
|
|
indexID, err := s.meta.indexMeta.CanCreateIndex(req)
|
2023-01-04 19:37:36 +08:00
|
|
|
if err != nil {
|
2023-01-06 14:21:37 +08:00
|
|
|
metrics.IndexRequestCounter.WithLabelValues(metrics.FailLabel).Inc()
|
2023-09-26 17:15:27 +08:00
|
|
|
return merr.Status(err), nil
|
2023-01-04 19:37:36 +08:00
|
|
|
}
|
|
|
|
|
2024-03-11 17:31:03 +08:00
|
|
|
// merge with previous params because create index would not pass mmap params
|
|
|
|
indexes := s.meta.indexMeta.GetFieldIndexes(req.GetCollectionID(), req.GetFieldID(), req.GetIndexName())
|
|
|
|
if len(indexes) == 1 {
|
2024-05-06 15:43:29 +08:00
|
|
|
req.UserIndexParams = UpdateParams(indexes[0], indexes[0].UserIndexParams, req.GetUserIndexParams())
|
|
|
|
req.IndexParams = UpdateParams(indexes[0], indexes[0].IndexParams, req.GetIndexParams())
|
2024-03-11 17:31:03 +08:00
|
|
|
}
|
|
|
|
|
2023-01-04 19:37:36 +08:00
|
|
|
if indexID == 0 {
|
2024-08-22 10:06:56 +08:00
|
|
|
indexID, err = s.allocator.AllocID(ctx)
|
2023-01-04 19:37:36 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Warn("failed to alloc indexID", zap.Error(err))
|
2023-01-06 14:21:37 +08:00
|
|
|
metrics.IndexRequestCounter.WithLabelValues(metrics.FailLabel).Inc()
|
2023-09-26 17:15:27 +08:00
|
|
|
return merr.Status(err), nil
|
2023-01-04 19:37:36 +08:00
|
|
|
}
|
2024-03-18 15:17:10 +08:00
|
|
|
if GetIndexType(req.GetIndexParams()) == indexparamcheck.IndexDISKANN && !s.indexNodeManager.ClientSupportDisk() {
|
2023-01-04 19:37:36 +08:00
|
|
|
errMsg := "all IndexNodes do not support disk indexes, please verify"
|
|
|
|
log.Warn(errMsg)
|
2024-01-24 00:04:55 +08:00
|
|
|
err = merr.WrapErrIndexNotSupported(indexparamcheck.IndexDISKANN)
|
2023-01-06 14:21:37 +08:00
|
|
|
metrics.IndexRequestCounter.WithLabelValues(metrics.FailLabel).Inc()
|
2023-09-26 17:15:27 +08:00
|
|
|
return merr.Status(err), nil
|
2023-01-04 19:37:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
index := &model.Index{
|
|
|
|
CollectionID: req.GetCollectionID(),
|
|
|
|
FieldID: req.GetFieldID(),
|
|
|
|
IndexID: indexID,
|
|
|
|
IndexName: req.GetIndexName(),
|
|
|
|
TypeParams: req.GetTypeParams(),
|
|
|
|
IndexParams: req.GetIndexParams(),
|
|
|
|
CreateTime: req.GetTimestamp(),
|
|
|
|
IsAutoIndex: req.GetIsAutoIndex(),
|
|
|
|
UserIndexParams: req.GetUserIndexParams(),
|
|
|
|
}
|
|
|
|
|
2024-05-06 15:43:29 +08:00
|
|
|
if err := ValidateIndexParams(index); err != nil {
|
|
|
|
metrics.IndexRequestCounter.WithLabelValues(metrics.FailLabel).Inc()
|
|
|
|
return merr.Status(err), nil
|
|
|
|
}
|
|
|
|
|
2023-01-04 19:37:36 +08:00
|
|
|
// Get flushed segments and create index
|
2024-03-04 16:56:59 +08:00
|
|
|
err = s.meta.indexMeta.CreateIndex(index)
|
2023-01-04 19:37:36 +08:00
|
|
|
if err != nil {
|
2023-07-14 15:56:31 +08:00
|
|
|
log.Error("CreateIndex fail",
|
2023-01-04 19:37:36 +08:00
|
|
|
zap.Int64("fieldID", req.GetFieldID()), zap.String("indexName", req.GetIndexName()), zap.Error(err))
|
2023-01-06 14:21:37 +08:00
|
|
|
metrics.IndexRequestCounter.WithLabelValues(metrics.FailLabel).Inc()
|
2023-09-26 17:15:27 +08:00
|
|
|
return merr.Status(err), nil
|
2023-01-04 19:37:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case s.notifyIndexChan <- req.GetCollectionID():
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2023-07-14 15:56:31 +08:00
|
|
|
log.Info("CreateIndex successfully",
|
2023-01-04 19:37:36 +08:00
|
|
|
zap.String("IndexName", req.GetIndexName()), zap.Int64("fieldID", req.GetFieldID()),
|
|
|
|
zap.Int64("IndexID", indexID))
|
2023-01-06 14:21:37 +08:00
|
|
|
metrics.IndexRequestCounter.WithLabelValues(metrics.SuccessLabel).Inc()
|
2023-10-11 21:01:35 +08:00
|
|
|
return merr.Success(), nil
|
2023-01-04 19:37:36 +08:00
|
|
|
}
|
|
|
|
|
2024-05-06 15:43:29 +08:00
|
|
|
func ValidateIndexParams(index *model.Index) error {
|
2024-08-21 00:22:54 +08:00
|
|
|
indexType := GetIndexType(index.IndexParams)
|
|
|
|
indexParams := funcutil.KeyValuePair2Map(index.IndexParams)
|
2024-08-24 01:40:58 +08:00
|
|
|
userIndexParams := funcutil.KeyValuePair2Map(index.UserIndexParams)
|
2024-08-21 00:22:54 +08:00
|
|
|
if err := indexparamcheck.ValidateMmapIndexParams(indexType, indexParams); err != nil {
|
|
|
|
return merr.WrapErrParameterInvalidMsg("invalid mmap index params", err.Error())
|
|
|
|
}
|
|
|
|
if err := indexparamcheck.ValidateMmapIndexParams(indexType, userIndexParams); err != nil {
|
|
|
|
return merr.WrapErrParameterInvalidMsg("invalid mmap user index params", err.Error())
|
2024-01-17 16:40:54 +08:00
|
|
|
}
|
2024-08-24 01:40:58 +08:00
|
|
|
if err := indexparamcheck.ValidateOffsetCacheIndexParams(indexType, indexParams); err != nil {
|
|
|
|
return merr.WrapErrParameterInvalidMsg("invalid offset cache index params", err.Error())
|
|
|
|
}
|
|
|
|
if err := indexparamcheck.ValidateOffsetCacheIndexParams(indexType, userIndexParams); err != nil {
|
|
|
|
return merr.WrapErrParameterInvalidMsg("invalid offset cache index params", err.Error())
|
|
|
|
}
|
2024-01-17 16:40:54 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-06 15:43:29 +08:00
|
|
|
func UpdateParams(index *model.Index, from []*commonpb.KeyValuePair, updates []*commonpb.KeyValuePair) []*commonpb.KeyValuePair {
|
2024-01-17 16:40:54 +08:00
|
|
|
params := make(map[string]string)
|
|
|
|
for _, param := range from {
|
|
|
|
params[param.GetKey()] = param.GetValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
// update the params
|
|
|
|
for _, param := range updates {
|
|
|
|
params[param.GetKey()] = param.GetValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
return lo.MapToSlice(params, func(k string, v string) *commonpb.KeyValuePair {
|
|
|
|
return &commonpb.KeyValuePair{
|
|
|
|
Key: k,
|
|
|
|
Value: v,
|
|
|
|
}
|
2024-05-06 15:43:29 +08:00
|
|
|
})
|
2024-01-17 16:40:54 +08:00
|
|
|
}
|
|
|
|
|
2023-12-21 18:07:24 +08:00
|
|
|
func (s *Server) AlterIndex(ctx context.Context, req *indexpb.AlterIndexRequest) (*commonpb.Status, error) {
|
|
|
|
log := log.Ctx(ctx).With(
|
|
|
|
zap.Int64("collectionID", req.GetCollectionID()),
|
|
|
|
zap.String("indexName", req.GetIndexName()),
|
|
|
|
)
|
|
|
|
log.Info("received AlterIndex request", zap.Any("params", req.GetParams()))
|
|
|
|
|
2024-03-11 13:05:02 +08:00
|
|
|
if req.IndexName == "" {
|
|
|
|
return merr.Status(merr.WrapErrParameterInvalidMsg("index name is empty")), nil
|
|
|
|
}
|
|
|
|
|
2023-12-21 18:07:24 +08:00
|
|
|
if err := merr.CheckHealthy(s.GetStateCode()); err != nil {
|
|
|
|
log.Warn(msgDataCoordIsUnhealthy(paramtable.GetNodeID()), zap.Error(err))
|
|
|
|
return merr.Status(err), nil
|
|
|
|
}
|
|
|
|
|
2024-03-04 16:56:59 +08:00
|
|
|
indexes := s.meta.indexMeta.GetIndexesForCollection(req.GetCollectionID(), req.GetIndexName())
|
2024-03-11 13:05:02 +08:00
|
|
|
if len(indexes) == 0 {
|
2024-02-29 15:55:01 +08:00
|
|
|
err := merr.WrapErrIndexNotFound(req.GetIndexName())
|
|
|
|
return merr.Status(err), nil
|
|
|
|
}
|
2024-03-04 16:56:59 +08:00
|
|
|
|
2023-12-21 18:07:24 +08:00
|
|
|
for _, index := range indexes {
|
2024-01-17 16:40:54 +08:00
|
|
|
// update user index params
|
2024-05-06 15:43:29 +08:00
|
|
|
newUserIndexParams := UpdateParams(index, index.UserIndexParams, req.GetParams())
|
2024-01-17 16:40:54 +08:00
|
|
|
log.Info("alter index user index params",
|
|
|
|
zap.String("indexName", index.IndexName),
|
|
|
|
zap.Any("params", newUserIndexParams),
|
|
|
|
)
|
|
|
|
index.UserIndexParams = newUserIndexParams
|
2023-12-21 18:07:24 +08:00
|
|
|
|
2024-01-17 16:40:54 +08:00
|
|
|
// update index params
|
2024-05-06 15:43:29 +08:00
|
|
|
newIndexParams := UpdateParams(index, index.IndexParams, req.GetParams())
|
2024-09-08 18:23:05 +08:00
|
|
|
log.Info("alter index index params",
|
2023-12-21 18:07:24 +08:00
|
|
|
zap.String("indexName", index.IndexName),
|
2024-01-17 16:40:54 +08:00
|
|
|
zap.Any("params", newIndexParams),
|
2023-12-21 18:07:24 +08:00
|
|
|
)
|
2024-01-17 16:40:54 +08:00
|
|
|
index.IndexParams = newIndexParams
|
2024-05-06 15:43:29 +08:00
|
|
|
|
|
|
|
if err := ValidateIndexParams(index); err != nil {
|
|
|
|
return merr.Status(err), nil
|
|
|
|
}
|
2023-12-21 18:07:24 +08:00
|
|
|
}
|
|
|
|
|
2024-03-04 16:56:59 +08:00
|
|
|
err := s.meta.indexMeta.AlterIndex(ctx, indexes...)
|
2023-12-21 18:07:24 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Warn("failed to alter index", zap.Error(err))
|
|
|
|
return merr.Status(err), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return merr.Success(), nil
|
|
|
|
}
|
|
|
|
|
2023-01-04 19:37:36 +08:00
|
|
|
// GetIndexState gets the index state of the index name in the request from Proxy.
|
2023-05-06 10:34:39 +08:00
|
|
|
// Deprecated
|
2023-01-11 14:35:40 +08:00
|
|
|
func (s *Server) GetIndexState(ctx context.Context, req *indexpb.GetIndexStateRequest) (*indexpb.GetIndexStateResponse, error) {
|
2023-07-14 15:56:31 +08:00
|
|
|
log := log.Ctx(ctx).With(
|
2023-09-15 10:07:19 +08:00
|
|
|
zap.Int64("collectionID", req.GetCollectionID()),
|
2023-10-08 21:23:32 +08:00
|
|
|
zap.String("indexName", req.GetIndexName()),
|
2023-07-14 15:56:31 +08:00
|
|
|
)
|
2023-10-08 21:23:32 +08:00
|
|
|
log.Info("receive GetIndexState request")
|
2023-01-04 19:37:36 +08:00
|
|
|
|
2023-09-26 17:15:27 +08:00
|
|
|
if err := merr.CheckHealthy(s.GetStateCode()); err != nil {
|
|
|
|
log.Warn(msgDataCoordIsUnhealthy(paramtable.GetNodeID()), zap.Error(err))
|
2023-01-11 14:35:40 +08:00
|
|
|
return &indexpb.GetIndexStateResponse{
|
2023-09-26 17:15:27 +08:00
|
|
|
Status: merr.Status(err),
|
2023-01-04 19:37:36 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-03-04 16:56:59 +08:00
|
|
|
indexes := s.meta.indexMeta.GetIndexesForCollection(req.GetCollectionID(), req.GetIndexName())
|
2023-01-04 19:37:36 +08:00
|
|
|
if len(indexes) == 0 {
|
2023-09-26 17:15:27 +08:00
|
|
|
err := merr.WrapErrIndexNotFound(req.GetIndexName())
|
2023-10-08 21:23:32 +08:00
|
|
|
log.Warn("GetIndexState fail", zap.Error(err))
|
2023-01-11 14:35:40 +08:00
|
|
|
return &indexpb.GetIndexStateResponse{
|
2023-09-26 17:15:27 +08:00
|
|
|
Status: merr.Status(err),
|
2023-01-04 19:37:36 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
if len(indexes) > 1 {
|
|
|
|
log.Warn(msgAmbiguousIndexName())
|
2023-09-26 17:15:27 +08:00
|
|
|
err := merr.WrapErrIndexDuplicate(req.GetIndexName())
|
2023-01-11 14:35:40 +08:00
|
|
|
return &indexpb.GetIndexStateResponse{
|
2023-09-26 17:15:27 +08:00
|
|
|
Status: merr.Status(err),
|
2023-01-04 19:37:36 +08:00
|
|
|
}, nil
|
|
|
|
}
|
2023-01-11 14:35:40 +08:00
|
|
|
ret := &indexpb.GetIndexStateResponse{
|
2023-10-11 21:01:35 +08:00
|
|
|
Status: merr.Success(),
|
2023-09-04 09:57:09 +08:00
|
|
|
State: commonpb.IndexState_Finished,
|
2023-01-04 19:37:36 +08:00
|
|
|
}
|
|
|
|
|
2023-10-08 21:23:32 +08:00
|
|
|
indexInfo := &indexpb.IndexInfo{}
|
2024-03-03 19:00:59 +08:00
|
|
|
// The total rows of all indexes should be based on the current perspective
|
2024-05-08 21:37:29 +08:00
|
|
|
segments := s.selectSegmentIndexesStats(WithCollection(req.GetCollectionID()), SegmentFilterFunc(func(info *SegmentInfo) bool {
|
2024-09-02 14:19:03 +08:00
|
|
|
return info.GetLevel() != datapb.SegmentLevel_L0 && (isFlush(info) || info.GetState() == commonpb.SegmentState_Dropped)
|
2024-05-08 21:37:29 +08:00
|
|
|
}))
|
2024-03-03 19:00:59 +08:00
|
|
|
|
|
|
|
s.completeIndexInfo(indexInfo, indexes[0], segments, false, indexes[0].CreateTime)
|
2023-01-04 19:37:36 +08:00
|
|
|
ret.State = indexInfo.State
|
|
|
|
ret.FailReason = indexInfo.IndexStateFailReason
|
|
|
|
|
2023-07-14 15:56:31 +08:00
|
|
|
log.Info("GetIndexState success",
|
2023-10-08 21:23:32 +08:00
|
|
|
zap.String("state", ret.GetState().String()),
|
|
|
|
)
|
2023-01-04 19:37:36 +08:00
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:35:40 +08:00
|
|
|
func (s *Server) GetSegmentIndexState(ctx context.Context, req *indexpb.GetSegmentIndexStateRequest) (*indexpb.GetSegmentIndexStateResponse, error) {
|
2023-07-14 15:56:31 +08:00
|
|
|
log := log.Ctx(ctx).With(
|
2023-09-15 10:07:19 +08:00
|
|
|
zap.Int64("collectionID", req.GetCollectionID()),
|
2023-07-14 15:56:31 +08:00
|
|
|
)
|
|
|
|
log.Info("receive GetSegmentIndexState",
|
2023-09-26 17:15:27 +08:00
|
|
|
zap.String("IndexName", req.GetIndexName()),
|
2023-12-29 11:44:45 +08:00
|
|
|
zap.Int64s("segmentIDs", req.GetSegmentIDs()),
|
2023-09-26 17:15:27 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
if err := merr.CheckHealthy(s.GetStateCode()); err != nil {
|
|
|
|
log.Warn(msgDataCoordIsUnhealthy(paramtable.GetNodeID()), zap.Error(err))
|
2023-01-11 14:35:40 +08:00
|
|
|
return &indexpb.GetSegmentIndexStateResponse{
|
2023-09-26 17:15:27 +08:00
|
|
|
Status: merr.Status(err),
|
2023-01-04 19:37:36 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:35:40 +08:00
|
|
|
ret := &indexpb.GetSegmentIndexStateResponse{
|
2023-10-11 21:01:35 +08:00
|
|
|
Status: merr.Success(),
|
2023-01-11 14:35:40 +08:00
|
|
|
States: make([]*indexpb.SegmentIndexState, 0),
|
2023-01-04 19:37:36 +08:00
|
|
|
}
|
2024-03-04 16:56:59 +08:00
|
|
|
indexID2CreateTs := s.meta.indexMeta.GetIndexIDByName(req.GetCollectionID(), req.GetIndexName())
|
2023-01-04 19:37:36 +08:00
|
|
|
if len(indexID2CreateTs) == 0 {
|
2023-09-26 17:15:27 +08:00
|
|
|
err := merr.WrapErrIndexNotFound(req.GetIndexName())
|
|
|
|
log.Warn("GetSegmentIndexState fail", zap.String("indexName", req.GetIndexName()), zap.Error(err))
|
2023-01-11 14:35:40 +08:00
|
|
|
return &indexpb.GetSegmentIndexStateResponse{
|
2023-09-26 17:15:27 +08:00
|
|
|
Status: merr.Status(err),
|
2023-01-04 19:37:36 +08:00
|
|
|
}, nil
|
|
|
|
}
|
2023-09-15 10:07:19 +08:00
|
|
|
for _, segID := range req.GetSegmentIDs() {
|
2023-12-29 11:44:45 +08:00
|
|
|
for indexID := range indexID2CreateTs {
|
2024-03-04 16:56:59 +08:00
|
|
|
state := s.meta.indexMeta.GetSegmentIndexState(req.GetCollectionID(), segID, indexID)
|
2023-12-29 11:44:45 +08:00
|
|
|
ret.States = append(ret.States, state)
|
|
|
|
}
|
2023-01-04 19:37:36 +08:00
|
|
|
}
|
2023-07-14 15:56:31 +08:00
|
|
|
log.Info("GetSegmentIndexState successfully", zap.String("indexName", req.GetIndexName()))
|
2023-01-04 19:37:36 +08:00
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2024-05-08 21:37:29 +08:00
|
|
|
func (s *Server) selectSegmentIndexesStats(filters ...SegmentFilter) map[int64]*indexStats {
|
2024-03-04 16:56:59 +08:00
|
|
|
ret := make(map[int64]*indexStats)
|
|
|
|
|
2024-05-08 21:37:29 +08:00
|
|
|
segments := s.meta.SelectSegments(filters...)
|
2024-03-15 16:37:09 +08:00
|
|
|
segmentIDs := lo.Map(segments, func(info *SegmentInfo, i int) int64 {
|
|
|
|
return info.GetID()
|
|
|
|
})
|
|
|
|
if len(segments) == 0 {
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
segmentsIndexes := s.meta.indexMeta.getSegmentsIndexStates(segments[0].CollectionID, segmentIDs)
|
|
|
|
for _, info := range segments {
|
2024-03-04 16:56:59 +08:00
|
|
|
is := &indexStats{
|
|
|
|
ID: info.GetID(),
|
|
|
|
numRows: info.GetNumOfRows(),
|
|
|
|
compactionFrom: info.GetCompactionFrom(),
|
2024-03-15 16:37:09 +08:00
|
|
|
indexStates: segmentsIndexes[info.GetID()],
|
2024-03-04 16:56:59 +08:00
|
|
|
state: info.GetState(),
|
|
|
|
lastExpireTime: info.GetLastExpireTime(),
|
|
|
|
}
|
|
|
|
ret[info.GetID()] = is
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2024-03-03 19:00:59 +08:00
|
|
|
func (s *Server) countIndexedRows(indexInfo *indexpb.IndexInfo, segments map[int64]*indexStats) int64 {
|
2023-06-01 18:14:32 +08:00
|
|
|
unIndexed, indexed := typeutil.NewSet[int64](), typeutil.NewSet[int64]()
|
2024-03-03 19:00:59 +08:00
|
|
|
for segID, seg := range segments {
|
|
|
|
if seg.state != commonpb.SegmentState_Flushed && seg.state != commonpb.SegmentState_Flushing {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
segIdx, ok := seg.indexStates[indexInfo.IndexID]
|
2023-06-01 18:14:32 +08:00
|
|
|
if !ok {
|
2024-03-03 19:00:59 +08:00
|
|
|
unIndexed.Insert(segID)
|
2023-06-01 18:14:32 +08:00
|
|
|
continue
|
|
|
|
}
|
2024-03-03 19:00:59 +08:00
|
|
|
switch segIdx.GetState() {
|
2023-06-01 18:14:32 +08:00
|
|
|
case commonpb.IndexState_Finished:
|
2024-03-03 19:00:59 +08:00
|
|
|
indexed.Insert(segID)
|
2023-06-01 18:14:32 +08:00
|
|
|
default:
|
2024-03-03 19:00:59 +08:00
|
|
|
unIndexed.Insert(segID)
|
2023-06-01 18:14:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
retrieveContinue := len(unIndexed) != 0
|
|
|
|
for retrieveContinue {
|
|
|
|
for segID := range unIndexed {
|
|
|
|
unIndexed.Remove(segID)
|
2024-03-03 19:00:59 +08:00
|
|
|
segment := segments[segID]
|
|
|
|
if segment == nil || len(segment.compactionFrom) == 0 {
|
2023-06-01 18:14:32 +08:00
|
|
|
continue
|
|
|
|
}
|
2024-03-03 19:00:59 +08:00
|
|
|
for _, fromID := range segment.compactionFrom {
|
|
|
|
fromSeg := segments[fromID]
|
2023-06-01 18:14:32 +08:00
|
|
|
if fromSeg == nil {
|
|
|
|
continue
|
|
|
|
}
|
2024-03-03 19:00:59 +08:00
|
|
|
if segIndex, ok := fromSeg.indexStates[indexInfo.IndexID]; ok && segIndex.GetState() == commonpb.IndexState_Finished {
|
2023-06-01 18:14:32 +08:00
|
|
|
indexed.Insert(fromID)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
unIndexed.Insert(fromID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
retrieveContinue = len(unIndexed) != 0
|
|
|
|
}
|
|
|
|
indexedRows := int64(0)
|
|
|
|
for segID := range indexed {
|
2024-03-03 19:00:59 +08:00
|
|
|
segment := segments[segID]
|
2023-06-01 18:14:32 +08:00
|
|
|
if segment != nil {
|
2024-03-03 19:00:59 +08:00
|
|
|
indexedRows += segment.numRows
|
2023-06-01 18:14:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return indexedRows
|
|
|
|
}
|
|
|
|
|
2023-05-06 10:34:39 +08:00
|
|
|
// completeIndexInfo get the index row count and index task state
|
|
|
|
// if realTime, calculate current statistics
|
|
|
|
// if not realTime, which means get info of the prior `CreateIndex` action, skip segments created after index's create time
|
2024-03-03 19:00:59 +08:00
|
|
|
func (s *Server) completeIndexInfo(indexInfo *indexpb.IndexInfo, index *model.Index, segments map[int64]*indexStats, realTime bool, ts Timestamp) {
|
2023-01-04 19:37:36 +08:00
|
|
|
var (
|
2023-06-01 18:14:32 +08:00
|
|
|
cntNone = 0
|
|
|
|
cntUnissued = 0
|
|
|
|
cntInProgress = 0
|
|
|
|
cntFinished = 0
|
|
|
|
cntFailed = 0
|
|
|
|
failReason string
|
|
|
|
totalRows = int64(0)
|
|
|
|
indexedRows = int64(0)
|
|
|
|
pendingIndexRows = int64(0)
|
2023-01-04 19:37:36 +08:00
|
|
|
)
|
|
|
|
|
2024-03-03 19:00:59 +08:00
|
|
|
for segID, seg := range segments {
|
|
|
|
if seg.state != commonpb.SegmentState_Flushed && seg.state != commonpb.SegmentState_Flushing {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
totalRows += seg.numRows
|
|
|
|
segIdx, ok := seg.indexStates[index.IndexID]
|
2023-03-20 10:21:56 +08:00
|
|
|
|
2023-01-04 19:37:36 +08:00
|
|
|
if !ok {
|
2024-03-03 19:00:59 +08:00
|
|
|
if seg.lastExpireTime <= ts {
|
2023-01-04 19:37:36 +08:00
|
|
|
cntUnissued++
|
|
|
|
}
|
2024-03-03 19:00:59 +08:00
|
|
|
pendingIndexRows += seg.numRows
|
2023-01-04 19:37:36 +08:00
|
|
|
continue
|
|
|
|
}
|
2024-03-03 19:00:59 +08:00
|
|
|
if segIdx.GetState() != commonpb.IndexState_Finished {
|
|
|
|
pendingIndexRows += seg.numRows
|
2023-06-01 18:14:32 +08:00
|
|
|
}
|
2023-03-20 10:21:56 +08:00
|
|
|
|
2023-05-06 10:34:39 +08:00
|
|
|
// if realTime, calculate current statistics
|
|
|
|
// if not realTime, skip segments created after index create
|
2024-03-03 19:00:59 +08:00
|
|
|
if !realTime && seg.lastExpireTime > ts {
|
2023-01-04 19:37:36 +08:00
|
|
|
continue
|
|
|
|
}
|
2023-03-20 10:21:56 +08:00
|
|
|
|
2024-03-03 19:00:59 +08:00
|
|
|
switch segIdx.GetState() {
|
2023-01-04 19:37:36 +08:00
|
|
|
case commonpb.IndexState_IndexStateNone:
|
|
|
|
// can't to here
|
2024-03-03 19:00:59 +08:00
|
|
|
log.Warn("receive unexpected index state: IndexStateNone", zap.Int64("segmentID", segID))
|
2023-01-04 19:37:36 +08:00
|
|
|
cntNone++
|
|
|
|
case commonpb.IndexState_Unissued:
|
|
|
|
cntUnissued++
|
|
|
|
case commonpb.IndexState_InProgress:
|
|
|
|
cntInProgress++
|
|
|
|
case commonpb.IndexState_Finished:
|
|
|
|
cntFinished++
|
2024-03-03 19:00:59 +08:00
|
|
|
indexedRows += seg.numRows
|
2023-01-04 19:37:36 +08:00
|
|
|
case commonpb.IndexState_Failed:
|
|
|
|
cntFailed++
|
2024-03-03 19:00:59 +08:00
|
|
|
failReason += fmt.Sprintf("%d: %s;", segID, segIdx.FailReason)
|
2023-01-04 19:37:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-01 18:14:32 +08:00
|
|
|
if realTime {
|
|
|
|
indexInfo.IndexedRows = indexedRows
|
|
|
|
} else {
|
|
|
|
indexInfo.IndexedRows = s.countIndexedRows(indexInfo, segments)
|
|
|
|
}
|
2023-01-04 19:37:36 +08:00
|
|
|
indexInfo.TotalRows = totalRows
|
2023-06-01 18:14:32 +08:00
|
|
|
indexInfo.PendingIndexRows = pendingIndexRows
|
2023-01-04 19:37:36 +08:00
|
|
|
switch {
|
|
|
|
case cntFailed > 0:
|
|
|
|
indexInfo.State = commonpb.IndexState_Failed
|
|
|
|
indexInfo.IndexStateFailReason = failReason
|
2023-02-28 10:29:50 +08:00
|
|
|
case cntInProgress > 0 || cntUnissued > 0:
|
2023-01-04 19:37:36 +08:00
|
|
|
indexInfo.State = commonpb.IndexState_InProgress
|
2023-02-28 10:29:50 +08:00
|
|
|
case cntNone > 0:
|
|
|
|
indexInfo.State = commonpb.IndexState_IndexStateNone
|
|
|
|
default:
|
|
|
|
indexInfo.State = commonpb.IndexState_Finished
|
2023-01-04 19:37:36 +08:00
|
|
|
}
|
|
|
|
|
2024-05-15 14:37:34 +08:00
|
|
|
log.RatedInfo(60, "completeIndexInfo success", zap.Int64("collectionID", index.CollectionID), zap.Int64("indexID", index.IndexID),
|
2023-01-04 19:37:36 +08:00
|
|
|
zap.Int64("totalRows", indexInfo.TotalRows), zap.Int64("indexRows", indexInfo.IndexedRows),
|
2023-06-01 18:14:32 +08:00
|
|
|
zap.Int64("pendingIndexRows", indexInfo.PendingIndexRows),
|
2023-01-04 19:37:36 +08:00
|
|
|
zap.String("state", indexInfo.State.String()), zap.String("failReason", indexInfo.IndexStateFailReason))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetIndexBuildProgress get the index building progress by num rows.
|
2023-05-06 10:34:39 +08:00
|
|
|
// Deprecated
|
2023-01-11 14:35:40 +08:00
|
|
|
func (s *Server) GetIndexBuildProgress(ctx context.Context, req *indexpb.GetIndexBuildProgressRequest) (*indexpb.GetIndexBuildProgressResponse, error) {
|
2023-07-14 15:56:31 +08:00
|
|
|
log := log.Ctx(ctx).With(
|
2023-09-15 10:07:19 +08:00
|
|
|
zap.Int64("collectionID", req.GetCollectionID()),
|
2023-07-14 15:56:31 +08:00
|
|
|
)
|
|
|
|
log.Info("receive GetIndexBuildProgress request", zap.String("indexName", req.GetIndexName()))
|
2023-09-26 17:15:27 +08:00
|
|
|
|
|
|
|
if err := merr.CheckHealthy(s.GetStateCode()); err != nil {
|
|
|
|
log.Warn(msgDataCoordIsUnhealthy(paramtable.GetNodeID()), zap.Error(err))
|
2023-01-11 14:35:40 +08:00
|
|
|
return &indexpb.GetIndexBuildProgressResponse{
|
2023-09-26 17:15:27 +08:00
|
|
|
Status: merr.Status(err),
|
2023-01-04 19:37:36 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-03-04 16:56:59 +08:00
|
|
|
indexes := s.meta.indexMeta.GetIndexesForCollection(req.GetCollectionID(), req.GetIndexName())
|
2023-01-04 19:37:36 +08:00
|
|
|
if len(indexes) == 0 {
|
2023-09-26 17:15:27 +08:00
|
|
|
err := merr.WrapErrIndexNotFound(req.GetIndexName())
|
|
|
|
log.Warn("GetIndexBuildProgress fail", zap.String("indexName", req.IndexName), zap.Error(err))
|
2023-01-11 14:35:40 +08:00
|
|
|
return &indexpb.GetIndexBuildProgressResponse{
|
2023-09-26 17:15:27 +08:00
|
|
|
Status: merr.Status(err),
|
2023-01-04 19:37:36 +08:00
|
|
|
}, nil
|
|
|
|
}
|
2023-01-11 16:59:42 +08:00
|
|
|
|
|
|
|
if len(indexes) > 1 {
|
|
|
|
log.Warn(msgAmbiguousIndexName())
|
2023-09-26 17:15:27 +08:00
|
|
|
err := merr.WrapErrIndexDuplicate(req.GetIndexName())
|
2023-01-11 16:59:42 +08:00
|
|
|
return &indexpb.GetIndexBuildProgressResponse{
|
2023-09-26 17:15:27 +08:00
|
|
|
Status: merr.Status(err),
|
2023-01-11 16:59:42 +08:00
|
|
|
}, nil
|
|
|
|
}
|
2023-01-11 14:35:40 +08:00
|
|
|
indexInfo := &indexpb.IndexInfo{
|
2023-09-15 10:07:19 +08:00
|
|
|
CollectionID: req.GetCollectionID(),
|
2023-06-01 18:14:32 +08:00
|
|
|
IndexID: indexes[0].IndexID,
|
|
|
|
IndexedRows: 0,
|
|
|
|
TotalRows: 0,
|
|
|
|
PendingIndexRows: 0,
|
|
|
|
State: 0,
|
2023-01-04 19:37:36 +08:00
|
|
|
}
|
2024-03-03 19:00:59 +08:00
|
|
|
|
|
|
|
// The total rows of all indexes should be based on the current perspective
|
2024-05-08 21:37:29 +08:00
|
|
|
segments := s.selectSegmentIndexesStats(WithCollection(req.GetCollectionID()), SegmentFilterFunc(func(info *SegmentInfo) bool {
|
2024-09-02 14:19:03 +08:00
|
|
|
return info.GetLevel() != datapb.SegmentLevel_L0 && (isFlush(info) || info.GetState() == commonpb.SegmentState_Dropped)
|
2024-05-08 21:37:29 +08:00
|
|
|
}))
|
2024-03-03 19:00:59 +08:00
|
|
|
|
|
|
|
s.completeIndexInfo(indexInfo, indexes[0], segments, false, indexes[0].CreateTime)
|
2023-07-25 10:05:00 +08:00
|
|
|
log.Info("GetIndexBuildProgress success", zap.Int64("collectionID", req.GetCollectionID()),
|
|
|
|
zap.String("indexName", req.GetIndexName()))
|
2023-01-11 14:35:40 +08:00
|
|
|
return &indexpb.GetIndexBuildProgressResponse{
|
2023-10-11 21:01:35 +08:00
|
|
|
Status: merr.Success(),
|
2023-06-01 18:14:32 +08:00
|
|
|
IndexedRows: indexInfo.IndexedRows,
|
|
|
|
TotalRows: indexInfo.TotalRows,
|
|
|
|
PendingIndexRows: indexInfo.PendingIndexRows,
|
2023-01-04 19:37:36 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-03-03 19:00:59 +08:00
|
|
|
// indexStats just for indexing statistics.
|
|
|
|
// Please use it judiciously.
|
|
|
|
type indexStats struct {
|
|
|
|
ID int64
|
|
|
|
numRows int64
|
|
|
|
compactionFrom []int64
|
|
|
|
indexStates map[int64]*indexpb.SegmentIndexState
|
|
|
|
state commonpb.SegmentState
|
|
|
|
lastExpireTime uint64
|
|
|
|
}
|
|
|
|
|
2023-01-04 19:37:36 +08:00
|
|
|
// DescribeIndex describe the index info of the collection.
|
2023-01-11 14:35:40 +08:00
|
|
|
func (s *Server) DescribeIndex(ctx context.Context, req *indexpb.DescribeIndexRequest) (*indexpb.DescribeIndexResponse, error) {
|
2023-07-14 15:56:31 +08:00
|
|
|
log := log.Ctx(ctx).With(
|
2023-09-15 10:07:19 +08:00
|
|
|
zap.Int64("collectionID", req.GetCollectionID()),
|
2023-10-08 21:23:32 +08:00
|
|
|
zap.String("indexName", req.GetIndexName()),
|
|
|
|
zap.Uint64("timestamp", req.GetTimestamp()),
|
2023-07-14 15:56:31 +08:00
|
|
|
)
|
2023-09-26 17:15:27 +08:00
|
|
|
if err := merr.CheckHealthy(s.GetStateCode()); err != nil {
|
|
|
|
log.Warn(msgDataCoordIsUnhealthy(paramtable.GetNodeID()), zap.Error(err))
|
2023-01-11 14:35:40 +08:00
|
|
|
return &indexpb.DescribeIndexResponse{
|
2023-09-26 17:15:27 +08:00
|
|
|
Status: merr.Status(err),
|
2023-01-04 19:37:36 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-03-04 16:56:59 +08:00
|
|
|
indexes := s.meta.indexMeta.GetIndexesForCollection(req.GetCollectionID(), req.GetIndexName())
|
2023-01-04 19:37:36 +08:00
|
|
|
if len(indexes) == 0 {
|
2023-09-26 17:15:27 +08:00
|
|
|
err := merr.WrapErrIndexNotFound(req.GetIndexName())
|
2024-05-21 19:27:39 +08:00
|
|
|
log.RatedWarn(60, "DescribeIndex fail", zap.Error(err))
|
2023-01-11 14:35:40 +08:00
|
|
|
return &indexpb.DescribeIndexResponse{
|
2023-09-26 17:15:27 +08:00
|
|
|
Status: merr.Status(err),
|
2023-01-04 19:37:36 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// The total rows of all indexes should be based on the current perspective
|
2024-05-08 21:37:29 +08:00
|
|
|
segments := s.selectSegmentIndexesStats(WithCollection(req.GetCollectionID()), SegmentFilterFunc(func(info *SegmentInfo) bool {
|
2024-09-02 14:19:03 +08:00
|
|
|
return info.GetLevel() != datapb.SegmentLevel_L0 && (isFlush(info) || info.GetState() == commonpb.SegmentState_Dropped)
|
2024-05-08 21:37:29 +08:00
|
|
|
}))
|
2024-03-03 19:00:59 +08:00
|
|
|
|
2023-01-11 14:35:40 +08:00
|
|
|
indexInfos := make([]*indexpb.IndexInfo, 0)
|
2023-01-04 19:37:36 +08:00
|
|
|
for _, index := range indexes {
|
2023-01-11 14:35:40 +08:00
|
|
|
indexInfo := &indexpb.IndexInfo{
|
2023-01-04 19:37:36 +08:00
|
|
|
CollectionID: index.CollectionID,
|
|
|
|
FieldID: index.FieldID,
|
|
|
|
IndexName: index.IndexName,
|
|
|
|
IndexID: index.IndexID,
|
|
|
|
TypeParams: index.TypeParams,
|
|
|
|
IndexParams: index.IndexParams,
|
|
|
|
IndexedRows: 0,
|
|
|
|
TotalRows: 0,
|
|
|
|
State: 0,
|
|
|
|
IndexStateFailReason: "",
|
|
|
|
IsAutoIndex: index.IsAutoIndex,
|
|
|
|
UserIndexParams: index.UserIndexParams,
|
|
|
|
}
|
2023-07-25 10:05:00 +08:00
|
|
|
createTs := index.CreateTime
|
|
|
|
if req.GetTimestamp() != 0 {
|
|
|
|
createTs = req.GetTimestamp()
|
|
|
|
}
|
|
|
|
s.completeIndexInfo(indexInfo, index, segments, false, createTs)
|
2023-01-04 19:37:36 +08:00
|
|
|
indexInfos = append(indexInfos, indexInfo)
|
|
|
|
}
|
2023-01-11 14:35:40 +08:00
|
|
|
return &indexpb.DescribeIndexResponse{
|
2023-10-11 21:01:35 +08:00
|
|
|
Status: merr.Success(),
|
2023-01-04 19:37:36 +08:00
|
|
|
IndexInfos: indexInfos,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-05-06 10:34:39 +08:00
|
|
|
// GetIndexStatistics get the statistics of the index. DescribeIndex doesn't contain statistics.
|
|
|
|
func (s *Server) GetIndexStatistics(ctx context.Context, req *indexpb.GetIndexStatisticsRequest) (*indexpb.GetIndexStatisticsResponse, error) {
|
2023-07-14 15:56:31 +08:00
|
|
|
log := log.Ctx(ctx).With(
|
2023-09-15 10:07:19 +08:00
|
|
|
zap.Int64("collectionID", req.GetCollectionID()),
|
2023-07-14 15:56:31 +08:00
|
|
|
)
|
|
|
|
log.Info("receive GetIndexStatistics request", zap.String("indexName", req.GetIndexName()))
|
2023-09-26 17:15:27 +08:00
|
|
|
if err := merr.CheckHealthy(s.GetStateCode()); err != nil {
|
|
|
|
log.Warn(msgDataCoordIsUnhealthy(paramtable.GetNodeID()), zap.Error(err))
|
2023-05-06 10:34:39 +08:00
|
|
|
return &indexpb.GetIndexStatisticsResponse{
|
2023-09-26 17:15:27 +08:00
|
|
|
Status: merr.Status(err),
|
2023-05-06 10:34:39 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-03-04 16:56:59 +08:00
|
|
|
indexes := s.meta.indexMeta.GetIndexesForCollection(req.GetCollectionID(), req.GetIndexName())
|
2023-05-06 10:34:39 +08:00
|
|
|
if len(indexes) == 0 {
|
2023-09-26 17:15:27 +08:00
|
|
|
err := merr.WrapErrIndexNotFound(req.GetIndexName())
|
2023-07-14 15:56:31 +08:00
|
|
|
log.Warn("GetIndexStatistics fail",
|
2023-09-15 10:07:19 +08:00
|
|
|
zap.String("indexName", req.GetIndexName()),
|
2023-09-26 17:15:27 +08:00
|
|
|
zap.Error(err))
|
2023-05-06 10:34:39 +08:00
|
|
|
return &indexpb.GetIndexStatisticsResponse{
|
2023-09-26 17:15:27 +08:00
|
|
|
Status: merr.Status(err),
|
2023-05-06 10:34:39 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// The total rows of all indexes should be based on the current perspective
|
2024-05-08 21:37:29 +08:00
|
|
|
segments := s.selectSegmentIndexesStats(WithCollection(req.GetCollectionID()), SegmentFilterFunc(func(info *SegmentInfo) bool {
|
2024-09-02 14:19:03 +08:00
|
|
|
return info.GetLevel() != datapb.SegmentLevel_L0 && (isFlush(info) || info.GetState() == commonpb.SegmentState_Dropped)
|
2024-05-08 21:37:29 +08:00
|
|
|
}))
|
2024-03-03 19:00:59 +08:00
|
|
|
|
2023-05-06 10:34:39 +08:00
|
|
|
indexInfos := make([]*indexpb.IndexInfo, 0)
|
|
|
|
for _, index := range indexes {
|
|
|
|
indexInfo := &indexpb.IndexInfo{
|
|
|
|
CollectionID: index.CollectionID,
|
|
|
|
FieldID: index.FieldID,
|
|
|
|
IndexName: index.IndexName,
|
|
|
|
IndexID: index.IndexID,
|
|
|
|
TypeParams: index.TypeParams,
|
|
|
|
IndexParams: index.IndexParams,
|
|
|
|
IndexedRows: 0,
|
|
|
|
TotalRows: 0,
|
|
|
|
State: 0,
|
|
|
|
IndexStateFailReason: "",
|
|
|
|
IsAutoIndex: index.IsAutoIndex,
|
|
|
|
UserIndexParams: index.UserIndexParams,
|
|
|
|
}
|
2023-07-25 10:05:00 +08:00
|
|
|
s.completeIndexInfo(indexInfo, index, segments, true, index.CreateTime)
|
2023-05-06 10:34:39 +08:00
|
|
|
indexInfos = append(indexInfos, indexInfo)
|
|
|
|
}
|
2023-07-14 15:56:31 +08:00
|
|
|
log.Debug("GetIndexStatisticsResponse success",
|
2023-05-06 10:34:39 +08:00
|
|
|
zap.String("indexName", req.GetIndexName()))
|
|
|
|
return &indexpb.GetIndexStatisticsResponse{
|
2023-10-11 21:01:35 +08:00
|
|
|
Status: merr.Success(),
|
2023-05-06 10:34:39 +08:00
|
|
|
IndexInfos: indexInfos,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-01-04 19:37:36 +08:00
|
|
|
// DropIndex deletes indexes based on IndexName. One IndexName corresponds to the index of an entire column. A column is
|
|
|
|
// divided into many segments, and each segment corresponds to an IndexBuildID. DataCoord uses IndexBuildID to record
|
|
|
|
// index tasks.
|
2023-01-11 14:35:40 +08:00
|
|
|
func (s *Server) DropIndex(ctx context.Context, req *indexpb.DropIndexRequest) (*commonpb.Status, error) {
|
2023-07-14 15:56:31 +08:00
|
|
|
log := log.Ctx(ctx).With(
|
2023-09-15 10:07:19 +08:00
|
|
|
zap.Int64("collectionID", req.GetCollectionID()),
|
2023-07-14 15:56:31 +08:00
|
|
|
)
|
|
|
|
log.Info("receive DropIndex request",
|
2023-01-04 19:37:36 +08:00
|
|
|
zap.Int64s("partitionIDs", req.GetPartitionIDs()), zap.String("indexName", req.GetIndexName()),
|
|
|
|
zap.Bool("drop all indexes", req.GetDropAll()))
|
|
|
|
|
2023-09-26 17:15:27 +08:00
|
|
|
if err := merr.CheckHealthy(s.GetStateCode()); err != nil {
|
|
|
|
log.Warn(msgDataCoordIsUnhealthy(paramtable.GetNodeID()), zap.Error(err))
|
|
|
|
return merr.Status(err), nil
|
|
|
|
}
|
2023-01-04 19:37:36 +08:00
|
|
|
|
2024-03-04 16:56:59 +08:00
|
|
|
indexes := s.meta.indexMeta.GetIndexesForCollection(req.GetCollectionID(), req.GetIndexName())
|
2023-01-04 19:37:36 +08:00
|
|
|
if len(indexes) == 0 {
|
|
|
|
log.Info(fmt.Sprintf("there is no index on collection: %d with the index name: %s", req.CollectionID, req.IndexName))
|
2023-10-11 21:01:35 +08:00
|
|
|
return merr.Success(), nil
|
2023-01-04 19:37:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if !req.GetDropAll() && len(indexes) > 1 {
|
|
|
|
log.Warn(msgAmbiguousIndexName())
|
2023-09-26 17:15:27 +08:00
|
|
|
err := merr.WrapErrIndexDuplicate(req.GetIndexName())
|
|
|
|
return merr.Status(err), nil
|
2023-01-04 19:37:36 +08:00
|
|
|
}
|
|
|
|
indexIDs := make([]UniqueID, 0)
|
|
|
|
for _, index := range indexes {
|
|
|
|
indexIDs = append(indexIDs, index.IndexID)
|
|
|
|
}
|
2023-09-27 10:33:27 +08:00
|
|
|
// Compatibility logic. To prevent the index on the corresponding segments
|
|
|
|
// from being dropped at the same time when dropping_partition in version 2.1
|
2023-01-04 19:37:36 +08:00
|
|
|
if len(req.GetPartitionIDs()) == 0 {
|
|
|
|
// drop collection index
|
2024-03-04 16:56:59 +08:00
|
|
|
err := s.meta.indexMeta.MarkIndexAsDeleted(req.GetCollectionID(), indexIDs)
|
2023-01-04 19:37:36 +08:00
|
|
|
if err != nil {
|
2023-07-14 15:56:31 +08:00
|
|
|
log.Warn("DropIndex fail", zap.String("indexName", req.IndexName), zap.Error(err))
|
2023-09-26 17:15:27 +08:00
|
|
|
return merr.Status(err), nil
|
2023-01-04 19:37:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-15 10:07:19 +08:00
|
|
|
log.Debug("DropIndex success", zap.Int64s("partitionIDs", req.GetPartitionIDs()),
|
|
|
|
zap.String("indexName", req.GetIndexName()), zap.Int64s("indexIDs", indexIDs))
|
2023-10-11 21:01:35 +08:00
|
|
|
return merr.Success(), nil
|
2023-01-04 19:37:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetIndexInfos gets the index file paths for segment from DataCoord.
|
2023-01-11 14:35:40 +08:00
|
|
|
func (s *Server) GetIndexInfos(ctx context.Context, req *indexpb.GetIndexInfoRequest) (*indexpb.GetIndexInfoResponse, error) {
|
2023-07-14 15:56:31 +08:00
|
|
|
log := log.Ctx(ctx).With(
|
2023-09-15 10:07:19 +08:00
|
|
|
zap.Int64("collectionID", req.GetCollectionID()),
|
2023-07-14 15:56:31 +08:00
|
|
|
)
|
2023-09-26 17:15:27 +08:00
|
|
|
|
|
|
|
if err := merr.CheckHealthy(s.GetStateCode()); err != nil {
|
|
|
|
log.Warn(msgDataCoordIsUnhealthy(paramtable.GetNodeID()), zap.Error(err))
|
2023-01-11 14:35:40 +08:00
|
|
|
return &indexpb.GetIndexInfoResponse{
|
2023-09-26 17:15:27 +08:00
|
|
|
Status: merr.Status(err),
|
2023-01-04 19:37:36 +08:00
|
|
|
}, nil
|
|
|
|
}
|
2023-01-11 14:35:40 +08:00
|
|
|
ret := &indexpb.GetIndexInfoResponse{
|
2023-10-11 21:01:35 +08:00
|
|
|
Status: merr.Success(),
|
2023-01-11 14:35:40 +08:00
|
|
|
SegmentInfo: map[int64]*indexpb.SegmentInfo{},
|
2023-01-04 19:37:36 +08:00
|
|
|
}
|
|
|
|
|
2023-09-15 10:07:19 +08:00
|
|
|
for _, segID := range req.GetSegmentIDs() {
|
2024-03-04 16:56:59 +08:00
|
|
|
segIdxes := s.meta.indexMeta.GetSegmentIndexes(req.GetCollectionID(), segID)
|
2023-01-11 14:35:40 +08:00
|
|
|
ret.SegmentInfo[segID] = &indexpb.SegmentInfo{
|
2023-09-15 10:07:19 +08:00
|
|
|
CollectionID: req.GetCollectionID(),
|
2023-01-04 19:37:36 +08:00
|
|
|
SegmentID: segID,
|
|
|
|
EnableIndex: false,
|
2023-01-11 14:35:40 +08:00
|
|
|
IndexInfos: make([]*indexpb.IndexFilePathInfo, 0),
|
2023-01-04 19:37:36 +08:00
|
|
|
}
|
|
|
|
if len(segIdxes) != 0 {
|
|
|
|
ret.SegmentInfo[segID].EnableIndex = true
|
|
|
|
for _, segIdx := range segIdxes {
|
|
|
|
if segIdx.IndexState == commonpb.IndexState_Finished {
|
|
|
|
indexFilePaths := metautil.BuildSegmentIndexFilePaths(s.meta.chunkManager.RootPath(), segIdx.BuildID, segIdx.IndexVersion,
|
|
|
|
segIdx.PartitionID, segIdx.SegmentID, segIdx.IndexFileKeys)
|
2024-03-04 16:56:59 +08:00
|
|
|
indexParams := s.meta.indexMeta.GetIndexParams(segIdx.CollectionID, segIdx.IndexID)
|
|
|
|
indexParams = append(indexParams, s.meta.indexMeta.GetTypeParams(segIdx.CollectionID, segIdx.IndexID)...)
|
2023-01-04 19:37:36 +08:00
|
|
|
ret.SegmentInfo[segID].IndexInfos = append(ret.SegmentInfo[segID].IndexInfos,
|
2023-01-11 14:35:40 +08:00
|
|
|
&indexpb.IndexFilePathInfo{
|
2023-09-25 21:39:27 +08:00
|
|
|
SegmentID: segID,
|
2024-03-04 16:56:59 +08:00
|
|
|
FieldID: s.meta.indexMeta.GetFieldIDByIndexID(segIdx.CollectionID, segIdx.IndexID),
|
2023-09-25 21:39:27 +08:00
|
|
|
IndexID: segIdx.IndexID,
|
|
|
|
BuildID: segIdx.BuildID,
|
2024-03-04 16:56:59 +08:00
|
|
|
IndexName: s.meta.indexMeta.GetIndexNameByID(segIdx.CollectionID, segIdx.IndexID),
|
2023-09-25 21:39:27 +08:00
|
|
|
IndexParams: indexParams,
|
|
|
|
IndexFilePaths: indexFilePaths,
|
|
|
|
SerializedSize: segIdx.IndexSize,
|
|
|
|
IndexVersion: segIdx.IndexVersion,
|
|
|
|
NumRows: segIdx.NumRows,
|
|
|
|
CurrentIndexVersion: segIdx.CurrentIndexVersion,
|
2023-01-04 19:37:36 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-14 15:56:31 +08:00
|
|
|
log.Debug("GetIndexInfos successfully", zap.String("indexName", req.GetIndexName()))
|
2023-01-04 19:37:36 +08:00
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
2024-03-07 17:37:01 +08:00
|
|
|
|
|
|
|
// ListIndexes returns all indexes created on provided collection.
|
|
|
|
func (s *Server) ListIndexes(ctx context.Context, req *indexpb.ListIndexesRequest) (*indexpb.ListIndexesResponse, error) {
|
|
|
|
log := log.Ctx(ctx).With(
|
|
|
|
zap.Int64("collectionID", req.GetCollectionID()),
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := merr.CheckHealthy(s.GetStateCode()); err != nil {
|
|
|
|
log.Warn(msgDataCoordIsUnhealthy(paramtable.GetNodeID()), zap.Error(err))
|
|
|
|
return &indexpb.ListIndexesResponse{
|
|
|
|
Status: merr.Status(err),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
indexes := s.meta.indexMeta.GetIndexesForCollection(req.GetCollectionID(), "")
|
|
|
|
|
|
|
|
indexInfos := lo.Map(indexes, func(index *model.Index, _ int) *indexpb.IndexInfo {
|
|
|
|
return &indexpb.IndexInfo{
|
|
|
|
CollectionID: index.CollectionID,
|
|
|
|
FieldID: index.FieldID,
|
|
|
|
IndexName: index.IndexName,
|
|
|
|
IndexID: index.IndexID,
|
|
|
|
TypeParams: index.TypeParams,
|
|
|
|
IndexParams: index.IndexParams,
|
|
|
|
IsAutoIndex: index.IsAutoIndex,
|
|
|
|
UserIndexParams: index.UserIndexParams,
|
|
|
|
}
|
|
|
|
})
|
2024-05-29 19:15:43 +08:00
|
|
|
log.Debug("List index success")
|
2024-03-07 17:37:01 +08:00
|
|
|
return &indexpb.ListIndexesResponse{
|
|
|
|
Status: merr.Success(),
|
|
|
|
IndexInfos: indexInfos,
|
|
|
|
}, nil
|
|
|
|
}
|