2021-01-26 09:38:40 +08:00
|
|
|
package indexservice
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-08 10:09:48 +08:00
|
|
|
"errors"
|
2021-01-26 09:38:40 +08:00
|
|
|
"log"
|
|
|
|
|
2021-02-24 17:12:06 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/allocator"
|
2021-03-08 10:09:48 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/kv"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/types"
|
2021-02-24 17:12:06 +08:00
|
|
|
|
2021-01-26 09:38:40 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/indexpb"
|
|
|
|
)
|
|
|
|
|
2021-02-23 09:58:06 +08:00
|
|
|
const (
|
|
|
|
IndexAddTaskName = "IndexAddTask"
|
|
|
|
)
|
|
|
|
|
2021-01-26 09:38:40 +08:00
|
|
|
type task interface {
|
2021-02-23 09:58:06 +08:00
|
|
|
Ctx() context.Context
|
2021-01-26 09:38:40 +08:00
|
|
|
ID() UniqueID // return ReqID
|
|
|
|
SetID(uid UniqueID) // set ReqID
|
2021-02-23 09:58:06 +08:00
|
|
|
Name() string
|
|
|
|
PreExecute(ctx context.Context) error
|
|
|
|
Execute(ctx context.Context) error
|
|
|
|
PostExecute(ctx context.Context) error
|
2021-01-26 09:38:40 +08:00
|
|
|
WaitToFinish() error
|
|
|
|
Notify(err error)
|
|
|
|
OnEnqueue() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type BaseTask struct {
|
|
|
|
done chan error
|
|
|
|
ctx context.Context
|
|
|
|
id UniqueID
|
|
|
|
table *metaTable
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bt *BaseTask) ID() UniqueID {
|
|
|
|
return bt.id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bt *BaseTask) setID(id UniqueID) {
|
|
|
|
bt.id = id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bt *BaseTask) WaitToFinish() error {
|
|
|
|
select {
|
|
|
|
case <-bt.ctx.Done():
|
|
|
|
return errors.New("Task wait to finished timeout")
|
|
|
|
case err := <-bt.done:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bt *BaseTask) Notify(err error) {
|
|
|
|
bt.done <- err
|
|
|
|
}
|
|
|
|
|
|
|
|
type IndexAddTask struct {
|
|
|
|
BaseTask
|
|
|
|
req *indexpb.BuildIndexRequest
|
2021-02-02 19:56:04 +08:00
|
|
|
indexBuildID UniqueID
|
2021-02-24 17:12:06 +08:00
|
|
|
idAllocator *allocator.GlobalIDAllocator
|
2021-01-26 09:38:40 +08:00
|
|
|
buildQueue TaskQueue
|
|
|
|
kv kv.Base
|
2021-03-08 10:09:48 +08:00
|
|
|
builderClient types.IndexNode
|
2021-01-26 09:38:40 +08:00
|
|
|
nodeClients *PriorityQueue
|
|
|
|
buildClientNodeID UniqueID
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:58:06 +08:00
|
|
|
func (it *IndexAddTask) Ctx() context.Context {
|
|
|
|
return it.ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *IndexAddTask) ID() UniqueID {
|
|
|
|
return it.id
|
|
|
|
}
|
|
|
|
|
2021-01-26 09:38:40 +08:00
|
|
|
func (it *IndexAddTask) SetID(ID UniqueID) {
|
|
|
|
it.BaseTask.setID(ID)
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:58:06 +08:00
|
|
|
func (it *IndexAddTask) Name() string {
|
|
|
|
return IndexAddTaskName
|
|
|
|
}
|
|
|
|
|
2021-01-26 09:38:40 +08:00
|
|
|
func (it *IndexAddTask) OnEnqueue() error {
|
|
|
|
var err error
|
2021-02-02 19:56:04 +08:00
|
|
|
it.indexBuildID, err = it.idAllocator.AllocOne()
|
2021-01-26 09:38:40 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:58:06 +08:00
|
|
|
func (it *IndexAddTask) PreExecute(ctx context.Context) error {
|
2021-01-26 09:38:40 +08:00
|
|
|
log.Println("pretend to check Index Req")
|
|
|
|
nodeID, builderClient := it.nodeClients.PeekClient()
|
|
|
|
if builderClient == nil {
|
|
|
|
return errors.New("IndexAddTask Service not available")
|
|
|
|
}
|
|
|
|
it.builderClient = builderClient
|
|
|
|
it.buildClientNodeID = nodeID
|
2021-02-02 19:56:04 +08:00
|
|
|
err := it.table.AddIndex(it.indexBuildID, it.req)
|
2021-01-26 09:38:40 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:58:06 +08:00
|
|
|
func (it *IndexAddTask) Execute(ctx context.Context) error {
|
2021-01-26 09:38:40 +08:00
|
|
|
cmd := &indexpb.BuildIndexCmd{
|
2021-02-02 19:56:04 +08:00
|
|
|
IndexBuildID: it.indexBuildID,
|
|
|
|
Req: it.req,
|
2021-01-26 09:38:40 +08:00
|
|
|
}
|
|
|
|
log.Println("before index ...")
|
2021-02-26 17:44:24 +08:00
|
|
|
resp, err := it.builderClient.BuildIndex(ctx, cmd)
|
2021-01-26 09:38:40 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Println("build index finish, err = ", err)
|
2021-03-09 13:55:35 +08:00
|
|
|
if resp.ErrorCode != commonpb.ErrorCode_ERROR_CODE_SUCCESS {
|
2021-01-26 09:38:40 +08:00
|
|
|
return errors.New(resp.Reason)
|
|
|
|
}
|
|
|
|
it.nodeClients.IncPriority(it.buildClientNodeID, 1)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:58:06 +08:00
|
|
|
func (it *IndexAddTask) PostExecute(ctx context.Context) error {
|
2021-01-26 09:38:40 +08:00
|
|
|
return nil
|
|
|
|
}
|