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"
|
2020-12-22 08:14:36 +08:00
|
|
|
"strconv"
|
2020-12-13 06:48:05 +08:00
|
|
|
|
2021-03-10 09:56:09 +08:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2021-03-08 10:09:48 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/kv"
|
2021-03-10 09:56:09 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/log"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/indexpb"
|
2021-03-08 10:09:48 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/storage"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/types"
|
2021-02-07 21:32:37 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/funcutil"
|
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-01-26 09:38:40 +08:00
|
|
|
index Index
|
|
|
|
kv kv.Base
|
|
|
|
savePaths []string
|
2021-03-12 14:22:09 +08:00
|
|
|
req *indexpb.BuildIndexRequest
|
2021-03-08 10:09:48 +08:00
|
|
|
serviceClient types.IndexService
|
2021-01-26 09:38:40 +08:00
|
|
|
nodeID UniqueID
|
|
|
|
}
|
|
|
|
|
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-03-10 09:56:09 +08:00
|
|
|
log.Debug("indexnode", zap.Int64("[IndexBuilderTask] Enqueue TaskID", it.ID()))
|
2020-12-10 17:55:55 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:58:06 +08:00
|
|
|
func (it *IndexBuildTask) PreExecute(ctx context.Context) error {
|
2021-03-10 09:56:09 +08:00
|
|
|
log.Debug("preExecute...")
|
2020-12-10 17:55:55 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:58:06 +08:00
|
|
|
func (it *IndexBuildTask) PostExecute(ctx context.Context) error {
|
2021-03-10 09:56:09 +08:00
|
|
|
log.Debug("PostExecute...")
|
2021-02-27 10:45:03 +08:00
|
|
|
|
2021-01-26 09:38:40 +08:00
|
|
|
defer func() {
|
2021-02-27 10:45:03 +08:00
|
|
|
if it.err != nil {
|
2021-01-26 09:38:40 +08:00
|
|
|
it.Rollback()
|
2020-12-13 06:48:05 +08:00
|
|
|
}
|
2021-01-26 09:38:40 +08:00
|
|
|
}()
|
2020-12-13 06:48:05 +08:00
|
|
|
|
2021-01-26 09:38:40 +08:00
|
|
|
if it.serviceClient == nil {
|
2021-02-27 10:45:03 +08:00
|
|
|
err := errors.New("IndexBuildTask, serviceClient is nil")
|
2021-03-10 09:56:09 +08:00
|
|
|
log.Debug("[IndexBuildTask][PostExecute] serviceClient is nil")
|
2021-01-26 09:38:40 +08:00
|
|
|
return err
|
|
|
|
}
|
2020-12-10 17:55:55 +08:00
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
nty := &indexpb.NotifyBuildIndexRequest{
|
2021-01-26 09:38:40 +08:00
|
|
|
Status: &commonpb.Status{
|
2021-03-10 22:06:22 +08:00
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
2020-12-13 06:48:05 +08:00
|
|
|
},
|
2021-03-12 14:22:09 +08:00
|
|
|
IndexBuildID: it.req.IndexBuildID,
|
2021-01-26 09:38:40 +08:00
|
|
|
NodeID: it.nodeID,
|
|
|
|
IndexFilePaths: it.savePaths,
|
2020-12-13 06:48:05 +08:00
|
|
|
}
|
2021-02-27 10:45:03 +08:00
|
|
|
if it.err != nil {
|
2021-03-10 22:06:22 +08:00
|
|
|
nty.Status.ErrorCode = commonpb.ErrorCode_BuildIndexError
|
2021-02-27 10:45:03 +08:00
|
|
|
}
|
2020-12-10 17:55:55 +08:00
|
|
|
|
2021-03-04 10:35:28 +08:00
|
|
|
ctx = context.TODO()
|
2021-02-26 17:44:24 +08:00
|
|
|
resp, err := it.serviceClient.NotifyBuildIndex(ctx, nty)
|
2021-01-26 09:38:40 +08:00
|
|
|
if err != nil {
|
2021-03-10 09:56:09 +08:00
|
|
|
log.Warn("indexnode", zap.String("error", err.Error()))
|
2021-01-26 09:38:40 +08:00
|
|
|
return err
|
2020-12-13 06:48:05 +08:00
|
|
|
}
|
2020-12-10 17:55:55 +08:00
|
|
|
|
2021-03-10 22:06:22 +08:00
|
|
|
if resp.ErrorCode != commonpb.ErrorCode_Success {
|
2021-01-26 09:38:40 +08:00
|
|
|
err = errors.New(resp.Reason)
|
2021-03-12 19:47:37 +08:00
|
|
|
log.Debug("indexnode", zap.String("[IndexBuildTask][PostExecute] err", err.Error()))
|
2021-01-26 09:38:40 +08:00
|
|
|
}
|
|
|
|
return err
|
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-03-10 09:56:09 +08:00
|
|
|
log.Debug("start build index ...")
|
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-03-12 19:47:37 +08:00
|
|
|
log.Error("indexnode", zap.String("NewCIndex err:", err.Error()))
|
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-03-10 09:56:09 +08:00
|
|
|
log.Warn("CIndexDelete Failed")
|
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()
|
2020-12-22 08:14:36 +08:00
|
|
|
keys := make([]string, 0)
|
|
|
|
blobs := make([]*Blob, 0)
|
|
|
|
for _, path := range toLoadDataPaths {
|
|
|
|
keys = append(keys, getKeyByPathNaive(path))
|
|
|
|
blob, err := getBlobByPath(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
blobs = append(blobs, blob)
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-12 19:47:37 +08:00
|
|
|
log.Error("indexnode", zap.String("BuildFloatVecIndexWithoutIds error", err.Error()))
|
2021-01-07 15:39:20 +08:00
|
|
|
return err
|
|
|
|
}
|
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-03-12 19:47:37 +08:00
|
|
|
log.Error("indexnode", zap.String("BuildBinaryVecIndexWithoutIds err", err.Error()))
|
2021-01-07 15:39:20 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-03-12 19:47:37 +08:00
|
|
|
log.Error("indexnode", zap.String("serialize err", err.Error()))
|
2021-01-23 20:58:46 +08:00
|
|
|
|
2020-12-22 08:14:36 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
getSavePathByKey := func(key string) string {
|
|
|
|
// TODO: fix me, use more reasonable method
|
2021-03-12 14:22:09 +08:00
|
|
|
return strconv.Itoa(int(it.req.IndexBuildID)) + "/" + 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))
|
|
|
|
}
|
|
|
|
|
|
|
|
it.savePaths = make([]string, 0)
|
|
|
|
for _, blob := range serializedIndexBlobs {
|
2020-12-25 11:10:31 +08:00
|
|
|
key, value := blob.Key, blob.Value
|
2020-12-22 08:14:36 +08:00
|
|
|
savePath := getSavePathByKey(key)
|
|
|
|
err := saveBlob(savePath, value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
it.savePaths = append(it.savePaths, savePath)
|
|
|
|
}
|
|
|
|
}
|
2021-02-22 18:33:40 +08:00
|
|
|
// err = it.index.Delete()
|
|
|
|
// if err != nil {
|
|
|
|
// log.Print("CIndexDelete Failed")
|
|
|
|
// }
|
2021-01-23 20:58:46 +08:00
|
|
|
return nil
|
2020-12-10 17:55:55 +08:00
|
|
|
}
|
2021-02-23 09:58:06 +08:00
|
|
|
func (it *IndexBuildTask) Rollback() error {
|
|
|
|
|
|
|
|
if it.savePaths == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := it.kv.MultiRemove(it.savePaths)
|
|
|
|
if err != nil {
|
2021-03-10 09:56:09 +08:00
|
|
|
log.Warn("indexnode", zap.String("IndexBuildTask Rollback Failed", err.Error()))
|
2021-02-23 09:58:06 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|