2021-04-19 13:50:12 +08:00
|
|
|
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed 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.
|
|
|
|
|
2021-01-15 14:38:36 +08:00
|
|
|
package indexnode
|
2020-12-10 17:55:55 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-08 10:09:48 +08:00
|
|
|
"errors"
|
2021-06-30 19:46:14 +08:00
|
|
|
"fmt"
|
2021-03-26 11:19:02 +08:00
|
|
|
"runtime"
|
2020-12-22 08:14:36 +08:00
|
|
|
"strconv"
|
2020-12-13 06:48:05 +08:00
|
|
|
|
2021-05-27 22:24:29 +08:00
|
|
|
"github.com/golang/protobuf/proto"
|
2021-03-10 09:56:09 +08:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/kv"
|
2021-05-27 22:24:29 +08:00
|
|
|
etcdkv "github.com/milvus-io/milvus/internal/kv/etcd"
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/commonpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/indexpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/storage"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/funcutil"
|
2021-06-30 19:46:14 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/retry"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/timerecord"
|
2021-07-22 11:40:11 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/trace"
|
2020-12-10 17:55:55 +08:00
|
|
|
)
|
|
|
|
|
2021-02-07 21:32:37 +08:00
|
|
|
const (
|
2021-02-23 09:58:06 +08:00
|
|
|
paramsKeyToParse = "params"
|
|
|
|
IndexBuildTaskName = "IndexBuildTask"
|
2021-02-07 21:32:37 +08:00
|
|
|
)
|
|
|
|
|
2020-12-10 17:55:55 +08:00
|
|
|
type task interface {
|
2021-02-23 09:58:06 +08:00
|
|
|
Ctx() context.Context
|
|
|
|
ID() UniqueID // return ReqID
|
|
|
|
Name() string
|
2020-12-10 17:55:55 +08:00
|
|
|
SetID(uid UniqueID) // set ReqID
|
2021-02-23 09:58:06 +08:00
|
|
|
PreExecute(ctx context.Context) error
|
|
|
|
Execute(ctx context.Context) error
|
|
|
|
PostExecute(ctx context.Context) error
|
2020-12-10 17:55:55 +08:00
|
|
|
WaitToFinish() error
|
|
|
|
Notify(err error)
|
2020-12-13 06:48:05 +08:00
|
|
|
OnEnqueue() error
|
2021-02-27 10:45:03 +08:00
|
|
|
SetError(err error)
|
2020-12-10 17:55:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type BaseTask struct {
|
2021-01-26 09:38:40 +08:00
|
|
|
done chan error
|
|
|
|
ctx context.Context
|
|
|
|
id UniqueID
|
2021-02-27 10:45:03 +08:00
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bt *BaseTask) SetError(err error) {
|
|
|
|
bt.err = err
|
2020-12-10 17:55:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bt *BaseTask) ID() UniqueID {
|
|
|
|
return bt.id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bt *BaseTask) setID(id UniqueID) {
|
|
|
|
bt.id = id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bt *BaseTask) WaitToFinish() error {
|
2020-12-13 06:48:05 +08:00
|
|
|
select {
|
|
|
|
case <-bt.ctx.Done():
|
|
|
|
return errors.New("timeout")
|
|
|
|
case err := <-bt.done:
|
|
|
|
return err
|
2020-12-10 17:55:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bt *BaseTask) Notify(err error) {
|
|
|
|
bt.done <- err
|
|
|
|
}
|
|
|
|
|
2021-01-26 09:38:40 +08:00
|
|
|
type IndexBuildTask struct {
|
2020-12-10 17:55:55 +08:00
|
|
|
BaseTask
|
2021-06-15 10:19:38 +08:00
|
|
|
index Index
|
|
|
|
kv kv.BaseKV
|
|
|
|
etcdKV *etcdkv.EtcdKV
|
|
|
|
savePaths []string
|
|
|
|
req *indexpb.CreateIndexRequest
|
|
|
|
nodeID UniqueID
|
2021-01-26 09:38:40 +08:00
|
|
|
}
|
|
|
|
|
2021-02-23 09:58:06 +08:00
|
|
|
func (it *IndexBuildTask) Ctx() context.Context {
|
|
|
|
return it.ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *IndexBuildTask) ID() UniqueID {
|
|
|
|
return it.id
|
2020-12-10 17:55:55 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 09:38:40 +08:00
|
|
|
func (it *IndexBuildTask) SetID(ID UniqueID) {
|
2020-12-13 06:48:05 +08:00
|
|
|
it.BaseTask.setID(ID)
|
|
|
|
}
|
2020-12-10 17:55:55 +08:00
|
|
|
|
2021-02-23 09:58:06 +08:00
|
|
|
func (bt *BaseTask) Name() string {
|
|
|
|
return IndexBuildTaskName
|
|
|
|
}
|
|
|
|
|
2021-01-26 09:38:40 +08:00
|
|
|
func (it *IndexBuildTask) OnEnqueue() error {
|
2021-03-12 14:22:09 +08:00
|
|
|
it.SetID(it.req.IndexBuildID)
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexNode IndexBuilderTask Enqueue", zap.Int64("TaskID", it.ID()))
|
2020-12-10 17:55:55 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-23 09:24:10 +08:00
|
|
|
func (it *IndexBuildTask) checkIndexMeta(ctx context.Context, pre bool) error {
|
2021-05-27 22:24:29 +08:00
|
|
|
fn := func() error {
|
|
|
|
indexMeta := indexpb.IndexMeta{}
|
|
|
|
_, values, versions, err := it.etcdKV.LoadWithPrefix2(it.req.MetaPath)
|
|
|
|
if err != nil {
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexNode checkIndexMeta", zap.Any("load meta error with path", it.req.MetaPath),
|
|
|
|
zap.Error(err), zap.Any("pre", pre))
|
2021-05-27 22:24:29 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexNode checkIndexMeta load meta success", zap.Any("path", it.req.MetaPath), zap.Any("pre", pre))
|
2021-05-27 22:24:29 +08:00
|
|
|
err = proto.UnmarshalText(values[0], &indexMeta)
|
|
|
|
if err != nil {
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexNode checkIndexMeta Unmarshal", zap.Error(err))
|
2021-05-27 22:24:29 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexNode checkIndexMeta Unmarshal success", zap.Any("IndexMeta", indexMeta))
|
2021-05-27 22:24:29 +08:00
|
|
|
if indexMeta.Version > it.req.Version || indexMeta.State == commonpb.IndexState_Finished {
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexNode checkIndexMeta Notify build index this version is not the latest version", zap.Any("version", it.req.Version))
|
2021-05-27 22:24:29 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if indexMeta.MarkDeleted {
|
|
|
|
indexMeta.State = commonpb.IndexState_Finished
|
|
|
|
v := proto.MarshalTextString(&indexMeta)
|
|
|
|
err := it.etcdKV.CompareVersionAndSwap(it.req.MetaPath, versions[0], v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-23 10:44:12 +08:00
|
|
|
errMsg := fmt.Sprintf("the index has been deleted with indexBuildID %d", indexMeta.IndexBuildID)
|
|
|
|
log.Warn(errMsg)
|
|
|
|
return fmt.Errorf(errMsg)
|
2021-05-27 22:24:29 +08:00
|
|
|
}
|
|
|
|
if pre {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
indexMeta.IndexFilePaths = it.savePaths
|
|
|
|
indexMeta.State = commonpb.IndexState_Finished
|
|
|
|
if it.err != nil {
|
2021-07-23 10:44:12 +08:00
|
|
|
log.Debug("IndexNode CreateIndex Failed", zap.Int64("IndexBuildID", indexMeta.IndexBuildID), zap.Any("err", err))
|
2021-05-27 22:24:29 +08:00
|
|
|
indexMeta.State = commonpb.IndexState_Failed
|
2021-07-29 14:47:22 +08:00
|
|
|
indexMeta.FailReason = it.err.Error()
|
2021-05-27 22:24:29 +08:00
|
|
|
}
|
2021-07-23 10:44:12 +08:00
|
|
|
log.Debug("IndexNode", zap.Int64("indexBuildID", indexMeta.IndexBuildID), zap.Any("IndexState", indexMeta.State))
|
2021-05-27 22:24:29 +08:00
|
|
|
err = it.etcdKV.CompareVersionAndSwap(it.req.MetaPath, versions[0],
|
|
|
|
proto.MarshalTextString(&indexMeta))
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexNode checkIndexMeta CompareVersionAndSwap", zap.Error(err))
|
2021-05-27 22:24:29 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-23 09:24:10 +08:00
|
|
|
err := retry.Do(ctx, fn, retry.Attempts(3))
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexNode checkIndexMeta final", zap.Error(err))
|
2021-05-27 22:24:29 +08:00
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:58:06 +08:00
|
|
|
func (it *IndexBuildTask) PreExecute(ctx context.Context) error {
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexNode IndexBuildTask preExecute...")
|
2021-07-22 11:40:11 +08:00
|
|
|
sp, ctx := trace.StartSpanFromContextWithOperationName(ctx, "CreateIndex-PreExecute")
|
|
|
|
defer sp.Finish()
|
2021-06-23 09:24:10 +08:00
|
|
|
return it.checkIndexMeta(ctx, true)
|
2020-12-10 17:55:55 +08:00
|
|
|
}
|
|
|
|
|
2021-02-23 09:58:06 +08:00
|
|
|
func (it *IndexBuildTask) PostExecute(ctx context.Context) error {
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexNode IndexBuildTask PostExecute...")
|
2021-07-22 11:40:11 +08:00
|
|
|
sp, _ := trace.StartSpanFromContextWithOperationName(ctx, "CreateIndex-PostExecute")
|
|
|
|
defer sp.Finish()
|
2021-02-27 10:45:03 +08:00
|
|
|
|
2021-06-23 09:24:10 +08:00
|
|
|
return it.checkIndexMeta(ctx, false)
|
2020-12-13 06:48:05 +08:00
|
|
|
}
|
|
|
|
|
2021-02-23 09:58:06 +08:00
|
|
|
func (it *IndexBuildTask) Execute(ctx context.Context) error {
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexNode IndexBuildTask Execute ...")
|
2021-07-22 11:40:11 +08:00
|
|
|
sp, _ := trace.StartSpanFromContextWithOperationName(ctx, "CreateIndex-Execute")
|
|
|
|
defer sp.Finish()
|
2021-06-30 19:46:14 +08:00
|
|
|
tr := timerecord.NewTimeRecorder(fmt.Sprintf("IndexBuildTask %d", it.req.IndexBuildID))
|
2021-01-26 09:38:40 +08:00
|
|
|
var err error
|
2021-02-07 21:32:37 +08:00
|
|
|
|
2020-12-22 08:14:36 +08:00
|
|
|
typeParams := make(map[string]string)
|
2021-03-12 14:22:09 +08:00
|
|
|
for _, kvPair := range it.req.GetTypeParams() {
|
2020-12-22 08:14:36 +08:00
|
|
|
key, value := kvPair.GetKey(), kvPair.GetValue()
|
|
|
|
_, ok := typeParams[key]
|
|
|
|
if ok {
|
|
|
|
return errors.New("duplicated key in type params")
|
|
|
|
}
|
2021-02-07 21:32:37 +08:00
|
|
|
if key == paramsKeyToParse {
|
|
|
|
params, err := funcutil.ParseIndexParamsMap(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for pk, pv := range params {
|
|
|
|
typeParams[pk] = pv
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
typeParams[key] = value
|
|
|
|
}
|
2020-12-22 08:14:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
indexParams := make(map[string]string)
|
2021-03-12 14:22:09 +08:00
|
|
|
for _, kvPair := range it.req.GetIndexParams() {
|
2020-12-22 08:14:36 +08:00
|
|
|
key, value := kvPair.GetKey(), kvPair.GetValue()
|
|
|
|
_, ok := indexParams[key]
|
|
|
|
if ok {
|
|
|
|
return errors.New("duplicated key in index params")
|
|
|
|
}
|
2021-02-07 21:32:37 +08:00
|
|
|
if key == paramsKeyToParse {
|
|
|
|
params, err := funcutil.ParseIndexParamsMap(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for pk, pv := range params {
|
|
|
|
indexParams[pk] = pv
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
indexParams[key] = value
|
|
|
|
}
|
2020-12-22 08:14:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
it.index, err = NewCIndex(typeParams, indexParams)
|
|
|
|
if err != nil {
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Error("IndexNode IndexBuildTask Execute NewCIndex failed", zap.Error(err))
|
2020-12-22 08:14:36 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-02-22 18:33:40 +08:00
|
|
|
defer func() {
|
|
|
|
err = it.index.Delete()
|
|
|
|
if err != nil {
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Warn("IndexNode IndexBuildTask Execute CIndexDelete Failed", zap.Error(err))
|
2021-02-22 18:33:40 +08:00
|
|
|
}
|
|
|
|
}()
|
2020-12-22 08:14:36 +08:00
|
|
|
|
|
|
|
getKeyByPathNaive := func(path string) string {
|
|
|
|
// splitElements := strings.Split(path, "/")
|
|
|
|
// return splitElements[len(splitElements)-1]
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
getValueByPath := func(path string) ([]byte, error) {
|
|
|
|
data, err := it.kv.Load(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return []byte(data), nil
|
|
|
|
}
|
|
|
|
getBlobByPath := func(path string) (*Blob, error) {
|
|
|
|
value, err := getValueByPath(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Blob{
|
|
|
|
Key: getKeyByPathNaive(path),
|
|
|
|
Value: value,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
getStorageBlobs := func(blobs []*Blob) []*storage.Blob {
|
2020-12-25 11:10:31 +08:00
|
|
|
return blobs
|
2020-12-22 08:14:36 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
toLoadDataPaths := it.req.GetDataPaths()
|
2021-03-26 11:19:02 +08:00
|
|
|
keys := make([]string, len(toLoadDataPaths))
|
|
|
|
blobs := make([]*Blob, len(toLoadDataPaths))
|
|
|
|
|
|
|
|
loadKey := func(idx int) error {
|
|
|
|
keys[idx] = getKeyByPathNaive(toLoadDataPaths[idx])
|
|
|
|
blob, err := getBlobByPath(toLoadDataPaths[idx])
|
2020-12-22 08:14:36 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-26 11:19:02 +08:00
|
|
|
|
|
|
|
blobs[idx] = blob
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err = funcutil.ProcessFuncParallel(len(toLoadDataPaths), runtime.NumCPU(), loadKey, "loadKey")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-12-22 08:14:36 +08:00
|
|
|
}
|
2021-07-23 10:44:12 +08:00
|
|
|
log.Debug("IndexNode load data success")
|
2021-06-30 19:46:14 +08:00
|
|
|
tr.Record("loadKey done")
|
2020-12-22 08:14:36 +08:00
|
|
|
|
|
|
|
storageBlobs := getStorageBlobs(blobs)
|
|
|
|
var insertCodec storage.InsertCodec
|
2021-02-23 14:35:00 +08:00
|
|
|
defer insertCodec.Close()
|
2021-01-15 14:38:36 +08:00
|
|
|
partitionID, segmentID, insertData, err2 := insertCodec.Deserialize(storageBlobs)
|
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
2020-12-22 08:14:36 +08:00
|
|
|
if len(insertData.Data) != 1 {
|
|
|
|
return errors.New("we expect only one field in deserialized insert data")
|
|
|
|
}
|
2021-06-30 19:46:14 +08:00
|
|
|
tr.Record("deserialize storage blobs done")
|
2020-12-22 08:14:36 +08:00
|
|
|
|
|
|
|
for _, value := range insertData.Data {
|
|
|
|
// TODO: BinaryVectorFieldData
|
2021-01-07 15:39:20 +08:00
|
|
|
floatVectorFieldData, fOk := value.(*storage.FloatVectorFieldData)
|
|
|
|
if fOk {
|
|
|
|
err = it.index.BuildFloatVecIndexWithoutIds(floatVectorFieldData.Data)
|
|
|
|
if err != nil {
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Error("IndexNode BuildFloatVecIndexWithoutIds failed", zap.Error(err))
|
2021-01-07 15:39:20 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-06-30 19:46:14 +08:00
|
|
|
tr.Record("build float vector index done")
|
2021-01-07 14:56:17 +08:00
|
|
|
}
|
|
|
|
|
2021-01-07 15:39:20 +08:00
|
|
|
binaryVectorFieldData, bOk := value.(*storage.BinaryVectorFieldData)
|
|
|
|
if bOk {
|
|
|
|
err = it.index.BuildBinaryVecIndexWithoutIds(binaryVectorFieldData.Data)
|
|
|
|
if err != nil {
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Error("IndexNode BuildBinaryVecIndexWithoutIds failed", zap.Error(err))
|
2021-01-07 15:39:20 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-06-30 19:46:14 +08:00
|
|
|
tr.Record("build binary vector index done")
|
2021-01-07 15:39:20 +08:00
|
|
|
}
|
|
|
|
|
2021-01-07 16:13:28 +08:00
|
|
|
if !fOk && !bOk {
|
2021-01-07 15:39:20 +08:00
|
|
|
return errors.New("we expect FloatVectorFieldData or BinaryVectorFieldData")
|
2020-12-22 08:14:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
indexBlobs, err := it.index.Serialize()
|
|
|
|
if err != nil {
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Error("IndexNode index Serialize failed", zap.Error(err))
|
2020-12-22 08:14:36 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-06-30 19:46:14 +08:00
|
|
|
tr.Record("serialize index done")
|
2020-12-22 08:14:36 +08:00
|
|
|
|
|
|
|
var indexCodec storage.IndexCodec
|
2021-03-12 14:22:09 +08:00
|
|
|
serializedIndexBlobs, err := indexCodec.Serialize(getStorageBlobs(indexBlobs), indexParams, it.req.IndexName, it.req.IndexID)
|
2020-12-22 08:14:36 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-30 19:46:14 +08:00
|
|
|
tr.Record("serialize index codec done")
|
2020-12-22 08:14:36 +08:00
|
|
|
|
|
|
|
getSavePathByKey := func(key string) string {
|
|
|
|
// TODO: fix me, use more reasonable method
|
2021-05-27 22:24:29 +08:00
|
|
|
return strconv.Itoa(int(it.req.IndexBuildID)) + "/" + strconv.Itoa(int(it.req.Version)) + "/" + strconv.Itoa(int(partitionID)) + "/" + strconv.Itoa(int(segmentID)) + "/" + key
|
2020-12-22 08:14:36 +08:00
|
|
|
}
|
|
|
|
saveBlob := func(path string, value []byte) error {
|
|
|
|
return it.kv.Save(path, string(value))
|
|
|
|
}
|
|
|
|
|
2021-03-26 11:19:02 +08:00
|
|
|
it.savePaths = make([]string, len(serializedIndexBlobs))
|
|
|
|
saveIndexFile := func(idx int) error {
|
|
|
|
blob := serializedIndexBlobs[idx]
|
2020-12-25 11:10:31 +08:00
|
|
|
key, value := blob.Key, blob.Value
|
2021-03-26 11:19:02 +08:00
|
|
|
|
2020-12-22 08:14:36 +08:00
|
|
|
savePath := getSavePathByKey(key)
|
2021-03-26 11:19:02 +08:00
|
|
|
|
2021-05-27 22:24:29 +08:00
|
|
|
saveIndexFileFn := func() error {
|
|
|
|
v, err := it.etcdKV.Load(it.req.MetaPath)
|
|
|
|
if err != nil {
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexNode load meta failed", zap.Any("path", it.req.MetaPath), zap.Error(err))
|
2021-05-27 22:24:29 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
indexMeta := indexpb.IndexMeta{}
|
|
|
|
err = proto.UnmarshalText(v, &indexMeta)
|
|
|
|
if err != nil {
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexNode Unmarshal indexMeta error ", zap.Error(err))
|
2021-05-27 22:24:29 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-06-30 19:46:14 +08:00
|
|
|
//log.Debug("IndexNode Unmarshal indexMeta success ", zap.Any("meta", indexMeta))
|
2021-05-27 22:24:29 +08:00
|
|
|
if indexMeta.Version > it.req.Version {
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexNode try saveIndexFile failed req.Version is low", zap.Any("req.Version", it.req.Version),
|
|
|
|
zap.Any("indexMeta.Version", indexMeta.Version))
|
2021-05-27 22:24:29 +08:00
|
|
|
return errors.New("This task has been reassigned ")
|
|
|
|
}
|
|
|
|
return saveBlob(savePath, value)
|
|
|
|
}
|
2021-06-23 09:24:10 +08:00
|
|
|
err := retry.Do(ctx, saveIndexFileFn, retry.Attempts(5))
|
2021-06-06 09:41:35 +08:00
|
|
|
log.Debug("IndexNode try saveIndexFile final", zap.Error(err), zap.Any("savePath", savePath))
|
2020-12-22 08:14:36 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-26 11:19:02 +08:00
|
|
|
|
|
|
|
it.savePaths[idx] = savePath
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err = funcutil.ProcessFuncParallel(len(serializedIndexBlobs), runtime.NumCPU(), saveIndexFile, "saveIndexFile")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-12-22 08:14:36 +08:00
|
|
|
}
|
2021-06-30 19:46:14 +08:00
|
|
|
tr.Record("save index file done")
|
2020-12-22 08:14:36 +08:00
|
|
|
}
|
2021-07-23 10:44:12 +08:00
|
|
|
log.Debug("IndexNode CreateIndex finished")
|
2021-06-30 19:46:14 +08:00
|
|
|
tr.Elapse("all done")
|
2021-01-23 20:58:46 +08:00
|
|
|
return nil
|
2020-12-10 17:55:55 +08:00
|
|
|
}
|